blob: a80f1f96b9c607e05b514011f119b0e81dfc3062 [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 Vaskob4750962022-10-06 15:33:35 +020035#include "plugins_exts/metadata.h"
Michal Vasko03ff5a72019-09-11 13:49:33 +020036#include "plugins_types.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020037#include "printer_data.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020038#include "schema_compile_node.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020039#include "tree.h"
Radek Krejci77114102021-03-10 15:21:57 +010040#include "tree_data.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020041#include "tree_data_internal.h"
Radek Krejci859a15a2021-03-05 20:56:59 +010042#include "tree_edit.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020043#include "tree_schema_internal.h"
44#include "xml.h"
Michal Vasko03ff5a72019-09-11 13:49:33 +020045
Michal Vaskodd528af2022-08-08 14:35:07 +020046static LY_ERR reparse_or_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t depth);
47static LY_ERR eval_expr_select(const struct lyxp_expr *exp, uint32_t *tok_idx, enum lyxp_expr_type etype,
Michal Vasko40308e72020-10-20 16:38:40 +020048 struct lyxp_set *set, uint32_t options);
Michal Vaskofe1af042022-07-29 14:58:59 +020049static LY_ERR moveto_resolve_model(const char **qname, uint32_t *qname_len, const struct lyxp_set *set,
Michal Vasko93923692021-05-07 15:28:02 +020050 const struct lysc_node *ctx_scnode, const struct lys_module **moveto_mod);
Michal Vasko47da6562022-07-14 15:43:15 +020051static LY_ERR moveto_axis_node_next(const struct lyd_node **iter, enum lyxp_node_type *iter_type,
52 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 +020053static LY_ERR moveto_node(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname,
54 enum lyxp_axis axis, uint32_t options);
55static LY_ERR moveto_scnode(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname,
56 enum lyxp_axis axis, uint32_t options);
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +020057static LY_ERR moveto_op_comp(struct lyxp_set *set1, struct lyxp_set *set2, const char *op, ly_bool *result);
Michal Vasko03ff5a72019-09-11 13:49:33 +020058
aPiecek96dc1e32021-10-08 15:45:59 +020059/* Functions are divided into the following basic classes:
60 *
61 * (re)parse functions:
62 * Parse functions parse the expression into
63 * tokens (syntactic analysis).
64 * Reparse functions perform semantic analysis
65 * (do not save the result, just a check) of
66 * the expression and fill repeat indices.
67 *
68 * warn functions:
69 * Warn functions check specific reasonable conditions for schema XPath
70 * and print a warning if they are not satisfied.
71 *
72 * moveto functions:
73 * They and only they actually change the context (set).
74 *
75 * eval functions:
76 * They execute a parsed XPath expression on some data subtree.
77 */
78
Michal Vasko03ff5a72019-09-11 13:49:33 +020079/**
80 * @brief Print the type of an XPath \p set.
81 *
82 * @param[in] set Set to use.
83 * @return Set type string.
84 */
85static const char *
86print_set_type(struct lyxp_set *set)
87{
88 switch (set->type) {
Michal Vasko03ff5a72019-09-11 13:49:33 +020089 case LYXP_SET_NODE_SET:
90 return "node set";
91 case LYXP_SET_SCNODE_SET:
92 return "schema node set";
93 case LYXP_SET_BOOLEAN:
94 return "boolean";
95 case LYXP_SET_NUMBER:
96 return "number";
97 case LYXP_SET_STRING:
98 return "string";
99 }
100
101 return NULL;
102}
103
Michal Vasko24cddf82020-06-01 08:17:01 +0200104const char *
Michal Vasko49fec8e2022-05-24 10:28:33 +0200105lyxp_token2str(enum lyxp_token tok)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200106{
107 switch (tok) {
108 case LYXP_TOKEN_PAR1:
109 return "(";
110 case LYXP_TOKEN_PAR2:
111 return ")";
112 case LYXP_TOKEN_BRACK1:
113 return "[";
114 case LYXP_TOKEN_BRACK2:
115 return "]";
116 case LYXP_TOKEN_DOT:
117 return ".";
118 case LYXP_TOKEN_DDOT:
119 return "..";
120 case LYXP_TOKEN_AT:
121 return "@";
122 case LYXP_TOKEN_COMMA:
123 return ",";
Michal Vaskoe0a8ce72023-04-04 08:30:44 +0200124 case LYXP_TOKEN_DCOLON:
125 return "::";
Michal Vasko03ff5a72019-09-11 13:49:33 +0200126 case LYXP_TOKEN_NAMETEST:
127 return "NameTest";
128 case LYXP_TOKEN_NODETYPE:
129 return "NodeType";
aPiecekfba75362021-10-07 12:39:48 +0200130 case LYXP_TOKEN_VARREF:
131 return "VariableReference";
Michal Vasko03ff5a72019-09-11 13:49:33 +0200132 case LYXP_TOKEN_FUNCNAME:
133 return "FunctionName";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200134 case LYXP_TOKEN_OPER_LOG:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200135 return "Operator(Logic)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200136 case LYXP_TOKEN_OPER_EQUAL:
137 return "Operator(Equal)";
138 case LYXP_TOKEN_OPER_NEQUAL:
139 return "Operator(Non-equal)";
140 case LYXP_TOKEN_OPER_COMP:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200141 return "Operator(Comparison)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200142 case LYXP_TOKEN_OPER_MATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200143 return "Operator(Math)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200144 case LYXP_TOKEN_OPER_UNI:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200145 return "Operator(Union)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200146 case LYXP_TOKEN_OPER_PATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200147 return "Operator(Path)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200148 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko14676352020-05-29 11:35:55 +0200149 return "Operator(Recursive Path)";
Michal Vaskoe0a8ce72023-04-04 08:30:44 +0200150 case LYXP_TOKEN_AXISNAME:
151 return "AxisName";
Michal Vasko03ff5a72019-09-11 13:49:33 +0200152 case LYXP_TOKEN_LITERAL:
153 return "Literal";
154 case LYXP_TOKEN_NUMBER:
155 return "Number";
156 default:
157 LOGINT(NULL);
158 return "";
159 }
160}
161
162/**
Michal Vasko49fec8e2022-05-24 10:28:33 +0200163 * @brief Transform string into an axis.
164 *
165 * @param[in] str String to transform.
166 * @param[in] str_len Length of @p str.
167 * @return Transformed axis.
168 */
169static enum lyxp_axis
Michal Vaskodd528af2022-08-08 14:35:07 +0200170str2axis(const char *str, uint32_t str_len)
Michal Vasko49fec8e2022-05-24 10:28:33 +0200171{
172 switch (str_len) {
173 case 4:
174 assert(!strncmp("self", str, str_len));
175 return LYXP_AXIS_SELF;
176 case 5:
177 assert(!strncmp("child", str, str_len));
178 return LYXP_AXIS_CHILD;
179 case 6:
180 assert(!strncmp("parent", str, str_len));
181 return LYXP_AXIS_PARENT;
182 case 8:
183 assert(!strncmp("ancestor", str, str_len));
184 return LYXP_AXIS_ANCESTOR;
185 case 9:
186 if (str[0] == 'a') {
187 assert(!strncmp("attribute", str, str_len));
188 return LYXP_AXIS_ATTRIBUTE;
189 } else if (str[0] == 'f') {
190 assert(!strncmp("following", str, str_len));
191 return LYXP_AXIS_FOLLOWING;
192 } else {
193 assert(!strncmp("preceding", str, str_len));
194 return LYXP_AXIS_PRECEDING;
195 }
196 break;
197 case 10:
198 assert(!strncmp("descendant", str, str_len));
199 return LYXP_AXIS_DESCENDANT;
200 case 16:
201 assert(!strncmp("ancestor-or-self", str, str_len));
202 return LYXP_AXIS_ANCESTOR_OR_SELF;
203 case 17:
204 if (str[0] == 'f') {
205 assert(!strncmp("following-sibling", str, str_len));
206 return LYXP_AXIS_FOLLOWING_SIBLING;
207 } else {
208 assert(!strncmp("preceding-sibling", str, str_len));
209 return LYXP_AXIS_PRECEDING_SIBLING;
210 }
211 break;
212 case 18:
213 assert(!strncmp("descendant-or-self", str, str_len));
214 return LYXP_AXIS_DESCENDANT_OR_SELF;
215 }
216
217 LOGINT(NULL);
218 return 0;
219}
220
221/**
Michal Vasko03ff5a72019-09-11 13:49:33 +0200222 * @brief Print the whole expression \p exp to debug output.
223 *
224 * @param[in] exp Expression to use.
225 */
226static void
Michal Vasko40308e72020-10-20 16:38:40 +0200227print_expr_struct_debug(const struct lyxp_expr *exp)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200228{
Radek Krejcif13b87b2020-12-01 22:02:17 +0100229#define MSG_BUFFER_SIZE 128
230 char tmp[MSG_BUFFER_SIZE];
Michal Vasko1fdd8fa2021-01-08 09:21:45 +0100231 uint32_t i, j;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200232
Radek Krejci52b6d512020-10-12 12:33:17 +0200233 if (!exp || (ly_ll < LY_LLDBG)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200234 return;
235 }
236
237 LOGDBG(LY_LDGXPATH, "expression \"%s\":", exp->expr);
238 for (i = 0; i < exp->used; ++i) {
Michal Vasko49fec8e2022-05-24 10:28:33 +0200239 sprintf(tmp, "\ttoken %s, in expression \"%.*s\"", lyxp_token2str(exp->tokens[i]), exp->tok_len[i],
Michal Vasko69730152020-10-09 16:30:07 +0200240 &exp->expr[exp->tok_pos[i]]);
Michal Vasko23049552021-03-04 15:57:44 +0100241 if (exp->repeat && exp->repeat[i]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200242 sprintf(tmp + strlen(tmp), " (repeat %d", exp->repeat[i][0]);
243 for (j = 1; exp->repeat[i][j]; ++j) {
244 sprintf(tmp + strlen(tmp), ", %d", exp->repeat[i][j]);
245 }
246 strcat(tmp, ")");
247 }
248 LOGDBG(LY_LDGXPATH, tmp);
249 }
Radek Krejcif13b87b2020-12-01 22:02:17 +0100250#undef MSG_BUFFER_SIZE
Michal Vasko03ff5a72019-09-11 13:49:33 +0200251}
252
253#ifndef NDEBUG
254
255/**
256 * @brief Print XPath set content to debug output.
257 *
258 * @param[in] set Set to print.
259 */
260static void
261print_set_debug(struct lyxp_set *set)
262{
263 uint32_t i;
264 char *str;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200265 struct lyxp_set_node *item;
266 struct lyxp_set_scnode *sitem;
267
Radek Krejci52b6d512020-10-12 12:33:17 +0200268 if (ly_ll < LY_LLDBG) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200269 return;
270 }
271
272 switch (set->type) {
273 case LYXP_SET_NODE_SET:
274 LOGDBG(LY_LDGXPATH, "set NODE SET:");
275 for (i = 0; i < set->used; ++i) {
276 item = &set->val.nodes[i];
277
278 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +0100279 case LYXP_NODE_NONE:
280 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): NONE", i + 1, item->pos);
281 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200282 case LYXP_NODE_ROOT:
283 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT", i + 1, item->pos);
284 break;
285 case LYXP_NODE_ROOT_CONFIG:
286 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT CONFIG", i + 1, item->pos);
287 break;
288 case LYXP_NODE_ELEM:
Michal Vasko222be682023-08-24 08:17:12 +0200289 if (item->node->schema && (item->node->schema->nodetype == LYS_LIST) &&
290 (lyd_child(item->node)->schema->nodetype == LYS_LEAF)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200291 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (1st child val: %s)", i + 1, item->pos,
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200292 item->node->schema->name, lyd_get_value(lyd_child(item->node)));
Michal Vasko222be682023-08-24 08:17:12 +0200293 } else if ((!item->node->schema && !lyd_child(item->node)) || (item->node->schema->nodetype == LYS_LEAFLIST)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200294 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (val: %s)", i + 1, item->pos,
Michal Vasko222be682023-08-24 08:17:12 +0200295 LYD_NAME(item->node), lyd_get_value(item->node));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200296 } else {
Michal Vasko222be682023-08-24 08:17:12 +0200297 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s", i + 1, item->pos, LYD_NAME(item->node));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200298 }
299 break;
300 case LYXP_NODE_TEXT:
Michal Vasko222be682023-08-24 08:17:12 +0200301 if (item->node->schema && (item->node->schema->nodetype & LYS_ANYDATA)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200302 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): TEXT <%s>", i + 1, item->pos,
Michal Vasko69730152020-10-09 16:30:07 +0200303 item->node->schema->nodetype == LYS_ANYXML ? "anyxml" : "anydata");
Michal Vasko03ff5a72019-09-11 13:49:33 +0200304 } else {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200305 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 +0200306 }
307 break;
Michal Vasko9f96a052020-03-10 09:41:45 +0100308 case LYXP_NODE_META:
309 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 +0200310 set->val.meta[i].meta->value);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200311 break;
312 }
313 }
314 break;
315
316 case LYXP_SET_SCNODE_SET:
317 LOGDBG(LY_LDGXPATH, "set SCNODE SET:");
318 for (i = 0; i < set->used; ++i) {
319 sitem = &set->val.scnodes[i];
320
321 switch (sitem->type) {
322 case LYXP_NODE_ROOT:
323 LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT", i + 1, sitem->in_ctx);
324 break;
325 case LYXP_NODE_ROOT_CONFIG:
326 LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT CONFIG", i + 1, sitem->in_ctx);
327 break;
328 case LYXP_NODE_ELEM:
329 LOGDBG(LY_LDGXPATH, "\t%d (%u): ELEM %s", i + 1, sitem->in_ctx, sitem->scnode->name);
330 break;
331 default:
332 LOGINT(NULL);
333 break;
334 }
335 }
336 break;
337
Michal Vasko03ff5a72019-09-11 13:49:33 +0200338 case LYXP_SET_BOOLEAN:
339 LOGDBG(LY_LDGXPATH, "set BOOLEAN");
Michal Vasko004d3152020-06-11 19:59:22 +0200340 LOGDBG(LY_LDGXPATH, "\t%s", (set->val.bln ? "true" : "false"));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200341 break;
342
343 case LYXP_SET_STRING:
344 LOGDBG(LY_LDGXPATH, "set STRING");
345 LOGDBG(LY_LDGXPATH, "\t%s", set->val.str);
346 break;
347
348 case LYXP_SET_NUMBER:
349 LOGDBG(LY_LDGXPATH, "set NUMBER");
350
351 if (isnan(set->val.num)) {
352 str = strdup("NaN");
353 } else if ((set->val.num == 0) || (set->val.num == -0.0f)) {
354 str = strdup("0");
355 } else if (isinf(set->val.num) && !signbit(set->val.num)) {
356 str = strdup("Infinity");
357 } else if (isinf(set->val.num) && signbit(set->val.num)) {
358 str = strdup("-Infinity");
359 } else if ((long long)set->val.num == set->val.num) {
360 if (asprintf(&str, "%lld", (long long)set->val.num) == -1) {
361 str = NULL;
362 }
363 } else {
364 if (asprintf(&str, "%03.1Lf", set->val.num) == -1) {
365 str = NULL;
366 }
367 }
368 LY_CHECK_ERR_RET(!str, LOGMEM(NULL), );
369
370 LOGDBG(LY_LDGXPATH, "\t%s", str);
371 free(str);
372 }
373}
374
375#endif
376
377/**
378 * @brief Realloc the string \p str.
379 *
380 * @param[in] ctx libyang context for logging.
381 * @param[in] needed How much free space is required.
382 * @param[in,out] str Pointer to the string to use.
383 * @param[in,out] used Used bytes in \p str.
384 * @param[in,out] size Allocated bytes in \p str.
385 * @return LY_ERR
386 */
387static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +0200388cast_string_realloc(const struct ly_ctx *ctx, uint64_t needed, char **str, uint32_t *used, uint32_t *size)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200389{
Michal Vasko2291ab92022-08-08 08:53:12 +0200390 if (*size - (unsigned)*used < needed) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200391 do {
Michal Vaskodd528af2022-08-08 14:35:07 +0200392 if ((UINT32_MAX - *size) < LYXP_STRING_CAST_SIZE_STEP) {
393 LOGERR(ctx, LY_EINVAL, "XPath string length limit (%" PRIu32 ") reached.", UINT32_MAX);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200394 return LY_EINVAL;
395 }
396 *size += LYXP_STRING_CAST_SIZE_STEP;
Michal Vasko2291ab92022-08-08 08:53:12 +0200397 } while (*size - (unsigned)*used < needed);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200398 *str = ly_realloc(*str, *size * sizeof(char));
399 LY_CHECK_ERR_RET(!(*str), LOGMEM(ctx), LY_EMEM);
400 }
401
402 return LY_SUCCESS;
403}
404
405/**
406 * @brief Cast nodes recursively to one string @p str.
407 *
Michal Vasko47da6562022-07-14 15:43:15 +0200408 * @param[in] node Node to cast, NULL if root.
409 * @param[in] set XPath set.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200410 * @param[in] indent Current indent.
411 * @param[in,out] str Resulting string.
412 * @param[in,out] used Used bytes in @p str.
413 * @param[in,out] size Allocated bytes in @p str.
Michal Vasko47da6562022-07-14 15:43:15 +0200414 * @return LY_ERR value.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200415 */
416static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +0200417cast_string_recursive(const struct lyd_node *node, struct lyxp_set *set, uint32_t indent, char **str, uint32_t *used,
418 uint32_t *size)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200419{
Radek Krejci7f769d72020-07-11 23:13:56 +0200420 char *buf, *line, *ptr = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200421 const char *value_str;
Michal Vasko222be682023-08-24 08:17:12 +0200422 uint16_t nodetype;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200423 const struct lyd_node *child;
Michal Vasko47da6562022-07-14 15:43:15 +0200424 enum lyxp_node_type child_type;
Michal Vasko60ea6352020-06-29 13:39:39 +0200425 struct lyd_node *tree;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200426 struct lyd_node_any *any;
427 LY_ERR rc;
428
Michal Vasko222be682023-08-24 08:17:12 +0200429 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && node && node->schema && (node->schema->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200430 return LY_SUCCESS;
431 }
432
Michal Vasko47da6562022-07-14 15:43:15 +0200433 if (!node) {
434 /* fake container */
435 LY_CHECK_RET(cast_string_realloc(set->ctx, 1, str, used, size));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200436 strcpy(*str + (*used - 1), "\n");
437 ++(*used);
438
439 ++indent;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200440
Michal Vasko47da6562022-07-14 15:43:15 +0200441 /* print all the top-level nodes */
442 child = NULL;
443 child_type = 0;
444 while (!moveto_axis_node_next(&child, &child_type, NULL, set->root_type, LYXP_AXIS_CHILD, set)) {
445 LY_CHECK_RET(cast_string_recursive(child, set, indent, str, used, size));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200446 }
447
Michal Vasko47da6562022-07-14 15:43:15 +0200448 /* end fake container */
449 LY_CHECK_RET(cast_string_realloc(set->ctx, 1, str, used, size));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200450 strcpy(*str + (*used - 1), "\n");
451 ++(*used);
452
453 --indent;
Michal Vasko47da6562022-07-14 15:43:15 +0200454 } else {
Michal Vasko222be682023-08-24 08:17:12 +0200455 if (node->schema) {
456 nodetype = node->schema->nodetype;
457 } else if (lyd_child(node)) {
458 nodetype = LYS_CONTAINER;
459 } else {
460 nodetype = LYS_LEAF;
461 }
462
463 switch (nodetype) {
Michal Vasko47da6562022-07-14 15:43:15 +0200464 case LYS_CONTAINER:
465 case LYS_LIST:
466 case LYS_RPC:
467 case LYS_NOTIF:
468 LY_CHECK_RET(cast_string_realloc(set->ctx, 1, str, used, size));
469 strcpy(*str + (*used - 1), "\n");
470 ++(*used);
471
472 for (child = lyd_child(node); child; child = child->next) {
473 LY_CHECK_RET(cast_string_recursive(child, set, indent + 1, str, used, size));
474 }
475
476 break;
477
478 case LYS_LEAF:
479 case LYS_LEAFLIST:
480 value_str = lyd_get_value(node);
481
482 /* print indent */
483 LY_CHECK_RET(cast_string_realloc(set->ctx, indent * 2 + strlen(value_str) + 1, str, used, size));
484 memset(*str + (*used - 1), ' ', indent * 2);
485 *used += indent * 2;
486
487 /* print value */
488 if (*used == 1) {
489 sprintf(*str + (*used - 1), "%s", value_str);
490 *used += strlen(value_str);
491 } else {
492 sprintf(*str + (*used - 1), "%s\n", value_str);
493 *used += strlen(value_str) + 1;
494 }
495
496 break;
497
498 case LYS_ANYXML:
499 case LYS_ANYDATA:
500 any = (struct lyd_node_any *)node;
501 if (!(void *)any->value.tree) {
502 /* no content */
503 buf = strdup("");
504 LY_CHECK_ERR_RET(!buf, LOGMEM(set->ctx), LY_EMEM);
505 } else {
506 struct ly_out *out;
507
508 if (any->value_type == LYD_ANYDATA_LYB) {
509 /* try to parse it into a data tree */
510 if (lyd_parse_data_mem((struct ly_ctx *)set->ctx, any->value.mem, LYD_LYB,
511 LYD_PARSE_ONLY | LYD_PARSE_STRICT, 0, &tree) == LY_SUCCESS) {
512 /* successfully parsed */
513 free(any->value.mem);
514 any->value.tree = tree;
515 any->value_type = LYD_ANYDATA_DATATREE;
516 }
517 /* error is covered by the following switch where LYD_ANYDATA_LYB causes failure */
518 }
519
520 switch (any->value_type) {
521 case LYD_ANYDATA_STRING:
522 case LYD_ANYDATA_XML:
523 case LYD_ANYDATA_JSON:
524 buf = strdup(any->value.json);
525 LY_CHECK_ERR_RET(!buf, LOGMEM(set->ctx), LY_EMEM);
526 break;
527 case LYD_ANYDATA_DATATREE:
528 LY_CHECK_RET(ly_out_new_memory(&buf, 0, &out));
529 rc = lyd_print_all(out, any->value.tree, LYD_XML, 0);
530 ly_out_free(out, NULL, 0);
531 LY_CHECK_RET(rc < 0, -rc);
532 break;
533 case LYD_ANYDATA_LYB:
534 LOGERR(set->ctx, LY_EINVAL, "Cannot convert LYB anydata into string.");
535 return LY_EINVAL;
536 }
537 }
538
539 line = strtok_r(buf, "\n", &ptr);
540 do {
541 rc = cast_string_realloc(set->ctx, indent * 2 + strlen(line) + 1, str, used, size);
542 if (rc != LY_SUCCESS) {
543 free(buf);
544 return rc;
545 }
546 memset(*str + (*used - 1), ' ', indent * 2);
547 *used += indent * 2;
548
549 strcpy(*str + (*used - 1), line);
550 *used += strlen(line);
551
552 strcpy(*str + (*used - 1), "\n");
553 *used += 1;
554 } while ((line = strtok_r(NULL, "\n", &ptr)));
555
556 free(buf);
557 break;
558
559 default:
560 LOGINT_RET(set->ctx);
561 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200562 }
563
564 return LY_SUCCESS;
565}
566
567/**
568 * @brief Cast an element into a string.
569 *
Michal Vasko47da6562022-07-14 15:43:15 +0200570 * @param[in] node Node to cast, NULL if root.
571 * @param[in] set XPath set.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200572 * @param[out] str Element cast to dynamically-allocated string.
573 * @return LY_ERR
574 */
575static LY_ERR
Michal Vasko47da6562022-07-14 15:43:15 +0200576cast_string_elem(const struct lyd_node *node, struct lyxp_set *set, char **str)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200577{
Michal Vaskodd528af2022-08-08 14:35:07 +0200578 uint32_t used, size;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200579 LY_ERR rc;
580
581 *str = malloc(LYXP_STRING_CAST_SIZE_START * sizeof(char));
Michal Vasko47da6562022-07-14 15:43:15 +0200582 LY_CHECK_ERR_RET(!*str, LOGMEM(set->ctx), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200583 (*str)[0] = '\0';
584 used = 1;
585 size = LYXP_STRING_CAST_SIZE_START;
586
Michal Vasko47da6562022-07-14 15:43:15 +0200587 rc = cast_string_recursive(node, set, 0, str, &used, &size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200588 if (rc != LY_SUCCESS) {
589 free(*str);
590 return rc;
591 }
592
593 if (size > used) {
594 *str = ly_realloc(*str, used * sizeof(char));
Michal Vasko47da6562022-07-14 15:43:15 +0200595 LY_CHECK_ERR_RET(!*str, LOGMEM(set->ctx), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200596 }
597 return LY_SUCCESS;
598}
599
600/**
601 * @brief Cast a LYXP_SET_NODE_SET set into a string.
602 * Context position aware.
603 *
604 * @param[in] set Set to cast.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200605 * @param[out] str Cast dynamically-allocated string.
606 * @return LY_ERR
607 */
608static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100609cast_node_set_to_string(struct lyxp_set *set, char **str)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200610{
Michal Vaskoaa677062021-03-09 13:52:53 +0100611 if (!set->used) {
612 *str = strdup("");
613 if (!*str) {
614 LOGMEM_RET(set->ctx);
615 }
616 return LY_SUCCESS;
617 }
618
Michal Vasko03ff5a72019-09-11 13:49:33 +0200619 switch (set->val.nodes[0].type) {
Michal Vasko2caefc12019-11-14 16:07:56 +0100620 case LYXP_NODE_NONE:
621 /* invalid */
622 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200623 case LYXP_NODE_ROOT:
624 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200625 case LYXP_NODE_ELEM:
626 case LYXP_NODE_TEXT:
Michal Vasko47da6562022-07-14 15:43:15 +0200627 return cast_string_elem(set->val.nodes[0].node, set, str);
Michal Vasko9f96a052020-03-10 09:41:45 +0100628 case LYXP_NODE_META:
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200629 *str = strdup(lyd_get_meta_value(set->val.meta[0].meta));
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200630 if (!*str) {
631 LOGMEM_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200632 }
633 return LY_SUCCESS;
634 }
635
636 LOGINT_RET(set->ctx);
637}
638
639/**
640 * @brief Cast a string into an XPath number.
641 *
642 * @param[in] str String to use.
643 * @return Cast number.
644 */
645static long double
646cast_string_to_number(const char *str)
647{
648 long double num;
649 char *ptr;
650
651 errno = 0;
652 num = strtold(str, &ptr);
653 if (errno || *ptr) {
654 num = NAN;
655 }
656 return num;
657}
658
659/**
660 * @brief Callback for checking value equality.
661 *
Michal Vasko62524a92021-02-26 10:08:50 +0100662 * Implementation of ::lyht_value_equal_cb.
Radek Krejci857189e2020-09-01 13:26:36 +0200663 *
Michal Vasko03ff5a72019-09-11 13:49:33 +0200664 * @param[in] val1_p First value.
665 * @param[in] val2_p Second value.
666 * @param[in] mod Whether hash table is being modified.
667 * @param[in] cb_data Callback data.
Radek Krejci857189e2020-09-01 13:26:36 +0200668 * @return Boolean value whether values are equal or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200669 */
Radek Krejci857189e2020-09-01 13:26:36 +0200670static ly_bool
671set_values_equal_cb(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void *UNUSED(cb_data))
Michal Vasko03ff5a72019-09-11 13:49:33 +0200672{
673 struct lyxp_set_hash_node *val1, *val2;
674
675 val1 = (struct lyxp_set_hash_node *)val1_p;
676 val2 = (struct lyxp_set_hash_node *)val2_p;
677
678 if ((val1->node == val2->node) && (val1->type == val2->type)) {
679 return 1;
680 }
681
682 return 0;
683}
684
685/**
686 * @brief Insert node and its hash into set.
687 *
688 * @param[in] set et to insert to.
689 * @param[in] node Node with hash.
690 * @param[in] type Node type.
691 */
692static void
693set_insert_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type)
694{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200695 LY_ERR r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200696 uint32_t i, hash;
697 struct lyxp_set_hash_node hnode;
698
699 if (!set->ht && (set->used >= LYD_HT_MIN_ITEMS)) {
700 /* create hash table and add all the nodes */
701 set->ht = lyht_new(1, sizeof(struct lyxp_set_hash_node), set_values_equal_cb, NULL, 1);
702 for (i = 0; i < set->used; ++i) {
703 hnode.node = set->val.nodes[i].node;
704 hnode.type = set->val.nodes[i].type;
705
Michal Vaskoae130f52023-04-20 14:25:16 +0200706 hash = lyht_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
707 hash = lyht_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
708 hash = lyht_hash_multi(hash, NULL, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200709
710 r = lyht_insert(set->ht, &hnode, hash, NULL);
711 assert(!r);
712 (void)r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200713
Michal Vasko513b87d2023-08-04 14:03:40 +0200714 if ((hnode.node == node) && (hnode.type == type)) {
Michal Vasko9d6befd2019-12-11 14:56:56 +0100715 /* it was just added, do not add it twice */
Michal Vasko513b87d2023-08-04 14:03:40 +0200716 return;
Michal Vasko9d6befd2019-12-11 14:56:56 +0100717 }
718 }
719 }
720
Michal Vasko513b87d2023-08-04 14:03:40 +0200721 if (set->ht) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200722 /* add the new node into hash table */
723 hnode.node = node;
724 hnode.type = type;
725
Michal Vaskoae130f52023-04-20 14:25:16 +0200726 hash = lyht_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
727 hash = lyht_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
728 hash = lyht_hash_multi(hash, NULL, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200729
730 r = lyht_insert(set->ht, &hnode, hash, NULL);
731 assert(!r);
732 (void)r;
733 }
734}
735
736/**
737 * @brief Remove node and its hash from set.
738 *
739 * @param[in] set Set to remove from.
740 * @param[in] node Node to remove.
741 * @param[in] type Node type.
742 */
743static void
744set_remove_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type)
745{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200746 LY_ERR r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200747 struct lyxp_set_hash_node hnode;
748 uint32_t hash;
749
750 if (set->ht) {
751 hnode.node = node;
752 hnode.type = type;
753
Michal Vaskoae130f52023-04-20 14:25:16 +0200754 hash = lyht_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
755 hash = lyht_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
756 hash = lyht_hash_multi(hash, NULL, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200757
758 r = lyht_remove(set->ht, &hnode, hash);
759 assert(!r);
760 (void)r;
761
762 if (!set->ht->used) {
Michal Vasko77b7f90a2023-01-31 15:42:41 +0100763 lyht_free(set->ht, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200764 set->ht = NULL;
765 }
766 }
767}
768
769/**
770 * @brief Check whether node is in set based on its hash.
771 *
772 * @param[in] set Set to search in.
773 * @param[in] node Node to search for.
774 * @param[in] type Node type.
775 * @param[in] skip_idx Index in @p set to skip.
776 * @return LY_ERR
777 */
778static LY_ERR
779set_dup_node_hash_check(const struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type, int skip_idx)
780{
781 struct lyxp_set_hash_node hnode, *match_p;
782 uint32_t hash;
783
784 hnode.node = node;
785 hnode.type = type;
786
Michal Vaskoae130f52023-04-20 14:25:16 +0200787 hash = lyht_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
788 hash = lyht_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
789 hash = lyht_hash_multi(hash, NULL, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200790
791 if (!lyht_find(set->ht, &hnode, hash, (void **)&match_p)) {
792 if ((skip_idx > -1) && (set->val.nodes[skip_idx].node == match_p->node) && (set->val.nodes[skip_idx].type == match_p->type)) {
793 /* we found it on the index that should be skipped, find another */
794 hnode = *match_p;
795 if (lyht_find_next(set->ht, &hnode, hash, (void **)&match_p)) {
796 /* none other found */
797 return LY_SUCCESS;
798 }
799 }
800
801 return LY_EEXIST;
802 }
803
804 /* not found */
805 return LY_SUCCESS;
806}
807
Michal Vaskod3678892020-05-21 10:06:58 +0200808void
809lyxp_set_free_content(struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200810{
811 if (!set) {
812 return;
813 }
814
815 if (set->type == LYXP_SET_NODE_SET) {
816 free(set->val.nodes);
Michal Vasko77b7f90a2023-01-31 15:42:41 +0100817 lyht_free(set->ht, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200818 } else if (set->type == LYXP_SET_SCNODE_SET) {
819 free(set->val.scnodes);
Michal Vasko77b7f90a2023-01-31 15:42:41 +0100820 lyht_free(set->ht, NULL);
Michal Vaskod3678892020-05-21 10:06:58 +0200821 } else {
822 if (set->type == LYXP_SET_STRING) {
823 free(set->val.str);
824 }
825 set->type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200826 }
Michal Vaskod3678892020-05-21 10:06:58 +0200827
828 set->val.nodes = NULL;
829 set->used = 0;
830 set->size = 0;
831 set->ht = NULL;
832 set->ctx_pos = 0;
aPiecek748da732021-06-01 09:56:43 +0200833 set->ctx_size = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200834}
835
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100836/**
837 * @brief Free dynamically-allocated set.
838 *
839 * @param[in] set Set to free.
840 */
841static void
Michal Vasko03ff5a72019-09-11 13:49:33 +0200842lyxp_set_free(struct lyxp_set *set)
843{
844 if (!set) {
845 return;
846 }
847
Michal Vaskod3678892020-05-21 10:06:58 +0200848 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200849 free(set);
850}
851
852/**
853 * @brief Initialize set context.
854 *
855 * @param[in] new Set to initialize.
856 * @param[in] set Arbitrary initialized set.
857 */
858static void
Michal Vasko4c7763f2020-07-27 17:40:37 +0200859set_init(struct lyxp_set *new, const struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200860{
861 memset(new, 0, sizeof *new);
Michal Vasko02a77382019-09-12 11:47:35 +0200862 if (set) {
Michal Vasko306e2832022-07-25 09:15:17 +0200863 new->non_child_axis = set->non_child_axis;
Michal Vasko02a77382019-09-12 11:47:35 +0200864 new->ctx = set->ctx;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200865 new->cur_node = set->cur_node;
Michal Vasko588112f2019-12-10 14:51:53 +0100866 new->root_type = set->root_type;
Michal Vasko6b26e742020-07-17 15:02:10 +0200867 new->context_op = set->context_op;
Michal Vaskof03ed032020-03-04 13:31:44 +0100868 new->tree = set->tree;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200869 new->cur_mod = set->cur_mod;
Michal Vasko02a77382019-09-12 11:47:35 +0200870 new->format = set->format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200871 new->prefix_data = set->prefix_data;
aPiecekfba75362021-10-07 12:39:48 +0200872 new->vars = set->vars;
Michal Vasko02a77382019-09-12 11:47:35 +0200873 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200874}
875
876/**
877 * @brief Create a deep copy of a set.
878 *
879 * @param[in] set Set to copy.
880 * @return Copy of @p set.
881 */
882static struct lyxp_set *
883set_copy(struct lyxp_set *set)
884{
885 struct lyxp_set *ret;
Michal Vasko1fdd8fa2021-01-08 09:21:45 +0100886 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200887
888 if (!set) {
889 return NULL;
890 }
891
892 ret = malloc(sizeof *ret);
893 LY_CHECK_ERR_RET(!ret, LOGMEM(set->ctx), NULL);
894 set_init(ret, set);
895
896 if (set->type == LYXP_SET_SCNODE_SET) {
897 ret->type = set->type;
898
899 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100900 if ((set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) ||
901 (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +0200902 uint32_t idx;
Michal Vasko26bbb272022-08-02 14:54:33 +0200903
Michal Vaskoe4a6d012023-05-22 14:34:52 +0200904 LY_CHECK_ERR_RET(lyxp_set_scnode_insert_node(ret, set->val.scnodes[i].scnode, set->val.scnodes[i].type,
Michal Vasko7333cb32022-07-29 16:30:29 +0200905 set->val.scnodes[i].axis, &idx), lyxp_set_free(ret), NULL);
Michal Vasko3f27c522020-01-06 08:37:49 +0100906 /* coverity seems to think scnodes can be NULL */
Radek Krejciaa6b53f2020-08-27 15:19:03 +0200907 if (!ret->val.scnodes) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200908 lyxp_set_free(ret);
909 return NULL;
910 }
Michal Vaskoba716542019-12-16 10:01:58 +0100911 ret->val.scnodes[idx].in_ctx = set->val.scnodes[i].in_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200912 }
913 }
914 } else if (set->type == LYXP_SET_NODE_SET) {
915 ret->type = set->type;
Michal Vasko08e9b112021-06-11 15:41:17 +0200916 if (set->used) {
917 ret->val.nodes = malloc(set->used * sizeof *ret->val.nodes);
918 LY_CHECK_ERR_RET(!ret->val.nodes, LOGMEM(set->ctx); free(ret), NULL);
919 memcpy(ret->val.nodes, set->val.nodes, set->used * sizeof *ret->val.nodes);
920 } else {
921 ret->val.nodes = NULL;
922 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200923
924 ret->used = ret->size = set->used;
925 ret->ctx_pos = set->ctx_pos;
926 ret->ctx_size = set->ctx_size;
Michal Vasko4a04e542021-02-01 08:58:30 +0100927 if (set->ht) {
928 ret->ht = lyht_dup(set->ht);
929 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200930 } else {
Radek Krejci0f969882020-08-21 16:56:47 +0200931 memcpy(ret, set, sizeof *ret);
932 if (set->type == LYXP_SET_STRING) {
933 ret->val.str = strdup(set->val.str);
934 LY_CHECK_ERR_RET(!ret->val.str, LOGMEM(set->ctx); free(ret), NULL);
935 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200936 }
937
938 return ret;
939}
940
941/**
942 * @brief Fill XPath set with a string. Any current data are disposed of.
943 *
944 * @param[in] set Set to fill.
945 * @param[in] string String to fill into \p set.
946 * @param[in] str_len Length of \p string. 0 is a valid value!
947 */
948static void
Michal Vaskodd528af2022-08-08 14:35:07 +0200949set_fill_string(struct lyxp_set *set, const char *string, uint32_t str_len)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200950{
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_STRING;
954 if ((str_len == 0) && (string[0] != '\0')) {
955 string = "";
956 }
957 set->val.str = strndup(string, str_len);
958}
959
960/**
961 * @brief Fill XPath set with a number. Any current data are disposed of.
962 *
963 * @param[in] set Set to fill.
964 * @param[in] number Number to fill into \p set.
965 */
966static void
967set_fill_number(struct lyxp_set *set, long double number)
968{
Michal Vaskod3678892020-05-21 10:06:58 +0200969 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200970
971 set->type = LYXP_SET_NUMBER;
972 set->val.num = number;
973}
974
975/**
976 * @brief Fill XPath set with a boolean. Any current data are disposed of.
977 *
978 * @param[in] set Set to fill.
979 * @param[in] boolean Boolean to fill into \p set.
980 */
981static void
Radek Krejci857189e2020-09-01 13:26:36 +0200982set_fill_boolean(struct lyxp_set *set, ly_bool boolean)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200983{
Michal Vaskod3678892020-05-21 10:06:58 +0200984 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200985
986 set->type = LYXP_SET_BOOLEAN;
Michal Vasko004d3152020-06-11 19:59:22 +0200987 set->val.bln = boolean;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200988}
989
990/**
991 * @brief Fill XPath set with the value from another set (deep assign).
992 * Any current data are disposed of.
993 *
994 * @param[in] trg Set to fill.
995 * @param[in] src Source set to copy into \p trg.
996 */
997static void
Michal Vasko4c7763f2020-07-27 17:40:37 +0200998set_fill_set(struct lyxp_set *trg, const struct lyxp_set *src)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200999{
1000 if (!trg || !src) {
1001 return;
1002 }
1003
Michal Vaskof06d19d2023-08-04 14:04:08 +02001004 lyxp_set_free_content(trg);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001005 set_init(trg, src);
1006
1007 if (src->type == LYXP_SET_SCNODE_SET) {
1008 trg->type = LYXP_SET_SCNODE_SET;
1009 trg->used = src->used;
1010 trg->size = src->used;
1011
Michal Vasko08e9b112021-06-11 15:41:17 +02001012 if (trg->size) {
1013 trg->val.scnodes = ly_realloc(trg->val.scnodes, trg->size * sizeof *trg->val.scnodes);
1014 LY_CHECK_ERR_RET(!trg->val.scnodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), );
1015 memcpy(trg->val.scnodes, src->val.scnodes, src->used * sizeof *src->val.scnodes);
1016 } else {
1017 trg->val.scnodes = NULL;
1018 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001019 } else if (src->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02001020 set_fill_boolean(trg, src->val.bln);
Michal Vasko44f3d2c2020-08-24 09:49:38 +02001021 } else if (src->type == LYXP_SET_NUMBER) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001022 set_fill_number(trg, src->val.num);
1023 } else if (src->type == LYXP_SET_STRING) {
1024 set_fill_string(trg, src->val.str, strlen(src->val.str));
1025 } else {
1026 if (trg->type == LYXP_SET_NODE_SET) {
1027 free(trg->val.nodes);
1028 } else if (trg->type == LYXP_SET_STRING) {
1029 free(trg->val.str);
1030 }
1031
Michal Vaskod3678892020-05-21 10:06:58 +02001032 assert(src->type == LYXP_SET_NODE_SET);
1033
1034 trg->type = LYXP_SET_NODE_SET;
1035 trg->used = src->used;
1036 trg->size = src->used;
1037 trg->ctx_pos = src->ctx_pos;
1038 trg->ctx_size = src->ctx_size;
1039
Michal Vasko08e9b112021-06-11 15:41:17 +02001040 if (trg->size) {
1041 trg->val.nodes = malloc(trg->size * sizeof *trg->val.nodes);
1042 LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), );
1043 memcpy(trg->val.nodes, src->val.nodes, src->used * sizeof *src->val.nodes);
1044 } else {
1045 trg->val.nodes = NULL;
1046 }
Michal Vaskod3678892020-05-21 10:06:58 +02001047 if (src->ht) {
1048 trg->ht = lyht_dup(src->ht);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001049 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02001050 trg->ht = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001051 }
1052 }
1053}
1054
1055/**
1056 * @brief Clear context of all schema nodes.
1057 *
1058 * @param[in] set Set to clear.
Michal Vasko1a09b212021-05-06 13:00:10 +02001059 * @param[in] new_ctx New context state for all the nodes currently in the context.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001060 */
1061static void
Michal Vasko1a09b212021-05-06 13:00:10 +02001062set_scnode_clear_ctx(struct lyxp_set *set, int32_t new_ctx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001063{
1064 uint32_t i;
1065
1066 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001067 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko1a09b212021-05-06 13:00:10 +02001068 set->val.scnodes[i].in_ctx = new_ctx;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001069 } else if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START) {
1070 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001071 }
1072 }
1073}
1074
1075/**
1076 * @brief Remove a node from a set. Removing last node changes
1077 * set into LYXP_SET_EMPTY. Context position aware.
1078 *
1079 * @param[in] set Set to use.
1080 * @param[in] idx Index from @p set of the node to be removed.
1081 */
1082static void
1083set_remove_node(struct lyxp_set *set, uint32_t idx)
1084{
1085 assert(set && (set->type == LYXP_SET_NODE_SET));
1086 assert(idx < set->used);
1087
1088 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1089
1090 --set->used;
Michal Vasko08e9b112021-06-11 15:41:17 +02001091 if (idx < set->used) {
1092 memmove(&set->val.nodes[idx], &set->val.nodes[idx + 1], (set->used - idx) * sizeof *set->val.nodes);
1093 } else if (!set->used) {
Michal Vaskod3678892020-05-21 10:06:58 +02001094 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001095 }
1096}
1097
1098/**
Michal Vasko2caefc12019-11-14 16:07:56 +01001099 * @brief Remove a node from a set by setting its type to LYXP_NODE_NONE.
Michal Vasko57eab132019-09-24 11:46:26 +02001100 *
1101 * @param[in] set Set to use.
1102 * @param[in] idx Index from @p set of the node to be removed.
1103 */
1104static void
Michal Vasko2caefc12019-11-14 16:07:56 +01001105set_remove_node_none(struct lyxp_set *set, uint32_t idx)
Michal Vasko57eab132019-09-24 11:46:26 +02001106{
1107 assert(set && (set->type == LYXP_SET_NODE_SET));
1108 assert(idx < set->used);
1109
Michal Vasko2caefc12019-11-14 16:07:56 +01001110 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1111 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1112 }
1113 set->val.nodes[idx].type = LYXP_NODE_NONE;
Michal Vasko57eab132019-09-24 11:46:26 +02001114}
1115
1116/**
Michal Vasko2caefc12019-11-14 16:07:56 +01001117 * @brief Remove all LYXP_NODE_NONE nodes from a set. Removing last node changes
Michal Vasko57eab132019-09-24 11:46:26 +02001118 * set into LYXP_SET_EMPTY. Context position aware.
1119 *
1120 * @param[in] set Set to consolidate.
1121 */
1122static void
Michal Vasko2caefc12019-11-14 16:07:56 +01001123set_remove_nodes_none(struct lyxp_set *set)
Michal Vasko57eab132019-09-24 11:46:26 +02001124{
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01001125 uint32_t i, orig_used, end = 0;
1126 int64_t start;
Michal Vasko57eab132019-09-24 11:46:26 +02001127
Michal Vaskod3678892020-05-21 10:06:58 +02001128 assert(set);
Michal Vasko57eab132019-09-24 11:46:26 +02001129
1130 orig_used = set->used;
1131 set->used = 0;
Michal Vaskod989ba02020-08-24 10:59:24 +02001132 for (i = 0; i < orig_used; ) {
Michal Vasko57eab132019-09-24 11:46:26 +02001133 start = -1;
1134 do {
Michal Vasko2caefc12019-11-14 16:07:56 +01001135 if ((set->val.nodes[i].type != LYXP_NODE_NONE) && (start == -1)) {
Michal Vasko57eab132019-09-24 11:46:26 +02001136 start = i;
Michal Vasko2caefc12019-11-14 16:07:56 +01001137 } else if ((start > -1) && (set->val.nodes[i].type == LYXP_NODE_NONE)) {
Michal Vasko57eab132019-09-24 11:46:26 +02001138 end = i;
1139 ++i;
1140 break;
1141 }
1142
1143 ++i;
1144 if (i == orig_used) {
1145 end = i;
1146 }
1147 } while (i < orig_used);
1148
1149 if (start > -1) {
1150 /* move the whole chunk of valid nodes together */
1151 if (set->used != (unsigned)start) {
1152 memmove(&set->val.nodes[set->used], &set->val.nodes[start], (end - start) * sizeof *set->val.nodes);
1153 }
1154 set->used += end - start;
1155 }
1156 }
Michal Vasko57eab132019-09-24 11:46:26 +02001157}
1158
1159/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02001160 * @brief Check for duplicates in a node set.
1161 *
1162 * @param[in] set Set to check.
1163 * @param[in] node Node to look for in @p set.
1164 * @param[in] node_type Type of @p node.
1165 * @param[in] skip_idx Index from @p set to skip.
1166 * @return LY_ERR
1167 */
1168static LY_ERR
1169set_dup_node_check(const struct lyxp_set *set, const struct lyd_node *node, enum lyxp_node_type node_type, int skip_idx)
1170{
1171 uint32_t i;
1172
Michal Vasko2caefc12019-11-14 16:07:56 +01001173 if (set->ht && node) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001174 return set_dup_node_hash_check(set, (struct lyd_node *)node, node_type, skip_idx);
1175 }
1176
1177 for (i = 0; i < set->used; ++i) {
1178 if ((skip_idx > -1) && (i == (unsigned)skip_idx)) {
1179 continue;
1180 }
1181
1182 if ((set->val.nodes[i].node == node) && (set->val.nodes[i].type == node_type)) {
1183 return LY_EEXIST;
1184 }
1185 }
1186
1187 return LY_SUCCESS;
1188}
1189
Radek Krejci857189e2020-09-01 13:26:36 +02001190ly_bool
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001191lyxp_set_scnode_contains(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type, int skip_idx,
1192 uint32_t *index_p)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001193{
1194 uint32_t i;
1195
1196 for (i = 0; i < set->used; ++i) {
1197 if ((skip_idx > -1) && (i == (unsigned)skip_idx)) {
1198 continue;
1199 }
1200
1201 if ((set->val.scnodes[i].scnode == node) && (set->val.scnodes[i].type == node_type)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001202 if (index_p) {
1203 *index_p = i;
1204 }
1205 return 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001206 }
1207 }
1208
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001209 return 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001210}
1211
Michal Vaskoecd62de2019-11-13 12:35:11 +01001212void
1213lyxp_set_scnode_merge(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001214{
1215 uint32_t orig_used, i, j;
1216
Michal Vaskod3678892020-05-21 10:06:58 +02001217 assert((set1->type == LYXP_SET_SCNODE_SET) && (set2->type == LYXP_SET_SCNODE_SET));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001218
Michal Vaskod3678892020-05-21 10:06:58 +02001219 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001220 return;
1221 }
1222
Michal Vaskod3678892020-05-21 10:06:58 +02001223 if (!set1->used) {
aPiecekadc1e4f2021-10-07 11:15:12 +02001224 /* release hidden allocated data (lyxp_set.size) */
1225 lyxp_set_free_content(set1);
1226 /* direct copying of the entire structure */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001227 memcpy(set1, set2, sizeof *set1);
1228 return;
1229 }
1230
1231 if (set1->used + set2->used > set1->size) {
1232 set1->size = set1->used + set2->used;
1233 set1->val.scnodes = ly_realloc(set1->val.scnodes, set1->size * sizeof *set1->val.scnodes);
1234 LY_CHECK_ERR_RET(!set1->val.scnodes, LOGMEM(set1->ctx), );
1235 }
1236
1237 orig_used = set1->used;
1238
1239 for (i = 0; i < set2->used; ++i) {
1240 for (j = 0; j < orig_used; ++j) {
1241 /* detect duplicities */
1242 if (set1->val.scnodes[j].scnode == set2->val.scnodes[i].scnode) {
1243 break;
1244 }
1245 }
1246
Michal Vasko0587bce2020-12-10 12:19:21 +01001247 if (j < orig_used) {
1248 /* node is there, but update its status if needed */
1249 if (set1->val.scnodes[j].in_ctx == LYXP_SET_SCNODE_START_USED) {
1250 set1->val.scnodes[j].in_ctx = set2->val.scnodes[i].in_ctx;
Michal Vasko1a09b212021-05-06 13:00:10 +02001251 } else if ((set1->val.scnodes[j].in_ctx == LYXP_SET_SCNODE_ATOM_NODE) &&
1252 (set2->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_VAL)) {
1253 set1->val.scnodes[j].in_ctx = set2->val.scnodes[i].in_ctx;
Michal Vasko0587bce2020-12-10 12:19:21 +01001254 }
1255 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001256 memcpy(&set1->val.scnodes[set1->used], &set2->val.scnodes[i], sizeof *set2->val.scnodes);
1257 ++set1->used;
1258 }
1259 }
1260
Michal Vaskod3678892020-05-21 10:06:58 +02001261 lyxp_set_free_content(set2);
1262 set2->type = LYXP_SET_SCNODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001263}
1264
1265/**
1266 * @brief Insert a node into a set. Context position aware.
1267 *
1268 * @param[in] set Set to use.
1269 * @param[in] node Node to insert to @p set.
1270 * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting.
1271 * @param[in] node_type Node type of @p node.
1272 * @param[in] idx Index in @p set to insert into.
1273 */
1274static void
1275set_insert_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx)
1276{
Michal Vaskod3678892020-05-21 10:06:58 +02001277 assert(set && (set->type == LYXP_SET_NODE_SET));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001278
Michal Vaskod3678892020-05-21 10:06:58 +02001279 if (!set->size) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001280 /* first item */
1281 if (idx) {
1282 /* no real harm done, but it is a bug */
1283 LOGINT(set->ctx);
1284 idx = 0;
1285 }
1286 set->val.nodes = malloc(LYXP_SET_SIZE_START * sizeof *set->val.nodes);
1287 LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), );
1288 set->type = LYXP_SET_NODE_SET;
1289 set->used = 0;
1290 set->size = LYXP_SET_SIZE_START;
1291 set->ctx_pos = 1;
1292 set->ctx_size = 1;
1293 set->ht = NULL;
1294 } else {
1295 /* not an empty set */
1296 if (set->used == set->size) {
1297
1298 /* set is full */
Michal Vasko871df522022-04-06 12:14:41 +02001299 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 +02001300 LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), );
Michal Vasko871df522022-04-06 12:14:41 +02001301 set->size *= LYXP_SET_SIZE_MUL_STEP;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001302 }
1303
1304 if (idx > set->used) {
1305 LOGINT(set->ctx);
1306 idx = set->used;
1307 }
1308
1309 /* make space for the new node */
1310 if (idx < set->used) {
1311 memmove(&set->val.nodes[idx + 1], &set->val.nodes[idx], (set->used - idx) * sizeof *set->val.nodes);
1312 }
1313 }
1314
1315 /* finally assign the value */
1316 set->val.nodes[idx].node = (struct lyd_node *)node;
1317 set->val.nodes[idx].type = node_type;
1318 set->val.nodes[idx].pos = pos;
1319 ++set->used;
1320
Michal Vasko2416fdd2021-10-01 11:07:10 +02001321 /* add into hash table */
1322 set_insert_node_hash(set, (struct lyd_node *)node, node_type);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001323}
1324
Michal Vaskoe4a6d012023-05-22 14:34:52 +02001325LY_ERR
1326lyxp_set_scnode_insert_node(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type,
Michal Vasko7333cb32022-07-29 16:30:29 +02001327 enum lyxp_axis axis, uint32_t *index_p)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001328{
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001329 uint32_t index;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001330
1331 assert(set->type == LYXP_SET_SCNODE_SET);
1332
Michal Vasko871df522022-04-06 12:14:41 +02001333 if (!set->size) {
1334 /* first item */
1335 set->val.scnodes = malloc(LYXP_SET_SIZE_START * sizeof *set->val.scnodes);
1336 LY_CHECK_ERR_RET(!set->val.scnodes, LOGMEM(set->ctx), LY_EMEM);
1337 set->type = LYXP_SET_SCNODE_SET;
1338 set->used = 0;
1339 set->size = LYXP_SET_SIZE_START;
1340 set->ctx_pos = 1;
1341 set->ctx_size = 1;
1342 set->ht = NULL;
1343 }
1344
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001345 if (lyxp_set_scnode_contains(set, node, node_type, -1, &index)) {
Michal Vaskoe4a6d012023-05-22 14:34:52 +02001346 /* BUG if axes differ, this new one is thrown away */
Radek Krejcif13b87b2020-12-01 22:02:17 +01001347 set->val.scnodes[index].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001348 } else {
1349 if (set->used == set->size) {
Michal Vasko871df522022-04-06 12:14:41 +02001350 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 +02001351 LY_CHECK_ERR_RET(!set->val.scnodes, LOGMEM(set->ctx), LY_EMEM);
Michal Vasko871df522022-04-06 12:14:41 +02001352 set->size *= LYXP_SET_SIZE_MUL_STEP;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001353 }
1354
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001355 index = set->used;
1356 set->val.scnodes[index].scnode = (struct lysc_node *)node;
1357 set->val.scnodes[index].type = node_type;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001358 set->val.scnodes[index].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko7333cb32022-07-29 16:30:29 +02001359 set->val.scnodes[index].axis = axis;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001360 ++set->used;
1361 }
1362
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001363 if (index_p) {
1364 *index_p = index;
1365 }
1366
1367 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001368}
1369
1370/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02001371 * @brief Set all nodes with ctx 1 to a new unique context value.
1372 *
1373 * @param[in] set Set to modify.
1374 * @return New context value.
1375 */
Michal Vasko5c4e5892019-11-14 12:31:38 +01001376static int32_t
Michal Vasko03ff5a72019-09-11 13:49:33 +02001377set_scnode_new_in_ctx(struct lyxp_set *set)
1378{
Michal Vasko5c4e5892019-11-14 12:31:38 +01001379 uint32_t i;
1380 int32_t ret_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001381
1382 assert(set->type == LYXP_SET_SCNODE_SET);
1383
Radek Krejcif13b87b2020-12-01 22:02:17 +01001384 ret_ctx = LYXP_SET_SCNODE_ATOM_PRED_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001385retry:
1386 for (i = 0; i < set->used; ++i) {
1387 if (set->val.scnodes[i].in_ctx >= ret_ctx) {
1388 ret_ctx = set->val.scnodes[i].in_ctx + 1;
1389 goto retry;
1390 }
1391 }
1392 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001393 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001394 set->val.scnodes[i].in_ctx = ret_ctx;
1395 }
1396 }
1397
1398 return ret_ctx;
1399}
1400
1401/**
1402 * @brief Get unique @p node position in the data.
1403 *
1404 * @param[in] node Node to find.
1405 * @param[in] node_type Node type of @p node.
1406 * @param[in] root Root node.
1407 * @param[in] root_type Type of the XPath @p root node.
1408 * @param[in] prev Node that we think is before @p node in DFS from @p root. Can optionally
1409 * be used to increase efficiency and start the DFS from this node.
1410 * @param[in] prev_pos Node @p prev position. Optional, but must be set if @p prev is set.
1411 * @return Node position.
1412 */
1413static uint32_t
1414get_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 +02001415 enum lyxp_node_type root_type, const struct lyd_node **prev, uint32_t *prev_pos)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001416{
Michal Vasko56daf732020-08-10 10:57:18 +02001417 const struct lyd_node *elem = NULL, *top_sibling;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001418 uint32_t pos = 1;
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001419 ly_bool found = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001420
1421 assert(prev && prev_pos && !root->prev->next);
1422
1423 if ((node_type == LYXP_NODE_ROOT) || (node_type == LYXP_NODE_ROOT_CONFIG)) {
1424 return 0;
1425 }
1426
1427 if (*prev) {
1428 /* start from the previous element instead from the root */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001429 pos = *prev_pos;
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001430 for (top_sibling = *prev; top_sibling->parent; top_sibling = lyd_parent(top_sibling)) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001431 goto dfs_search;
1432 }
1433
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001434 LY_LIST_FOR(root, top_sibling) {
Michal Vasko56daf732020-08-10 10:57:18 +02001435 LYD_TREE_DFS_BEGIN(top_sibling, elem) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001436dfs_search:
Michal Vaskoa9309bb2021-07-09 09:31:55 +02001437 LYD_TREE_DFS_continue = 0;
1438
Michal Vasko56daf732020-08-10 10:57:18 +02001439 if (*prev && !elem) {
1440 /* resume previous DFS */
1441 elem = LYD_TREE_DFS_next = (struct lyd_node *)*prev;
1442 LYD_TREE_DFS_continue = 0;
1443 }
1444
Michal Vasko222be682023-08-24 08:17:12 +02001445 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (elem->schema->flags & LYS_CONFIG_R)) {
Michal Vasko56daf732020-08-10 10:57:18 +02001446 /* skip */
1447 LYD_TREE_DFS_continue = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001448 } else {
Michal Vasko56daf732020-08-10 10:57:18 +02001449 if (elem == node) {
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001450 found = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001451 break;
1452 }
Michal Vasko56daf732020-08-10 10:57:18 +02001453 ++pos;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001454 }
Michal Vasko56daf732020-08-10 10:57:18 +02001455
1456 LYD_TREE_DFS_END(top_sibling, elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001457 }
1458
1459 /* node found */
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001460 if (found) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001461 break;
1462 }
1463 }
1464
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001465 if (!found) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001466 if (!(*prev)) {
1467 /* we went from root and failed to find it, cannot be */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001468 LOGINT(LYD_CTX(node));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001469 return 0;
1470 } else {
Michal Vasko56daf732020-08-10 10:57:18 +02001471 /* start the search again from the beginning */
1472 *prev = root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001473
Michal Vasko56daf732020-08-10 10:57:18 +02001474 top_sibling = root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001475 pos = 1;
1476 goto dfs_search;
1477 }
1478 }
1479
1480 /* remember the last found node for next time */
1481 *prev = node;
1482 *prev_pos = pos;
1483
1484 return pos;
1485}
1486
1487/**
1488 * @brief Assign (fill) missing node positions.
1489 *
1490 * @param[in] set Set to fill positions in.
1491 * @param[in] root Context root node.
1492 * @param[in] root_type Context root type.
1493 * @return LY_ERR
1494 */
1495static LY_ERR
1496set_assign_pos(struct lyxp_set *set, const struct lyd_node *root, enum lyxp_node_type root_type)
1497{
1498 const struct lyd_node *prev = NULL, *tmp_node;
1499 uint32_t i, tmp_pos = 0;
1500
1501 for (i = 0; i < set->used; ++i) {
1502 if (!set->val.nodes[i].pos) {
1503 tmp_node = NULL;
1504 switch (set->val.nodes[i].type) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001505 case LYXP_NODE_META:
1506 tmp_node = set->val.meta[i].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001507 if (!tmp_node) {
1508 LOGINT_RET(root->schema->module->ctx);
1509 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01001510 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001511 case LYXP_NODE_ELEM:
1512 case LYXP_NODE_TEXT:
1513 if (!tmp_node) {
1514 tmp_node = set->val.nodes[i].node;
1515 }
1516 set->val.nodes[i].pos = get_node_pos(tmp_node, set->val.nodes[i].type, root, root_type, &prev, &tmp_pos);
1517 break;
1518 default:
1519 /* all roots have position 0 */
1520 break;
1521 }
1522 }
1523 }
1524
1525 return LY_SUCCESS;
1526}
1527
1528/**
Michal Vasko9f96a052020-03-10 09:41:45 +01001529 * @brief Get unique @p meta position in the parent metadata.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001530 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001531 * @param[in] meta Metadata to use.
1532 * @return Metadata position.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001533 */
Michal Vaskodd528af2022-08-08 14:35:07 +02001534static uint32_t
Michal Vasko9f96a052020-03-10 09:41:45 +01001535get_meta_pos(struct lyd_meta *meta)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001536{
Michal Vaskodd528af2022-08-08 14:35:07 +02001537 uint32_t pos = 0;
Michal Vasko9f96a052020-03-10 09:41:45 +01001538 struct lyd_meta *meta2;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001539
Michal Vasko9f96a052020-03-10 09:41:45 +01001540 for (meta2 = meta->parent->meta; meta2 && (meta2 != meta); meta2 = meta2->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001541 ++pos;
1542 }
1543
Michal Vasko9f96a052020-03-10 09:41:45 +01001544 assert(meta2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001545 return pos;
1546}
1547
1548/**
1549 * @brief Compare 2 nodes in respect to XPath document order.
1550 *
1551 * @param[in] item1 1st node.
1552 * @param[in] item2 2nd node.
1553 * @return If 1st > 2nd returns 1, 1st == 2nd returns 0, and 1st < 2nd returns -1.
1554 */
1555static int
1556set_sort_compare(struct lyxp_set_node *item1, struct lyxp_set_node *item2)
1557{
Michal Vasko9f96a052020-03-10 09:41:45 +01001558 uint32_t meta_pos1 = 0, meta_pos2 = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001559
1560 if (item1->pos < item2->pos) {
1561 return -1;
1562 }
1563
1564 if (item1->pos > item2->pos) {
1565 return 1;
1566 }
1567
1568 /* node positions are equal, the fun case */
1569
1570 /* 1st ELEM - == - 2nd TEXT, 1st TEXT - == - 2nd ELEM */
1571 /* special case since text nodes are actually saved as their parents */
1572 if ((item1->node == item2->node) && (item1->type != item2->type)) {
1573 if (item1->type == LYXP_NODE_ELEM) {
1574 assert(item2->type == LYXP_NODE_TEXT);
1575 return -1;
1576 } else {
1577 assert((item1->type == LYXP_NODE_TEXT) && (item2->type == LYXP_NODE_ELEM));
1578 return 1;
1579 }
1580 }
1581
Michal Vasko9f96a052020-03-10 09:41:45 +01001582 /* we need meta positions now */
1583 if (item1->type == LYXP_NODE_META) {
1584 meta_pos1 = get_meta_pos((struct lyd_meta *)item1->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001585 }
Michal Vasko9f96a052020-03-10 09:41:45 +01001586 if (item2->type == LYXP_NODE_META) {
1587 meta_pos2 = get_meta_pos((struct lyd_meta *)item2->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001588 }
1589
Michal Vasko9f96a052020-03-10 09:41:45 +01001590 /* 1st ROOT - 2nd ROOT, 1st ELEM - 2nd ELEM, 1st TEXT - 2nd TEXT, 1st META - =pos= - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001591 /* check for duplicates */
1592 if (item1->node == item2->node) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001593 assert((item1->type == item2->type) && ((item1->type != LYXP_NODE_META) || (meta_pos1 == meta_pos2)));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001594 return 0;
1595 }
1596
Michal Vasko9f96a052020-03-10 09:41:45 +01001597 /* 1st ELEM - 2nd TEXT, 1st ELEM - any pos - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001598 /* elem is always first, 2nd node is after it */
1599 if (item1->type == LYXP_NODE_ELEM) {
1600 assert(item2->type != LYXP_NODE_ELEM);
1601 return -1;
1602 }
1603
Michal Vasko9f96a052020-03-10 09:41:45 +01001604 /* 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 +02001605 /* 2nd is before 1st */
Michal Vasko69730152020-10-09 16:30:07 +02001606 if (((item1->type == LYXP_NODE_TEXT) &&
1607 ((item2->type == LYXP_NODE_ELEM) || (item2->type == LYXP_NODE_META))) ||
1608 ((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_ELEM)) ||
1609 (((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_META)) &&
1610 (meta_pos1 > meta_pos2))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001611 return 1;
1612 }
1613
Michal Vasko9f96a052020-03-10 09:41:45 +01001614 /* 1st META - any pos - 2nd TEXT, 1st META <pos< - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001615 /* 2nd is after 1st */
1616 return -1;
1617}
1618
1619/**
1620 * @brief Set cast for comparisons.
1621 *
Michal Vasko8abcecc2022-07-28 09:55:01 +02001622 * @param[in,out] trg Target set to cast source into.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001623 * @param[in] src Source set.
1624 * @param[in] type Target set type.
1625 * @param[in] src_idx Source set node index.
Michal Vasko8abcecc2022-07-28 09:55:01 +02001626 * @return LY_SUCCESS on success.
1627 * @return LY_ERR value on error.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001628 */
1629static LY_ERR
Michal Vasko8abcecc2022-07-28 09:55:01 +02001630set_comp_cast(struct lyxp_set *trg, const struct lyxp_set *src, enum lyxp_set_type type, uint32_t src_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001631{
1632 assert(src->type == LYXP_SET_NODE_SET);
1633
1634 set_init(trg, src);
1635
1636 /* insert node into target set */
1637 set_insert_node(trg, src->val.nodes[src_idx].node, src->val.nodes[src_idx].pos, src->val.nodes[src_idx].type, 0);
1638
1639 /* cast target set appropriately */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001640 return lyxp_set_cast(trg, type);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001641}
1642
Michal Vasko4c7763f2020-07-27 17:40:37 +02001643/**
1644 * @brief Set content canonization for comparisons.
1645 *
Michal Vasko8abcecc2022-07-28 09:55:01 +02001646 * @param[in,out] set Set to canonize.
Michal Vasko4c7763f2020-07-27 17:40:37 +02001647 * @param[in] xp_node Source XPath node/meta to use for canonization.
Michal Vasko8abcecc2022-07-28 09:55:01 +02001648 * @return LY_SUCCESS on success.
1649 * @return LY_ERR value on error.
Michal Vasko4c7763f2020-07-27 17:40:37 +02001650 */
1651static LY_ERR
Michal Vasko8abcecc2022-07-28 09:55:01 +02001652set_comp_canonize(struct lyxp_set *set, const struct lyxp_set_node *xp_node)
Michal Vasko4c7763f2020-07-27 17:40:37 +02001653{
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001654 const struct lysc_type *type = NULL;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001655 struct lyd_value val;
1656 struct ly_err_item *err = NULL;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001657 LY_ERR rc;
1658
1659 /* is there anything to canonize even? */
Michal Vasko8abcecc2022-07-28 09:55:01 +02001660 if (set->type == LYXP_SET_STRING) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02001661 /* do we have a type to use for canonization? */
Michal Vasko222be682023-08-24 08:17:12 +02001662 if ((xp_node->type == LYXP_NODE_ELEM) && xp_node->node->schema && (xp_node->node->schema->nodetype & LYD_NODE_TERM)) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02001663 type = ((struct lyd_node_term *)xp_node->node)->value.realtype;
1664 } else if (xp_node->type == LYXP_NODE_META) {
1665 type = ((struct lyd_meta *)xp_node->node)->value.realtype;
1666 }
1667 }
1668 if (!type) {
Michal Vasko8abcecc2022-07-28 09:55:01 +02001669 /* no canonization needed/possible */
1670 return LY_SUCCESS;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001671 }
1672
Michal Vasko8abcecc2022-07-28 09:55:01 +02001673 /* check for built-in types without required canonization */
1674 if ((type->basetype == LY_TYPE_STRING) && (type->plugin->store == lyplg_type_store_string)) {
1675 /* string */
1676 return LY_SUCCESS;
1677 }
1678 if ((type->basetype == LY_TYPE_BOOL) && (type->plugin->store == lyplg_type_store_boolean)) {
1679 /* boolean */
1680 return LY_SUCCESS;
1681 }
1682 if ((type->basetype == LY_TYPE_ENUM) && (type->plugin->store == lyplg_type_store_enum)) {
1683 /* enumeration */
1684 return LY_SUCCESS;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001685 }
1686
Michal Vasko8abcecc2022-07-28 09:55:01 +02001687 /* print canonized string, ignore errors, the value may not satisfy schema constraints */
1688 rc = type->plugin->store(set->ctx, type, set->val.str, strlen(set->val.str), 0, set->format, set->prefix_data,
Michal Vasko405cc9e2020-12-01 12:01:27 +01001689 LYD_HINT_DATA, xp_node->node->schema, &val, NULL, &err);
Michal Vasko4c7763f2020-07-27 17:40:37 +02001690 ly_err_free(err);
1691 if (rc) {
Michal Vasko8abcecc2022-07-28 09:55:01 +02001692 /* invalid value, function store automaticaly dealloc value when fail */
1693 return LY_SUCCESS;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001694 }
1695
Michal Vasko8abcecc2022-07-28 09:55:01 +02001696 /* use the canonized string value */
1697 free(set->val.str);
1698 set->val.str = strdup(lyd_value_get_canonical(set->ctx, &val));
1699 type->plugin->free(set->ctx, &val);
1700 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM);
Michal Vasko4c7763f2020-07-27 17:40:37 +02001701
Michal Vasko4c7763f2020-07-27 17:40:37 +02001702 return LY_SUCCESS;
1703}
1704
Michal Vasko03ff5a72019-09-11 13:49:33 +02001705/**
1706 * @brief Bubble sort @p set into XPath document order.
Michal Vasko49fec8e2022-05-24 10:28:33 +02001707 * Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001708 *
1709 * @param[in] set Set to sort.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001710 * @return How many times the whole set was traversed - 1 (if set was sorted, returns 0).
1711 */
1712static int
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001713set_sort(struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001714{
1715 uint32_t i, j;
Radek Krejci1deb5be2020-08-26 16:43:36 +02001716 int ret = 0, cmp;
Radek Krejci857189e2020-09-01 13:26:36 +02001717 ly_bool inverted, change;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001718 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001719 struct lyxp_set_node item;
1720 struct lyxp_set_hash_node hnode;
1721 uint64_t hash;
1722
Michal Vasko3cf8fbf2020-05-27 15:21:21 +02001723 if ((set->type != LYXP_SET_NODE_SET) || (set->used < 2)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001724 return 0;
1725 }
1726
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001727 /* find first top-level node to be used as anchor for positions */
Michal Vasko4a7d4d62021-12-13 17:05:06 +01001728 for (root = set->tree; root->parent; root = lyd_parent(root)) {}
Michal Vaskod989ba02020-08-24 10:59:24 +02001729 for ( ; root->prev->next; root = root->prev) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001730
1731 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001732 if (set_assign_pos(set, root, set->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001733 return -1;
1734 }
1735
Michal Vasko88a9e802022-05-24 10:50:28 +02001736#ifndef NDEBUG
Michal Vasko03ff5a72019-09-11 13:49:33 +02001737 LOGDBG(LY_LDGXPATH, "SORT BEGIN");
1738 print_set_debug(set);
Michal Vasko88a9e802022-05-24 10:50:28 +02001739#endif
Michal Vasko03ff5a72019-09-11 13:49:33 +02001740
1741 for (i = 0; i < set->used; ++i) {
1742 inverted = 0;
1743 change = 0;
1744
1745 for (j = 1; j < set->used - i; ++j) {
1746 /* compare node positions */
1747 if (inverted) {
1748 cmp = set_sort_compare(&set->val.nodes[j], &set->val.nodes[j - 1]);
1749 } else {
1750 cmp = set_sort_compare(&set->val.nodes[j - 1], &set->val.nodes[j]);
1751 }
1752
1753 /* swap if needed */
1754 if ((inverted && (cmp < 0)) || (!inverted && (cmp > 0))) {
1755 change = 1;
1756
1757 item = set->val.nodes[j - 1];
1758 set->val.nodes[j - 1] = set->val.nodes[j];
1759 set->val.nodes[j] = item;
1760 } else {
1761 /* whether node_pos1 should be smaller than node_pos2 or the other way around */
1762 inverted = !inverted;
1763 }
1764 }
1765
1766 ++ret;
1767
1768 if (!change) {
1769 break;
1770 }
1771 }
1772
Michal Vasko88a9e802022-05-24 10:50:28 +02001773#ifndef NDEBUG
Michal Vasko03ff5a72019-09-11 13:49:33 +02001774 LOGDBG(LY_LDGXPATH, "SORT END %d", ret);
1775 print_set_debug(set);
Michal Vasko88a9e802022-05-24 10:50:28 +02001776#endif
Michal Vasko03ff5a72019-09-11 13:49:33 +02001777
1778 /* check node hashes */
1779 if (set->used >= LYD_HT_MIN_ITEMS) {
1780 assert(set->ht);
1781 for (i = 0; i < set->used; ++i) {
1782 hnode.node = set->val.nodes[i].node;
1783 hnode.type = set->val.nodes[i].type;
1784
Michal Vaskoae130f52023-04-20 14:25:16 +02001785 hash = lyht_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
1786 hash = lyht_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
1787 hash = lyht_hash_multi(hash, NULL, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001788
1789 assert(!lyht_find(set->ht, &hnode, hash, NULL));
1790 }
1791 }
1792
1793 return ret - 1;
1794}
1795
Michal Vasko03ff5a72019-09-11 13:49:33 +02001796/**
1797 * @brief Merge 2 sorted sets into one.
1798 *
1799 * @param[in,out] trg Set to merge into. Duplicates are removed.
1800 * @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 +02001801 * @return LY_ERR
1802 */
1803static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001804set_sorted_merge(struct lyxp_set *trg, struct lyxp_set *src)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001805{
1806 uint32_t i, j, k, count, dup_count;
1807 int cmp;
1808 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001809
Michal Vaskod3678892020-05-21 10:06:58 +02001810 if ((trg->type != LYXP_SET_NODE_SET) || (src->type != LYXP_SET_NODE_SET)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001811 return LY_EINVAL;
1812 }
1813
Michal Vaskod3678892020-05-21 10:06:58 +02001814 if (!src->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001815 return LY_SUCCESS;
Michal Vaskod3678892020-05-21 10:06:58 +02001816 } else if (!trg->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001817 set_fill_set(trg, src);
Michal Vaskod3678892020-05-21 10:06:58 +02001818 lyxp_set_free_content(src);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001819 return LY_SUCCESS;
1820 }
1821
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001822 /* find first top-level node to be used as anchor for positions */
Michal Vasko0143b682021-12-13 17:13:09 +01001823 for (root = trg->tree; root->parent; root = lyd_parent(root)) {}
Michal Vaskod989ba02020-08-24 10:59:24 +02001824 for ( ; root->prev->next; root = root->prev) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001825
1826 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001827 if (set_assign_pos(trg, root, trg->root_type) || set_assign_pos(src, root, src->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001828 return LY_EINT;
1829 }
1830
1831#ifndef NDEBUG
1832 LOGDBG(LY_LDGXPATH, "MERGE target");
1833 print_set_debug(trg);
1834 LOGDBG(LY_LDGXPATH, "MERGE source");
1835 print_set_debug(src);
1836#endif
1837
1838 /* make memory for the merge (duplicates are not detected yet, so space
1839 * will likely be wasted on them, too bad) */
1840 if (trg->size - trg->used < src->used) {
1841 trg->size = trg->used + src->used;
1842
1843 trg->val.nodes = ly_realloc(trg->val.nodes, trg->size * sizeof *trg->val.nodes);
1844 LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx), LY_EMEM);
1845 }
1846
1847 i = 0;
1848 j = 0;
1849 count = 0;
1850 dup_count = 0;
1851 do {
1852 cmp = set_sort_compare(&src->val.nodes[i], &trg->val.nodes[j]);
1853 if (!cmp) {
1854 if (!count) {
1855 /* duplicate, just skip it */
1856 ++i;
1857 ++j;
1858 } else {
1859 /* we are copying something already, so let's copy the duplicate too,
1860 * we are hoping that afterwards there are some more nodes to
1861 * copy and this way we can copy them all together */
1862 ++count;
1863 ++dup_count;
1864 ++i;
1865 ++j;
1866 }
1867 } else if (cmp < 0) {
1868 /* inserting src node into trg, just remember it for now */
1869 ++count;
1870 ++i;
1871
1872 /* insert the hash now */
1873 set_insert_node_hash(trg, src->val.nodes[i - 1].node, src->val.nodes[i - 1].type);
1874 } else if (count) {
1875copy_nodes:
1876 /* time to actually copy the nodes, we have found the largest block of nodes */
1877 memmove(&trg->val.nodes[j + (count - dup_count)],
1878 &trg->val.nodes[j],
1879 (trg->used - j) * sizeof *trg->val.nodes);
1880 memcpy(&trg->val.nodes[j - dup_count], &src->val.nodes[i - count], count * sizeof *src->val.nodes);
1881
1882 trg->used += count - dup_count;
1883 /* do not change i, except the copying above, we are basically doing exactly what is in the else branch below */
1884 j += count - dup_count;
1885
1886 count = 0;
1887 dup_count = 0;
1888 } else {
1889 ++j;
1890 }
1891 } while ((i < src->used) && (j < trg->used));
1892
1893 if ((i < src->used) || count) {
1894 /* insert all the hashes first */
1895 for (k = i; k < src->used; ++k) {
1896 set_insert_node_hash(trg, src->val.nodes[k].node, src->val.nodes[k].type);
1897 }
1898
1899 /* loop ended, but we need to copy something at trg end */
1900 count += src->used - i;
1901 i = src->used;
1902 goto copy_nodes;
1903 }
1904
1905 /* we are inserting hashes before the actual node insert, which causes
1906 * situations when there were initially not enough items for a hash table,
1907 * but even after some were inserted, hash table was not created (during
1908 * insertion the number of items is not updated yet) */
1909 if (!trg->ht && (trg->used >= LYD_HT_MIN_ITEMS)) {
1910 set_insert_node_hash(trg, NULL, 0);
1911 }
1912
1913#ifndef NDEBUG
1914 LOGDBG(LY_LDGXPATH, "MERGE result");
1915 print_set_debug(trg);
1916#endif
1917
Michal Vaskod3678892020-05-21 10:06:58 +02001918 lyxp_set_free_content(src);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001919 return LY_SUCCESS;
1920}
1921
Michal Vasko14676352020-05-29 11:35:55 +02001922LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02001923lyxp_check_token(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint32_t tok_idx, enum lyxp_token want_tok)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001924{
Michal Vasko004d3152020-06-11 19:59:22 +02001925 if (exp->used == tok_idx) {
Michal Vasko14676352020-05-29 11:35:55 +02001926 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001927 LOGVAL(ctx, LY_VCODE_XP_EOF);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001928 }
Michal Vasko14676352020-05-29 11:35:55 +02001929 return LY_EINCOMPLETE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001930 }
1931
Michal Vasko004d3152020-06-11 19:59:22 +02001932 if (want_tok && (exp->tokens[tok_idx] != want_tok)) {
Michal Vasko14676352020-05-29 11:35:55 +02001933 if (ctx) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02001934 LOGVAL(ctx, LY_VCODE_XP_INTOK2, lyxp_token2str(exp->tokens[tok_idx]),
1935 &exp->expr[exp->tok_pos[tok_idx]], lyxp_token2str(want_tok));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001936 }
Michal Vasko14676352020-05-29 11:35:55 +02001937 return LY_ENOT;
1938 }
1939
1940 return LY_SUCCESS;
1941}
1942
Michal Vasko004d3152020-06-11 19:59:22 +02001943LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02001944lyxp_next_token(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint32_t *tok_idx, enum lyxp_token want_tok)
Michal Vasko004d3152020-06-11 19:59:22 +02001945{
1946 LY_CHECK_RET(lyxp_check_token(ctx, exp, *tok_idx, want_tok));
1947
1948 /* skip the token */
1949 ++(*tok_idx);
1950
1951 return LY_SUCCESS;
1952}
1953
Michal Vasko14676352020-05-29 11:35:55 +02001954/* just like lyxp_check_token() but tests for 2 tokens */
1955static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02001956exp_check_token2(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint32_t tok_idx, enum lyxp_token want_tok1,
Radek Krejci0f969882020-08-21 16:56:47 +02001957 enum lyxp_token want_tok2)
Michal Vasko14676352020-05-29 11:35:55 +02001958{
Michal Vasko004d3152020-06-11 19:59:22 +02001959 if (exp->used == tok_idx) {
Michal Vasko14676352020-05-29 11:35:55 +02001960 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001961 LOGVAL(ctx, LY_VCODE_XP_EOF);
Michal Vasko14676352020-05-29 11:35:55 +02001962 }
1963 return LY_EINCOMPLETE;
1964 }
1965
Michal Vasko004d3152020-06-11 19:59:22 +02001966 if ((exp->tokens[tok_idx] != want_tok1) && (exp->tokens[tok_idx] != want_tok2)) {
Michal Vasko14676352020-05-29 11:35:55 +02001967 if (ctx) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02001968 LOGVAL(ctx, LY_VCODE_XP_INTOK, lyxp_token2str(exp->tokens[tok_idx]),
Michal Vasko0b468e62020-10-19 09:33:04 +02001969 &exp->expr[exp->tok_pos[tok_idx]]);
Michal Vasko14676352020-05-29 11:35:55 +02001970 }
1971 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001972 }
1973
1974 return LY_SUCCESS;
1975}
1976
Michal Vasko4911eeb2021-06-28 11:23:05 +02001977LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02001978lyxp_next_token2(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint32_t *tok_idx, enum lyxp_token want_tok1,
Michal Vasko4911eeb2021-06-28 11:23:05 +02001979 enum lyxp_token want_tok2)
1980{
1981 LY_CHECK_RET(exp_check_token2(ctx, exp, *tok_idx, want_tok1, want_tok2));
1982
1983 /* skip the token */
1984 ++(*tok_idx);
1985
1986 return LY_SUCCESS;
1987}
1988
Michal Vasko03ff5a72019-09-11 13:49:33 +02001989/**
1990 * @brief Stack operation push on the repeat array.
1991 *
1992 * @param[in] exp Expression to use.
Michal Vasko831e3bd2023-04-24 10:43:38 +02001993 * @param[in] tok_idx Position in the expresion @p exp.
1994 * @param[in] repeat_expr_type Repeated expression type, this value is pushed.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001995 */
1996static void
Michal Vasko831e3bd2023-04-24 10:43:38 +02001997exp_repeat_push(struct lyxp_expr *exp, uint32_t tok_idx, enum lyxp_expr_type repeat_expr_type)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001998{
Michal Vaskodd528af2022-08-08 14:35:07 +02001999 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002000
Michal Vasko004d3152020-06-11 19:59:22 +02002001 if (exp->repeat[tok_idx]) {
Radek Krejci1e008d22020-08-17 11:37:37 +02002002 for (i = 0; exp->repeat[tok_idx][i]; ++i) {}
Michal Vasko004d3152020-06-11 19:59:22 +02002003 exp->repeat[tok_idx] = realloc(exp->repeat[tok_idx], (i + 2) * sizeof *exp->repeat[tok_idx]);
2004 LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), );
Michal Vasko831e3bd2023-04-24 10:43:38 +02002005 exp->repeat[tok_idx][i] = repeat_expr_type;
Michal Vasko004d3152020-06-11 19:59:22 +02002006 exp->repeat[tok_idx][i + 1] = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002007 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02002008 exp->repeat[tok_idx] = calloc(2, sizeof *exp->repeat[tok_idx]);
2009 LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), );
Michal Vasko831e3bd2023-04-24 10:43:38 +02002010 exp->repeat[tok_idx][0] = repeat_expr_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002011 }
2012}
2013
2014/**
2015 * @brief Reparse Predicate. Logs directly on error.
2016 *
2017 * [7] Predicate ::= '[' Expr ']'
2018 *
2019 * @param[in] ctx Context for logging.
2020 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002021 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002022 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002023 * @return LY_ERR
2024 */
2025static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02002026reparse_predicate(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t depth)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002027{
2028 LY_ERR rc;
2029
Michal Vasko004d3152020-06-11 19:59:22 +02002030 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002031 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002032 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002033
aPiecekbf968d92021-05-27 14:35:05 +02002034 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002035 LY_CHECK_RET(rc);
2036
Michal Vasko004d3152020-06-11 19:59:22 +02002037 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002038 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002039 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002040
2041 return LY_SUCCESS;
2042}
2043
2044/**
2045 * @brief Reparse RelativeLocationPath. Logs directly on error.
2046 *
2047 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
2048 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
2049 * [6] NodeTest ::= NameTest | NodeType '(' ')'
2050 *
2051 * @param[in] ctx Context for logging.
2052 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002053 * @param[in] tok_idx Position in the expression \p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002054 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002055 * @return LY_ERR (LY_EINCOMPLETE on forward reference)
2056 */
2057static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02002058reparse_relative_location_path(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t depth)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002059{
2060 LY_ERR rc;
2061
Michal Vasko004d3152020-06-11 19:59:22 +02002062 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002063 LY_CHECK_RET(rc);
2064
2065 goto step;
2066 do {
2067 /* '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02002068 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002069
Michal Vasko004d3152020-06-11 19:59:22 +02002070 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002071 LY_CHECK_RET(rc);
2072step:
2073 /* Step */
Michal Vasko004d3152020-06-11 19:59:22 +02002074 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002075 case LYXP_TOKEN_DOT:
Michal Vasko004d3152020-06-11 19:59:22 +02002076 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002077 break;
2078
2079 case LYXP_TOKEN_DDOT:
Michal Vasko004d3152020-06-11 19:59:22 +02002080 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002081 break;
2082
Michal Vasko49fec8e2022-05-24 10:28:33 +02002083 case LYXP_TOKEN_AXISNAME:
2084 ++(*tok_idx);
2085
2086 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_DCOLON);
2087 LY_CHECK_RET(rc);
2088
2089 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02002090 case LYXP_TOKEN_AT:
Michal Vasko004d3152020-06-11 19:59:22 +02002091 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002092
Michal Vasko004d3152020-06-11 19:59:22 +02002093 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002094 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002095 if ((exp->tokens[*tok_idx] != LYXP_TOKEN_NAMETEST) && (exp->tokens[*tok_idx] != LYXP_TOKEN_NODETYPE)) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02002096 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 +02002097 return LY_EVALID;
2098 }
Michal Vasko49fec8e2022-05-24 10:28:33 +02002099 if (exp->tokens[*tok_idx] == LYXP_TOKEN_NODETYPE) {
2100 goto reparse_nodetype;
2101 }
Radek Krejci0f969882020-08-21 16:56:47 +02002102 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02002103 case LYXP_TOKEN_NAMETEST:
Michal Vasko004d3152020-06-11 19:59:22 +02002104 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002105 goto reparse_predicate;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002106
2107 case LYXP_TOKEN_NODETYPE:
Michal Vasko49fec8e2022-05-24 10:28:33 +02002108reparse_nodetype:
Michal Vasko004d3152020-06-11 19:59:22 +02002109 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002110
2111 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02002112 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002113 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002114 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002115
2116 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02002117 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002118 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002119 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002120
2121reparse_predicate:
2122 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002123 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
aPiecekbf968d92021-05-27 14:35:05 +02002124 rc = reparse_predicate(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002125 LY_CHECK_RET(rc);
2126 }
2127 break;
2128 default:
Michal Vasko49fec8e2022-05-24 10:28:33 +02002129 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 +02002130 return LY_EVALID;
2131 }
Michal Vasko004d3152020-06-11 19:59:22 +02002132 } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02002133
2134 return LY_SUCCESS;
2135}
2136
2137/**
2138 * @brief Reparse AbsoluteLocationPath. Logs directly on error.
2139 *
2140 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
2141 *
2142 * @param[in] ctx Context for logging.
2143 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002144 * @param[in] tok_idx Position in the expression \p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002145 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002146 * @return LY_ERR
2147 */
2148static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02002149reparse_absolute_location_path(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t depth)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002150{
2151 LY_ERR rc;
2152
Michal Vasko004d3152020-06-11 19:59:22 +02002153 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 +02002154
2155 /* '/' RelativeLocationPath? */
Michal Vasko004d3152020-06-11 19:59:22 +02002156 if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002157 /* '/' */
Michal Vasko004d3152020-06-11 19:59:22 +02002158 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002159
Michal Vasko004d3152020-06-11 19:59:22 +02002160 if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002161 return LY_SUCCESS;
2162 }
Michal Vasko004d3152020-06-11 19:59:22 +02002163 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002164 case LYXP_TOKEN_DOT:
2165 case LYXP_TOKEN_DDOT:
Michal Vasko49fec8e2022-05-24 10:28:33 +02002166 case LYXP_TOKEN_AXISNAME:
Michal Vasko03ff5a72019-09-11 13:49:33 +02002167 case LYXP_TOKEN_AT:
2168 case LYXP_TOKEN_NAMETEST:
2169 case LYXP_TOKEN_NODETYPE:
aPiecekbf968d92021-05-27 14:35:05 +02002170 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002171 LY_CHECK_RET(rc);
Radek Krejci0f969882020-08-21 16:56:47 +02002172 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02002173 default:
2174 break;
2175 }
2176
Michal Vasko03ff5a72019-09-11 13:49:33 +02002177 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02002178 /* '//' RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002179 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002180
aPiecekbf968d92021-05-27 14:35:05 +02002181 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002182 LY_CHECK_RET(rc);
2183 }
2184
2185 return LY_SUCCESS;
2186}
2187
2188/**
2189 * @brief Reparse FunctionCall. Logs directly on error.
2190 *
2191 * [9] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
2192 *
2193 * @param[in] ctx Context for logging.
2194 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002195 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002196 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002197 * @return LY_ERR
2198 */
2199static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02002200reparse_function_call(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t depth)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002201{
Radek Krejci1deb5be2020-08-26 16:43:36 +02002202 int8_t min_arg_count = -1;
Michal Vaskodd528af2022-08-08 14:35:07 +02002203 uint32_t arg_count, max_arg_count = 0, func_tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002204 LY_ERR rc;
2205
Michal Vasko004d3152020-06-11 19:59:22 +02002206 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_FUNCNAME);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002207 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002208 func_tok_idx = *tok_idx;
2209 switch (exp->tok_len[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002210 case 3:
Michal Vasko004d3152020-06-11 19:59:22 +02002211 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) {
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]], "sum", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002215 min_arg_count = 1;
2216 max_arg_count = 1;
2217 }
2218 break;
2219 case 4:
Michal Vasko004d3152020-06-11 19:59:22 +02002220 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002221 min_arg_count = 1;
2222 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002223 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002224 min_arg_count = 0;
2225 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002226 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002227 min_arg_count = 0;
2228 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002229 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002230 min_arg_count = 0;
2231 max_arg_count = 0;
2232 }
2233 break;
2234 case 5:
Michal Vasko004d3152020-06-11 19:59:22 +02002235 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 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]], "false", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002239 min_arg_count = 0;
2240 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002241 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002242 min_arg_count = 1;
2243 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002244 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002245 min_arg_count = 1;
2246 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002247 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002248 min_arg_count = 1;
2249 max_arg_count = 1;
2250 }
2251 break;
2252 case 6:
Michal Vasko004d3152020-06-11 19:59:22 +02002253 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002254 min_arg_count = 2;
Radek Krejci1deb5be2020-08-26 16:43:36 +02002255 max_arg_count = UINT32_MAX;
Michal Vasko004d3152020-06-11 19:59:22 +02002256 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002257 min_arg_count = 0;
2258 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002259 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002260 min_arg_count = 0;
2261 max_arg_count = 1;
2262 }
2263 break;
2264 case 7:
Michal Vasko004d3152020-06-11 19:59:22 +02002265 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002266 min_arg_count = 1;
2267 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002268 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002269 min_arg_count = 1;
2270 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002271 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002272 min_arg_count = 0;
2273 max_arg_count = 0;
2274 }
2275 break;
2276 case 8:
Michal Vasko004d3152020-06-11 19:59:22 +02002277 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002278 min_arg_count = 2;
2279 max_arg_count = 2;
Michal Vasko004d3152020-06-11 19:59:22 +02002280 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002281 min_arg_count = 0;
2282 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002283 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002284 min_arg_count = 2;
2285 max_arg_count = 2;
2286 }
2287 break;
2288 case 9:
Michal Vasko004d3152020-06-11 19:59:22 +02002289 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002290 min_arg_count = 2;
2291 max_arg_count = 3;
Michal Vasko004d3152020-06-11 19:59:22 +02002292 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002293 min_arg_count = 3;
2294 max_arg_count = 3;
2295 }
2296 break;
2297 case 10:
Michal Vasko004d3152020-06-11 19:59:22 +02002298 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002299 min_arg_count = 0;
2300 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002301 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002302 min_arg_count = 1;
2303 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002304 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002305 min_arg_count = 2;
2306 max_arg_count = 2;
2307 }
2308 break;
2309 case 11:
Michal Vasko004d3152020-06-11 19:59:22 +02002310 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002311 min_arg_count = 2;
2312 max_arg_count = 2;
2313 }
2314 break;
2315 case 12:
Michal Vasko004d3152020-06-11 19:59:22 +02002316 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002317 min_arg_count = 2;
2318 max_arg_count = 2;
2319 }
2320 break;
2321 case 13:
Michal Vasko004d3152020-06-11 19:59:22 +02002322 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) {
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]], "string-length", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002326 min_arg_count = 0;
2327 max_arg_count = 1;
2328 }
2329 break;
2330 case 15:
Michal Vasko004d3152020-06-11 19:59:22 +02002331 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002332 min_arg_count = 0;
2333 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002334 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002335 min_arg_count = 2;
2336 max_arg_count = 2;
2337 }
2338 break;
2339 case 16:
Michal Vasko004d3152020-06-11 19:59:22 +02002340 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002341 min_arg_count = 2;
2342 max_arg_count = 2;
2343 }
2344 break;
2345 case 20:
Michal Vasko004d3152020-06-11 19:59:22 +02002346 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002347 min_arg_count = 2;
2348 max_arg_count = 2;
2349 }
2350 break;
2351 }
2352 if (min_arg_count == -1) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002353 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 +02002354 return LY_EINVAL;
2355 }
Michal Vasko004d3152020-06-11 19:59:22 +02002356 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002357
2358 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02002359 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002360 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002361 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002362
2363 /* ( Expr ( ',' Expr )* )? */
2364 arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002365 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002366 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002367 if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002368 ++arg_count;
aPiecekbf968d92021-05-27 14:35:05 +02002369 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002370 LY_CHECK_RET(rc);
2371 }
Michal Vasko004d3152020-06-11 19:59:22 +02002372 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) {
2373 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002374
2375 ++arg_count;
aPiecekbf968d92021-05-27 14:35:05 +02002376 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002377 LY_CHECK_RET(rc);
2378 }
2379
2380 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02002381 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002382 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002383 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002384
Radek Krejci857189e2020-09-01 13:26:36 +02002385 if ((arg_count < (uint32_t)min_arg_count) || (arg_count > max_arg_count)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002386 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 +02002387 return LY_EVALID;
2388 }
2389
2390 return LY_SUCCESS;
2391}
2392
2393/**
2394 * @brief Reparse PathExpr. Logs directly on error.
2395 *
2396 * [10] PathExpr ::= LocationPath | PrimaryExpr Predicate*
2397 * | PrimaryExpr Predicate* '/' RelativeLocationPath
2398 * | PrimaryExpr Predicate* '//' RelativeLocationPath
2399 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
aPiecekfba75362021-10-07 12:39:48 +02002400 * [8] PrimaryExpr ::= VariableReference | '(' Expr ')' | Literal | Number | FunctionCall
Michal Vasko03ff5a72019-09-11 13:49:33 +02002401 *
2402 * @param[in] ctx Context for logging.
2403 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002404 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002405 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002406 * @return LY_ERR
2407 */
2408static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02002409reparse_path_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t depth)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002410{
2411 LY_ERR rc;
2412
Michal Vasko004d3152020-06-11 19:59:22 +02002413 if (lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko14676352020-05-29 11:35:55 +02002414 return LY_EVALID;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002415 }
2416
Michal Vasko004d3152020-06-11 19:59:22 +02002417 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002418 case LYXP_TOKEN_PAR1:
2419 /* '(' Expr ')' Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002420 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002421
aPiecekbf968d92021-05-27 14:35:05 +02002422 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002423 LY_CHECK_RET(rc);
2424
Michal Vasko004d3152020-06-11 19:59:22 +02002425 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002426 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002427 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002428 goto predicate;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002429 case LYXP_TOKEN_DOT:
2430 case LYXP_TOKEN_DDOT:
Michal Vasko49fec8e2022-05-24 10:28:33 +02002431 case LYXP_TOKEN_AXISNAME:
Michal Vasko03ff5a72019-09-11 13:49:33 +02002432 case LYXP_TOKEN_AT:
2433 case LYXP_TOKEN_NAMETEST:
2434 case LYXP_TOKEN_NODETYPE:
2435 /* RelativeLocationPath */
aPiecekbf968d92021-05-27 14:35:05 +02002436 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002437 LY_CHECK_RET(rc);
2438 break;
aPiecekfba75362021-10-07 12:39:48 +02002439 case LYXP_TOKEN_VARREF:
2440 /* VariableReference */
2441 ++(*tok_idx);
2442 goto predicate;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002443 case LYXP_TOKEN_FUNCNAME:
2444 /* FunctionCall */
aPiecekbf968d92021-05-27 14:35:05 +02002445 rc = reparse_function_call(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002446 LY_CHECK_RET(rc);
2447 goto predicate;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002448 case LYXP_TOKEN_OPER_PATH:
2449 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02002450 /* AbsoluteLocationPath */
aPiecekbf968d92021-05-27 14:35:05 +02002451 rc = reparse_absolute_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002452 LY_CHECK_RET(rc);
2453 break;
2454 case LYXP_TOKEN_LITERAL:
2455 /* Literal */
Michal Vasko004d3152020-06-11 19:59:22 +02002456 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002457 goto predicate;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002458 case LYXP_TOKEN_NUMBER:
2459 /* Number */
Michal Vasko004d3152020-06-11 19:59:22 +02002460 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002461 goto predicate;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002462 default:
Michal Vasko49fec8e2022-05-24 10:28:33 +02002463 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 +02002464 return LY_EVALID;
2465 }
2466
2467 return LY_SUCCESS;
2468
2469predicate:
2470 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002471 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
aPiecekbf968d92021-05-27 14:35:05 +02002472 rc = reparse_predicate(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002473 LY_CHECK_RET(rc);
2474 }
2475
2476 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002477 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002478
2479 /* '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02002480 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002481
aPiecekbf968d92021-05-27 14:35:05 +02002482 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002483 LY_CHECK_RET(rc);
2484 }
2485
2486 return LY_SUCCESS;
2487}
2488
2489/**
2490 * @brief Reparse UnaryExpr. Logs directly on error.
2491 *
2492 * [17] UnaryExpr ::= UnionExpr | '-' UnaryExpr
2493 * [18] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
2494 *
2495 * @param[in] ctx Context for logging.
2496 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002497 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002498 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002499 * @return LY_ERR
2500 */
2501static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02002502reparse_unary_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t depth)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002503{
Michal Vaskodd528af2022-08-08 14:35:07 +02002504 uint32_t prev_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002505 LY_ERR rc;
2506
2507 /* ('-')* */
Michal Vasko004d3152020-06-11 19:59:22 +02002508 prev_exp = *tok_idx;
Michal Vasko69730152020-10-09 16:30:07 +02002509 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2510 (exp->expr[exp->tok_pos[*tok_idx]] == '-')) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002511 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNARY);
Michal Vasko004d3152020-06-11 19:59:22 +02002512 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002513 }
2514
2515 /* PathExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002516 prev_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002517 rc = reparse_path_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002518 LY_CHECK_RET(rc);
2519
2520 /* ('|' PathExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002521 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_UNI)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002522 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNION);
Michal Vasko004d3152020-06-11 19:59:22 +02002523 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002524
aPiecekbf968d92021-05-27 14:35:05 +02002525 rc = reparse_path_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002526 LY_CHECK_RET(rc);
2527 }
2528
2529 return LY_SUCCESS;
2530}
2531
2532/**
2533 * @brief Reparse AdditiveExpr. Logs directly on error.
2534 *
2535 * [15] AdditiveExpr ::= MultiplicativeExpr
2536 * | AdditiveExpr '+' MultiplicativeExpr
2537 * | AdditiveExpr '-' MultiplicativeExpr
2538 * [16] MultiplicativeExpr ::= UnaryExpr
2539 * | MultiplicativeExpr '*' UnaryExpr
2540 * | MultiplicativeExpr 'div' UnaryExpr
2541 * | MultiplicativeExpr 'mod' UnaryExpr
2542 *
2543 * @param[in] ctx Context for logging.
2544 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002545 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002546 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002547 * @return LY_ERR
2548 */
2549static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02002550reparse_additive_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t depth)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002551{
Michal Vaskodd528af2022-08-08 14:35:07 +02002552 uint32_t prev_add_exp, prev_mul_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002553 LY_ERR rc;
2554
Michal Vasko004d3152020-06-11 19:59:22 +02002555 prev_add_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002556 goto reparse_multiplicative_expr;
2557
2558 /* ('+' / '-' MultiplicativeExpr)* */
Michal Vasko69730152020-10-09 16:30:07 +02002559 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2560 ((exp->expr[exp->tok_pos[*tok_idx]] == '+') || (exp->expr[exp->tok_pos[*tok_idx]] == '-'))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002561 exp_repeat_push(exp, prev_add_exp, LYXP_EXPR_ADDITIVE);
Michal Vasko004d3152020-06-11 19:59:22 +02002562 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002563
2564reparse_multiplicative_expr:
2565 /* UnaryExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002566 prev_mul_exp = *tok_idx;
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 /* ('*' / 'div' / 'mod' UnaryExpr)* */
Michal Vasko69730152020-10-09 16:30:07 +02002571 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2572 ((exp->expr[exp->tok_pos[*tok_idx]] == '*') || (exp->tok_len[*tok_idx] == 3))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002573 exp_repeat_push(exp, prev_mul_exp, LYXP_EXPR_MULTIPLICATIVE);
Michal Vasko004d3152020-06-11 19:59:22 +02002574 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002575
aPiecekbf968d92021-05-27 14:35:05 +02002576 rc = reparse_unary_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002577 LY_CHECK_RET(rc);
2578 }
2579 }
2580
2581 return LY_SUCCESS;
2582}
2583
2584/**
2585 * @brief Reparse EqualityExpr. Logs directly on error.
2586 *
2587 * [13] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
2588 * | EqualityExpr '!=' RelationalExpr
2589 * [14] RelationalExpr ::= AdditiveExpr
2590 * | RelationalExpr '<' AdditiveExpr
2591 * | RelationalExpr '>' AdditiveExpr
2592 * | RelationalExpr '<=' AdditiveExpr
2593 * | RelationalExpr '>=' AdditiveExpr
2594 *
2595 * @param[in] ctx Context for logging.
2596 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002597 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002598 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002599 * @return LY_ERR
2600 */
2601static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02002602reparse_equality_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t depth)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002603{
Michal Vaskodd528af2022-08-08 14:35:07 +02002604 uint32_t prev_eq_exp, prev_rel_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002605 LY_ERR rc;
2606
Michal Vasko004d3152020-06-11 19:59:22 +02002607 prev_eq_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002608 goto reparse_additive_expr;
2609
2610 /* ('=' / '!=' RelationalExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002611 while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_EQUAL, LYXP_TOKEN_OPER_NEQUAL)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002612 exp_repeat_push(exp, prev_eq_exp, LYXP_EXPR_EQUALITY);
Michal Vasko004d3152020-06-11 19:59:22 +02002613 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002614
2615reparse_additive_expr:
2616 /* AdditiveExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002617 prev_rel_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002618 rc = reparse_additive_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002619 LY_CHECK_RET(rc);
2620
2621 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002622 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_COMP)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002623 exp_repeat_push(exp, prev_rel_exp, LYXP_EXPR_RELATIONAL);
Michal Vasko004d3152020-06-11 19:59:22 +02002624 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002625
aPiecekbf968d92021-05-27 14:35:05 +02002626 rc = reparse_additive_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002627 LY_CHECK_RET(rc);
2628 }
2629 }
2630
2631 return LY_SUCCESS;
2632}
2633
2634/**
2635 * @brief Reparse OrExpr. Logs directly on error.
2636 *
2637 * [11] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
2638 * [12] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
2639 *
2640 * @param[in] ctx Context for logging.
2641 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002642 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002643 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002644 * @return LY_ERR
2645 */
2646static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02002647reparse_or_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t depth)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002648{
Michal Vaskodd528af2022-08-08 14:35:07 +02002649 uint32_t prev_or_exp, prev_and_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002650 LY_ERR rc;
2651
aPiecekbf968d92021-05-27 14:35:05 +02002652 ++depth;
2653 LY_CHECK_ERR_RET(depth > LYXP_MAX_BLOCK_DEPTH, LOGVAL(ctx, LY_VCODE_XP_DEPTH), LY_EINVAL);
2654
Michal Vasko004d3152020-06-11 19:59:22 +02002655 prev_or_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002656 goto reparse_equality_expr;
2657
2658 /* ('or' AndExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002659 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 +02002660 exp_repeat_push(exp, prev_or_exp, LYXP_EXPR_OR);
Michal Vasko004d3152020-06-11 19:59:22 +02002661 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002662
2663reparse_equality_expr:
2664 /* EqualityExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002665 prev_and_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002666 rc = reparse_equality_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002667 LY_CHECK_RET(rc);
2668
2669 /* ('and' EqualityExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002670 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 +02002671 exp_repeat_push(exp, prev_and_exp, LYXP_EXPR_AND);
Michal Vasko004d3152020-06-11 19:59:22 +02002672 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002673
aPiecekbf968d92021-05-27 14:35:05 +02002674 rc = reparse_equality_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002675 LY_CHECK_RET(rc);
2676 }
2677 }
2678
2679 return LY_SUCCESS;
2680}
Radek Krejcib1646a92018-11-02 16:08:26 +01002681
2682/**
2683 * @brief Parse NCName.
2684 *
2685 * @param[in] ncname Name to parse.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002686 * @return Length of @p ncname valid bytes.
Radek Krejcib1646a92018-11-02 16:08:26 +01002687 */
Michal Vasko49fec8e2022-05-24 10:28:33 +02002688static ssize_t
Radek Krejcib1646a92018-11-02 16:08:26 +01002689parse_ncname(const char *ncname)
2690{
Radek Krejci1deb5be2020-08-26 16:43:36 +02002691 uint32_t uc;
Radek Krejcid4270262019-01-07 15:07:25 +01002692 size_t size;
Michal Vasko49fec8e2022-05-24 10:28:33 +02002693 ssize_t len = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002694
2695 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), 0);
2696 if (!is_xmlqnamestartchar(uc) || (uc == ':')) {
2697 return len;
2698 }
2699
2700 do {
2701 len += size;
Radek Krejci9a564c92019-01-07 14:53:57 +01002702 if (!*ncname) {
2703 break;
2704 }
Radek Krejcid4270262019-01-07 15:07:25 +01002705 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), -len);
Radek Krejcib1646a92018-11-02 16:08:26 +01002706 } while (is_xmlqnamechar(uc) && (uc != ':'));
2707
2708 return len;
2709}
2710
2711/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02002712 * @brief Add @p token into the expression @p exp.
Radek Krejcib1646a92018-11-02 16:08:26 +01002713 *
Michal Vasko03ff5a72019-09-11 13:49:33 +02002714 * @param[in] ctx Context for logging.
Radek Krejcib1646a92018-11-02 16:08:26 +01002715 * @param[in] exp Expression to use.
2716 * @param[in] token Token to add.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002717 * @param[in] tok_pos Token position in the XPath expression.
Radek Krejcib1646a92018-11-02 16:08:26 +01002718 * @param[in] tok_len Token length in the XPath expression.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002719 * @return LY_ERR
Radek Krejcib1646a92018-11-02 16:08:26 +01002720 */
2721static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02002722exp_add_token(const struct ly_ctx *ctx, struct lyxp_expr *exp, enum lyxp_token token, uint32_t tok_pos, uint32_t tok_len)
Radek Krejcib1646a92018-11-02 16:08:26 +01002723{
2724 uint32_t prev;
2725
2726 if (exp->used == exp->size) {
2727 prev = exp->size;
2728 exp->size += LYXP_EXPR_SIZE_STEP;
2729 if (prev > exp->size) {
2730 LOGINT(ctx);
2731 return LY_EINT;
2732 }
2733
2734 exp->tokens = ly_realloc(exp->tokens, exp->size * sizeof *exp->tokens);
2735 LY_CHECK_ERR_RET(!exp->tokens, LOGMEM(ctx), LY_EMEM);
2736 exp->tok_pos = ly_realloc(exp->tok_pos, exp->size * sizeof *exp->tok_pos);
2737 LY_CHECK_ERR_RET(!exp->tok_pos, LOGMEM(ctx), LY_EMEM);
2738 exp->tok_len = ly_realloc(exp->tok_len, exp->size * sizeof *exp->tok_len);
2739 LY_CHECK_ERR_RET(!exp->tok_len, LOGMEM(ctx), LY_EMEM);
2740 }
2741
2742 exp->tokens[exp->used] = token;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002743 exp->tok_pos[exp->used] = tok_pos;
Radek Krejcib1646a92018-11-02 16:08:26 +01002744 exp->tok_len[exp->used] = tok_len;
2745 ++exp->used;
2746 return LY_SUCCESS;
2747}
2748
2749void
Michal Vasko14676352020-05-29 11:35:55 +02002750lyxp_expr_free(const struct ly_ctx *ctx, struct lyxp_expr *expr)
Radek Krejcib1646a92018-11-02 16:08:26 +01002751{
Michal Vaskodd528af2022-08-08 14:35:07 +02002752 uint32_t i;
Radek Krejcib1646a92018-11-02 16:08:26 +01002753
2754 if (!expr) {
2755 return;
2756 }
2757
2758 lydict_remove(ctx, expr->expr);
2759 free(expr->tokens);
2760 free(expr->tok_pos);
2761 free(expr->tok_len);
2762 if (expr->repeat) {
2763 for (i = 0; i < expr->used; ++i) {
2764 free(expr->repeat[i]);
2765 }
2766 }
2767 free(expr->repeat);
2768 free(expr);
2769}
2770
Michal Vasko49fec8e2022-05-24 10:28:33 +02002771/**
2772 * @brief Parse Axis name.
2773 *
2774 * @param[in] str String to parse.
2775 * @param[in] str_len Length of @p str.
2776 * @return LY_SUCCESS if an axis.
2777 * @return LY_ENOT otherwise.
2778 */
2779static LY_ERR
2780expr_parse_axis(const char *str, size_t str_len)
2781{
2782 switch (str_len) {
2783 case 4:
2784 if (!strncmp("self", str, str_len)) {
2785 return LY_SUCCESS;
2786 }
2787 break;
2788 case 5:
2789 if (!strncmp("child", str, str_len)) {
2790 return LY_SUCCESS;
2791 }
2792 break;
2793 case 6:
2794 if (!strncmp("parent", str, str_len)) {
2795 return LY_SUCCESS;
2796 }
2797 break;
2798 case 8:
2799 if (!strncmp("ancestor", str, str_len)) {
2800 return LY_SUCCESS;
2801 }
2802 break;
2803 case 9:
2804 if (!strncmp("attribute", str, str_len)) {
2805 return LY_SUCCESS;
2806 } else if (!strncmp("following", str, str_len)) {
2807 return LY_SUCCESS;
2808 } else if (!strncmp("namespace", str, str_len)) {
2809 LOGERR(NULL, LY_EINVAL, "Axis \"namespace\" not supported.");
2810 return LY_ENOT;
2811 } else if (!strncmp("preceding", str, str_len)) {
2812 return LY_SUCCESS;
2813 }
2814 break;
2815 case 10:
2816 if (!strncmp("descendant", str, str_len)) {
2817 return LY_SUCCESS;
2818 }
2819 break;
2820 case 16:
2821 if (!strncmp("ancestor-or-self", str, str_len)) {
2822 return LY_SUCCESS;
2823 }
2824 break;
2825 case 17:
2826 if (!strncmp("following-sibling", str, str_len)) {
2827 return LY_SUCCESS;
2828 } else if (!strncmp("preceding-sibling", str, str_len)) {
2829 return LY_SUCCESS;
2830 }
2831 break;
2832 case 18:
2833 if (!strncmp("descendant-or-self", str, str_len)) {
2834 return LY_SUCCESS;
2835 }
2836 break;
2837 }
2838
2839 return LY_ENOT;
2840}
2841
Radek Krejcif03a9e22020-09-18 20:09:31 +02002842LY_ERR
2843lyxp_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 +01002844{
Radek Krejcif03a9e22020-09-18 20:09:31 +02002845 LY_ERR ret = LY_SUCCESS;
2846 struct lyxp_expr *expr;
Radek Krejcid4270262019-01-07 15:07:25 +01002847 size_t parsed = 0, tok_len;
Radek Krejcib1646a92018-11-02 16:08:26 +01002848 enum lyxp_token tok_type;
Michal Vasko49fec8e2022-05-24 10:28:33 +02002849 ly_bool prev_func_check = 0, prev_ntype_check = 0, has_axis;
Michal Vaskodd528af2022-08-08 14:35:07 +02002850 uint32_t tok_idx = 0;
Michal Vasko49fec8e2022-05-24 10:28:33 +02002851 ssize_t ncname_len;
Radek Krejcib1646a92018-11-02 16:08:26 +01002852
Radek Krejcif03a9e22020-09-18 20:09:31 +02002853 assert(expr_p);
2854
2855 if (!expr_str[0]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002856 LOGVAL(ctx, LY_VCODE_XP_EOF);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002857 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02002858 }
2859
2860 if (!expr_len) {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002861 expr_len = strlen(expr_str);
Michal Vasko004d3152020-06-11 19:59:22 +02002862 }
Michal Vaskodd528af2022-08-08 14:35:07 +02002863 if (expr_len > UINT32_MAX) {
2864 LOGVAL(ctx, LYVE_XPATH, "XPath expression cannot be longer than %" PRIu32 " characters.", UINT32_MAX);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002865 return LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01002866 }
2867
2868 /* init lyxp_expr structure */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002869 expr = calloc(1, sizeof *expr);
2870 LY_CHECK_ERR_GOTO(!expr, LOGMEM(ctx); ret = LY_EMEM, error);
2871 LY_CHECK_GOTO(ret = lydict_insert(ctx, expr_str, expr_len, &expr->expr), error);
2872 expr->used = 0;
2873 expr->size = LYXP_EXPR_SIZE_START;
2874 expr->tokens = malloc(expr->size * sizeof *expr->tokens);
2875 LY_CHECK_ERR_GOTO(!expr->tokens, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002876
Radek Krejcif03a9e22020-09-18 20:09:31 +02002877 expr->tok_pos = malloc(expr->size * sizeof *expr->tok_pos);
2878 LY_CHECK_ERR_GOTO(!expr->tok_pos, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002879
Radek Krejcif03a9e22020-09-18 20:09:31 +02002880 expr->tok_len = malloc(expr->size * sizeof *expr->tok_len);
2881 LY_CHECK_ERR_GOTO(!expr->tok_len, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002882
Michal Vasko004d3152020-06-11 19:59:22 +02002883 /* make expr 0-terminated */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002884 expr_str = expr->expr;
Michal Vasko004d3152020-06-11 19:59:22 +02002885
Radek Krejcif03a9e22020-09-18 20:09:31 +02002886 while (is_xmlws(expr_str[parsed])) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002887 ++parsed;
2888 }
2889
2890 do {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002891 if (expr_str[parsed] == '(') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002892
2893 /* '(' */
2894 tok_len = 1;
2895 tok_type = LYXP_TOKEN_PAR1;
2896
Michal Vasko49fec8e2022-05-24 10:28:33 +02002897 if (prev_ntype_check && expr->used && (expr->tokens[expr->used - 1] == LYXP_TOKEN_NAMETEST) &&
2898 (((expr->tok_len[expr->used - 1] == 4) &&
2899 (!strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "node", 4) ||
2900 !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "text", 4))) ||
2901 ((expr->tok_len[expr->used - 1] == 7) &&
2902 !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "comment", 7)))) {
2903 /* it is NodeType after all */
2904 expr->tokens[expr->used - 1] = LYXP_TOKEN_NODETYPE;
2905
2906 prev_ntype_check = 0;
2907 prev_func_check = 0;
2908 } else if (prev_func_check && expr->used && (expr->tokens[expr->used - 1] == LYXP_TOKEN_NAMETEST)) {
2909 /* it is FunctionName after all */
2910 expr->tokens[expr->used - 1] = LYXP_TOKEN_FUNCNAME;
2911
2912 prev_ntype_check = 0;
2913 prev_func_check = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002914 }
2915
Radek Krejcif03a9e22020-09-18 20:09:31 +02002916 } else if (expr_str[parsed] == ')') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002917
2918 /* ')' */
2919 tok_len = 1;
2920 tok_type = LYXP_TOKEN_PAR2;
2921
Radek Krejcif03a9e22020-09-18 20:09:31 +02002922 } else if (expr_str[parsed] == '[') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002923
2924 /* '[' */
2925 tok_len = 1;
2926 tok_type = LYXP_TOKEN_BRACK1;
2927
Radek Krejcif03a9e22020-09-18 20:09:31 +02002928 } else if (expr_str[parsed] == ']') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002929
2930 /* ']' */
2931 tok_len = 1;
2932 tok_type = LYXP_TOKEN_BRACK2;
2933
Radek Krejcif03a9e22020-09-18 20:09:31 +02002934 } else if (!strncmp(&expr_str[parsed], "..", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002935
2936 /* '..' */
2937 tok_len = 2;
2938 tok_type = LYXP_TOKEN_DDOT;
2939
Radek Krejcif03a9e22020-09-18 20:09:31 +02002940 } else if ((expr_str[parsed] == '.') && (!isdigit(expr_str[parsed + 1]))) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002941
2942 /* '.' */
2943 tok_len = 1;
2944 tok_type = LYXP_TOKEN_DOT;
2945
Radek Krejcif03a9e22020-09-18 20:09:31 +02002946 } else if (expr_str[parsed] == '@') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002947
2948 /* '@' */
2949 tok_len = 1;
2950 tok_type = LYXP_TOKEN_AT;
2951
Radek Krejcif03a9e22020-09-18 20:09:31 +02002952 } else if (expr_str[parsed] == ',') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002953
2954 /* ',' */
2955 tok_len = 1;
2956 tok_type = LYXP_TOKEN_COMMA;
2957
Radek Krejcif03a9e22020-09-18 20:09:31 +02002958 } else if (expr_str[parsed] == '\'') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002959
2960 /* Literal with ' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002961 for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\''); ++tok_len) {}
2962 LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0',
Radek Krejci2efc45b2020-12-22 16:25:44 +01002963 LOGVAL(ctx, LY_VCODE_XP_EOE, expr_str[parsed], &expr_str[parsed]); ret = LY_EVALID,
Michal Vasko69730152020-10-09 16:30:07 +02002964 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002965 ++tok_len;
2966 tok_type = LYXP_TOKEN_LITERAL;
2967
Radek Krejcif03a9e22020-09-18 20:09:31 +02002968 } else if (expr_str[parsed] == '\"') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002969
2970 /* Literal with " */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002971 for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\"'); ++tok_len) {}
2972 LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0',
Radek Krejci2efc45b2020-12-22 16:25:44 +01002973 LOGVAL(ctx, LY_VCODE_XP_EOE, expr_str[parsed], &expr_str[parsed]); ret = LY_EVALID,
Michal Vasko69730152020-10-09 16:30:07 +02002974 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002975 ++tok_len;
2976 tok_type = LYXP_TOKEN_LITERAL;
2977
Radek Krejcif03a9e22020-09-18 20:09:31 +02002978 } else if ((expr_str[parsed] == '.') || (isdigit(expr_str[parsed]))) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002979
2980 /* Number */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002981 for (tok_len = 0; isdigit(expr_str[parsed + tok_len]); ++tok_len) {}
2982 if (expr_str[parsed + tok_len] == '.') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002983 ++tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02002984 for ( ; isdigit(expr_str[parsed + tok_len]); ++tok_len) {}
Radek Krejcib1646a92018-11-02 16:08:26 +01002985 }
2986 tok_type = LYXP_TOKEN_NUMBER;
2987
aPiecekfba75362021-10-07 12:39:48 +02002988 } else if (expr_str[parsed] == '$') {
2989
2990 /* VariableReference */
2991 parsed++;
Michal Vasko49fec8e2022-05-24 10:28:33 +02002992 ncname_len = parse_ncname(&expr_str[parsed]);
aPiecekfba75362021-10-07 12:39:48 +02002993 LY_CHECK_ERR_GOTO(ncname_len < 1, LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed - ncname_len],
Michal Vasko7b3a00e2023-08-09 11:58:03 +02002994 (uint32_t)(parsed - ncname_len + 1), expr_str); ret = LY_EVALID, error);
aPiecekfba75362021-10-07 12:39:48 +02002995 tok_len = ncname_len;
2996 LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == ':',
2997 LOGVAL(ctx, LYVE_XPATH, "Variable with prefix is not supported."); ret = LY_EVALID,
2998 error);
2999 tok_type = LYXP_TOKEN_VARREF;
3000
Radek Krejcif03a9e22020-09-18 20:09:31 +02003001 } else if (expr_str[parsed] == '/') {
Radek Krejcib1646a92018-11-02 16:08:26 +01003002
3003 /* Operator '/', '//' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02003004 if (!strncmp(&expr_str[parsed], "//", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01003005 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003006 tok_type = LYXP_TOKEN_OPER_RPATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01003007 } else {
3008 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003009 tok_type = LYXP_TOKEN_OPER_PATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01003010 }
Radek Krejcib1646a92018-11-02 16:08:26 +01003011
Radek Krejcif03a9e22020-09-18 20:09:31 +02003012 } else if (!strncmp(&expr_str[parsed], "!=", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01003013
Michal Vasko3e48bf32020-06-01 08:39:07 +02003014 /* Operator '!=' */
Radek Krejcib1646a92018-11-02 16:08:26 +01003015 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003016 tok_type = LYXP_TOKEN_OPER_NEQUAL;
3017
Radek Krejcif03a9e22020-09-18 20:09:31 +02003018 } else if (!strncmp(&expr_str[parsed], "<=", 2) || !strncmp(&expr_str[parsed], ">=", 2)) {
Michal Vasko3e48bf32020-06-01 08:39:07 +02003019
3020 /* Operator '<=', '>=' */
3021 tok_len = 2;
3022 tok_type = LYXP_TOKEN_OPER_COMP;
Radek Krejcib1646a92018-11-02 16:08:26 +01003023
Radek Krejcif03a9e22020-09-18 20:09:31 +02003024 } else if (expr_str[parsed] == '|') {
Radek Krejcib1646a92018-11-02 16:08:26 +01003025
3026 /* Operator '|' */
3027 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003028 tok_type = LYXP_TOKEN_OPER_UNI;
Radek Krejcib1646a92018-11-02 16:08:26 +01003029
Radek Krejcif03a9e22020-09-18 20:09:31 +02003030 } else if ((expr_str[parsed] == '+') || (expr_str[parsed] == '-')) {
Radek Krejcib1646a92018-11-02 16:08:26 +01003031
3032 /* Operator '+', '-' */
3033 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003034 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01003035
Radek Krejcif03a9e22020-09-18 20:09:31 +02003036 } else if (expr_str[parsed] == '=') {
Radek Krejcib1646a92018-11-02 16:08:26 +01003037
Michal Vasko3e48bf32020-06-01 08:39:07 +02003038 /* Operator '=' */
Radek Krejcib1646a92018-11-02 16:08:26 +01003039 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003040 tok_type = LYXP_TOKEN_OPER_EQUAL;
3041
Radek Krejcif03a9e22020-09-18 20:09:31 +02003042 } else if ((expr_str[parsed] == '<') || (expr_str[parsed] == '>')) {
Michal Vasko3e48bf32020-06-01 08:39:07 +02003043
3044 /* Operator '<', '>' */
3045 tok_len = 1;
3046 tok_type = LYXP_TOKEN_OPER_COMP;
Radek Krejcib1646a92018-11-02 16:08:26 +01003047
Michal Vasko69730152020-10-09 16:30:07 +02003048 } else if (expr->used && (expr->tokens[expr->used - 1] != LYXP_TOKEN_AT) &&
3049 (expr->tokens[expr->used - 1] != LYXP_TOKEN_PAR1) &&
3050 (expr->tokens[expr->used - 1] != LYXP_TOKEN_BRACK1) &&
3051 (expr->tokens[expr->used - 1] != LYXP_TOKEN_COMMA) &&
3052 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_LOG) &&
3053 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_EQUAL) &&
3054 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_NEQUAL) &&
3055 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_COMP) &&
3056 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_MATH) &&
3057 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_UNI) &&
3058 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_PATH) &&
3059 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_RPATH)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01003060
3061 /* Operator '*', 'or', 'and', 'mod', or 'div' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02003062 if (expr_str[parsed] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01003063 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003064 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01003065
Radek Krejcif03a9e22020-09-18 20:09:31 +02003066 } else if (!strncmp(&expr_str[parsed], "or", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01003067 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003068 tok_type = LYXP_TOKEN_OPER_LOG;
Radek Krejcib1646a92018-11-02 16:08:26 +01003069
Radek Krejcif03a9e22020-09-18 20:09:31 +02003070 } else if (!strncmp(&expr_str[parsed], "and", 3)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01003071 tok_len = 3;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003072 tok_type = LYXP_TOKEN_OPER_LOG;
Radek Krejcib1646a92018-11-02 16:08:26 +01003073
Radek Krejcif03a9e22020-09-18 20:09:31 +02003074 } else if (!strncmp(&expr_str[parsed], "mod", 3) || !strncmp(&expr_str[parsed], "div", 3)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01003075 tok_len = 3;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003076 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01003077
Michal Vasko49fec8e2022-05-24 10:28:33 +02003078 } else if (prev_ntype_check || prev_func_check) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003079 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 +02003080 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 +02003081 ret = LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01003082 goto error;
3083 } else {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02003084 LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed], (uint32_t)(parsed + 1), expr_str);
Radek Krejcif03a9e22020-09-18 20:09:31 +02003085 ret = LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01003086 goto error;
3087 }
Radek Krejcib1646a92018-11-02 16:08:26 +01003088 } else {
3089
Michal Vasko49fec8e2022-05-24 10:28:33 +02003090 /* (AxisName '::')? ((NCName ':')? '*' | QName) or NodeType/FunctionName */
3091 if (expr_str[parsed] == '*') {
3092 ncname_len = 1;
3093 } else {
3094 ncname_len = parse_ncname(&expr_str[parsed]);
3095 LY_CHECK_ERR_GOTO(ncname_len < 1, LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed - ncname_len],
Michal Vasko7b3a00e2023-08-09 11:58:03 +02003096 (uint32_t)(parsed - ncname_len + 1), expr_str); ret = LY_EVALID, error);
Michal Vasko49fec8e2022-05-24 10:28:33 +02003097 }
Radek Krejcib1646a92018-11-02 16:08:26 +01003098 tok_len = ncname_len;
3099
Michal Vasko49fec8e2022-05-24 10:28:33 +02003100 has_axis = 0;
3101 if (!strncmp(&expr_str[parsed + tok_len], "::", 2)) {
3102 /* axis */
3103 LY_CHECK_ERR_GOTO(expr_parse_axis(&expr_str[parsed], ncname_len),
Michal Vasko7b3a00e2023-08-09 11:58:03 +02003104 LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed], (uint32_t)(parsed + 1), expr_str); ret = LY_EVALID,
3105 error);
Michal Vasko49fec8e2022-05-24 10:28:33 +02003106 tok_type = LYXP_TOKEN_AXISNAME;
3107
3108 LY_CHECK_GOTO(ret = exp_add_token(ctx, expr, tok_type, parsed, tok_len), error);
3109 parsed += tok_len;
3110
3111 /* '::' */
3112 tok_len = 2;
3113 tok_type = LYXP_TOKEN_DCOLON;
3114
3115 LY_CHECK_GOTO(ret = exp_add_token(ctx, expr, tok_type, parsed, tok_len), error);
3116 parsed += tok_len;
3117
3118 if (expr_str[parsed] == '*') {
3119 ncname_len = 1;
3120 } else {
3121 ncname_len = parse_ncname(&expr_str[parsed]);
3122 LY_CHECK_ERR_GOTO(ncname_len < 1, LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed - ncname_len],
Michal Vasko7b3a00e2023-08-09 11:58:03 +02003123 (uint32_t)(parsed - ncname_len + 1), expr_str); ret = LY_EVALID, error);
Michal Vasko49fec8e2022-05-24 10:28:33 +02003124 }
3125 tok_len = ncname_len;
3126
3127 has_axis = 1;
3128 }
3129
Radek Krejcif03a9e22020-09-18 20:09:31 +02003130 if (expr_str[parsed + tok_len] == ':') {
Radek Krejcib1646a92018-11-02 16:08:26 +01003131 ++tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02003132 if (expr_str[parsed + tok_len] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01003133 ++tok_len;
3134 } else {
Radek Krejcif03a9e22020-09-18 20:09:31 +02003135 ncname_len = parse_ncname(&expr_str[parsed + tok_len]);
Michal Vaskoe2be5462021-08-04 10:49:42 +02003136 LY_CHECK_ERR_GOTO(ncname_len < 1, LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed - ncname_len],
Michal Vasko7b3a00e2023-08-09 11:58:03 +02003137 (uint32_t)(parsed - ncname_len + 1), expr_str); ret = LY_EVALID, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01003138 tok_len += ncname_len;
3139 }
Michal Vasko49fec8e2022-05-24 10:28:33 +02003140 /* remove old flags to prevent ambiguities */
3141 prev_ntype_check = 0;
3142 prev_func_check = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01003143 tok_type = LYXP_TOKEN_NAMETEST;
3144 } else {
Michal Vasko49fec8e2022-05-24 10:28:33 +02003145 /* if not '*', there is no prefix so it can still be NodeType/FunctionName, we can't finally decide now */
3146 prev_ntype_check = (expr_str[parsed] == '*') ? 0 : 1;
3147 prev_func_check = (prev_ntype_check && !has_axis) ? 1 : 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01003148 tok_type = LYXP_TOKEN_NAMETEST;
3149 }
3150 }
3151
3152 /* store the token, move on to the next one */
Radek Krejcif03a9e22020-09-18 20:09:31 +02003153 LY_CHECK_GOTO(ret = exp_add_token(ctx, expr, tok_type, parsed, tok_len), error);
Radek Krejcib1646a92018-11-02 16:08:26 +01003154 parsed += tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02003155 while (is_xmlws(expr_str[parsed])) {
Radek Krejcib1646a92018-11-02 16:08:26 +01003156 ++parsed;
3157 }
3158
Radek Krejcif03a9e22020-09-18 20:09:31 +02003159 } while (expr_str[parsed]);
Radek Krejcib1646a92018-11-02 16:08:26 +01003160
Michal Vasko004d3152020-06-11 19:59:22 +02003161 if (reparse) {
3162 /* prealloc repeat */
Radek Krejcif03a9e22020-09-18 20:09:31 +02003163 expr->repeat = calloc(expr->size, sizeof *expr->repeat);
3164 LY_CHECK_ERR_GOTO(!expr->repeat, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01003165
Michal Vasko004d3152020-06-11 19:59:22 +02003166 /* fill repeat */
aPiecekbf968d92021-05-27 14:35:05 +02003167 LY_CHECK_ERR_GOTO(reparse_or_expr(ctx, expr, &tok_idx, 0), ret = LY_EVALID, error);
Radek Krejcif03a9e22020-09-18 20:09:31 +02003168 if (expr->used > tok_idx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003169 LOGVAL(ctx, LYVE_XPATH, "Unparsed characters \"%s\" left at the end of an XPath expression.",
Michal Vasko69730152020-10-09 16:30:07 +02003170 &expr->expr[expr->tok_pos[tok_idx]]);
Radek Krejcif03a9e22020-09-18 20:09:31 +02003171 ret = LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02003172 goto error;
3173 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003174 }
3175
Radek Krejcif03a9e22020-09-18 20:09:31 +02003176 print_expr_struct_debug(expr);
3177 *expr_p = expr;
3178 return LY_SUCCESS;
Radek Krejcib1646a92018-11-02 16:08:26 +01003179
3180error:
Radek Krejcif03a9e22020-09-18 20:09:31 +02003181 lyxp_expr_free(ctx, expr);
3182 return ret;
Radek Krejcib1646a92018-11-02 16:08:26 +01003183}
3184
Michal Vasko1734be92020-09-22 08:55:10 +02003185LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02003186lyxp_expr_dup(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint32_t start_idx, uint32_t end_idx,
Michal Vaskoe33134a2022-07-29 14:54:40 +02003187 struct lyxp_expr **dup_p)
Michal Vasko004d3152020-06-11 19:59:22 +02003188{
Michal Vasko1734be92020-09-22 08:55:10 +02003189 LY_ERR ret = LY_SUCCESS;
3190 struct lyxp_expr *dup = NULL;
Michal Vaskod59039d2022-09-09 09:07:30 +02003191 uint32_t used = 0, i, j, expr_len;
Michal Vaskoe33134a2022-07-29 14:54:40 +02003192 const char *expr_start;
3193
3194 assert((!start_idx && !end_idx) || ((start_idx < exp->used) && (end_idx < exp->used) && (start_idx <= end_idx)));
Michal Vasko004d3152020-06-11 19:59:22 +02003195
Michal Vasko7f45cf22020-10-01 12:49:44 +02003196 if (!exp) {
3197 goto cleanup;
3198 }
3199
Michal Vaskoe33134a2022-07-29 14:54:40 +02003200 if (!start_idx && !end_idx) {
3201 end_idx = exp->used - 1;
3202 }
3203
3204 expr_start = exp->expr + exp->tok_pos[start_idx];
3205 expr_len = (exp->tok_pos[end_idx] + exp->tok_len[end_idx]) - exp->tok_pos[start_idx];
3206
Michal Vasko004d3152020-06-11 19:59:22 +02003207 dup = calloc(1, sizeof *dup);
Michal Vasko1734be92020-09-22 08:55:10 +02003208 LY_CHECK_ERR_GOTO(!dup, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02003209
Michal Vasko08e9b112021-06-11 15:41:17 +02003210 if (exp->used) {
Michal Vaskoe33134a2022-07-29 14:54:40 +02003211 used = (end_idx - start_idx) + 1;
3212
3213 dup->tokens = malloc(used * sizeof *dup->tokens);
Michal Vasko08e9b112021-06-11 15:41:17 +02003214 LY_CHECK_ERR_GOTO(!dup->tokens, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vaskoe33134a2022-07-29 14:54:40 +02003215 memcpy(dup->tokens, exp->tokens + start_idx, used * sizeof *dup->tokens);
Michal Vasko004d3152020-06-11 19:59:22 +02003216
Michal Vaskoe33134a2022-07-29 14:54:40 +02003217 dup->tok_pos = malloc(used * sizeof *dup->tok_pos);
Michal Vasko08e9b112021-06-11 15:41:17 +02003218 LY_CHECK_ERR_GOTO(!dup->tok_pos, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vaskoe33134a2022-07-29 14:54:40 +02003219 memcpy(dup->tok_pos, exp->tok_pos + start_idx, used * sizeof *dup->tok_pos);
Michal Vasko004d3152020-06-11 19:59:22 +02003220
Michal Vaskoe33134a2022-07-29 14:54:40 +02003221 if (start_idx) {
3222 /* fix the indices in the expression */
3223 for (i = 0; i < used; ++i) {
3224 dup->tok_pos[i] -= expr_start - exp->expr;
3225 }
3226 }
3227
3228 dup->tok_len = malloc(used * sizeof *dup->tok_len);
Michal Vasko08e9b112021-06-11 15:41:17 +02003229 LY_CHECK_ERR_GOTO(!dup->tok_len, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vaskoe33134a2022-07-29 14:54:40 +02003230 memcpy(dup->tok_len, exp->tok_len + start_idx, used * sizeof *dup->tok_len);
Michal Vasko004d3152020-06-11 19:59:22 +02003231
Michal Vasko79a7a872022-06-17 09:00:48 +02003232 if (exp->repeat) {
Michal Vaskoe33134a2022-07-29 14:54:40 +02003233 dup->repeat = malloc(used * sizeof *dup->repeat);
Michal Vasko79a7a872022-06-17 09:00:48 +02003234 LY_CHECK_ERR_GOTO(!dup->repeat, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vaskoe33134a2022-07-29 14:54:40 +02003235 for (i = start_idx; i <= end_idx; ++i) {
Michal Vasko79a7a872022-06-17 09:00:48 +02003236 if (!exp->repeat[i]) {
Michal Vaskoe33134a2022-07-29 14:54:40 +02003237 dup->repeat[i - start_idx] = NULL;
Michal Vasko79a7a872022-06-17 09:00:48 +02003238 } else {
3239 for (j = 0; exp->repeat[i][j]; ++j) {}
3240 /* the ending 0 as well */
3241 ++j;
Michal Vasko004d3152020-06-11 19:59:22 +02003242
Michal Vaskoe33134a2022-07-29 14:54:40 +02003243 dup->repeat[i - start_idx] = malloc(j * sizeof **dup->repeat);
3244 LY_CHECK_ERR_GOTO(!dup->repeat[i - start_idx], LOGMEM(ctx); ret = LY_EMEM, cleanup);
3245 memcpy(dup->repeat[i - start_idx], exp->repeat[i], j * sizeof **dup->repeat);
Michal Vasko79a7a872022-06-17 09:00:48 +02003246 }
Michal Vasko08e9b112021-06-11 15:41:17 +02003247 }
Michal Vasko004d3152020-06-11 19:59:22 +02003248 }
3249 }
3250
Michal Vaskoe33134a2022-07-29 14:54:40 +02003251 dup->used = used;
3252 dup->size = used;
3253
3254 /* copy only subexpression */
3255 LY_CHECK_GOTO(ret = lydict_insert(ctx, expr_start, expr_len, &dup->expr), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02003256
Michal Vasko1734be92020-09-22 08:55:10 +02003257cleanup:
3258 if (ret) {
3259 lyxp_expr_free(ctx, dup);
3260 } else {
3261 *dup_p = dup;
3262 }
3263 return ret;
Michal Vasko004d3152020-06-11 19:59:22 +02003264}
3265
Michal Vasko03ff5a72019-09-11 13:49:33 +02003266/**
3267 * @brief Get the last-added schema node that is currently in the context.
3268 *
3269 * @param[in] set Set to search in.
3270 * @return Last-added schema context node, NULL if no node is in context.
3271 */
3272static struct lysc_node *
3273warn_get_scnode_in_ctx(struct lyxp_set *set)
3274{
3275 uint32_t i;
3276
3277 if (!set || (set->type != LYXP_SET_SCNODE_SET)) {
3278 return NULL;
3279 }
3280
3281 i = set->used;
3282 do {
3283 --i;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003284 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003285 /* if there are more, simply return the first found (last added) */
3286 return set->val.scnodes[i].scnode;
3287 }
3288 } while (i);
3289
3290 return NULL;
3291}
3292
3293/**
3294 * @brief Test whether a type is numeric - integer type or decimal64.
3295 *
3296 * @param[in] type Type to test.
Radek Krejci857189e2020-09-01 13:26:36 +02003297 * @return Boolean value whether @p type is numeric type or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003298 */
Radek Krejci857189e2020-09-01 13:26:36 +02003299static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003300warn_is_numeric_type(struct lysc_type *type)
3301{
3302 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003303 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003304 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003305
3306 switch (type->basetype) {
3307 case LY_TYPE_DEC64:
3308 case LY_TYPE_INT8:
3309 case LY_TYPE_UINT8:
3310 case LY_TYPE_INT16:
3311 case LY_TYPE_UINT16:
3312 case LY_TYPE_INT32:
3313 case LY_TYPE_UINT32:
3314 case LY_TYPE_INT64:
3315 case LY_TYPE_UINT64:
3316 return 1;
3317 case LY_TYPE_UNION:
3318 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003319 LY_ARRAY_FOR(uni->types, u) {
3320 ret = warn_is_numeric_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003321 if (ret) {
3322 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003323 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003324 }
3325 }
3326 /* did not find any suitable type */
3327 return 0;
3328 case LY_TYPE_LEAFREF:
3329 return warn_is_numeric_type(((struct lysc_type_leafref *)type)->realtype);
3330 default:
3331 return 0;
3332 }
3333}
3334
3335/**
3336 * @brief Test whether a type is string-like - no integers, decimal64 or binary.
3337 *
3338 * @param[in] type Type to test.
Radek Krejci857189e2020-09-01 13:26:36 +02003339 * @return Boolean value whether @p type's basetype is string type or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003340 */
Radek Krejci857189e2020-09-01 13:26:36 +02003341static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003342warn_is_string_type(struct lysc_type *type)
3343{
3344 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003345 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003346 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003347
3348 switch (type->basetype) {
3349 case LY_TYPE_BITS:
3350 case LY_TYPE_ENUM:
3351 case LY_TYPE_IDENT:
3352 case LY_TYPE_INST:
3353 case LY_TYPE_STRING:
3354 return 1;
3355 case LY_TYPE_UNION:
3356 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003357 LY_ARRAY_FOR(uni->types, u) {
3358 ret = warn_is_string_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003359 if (ret) {
3360 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003361 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003362 }
3363 }
3364 /* did not find any suitable type */
3365 return 0;
3366 case LY_TYPE_LEAFREF:
3367 return warn_is_string_type(((struct lysc_type_leafref *)type)->realtype);
3368 default:
3369 return 0;
3370 }
3371}
3372
3373/**
3374 * @brief Test whether a type is one specific type.
3375 *
3376 * @param[in] type Type to test.
3377 * @param[in] base Expected type.
Radek Krejci857189e2020-09-01 13:26:36 +02003378 * @return Boolean value whether the given @p type is of the specific basetype @p base.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003379 */
Radek Krejci857189e2020-09-01 13:26:36 +02003380static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003381warn_is_specific_type(struct lysc_type *type, LY_DATA_TYPE base)
3382{
3383 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003384 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003385 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003386
3387 if (type->basetype == base) {
3388 return 1;
3389 } else if (type->basetype == LY_TYPE_UNION) {
3390 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003391 LY_ARRAY_FOR(uni->types, u) {
3392 ret = warn_is_specific_type(uni->types[u], base);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003393 if (ret) {
3394 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003395 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003396 }
3397 }
3398 /* did not find any suitable type */
3399 return 0;
3400 } else if (type->basetype == LY_TYPE_LEAFREF) {
3401 return warn_is_specific_type(((struct lysc_type_leafref *)type)->realtype, base);
3402 }
3403
3404 return 0;
3405}
3406
3407/**
3408 * @brief Get next type of a (union) type.
3409 *
3410 * @param[in] type Base type.
3411 * @param[in] prev_type Previously returned type.
3412 * @return Next type or NULL.
3413 */
3414static struct lysc_type *
3415warn_is_equal_type_next_type(struct lysc_type *type, struct lysc_type *prev_type)
3416{
3417 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003418 ly_bool found = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003419 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003420
Michal Vasko4e55f5a2022-12-14 12:15:00 +01003421 if (type->basetype == LY_TYPE_UNION) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003422 uni = (struct lysc_type_union *)type;
3423 if (!prev_type) {
3424 return uni->types[0];
3425 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003426 LY_ARRAY_FOR(uni->types, u) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003427 if (found) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003428 return uni->types[u];
Michal Vasko03ff5a72019-09-11 13:49:33 +02003429 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003430 if (prev_type == uni->types[u]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003431 found = 1;
3432 }
3433 }
3434 return NULL;
Michal Vasko4e55f5a2022-12-14 12:15:00 +01003435 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003436 if (prev_type) {
3437 assert(type == prev_type);
3438 return NULL;
3439 } else {
3440 return type;
3441 }
3442 }
3443}
3444
3445/**
3446 * @brief Test whether 2 types have a common type.
3447 *
3448 * @param[in] type1 First type.
3449 * @param[in] type2 Second type.
3450 * @return 1 if they do, 0 otherwise.
3451 */
3452static int
3453warn_is_equal_type(struct lysc_type *type1, struct lysc_type *type2)
3454{
3455 struct lysc_type *t1, *rt1, *t2, *rt2;
3456
3457 t1 = NULL;
3458 while ((t1 = warn_is_equal_type_next_type(type1, t1))) {
3459 if (t1->basetype == LY_TYPE_LEAFREF) {
3460 rt1 = ((struct lysc_type_leafref *)t1)->realtype;
3461 } else {
3462 rt1 = t1;
3463 }
3464
3465 t2 = NULL;
3466 while ((t2 = warn_is_equal_type_next_type(type2, t2))) {
3467 if (t2->basetype == LY_TYPE_LEAFREF) {
3468 rt2 = ((struct lysc_type_leafref *)t2)->realtype;
3469 } else {
3470 rt2 = t2;
3471 }
3472
3473 if (rt2->basetype == rt1->basetype) {
3474 /* match found */
3475 return 1;
3476 }
3477 }
3478 }
3479
3480 return 0;
3481}
3482
3483/**
Michal Vaskoaa956522021-11-11 10:45:34 +01003484 * @brief Print warning with information about the XPath subexpression that caused previous warning.
3485 *
3486 * @param[in] ctx Context for logging.
3487 * @param[in] tok_pos Index of the subexpression in the whole expression.
3488 * @param[in] subexpr Subexpression start.
3489 * @param[in] subexpr_len Length of @p subexpr to print.
3490 * @param[in] cur_scnode Expression context node.
3491 */
3492static void
Michal Vaskodd528af2022-08-08 14:35:07 +02003493warn_subexpr_log(const struct ly_ctx *ctx, uint32_t tok_pos, const char *subexpr, int subexpr_len,
Michal Vaskoaa956522021-11-11 10:45:34 +01003494 const struct lysc_node *cur_scnode)
3495{
3496 char *path;
3497
3498 path = lysc_path(cur_scnode, LYSC_PATH_LOG, NULL, 0);
Michal Vaskodd528af2022-08-08 14:35:07 +02003499 LOGWRN(ctx, "Previous warning generated by XPath subexpression[%" PRIu32 "] \"%.*s\" with context node \"%s\".",
Michal Vaskoaa956522021-11-11 10:45:34 +01003500 tok_pos, subexpr_len, subexpr, path);
3501 free(path);
3502}
3503
3504/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02003505 * @brief Check both operands of comparison operators.
3506 *
3507 * @param[in] ctx Context for errors.
3508 * @param[in] set1 First operand set.
3509 * @param[in] set2 Second operand set.
3510 * @param[in] numbers_only Whether accept only numbers or other types are fine too (for '=' and '!=').
3511 * @param[in] expr Start of the expression to print with the warning.
3512 * @param[in] tok_pos Token position.
3513 */
3514static void
Michal Vaskoaa956522021-11-11 10:45:34 +01003515warn_operands(struct ly_ctx *ctx, struct lyxp_set *set1, struct lyxp_set *set2, ly_bool numbers_only, const char *expr,
Michal Vaskodd528af2022-08-08 14:35:07 +02003516 uint32_t tok_pos)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003517{
3518 struct lysc_node_leaf *node1, *node2;
Radek Krejci857189e2020-09-01 13:26:36 +02003519 ly_bool leaves = 1, warning = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003520
3521 node1 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set1);
3522 node2 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set2);
3523
3524 if (!node1 && !node2) {
3525 /* no node-sets involved, nothing to do */
3526 return;
3527 }
3528
3529 if (node1) {
3530 if (!(node1->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3531 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node1->nodetype), node1->name);
3532 warning = 1;
3533 leaves = 0;
3534 } else if (numbers_only && !warn_is_numeric_type(node1->type)) {
3535 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node1->name);
3536 warning = 1;
3537 }
3538 }
3539
3540 if (node2) {
3541 if (!(node2->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3542 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node2->nodetype), node2->name);
3543 warning = 1;
3544 leaves = 0;
3545 } else if (numbers_only && !warn_is_numeric_type(node2->type)) {
3546 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node2->name);
3547 warning = 1;
3548 }
3549 }
3550
3551 if (node1 && node2 && leaves && !numbers_only) {
Michal Vasko69730152020-10-09 16:30:07 +02003552 if ((warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type)) ||
3553 (!warn_is_numeric_type(node1->type) && warn_is_numeric_type(node2->type)) ||
3554 (!warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type) &&
3555 !warn_is_equal_type(node1->type, node2->type))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003556 LOGWRN(ctx, "Incompatible types of operands \"%s\" and \"%s\" for comparison.", node1->name, node2->name);
3557 warning = 1;
3558 }
3559 }
3560
3561 if (warning) {
Michal Vaskoaa956522021-11-11 10:45:34 +01003562 warn_subexpr_log(ctx, tok_pos, expr + tok_pos, 20, set1->cur_scnode);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003563 }
3564}
3565
3566/**
3567 * @brief Check that a value is valid for a leaf. If not applicable, does nothing.
3568 *
3569 * @param[in] exp Parsed XPath expression.
3570 * @param[in] set Set with the leaf/leaf-list.
3571 * @param[in] val_exp Index of the value (literal/number) in @p exp.
3572 * @param[in] equal_exp Index of the start of the equality expression in @p exp.
3573 * @param[in] last_equal_exp Index of the end of the equality expression in @p exp.
3574 */
3575static void
Michal Vaskodd528af2022-08-08 14:35:07 +02003576warn_equality_value(const struct lyxp_expr *exp, struct lyxp_set *set, uint32_t val_exp, uint32_t equal_exp,
3577 uint32_t last_equal_exp)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003578{
3579 struct lysc_node *scnode;
3580 struct lysc_type *type;
3581 char *value;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003582 struct lyd_value storage;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003583 LY_ERR rc;
3584 struct ly_err_item *err = NULL;
3585
Michal Vasko69730152020-10-09 16:30:07 +02003586 if ((scnode = warn_get_scnode_in_ctx(set)) && (scnode->nodetype & (LYS_LEAF | LYS_LEAFLIST)) &&
3587 ((exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) || (exp->tokens[val_exp] == LYXP_TOKEN_NUMBER))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003588 /* check that the node can have the specified value */
3589 if (exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) {
3590 value = strndup(exp->expr + exp->tok_pos[val_exp] + 1, exp->tok_len[val_exp] - 2);
3591 } else {
3592 value = strndup(exp->expr + exp->tok_pos[val_exp], exp->tok_len[val_exp]);
3593 }
3594 if (!value) {
3595 LOGMEM(set->ctx);
3596 return;
3597 }
3598
3599 if ((((struct lysc_node_leaf *)scnode)->type->basetype == LY_TYPE_IDENT) && !strchr(value, ':')) {
3600 LOGWRN(set->ctx, "Identityref \"%s\" comparison with identity \"%s\" without prefix, consider adding"
Michal Vasko69730152020-10-09 16:30:07 +02003601 " a prefix or best using \"derived-from(-or-self)()\" functions.", scnode->name, value);
Michal Vaskoaa956522021-11-11 10:45:34 +01003602 warn_subexpr_log(set->ctx, exp->tok_pos[equal_exp], exp->expr + exp->tok_pos[equal_exp],
Radek Krejci0f969882020-08-21 16:56:47 +02003603 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
Michal Vaskoaa956522021-11-11 10:45:34 +01003604 set->cur_scnode);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003605 }
3606
3607 type = ((struct lysc_node_leaf *)scnode)->type;
3608 if (type->basetype != LY_TYPE_IDENT) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003609 rc = type->plugin->store(set->ctx, type, value, strlen(value), 0, set->format, set->prefix_data,
Michal Vasko405cc9e2020-12-01 12:01:27 +01003610 LYD_HINT_DATA, scnode, &storage, NULL, &err);
Michal Vaskobf42e832020-11-23 16:59:42 +01003611 if (rc == LY_EINCOMPLETE) {
3612 rc = LY_SUCCESS;
3613 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003614
3615 if (err) {
3616 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type (%s).", value, err->msg);
3617 ly_err_free(err);
3618 } else if (rc != LY_SUCCESS) {
3619 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type.", value);
3620 }
3621 if (rc != LY_SUCCESS) {
Michal Vaskoaa956522021-11-11 10:45:34 +01003622 warn_subexpr_log(set->ctx, exp->tok_pos[equal_exp], exp->expr + exp->tok_pos[equal_exp],
Radek Krejci0f969882020-08-21 16:56:47 +02003623 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
Michal Vaskoaa956522021-11-11 10:45:34 +01003624 set->cur_scnode);
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003625 } else {
3626 type->plugin->free(set->ctx, &storage);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003627 }
3628 }
3629 free(value);
3630 }
3631}
3632
3633/*
3634 * XPath functions
3635 */
3636
3637/**
3638 * @brief Execute the YANG 1.1 bit-is-set(node-set, string) function. Returns LYXP_SET_BOOLEAN
3639 * depending on whether the first node bit value from the second argument is set.
3640 *
3641 * @param[in] args Array of arguments.
3642 * @param[in] arg_count Count of elements in @p args.
3643 * @param[in,out] set Context and result set at the same time.
3644 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003645 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003646 */
3647static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02003648xpath_bit_is_set(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003649{
3650 struct lyd_node_term *leaf;
3651 struct lysc_node_leaf *sleaf;
Michal Vasko2588b952021-07-29 07:43:26 +02003652 struct lyd_value_bits *bits;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003653 LY_ERR rc = LY_SUCCESS;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003654 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003655
3656 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003657 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003658 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003659 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3660 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3661 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3662 sleaf->name);
3663 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_BITS)) {
3664 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"bits\".", __func__, sleaf->name);
3665 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003666 }
3667
3668 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3669 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003670 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3671 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003672 } else if (!warn_is_string_type(sleaf->type)) {
3673 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003674 }
3675 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003676 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003677 return rc;
3678 }
3679
Michal Vaskod3678892020-05-21 10:06:58 +02003680 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003681 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 +02003682 return LY_EVALID;
3683 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003684 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003685 LY_CHECK_RET(rc);
3686
3687 set_fill_boolean(set, 0);
Michal Vaskod3678892020-05-21 10:06:58 +02003688 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003689 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
Michal Vasko2588b952021-07-29 07:43:26 +02003690 if ((leaf->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (leaf->value.realtype->basetype == LY_TYPE_BITS)) {
3691 LYD_VALUE_GET(&leaf->value, bits);
3692 LY_ARRAY_FOR(bits->items, u) {
3693 if (!strcmp(bits->items[u]->name, args[1]->val.str)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003694 set_fill_boolean(set, 1);
3695 break;
3696 }
3697 }
3698 }
3699 }
3700
3701 return LY_SUCCESS;
3702}
3703
3704/**
3705 * @brief Execute the XPath boolean(object) function. Returns LYXP_SET_BOOLEAN
3706 * with the argument converted to boolean.
3707 *
3708 * @param[in] args Array of arguments.
3709 * @param[in] arg_count Count of elements in @p args.
3710 * @param[in,out] set Context and result set at the same time.
3711 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003712 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003713 */
3714static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02003715xpath_boolean(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003716{
3717 LY_ERR rc;
3718
3719 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02003720 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003721 return LY_SUCCESS;
3722 }
3723
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003724 rc = lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003725 LY_CHECK_RET(rc);
3726 set_fill_set(set, args[0]);
3727
3728 return LY_SUCCESS;
3729}
3730
3731/**
3732 * @brief Execute the XPath ceiling(number) function. Returns LYXP_SET_NUMBER
3733 * with the first argument rounded up to the nearest integer.
3734 *
3735 * @param[in] args Array of arguments.
3736 * @param[in] arg_count Count of elements in @p args.
3737 * @param[in,out] set Context and result set at the same time.
3738 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003739 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003740 */
3741static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02003742xpath_ceiling(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003743{
3744 struct lysc_node_leaf *sleaf;
3745 LY_ERR rc = LY_SUCCESS;
3746
3747 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003748 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003749 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003750 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3751 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3752 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3753 sleaf->name);
3754 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
3755 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
3756 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003757 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003758 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003759 return rc;
3760 }
3761
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003762 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003763 LY_CHECK_RET(rc);
3764 if ((long long)args[0]->val.num != args[0]->val.num) {
3765 set_fill_number(set, ((long long)args[0]->val.num) + 1);
3766 } else {
3767 set_fill_number(set, args[0]->val.num);
3768 }
3769
3770 return LY_SUCCESS;
3771}
3772
3773/**
3774 * @brief Execute the XPath concat(string, string, string*) function.
3775 * Returns LYXP_SET_STRING with the concatenation of all the arguments.
3776 *
3777 * @param[in] args Array of arguments.
3778 * @param[in] arg_count Count of elements in @p args.
3779 * @param[in,out] set Context and result set at the same time.
3780 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003781 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003782 */
3783static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02003784xpath_concat(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003785{
Michal Vaskodd528af2022-08-08 14:35:07 +02003786 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003787 char *str = NULL;
3788 size_t used = 1;
3789 LY_ERR rc = LY_SUCCESS;
3790 struct lysc_node_leaf *sleaf;
3791
3792 if (options & LYXP_SCNODE_ALL) {
3793 for (i = 0; i < arg_count; ++i) {
3794 if ((args[i]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[i]))) {
3795 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3796 LOGWRN(set->ctx, "Argument #%u of %s is a %s node \"%s\".",
Michal Vasko69730152020-10-09 16:30:07 +02003797 i + 1, __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003798 } else if (!warn_is_string_type(sleaf->type)) {
Radek Krejci70124c82020-08-14 22:17:03 +02003799 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 +02003800 }
3801 }
3802 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003803 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003804 return rc;
3805 }
3806
3807 for (i = 0; i < arg_count; ++i) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003808 rc = lyxp_set_cast(args[i], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003809 if (rc != LY_SUCCESS) {
3810 free(str);
3811 return rc;
3812 }
3813
3814 str = ly_realloc(str, (used + strlen(args[i]->val.str)) * sizeof(char));
3815 LY_CHECK_ERR_RET(!str, LOGMEM(set->ctx), LY_EMEM);
3816 strcpy(str + used - 1, args[i]->val.str);
3817 used += strlen(args[i]->val.str);
3818 }
3819
3820 /* free, kind of */
Michal Vaskod3678892020-05-21 10:06:58 +02003821 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003822 set->type = LYXP_SET_STRING;
3823 set->val.str = str;
3824
3825 return LY_SUCCESS;
3826}
3827
3828/**
3829 * @brief Execute the XPath contains(string, string) function.
3830 * Returns LYXP_SET_BOOLEAN whether the second argument can
3831 * be found in the first or not.
3832 *
3833 * @param[in] args Array of arguments.
3834 * @param[in] arg_count Count of elements in @p args.
3835 * @param[in,out] set Context and result set at the same time.
3836 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003837 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003838 */
3839static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02003840xpath_contains(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003841{
3842 struct lysc_node_leaf *sleaf;
3843 LY_ERR rc = LY_SUCCESS;
3844
3845 if (options & LYXP_SCNODE_ALL) {
3846 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3847 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003848 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3849 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003850 } else if (!warn_is_string_type(sleaf->type)) {
3851 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003852 }
3853 }
3854
3855 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3856 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003857 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3858 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003859 } else if (!warn_is_string_type(sleaf->type)) {
3860 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003861 }
3862 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003863 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003864 return rc;
3865 }
3866
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003867 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003868 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003869 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003870 LY_CHECK_RET(rc);
3871
3872 if (strstr(args[0]->val.str, args[1]->val.str)) {
3873 set_fill_boolean(set, 1);
3874 } else {
3875 set_fill_boolean(set, 0);
3876 }
3877
3878 return LY_SUCCESS;
3879}
3880
3881/**
3882 * @brief Execute the XPath count(node-set) function. Returns LYXP_SET_NUMBER
3883 * with the size of the node-set from the argument.
3884 *
3885 * @param[in] args Array of arguments.
3886 * @param[in] arg_count Count of elements in @p args.
3887 * @param[in,out] set Context and result set at the same time.
3888 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003889 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003890 */
3891static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02003892xpath_count(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003893{
Michal Vasko03ff5a72019-09-11 13:49:33 +02003894 LY_ERR rc = LY_SUCCESS;
3895
3896 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003897 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003898 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003899 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003900 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003901 return rc;
3902 }
3903
Michal Vasko03ff5a72019-09-11 13:49:33 +02003904 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003905 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "count(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003906 return LY_EVALID;
3907 }
3908
3909 set_fill_number(set, args[0]->used);
3910 return LY_SUCCESS;
3911}
3912
3913/**
3914 * @brief Execute the XPath current() function. Returns LYXP_SET_NODE_SET
3915 * with the context with the intial node.
3916 *
3917 * @param[in] args Array of arguments.
3918 * @param[in] arg_count Count of elements in @p args.
3919 * @param[in,out] set Context and result set at the same time.
3920 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003921 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003922 */
3923static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02003924xpath_current(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003925{
3926 if (arg_count || args) {
Radek Krejcie87c7dc2021-06-02 21:25:42 +02003927 LOGVAL(set->ctx, LY_VCODE_XP_INARGCOUNT, arg_count, LY_PRI_LENSTR("current()"));
Michal Vasko03ff5a72019-09-11 13:49:33 +02003928 return LY_EVALID;
3929 }
3930
3931 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02003932 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003933
Michal Vasko296dfaf2021-12-13 16:57:42 +01003934 if (set->cur_scnode) {
Michal Vaskoe4a6d012023-05-22 14:34:52 +02003935 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, set->cur_scnode, LYXP_NODE_ELEM, LYXP_AXIS_SELF, NULL));
Michal Vasko296dfaf2021-12-13 16:57:42 +01003936 } else {
3937 /* root node */
Michal Vaskoe4a6d012023-05-22 14:34:52 +02003938 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, NULL, set->root_type, LYXP_AXIS_SELF, NULL));
Michal Vasko296dfaf2021-12-13 16:57:42 +01003939 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003940 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02003941 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003942
Michal Vasko296dfaf2021-12-13 16:57:42 +01003943 if (set->cur_node) {
3944 /* position is filled later */
3945 set_insert_node(set, set->cur_node, 0, LYXP_NODE_ELEM, 0);
3946 } else {
3947 /* root node */
3948 set_insert_node(set, NULL, 0, set->root_type, 0);
3949 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003950 }
3951
3952 return LY_SUCCESS;
3953}
3954
3955/**
3956 * @brief Execute the YANG 1.1 deref(node-set) function. Returns LYXP_SET_NODE_SET with either
3957 * leafref or instance-identifier target node(s).
3958 *
3959 * @param[in] args Array of arguments.
3960 * @param[in] arg_count Count of elements in @p args.
3961 * @param[in,out] set Context and result set at the same time.
3962 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003963 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003964 */
3965static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02003966xpath_deref(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003967{
3968 struct lyd_node_term *leaf;
Michal Vasko42e497c2020-01-06 08:38:25 +01003969 struct lysc_node_leaf *sleaf = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02003970 struct lysc_type_leafref *lref;
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003971 const struct lysc_node *target;
Michal Vasko004d3152020-06-11 19:59:22 +02003972 struct ly_path *p;
3973 struct lyd_node *node;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003974 char *errmsg = NULL;
Michal Vasko00cbf532020-06-15 13:58:47 +02003975 uint8_t oper;
Michal Vasko741bb562021-06-24 11:59:50 +02003976 LY_ERR r;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003977
3978 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003979 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003980 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003981 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
Michal Vasko423ba3f2021-07-19 13:08:50 +02003982 if (!(sleaf->nodetype & LYD_NODE_TERM)) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003983 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3984 sleaf->name);
Michal Vaskoed725d72021-06-23 12:03:45 +02003985 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_LEAFREF) &&
3986 !warn_is_specific_type(sleaf->type, LY_TYPE_INST)) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003987 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"leafref\" nor \"instance-identifier\".",
3988 __func__, sleaf->name);
3989 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003990 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003991 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko423ba3f2021-07-19 13:08:50 +02003992 if (sleaf && (sleaf->nodetype & LYD_NODE_TERM) && (sleaf->type->basetype == LY_TYPE_LEAFREF)) {
Michal Vasko004d3152020-06-11 19:59:22 +02003993 lref = (struct lysc_type_leafref *)sleaf->type;
Michal Vaskod1e53b92021-01-28 13:11:06 +01003994 oper = (sleaf->flags & LYS_IS_OUTPUT) ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT;
Michal Vasko004d3152020-06-11 19:59:22 +02003995
3996 /* it was already evaluated on schema, it must succeed */
Michal Vasko741bb562021-06-24 11:59:50 +02003997 r = ly_path_compile_leafref(set->ctx, &sleaf->node, NULL, lref->path, oper, LY_PATH_TARGET_MANY,
Michal Vasko24fc4d12021-07-12 14:41:20 +02003998 LY_VALUE_SCHEMA_RESOLVED, lref->prefixes, &p);
Michal Vasko741bb562021-06-24 11:59:50 +02003999 if (!r) {
4000 /* get the target node */
4001 target = p[LY_ARRAY_COUNT(p) - 1].node;
4002 ly_path_free(set->ctx, p);
Michal Vasko004d3152020-06-11 19:59:22 +02004003
Michal Vaskoe4a6d012023-05-22 14:34:52 +02004004 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, target, LYXP_NODE_ELEM, LYXP_AXIS_SELF, NULL));
Michal Vasko741bb562021-06-24 11:59:50 +02004005 } /* else the target was found before but is disabled so it was removed */
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02004006 }
4007
Michal Vasko741bb562021-06-24 11:59:50 +02004008 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004009 }
4010
Michal Vaskod3678892020-05-21 10:06:58 +02004011 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004012 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "deref(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004013 return LY_EVALID;
4014 }
4015
Michal Vaskod3678892020-05-21 10:06:58 +02004016 lyxp_set_free_content(set);
4017 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004018 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
4019 sleaf = (struct lysc_node_leaf *)leaf->schema;
4020 if (sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
4021 if (sleaf->type->basetype == LY_TYPE_LEAFREF) {
4022 /* find leafref target */
Radek Krejci0b013302021-03-29 15:22:32 +02004023 if (lyplg_type_resolve_leafref((struct lysc_type_leafref *)sleaf->type, &leaf->node, &leaf->value, set->tree,
Michal Vasko9e685082021-01-29 14:49:09 +01004024 &node, &errmsg)) {
Michal Vasko1a2b8ee2022-12-05 10:41:55 +01004025 LOGERR(set->ctx, LY_EVALID, "%s", errmsg);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004026 free(errmsg);
Michal Vasko004d3152020-06-11 19:59:22 +02004027 return LY_EVALID;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004028 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004029 } else {
4030 assert(sleaf->type->basetype == LY_TYPE_INST);
Michal Vasko90189962023-02-28 12:10:34 +01004031 if (ly_path_eval(leaf->value.target, set->tree, NULL, &node)) {
Michal Vaskoba99a3e2020-08-18 15:50:05 +02004032 LOGERR(set->ctx, LY_EVALID, "Invalid instance-identifier \"%s\" value - required instance not found.",
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02004033 lyd_get_value(&leaf->node));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004034 return LY_EVALID;
4035 }
4036 }
Michal Vasko004d3152020-06-11 19:59:22 +02004037
4038 /* insert it */
4039 set_insert_node(set, node, 0, LYXP_NODE_ELEM, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004040 }
4041 }
4042
4043 return LY_SUCCESS;
4044}
4045
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004046static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02004047xpath_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 +02004048{
Michal Vaskofe1af042022-07-29 14:58:59 +02004049 uint32_t i, id_len;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004050 LY_ARRAY_COUNT_TYPE u;
4051 struct lyd_node_term *leaf;
4052 struct lysc_node_leaf *sleaf;
4053 struct lyd_meta *meta;
Michal Vasko93923692021-05-07 15:28:02 +02004054 struct lyd_value *val;
4055 const struct lys_module *mod;
4056 const char *id_name;
Michal Vasko93923692021-05-07 15:28:02 +02004057 struct lysc_ident *id;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004058 LY_ERR rc = LY_SUCCESS;
Radek Krejci857189e2020-09-01 13:26:36 +02004059 ly_bool found;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004060
4061 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02004062 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004063 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", func);
Michal Vasko5676f4e2021-04-06 17:14:45 +02004064 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4065 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4066 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype),
4067 sleaf->name);
4068 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_IDENT)) {
4069 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"identityref\".", func, sleaf->name);
4070 }
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004071 }
4072
4073 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4074 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4075 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype),
Michal Vasko69730152020-10-09 16:30:07 +02004076 sleaf->name);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004077 } else if (!warn_is_string_type(sleaf->type)) {
4078 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", func, sleaf->name);
4079 }
4080 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004081 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004082 return rc;
4083 }
4084
4085 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004086 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 +02004087 return LY_EVALID;
4088 }
4089 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
4090 LY_CHECK_RET(rc);
4091
Michal Vasko93923692021-05-07 15:28:02 +02004092 /* parse the identity */
4093 id_name = args[1]->val.str;
4094 id_len = strlen(id_name);
4095 rc = moveto_resolve_model(&id_name, &id_len, set, set->cur_node ? set->cur_node->schema : NULL, &mod);
4096 LY_CHECK_RET(rc);
4097 if (!mod) {
4098 LOGVAL(set->ctx, LYVE_XPATH, "Identity \"%.*s\" without a prefix.", (int)id_len, id_name);
4099 return LY_EVALID;
4100 }
4101
4102 /* find the identity */
4103 found = 0;
4104 LY_ARRAY_FOR(mod->identities, u) {
4105 if (!ly_strncmp(mod->identities[u].name, id_name, id_len)) {
4106 /* we have match */
4107 found = 1;
4108 break;
4109 }
4110 }
4111 if (!found) {
4112 LOGVAL(set->ctx, LYVE_XPATH, "Identity \"%.*s\" not found in module \"%s\".", (int)id_len, id_name, mod->name);
4113 return LY_EVALID;
4114 }
4115 id = &mod->identities[u];
4116
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004117 set_fill_boolean(set, 0);
4118 found = 0;
4119 for (i = 0; i < args[0]->used; ++i) {
4120 if ((args[0]->val.nodes[i].type != LYXP_NODE_ELEM) && (args[0]->val.nodes[i].type != LYXP_NODE_META)) {
4121 continue;
4122 }
4123
4124 if (args[0]->val.nodes[i].type == LYXP_NODE_ELEM) {
4125 leaf = (struct lyd_node_term *)args[0]->val.nodes[i].node;
4126 sleaf = (struct lysc_node_leaf *)leaf->schema;
4127 val = &leaf->value;
4128 if (!(sleaf->nodetype & LYD_NODE_TERM) || (leaf->value.realtype->basetype != LY_TYPE_IDENT)) {
4129 /* uninteresting */
4130 continue;
4131 }
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004132 } else {
4133 meta = args[0]->val.meta[i].meta;
4134 val = &meta->value;
4135 if (val->realtype->basetype != LY_TYPE_IDENT) {
4136 /* uninteresting */
4137 continue;
4138 }
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004139 }
4140
Michal Vasko93923692021-05-07 15:28:02 +02004141 /* check the identity itself */
4142 if (self_match && (id == val->ident)) {
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004143 set_fill_boolean(set, 1);
4144 found = 1;
4145 }
Michal Vasko93923692021-05-07 15:28:02 +02004146 if (!found && !lyplg_type_identity_isderived(id, val->ident)) {
4147 set_fill_boolean(set, 1);
4148 found = 1;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004149 }
4150
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004151 if (found) {
4152 break;
4153 }
4154 }
4155
4156 return LY_SUCCESS;
4157}
4158
Michal Vasko03ff5a72019-09-11 13:49:33 +02004159/**
4160 * @brief Execute the YANG 1.1 derived-from(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
4161 * on whether the first argument nodes contain a node of an identity derived from the second
4162 * argument identity.
4163 *
4164 * @param[in] args Array of arguments.
4165 * @param[in] arg_count Count of elements in @p args.
4166 * @param[in,out] set Context and result set at the same time.
4167 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004168 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004169 */
4170static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004171xpath_derived_from(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004172{
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004173 return xpath_derived_(args, set, options, 0, __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004174}
4175
4176/**
4177 * @brief Execute the YANG 1.1 derived-from-or-self(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
4178 * on whether the first argument nodes contain a node of an identity that either is or is derived from
4179 * the second argument identity.
4180 *
4181 * @param[in] args Array of arguments.
4182 * @param[in] arg_count Count of elements in @p args.
4183 * @param[in,out] set Context and result set at the same time.
4184 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004185 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004186 */
4187static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004188xpath_derived_from_or_self(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004189{
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004190 return xpath_derived_(args, set, options, 1, __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004191}
4192
4193/**
4194 * @brief Execute the YANG 1.1 enum-value(node-set) function. Returns LYXP_SET_NUMBER
4195 * with the integer value of the first node's enum value, otherwise NaN.
4196 *
4197 * @param[in] args Array of arguments.
4198 * @param[in] arg_count Count of elements in @p args.
4199 * @param[in,out] set Context and result set at the same time.
4200 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004201 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004202 */
4203static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004204xpath_enum_value(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004205{
4206 struct lyd_node_term *leaf;
4207 struct lysc_node_leaf *sleaf;
4208 LY_ERR rc = LY_SUCCESS;
4209
4210 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02004211 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004212 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02004213 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4214 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4215 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4216 sleaf->name);
4217 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_ENUM)) {
4218 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"enumeration\".", __func__, sleaf->name);
4219 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004220 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004221 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004222 return rc;
4223 }
4224
Michal Vaskod3678892020-05-21 10:06:58 +02004225 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004226 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "enum-value(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004227 return LY_EVALID;
4228 }
4229
4230 set_fill_number(set, NAN);
Michal Vaskod3678892020-05-21 10:06:58 +02004231 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004232 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
4233 sleaf = (struct lysc_node_leaf *)leaf->schema;
4234 if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_ENUM)) {
4235 set_fill_number(set, leaf->value.enum_item->value);
4236 }
4237 }
4238
4239 return LY_SUCCESS;
4240}
4241
4242/**
4243 * @brief Execute the XPath false() function. Returns LYXP_SET_BOOLEAN
4244 * with false value.
4245 *
4246 * @param[in] args Array of arguments.
4247 * @param[in] arg_count Count of elements in @p args.
4248 * @param[in,out] set Context and result set at the same time.
4249 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004250 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004251 */
4252static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004253xpath_false(struct lyxp_set **UNUSED(args), uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004254{
4255 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004256 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004257 return LY_SUCCESS;
4258 }
4259
4260 set_fill_boolean(set, 0);
4261 return LY_SUCCESS;
4262}
4263
4264/**
4265 * @brief Execute the XPath floor(number) function. Returns LYXP_SET_NUMBER
4266 * with the first argument floored (truncated).
4267 *
4268 * @param[in] args Array of arguments.
4269 * @param[in] arg_count Count of elements in @p args.
4270 * @param[in,out] set Context and result set at the same time.
4271 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004272 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004273 */
4274static LY_ERR
Michal Vasko0ff8d632023-04-06 12:31:17 +02004275xpath_floor(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004276{
Michal Vasko0ff8d632023-04-06 12:31:17 +02004277 struct lysc_node_leaf *sleaf;
4278 LY_ERR rc = LY_SUCCESS;
4279
4280 if (options & LYXP_SCNODE_ALL) {
4281 if (args[0]->type != LYXP_SET_SCNODE_SET) {
4282 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
4283 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4284 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4285 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4286 sleaf->name);
4287 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
4288 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
4289 }
4290 }
4291 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
4292 return rc;
4293 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004294
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004295 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004296 LY_CHECK_RET(rc);
4297 if (isfinite(args[0]->val.num)) {
4298 set_fill_number(set, (long long)args[0]->val.num);
4299 }
4300
4301 return LY_SUCCESS;
4302}
4303
4304/**
4305 * @brief Execute the XPath lang(string) function. Returns LYXP_SET_BOOLEAN
4306 * whether the language of the text matches the one from the argument.
4307 *
4308 * @param[in] args Array of arguments.
4309 * @param[in] arg_count Count of elements in @p args.
4310 * @param[in,out] set Context and result set at the same time.
4311 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004312 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004313 */
4314static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004315xpath_lang(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004316{
4317 const struct lyd_node *node;
4318 struct lysc_node_leaf *sleaf;
Michal Vasko9f96a052020-03-10 09:41:45 +01004319 struct lyd_meta *meta = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004320 const char *val;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004321 LY_ERR rc = LY_SUCCESS;
4322
4323 if (options & LYXP_SCNODE_ALL) {
4324 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4325 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004326 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4327 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004328 } else if (!warn_is_string_type(sleaf->type)) {
4329 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004330 }
4331 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004332 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004333 return rc;
4334 }
4335
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004336 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004337 LY_CHECK_RET(rc);
4338
Michal Vasko03ff5a72019-09-11 13:49:33 +02004339 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004340 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "lang(string)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004341 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004342 } else if (!set->used) {
4343 set_fill_boolean(set, 0);
4344 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004345 }
4346
4347 switch (set->val.nodes[0].type) {
4348 case LYXP_NODE_ELEM:
4349 case LYXP_NODE_TEXT:
4350 node = set->val.nodes[0].node;
4351 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004352 case LYXP_NODE_META:
4353 node = set->val.meta[0].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004354 break;
4355 default:
4356 /* nothing to do with roots */
4357 set_fill_boolean(set, 0);
4358 return LY_SUCCESS;
4359 }
4360
Michal Vasko9f96a052020-03-10 09:41:45 +01004361 /* find lang metadata */
Michal Vasko9e685082021-01-29 14:49:09 +01004362 for ( ; node; node = lyd_parent(node)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01004363 for (meta = node->meta; meta; meta = meta->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004364 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004365 if (meta->name && !strcmp(meta->name, "lang") && !strcmp(meta->annotation->module->name, "xml")) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004366 break;
4367 }
4368 }
4369
Michal Vasko9f96a052020-03-10 09:41:45 +01004370 if (meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004371 break;
4372 }
4373 }
4374
4375 /* compare languages */
Michal Vasko9f96a052020-03-10 09:41:45 +01004376 if (!meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004377 set_fill_boolean(set, 0);
4378 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004379 uint64_t i;
4380
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02004381 val = lyd_get_meta_value(meta);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004382 for (i = 0; args[0]->val.str[i]; ++i) {
4383 if (tolower(args[0]->val.str[i]) != tolower(val[i])) {
4384 set_fill_boolean(set, 0);
4385 break;
4386 }
4387 }
4388 if (!args[0]->val.str[i]) {
4389 if (!val[i] || (val[i] == '-')) {
4390 set_fill_boolean(set, 1);
4391 } else {
4392 set_fill_boolean(set, 0);
4393 }
4394 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004395 }
4396
4397 return LY_SUCCESS;
4398}
4399
4400/**
4401 * @brief Execute the XPath last() function. Returns LYXP_SET_NUMBER
4402 * with the context size.
4403 *
4404 * @param[in] args Array of arguments.
4405 * @param[in] arg_count Count of elements in @p args.
4406 * @param[in,out] set Context and result set at the same time.
4407 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004408 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004409 */
4410static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004411xpath_last(struct lyxp_set **UNUSED(args), uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004412{
4413 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004414 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004415 return LY_SUCCESS;
4416 }
4417
Michal Vasko03ff5a72019-09-11 13:49:33 +02004418 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004419 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "last()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004420 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004421 } else if (!set->used) {
4422 set_fill_number(set, 0);
4423 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004424 }
4425
4426 set_fill_number(set, set->ctx_size);
4427 return LY_SUCCESS;
4428}
4429
4430/**
4431 * @brief Execute the XPath local-name(node-set?) function. Returns LYXP_SET_STRING
4432 * with the node name without namespace from the argument or the context.
4433 *
4434 * @param[in] args Array of arguments.
4435 * @param[in] arg_count Count of elements in @p args.
4436 * @param[in,out] set Context and result set at the same time.
4437 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004438 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004439 */
4440static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004441xpath_local_name(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004442{
4443 struct lyxp_set_node *item;
Michal Vasko69730152020-10-09 16:30:07 +02004444
Michal Vasko03ff5a72019-09-11 13:49:33 +02004445 /* suppress unused variable warning */
4446 (void)options;
4447
4448 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004449 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004450 return LY_SUCCESS;
4451 }
4452
4453 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004454 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004455 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004456 "local-name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004457 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004458 } else if (!args[0]->used) {
4459 set_fill_string(set, "", 0);
4460 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004461 }
4462
4463 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004464 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004465
4466 item = &args[0]->val.nodes[0];
4467 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004468 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004469 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "local-name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004470 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004471 } else if (!set->used) {
4472 set_fill_string(set, "", 0);
4473 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004474 }
4475
4476 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004477 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004478
4479 item = &set->val.nodes[0];
4480 }
4481
4482 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004483 case LYXP_NODE_NONE:
4484 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004485 case LYXP_NODE_ROOT:
4486 case LYXP_NODE_ROOT_CONFIG:
4487 case LYXP_NODE_TEXT:
4488 set_fill_string(set, "", 0);
4489 break;
4490 case LYXP_NODE_ELEM:
Michal Vasko222be682023-08-24 08:17:12 +02004491 set_fill_string(set, LYD_NAME(item->node), strlen(LYD_NAME(item->node)));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004492 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004493 case LYXP_NODE_META:
4494 set_fill_string(set, ((struct lyd_meta *)item->node)->name, strlen(((struct lyd_meta *)item->node)->name));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004495 break;
4496 }
4497
4498 return LY_SUCCESS;
4499}
4500
4501/**
4502 * @brief Execute the XPath name(node-set?) function. Returns LYXP_SET_STRING
4503 * with the node name fully qualified (with namespace) from the argument or the context.
4504 *
4505 * @param[in] args Array of arguments.
4506 * @param[in] arg_count Count of elements in @p args.
4507 * @param[in,out] set Context and result set at the same time.
4508 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004509 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004510 */
4511static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004512xpath_name(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004513{
4514 struct lyxp_set_node *item;
Michal Vasko420cc252023-08-24 08:14:24 +02004515 const struct lys_module *mod = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004516 char *str;
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004517 const char *name = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004518
4519 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004520 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004521 return LY_SUCCESS;
4522 }
4523
4524 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004525 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004526 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004527 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004528 } else if (!args[0]->used) {
4529 set_fill_string(set, "", 0);
4530 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004531 }
4532
4533 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004534 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004535
4536 item = &args[0]->val.nodes[0];
4537 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004538 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004539 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004540 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004541 } else if (!set->used) {
4542 set_fill_string(set, "", 0);
4543 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004544 }
4545
4546 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004547 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004548
4549 item = &set->val.nodes[0];
4550 }
4551
4552 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004553 case LYXP_NODE_NONE:
4554 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004555 case LYXP_NODE_ROOT:
4556 case LYXP_NODE_ROOT_CONFIG:
4557 case LYXP_NODE_TEXT:
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004558 /* keep NULL */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004559 break;
4560 case LYXP_NODE_ELEM:
Michal Vasko420cc252023-08-24 08:14:24 +02004561 mod = lyd_node_module(item->node);
4562 name = LYD_NAME(item->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004563 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004564 case LYXP_NODE_META:
4565 mod = ((struct lyd_meta *)item->node)->annotation->module;
4566 name = ((struct lyd_meta *)item->node)->name;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004567 break;
4568 }
4569
4570 if (mod && name) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004571 int rc = asprintf(&str, "%s:%s", ly_get_prefix(mod, set->format, set->prefix_data), name);
Michal Vasko26bbb272022-08-02 14:54:33 +02004572
Michal Vasko03ff5a72019-09-11 13:49:33 +02004573 LY_CHECK_ERR_RET(rc == -1, LOGMEM(set->ctx), LY_EMEM);
4574 set_fill_string(set, str, strlen(str));
4575 free(str);
4576 } else {
4577 set_fill_string(set, "", 0);
4578 }
4579
4580 return LY_SUCCESS;
4581}
4582
4583/**
4584 * @brief Execute the XPath namespace-uri(node-set?) function. Returns LYXP_SET_STRING
4585 * with the namespace of the node from the argument or the context.
4586 *
4587 * @param[in] args Array of arguments.
4588 * @param[in] arg_count Count of elements in @p args.
4589 * @param[in,out] set Context and result set at the same time.
4590 * @param[in] options XPath options.
4591 * @return LY_ERR (LY_EINVAL for wrong arguments on schema)
4592 */
4593static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004594xpath_namespace_uri(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004595{
4596 struct lyxp_set_node *item;
Michal Vasko420cc252023-08-24 08:14:24 +02004597 const struct lys_module *mod;
Michal Vasko69730152020-10-09 16:30:07 +02004598
Michal Vasko03ff5a72019-09-11 13:49:33 +02004599 /* suppress unused variable warning */
4600 (void)options;
4601
4602 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004603 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004604 return LY_SUCCESS;
4605 }
4606
4607 if (arg_count) {
Michal Vaskod3678892020-05-21 10:06:58 +02004608 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004609 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
Michal Vasko69730152020-10-09 16:30:07 +02004610 "namespace-uri(node-set?)");
Michal Vaskod3678892020-05-21 10:06:58 +02004611 return LY_EVALID;
4612 } else if (!args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004613 set_fill_string(set, "", 0);
4614 return LY_SUCCESS;
4615 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004616
4617 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004618 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004619
4620 item = &args[0]->val.nodes[0];
4621 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004622 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004623 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "namespace-uri(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004624 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004625 } else if (!set->used) {
4626 set_fill_string(set, "", 0);
4627 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004628 }
4629
4630 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004631 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004632
4633 item = &set->val.nodes[0];
4634 }
4635
4636 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004637 case LYXP_NODE_NONE:
4638 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004639 case LYXP_NODE_ROOT:
4640 case LYXP_NODE_ROOT_CONFIG:
4641 case LYXP_NODE_TEXT:
4642 set_fill_string(set, "", 0);
4643 break;
4644 case LYXP_NODE_ELEM:
Michal Vasko9f96a052020-03-10 09:41:45 +01004645 case LYXP_NODE_META:
Michal Vasko03ff5a72019-09-11 13:49:33 +02004646 if (item->type == LYXP_NODE_ELEM) {
Michal Vasko420cc252023-08-24 08:14:24 +02004647 mod = lyd_node_module(item->node);
Michal Vasko9f96a052020-03-10 09:41:45 +01004648 } else { /* LYXP_NODE_META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004649 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004650 mod = ((struct lyd_meta *)item->node)->annotation->module;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004651 }
4652
4653 set_fill_string(set, mod->ns, strlen(mod->ns));
4654 break;
4655 }
4656
4657 return LY_SUCCESS;
4658}
4659
4660/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02004661 * @brief Execute the XPath normalize-space(string?) function. Returns LYXP_SET_STRING
4662 * with normalized value (no leading, trailing, double white spaces) of the node
4663 * from the argument or the context.
4664 *
4665 * @param[in] args Array of arguments.
4666 * @param[in] arg_count Count of elements in @p args.
4667 * @param[in,out] set Context and result set at the same time.
4668 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004669 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004670 */
4671static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004672xpath_normalize_space(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004673{
Michal Vaskodd528af2022-08-08 14:35:07 +02004674 uint32_t i, new_used;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004675 char *new;
Radek Krejci857189e2020-09-01 13:26:36 +02004676 ly_bool have_spaces = 0, space_before = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004677 struct lysc_node_leaf *sleaf;
4678 LY_ERR rc = LY_SUCCESS;
4679
4680 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004681 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) &&
4682 (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004683 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004684 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4685 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004686 } else if (!warn_is_string_type(sleaf->type)) {
4687 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004688 }
4689 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004690 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004691 return rc;
4692 }
4693
4694 if (arg_count) {
4695 set_fill_set(set, args[0]);
4696 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004697 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004698 LY_CHECK_RET(rc);
4699
4700 /* is there any normalization necessary? */
4701 for (i = 0; set->val.str[i]; ++i) {
4702 if (is_xmlws(set->val.str[i])) {
4703 if ((i == 0) || space_before || (!set->val.str[i + 1])) {
4704 have_spaces = 1;
4705 break;
4706 }
4707 space_before = 1;
4708 } else {
4709 space_before = 0;
4710 }
4711 }
4712
4713 /* yep, there is */
4714 if (have_spaces) {
4715 /* it's enough, at least one character will go, makes space for ending '\0' */
4716 new = malloc(strlen(set->val.str) * sizeof(char));
4717 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4718 new_used = 0;
4719
4720 space_before = 0;
4721 for (i = 0; set->val.str[i]; ++i) {
4722 if (is_xmlws(set->val.str[i])) {
4723 if ((i == 0) || space_before) {
4724 space_before = 1;
4725 continue;
4726 } else {
4727 space_before = 1;
4728 }
4729 } else {
4730 space_before = 0;
4731 }
4732
4733 new[new_used] = (space_before ? ' ' : set->val.str[i]);
4734 ++new_used;
4735 }
4736
4737 /* at worst there is one trailing space now */
4738 if (new_used && is_xmlws(new[new_used - 1])) {
4739 --new_used;
4740 }
4741
4742 new = ly_realloc(new, (new_used + 1) * sizeof(char));
4743 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4744 new[new_used] = '\0';
4745
4746 free(set->val.str);
4747 set->val.str = new;
4748 }
4749
4750 return LY_SUCCESS;
4751}
4752
4753/**
4754 * @brief Execute the XPath not(boolean) function. Returns LYXP_SET_BOOLEAN
4755 * with the argument converted to boolean and logically inverted.
4756 *
4757 * @param[in] args Array of arguments.
4758 * @param[in] arg_count Count of elements in @p args.
4759 * @param[in,out] set Context and result set at the same time.
4760 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004761 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004762 */
4763static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004764xpath_not(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004765{
4766 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004767 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004768 return LY_SUCCESS;
4769 }
4770
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004771 lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02004772 if (args[0]->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004773 set_fill_boolean(set, 0);
4774 } else {
4775 set_fill_boolean(set, 1);
4776 }
4777
4778 return LY_SUCCESS;
4779}
4780
4781/**
4782 * @brief Execute the XPath number(object?) function. Returns LYXP_SET_NUMBER
4783 * with the number representation of either the argument or the context.
4784 *
4785 * @param[in] args Array of arguments.
4786 * @param[in] arg_count Count of elements in @p args.
4787 * @param[in,out] set Context and result set at the same time.
4788 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004789 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004790 */
4791static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004792xpath_number(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004793{
4794 LY_ERR rc;
4795
4796 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004797 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004798 return LY_SUCCESS;
4799 }
4800
4801 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004802 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004803 LY_CHECK_RET(rc);
4804 set_fill_set(set, args[0]);
4805 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004806 rc = lyxp_set_cast(set, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004807 LY_CHECK_RET(rc);
4808 }
4809
4810 return LY_SUCCESS;
4811}
4812
4813/**
4814 * @brief Execute the XPath position() function. Returns LYXP_SET_NUMBER
4815 * with the context position.
4816 *
4817 * @param[in] args Array of arguments.
4818 * @param[in] arg_count Count of elements in @p args.
4819 * @param[in,out] set Context and result set at the same time.
4820 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004821 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004822 */
4823static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004824xpath_position(struct lyxp_set **UNUSED(args), uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004825{
4826 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004827 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004828 return LY_SUCCESS;
4829 }
4830
Michal Vasko03ff5a72019-09-11 13:49:33 +02004831 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004832 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "position()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004833 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004834 } else if (!set->used) {
4835 set_fill_number(set, 0);
4836 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004837 }
4838
4839 set_fill_number(set, set->ctx_pos);
4840
4841 /* UNUSED in 'Release' build type */
4842 (void)options;
4843 return LY_SUCCESS;
4844}
4845
4846/**
4847 * @brief Execute the YANG 1.1 re-match(string, string) function. Returns LYXP_SET_BOOLEAN
4848 * depending on whether the second argument regex matches the first argument string. For details refer to
4849 * YANG 1.1 RFC section 10.2.1.
4850 *
4851 * @param[in] args Array of arguments.
4852 * @param[in] arg_count Count of elements in @p args.
4853 * @param[in,out] set Context and result set at the same time.
4854 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004855 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004856 */
4857static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004858xpath_re_match(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004859{
4860 struct lysc_pattern **patterns = NULL, **pattern;
4861 struct lysc_node_leaf *sleaf;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004862 LY_ERR rc = LY_SUCCESS;
4863 struct ly_err_item *err;
4864
4865 if (options & LYXP_SCNODE_ALL) {
4866 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4867 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4868 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004869 } else if (!warn_is_string_type(sleaf->type)) {
4870 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004871 }
4872 }
4873
4874 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4875 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4876 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 +02004877 } else if (!warn_is_string_type(sleaf->type)) {
4878 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004879 }
4880 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004881 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004882 return rc;
4883 }
4884
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004885 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004886 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004887 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004888 LY_CHECK_RET(rc);
4889
4890 LY_ARRAY_NEW_RET(set->ctx, patterns, pattern, LY_EMEM);
Radek Iša45802b52021-02-09 09:21:58 +01004891 *pattern = calloc(1, sizeof **pattern);
Radek Krejciddace2c2021-01-08 11:30:56 +01004892 LOG_LOCSET(NULL, set->cur_node, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01004893 rc = lys_compile_type_pattern_check(set->ctx, args[1]->val.str, &(*pattern)->code);
Michal Vasko4a7d4d62021-12-13 17:05:06 +01004894 if (set->cur_node) {
4895 LOG_LOCBACK(0, 1, 0, 0);
4896 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004897 if (rc != LY_SUCCESS) {
4898 LY_ARRAY_FREE(patterns);
4899 return rc;
4900 }
4901
Radek Krejci0b013302021-03-29 15:22:32 +02004902 rc = lyplg_type_validate_patterns(patterns, args[0]->val.str, strlen(args[0]->val.str), &err);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004903 pcre2_code_free((*pattern)->code);
4904 free(*pattern);
4905 LY_ARRAY_FREE(patterns);
4906 if (rc && (rc != LY_EVALID)) {
Michal Vasko177d0ed2020-11-23 16:43:03 +01004907 ly_err_print(set->ctx, err);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004908 ly_err_free(err);
4909 return rc;
4910 }
4911
4912 if (rc == LY_EVALID) {
4913 ly_err_free(err);
4914 set_fill_boolean(set, 0);
4915 } else {
4916 set_fill_boolean(set, 1);
4917 }
4918
4919 return LY_SUCCESS;
4920}
4921
4922/**
4923 * @brief Execute the XPath round(number) function. Returns LYXP_SET_NUMBER
4924 * with the rounded first argument. For details refer to
4925 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-round.
4926 *
4927 * @param[in] args Array of arguments.
4928 * @param[in] arg_count Count of elements in @p args.
4929 * @param[in,out] set Context and result set at the same time.
4930 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004931 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004932 */
4933static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004934xpath_round(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004935{
4936 struct lysc_node_leaf *sleaf;
4937 LY_ERR rc = LY_SUCCESS;
4938
4939 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02004940 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004941 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02004942 } else if ((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),
4945 sleaf->name);
4946 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
4947 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
4948 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004949 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004950 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004951 return rc;
4952 }
4953
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004954 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004955 LY_CHECK_RET(rc);
4956
4957 /* cover only the cases where floor can't be used */
4958 if ((args[0]->val.num == -0.0f) || ((args[0]->val.num < 0) && (args[0]->val.num >= -0.5))) {
4959 set_fill_number(set, -0.0f);
4960 } else {
4961 args[0]->val.num += 0.5;
4962 rc = xpath_floor(args, 1, args[0], options);
4963 LY_CHECK_RET(rc);
4964 set_fill_number(set, args[0]->val.num);
4965 }
4966
4967 return LY_SUCCESS;
4968}
4969
4970/**
4971 * @brief Execute the XPath starts-with(string, string) function.
4972 * Returns LYXP_SET_BOOLEAN whether the second argument is
4973 * the prefix of the first or not.
4974 *
4975 * @param[in] args Array of arguments.
4976 * @param[in] arg_count Count of elements in @p args.
4977 * @param[in,out] set Context and result set at the same time.
4978 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004979 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004980 */
4981static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004982xpath_starts_with(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004983{
4984 struct lysc_node_leaf *sleaf;
4985 LY_ERR rc = LY_SUCCESS;
4986
4987 if (options & LYXP_SCNODE_ALL) {
4988 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4989 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4990 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 +02004991 } else if (!warn_is_string_type(sleaf->type)) {
4992 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004993 }
4994 }
4995
4996 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4997 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4998 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 +02004999 } else if (!warn_is_string_type(sleaf->type)) {
5000 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005001 }
5002 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005003 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005004 return rc;
5005 }
5006
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005007 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005008 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005009 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005010 LY_CHECK_RET(rc);
5011
5012 if (strncmp(args[0]->val.str, args[1]->val.str, strlen(args[1]->val.str))) {
5013 set_fill_boolean(set, 0);
5014 } else {
5015 set_fill_boolean(set, 1);
5016 }
5017
5018 return LY_SUCCESS;
5019}
5020
5021/**
5022 * @brief Execute the XPath string(object?) function. Returns LYXP_SET_STRING
5023 * with the string representation of either the argument or the context.
5024 *
5025 * @param[in] args Array of arguments.
5026 * @param[in] arg_count Count of elements in @p args.
5027 * @param[in,out] set Context and result set at the same time.
5028 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005029 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005030 */
5031static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02005032xpath_string(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005033{
5034 LY_ERR rc;
5035
5036 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02005037 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005038 return LY_SUCCESS;
5039 }
5040
5041 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005042 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005043 LY_CHECK_RET(rc);
5044 set_fill_set(set, args[0]);
5045 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005046 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005047 LY_CHECK_RET(rc);
5048 }
5049
5050 return LY_SUCCESS;
5051}
5052
5053/**
5054 * @brief Execute the XPath string-length(string?) function. Returns LYXP_SET_NUMBER
5055 * with the length of the string in either the argument or the context.
5056 *
5057 * @param[in] args Array of arguments.
5058 * @param[in] arg_count Count of elements in @p args.
5059 * @param[in,out] set Context and result set at the same time.
5060 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005061 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005062 */
5063static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02005064xpath_string_length(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005065{
5066 struct lysc_node_leaf *sleaf;
5067 LY_ERR rc = LY_SUCCESS;
5068
5069 if (options & LYXP_SCNODE_ALL) {
5070 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5071 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5072 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 +02005073 } else if (!warn_is_string_type(sleaf->type)) {
5074 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005075 }
5076 }
5077 if (!arg_count && (set->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set))) {
5078 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5079 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 +02005080 } else if (!warn_is_string_type(sleaf->type)) {
5081 LOGWRN(set->ctx, "Argument #0 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005082 }
5083 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005084 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005085 return rc;
5086 }
5087
5088 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005089 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005090 LY_CHECK_RET(rc);
5091 set_fill_number(set, strlen(args[0]->val.str));
5092 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005093 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005094 LY_CHECK_RET(rc);
5095 set_fill_number(set, strlen(set->val.str));
5096 }
5097
5098 return LY_SUCCESS;
5099}
5100
5101/**
5102 * @brief Execute the XPath substring(string, number, number?) function.
5103 * Returns LYXP_SET_STRING substring of the first argument starting
5104 * on the second argument index ending on the third argument index,
5105 * indexed from 1. For exact definition refer to
5106 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-substring.
5107 *
5108 * @param[in] args Array of arguments.
5109 * @param[in] arg_count Count of elements in @p args.
5110 * @param[in,out] set Context and result set at the same time.
5111 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005112 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005113 */
5114static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02005115xpath_substring(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005116{
Michal Vasko6db996e2022-07-28 10:28:04 +02005117 int64_t start;
5118 int32_t len;
Michal Vaskodd528af2022-08-08 14:35:07 +02005119 uint32_t str_start, str_len, pos;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005120 struct lysc_node_leaf *sleaf;
5121 LY_ERR rc = LY_SUCCESS;
5122
5123 if (options & LYXP_SCNODE_ALL) {
5124 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5125 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5126 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 +02005127 } else if (!warn_is_string_type(sleaf->type)) {
5128 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005129 }
5130 }
5131
5132 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5133 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5134 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 +02005135 } else if (!warn_is_numeric_type(sleaf->type)) {
5136 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005137 }
5138 }
5139
Michal Vasko69730152020-10-09 16:30:07 +02005140 if ((arg_count == 3) && (args[2]->type == LYXP_SET_SCNODE_SET) &&
5141 (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005142 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5143 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 +02005144 } else if (!warn_is_numeric_type(sleaf->type)) {
5145 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005146 }
5147 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005148 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005149 return rc;
5150 }
5151
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005152 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005153 LY_CHECK_RET(rc);
5154
5155 /* start */
5156 if (xpath_round(&args[1], 1, args[1], options)) {
5157 return -1;
5158 }
5159 if (isfinite(args[1]->val.num)) {
5160 start = args[1]->val.num - 1;
5161 } else if (isinf(args[1]->val.num) && signbit(args[1]->val.num)) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02005162 start = INT32_MIN;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005163 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02005164 start = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005165 }
5166
5167 /* len */
5168 if (arg_count == 3) {
5169 rc = xpath_round(&args[2], 1, args[2], options);
5170 LY_CHECK_RET(rc);
Radek Krejci1deb5be2020-08-26 16:43:36 +02005171 if (isnan(args[2]->val.num) || signbit(args[2]->val.num)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005172 len = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005173 } else if (isfinite(args[2]->val.num)) {
5174 len = args[2]->val.num;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005175 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02005176 len = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005177 }
5178 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02005179 len = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005180 }
5181
5182 /* find matching character positions */
5183 str_start = 0;
5184 str_len = 0;
5185 for (pos = 0; args[0]->val.str[pos]; ++pos) {
5186 if (pos < start) {
5187 ++str_start;
5188 } else if (pos < start + len) {
5189 ++str_len;
5190 } else {
5191 break;
5192 }
5193 }
5194
5195 set_fill_string(set, args[0]->val.str + str_start, str_len);
5196 return LY_SUCCESS;
5197}
5198
5199/**
5200 * @brief Execute the XPath substring-after(string, string) function.
5201 * Returns LYXP_SET_STRING with the string succeeding the occurance
5202 * of the second argument in the first or an empty string.
5203 *
5204 * @param[in] args Array of arguments.
5205 * @param[in] arg_count Count of elements in @p args.
5206 * @param[in,out] set Context and result set at the same time.
5207 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005208 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005209 */
5210static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02005211xpath_substring_after(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005212{
5213 char *ptr;
5214 struct lysc_node_leaf *sleaf;
5215 LY_ERR rc = LY_SUCCESS;
5216
5217 if (options & LYXP_SCNODE_ALL) {
5218 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5219 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5220 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 +02005221 } else if (!warn_is_string_type(sleaf->type)) {
5222 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005223 }
5224 }
5225
5226 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5227 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5228 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 +02005229 } else if (!warn_is_string_type(sleaf->type)) {
5230 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005231 }
5232 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005233 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005234 return rc;
5235 }
5236
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005237 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005238 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005239 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005240 LY_CHECK_RET(rc);
5241
5242 ptr = strstr(args[0]->val.str, args[1]->val.str);
5243 if (ptr) {
5244 set_fill_string(set, ptr + strlen(args[1]->val.str), strlen(ptr + strlen(args[1]->val.str)));
5245 } else {
5246 set_fill_string(set, "", 0);
5247 }
5248
5249 return LY_SUCCESS;
5250}
5251
5252/**
5253 * @brief Execute the XPath substring-before(string, string) function.
5254 * Returns LYXP_SET_STRING with the string preceding the occurance
5255 * of the second argument in the first or an empty string.
5256 *
5257 * @param[in] args Array of arguments.
5258 * @param[in] arg_count Count of elements in @p args.
5259 * @param[in,out] set Context and result set at the same time.
5260 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005261 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005262 */
5263static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02005264xpath_substring_before(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005265{
5266 char *ptr;
5267 struct lysc_node_leaf *sleaf;
5268 LY_ERR rc = LY_SUCCESS;
5269
5270 if (options & LYXP_SCNODE_ALL) {
5271 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5272 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5273 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 +02005274 } else if (!warn_is_string_type(sleaf->type)) {
5275 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005276 }
5277 }
5278
5279 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5280 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5281 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 +02005282 } else if (!warn_is_string_type(sleaf->type)) {
5283 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005284 }
5285 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005286 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005287 return rc;
5288 }
5289
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005290 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005291 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005292 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005293 LY_CHECK_RET(rc);
5294
5295 ptr = strstr(args[0]->val.str, args[1]->val.str);
5296 if (ptr) {
5297 set_fill_string(set, args[0]->val.str, ptr - args[0]->val.str);
5298 } else {
5299 set_fill_string(set, "", 0);
5300 }
5301
5302 return LY_SUCCESS;
5303}
5304
5305/**
5306 * @brief Execute the XPath sum(node-set) function. Returns LYXP_SET_NUMBER
5307 * with the sum of all the nodes in the context.
5308 *
5309 * @param[in] args Array of arguments.
5310 * @param[in] arg_count Count of elements in @p args.
5311 * @param[in,out] set Context and result set at the same time.
5312 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005313 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005314 */
5315static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02005316xpath_sum(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005317{
5318 long double num;
5319 char *str;
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01005320 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005321 struct lyxp_set set_item;
5322 struct lysc_node_leaf *sleaf;
5323 LY_ERR rc = LY_SUCCESS;
5324
5325 if (options & LYXP_SCNODE_ALL) {
5326 if (args[0]->type == LYXP_SET_SCNODE_SET) {
5327 for (i = 0; i < args[0]->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005328 if (args[0]->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005329 sleaf = (struct lysc_node_leaf *)args[0]->val.scnodes[i].scnode;
5330 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5331 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__,
Michal Vasko69730152020-10-09 16:30:07 +02005332 lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005333 } else if (!warn_is_numeric_type(sleaf->type)) {
5334 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005335 }
5336 }
5337 }
5338 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005339 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005340 return rc;
5341 }
5342
5343 set_fill_number(set, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005344
5345 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005346 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "sum(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02005347 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02005348 } else if (!args[0]->used) {
5349 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005350 }
5351
Michal Vasko5c4e5892019-11-14 12:31:38 +01005352 set_init(&set_item, set);
5353
Michal Vasko03ff5a72019-09-11 13:49:33 +02005354 set_item.type = LYXP_SET_NODE_SET;
Michal Vasko41decbf2021-11-02 11:50:21 +01005355 set_item.val.nodes = calloc(1, sizeof *set_item.val.nodes);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005356 LY_CHECK_ERR_RET(!set_item.val.nodes, LOGMEM(set->ctx), LY_EMEM);
5357
5358 set_item.used = 1;
5359 set_item.size = 1;
5360
5361 for (i = 0; i < args[0]->used; ++i) {
5362 set_item.val.nodes[0] = args[0]->val.nodes[i];
5363
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005364 rc = cast_node_set_to_string(&set_item, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005365 LY_CHECK_RET(rc);
5366 num = cast_string_to_number(str);
5367 free(str);
5368 set->val.num += num;
5369 }
5370
5371 free(set_item.val.nodes);
5372
5373 return LY_SUCCESS;
5374}
5375
5376/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02005377 * @brief Execute the XPath translate(string, string, string) function.
5378 * Returns LYXP_SET_STRING with the first argument with the characters
5379 * from the second argument replaced by those on the corresponding
5380 * positions in the third argument.
5381 *
5382 * @param[in] args Array of arguments.
5383 * @param[in] arg_count Count of elements in @p args.
5384 * @param[in,out] set Context and result set at the same time.
5385 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005386 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005387 */
5388static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02005389xpath_translate(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005390{
Michal Vaskodd528af2022-08-08 14:35:07 +02005391 uint32_t i, j, new_used;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005392 char *new;
Radek Krejci857189e2020-09-01 13:26:36 +02005393 ly_bool have_removed;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005394 struct lysc_node_leaf *sleaf;
5395 LY_ERR rc = LY_SUCCESS;
5396
5397 if (options & LYXP_SCNODE_ALL) {
5398 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5399 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5400 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 +02005401 } else if (!warn_is_string_type(sleaf->type)) {
5402 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005403 }
5404 }
5405
5406 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5407 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5408 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 +02005409 } else if (!warn_is_string_type(sleaf->type)) {
5410 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005411 }
5412 }
5413
5414 if ((args[2]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
5415 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5416 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 +02005417 } else if (!warn_is_string_type(sleaf->type)) {
5418 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005419 }
5420 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005421 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005422 return rc;
5423 }
5424
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005425 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005426 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005427 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005428 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005429 rc = lyxp_set_cast(args[2], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005430 LY_CHECK_RET(rc);
5431
5432 new = malloc((strlen(args[0]->val.str) + 1) * sizeof(char));
5433 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5434 new_used = 0;
5435
5436 have_removed = 0;
5437 for (i = 0; args[0]->val.str[i]; ++i) {
Radek Krejci857189e2020-09-01 13:26:36 +02005438 ly_bool found = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005439
5440 for (j = 0; args[1]->val.str[j]; ++j) {
5441 if (args[0]->val.str[i] == args[1]->val.str[j]) {
5442 /* removing this char */
5443 if (j >= strlen(args[2]->val.str)) {
5444 have_removed = 1;
5445 found = 1;
5446 break;
5447 }
5448 /* replacing this char */
5449 new[new_used] = args[2]->val.str[j];
5450 ++new_used;
5451 found = 1;
5452 break;
5453 }
5454 }
5455
5456 /* copying this char */
5457 if (!found) {
5458 new[new_used] = args[0]->val.str[i];
5459 ++new_used;
5460 }
5461 }
5462
5463 if (have_removed) {
5464 new = ly_realloc(new, (new_used + 1) * sizeof(char));
5465 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5466 }
5467 new[new_used] = '\0';
5468
Michal Vaskod3678892020-05-21 10:06:58 +02005469 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005470 set->type = LYXP_SET_STRING;
5471 set->val.str = new;
5472
5473 return LY_SUCCESS;
5474}
5475
5476/**
5477 * @brief Execute the XPath true() function. Returns LYXP_SET_BOOLEAN
5478 * with true value.
5479 *
5480 * @param[in] args Array of arguments.
5481 * @param[in] arg_count Count of elements in @p args.
5482 * @param[in,out] set Context and result set at the same time.
5483 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005484 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005485 */
5486static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02005487xpath_true(struct lyxp_set **UNUSED(args), uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005488{
5489 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02005490 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005491 return LY_SUCCESS;
5492 }
5493
5494 set_fill_boolean(set, 1);
5495 return LY_SUCCESS;
5496}
5497
Michal Vasko03ff5a72019-09-11 13:49:33 +02005498/**
Michal Vasko49fec8e2022-05-24 10:28:33 +02005499 * @brief Execute the XPath node() processing instruction (node type). Returns LYXP_SET_NODE_SET
5500 * with only nodes from the context.
5501 *
5502 * @param[in,out] set Context and result set at the same time.
5503 * @param[in] axis Axis to search on.
5504 * @param[in] options XPath options.
5505 * @return LY_ERR
5506 */
5507static LY_ERR
5508xpath_pi_node(struct lyxp_set *set, enum lyxp_axis axis, uint32_t options)
5509{
5510 if (options & LYXP_SCNODE_ALL) {
5511 return moveto_scnode(set, NULL, NULL, axis, options);
5512 }
5513
5514 if (set->type != LYXP_SET_NODE_SET) {
5515 lyxp_set_free_content(set);
5516 return LY_SUCCESS;
5517 }
5518
5519 /* just like moving to a node with no restrictions */
5520 return moveto_node(set, NULL, NULL, axis, options);
5521}
5522
5523/**
5524 * @brief Execute the XPath text() processing instruction (node type). Returns LYXP_SET_NODE_SET
5525 * with the text content of the nodes in the context.
5526 *
5527 * @param[in,out] set Context and result set at the same time.
5528 * @param[in] axis Axis to search on.
5529 * @param[in] options XPath options.
5530 * @return LY_ERR
5531 */
5532static LY_ERR
5533xpath_pi_text(struct lyxp_set *set, enum lyxp_axis axis, uint32_t options)
5534{
5535 uint32_t i;
5536
5537 if (options & LYXP_SCNODE_ALL) {
5538 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
5539 return LY_SUCCESS;
5540 }
5541
5542 if (set->type != LYXP_SET_NODE_SET) {
5543 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "text()");
5544 return LY_EVALID;
5545 }
5546
5547 if (axis != LYXP_AXIS_CHILD) {
5548 /* even following and preceding axescan return text nodes, but whatever */
5549 lyxp_set_free_content(set);
5550 return LY_SUCCESS;
5551 }
5552
5553 for (i = 0; i < set->used; ++i) {
5554 switch (set->val.nodes[i].type) {
5555 case LYXP_NODE_NONE:
5556 LOGINT_RET(set->ctx);
5557 case LYXP_NODE_ELEM:
Michal Vasko222be682023-08-24 08:17:12 +02005558 if (!set->val.nodes[i].node->schema || (set->val.nodes[i].node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02005559 set->val.nodes[i].type = LYXP_NODE_TEXT;
5560 break;
5561 }
5562 /* fall through */
5563 case LYXP_NODE_ROOT:
5564 case LYXP_NODE_ROOT_CONFIG:
5565 case LYXP_NODE_TEXT:
5566 case LYXP_NODE_META:
5567 set_remove_node_none(set, i);
5568 break;
5569 }
5570 }
5571 set_remove_nodes_none(set);
5572
5573 return LY_SUCCESS;
5574}
5575
5576/**
Michal Vasko6346ece2019-09-24 13:12:53 +02005577 * @brief Skip prefix and return corresponding model if there is a prefix. Logs directly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005578 *
Michal Vasko2104e9f2020-03-06 08:23:25 +01005579 * XPath @p set is expected to be a (sc)node set!
5580 *
Michal Vasko6346ece2019-09-24 13:12:53 +02005581 * @param[in,out] qname Qualified node name. If includes prefix, it is skipped.
5582 * @param[in,out] qname_len Length of @p qname, is updated accordingly.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005583 * @param[in] set Set with general XPath context.
5584 * @param[in] ctx_scnode Context node to inherit module for unprefixed node for ::LY_PREF_JSON.
Michal Vasko6346ece2019-09-24 13:12:53 +02005585 * @param[out] moveto_mod Expected module of a matching node.
5586 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005587 */
Michal Vasko6346ece2019-09-24 13:12:53 +02005588static LY_ERR
Michal Vaskofe1af042022-07-29 14:58:59 +02005589moveto_resolve_model(const char **qname, uint32_t *qname_len, const struct lyxp_set *set,
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005590 const struct lysc_node *ctx_scnode, const struct lys_module **moveto_mod)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005591{
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02005592 const struct lys_module *mod = NULL;
Michal Vasko6346ece2019-09-24 13:12:53 +02005593 const char *ptr;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005594 size_t pref_len;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005595
Michal Vasko2104e9f2020-03-06 08:23:25 +01005596 assert((set->type == LYXP_SET_NODE_SET) || (set->type == LYXP_SET_SCNODE_SET));
5597
Michal Vasko6346ece2019-09-24 13:12:53 +02005598 if ((ptr = ly_strnchr(*qname, ':', *qname_len))) {
5599 /* specific module */
5600 pref_len = ptr - *qname;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005601 mod = ly_resolve_prefix(set->ctx, *qname, pref_len, set->format, set->prefix_data);
Michal Vasko6346ece2019-09-24 13:12:53 +02005602
Michal Vasko004d3152020-06-11 19:59:22 +02005603 /* check for errors and non-implemented modules, as they are not valid */
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005604 if (!mod || !mod->implemented) {
Michal Vasko7b3a00e2023-08-09 11:58:03 +02005605 LOGVAL(set->ctx, LY_VCODE_XP_INMOD, (int)pref_len, *qname);
Michal Vasko6346ece2019-09-24 13:12:53 +02005606 return LY_EVALID;
5607 }
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005608
Michal Vasko6346ece2019-09-24 13:12:53 +02005609 *qname += pref_len + 1;
5610 *qname_len -= pref_len + 1;
5611 } else if (((*qname)[0] == '*') && (*qname_len == 1)) {
5612 /* all modules - special case */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005613 mod = NULL;
Michal Vasko6346ece2019-09-24 13:12:53 +02005614 } else {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005615 switch (set->format) {
Radek Krejci8df109d2021-04-23 12:19:08 +02005616 case LY_VALUE_SCHEMA:
5617 case LY_VALUE_SCHEMA_RESOLVED:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005618 /* current module */
5619 mod = set->cur_mod;
5620 break;
Radek Krejci224d4b42021-04-23 13:54:59 +02005621 case LY_VALUE_CANON:
Radek Krejci8df109d2021-04-23 12:19:08 +02005622 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +02005623 case LY_VALUE_LYB:
Michal Vaskoddd76592022-01-17 13:34:48 +01005624 case LY_VALUE_STR_NS:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005625 /* inherit parent (context node) module */
5626 if (ctx_scnode) {
5627 mod = ctx_scnode->module;
5628 } else {
5629 mod = NULL;
5630 }
5631 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02005632 case LY_VALUE_XML:
Michal Vasko52143d12021-04-14 15:36:39 +02005633 /* all nodes need to be prefixed */
5634 LOGVAL(set->ctx, LYVE_DATA, "Non-prefixed node \"%.*s\" in XML xpath found.", *qname_len, *qname);
5635 return LY_EVALID;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005636 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005637 }
5638
Michal Vasko6346ece2019-09-24 13:12:53 +02005639 *moveto_mod = mod;
5640 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005641}
5642
5643/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02005644 * @brief Move context @p set to the root. Handles absolute path.
5645 * Result is LYXP_SET_NODE_SET.
5646 *
5647 * @param[in,out] set Set to use.
5648 * @param[in] options Xpath options.
Michal Vaskob0099a92020-08-31 14:55:23 +02005649 * @return LY_ERR value.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005650 */
Michal Vaskob0099a92020-08-31 14:55:23 +02005651static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005652moveto_root(struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005653{
aPiecek8b0cc152021-05-31 16:40:31 +02005654 assert(!(options & LYXP_SKIP_EXPR));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005655
5656 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02005657 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskoe4a6d012023-05-22 14:34:52 +02005658 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, NULL, set->root_type, LYXP_AXIS_SELF, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005659 } else {
Michal Vaskoef71b392023-08-16 09:45:23 +02005660 lyxp_set_free_content(set);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005661 set_insert_node(set, NULL, 0, set->root_type, 0);
Michal Vasko306e2832022-07-25 09:15:17 +02005662 set->non_child_axis = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005663 }
Michal Vaskob0099a92020-08-31 14:55:23 +02005664
5665 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005666}
5667
5668/**
5669 * @brief Check @p node as a part of NameTest processing.
5670 *
5671 * @param[in] node Node to check.
Michal Vasko49fec8e2022-05-24 10:28:33 +02005672 * @param[in] node_type Node type of @p node.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005673 * @param[in] set Set to read general context from.
Michal Vaskod3678892020-05-21 10:06:58 +02005674 * @param[in] node_name Node name in the dictionary to move to, NULL for any node.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005675 * @param[in] moveto_mod Expected module of the node, NULL for no prefix.
Michal Vaskocdad7122020-11-09 21:04:44 +01005676 * @param[in] options XPath options.
Michal Vasko6346ece2019-09-24 13:12:53 +02005677 * @return LY_ERR (LY_ENOT if node does not match, LY_EINCOMPLETE on unresolved when,
Michal Vaskocd2c88a2022-06-07 10:54:34 +02005678 * LY_EINVAL if neither node nor any children match)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005679 */
5680static LY_ERR
Michal Vasko49fec8e2022-05-24 10:28:33 +02005681moveto_node_check(const struct lyd_node *node, enum lyxp_node_type node_type, const struct lyxp_set *set,
5682 const char *node_name, const struct lys_module *moveto_mod, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005683{
Michal Vaskofbd0da42023-08-17 14:57:44 +02005684 const struct lysc_node *schema;
5685
Michal Vasko49fec8e2022-05-24 10:28:33 +02005686 if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT)) {
5687 assert(node_type == set->root_type);
5688
5689 if (node_name || moveto_mod) {
5690 /* root will not match a specific node */
5691 return LY_ENOT;
5692 }
5693 return LY_SUCCESS;
5694 } else if (node_type != LYXP_NODE_ELEM) {
5695 /* other types will not match */
5696 return LY_ENOT;
5697 }
5698
Michal Vaskofbd0da42023-08-17 14:57:44 +02005699 /* get schema node even of an opaque node */
5700 schema = lyd_node_schema(node);
5701 if (!schema) {
5702 /* unknown opaque node never matches */
Michal Vaskodca9f122021-07-16 13:56:22 +02005703 return LY_ENOT;
5704 }
5705
Michal Vasko03ff5a72019-09-11 13:49:33 +02005706 /* module check */
Michal Vasko19089f02022-06-07 11:02:11 +02005707 if (moveto_mod) {
Michal Vaskofbd0da42023-08-17 14:57:44 +02005708 if ((set->ctx == LYD_CTX(node)) && (schema->module != moveto_mod)) {
Michal Vasko19089f02022-06-07 11:02:11 +02005709 return LY_ENOT;
Michal Vaskofbd0da42023-08-17 14:57:44 +02005710 } else if ((set->ctx != LYD_CTX(node)) && strcmp(schema->module->name, moveto_mod->name)) {
Michal Vasko19089f02022-06-07 11:02:11 +02005711 return LY_ENOT;
5712 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005713 }
5714
Michal Vasko5c4e5892019-11-14 12:31:38 +01005715 /* context check */
Michal Vaskofbd0da42023-08-17 14:57:44 +02005716 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (schema->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005717 return LY_EINVAL;
Michal Vaskofbd0da42023-08-17 14:57:44 +02005718 } else if (set->context_op && (schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && (schema != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005719 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005720 }
5721
5722 /* name check */
Michal Vasko19089f02022-06-07 11:02:11 +02005723 if (node_name) {
Michal Vaskofbd0da42023-08-17 14:57:44 +02005724 if ((set->ctx == LYD_CTX(node)) && (schema->name != node_name)) {
Michal Vasko19089f02022-06-07 11:02:11 +02005725 return LY_ENOT;
Michal Vaskofbd0da42023-08-17 14:57:44 +02005726 } else if ((set->ctx != LYD_CTX(node)) && strcmp(schema->name, node_name)) {
Michal Vasko19089f02022-06-07 11:02:11 +02005727 return LY_ENOT;
5728 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005729 }
5730
Michal Vasko6d657122022-10-19 14:38:35 +02005731 /* when check, accept the context node because it should only be the path ".", we have checked the when is valid before */
Michal Vaskofbd0da42023-08-17 14:57:44 +02005732 if (!(options & LYXP_IGNORE_WHEN) && lysc_has_when(schema) && !(node->flags & LYD_WHEN_TRUE) &&
Michal Vasko6d657122022-10-19 14:38:35 +02005733 (node != set->cur_node)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005734 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01005735 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005736
5737 /* match */
5738 return LY_SUCCESS;
5739}
5740
5741/**
Michal Vasko49fec8e2022-05-24 10:28:33 +02005742 * @brief Get the next node in a forward DFS.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005743 *
Michal Vasko49fec8e2022-05-24 10:28:33 +02005744 * @param[in] iter Last returned node.
5745 * @param[in] stop Node to stop the search on and not return.
5746 * @return Next node, NULL if there are no more.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005747 */
Michal Vasko49fec8e2022-05-24 10:28:33 +02005748static const struct lyd_node *
5749moveto_axis_node_next_dfs_forward(const struct lyd_node *iter, const struct lyd_node *stop)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005750{
Michal Vasko49fec8e2022-05-24 10:28:33 +02005751 const struct lyd_node *next = NULL;
5752
5753 /* 1) child */
5754 next = lyd_child(iter);
5755 if (!next) {
5756 if (iter == stop) {
5757 /* reached stop, no more descendants */
5758 return NULL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005759 }
Michal Vasko49fec8e2022-05-24 10:28:33 +02005760 /* 2) child next sibling */
5761 next = iter->next;
5762 }
5763 while (!next) {
5764 iter = lyd_parent(iter);
5765 if ((!stop && !iter) || (stop && (lyd_parent(iter) == lyd_parent(stop)))) {
5766 return NULL;
5767 }
5768 next = iter->next;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005769 }
5770
Michal Vasko49fec8e2022-05-24 10:28:33 +02005771 return next;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005772}
5773
5774/**
Michal Vasko49fec8e2022-05-24 10:28:33 +02005775 * @brief Get the next node in a backward DFS.
5776 *
5777 * @param[in] iter Last returned node.
5778 * @param[in] stop Node to stop the search on and not return.
5779 * @return Next node, NULL if there are no more.
5780 */
5781static const struct lyd_node *
5782moveto_axis_node_next_dfs_backward(const struct lyd_node *iter, const struct lyd_node *stop)
5783{
5784 const struct lyd_node *next = NULL;
5785
5786 /* 1) previous sibling innermost last child */
5787 next = iter->prev->next ? iter->prev : NULL;
5788 while (next && lyd_child(next)) {
5789 next = lyd_child(next);
5790 next = next->prev;
5791 }
5792
5793 if (!next) {
5794 /* 2) parent */
5795 iter = lyd_parent(iter);
5796 if ((!stop && !iter) || (stop && (lyd_parent(iter) == lyd_parent(stop)))) {
5797 return NULL;
5798 }
5799 next = iter;
5800 }
5801
5802 return next;
5803}
5804
5805/**
5806 * @brief Get the first node on an axis for a context node.
5807 *
5808 * @param[in,out] iter NULL, updated to the next node.
5809 * @param[in,out] iter_type Node type 0 of @p iter, updated to the node type of the next node.
5810 * @param[in] node Context node.
5811 * @param[in] node_type Type of @p node.
5812 * @param[in] axis Axis to use.
5813 * @param[in] set XPath set with the general context.
5814 * @return LY_SUCCESS on success.
5815 * @return LY_ENOTFOUND if no next node found.
5816 */
5817static LY_ERR
5818moveto_axis_node_next_first(const struct lyd_node **iter, enum lyxp_node_type *iter_type, const struct lyd_node *node,
5819 enum lyxp_node_type node_type, enum lyxp_axis axis, struct lyxp_set *set)
5820{
5821 const struct lyd_node *next = NULL;
5822 enum lyxp_node_type next_type = 0;
5823
5824 assert(!*iter);
5825 assert(!*iter_type);
5826
5827 switch (axis) {
5828 case LYXP_AXIS_ANCESTOR_OR_SELF:
5829 case LYXP_AXIS_DESCENDANT_OR_SELF:
5830 case LYXP_AXIS_SELF:
5831 /* return the context node */
5832 next = node;
5833 next_type = node_type;
5834 break;
5835
5836 case LYXP_AXIS_ANCESTOR:
5837 case LYXP_AXIS_PARENT:
5838 if (node_type == LYXP_NODE_ELEM) {
5839 next = lyd_parent(node);
5840 next_type = next ? LYXP_NODE_ELEM : set->root_type;
5841 } else if (node_type == LYXP_NODE_TEXT) {
5842 next = node;
5843 next_type = LYXP_NODE_ELEM;
5844 } else if (node_type == LYXP_NODE_META) {
5845 next = ((struct lyd_meta *)node)->parent;
5846 next_type = LYXP_NODE_ELEM;
5847 } /* else root does not have a parent */
5848 break;
5849
5850 case LYXP_AXIS_CHILD:
5851 if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT)) {
5852 assert(!node);
5853
5854 /* search in all the trees */
5855 next = set->tree;
5856 next_type = next ? LYXP_NODE_ELEM : 0;
5857 } else {
5858 /* search in children */
5859 next = lyd_child(node);
5860 next_type = next ? LYXP_NODE_ELEM : 0;
5861 }
5862 break;
5863
5864 case LYXP_AXIS_DESCENDANT:
5865 if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT)) {
5866 /* top-level nodes */
5867 next = set->tree;
5868 next_type = LYXP_NODE_ELEM;
5869 } else if (node_type == LYXP_NODE_ELEM) {
5870 /* start from the context node */
5871 next = moveto_axis_node_next_dfs_forward(node, node);
5872 next_type = next ? LYXP_NODE_ELEM : 0;
5873 } /* else no children */
5874 break;
5875
5876 case LYXP_AXIS_FOLLOWING:
5877 case LYXP_AXIS_FOLLOWING_SIBLING:
5878 if (node_type == LYXP_NODE_ELEM) {
5879 /* first next sibling */
5880 next = node->next;
5881 next_type = next ? LYXP_NODE_ELEM : 0;
5882 } /* else no sibling */
5883 break;
5884
5885 case LYXP_AXIS_PRECEDING:
5886 if ((node_type == LYXP_NODE_ELEM) && node->prev->next) {
5887 /* skip ancestors */
5888 next = moveto_axis_node_next_dfs_backward(node, NULL);
5889 assert(next);
5890 next_type = LYXP_NODE_ELEM;
5891 } /* else no sibling */
5892 break;
5893
5894 case LYXP_AXIS_PRECEDING_SIBLING:
5895 if (node_type == LYXP_NODE_ELEM) {
5896 /* first previous sibling */
5897 next = node->prev->next ? node->prev : NULL;
5898 next_type = next ? LYXP_NODE_ELEM : 0;
5899 } /* else no sibling */
5900 break;
5901
5902 case LYXP_AXIS_ATTRIBUTE:
5903 /* handled specially */
5904 assert(0);
5905 LOGINT(set->ctx);
5906 break;
5907 }
5908
5909 *iter = next;
5910 *iter_type = next_type;
5911 return next_type ? LY_SUCCESS : LY_ENOTFOUND;
5912}
5913
5914/**
5915 * @brief Iterate over all nodes on an axis for a context node.
5916 *
5917 * @param[in,out] iter Last returned node, start with NULL, updated to the next node.
5918 * @param[in,out] iter_type Node type of @p iter, start with 0, updated to the node type of the next node.
5919 * @param[in] node Context node.
5920 * @param[in] node_type Type of @p node.
5921 * @param[in] axis Axis to use.
5922 * @param[in] set XPath set with the general context.
5923 * @return LY_SUCCESS on success.
5924 * @return LY_ENOTFOUND if no next node found.
5925 */
5926static LY_ERR
5927moveto_axis_node_next(const struct lyd_node **iter, enum lyxp_node_type *iter_type, const struct lyd_node *node,
5928 enum lyxp_node_type node_type, enum lyxp_axis axis, struct lyxp_set *set)
5929{
5930 const struct lyd_node *next = NULL;
5931 enum lyxp_node_type next_type = 0;
5932
5933 if (!*iter_type) {
5934 /* first returned node */
5935 return moveto_axis_node_next_first(iter, iter_type, node, node_type, axis, set);
5936 }
5937
5938 switch (axis) {
5939 case LYXP_AXIS_ANCESTOR_OR_SELF:
5940 if ((*iter == node) && (*iter_type == node_type)) {
5941 /* fake first ancestor, we returned self before */
5942 *iter = NULL;
5943 *iter_type = 0;
5944 return moveto_axis_node_next_first(iter, iter_type, node, node_type, LYXP_AXIS_ANCESTOR, set);
5945 } /* else continue ancestor */
5946
5947 /* fallthrough */
5948 case LYXP_AXIS_ANCESTOR:
5949 if (*iter_type == LYXP_NODE_ELEM) {
5950 /* iter parent */
5951 next = lyd_parent(*iter);
5952 next_type = next ? LYXP_NODE_ELEM : set->root_type;
5953 } /* else root, no ancestors */
5954 break;
5955
5956 case LYXP_AXIS_CHILD:
5957 assert(*iter_type == LYXP_NODE_ELEM);
5958
5959 /* next sibling (child) */
5960 next = (*iter)->next;
5961 next_type = next ? LYXP_NODE_ELEM : 0;
5962 break;
5963
5964 case LYXP_AXIS_DESCENDANT_OR_SELF:
5965 if ((*iter == node) && (*iter_type == node_type)) {
5966 /* fake first descendant, we returned self before */
5967 *iter = NULL;
5968 *iter_type = 0;
5969 return moveto_axis_node_next_first(iter, iter_type, node, node_type, LYXP_AXIS_DESCENDANT, set);
5970 } /* else continue descendant */
5971
5972 /* fallthrough */
5973 case LYXP_AXIS_DESCENDANT:
5974 assert(*iter_type == LYXP_NODE_ELEM);
5975 next = moveto_axis_node_next_dfs_forward(*iter, node);
5976 next_type = next ? LYXP_NODE_ELEM : 0;
5977 break;
5978
5979 case LYXP_AXIS_FOLLOWING:
5980 assert(*iter_type == LYXP_NODE_ELEM);
5981 next = moveto_axis_node_next_dfs_forward(*iter, NULL);
5982 next_type = next ? LYXP_NODE_ELEM : 0;
5983 break;
5984
5985 case LYXP_AXIS_FOLLOWING_SIBLING:
5986 assert(*iter_type == LYXP_NODE_ELEM);
5987
5988 /* next sibling */
5989 next = (*iter)->next;
5990 next_type = next ? LYXP_NODE_ELEM : 0;
5991 break;
5992
5993 case LYXP_AXIS_PARENT:
5994 case LYXP_AXIS_SELF:
5995 /* parent/self was returned before */
5996 break;
5997
5998 case LYXP_AXIS_PRECEDING:
5999 assert(*iter_type == LYXP_NODE_ELEM);
6000 next = moveto_axis_node_next_dfs_backward(*iter, NULL);
6001 next_type = next ? LYXP_NODE_ELEM : 0;
6002 break;
6003
6004 case LYXP_AXIS_PRECEDING_SIBLING:
6005 assert(*iter_type == LYXP_NODE_ELEM);
6006
6007 /* previous sibling */
6008 next = (*iter)->prev->next ? (*iter)->prev : NULL;
6009 next_type = next ? LYXP_NODE_ELEM : 0;
6010 break;
6011
6012 case LYXP_AXIS_ATTRIBUTE:
6013 /* handled specially */
6014 assert(0);
6015 LOGINT(set->ctx);
6016 break;
6017 }
6018
6019 *iter = next;
6020 *iter_type = next_type;
6021 return next_type ? LY_SUCCESS : LY_ENOTFOUND;
6022}
6023
6024/**
6025 * @brief Move context @p set to a node. Result is LYXP_SET_NODE_SET. Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006026 *
6027 * @param[in,out] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006028 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02006029 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko49fec8e2022-05-24 10:28:33 +02006030 * @param[in] axis Axis to search on.
Michal Vaskocdad7122020-11-09 21:04:44 +01006031 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006032 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6033 */
6034static LY_ERR
Michal Vasko49fec8e2022-05-24 10:28:33 +02006035moveto_node(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, enum lyxp_axis axis,
6036 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006037{
Michal Vasko230cf972021-12-02 12:31:00 +01006038 LY_ERR r, rc = LY_SUCCESS;
Michal Vasko49fec8e2022-05-24 10:28:33 +02006039 const struct lyd_node *iter;
6040 enum lyxp_node_type iter_type;
Michal Vasko230cf972021-12-02 12:31:00 +01006041 struct lyxp_set result;
Michal Vasko49fec8e2022-05-24 10:28:33 +02006042 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006043
aPiecek8b0cc152021-05-31 16:40:31 +02006044 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006045 return LY_SUCCESS;
6046 }
6047
6048 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006049 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006050 return LY_EVALID;
6051 }
6052
Michal Vasko230cf972021-12-02 12:31:00 +01006053 /* init result set */
6054 set_init(&result, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006055
Michal Vasko49fec8e2022-05-24 10:28:33 +02006056 for (i = 0; i < set->used; ++i) {
6057 /* iterate over all the nodes on the axis of the node */
6058 iter = NULL;
6059 iter_type = 0;
6060 while (!moveto_axis_node_next(&iter, &iter_type, set->val.nodes[i].node, set->val.nodes[i].type, axis, set)) {
6061 r = moveto_node_check(iter, iter_type, set, ncname, moveto_mod, options);
6062 if (r == LY_EINCOMPLETE) {
Michal Vasko230cf972021-12-02 12:31:00 +01006063 rc = r;
6064 goto cleanup;
Michal Vasko49fec8e2022-05-24 10:28:33 +02006065 } else if (r) {
6066 continue;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006067 }
Michal Vasko49fec8e2022-05-24 10:28:33 +02006068
6069 /* check for duplicates if they are possible */
6070 switch (axis) {
6071 case LYXP_AXIS_ANCESTOR:
6072 case LYXP_AXIS_ANCESTOR_OR_SELF:
6073 case LYXP_AXIS_DESCENDANT:
6074 case LYXP_AXIS_DESCENDANT_OR_SELF:
6075 case LYXP_AXIS_FOLLOWING:
6076 case LYXP_AXIS_FOLLOWING_SIBLING:
6077 case LYXP_AXIS_PARENT:
6078 case LYXP_AXIS_PRECEDING:
6079 case LYXP_AXIS_PRECEDING_SIBLING:
Michal Vasko306e2832022-07-25 09:15:17 +02006080 result.non_child_axis = 1;
Michal Vasko49fec8e2022-05-24 10:28:33 +02006081 if (set_dup_node_check(&result, iter, iter_type, -1)) {
6082 continue;
6083 }
6084 break;
6085 case LYXP_AXIS_CHILD:
6086 case LYXP_AXIS_SELF:
6087 break;
6088 case LYXP_AXIS_ATTRIBUTE:
6089 /* handled specially */
6090 assert(0);
6091 LOGINT(set->ctx);
6092 break;
6093 }
6094
6095 /* matching node */
6096 set_insert_node(&result, iter, 0, iter_type, result.used);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006097 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006098 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006099
Michal Vasko230cf972021-12-02 12:31:00 +01006100 /* move result to the set */
6101 lyxp_set_free_content(set);
6102 *set = result;
6103 result.type = LYXP_SET_NUMBER;
Michal Vasko49fec8e2022-05-24 10:28:33 +02006104
Michal Vasko306e2832022-07-25 09:15:17 +02006105 /* sort the final set if the document order could have been broken */
6106 if (set->non_child_axis) {
6107 set_sort(set);
6108 } else {
6109 assert(!set_sort(set));
6110 }
Michal Vasko230cf972021-12-02 12:31:00 +01006111
6112cleanup:
6113 lyxp_set_free_content(&result);
6114 return rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006115}
6116
6117/**
Michal Vasko49fec8e2022-05-24 10:28:33 +02006118 * @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 +02006119 *
6120 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02006121 * @param[in] scnode Matching node schema.
Michal Vasko004d3152020-06-11 19:59:22 +02006122 * @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 +01006123 * @param[in] options XPath options.
Michal Vaskod3678892020-05-21 10:06:58 +02006124 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6125 */
6126static LY_ERR
Michal Vasko49fec8e2022-05-24 10:28:33 +02006127moveto_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 +01006128 uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02006129{
Michal Vaskoddd76592022-01-17 13:34:48 +01006130 LY_ERR ret = LY_SUCCESS, r;
Michal Vaskod3678892020-05-21 10:06:58 +02006131 uint32_t i;
Michal Vaskod3678892020-05-21 10:06:58 +02006132 const struct lyd_node *siblings;
Michal Vasko230cf972021-12-02 12:31:00 +01006133 struct lyxp_set result;
Michal Vasko004d3152020-06-11 19:59:22 +02006134 struct lyd_node *sub, *inst = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02006135
Michal Vasko004d3152020-06-11 19:59:22 +02006136 assert(scnode && (!(scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)) || predicates));
Michal Vaskod3678892020-05-21 10:06:58 +02006137
Michal Vasko50aaa072021-12-02 13:11:56 +01006138 /* init result set */
6139 set_init(&result, set);
6140
aPiecek8b0cc152021-05-31 16:40:31 +02006141 if (options & LYXP_SKIP_EXPR) {
Michal Vasko004d3152020-06-11 19:59:22 +02006142 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02006143 }
6144
6145 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006146 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko004d3152020-06-11 19:59:22 +02006147 ret = LY_EVALID;
6148 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02006149 }
6150
6151 /* context check for all the nodes since we have the schema node */
6152 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) {
6153 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02006154 goto cleanup;
Michal Vasko69730152020-10-09 16:30:07 +02006155 } else if (set->context_op && (scnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) &&
6156 (scnode != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02006157 lyxp_set_free_content(set);
6158 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +02006159 }
6160
6161 /* create specific data instance if needed */
6162 if (scnode->nodetype == LYS_LIST) {
Michal Vasko90189962023-02-28 12:10:34 +01006163 LY_CHECK_GOTO(ret = lyd_create_list(scnode, predicates, NULL, &inst), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02006164 } else if (scnode->nodetype == LYS_LEAFLIST) {
6165 LY_CHECK_GOTO(ret = lyd_create_term2(scnode, &predicates[0].value, &inst), cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02006166 }
6167
Michal Vasko230cf972021-12-02 12:31:00 +01006168 for (i = 0; i < set->used; ++i) {
Michal Vaskod3678892020-05-21 10:06:58 +02006169 siblings = NULL;
6170
6171 if ((set->val.nodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.nodes[i].type == LYXP_NODE_ROOT)) {
6172 assert(!set->val.nodes[i].node);
6173
6174 /* search in all the trees */
6175 siblings = set->tree;
6176 } else if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
6177 /* search in children */
Radek Krejcia1c1e542020-09-29 16:06:52 +02006178 siblings = lyd_child(set->val.nodes[i].node);
Michal Vaskod3678892020-05-21 10:06:58 +02006179 }
6180
6181 /* find the node using hashes */
Michal Vasko004d3152020-06-11 19:59:22 +02006182 if (inst) {
Michal Vaskoddd76592022-01-17 13:34:48 +01006183 r = lyd_find_sibling_first(siblings, inst, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02006184 } else {
Michal Vaskoddd76592022-01-17 13:34:48 +01006185 r = lyd_find_sibling_val(siblings, scnode, NULL, 0, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02006186 }
Michal Vaskod8a52012023-08-15 11:38:10 +02006187 if (r == LY_ENOTFOUND) {
6188 /* may still be an opaque node */
6189 r = lyd_find_sibling_opaq_next(siblings, scnode->name, &sub);
6190 }
Michal Vaskoddd76592022-01-17 13:34:48 +01006191 LY_CHECK_ERR_GOTO(r && (r != LY_ENOTFOUND), ret = r, cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02006192
6193 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01006194 if (!(options & LYXP_IGNORE_WHEN) && sub && lysc_has_when(sub->schema) && !(sub->flags & LYD_WHEN_TRUE)) {
Michal Vasko004d3152020-06-11 19:59:22 +02006195 ret = LY_EINCOMPLETE;
6196 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02006197 }
6198
6199 if (sub) {
6200 /* pos filled later */
Michal Vasko230cf972021-12-02 12:31:00 +01006201 set_insert_node(&result, sub, 0, LYXP_NODE_ELEM, result.used);
Michal Vaskod3678892020-05-21 10:06:58 +02006202 }
6203 }
6204
Michal Vasko230cf972021-12-02 12:31:00 +01006205 /* move result to the set */
6206 lyxp_set_free_content(set);
6207 *set = result;
6208 result.type = LYXP_SET_NUMBER;
6209 assert(!set_sort(set));
6210
Michal Vasko004d3152020-06-11 19:59:22 +02006211cleanup:
Michal Vasko230cf972021-12-02 12:31:00 +01006212 lyxp_set_free_content(&result);
Michal Vasko004d3152020-06-11 19:59:22 +02006213 lyd_free_tree(inst);
6214 return ret;
Michal Vaskod3678892020-05-21 10:06:58 +02006215}
6216
6217/**
Michal Vasko49fec8e2022-05-24 10:28:33 +02006218 * @brief Check @p node as a part of schema NameTest processing.
6219 *
6220 * @param[in] node Schema node to check.
6221 * @param[in] ctx_scnode Context node.
6222 * @param[in] set Set to read general context from.
6223 * @param[in] node_name Node name in the dictionary to move to, NULL for any nodes.
6224 * @param[in] moveto_mod Expected module of the node, NULL for no prefix.
6225 * @return LY_ERR (LY_ENOT if node does not match, LY_EINVAL if neither node nor any children match)
6226 */
6227static LY_ERR
6228moveto_scnode_check(const struct lysc_node *node, const struct lysc_node *ctx_scnode, const struct lyxp_set *set,
6229 const char *node_name, const struct lys_module *moveto_mod)
6230{
6231 if (!moveto_mod && node_name) {
6232 switch (set->format) {
6233 case LY_VALUE_SCHEMA:
6234 case LY_VALUE_SCHEMA_RESOLVED:
6235 /* use current module */
6236 moveto_mod = set->cur_mod;
6237 break;
6238 case LY_VALUE_JSON:
6239 case LY_VALUE_LYB:
6240 case LY_VALUE_STR_NS:
6241 /* inherit module of the context node, if any */
6242 if (ctx_scnode) {
6243 moveto_mod = ctx_scnode->module;
6244 }
6245 break;
6246 case LY_VALUE_CANON:
6247 case LY_VALUE_XML:
6248 /* not defined */
6249 LOGINT(set->ctx);
6250 return LY_EINVAL;
6251 }
6252 }
6253
6254 if (!node) {
6255 /* root will not match a specific node */
6256 if (node_name || moveto_mod) {
6257 return LY_ENOT;
6258 }
6259 return LY_SUCCESS;
6260 }
6261
6262 /* module check */
6263 if (moveto_mod && (node->module != moveto_mod)) {
6264 return LY_ENOT;
6265 }
6266
6267 /* context check */
6268 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (node->flags & LYS_CONFIG_R)) {
6269 return LY_EINVAL;
6270 } else if (set->context_op && (node->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && (node != set->context_op)) {
6271 return LY_EINVAL;
6272 }
6273
6274 /* name check */
6275 if (node_name && (node->name != node_name)) {
6276 return LY_ENOT;
6277 }
6278
6279 /* match */
6280 return LY_SUCCESS;
6281}
6282
6283/**
6284 * @brief Get the next node in a forward schema node DFS.
6285 *
6286 * @param[in] iter Last returned node.
6287 * @param[in] stop Node to stop the search on and not return.
6288 * @param[in] getnext_opts Options for ::lys_getnext().
6289 * @return Next node, NULL if there are no more.
6290 */
6291static const struct lysc_node *
6292moveto_axis_scnode_next_dfs_forward(const struct lysc_node *iter, const struct lysc_node *stop, uint32_t getnext_opts)
6293{
6294 const struct lysc_node *next = NULL;
6295
6296 next = lysc_node_child(iter);
6297 if (!next) {
6298 /* no children, try siblings */
Michal Vasko34a22fe2022-06-15 07:58:55 +02006299 if ((iter == stop) || !lysc_data_parent(iter)) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02006300 /* we are done, no next element to process */
6301 return NULL;
6302 }
6303
6304 next = lys_getnext(iter, lysc_data_parent(iter), NULL, getnext_opts);
6305 }
6306 while (!next && iter) {
6307 /* parent is already processed, go to its sibling */
6308 iter = iter->parent;
Michal Vasko34a22fe2022-06-15 07:58:55 +02006309 if ((iter == stop) || !lysc_data_parent(iter)) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02006310 /* we are done, no next element to process */
6311 return NULL;
6312 }
6313 next = lys_getnext(iter, lysc_data_parent(iter), NULL, getnext_opts);
6314 }
6315
6316 return next;
6317}
6318
6319/**
6320 * @brief Consider schema node based on its in_ctx enum value.
6321 *
6322 * @param[in,out] in_ctx In_ctx enum of the schema node, may be updated.
6323 * @param[in] axis Axis to use.
6324 * @return LY_SUCCESS on success.
6325 * @return LY_ENOT if the node should not be returned.
6326 */
6327static LY_ERR
6328moveto_axis_scnode_next_in_ctx(int32_t *in_ctx, enum lyxp_axis axis)
6329{
6330 switch (axis) {
6331 case LYXP_AXIS_SELF:
6332 if ((*in_ctx == LYXP_SET_SCNODE_START) || (*in_ctx == LYXP_SET_SCNODE_ATOM_CTX)) {
6333 /* additionally put the start node into context */
6334 *in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
6335 return LY_SUCCESS;
6336 }
6337 break;
6338 case LYXP_AXIS_PARENT:
6339 case LYXP_AXIS_ANCESTOR_OR_SELF:
6340 case LYXP_AXIS_ANCESTOR:
6341 case LYXP_AXIS_DESCENDANT_OR_SELF:
6342 case LYXP_AXIS_DESCENDANT:
6343 case LYXP_AXIS_FOLLOWING:
6344 case LYXP_AXIS_FOLLOWING_SIBLING:
6345 case LYXP_AXIS_PRECEDING:
6346 case LYXP_AXIS_PRECEDING_SIBLING:
6347 case LYXP_AXIS_CHILD:
6348 if (*in_ctx == LYXP_SET_SCNODE_START) {
6349 /* remember that context node was used */
6350 *in_ctx = LYXP_SET_SCNODE_START_USED;
6351 return LY_SUCCESS;
6352 } else if (*in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
6353 /* traversed */
6354 *in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
6355 return LY_SUCCESS;
6356 }
6357 break;
6358 case LYXP_AXIS_ATTRIBUTE:
6359 /* unreachable */
6360 assert(0);
6361 LOGINT(NULL);
6362 break;
6363 }
6364
6365 return LY_ENOT;
6366}
6367
6368/**
6369 * @brief Get previous sibling for a schema node.
6370 *
6371 * @param[in] scnode Schema node.
6372 * @param[in] getnext_opts Options for ::lys_getnext().
6373 * @return Previous sibling, NULL if none.
6374 */
6375static const struct lysc_node *
6376moveto_axis_scnode_preceding_sibling(const struct lysc_node *scnode, uint32_t getnext_opts)
6377{
6378 const struct lysc_node *next = NULL, *prev = NULL;
6379
6380 while ((next = lys_getnext(next, lysc_data_parent(scnode), scnode->module->compiled, getnext_opts))) {
6381 if (next == scnode) {
6382 break;
6383 }
6384
6385 prev = next;
6386 }
6387
6388 return prev;
6389}
6390
6391/**
6392 * @brief Get the first schema node on an axis for a context node.
6393 *
6394 * @param[in,out] iter Last returned node, start with NULL, updated to the next node.
6395 * @param[in,out] iter_type Node type of @p iter, start with 0, updated to the node type of the next node.
6396 * @param[in,out] iter_mod Internal module iterator, do not change.
6397 * @param[in,out] iter_mod_idx Internal module index iterator, do not change.
6398 * @param[in] scnode Context node.
6399 * @param[in] node_type Type of @p scnode.
6400 * @param[in] in_ctx In_ctx enum of @p scnode.
6401 * @param[in] axis Axis to use.
6402 * @param[in] set XPath set with the general context.
6403 * @param[in] getnext_opts Options for ::lys_getnext().
6404 * @return LY_SUCCESS on success.
6405 * @return LY_ENOTFOUND if no next node found.
6406 */
6407static LY_ERR
6408moveto_axis_scnode_next_first(const struct lysc_node **iter, enum lyxp_node_type *iter_type, const struct lys_module **iter_mod,
6409 uint32_t *iter_mod_idx, const struct lysc_node *scnode, enum lyxp_node_type node_type, enum lyxp_axis axis,
6410 struct lyxp_set *set, uint32_t getnext_opts)
6411{
6412 const struct lysc_node *next = NULL;
6413 enum lyxp_node_type next_type = 0;
6414
6415 assert(!*iter);
6416 assert(!*iter_type);
6417
6418 *iter_mod = NULL;
6419 *iter_mod_idx = 0;
6420
6421 switch (axis) {
6422 case LYXP_AXIS_ANCESTOR_OR_SELF:
6423 case LYXP_AXIS_DESCENDANT_OR_SELF:
6424 case LYXP_AXIS_SELF:
6425 if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT) || (node_type == LYXP_NODE_ELEM)) {
6426 /* just return the node */
6427 next = scnode;
6428 next_type = node_type;
6429 }
6430 break;
6431
6432 case LYXP_AXIS_ANCESTOR:
6433 case LYXP_AXIS_PARENT:
6434 if (node_type == LYXP_NODE_ELEM) {
6435 next = lysc_data_parent(scnode);
6436 next_type = next ? LYXP_NODE_ELEM : set->root_type;
6437 } /* else no parent */
6438 break;
6439
6440 case LYXP_AXIS_DESCENDANT:
6441 case LYXP_AXIS_CHILD:
6442 if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT)) {
6443 /* it can actually be in any module, it's all <running>, and even if it's moveto_mod (if set),
6444 * it can be in a top-level augment */
6445 while ((*iter_mod = ly_ctx_get_module_iter(set->ctx, iter_mod_idx))) {
6446 /* module may not be implemented or not compiled yet */
6447 if (!(*iter_mod)->compiled) {
6448 continue;
6449 }
6450
6451 /* get next node */
6452 if ((next = lys_getnext(NULL, NULL, (*iter_mod)->compiled, getnext_opts))) {
6453 next_type = LYXP_NODE_ELEM;
6454 break;
6455 }
6456 }
6457 } else if (node_type == LYXP_NODE_ELEM) {
6458 /* get next node */
6459 next = lys_getnext(NULL, scnode, NULL, getnext_opts);
6460 next_type = next ? LYXP_NODE_ELEM : 0;
6461 }
6462 break;
6463
6464 case LYXP_AXIS_FOLLOWING:
6465 case LYXP_AXIS_FOLLOWING_SIBLING:
6466 if (node_type == LYXP_NODE_ELEM) {
6467 /* first next sibling */
6468 next = lys_getnext(scnode, lysc_data_parent(scnode), scnode->module->compiled, getnext_opts);
6469 next_type = next ? LYXP_NODE_ELEM : 0;
6470 } /* else no sibling */
6471 break;
6472
6473 case LYXP_AXIS_PRECEDING:
6474 case LYXP_AXIS_PRECEDING_SIBLING:
6475 if (node_type == LYXP_NODE_ELEM) {
6476 /* first parent sibling */
6477 next = lys_getnext(NULL, lysc_data_parent(scnode), scnode->module->compiled, getnext_opts);
6478 if (next == scnode) {
6479 /* no preceding sibling */
6480 next = NULL;
6481 }
6482 next_type = next ? LYXP_NODE_ELEM : 0;
6483 } /* else no sibling */
6484 break;
6485
6486 case LYXP_AXIS_ATTRIBUTE:
6487 /* unreachable */
6488 assert(0);
6489 LOGINT(set->ctx);
6490 break;
6491 }
6492
6493 *iter = next;
6494 *iter_type = next_type;
6495 return next_type ? LY_SUCCESS : LY_ENOTFOUND;
6496}
6497
6498/**
6499 * @brief Iterate over all schema nodes on an axis for a context node.
6500 *
6501 * @param[in,out] iter Last returned node, start with NULL, updated to the next node.
6502 * @param[in,out] iter_type Node type of @p iter, start with 0, updated to the node type of the next node.
6503 * @param[in,out] iter_mod Internal module iterator, do not change.
6504 * @param[in,out] iter_mod_idx Internal module index iterator, do not change.
6505 * @param[in] scnode Context node.
6506 * @param[in] node_type Type of @p scnode.
6507 * @param[in] axis Axis to use.
6508 * @param[in] set XPath set with the general context.
6509 * @param[in] getnext_opts Options for ::lys_getnext().
6510 * @return LY_SUCCESS on success.
6511 * @return LY_ENOTFOUND if no next node found.
6512 */
6513static LY_ERR
6514moveto_axis_scnode_next(const struct lysc_node **iter, enum lyxp_node_type *iter_type, const struct lys_module **iter_mod,
6515 uint32_t *iter_mod_idx, const struct lysc_node *scnode, enum lyxp_node_type node_type, enum lyxp_axis axis,
6516 struct lyxp_set *set, uint32_t getnext_opts)
6517{
6518 const struct lysc_node *next = NULL, *dfs_stop;
6519 enum lyxp_node_type next_type = 0;
6520
6521 if (!*iter_type) {
6522 /* first returned node */
6523 return moveto_axis_scnode_next_first(iter, iter_type, iter_mod, iter_mod_idx, scnode, node_type, axis, set,
6524 getnext_opts);
6525 }
6526
6527 switch (axis) {
6528 case LYXP_AXIS_PARENT:
6529 case LYXP_AXIS_SELF:
6530 /* parent/self was returned before */
6531 break;
6532
6533 case LYXP_AXIS_ANCESTOR_OR_SELF:
6534 if ((*iter == scnode) && (*iter_type == node_type)) {
6535 /* fake first ancestor, we returned self before */
6536 *iter = NULL;
6537 *iter_type = 0;
6538 return moveto_axis_scnode_next_first(iter, iter_type, iter_mod, iter_mod_idx, scnode, node_type,
6539 LYXP_AXIS_ANCESTOR, set, getnext_opts);
6540 } /* else continue ancestor */
6541
6542 /* fallthrough */
6543 case LYXP_AXIS_ANCESTOR:
6544 if (*iter_type == LYXP_NODE_ELEM) {
6545 next = lysc_data_parent(*iter);
6546 next_type = next ? LYXP_NODE_ELEM : set->root_type;
6547 } /* else no ancestor */
6548 break;
6549
6550 case LYXP_AXIS_DESCENDANT_OR_SELF:
6551 if ((*iter == scnode) && (*iter_type == node_type)) {
6552 /* fake first descendant, we returned self before */
6553 *iter = NULL;
6554 *iter_type = 0;
6555 return moveto_axis_scnode_next_first(iter, iter_type, iter_mod, iter_mod_idx, scnode, node_type,
6556 LYXP_AXIS_DESCENDANT, set, getnext_opts);
6557 } /* else DFS until context node */
6558 dfs_stop = scnode;
6559
6560 /* fallthrough */
6561 case LYXP_AXIS_DESCENDANT:
6562 if (axis == LYXP_AXIS_DESCENDANT) {
6563 /* DFS until the context node */
6564 dfs_stop = scnode;
6565 }
6566
6567 /* fallthrough */
6568 case LYXP_AXIS_PRECEDING:
6569 if (axis == LYXP_AXIS_PRECEDING) {
6570 /* DFS until the previous sibling */
6571 dfs_stop = moveto_axis_scnode_preceding_sibling(scnode, getnext_opts);
6572 assert(dfs_stop);
6573
6574 if (*iter == dfs_stop) {
6575 /* we are done */
6576 break;
6577 }
6578 }
6579
6580 /* fallthrough */
6581 case LYXP_AXIS_FOLLOWING:
6582 if (axis == LYXP_AXIS_FOLLOWING) {
6583 /* DFS through the whole module */
6584 dfs_stop = NULL;
6585 }
6586
6587 /* nested nodes */
6588 assert(*iter);
6589 next = moveto_axis_scnode_next_dfs_forward(*iter, dfs_stop, getnext_opts);
6590 if (next) {
6591 next_type = LYXP_NODE_ELEM;
6592 break;
6593 } /* else get next top-level node just like a child */
6594
6595 /* fallthrough */
6596 case LYXP_AXIS_CHILD:
6597 case LYXP_AXIS_FOLLOWING_SIBLING:
6598 if (!*iter_mod) {
6599 /* nodes from a single module */
6600 if ((next = lys_getnext(*iter, lysc_data_parent(*iter), (*iter)->module->compiled, getnext_opts))) {
6601 next_type = LYXP_NODE_ELEM;
6602 break;
6603 }
6604
6605 assert(scnode);
Michal Vaskoa353cce2022-11-14 10:09:55 +01006606 if ((axis != LYXP_AXIS_CHILD) && !lysc_data_parent(scnode)) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02006607 /* iterating over top-level nodes, find next */
6608 while (lysc_data_parent(*iter)) {
6609 *iter = lysc_data_parent(*iter);
6610 }
6611 if ((next = lys_getnext(*iter, NULL, (*iter)->module->compiled, getnext_opts))) {
6612 next_type = LYXP_NODE_ELEM;
6613 break;
6614 }
6615 }
6616 }
6617
6618 while (*iter_mod) {
6619 /* module top-level nodes */
6620 if ((next = lys_getnext(*iter, NULL, (*iter_mod)->compiled, getnext_opts))) {
6621 next_type = LYXP_NODE_ELEM;
6622 break;
6623 }
6624
6625 /* get next module */
6626 while ((*iter_mod = ly_ctx_get_module_iter(set->ctx, iter_mod_idx))) {
6627 /* module may not be implemented or not compiled yet */
6628 if ((*iter_mod)->compiled) {
6629 break;
6630 }
6631 }
6632
6633 /* new module, start over */
6634 *iter = NULL;
6635 }
6636 break;
6637
6638 case LYXP_AXIS_PRECEDING_SIBLING:
6639 assert(*iter);
6640
6641 /* next parent sibling until scnode */
6642 next = lys_getnext(*iter, lysc_data_parent(*iter), (*iter)->module->compiled, getnext_opts);
6643 if (next == scnode) {
6644 /* no previous sibling */
6645 next = NULL;
6646 }
6647 next_type = next ? LYXP_NODE_ELEM : 0;
6648 break;
6649
6650 case LYXP_AXIS_ATTRIBUTE:
6651 /* unreachable */
6652 assert(0);
6653 LOGINT(set->ctx);
6654 break;
6655 }
6656
6657 *iter = next;
6658 *iter_type = next_type;
6659 return next_type ? LY_SUCCESS : LY_ENOTFOUND;
6660}
6661
6662/**
Michal Vaskod3678892020-05-21 10:06:58 +02006663 * @brief Move context @p set to a schema node. Result is LYXP_SET_SCNODE_SET (or LYXP_SET_EMPTY).
6664 *
6665 * @param[in,out] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006666 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02006667 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko49fec8e2022-05-24 10:28:33 +02006668 * @param[in] axis Axis to search on.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006669 * @param[in] options XPath options.
6670 * @return LY_ERR
6671 */
6672static LY_ERR
Michal Vasko49fec8e2022-05-24 10:28:33 +02006673moveto_scnode(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, enum lyxp_axis axis,
6674 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006675{
Radek Krejci857189e2020-09-01 13:26:36 +02006676 ly_bool temp_ctx = 0;
Michal Vasko49fec8e2022-05-24 10:28:33 +02006677 uint32_t getnext_opts, orig_used, i, mod_idx, idx;
stewegd8e2fc92023-05-31 09:52:56 +02006678 const struct lys_module *mod = NULL;
Michal Vasko49fec8e2022-05-24 10:28:33 +02006679 const struct lysc_node *iter;
6680 enum lyxp_node_type iter_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006681
aPiecek8b0cc152021-05-31 16:40:31 +02006682 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006683 return LY_SUCCESS;
6684 }
6685
6686 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006687 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006688 return LY_EVALID;
6689 }
6690
Michal Vaskocafad9d2019-11-07 15:20:03 +01006691 /* getnext opts */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01006692 getnext_opts = 0;
Michal Vaskocafad9d2019-11-07 15:20:03 +01006693 if (options & LYXP_SCNODE_OUTPUT) {
6694 getnext_opts |= LYS_GETNEXT_OUTPUT;
6695 }
Michal Vaskoe482bb92023-02-01 11:41:41 +01006696 if (options & LYXP_SCNODE_SCHEMAMOUNT) {
6697 getnext_opts |= LYS_GETNEXT_WITHSCHEMAMOUNT;
6698 }
Michal Vaskocafad9d2019-11-07 15:20:03 +01006699
Michal Vasko03ff5a72019-09-11 13:49:33 +02006700 orig_used = set->used;
6701 for (i = 0; i < orig_used; ++i) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02006702 /* update in_ctx first */
6703 if (moveto_axis_scnode_next_in_ctx(&set->val.scnodes[i].in_ctx, axis)) {
6704 /* not usable, skip */
6705 continue;
6706 }
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006707
Michal Vasko49fec8e2022-05-24 10:28:33 +02006708 iter = NULL;
6709 iter_type = 0;
6710 while (!moveto_axis_scnode_next(&iter, &iter_type, &mod, &mod_idx, set->val.scnodes[i].scnode,
6711 set->val.scnodes[i].type, axis, set, getnext_opts)) {
6712 if (moveto_scnode_check(iter, NULL, set, ncname, moveto_mod)) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006713 continue;
6714 }
6715
Michal Vasko49fec8e2022-05-24 10:28:33 +02006716 /* insert */
Michal Vaskoe4a6d012023-05-22 14:34:52 +02006717 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, iter_type, axis, &idx));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006718
Michal Vasko49fec8e2022-05-24 10:28:33 +02006719 /* we need to prevent these nodes from being considered in this moveto */
6720 if ((idx < orig_used) && (idx > i)) {
6721 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX;
6722 temp_ctx = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006723 }
6724 }
Michal Vasko66330fc2022-11-21 15:52:24 +01006725
6726 if (moveto_mod && ncname && ((axis == LYXP_AXIS_DESCENDANT) || (axis == LYXP_AXIS_CHILD)) &&
6727 (set->val.scnodes[i].type == LYXP_NODE_ELEM) && !ly_nested_ext_schema(NULL, set->val.scnodes[i].scnode,
6728 moveto_mod->name, strlen(moveto_mod->name), LY_VALUE_JSON, NULL, ncname, strlen(ncname), &iter, NULL)) {
6729 /* there is a matching node from an extension, use it */
Michal Vaskoe4a6d012023-05-22 14:34:52 +02006730 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, axis, &idx));
Michal Vasko66330fc2022-11-21 15:52:24 +01006731 if ((idx < orig_used) && (idx > i)) {
6732 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX;
6733 temp_ctx = 1;
6734 }
6735 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006736 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006737
6738 /* correct temporary in_ctx values */
6739 if (temp_ctx) {
6740 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006741 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_NEW_CTX) {
6742 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006743 }
6744 }
6745 }
6746
6747 return LY_SUCCESS;
6748}
6749
6750/**
Michal Vasko49fec8e2022-05-24 10:28:33 +02006751 * @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 +02006752 * Context position aware.
6753 *
6754 * @param[in] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006755 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02006756 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01006757 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006758 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6759 */
6760static LY_ERR
Michal Vasko49fec8e2022-05-24 10:28:33 +02006761moveto_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 +02006762{
6763 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006764 const struct lyd_node *next, *elem, *start;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006765 struct lyxp_set ret_set;
6766 LY_ERR rc;
6767
aPiecek8b0cc152021-05-31 16:40:31 +02006768 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006769 return LY_SUCCESS;
6770 }
6771
6772 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006773 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006774 return LY_EVALID;
6775 }
6776
Michal Vasko9f96a052020-03-10 09:41:45 +01006777 /* 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 +02006778 rc = xpath_pi_node(set, LYXP_AXIS_CHILD, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006779 LY_CHECK_RET(rc);
6780
Michal Vasko6346ece2019-09-24 13:12:53 +02006781 /* this loop traverses all the nodes in the set and adds/keeps only those that match qname */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006782 set_init(&ret_set, set);
6783 for (i = 0; i < set->used; ++i) {
6784
6785 /* TREE DFS */
6786 start = set->val.nodes[i].node;
6787 for (elem = next = start; elem; elem = next) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02006788 rc = moveto_node_check(elem, LYXP_NODE_ELEM, set, ncname, moveto_mod, options);
Michal Vasko6346ece2019-09-24 13:12:53 +02006789 if (!rc) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006790 /* add matching node into result set */
6791 set_insert_node(&ret_set, elem, 0, LYXP_NODE_ELEM, ret_set.used);
6792 if (set_dup_node_check(set, elem, LYXP_NODE_ELEM, i)) {
6793 /* the node is a duplicate, we'll process it later in the set */
6794 goto skip_children;
6795 }
Michal Vasko6346ece2019-09-24 13:12:53 +02006796 } else if (rc == LY_EINCOMPLETE) {
Michal Vasko6346ece2019-09-24 13:12:53 +02006797 return rc;
6798 } else if (rc == LY_EINVAL) {
6799 goto skip_children;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006800 }
6801
6802 /* TREE DFS NEXT ELEM */
6803 /* select element for the next run - children first */
Radek Krejcia1c1e542020-09-29 16:06:52 +02006804 next = lyd_child(elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006805 if (!next) {
6806skip_children:
6807 /* no children, so try siblings, but only if it's not the start,
6808 * that is considered to be the root and it's siblings are not traversed */
6809 if (elem != start) {
6810 next = elem->next;
6811 } else {
6812 break;
6813 }
6814 }
6815 while (!next) {
6816 /* no siblings, go back through the parents */
Michal Vasko9e685082021-01-29 14:49:09 +01006817 if (lyd_parent(elem) == start) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006818 /* we are done, no next element to process */
6819 break;
6820 }
6821 /* parent is already processed, go to its sibling */
Michal Vasko9e685082021-01-29 14:49:09 +01006822 elem = lyd_parent(elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006823 next = elem->next;
6824 }
6825 }
6826 }
6827
6828 /* make the temporary set the current one */
6829 ret_set.ctx_pos = set->ctx_pos;
6830 ret_set.ctx_size = set->ctx_size;
Michal Vaskod3678892020-05-21 10:06:58 +02006831 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006832 memcpy(set, &ret_set, sizeof *set);
Michal Vasko306e2832022-07-25 09:15:17 +02006833 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006834
6835 return LY_SUCCESS;
6836}
6837
6838/**
Michal Vasko49fec8e2022-05-24 10:28:33 +02006839 * @brief Move context @p set to a child schema node and all its descendants starting from a node.
6840 * Result is LYXP_SET_NODE_SET.
6841 *
6842 * @param[in] set Set to use.
6843 * @param[in] start Start node whose subtree to add.
6844 * @param[in] start_idx Index of @p start in @p set.
6845 * @param[in] moveto_mod Matching node module, NULL for no prefix.
6846 * @param[in] ncname Matching node name in the dictionary, NULL for any.
6847 * @param[in] options XPath options.
6848 * @return LY_ERR value.
6849 */
6850static LY_ERR
6851moveto_scnode_dfs(struct lyxp_set *set, const struct lysc_node *start, uint32_t start_idx,
6852 const struct lys_module *moveto_mod, const char *ncname, uint32_t options)
6853{
6854 const struct lysc_node *next, *elem;
6855 uint32_t idx;
6856 LY_ERR rc;
6857
6858 /* TREE DFS */
6859 for (elem = next = start; elem; elem = next) {
6860 if ((elem == start) || (elem->nodetype & (LYS_CHOICE | LYS_CASE))) {
6861 /* schema-only nodes, skip root */
6862 goto next_iter;
6863 }
6864
6865 rc = moveto_scnode_check(elem, start, set, ncname, moveto_mod);
6866 if (!rc) {
6867 if (lyxp_set_scnode_contains(set, elem, LYXP_NODE_ELEM, start_idx, &idx)) {
6868 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
6869 if (idx > start_idx) {
6870 /* we will process it later in the set */
6871 goto skip_children;
6872 }
6873 } else {
Michal Vaskoe4a6d012023-05-22 14:34:52 +02006874 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, elem, LYXP_NODE_ELEM, LYXP_AXIS_DESCENDANT, NULL));
Michal Vasko49fec8e2022-05-24 10:28:33 +02006875 }
6876 } else if (rc == LY_EINVAL) {
6877 goto skip_children;
6878 }
6879
6880next_iter:
6881 /* TREE DFS NEXT ELEM */
6882 /* select element for the next run - children first */
6883 next = lysc_node_child(elem);
6884 if (next && (next->nodetype == LYS_INPUT) && (options & LYXP_SCNODE_OUTPUT)) {
6885 next = next->next;
6886 } else if (next && (next->nodetype == LYS_OUTPUT) && !(options & LYXP_SCNODE_OUTPUT)) {
6887 next = next->next;
6888 }
6889 if (!next) {
6890skip_children:
6891 /* no children, so try siblings, but only if it's not the start,
6892 * that is considered to be the root and it's siblings are not traversed */
6893 if (elem != start) {
6894 next = elem->next;
6895 } else {
6896 break;
6897 }
6898 }
6899 while (!next) {
6900 /* no siblings, go back through the parents */
6901 if (elem->parent == start) {
6902 /* we are done, no next element to process */
6903 break;
6904 }
6905 /* parent is already processed, go to its sibling */
6906 elem = elem->parent;
6907 next = elem->next;
6908 }
6909 }
6910
6911 return LY_SUCCESS;
6912}
6913
6914/**
6915 * @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 +02006916 *
6917 * @param[in] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006918 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02006919 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006920 * @param[in] options XPath options.
Michal Vasko49fec8e2022-05-24 10:28:33 +02006921 * @return LY_ERR value.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006922 */
6923static LY_ERR
Michal Vasko49fec8e2022-05-24 10:28:33 +02006924moveto_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 +02006925{
Michal Vasko49fec8e2022-05-24 10:28:33 +02006926 uint32_t i, orig_used, mod_idx;
6927 const struct lys_module *mod;
6928 const struct lysc_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006929
aPiecek8b0cc152021-05-31 16:40:31 +02006930 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006931 return LY_SUCCESS;
6932 }
6933
6934 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006935 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006936 return LY_EVALID;
6937 }
6938
Michal Vasko03ff5a72019-09-11 13:49:33 +02006939 orig_used = set->used;
6940 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006941 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
6942 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006943 continue;
6944 }
6945
6946 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01006947 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vaskoec4df482019-12-16 10:02:18 +01006948 } else {
Michal Vasko1a09b212021-05-06 13:00:10 +02006949 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006950 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006951
Michal Vasko49fec8e2022-05-24 10:28:33 +02006952 if ((set->val.scnodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.scnodes[i].type == LYXP_NODE_ROOT)) {
6953 /* traverse all top-level nodes in all the modules */
6954 mod_idx = 0;
6955 while ((mod = ly_ctx_get_module_iter(set->ctx, &mod_idx))) {
6956 /* module may not be implemented or not compiled yet */
6957 if (!mod->compiled) {
6958 continue;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006959 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006960
Michal Vasko49fec8e2022-05-24 10:28:33 +02006961 root = NULL;
6962 /* no getnext opts needed */
6963 while ((root = lys_getnext(root, NULL, mod->compiled, 0))) {
6964 LY_CHECK_RET(moveto_scnode_dfs(set, root, i, moveto_mod, ncname, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006965 }
6966 }
Michal Vasko49fec8e2022-05-24 10:28:33 +02006967
6968 } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
6969 /* add all the descendants recursively */
6970 LY_CHECK_RET(moveto_scnode_dfs(set, set->val.scnodes[i].scnode, i, moveto_mod, ncname, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006971 }
6972 }
6973
6974 return LY_SUCCESS;
6975}
6976
6977/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02006978 * @brief Move context @p set to an attribute. Result is LYXP_SET_NODE_SET.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006979 * Indirectly context position aware.
6980 *
6981 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02006982 * @param[in] mod Matching metadata module, NULL for any.
6983 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
aPiecek8b0cc152021-05-31 16:40:31 +02006984 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006985 * @return LY_ERR
6986 */
6987static LY_ERR
aPiecek8b0cc152021-05-31 16:40:31 +02006988moveto_attr(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006989{
Michal Vasko9f96a052020-03-10 09:41:45 +01006990 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006991
aPiecek8b0cc152021-05-31 16:40:31 +02006992 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006993 return LY_SUCCESS;
6994 }
6995
6996 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006997 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006998 return LY_EVALID;
6999 }
7000
Radek Krejci1deb5be2020-08-26 16:43:36 +02007001 for (uint32_t i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02007002 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007003
7004 /* only attributes of an elem (not dummy) can be in the result, skip all the rest;
7005 * our attributes are always qualified */
Michal Vasko5c4e5892019-11-14 12:31:38 +01007006 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01007007 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007008
7009 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02007010 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007011 continue;
7012 }
7013
Michal Vaskod3678892020-05-21 10:06:58 +02007014 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007015 /* match */
7016 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01007017 set->val.meta[i].meta = sub;
7018 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007019 /* pos does not change */
7020 replaced = 1;
7021 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01007022 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 +02007023 }
7024 ++i;
7025 }
7026 }
7027 }
7028
7029 if (!replaced) {
7030 /* no match */
7031 set_remove_node(set, i);
7032 }
7033 }
7034
7035 return LY_SUCCESS;
7036}
7037
7038/**
7039 * @brief Move context @p set1 to union with @p set2. @p set2 is emptied afterwards.
Michal Vasko61ac2f62020-05-25 12:39:51 +02007040 * Result is LYXP_SET_NODE_SET. Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007041 *
7042 * @param[in,out] set1 Set to use for the result.
7043 * @param[in] set2 Set that is copied to @p set1.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007044 * @return LY_ERR
7045 */
7046static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007047moveto_union(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007048{
7049 LY_ERR rc;
7050
Michal Vaskod3678892020-05-21 10:06:58 +02007051 if ((set1->type != LYXP_SET_NODE_SET) || (set2->type != LYXP_SET_NODE_SET)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01007052 LOGVAL(set1->ctx, LY_VCODE_XP_INOP_2, "union", print_set_type(set1), print_set_type(set2));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007053 return LY_EVALID;
7054 }
7055
7056 /* set2 is empty or both set1 and set2 */
Michal Vaskod3678892020-05-21 10:06:58 +02007057 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007058 return LY_SUCCESS;
7059 }
7060
Michal Vaskod3678892020-05-21 10:06:58 +02007061 if (!set1->used) {
aPiecekadc1e4f2021-10-07 11:15:12 +02007062 /* release hidden allocated data (lyxp_set.size) */
7063 lyxp_set_free_content(set1);
7064 /* direct copying of the entire structure */
Michal Vasko03ff5a72019-09-11 13:49:33 +02007065 memcpy(set1, set2, sizeof *set1);
7066 /* dynamic memory belongs to set1 now, do not free */
Michal Vaskod3678892020-05-21 10:06:58 +02007067 memset(set2, 0, sizeof *set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007068 return LY_SUCCESS;
7069 }
7070
7071 /* we assume sets are sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007072 assert(!set_sort(set1) && !set_sort(set2));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007073
7074 /* sort, remove duplicates */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007075 rc = set_sorted_merge(set1, set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007076 LY_CHECK_RET(rc);
7077
7078 /* final set must be sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007079 assert(!set_sort(set1));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007080
7081 return LY_SUCCESS;
7082}
7083
7084/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02007085 * @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 +02007086 * Context position aware.
7087 *
7088 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02007089 * @param[in] mod Matching metadata module, NULL for any.
7090 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01007091 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007092 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7093 */
7094static int
Michal Vaskocdad7122020-11-09 21:04:44 +01007095moveto_attr_alldesc(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007096{
Michal Vasko9f96a052020-03-10 09:41:45 +01007097 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007098 struct lyxp_set *set_all_desc = NULL;
7099 LY_ERR rc;
7100
aPiecek8b0cc152021-05-31 16:40:31 +02007101 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007102 return LY_SUCCESS;
7103 }
7104
7105 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01007106 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007107 return LY_EVALID;
7108 }
7109
Michal Vasko03ff5a72019-09-11 13:49:33 +02007110 /* can be optimized similarly to moveto_node_alldesc() and save considerable amount of memory,
7111 * but it likely won't be used much, so it's a waste of time */
7112 /* copy the context */
7113 set_all_desc = set_copy(set);
7114 /* get all descendant nodes (the original context nodes are removed) */
Michal Vasko49fec8e2022-05-24 10:28:33 +02007115 rc = moveto_node_alldesc_child(set_all_desc, NULL, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007116 if (rc != LY_SUCCESS) {
7117 lyxp_set_free(set_all_desc);
7118 return rc;
7119 }
7120 /* prepend the original context nodes */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007121 rc = moveto_union(set, set_all_desc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007122 if (rc != LY_SUCCESS) {
7123 lyxp_set_free(set_all_desc);
7124 return rc;
7125 }
7126 lyxp_set_free(set_all_desc);
7127
Radek Krejci1deb5be2020-08-26 16:43:36 +02007128 for (uint32_t i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02007129 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007130
7131 /* only attributes of an elem can be in the result, skip all the rest,
7132 * we have all attributes qualified in lyd tree */
7133 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01007134 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007135 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02007136 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007137 continue;
7138 }
7139
Michal Vaskod3678892020-05-21 10:06:58 +02007140 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007141 /* match */
7142 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01007143 set->val.meta[i].meta = sub;
7144 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007145 /* pos does not change */
7146 replaced = 1;
7147 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01007148 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 +02007149 }
7150 ++i;
7151 }
7152 }
7153 }
7154
7155 if (!replaced) {
7156 /* no match */
7157 set_remove_node(set, i);
7158 }
7159 }
7160
7161 return LY_SUCCESS;
7162}
7163
7164/**
Michal Vasko8abcecc2022-07-28 09:55:01 +02007165 * @brief Move context @p set1 single item to the result of a comparison.
7166 *
7167 * @param[in] set1 First set with the item to compare.
7168 * @param[in] idx1 Index of the item in @p set1.
7169 * @param[in] set2 Second set.
7170 * @param[in] op Comparison operator to process.
7171 * @param[in] switch_operands Whether to switch sets as operands; whether it is `set1 op set2` or `set2 op set1`.
7172 * @param[out] result Result of the comparison.
7173 * @return LY_ERR value.
7174 */
7175static LY_ERR
7176moveto_op_comp_item(const struct lyxp_set *set1, uint32_t idx1, struct lyxp_set *set2, const char *op,
7177 ly_bool switch_operands, ly_bool *result)
7178{
7179 struct lyxp_set tmp1 = {0};
7180 LY_ERR rc = LY_SUCCESS;
7181
7182 assert(set1->type == LYXP_SET_NODE_SET);
7183
7184 /* cast set1 */
7185 switch (set2->type) {
7186 case LYXP_SET_NUMBER:
7187 rc = set_comp_cast(&tmp1, set1, LYXP_SET_NUMBER, idx1);
7188 break;
7189 case LYXP_SET_BOOLEAN:
7190 rc = set_comp_cast(&tmp1, set1, LYXP_SET_BOOLEAN, idx1);
7191 break;
7192 default:
7193 rc = set_comp_cast(&tmp1, set1, LYXP_SET_STRING, idx1);
7194 break;
7195 }
7196 LY_CHECK_GOTO(rc, cleanup);
7197
7198 /* canonize set2 */
7199 LY_CHECK_GOTO(rc = set_comp_canonize(set2, &set1->val.nodes[idx1]), cleanup);
7200
7201 /* compare recursively and store the result */
7202 if (switch_operands) {
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007203 LY_CHECK_GOTO(rc = moveto_op_comp(set2, &tmp1, op, result), cleanup);
Michal Vasko8abcecc2022-07-28 09:55:01 +02007204 } else {
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007205 LY_CHECK_GOTO(rc = moveto_op_comp(&tmp1, set2, op, result), cleanup);
Michal Vasko8abcecc2022-07-28 09:55:01 +02007206 }
7207
7208cleanup:
7209 lyxp_set_free_content(&tmp1);
7210 return rc;
7211}
7212
7213/**
7214 * @brief Move context @p set1 to the result of a comparison. Handles '=', '!=', '<=', '<', '>=', or '>'.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007215 * Result is LYXP_SET_BOOLEAN. Indirectly context position aware.
7216 *
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007217 * @param[in] set1 Set acting as the first operand for @p op.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007218 * @param[in] set2 Set acting as the second operand for @p op.
7219 * @param[in] op Comparison operator to process.
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007220 * @param[out] result Result of the comparison.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007221 * @return LY_ERR
7222 */
7223static LY_ERR
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007224moveto_op_comp(struct lyxp_set *set1, struct lyxp_set *set2, const char *op, ly_bool *result)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007225{
7226 /*
7227 * NODE SET + NODE SET = NODE SET + STRING /(1 NODE SET) 2 STRING
7228 * NODE SET + STRING = STRING + STRING /1 STRING (2 STRING)
7229 * NODE SET + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
7230 * NODE SET + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
7231 * STRING + NODE SET = STRING + STRING /(1 STRING) 2 STRING
7232 * NUMBER + NODE SET = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
7233 * BOOLEAN + NODE SET = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
7234 *
7235 * '=' or '!='
7236 * BOOLEAN + BOOLEAN
7237 * BOOLEAN + STRING = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
7238 * BOOLEAN + NUMBER = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
7239 * STRING + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
7240 * NUMBER + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
7241 * NUMBER + NUMBER
7242 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
7243 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
7244 * STRING + STRING
7245 *
7246 * '<=', '<', '>=', '>'
7247 * NUMBER + NUMBER
7248 * BOOLEAN + BOOLEAN = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
7249 * BOOLEAN + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
7250 * BOOLEAN + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
7251 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
7252 * STRING + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
7253 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
7254 * NUMBER + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
7255 * STRING + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
7256 */
Michal Vasko8abcecc2022-07-28 09:55:01 +02007257 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007258 LY_ERR rc;
7259
Michal Vasko03ff5a72019-09-11 13:49:33 +02007260 /* iterative evaluation with node-sets */
7261 if ((set1->type == LYXP_SET_NODE_SET) || (set2->type == LYXP_SET_NODE_SET)) {
7262 if (set1->type == LYXP_SET_NODE_SET) {
7263 for (i = 0; i < set1->used; ++i) {
Michal Vasko8abcecc2022-07-28 09:55:01 +02007264 /* evaluate for the single item */
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007265 LY_CHECK_RET(moveto_op_comp_item(set1, i, set2, op, 0, result));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007266
7267 /* lazy evaluation until true */
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007268 if (*result) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007269 return LY_SUCCESS;
7270 }
7271 }
7272 } else {
7273 for (i = 0; i < set2->used; ++i) {
Michal Vasko8abcecc2022-07-28 09:55:01 +02007274 /* evaluate for the single item */
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007275 LY_CHECK_RET(moveto_op_comp_item(set2, i, set1, op, 1, result));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007276
7277 /* lazy evaluation until true */
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007278 if (*result) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007279 return LY_SUCCESS;
7280 }
7281 }
7282 }
7283
Michal Vasko8abcecc2022-07-28 09:55:01 +02007284 /* false for all the nodes */
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007285 *result = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007286 return LY_SUCCESS;
7287 }
7288
7289 /* first convert properly */
7290 if ((op[0] == '=') || (op[0] == '!')) {
7291 if ((set1->type == LYXP_SET_BOOLEAN) || (set2->type == LYXP_SET_BOOLEAN)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007292 lyxp_set_cast(set1, LYXP_SET_BOOLEAN);
7293 lyxp_set_cast(set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007294 } else if ((set1->type == LYXP_SET_NUMBER) || (set2->type == LYXP_SET_NUMBER)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007295 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007296 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007297 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007298 LY_CHECK_RET(rc);
7299 } /* else we have 2 strings */
7300 } else {
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
7307 assert(set1->type == set2->type);
7308
7309 /* compute result */
7310 if (op[0] == '=') {
7311 if (set1->type == LYXP_SET_BOOLEAN) {
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007312 *result = (set1->val.bln == set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007313 } else if (set1->type == LYXP_SET_NUMBER) {
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007314 *result = (set1->val.num == set2->val.num);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007315 } else {
7316 assert(set1->type == LYXP_SET_STRING);
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007317 *result = strcmp(set1->val.str, set2->val.str) ? 0 : 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007318 }
7319 } else if (op[0] == '!') {
7320 if (set1->type == LYXP_SET_BOOLEAN) {
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007321 *result = (set1->val.bln != set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007322 } else if (set1->type == LYXP_SET_NUMBER) {
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007323 *result = (set1->val.num != set2->val.num);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007324 } else {
7325 assert(set1->type == LYXP_SET_STRING);
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007326 *result = strcmp(set1->val.str, set2->val.str) ? 1 : 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007327 }
7328 } else {
7329 assert(set1->type == LYXP_SET_NUMBER);
7330 if (op[0] == '<') {
7331 if (op[1] == '=') {
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007332 *result = (set1->val.num <= set2->val.num);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007333 } else {
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007334 *result = (set1->val.num < set2->val.num);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007335 }
7336 } else {
7337 if (op[1] == '=') {
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007338 *result = (set1->val.num >= set2->val.num);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007339 } else {
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007340 *result = (set1->val.num > set2->val.num);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007341 }
7342 }
7343 }
7344
Michal Vasko03ff5a72019-09-11 13:49:33 +02007345 return LY_SUCCESS;
7346}
7347
7348/**
7349 * @brief Move context @p set to the result of a basic operation. Handles '+', '-', unary '-', '*', 'div',
7350 * or 'mod'. Result is LYXP_SET_NUMBER. Indirectly context position aware.
7351 *
7352 * @param[in,out] set1 Set to use for the result.
7353 * @param[in] set2 Set acting as the second operand for @p op.
7354 * @param[in] op Operator to process.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007355 * @return LY_ERR
7356 */
7357static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007358moveto_op_math(struct lyxp_set *set1, struct lyxp_set *set2, const char *op)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007359{
7360 LY_ERR rc;
7361
7362 /* unary '-' */
7363 if (!set2 && (op[0] == '-')) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007364 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007365 LY_CHECK_RET(rc);
7366 set1->val.num *= -1;
7367 lyxp_set_free(set2);
7368 return LY_SUCCESS;
7369 }
7370
7371 assert(set1 && set2);
7372
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007373 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007374 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007375 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007376 LY_CHECK_RET(rc);
7377
7378 switch (op[0]) {
7379 /* '+' */
7380 case '+':
7381 set1->val.num += set2->val.num;
7382 break;
7383
7384 /* '-' */
7385 case '-':
7386 set1->val.num -= set2->val.num;
7387 break;
7388
7389 /* '*' */
7390 case '*':
7391 set1->val.num *= set2->val.num;
7392 break;
7393
7394 /* 'div' */
7395 case 'd':
7396 set1->val.num /= set2->val.num;
7397 break;
7398
7399 /* 'mod' */
7400 case 'm':
7401 set1->val.num = ((long long)set1->val.num) % ((long long)set2->val.num);
7402 break;
7403
7404 default:
7405 LOGINT_RET(set1->ctx);
7406 }
7407
7408 return LY_SUCCESS;
7409}
7410
Michal Vasko03ff5a72019-09-11 13:49:33 +02007411/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02007412 * @brief Evaluate Predicate. Logs directly on error.
7413 *
Michal Vaskod3678892020-05-21 10:06:58 +02007414 * [9] Predicate ::= '[' Expr ']'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007415 *
7416 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007417 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02007418 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007419 * @param[in] options XPath options.
Michal Vasko49fec8e2022-05-24 10:28:33 +02007420 * @param[in] axis Axis to search on.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007421 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7422 */
7423static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02007424eval_predicate(const struct lyxp_expr *exp, uint32_t *tok_idx, struct lyxp_set *set, uint32_t options, enum lyxp_axis axis)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007425{
7426 LY_ERR rc;
Michal Vaskodd528af2022-08-08 14:35:07 +02007427 uint32_t i, orig_exp, orig_pos, orig_size;
Michal Vasko5c4e5892019-11-14 12:31:38 +01007428 int32_t pred_in_ctx;
Michal Vasko88a9e802022-05-24 10:50:28 +02007429 ly_bool reverse_axis = 0;
aPiecekfff4dca2021-10-07 10:59:53 +02007430 struct lyxp_set set2 = {0};
Michal Vasko03ff5a72019-09-11 13:49:33 +02007431
7432 /* '[' */
aPiecek8b0cc152021-05-31 16:40:31 +02007433 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02007434 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007435 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007436
aPiecek8b0cc152021-05-31 16:40:31 +02007437 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007438only_parse:
aPiecek8b0cc152021-05-31 16:40:31 +02007439 rc = eval_expr_select(exp, tok_idx, 0, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007440 LY_CHECK_RET(rc);
7441 } else if (set->type == LYXP_SET_NODE_SET) {
7442 /* 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 +01007443 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007444
7445 /* empty set, nothing to evaluate */
7446 if (!set->used) {
7447 goto only_parse;
7448 }
7449
Michal Vasko49fec8e2022-05-24 10:28:33 +02007450 /* decide forward or reverse axis */
7451 switch (axis) {
7452 case LYXP_AXIS_ANCESTOR:
7453 case LYXP_AXIS_ANCESTOR_OR_SELF:
7454 case LYXP_AXIS_PRECEDING:
7455 case LYXP_AXIS_PRECEDING_SIBLING:
7456 reverse_axis = 1;
7457 break;
7458 case LYXP_AXIS_DESCENDANT:
7459 case LYXP_AXIS_DESCENDANT_OR_SELF:
7460 case LYXP_AXIS_FOLLOWING:
7461 case LYXP_AXIS_FOLLOWING_SIBLING:
7462 case LYXP_AXIS_PARENT:
7463 case LYXP_AXIS_CHILD:
7464 case LYXP_AXIS_SELF:
7465 case LYXP_AXIS_ATTRIBUTE:
7466 reverse_axis = 0;
7467 break;
7468 }
7469
Michal Vasko004d3152020-06-11 19:59:22 +02007470 orig_exp = *tok_idx;
Michal Vasko49fec8e2022-05-24 10:28:33 +02007471 orig_pos = reverse_axis ? set->used + 1 : 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007472 orig_size = set->used;
Michal Vasko39dbf352020-05-21 10:08:59 +02007473 for (i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007474 set_init(&set2, set);
7475 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 +02007476
7477 /* remember the node context position for position() and context size for last() */
7478 orig_pos += reverse_axis ? -1 : 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007479
7480 set2.ctx_pos = orig_pos;
7481 set2.ctx_size = orig_size;
Michal Vasko004d3152020-06-11 19:59:22 +02007482 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007483
Michal Vasko004d3152020-06-11 19:59:22 +02007484 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007485 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02007486 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007487 return rc;
7488 }
7489
Michal Vasko49fec8e2022-05-24 10:28:33 +02007490 /* number is a proximity position */
Michal Vasko03ff5a72019-09-11 13:49:33 +02007491 if (set2.type == LYXP_SET_NUMBER) {
7492 if ((long long)set2.val.num == orig_pos) {
7493 set2.val.num = 1;
7494 } else {
7495 set2.val.num = 0;
7496 }
7497 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007498 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007499
7500 /* predicate satisfied or not? */
Michal Vasko004d3152020-06-11 19:59:22 +02007501 if (!set2.val.bln) {
Michal Vasko2caefc12019-11-14 16:07:56 +01007502 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007503 }
7504 }
Michal Vasko2caefc12019-11-14 16:07:56 +01007505 set_remove_nodes_none(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007506
7507 } else if (set->type == LYXP_SET_SCNODE_SET) {
7508 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01007509 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007510 /* there is a currently-valid node */
7511 break;
7512 }
7513 }
7514 /* empty set, nothing to evaluate */
7515 if (i == set->used) {
7516 goto only_parse;
7517 }
7518
Michal Vasko004d3152020-06-11 19:59:22 +02007519 orig_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007520
Michal Vasko03ff5a72019-09-11 13:49:33 +02007521 /* set special in_ctx to all the valid snodes */
7522 pred_in_ctx = set_scnode_new_in_ctx(set);
7523
7524 /* use the valid snodes one-by-one */
7525 for (i = 0; i < set->used; ++i) {
7526 if (set->val.scnodes[i].in_ctx != pred_in_ctx) {
7527 continue;
7528 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01007529 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007530
Michal Vasko004d3152020-06-11 19:59:22 +02007531 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007532
Michal Vasko004d3152020-06-11 19:59:22 +02007533 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007534 LY_CHECK_RET(rc);
7535
7536 set->val.scnodes[i].in_ctx = pred_in_ctx;
7537 }
7538
7539 /* restore the state as it was before the predicate */
7540 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01007541 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007542 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007543 } else if (set->val.scnodes[i].in_ctx == pred_in_ctx) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01007544 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007545 }
7546 }
7547
7548 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02007549 set2.type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007550 set_fill_set(&set2, set);
7551
Michal Vasko004d3152020-06-11 19:59:22 +02007552 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007553 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02007554 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007555 return rc;
7556 }
7557
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007558 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02007559 if (!set2.val.bln) {
Michal Vaskod3678892020-05-21 10:06:58 +02007560 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007561 }
Michal Vaskod3678892020-05-21 10:06:58 +02007562 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007563 }
7564
7565 /* ']' */
Michal Vasko004d3152020-06-11 19:59:22 +02007566 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_BRACK2);
aPiecek8b0cc152021-05-31 16:40:31 +02007567 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02007568 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007569 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007570
7571 return LY_SUCCESS;
7572}
7573
7574/**
Michal Vaskod3678892020-05-21 10:06:58 +02007575 * @brief Evaluate Literal. Logs directly on error.
7576 *
7577 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007578 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02007579 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7580 */
7581static void
Michal Vaskodd528af2022-08-08 14:35:07 +02007582eval_literal(const struct lyxp_expr *exp, uint32_t *tok_idx, struct lyxp_set *set)
Michal Vaskod3678892020-05-21 10:06:58 +02007583{
7584 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007585 if (exp->tok_len[*tok_idx] == 2) {
Michal Vaskod3678892020-05-21 10:06:58 +02007586 set_fill_string(set, "", 0);
7587 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007588 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 +02007589 }
7590 }
7591 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02007592 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007593 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007594}
7595
7596/**
Michal Vasko3d969ff2022-07-29 15:02:08 +02007597 * @brief Check that a nametest in a predicate matches a key node.
7598 *
7599 * @param[in] nametest Nametest to check.
7600 * @param[in] len Length of @p nametest.
7601 * @param[in] ctx_scnode Found schema node as the context for the predicate.
7602 * @param[in] set Context set.
7603 * @param[in] key Expected key node.
7604 * @return LY_SUCCESS on success,
7605 * @return LY_ENOT if a predicate could not be compiled.
7606 * @return LY_ERR on any error.
7607 */
7608static LY_ERR
7609eval_name_test_try_compile_predicate_key(const char *nametest, uint32_t len, const struct lysc_node *ctx_scnode,
7610 const struct lyxp_set *set, const struct lysc_node *key)
7611{
7612 const struct lys_module *mod;
7613
7614 /* prefix (module) */
7615 LY_CHECK_RET(moveto_resolve_model(&nametest, &len, set, ctx_scnode, &mod));
7616 if (mod != key->module) {
7617 return LY_ENOT;
7618 }
7619
7620 /* node name */
7621 if (ly_strncmp(key->name, nametest, len)) {
7622 return LY_ENOT;
7623 }
7624
7625 return LY_SUCCESS;
7626}
7627
7628/**
7629 * @brief Append a simple predicate for the node.
7630 *
7631 * @param[in] exp Full parsed XPath expression.
7632 * @param[in] tok_idx Predicate start index in @p exp.
7633 * @param[in] end_tok_idx Predicate end index in @p exp.
7634 * @param[in] ctx_scnode Found schema node as the context for the predicate.
7635 * @param[in] set Context set.
7636 * @param[in] pred_node Node with the value referenced in the predicate.
7637 * @param[in,out] pred Predicate to append to.
7638 * @param[in,out] pred_len Length of @p pred, is updated.
7639 * @return LY_SUCCESS on success,
7640 * @return LY_ENOT if a predicate could not be compiled.
7641 * @return LY_ERR on any error.
7642 */
7643static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02007644eval_name_test_try_compile_predicate_append(const struct lyxp_expr *exp, uint32_t tok_idx, uint32_t end_tok_idx,
Michal Vasko3d969ff2022-07-29 15:02:08 +02007645 const struct lysc_node *ctx_scnode, const struct lyxp_set *set, const struct lysc_node *pred_node, char **pred,
7646 uint32_t *pred_len)
7647{
7648 LY_ERR rc = LY_SUCCESS;
7649 uint32_t i;
7650 const struct lyd_node *siblings;
7651 struct lyd_node *ctx_node;
Michal Vasko5fb92a22022-08-02 09:21:37 +02007652 const struct lysc_node *sparent, *cur_scnode;
Michal Vasko3d969ff2022-07-29 15:02:08 +02007653 struct lyxp_expr *val_exp = NULL;
7654 struct lyxp_set set2 = {0};
7655 char quot;
7656
7657 /* duplicate the value expression */
7658 LY_CHECK_GOTO(rc = lyxp_expr_dup(set->ctx, exp, tok_idx, end_tok_idx, &val_exp), cleanup);
7659
7660 /* get its atoms */
Michal Vasko5fb92a22022-08-02 09:21:37 +02007661 cur_scnode = set->cur_node ? set->cur_node->schema : NULL;
7662 LY_CHECK_GOTO(rc = lyxp_atomize(set->ctx, val_exp, set->cur_mod, set->format, set->prefix_data, cur_scnode,
7663 ctx_scnode, &set2, LYXP_SCNODE), cleanup);
Michal Vasko3d969ff2022-07-29 15:02:08 +02007664
7665 /* check whether we can compile a single predicate (evaluation result value is always the same) */
7666 for (i = 0; i < set2.used; ++i) {
7667 if ((set2.val.scnodes[i].type != LYXP_NODE_ELEM) || (set2.val.scnodes[i].in_ctx < LYXP_SET_SCNODE_ATOM_NODE)) {
7668 /* skip root and context node */
7669 continue;
7670 }
7671
Michal Vasko5fb92a22022-08-02 09:21:37 +02007672 /* 1) context node descendants are traversed - do best-effort detection of the value dependency on the
7673 * context node instance */
7674 if ((set2.val.scnodes[i].axis == LYXP_AXIS_CHILD) && (set2.val.scnodes[i].scnode->parent == ctx_scnode)) {
7675 /* 1.1) context node child was accessed on the child axis, certain dependency */
Michal Vasko3d969ff2022-07-29 15:02:08 +02007676 rc = LY_ENOT;
7677 goto cleanup;
7678 }
Michal Vasko5fb92a22022-08-02 09:21:37 +02007679 if ((set2.val.scnodes[i].axis == LYXP_AXIS_DESCENDANT) || (set2.val.scnodes[i].axis == LYXP_AXIS_DESCENDANT_OR_SELF)) {
7680 for (sparent = set2.val.scnodes[i].scnode->parent; sparent && (sparent != ctx_scnode); sparent = sparent->parent) {}
7681 if (sparent) {
7682 /* 1.2) context node descendant was accessed on the descendant axis, probable dependency */
7683 rc = LY_ENOT;
7684 goto cleanup;
7685 }
7686 }
Michal Vasko3d969ff2022-07-29 15:02:08 +02007687
Michal Vasko5fb92a22022-08-02 09:21:37 +02007688 /* 2) multi-instance nodes (list or leaf-list) are traversed - all the instances need to be considered,
7689 * but the current node can be safely ignored, it is always the same data instance */
7690 if ((set2.val.scnodes[i].scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)) && (cur_scnode != set2.val.scnodes[i].scnode)) {
Michal Vasko3d969ff2022-07-29 15:02:08 +02007691 rc = LY_ENOT;
7692 goto cleanup;
7693 }
Michal Vasko3d969ff2022-07-29 15:02:08 +02007694 }
7695
7696 /* get any data instance of the context node, we checked it makes no difference */
7697 siblings = set->val.nodes[0].node ? lyd_child(set->val.nodes[0].node) : set->tree;
7698 LY_CHECK_GOTO(rc = lyd_find_sibling_schema(siblings, ctx_scnode, &ctx_node), cleanup);
7699
7700 /* evaluate the value subexpression with the root context node */
7701 lyxp_set_free_content(&set2);
7702 LY_CHECK_GOTO(rc = lyxp_eval(set->ctx, val_exp, set->cur_mod, set->format, set->prefix_data, set->cur_node,
7703 ctx_node, set->tree, NULL, &set2, 0), cleanup);
7704
7705 /* cast it into a string */
7706 LY_CHECK_GOTO(rc = lyxp_set_cast(&set2, LYXP_SET_STRING), cleanup);
7707
7708 /* append the JSON predicate */
7709 *pred = ly_realloc(*pred, *pred_len + 1 + strlen(pred_node->name) + 2 + strlen(set2.val.str) + 3);
7710 LY_CHECK_ERR_GOTO(!*pred, LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7711 quot = strchr(set2.val.str, '\'') ? '\"' : '\'';
7712 *pred_len += sprintf(*pred + *pred_len, "[%s=%c%s%c]", pred_node->name, quot, set2.val.str, quot);
7713
7714cleanup:
7715 lyxp_expr_free(set->ctx, val_exp);
7716 lyxp_set_free_content(&set2);
7717 return rc;
7718}
7719
7720/**
Michal Vasko004d3152020-06-11 19:59:22 +02007721 * @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 +02007722 *
Michal Vasko004d3152020-06-11 19:59:22 +02007723 * @param[in] exp Full parsed XPath expression.
7724 * @param[in,out] tok_idx Index in @p exp at the beginning of the predicate, is updated on success.
Michal Vasko3d969ff2022-07-29 15:02:08 +02007725 * @param[in] ctx_scnode Found schema node as the context for the predicate.
7726 * @param[in] set Context set.
Michal Vasko004d3152020-06-11 19:59:22 +02007727 * @param[out] predicates Parsed predicates.
Michal Vasko004d3152020-06-11 19:59:22 +02007728 * @return LY_SUCCESS on success,
Michal Vasko3d969ff2022-07-29 15:02:08 +02007729 * @return LY_ENOT if a predicate could not be compiled.
Michal Vasko004d3152020-06-11 19:59:22 +02007730 * @return LY_ERR on any error.
Michal Vaskod3678892020-05-21 10:06:58 +02007731 */
7732static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02007733eval_name_test_try_compile_predicates(const struct lyxp_expr *exp, uint32_t *tok_idx, const struct lysc_node *ctx_scnode,
Michal Vasko90189962023-02-28 12:10:34 +01007734 const struct lyxp_set *set, struct ly_path_predicate **predicates)
Michal Vaskod3678892020-05-21 10:06:58 +02007735{
Michal Vasko3d969ff2022-07-29 15:02:08 +02007736 LY_ERR rc = LY_SUCCESS;
Michal Vaskod4a6d042022-12-08 08:34:29 +01007737 uint32_t e_idx, val_start_idx, pred_idx = 0, temp_lo = 0, pred_len = 0, nested_pred;
Michal Vaskod3678892020-05-21 10:06:58 +02007738 const struct lysc_node *key;
Michal Vasko3d969ff2022-07-29 15:02:08 +02007739 char *pred = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02007740 struct lyxp_expr *exp2 = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02007741
Michal Vasko3d969ff2022-07-29 15:02:08 +02007742 assert(ctx_scnode->nodetype & (LYS_LIST | LYS_LEAFLIST));
Michal Vaskod3678892020-05-21 10:06:58 +02007743
Michal Vasko3d969ff2022-07-29 15:02:08 +02007744 /* turn logging off */
Michal Vaskod4a6d042022-12-08 08:34:29 +01007745 ly_temp_log_options(&temp_lo);
Michal Vasko3d969ff2022-07-29 15:02:08 +02007746
7747 if (ctx_scnode->nodetype == LYS_LIST) {
7748 /* check for predicates "[key1=...][key2=...]..." */
7749
Michal Vasko004d3152020-06-11 19:59:22 +02007750 /* get key count */
Michal Vasko3d969ff2022-07-29 15:02:08 +02007751 if (ctx_scnode->flags & LYS_KEYLESS) {
7752 rc = LY_ENOT;
7753 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +02007754 }
Michal Vaskod3678892020-05-21 10:06:58 +02007755
Michal Vasko004d3152020-06-11 19:59:22 +02007756 /* learn where the predicates end */
7757 e_idx = *tok_idx;
Michal Vasko3d969ff2022-07-29 15:02:08 +02007758 for (key = lysc_node_child(ctx_scnode); key && (key->flags & LYS_KEY); key = key->next) {
Michal Vasko004d3152020-06-11 19:59:22 +02007759 /* '[' */
7760 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
Michal Vasko3d969ff2022-07-29 15:02:08 +02007761 rc = LY_ENOT;
7762 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +02007763 }
7764 ++e_idx;
7765
Michal Vasko3354d272021-04-06 09:40:06 +02007766 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_NAMETEST)) {
Michal Vasko3d969ff2022-07-29 15:02:08 +02007767 /* not a key */
7768 rc = LY_ENOT;
7769 goto cleanup;
Michal Vasko3354d272021-04-06 09:40:06 +02007770 }
7771
Michal Vasko3d969ff2022-07-29 15:02:08 +02007772 /* check key */
7773 LY_CHECK_GOTO(rc = eval_name_test_try_compile_predicate_key(exp->expr + exp->tok_pos[e_idx],
7774 exp->tok_len[e_idx], ctx_scnode, set, key), cleanup);
7775
7776 ++e_idx;
7777
7778 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_OPER_EQUAL)) {
7779 /* not '=' */
7780 rc = LY_ENOT;
7781 goto cleanup;
7782 }
7783 ++e_idx;
7784
7785 /* value start */
7786 val_start_idx = e_idx;
7787
Michal Vasko004d3152020-06-11 19:59:22 +02007788 /* ']' */
Michal Vasko3ef7e252022-11-16 08:29:49 +01007789 nested_pred = 1;
7790 do {
7791 ++e_idx;
7792
7793 if ((nested_pred == 1) && !lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_OPER_LOG)) {
Michal Vaskob8ee3562022-08-02 10:43:17 +02007794 /* higher priority than '=' */
7795 rc = LY_ENOT;
7796 goto cleanup;
Michal Vasko3ef7e252022-11-16 08:29:49 +01007797 } else if (!lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
7798 /* nested predicate */
7799 ++nested_pred;
7800 } else if (!lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
7801 /* predicate end */
7802 --nested_pred;
Michal Vaskob8ee3562022-08-02 10:43:17 +02007803 }
Michal Vasko3ef7e252022-11-16 08:29:49 +01007804 } while (nested_pred);
Michal Vasko004d3152020-06-11 19:59:22 +02007805
Michal Vasko3d969ff2022-07-29 15:02:08 +02007806 /* try to evaluate the value */
7807 LY_CHECK_GOTO(rc = eval_name_test_try_compile_predicate_append(exp, val_start_idx, e_idx - 1, ctx_scnode,
7808 set, key, &pred, &pred_len), cleanup);
7809
7810 ++e_idx;
Michal Vasko004d3152020-06-11 19:59:22 +02007811 }
Michal Vasko004d3152020-06-11 19:59:22 +02007812 } else {
Michal Vasko3d969ff2022-07-29 15:02:08 +02007813 /* check for predicate "[.=...]" */
7814
Michal Vasko004d3152020-06-11 19:59:22 +02007815 /* learn just where this single predicate ends */
7816 e_idx = *tok_idx;
7817
Michal Vaskod3678892020-05-21 10:06:58 +02007818 /* '[' */
Michal Vasko004d3152020-06-11 19:59:22 +02007819 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
Michal Vasko3d969ff2022-07-29 15:02:08 +02007820 rc = LY_ENOT;
7821 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +02007822 }
7823 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02007824
Michal Vasko3354d272021-04-06 09:40:06 +02007825 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_DOT)) {
Michal Vasko3d969ff2022-07-29 15:02:08 +02007826 /* not the node value */
7827 rc = LY_ENOT;
7828 goto cleanup;
Michal Vasko3354d272021-04-06 09:40:06 +02007829 }
Michal Vasko3d969ff2022-07-29 15:02:08 +02007830 ++e_idx;
7831
7832 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_OPER_EQUAL)) {
7833 /* not '=' */
7834 rc = LY_ENOT;
7835 goto cleanup;
7836 }
7837 ++e_idx;
7838
7839 /* value start */
7840 val_start_idx = e_idx;
Michal Vasko3354d272021-04-06 09:40:06 +02007841
Michal Vaskod3678892020-05-21 10:06:58 +02007842 /* ']' */
Michal Vasko31f19632022-11-16 09:38:40 +01007843 nested_pred = 1;
7844 do {
7845 ++e_idx;
7846
7847 if ((nested_pred == 1) && !lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_OPER_LOG)) {
Michal Vaskob8ee3562022-08-02 10:43:17 +02007848 /* higher priority than '=' */
7849 rc = LY_ENOT;
7850 goto cleanup;
Michal Vasko31f19632022-11-16 09:38:40 +01007851 } else if (!lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
7852 /* nested predicate */
7853 ++nested_pred;
7854 } else if (!lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
7855 /* predicate end */
7856 --nested_pred;
Michal Vaskob8ee3562022-08-02 10:43:17 +02007857 }
Michal Vasko31f19632022-11-16 09:38:40 +01007858 } while (nested_pred);
Michal Vasko3d969ff2022-07-29 15:02:08 +02007859
7860 /* try to evaluate the value */
7861 LY_CHECK_GOTO(rc = eval_name_test_try_compile_predicate_append(exp, val_start_idx, e_idx - 1, ctx_scnode, set,
7862 ctx_scnode, &pred, &pred_len), cleanup);
7863
Michal Vasko004d3152020-06-11 19:59:22 +02007864 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02007865 }
7866
Michal Vasko004d3152020-06-11 19:59:22 +02007867 /* parse the predicate(s) */
Michal Vasko3d969ff2022-07-29 15:02:08 +02007868 LY_CHECK_GOTO(rc = ly_path_parse_predicate(set->ctx, ctx_scnode, pred, pred_len, LY_PATH_PREFIX_OPTIONAL,
7869 LY_PATH_PRED_SIMPLE, &exp2), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02007870
7871 /* compile */
Michal Vasko3d969ff2022-07-29 15:02:08 +02007872 rc = ly_path_compile_predicate(set->ctx, set->cur_node ? set->cur_node->schema : NULL, set->cur_mod, ctx_scnode, exp2,
Michal Vasko90189962023-02-28 12:10:34 +01007873 &pred_idx, LY_VALUE_JSON, NULL, predicates);
Michal Vasko3d969ff2022-07-29 15:02:08 +02007874 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02007875
7876 /* success, the predicate must include all the needed information for hash-based search */
7877 *tok_idx = e_idx;
7878
7879cleanup:
Michal Vaskod4a6d042022-12-08 08:34:29 +01007880 ly_temp_log_options(NULL);
Michal Vasko3d969ff2022-07-29 15:02:08 +02007881 lyxp_expr_free(set->ctx, exp2);
7882 free(pred);
7883 return rc;
Michal Vaskod3678892020-05-21 10:06:58 +02007884}
7885
7886/**
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007887 * @brief Search for/check the next schema node that could be the only matching schema node meaning the
7888 * data node(s) could be found using a single hash-based search.
7889 *
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007890 * @param[in] ctx libyang context.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007891 * @param[in] node Next context node to check.
7892 * @param[in] name Expected node name.
7893 * @param[in] name_len Length of @p name.
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007894 * @param[in] moveto_mod Expected node module, can be NULL for JSON format with no prefix.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007895 * @param[in] root_type XPath root type.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007896 * @param[in] format Prefix format.
7897 * @param[in,out] found Previously found node, is updated.
7898 * @return LY_SUCCESS on success,
7899 * @return LY_ENOT if the whole check failed and hashes cannot be used.
7900 */
7901static LY_ERR
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007902eval_name_test_with_predicate_get_scnode(const struct ly_ctx *ctx, const struct lyd_node *node, const char *name,
Michal Vaskodd528af2022-08-08 14:35:07 +02007903 uint32_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 +02007904 const struct lysc_node **found)
7905{
7906 const struct lysc_node *scnode;
7907 const struct lys_module *mod;
7908 uint32_t idx = 0;
7909
Radek Krejci8df109d2021-04-23 12:19:08 +02007910 assert((format == LY_VALUE_JSON) || moveto_mod);
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007911
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007912continue_search:
Michal Vasko7d1d0912020-10-16 08:38:30 +02007913 scnode = NULL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007914 if (!node) {
Radek Krejci8df109d2021-04-23 12:19:08 +02007915 if ((format == LY_VALUE_JSON) && !moveto_mod) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007916 /* search all modules for a single match */
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007917 while ((mod = ly_ctx_get_module_iter(ctx, &idx))) {
Michal Vasko35a3b1d2021-07-14 09:34:16 +02007918 if (!mod->implemented) {
7919 continue;
7920 }
7921
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007922 scnode = lys_find_child(NULL, mod, name, name_len, 0, 0);
7923 if (scnode) {
7924 /* we have found a match */
7925 break;
7926 }
7927 }
7928
Michal Vasko7d1d0912020-10-16 08:38:30 +02007929 if (!scnode) {
7930 /* all modules searched */
7931 idx = 0;
7932 }
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007933 } else {
7934 /* search in top-level */
7935 scnode = lys_find_child(NULL, moveto_mod, name, name_len, 0, 0);
7936 }
7937 } else if (!*found || (lysc_data_parent(*found) != node->schema)) {
Radek Krejci8df109d2021-04-23 12:19:08 +02007938 if ((format == LY_VALUE_JSON) && !moveto_mod) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007939 /* we must adjust the module to inherit the one from the context node */
7940 moveto_mod = node->schema->module;
7941 }
7942
7943 /* search in children, do not repeat the same search */
7944 scnode = lys_find_child(node->schema, moveto_mod, name, name_len, 0, 0);
Michal Vasko7d1d0912020-10-16 08:38:30 +02007945 } /* else skip redundant search */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007946
7947 /* additional context check */
7948 if (scnode && (root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) {
7949 scnode = NULL;
7950 }
7951
7952 if (scnode) {
7953 if (*found) {
7954 /* we found a schema node with the same name but at different level, give up, too complicated
7955 * (more hash-based searches would be required, not supported) */
7956 return LY_ENOT;
7957 } else {
7958 /* remember the found schema node and continue to make sure it can be used */
7959 *found = scnode;
7960 }
7961 }
7962
7963 if (idx) {
7964 /* continue searching all the following models */
7965 goto continue_search;
7966 }
7967
7968 return LY_SUCCESS;
7969}
7970
7971/**
Michal Vasko4ad69e72021-10-26 16:25:55 +02007972 * @brief Generate message when no matching schema nodes were found for a path segment.
7973 *
7974 * @param[in] set XPath set.
7975 * @param[in] scparent Previous schema parent in the context, if only one.
7976 * @param[in] ncname XPath NCName being evaluated.
7977 * @param[in] ncname_len Length of @p ncname.
7978 * @param[in] expr Whole XPath expression.
7979 * @param[in] options XPath options.
7980 */
7981static void
7982eval_name_test_scnode_no_match_msg(struct lyxp_set *set, const struct lyxp_set_scnode *scparent, const char *ncname,
Michal Vaskodd528af2022-08-08 14:35:07 +02007983 uint32_t ncname_len, const char *expr, uint32_t options)
Michal Vasko4ad69e72021-10-26 16:25:55 +02007984{
7985 const char *format;
7986 char *path = NULL, *ppath = NULL;
7987
7988 path = lysc_path(set->cur_scnode, LYSC_PATH_LOG, NULL, 0);
7989 if (scparent) {
7990 /* generate path for the parent */
7991 if (scparent->type == LYXP_NODE_ELEM) {
7992 ppath = lysc_path(scparent->scnode, LYSC_PATH_LOG, NULL, 0);
7993 } else if (scparent->type == LYXP_NODE_ROOT) {
7994 ppath = strdup("<root>");
7995 } else if (scparent->type == LYXP_NODE_ROOT_CONFIG) {
7996 ppath = strdup("<config-root>");
7997 }
7998 }
7999 if (ppath) {
8000 format = "Schema node \"%.*s\" for parent \"%s\" not found; in expr \"%.*s\" with context node \"%s\".";
8001 if (options & LYXP_SCNODE_ERROR) {
8002 LOGERR(set->ctx, LY_EVALID, format, ncname_len, ncname, ppath, (ncname - expr) + ncname_len, expr, path);
8003 } else {
8004 LOGWRN(set->ctx, format, ncname_len, ncname, ppath, (ncname - expr) + ncname_len, expr, path);
8005 }
8006 } else {
8007 format = "Schema node \"%.*s\" not found; in expr \"%.*s\" with context node \"%s\".";
8008 if (options & LYXP_SCNODE_ERROR) {
8009 LOGERR(set->ctx, LY_EVALID, format, ncname_len, ncname, (ncname - expr) + ncname_len, expr, path);
8010 } else {
8011 LOGWRN(set->ctx, format, ncname_len, ncname, (ncname - expr) + ncname_len, expr, path);
8012 }
8013 }
8014 free(path);
8015 free(ppath);
8016}
8017
8018/**
Michal Vaskod3678892020-05-21 10:06:58 +02008019 * @brief Evaluate NameTest and any following Predicates. Logs directly on error.
8020 *
8021 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
8022 * [6] NodeTest ::= NameTest | NodeType '(' ')'
8023 * [7] NameTest ::= '*' | NCName ':' '*' | QName
8024 *
8025 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008026 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko49fec8e2022-05-24 10:28:33 +02008027 * @param[in] axis What axis to search on.
Michal Vaskod3678892020-05-21 10:06:58 +02008028 * @param[in] all_desc Whether to search all the descendants or children only.
aPiecek8b0cc152021-05-31 16:40:31 +02008029 * @param[in,out] set Context and result set.
Michal Vaskod3678892020-05-21 10:06:58 +02008030 * @param[in] options XPath options.
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008031 * @return LY_ERR (LY_EINCOMPLETE on unresolved when, LY_ENOT for not found schema node)
Michal Vaskod3678892020-05-21 10:06:58 +02008032 */
8033static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02008034eval_name_test_with_predicate(const struct lyxp_expr *exp, uint32_t *tok_idx, enum lyxp_axis axis, ly_bool all_desc,
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008035 struct lyxp_set *set, uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02008036{
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008037 LY_ERR rc = LY_SUCCESS, r;
Michal Vasko004d3152020-06-11 19:59:22 +02008038 const char *ncname, *ncname_dict = NULL;
Michal Vasko56c008c2022-07-29 14:57:47 +02008039 uint32_t i, ncname_len;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008040 const struct lys_module *moveto_mod = NULL;
Michal Vaskoc71c37b2021-01-11 13:40:02 +01008041 const struct lysc_node *scnode = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02008042 struct ly_path_predicate *predicates = NULL;
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008043 int scnode_skip_pred = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02008044
aPiecek8b0cc152021-05-31 16:40:31 +02008045 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008046 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008047 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02008048
aPiecek8b0cc152021-05-31 16:40:31 +02008049 if (options & LYXP_SKIP_EXPR) {
Michal Vaskod3678892020-05-21 10:06:58 +02008050 goto moveto;
8051 }
8052
Michal Vasko004d3152020-06-11 19:59:22 +02008053 ncname = &exp->expr[exp->tok_pos[*tok_idx - 1]];
8054 ncname_len = exp->tok_len[*tok_idx - 1];
Michal Vaskod3678892020-05-21 10:06:58 +02008055
Michal Vaskoc71c37b2021-01-11 13:40:02 +01008056 if ((ncname[0] == '*') && (ncname_len == 1)) {
8057 /* all nodes will match */
8058 goto moveto;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008059 }
8060
Michal Vaskoc71c37b2021-01-11 13:40:02 +01008061 /* parse (and skip) module name */
8062 rc = moveto_resolve_model(&ncname, &ncname_len, set, NULL, &moveto_mod);
Michal Vaskod3678892020-05-21 10:06:58 +02008063 LY_CHECK_GOTO(rc, cleanup);
8064
Michal Vasko49fec8e2022-05-24 10:28:33 +02008065 if ((ncname[0] == '*') && (ncname_len == 1)) {
8066 /* all nodes from the module will match */
8067 goto moveto;
8068 }
8069
8070 if (((set->format == LY_VALUE_JSON) || moveto_mod) && (axis == LYXP_AXIS_CHILD) && !all_desc &&
8071 (set->type == LYXP_SET_NODE_SET)) {
Michal Vaskod3678892020-05-21 10:06:58 +02008072 /* find the matching schema node in some parent in the context */
Michal Vasko56c008c2022-07-29 14:57:47 +02008073 for (i = 0; i < set->used; ++i) {
Michal Vaskoc71c37b2021-01-11 13:40:02 +01008074 if (eval_name_test_with_predicate_get_scnode(set->ctx, set->val.nodes[i].node, ncname, ncname_len,
8075 moveto_mod, set->root_type, set->format, &scnode)) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008076 /* check failed */
8077 scnode = NULL;
8078 break;
Michal Vaskod3678892020-05-21 10:06:58 +02008079 }
8080 }
8081
Michal Vasko004d3152020-06-11 19:59:22 +02008082 if (scnode && (scnode->nodetype & (LYS_LIST | LYS_LEAFLIST))) {
8083 /* try to create the predicates */
Michal Vasko90189962023-02-28 12:10:34 +01008084 if (eval_name_test_try_compile_predicates(exp, tok_idx, scnode, set, &predicates)) {
Michal Vasko004d3152020-06-11 19:59:22 +02008085 /* hashes cannot be used */
Michal Vaskod3678892020-05-21 10:06:58 +02008086 scnode = NULL;
8087 }
8088 }
8089 }
8090
Michal Vaskoc71c37b2021-01-11 13:40:02 +01008091 if (!scnode) {
8092 /* we are not able to match based on a schema node and not all the modules match ("*"),
Michal Vasko004d3152020-06-11 19:59:22 +02008093 * use dictionary for efficient comparison */
Radek Krejci011e4aa2020-09-04 15:22:31 +02008094 LY_CHECK_GOTO(rc = lydict_insert(set->ctx, ncname, ncname_len, &ncname_dict), cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02008095 }
8096
8097moveto:
8098 /* move to the attribute(s), data node(s), or schema node(s) */
Michal Vasko49fec8e2022-05-24 10:28:33 +02008099 if (axis == LYXP_AXIS_ATTRIBUTE) {
aPiecek8b0cc152021-05-31 16:40:31 +02008100 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008101 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskod3678892020-05-21 10:06:58 +02008102 } else {
8103 if (all_desc) {
Michal Vaskocdad7122020-11-09 21:04:44 +01008104 rc = moveto_attr_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02008105 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02008106 rc = moveto_attr(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02008107 }
8108 LY_CHECK_GOTO(rc, cleanup);
8109 }
8110 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02008111 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008112 const struct lyxp_set_scnode *scparent = NULL;
Michal Vasko56c008c2022-07-29 14:57:47 +02008113 ly_bool found = 0;
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008114
8115 /* remember parent if there is only one, to print in the warning */
8116 for (i = 0; i < set->used; ++i) {
8117 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
8118 if (!scparent) {
8119 /* remember the context node */
8120 scparent = &set->val.scnodes[i];
8121 } else {
8122 /* several context nodes, no reasonable error possible */
8123 scparent = NULL;
8124 break;
8125 }
8126 }
8127 }
Radek Krejci1deb5be2020-08-26 16:43:36 +02008128
Michal Vasko49fec8e2022-05-24 10:28:33 +02008129 if (all_desc && (axis == LYXP_AXIS_CHILD)) {
8130 /* efficient evaluation that does not add all the descendants into the set */
8131 rc = moveto_scnode_alldesc_child(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02008132 } else {
Michal Vasko49fec8e2022-05-24 10:28:33 +02008133 if (all_desc) {
8134 /* "//" == "/descendant-or-self::node()/" */
8135 rc = xpath_pi_node(set, LYXP_AXIS_DESCENDANT_OR_SELF, options);
8136 LY_CHECK_GOTO(rc, cleanup);
8137 }
8138 rc = moveto_scnode(set, moveto_mod, ncname_dict, axis, options);
Michal Vaskod3678892020-05-21 10:06:58 +02008139 }
8140 LY_CHECK_GOTO(rc, cleanup);
8141
Michal Vasko56c008c2022-07-29 14:57:47 +02008142 i = set->used;
8143 do {
8144 --i;
Michal Vasko1a09b212021-05-06 13:00:10 +02008145 if (set->val.scnodes[i].in_ctx > LYXP_SET_SCNODE_ATOM_NODE) {
Michal Vasko56c008c2022-07-29 14:57:47 +02008146 found = 1;
Michal Vaskod3678892020-05-21 10:06:58 +02008147 break;
8148 }
Michal Vasko56c008c2022-07-29 14:57:47 +02008149 } while (i);
8150 if (!found) {
Michal Vasko4ad69e72021-10-26 16:25:55 +02008151 /* generate message */
8152 eval_name_test_scnode_no_match_msg(set, scparent, ncname, ncname_len, exp->expr, options);
8153
8154 if (options & LYXP_SCNODE_ERROR) {
8155 /* error */
8156 rc = LY_EVALID;
8157 goto cleanup;
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008158 }
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008159
8160 /* skip the predicates and the rest of this path to not generate invalid warnings */
8161 rc = LY_ENOT;
8162 scnode_skip_pred = 1;
Michal Vaskod3678892020-05-21 10:06:58 +02008163 }
8164 } else {
Michal Vasko49fec8e2022-05-24 10:28:33 +02008165 if (all_desc && (axis == LYXP_AXIS_CHILD)) {
8166 /* efficient evaluation */
8167 rc = moveto_node_alldesc_child(set, moveto_mod, ncname_dict, options);
8168 } else if (scnode && (axis == LYXP_AXIS_CHILD)) {
8169 /* we can find the child nodes using hashes */
8170 rc = moveto_node_hash_child(set, scnode, predicates, options);
Michal Vaskod3678892020-05-21 10:06:58 +02008171 } else {
Michal Vasko49fec8e2022-05-24 10:28:33 +02008172 if (all_desc) {
8173 /* "//" == "/descendant-or-self::node()/" */
8174 rc = xpath_pi_node(set, LYXP_AXIS_DESCENDANT_OR_SELF, options);
8175 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02008176 }
Michal Vasko49fec8e2022-05-24 10:28:33 +02008177 rc = moveto_node(set, moveto_mod, ncname_dict, axis, options);
Michal Vaskod3678892020-05-21 10:06:58 +02008178 }
8179 LY_CHECK_GOTO(rc, cleanup);
8180 }
8181 }
8182
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008183 if (scnode_skip_pred) {
8184 /* skip predicates */
8185 options |= LYXP_SKIP_EXPR;
8186 }
8187
Michal Vaskod3678892020-05-21 10:06:58 +02008188 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02008189 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02008190 r = eval_predicate(exp, tok_idx, set, options, axis);
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008191 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02008192 }
8193
8194cleanup:
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008195 if (scnode_skip_pred) {
8196 /* restore options */
8197 options &= ~LYXP_SKIP_EXPR;
8198 }
aPiecek8b0cc152021-05-31 16:40:31 +02008199 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko004d3152020-06-11 19:59:22 +02008200 lydict_remove(set->ctx, ncname_dict);
Michal Vasko90189962023-02-28 12:10:34 +01008201 ly_path_predicates_free(set->ctx, predicates);
Michal Vaskodb51a8d2020-05-27 15:22:29 +02008202 }
Michal Vaskod3678892020-05-21 10:06:58 +02008203 return rc;
8204}
8205
8206/**
8207 * @brief Evaluate NodeType and any following Predicates. Logs directly on error.
8208 *
8209 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
8210 * [6] NodeTest ::= NameTest | NodeType '(' ')'
8211 * [8] NodeType ::= 'text' | 'node'
8212 *
8213 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008214 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko49fec8e2022-05-24 10:28:33 +02008215 * @param[in] axis Axis to search on.
8216 * @param[in] all_desc Whether to search all the descendants or axis only.
aPiecek8b0cc152021-05-31 16:40:31 +02008217 * @param[in,out] set Context and result set.
Michal Vaskod3678892020-05-21 10:06:58 +02008218 * @param[in] options XPath options.
8219 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8220 */
8221static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02008222eval_node_type_with_predicate(const struct lyxp_expr *exp, uint32_t *tok_idx, enum lyxp_axis axis, ly_bool all_desc,
Radek Krejci1deb5be2020-08-26 16:43:36 +02008223 struct lyxp_set *set, uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02008224{
8225 LY_ERR rc;
8226
Michal Vaskod3678892020-05-21 10:06:58 +02008227 (void)all_desc;
8228
aPiecek8b0cc152021-05-31 16:40:31 +02008229 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko004d3152020-06-11 19:59:22 +02008230 assert(exp->tok_len[*tok_idx] == 4);
Michal Vasko49fec8e2022-05-24 10:28:33 +02008231 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "node", 4)) {
8232 rc = xpath_pi_node(set, axis, options);
Michal Vaskod3678892020-05-21 10:06:58 +02008233 } else {
Michal Vasko49fec8e2022-05-24 10:28:33 +02008234 assert(!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "text", 4));
8235 rc = xpath_pi_text(set, axis, options);
Michal Vaskod3678892020-05-21 10:06:58 +02008236 }
Michal Vasko49fec8e2022-05-24 10:28:33 +02008237 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02008238 }
aPiecek8b0cc152021-05-31 16:40:31 +02008239 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008240 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008241 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02008242
8243 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02008244 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
aPiecek8b0cc152021-05-31 16:40:31 +02008245 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008246 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008247 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02008248
8249 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02008250 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
aPiecek8b0cc152021-05-31 16:40:31 +02008251 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008252 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008253 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02008254
8255 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02008256 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02008257 rc = eval_predicate(exp, tok_idx, set, options, axis);
Michal Vaskod3678892020-05-21 10:06:58 +02008258 LY_CHECK_RET(rc);
8259 }
8260
8261 return LY_SUCCESS;
8262}
8263
8264/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02008265 * @brief Evaluate RelativeLocationPath. Logs directly on error.
8266 *
8267 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
8268 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
Michal Vaskod3678892020-05-21 10:06:58 +02008269 * [6] NodeTest ::= NameTest | NodeType '(' ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02008270 *
8271 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008272 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008273 * @param[in] all_desc Whether to search all the descendants or children only.
aPiecek8b0cc152021-05-31 16:40:31 +02008274 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008275 * @param[in] options XPath options.
8276 * @return LY_ERR (YL_EINCOMPLETE on unresolved when)
8277 */
8278static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02008279eval_relative_location_path(const struct lyxp_expr *exp, uint32_t *tok_idx, ly_bool all_desc, struct lyxp_set *set,
Michal Vasko40308e72020-10-20 16:38:40 +02008280 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008281{
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008282 LY_ERR rc = LY_SUCCESS;
Michal Vasko49fec8e2022-05-24 10:28:33 +02008283 enum lyxp_axis axis;
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008284 int scnode_skip_path = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008285
8286 goto step;
8287 do {
8288 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02008289 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008290 all_desc = 0;
8291 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02008292 assert(exp->tok_len[*tok_idx] == 2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008293 all_desc = 1;
8294 }
aPiecek8b0cc152021-05-31 16:40:31 +02008295 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008296 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008297 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008298
8299step:
Michal Vasko49fec8e2022-05-24 10:28:33 +02008300 /* AxisSpecifier */
8301 if (exp->tokens[*tok_idx] == LYXP_TOKEN_AXISNAME) {
8302 axis = str2axis(exp->expr + exp->tok_pos[*tok_idx], exp->tok_len[*tok_idx]);
Michal Vaskod3678892020-05-21 10:06:58 +02008303
aPiecek8b0cc152021-05-31 16:40:31 +02008304 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008305 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
8306 ++(*tok_idx);
8307
8308 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_DCOLON);
8309 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
8310 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
8311 ++(*tok_idx);
8312 } else if (exp->tokens[*tok_idx] == LYXP_TOKEN_AT) {
8313 axis = LYXP_AXIS_ATTRIBUTE;
8314
8315 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
8316 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008317 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02008318 } else {
Michal Vasko49fec8e2022-05-24 10:28:33 +02008319 /* default */
8320 axis = LYXP_AXIS_CHILD;
Michal Vaskod3678892020-05-21 10:06:58 +02008321 }
8322
Michal Vasko49fec8e2022-05-24 10:28:33 +02008323 /* NodeTest Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02008324 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008325 case LYXP_TOKEN_DOT:
8326 /* evaluate '.' */
Michal Vasko49fec8e2022-05-24 10:28:33 +02008327 if (!(options & LYXP_SKIP_EXPR)) {
8328 if (((options & LYXP_SCNODE_ALL) && (set->type != LYXP_SET_SCNODE_SET)) ||
8329 (!(options & LYXP_SCNODE_ALL) && (set->type != LYXP_SET_NODE_SET))) {
8330 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
8331 rc = LY_EVALID;
8332 goto cleanup;
8333 }
8334
8335 if (all_desc) {
8336 rc = xpath_pi_node(set, LYXP_AXIS_DESCENDANT_OR_SELF, options);
8337 LY_CHECK_GOTO(rc, cleanup);
8338 }
8339 rc = xpath_pi_node(set, LYXP_AXIS_SELF, options);
8340 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008341 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008342 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008343 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008344 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008345 break;
8346
8347 case LYXP_TOKEN_DDOT:
8348 /* evaluate '..' */
Michal Vasko49fec8e2022-05-24 10:28:33 +02008349 if (!(options & LYXP_SKIP_EXPR)) {
8350 if (((options & LYXP_SCNODE_ALL) && (set->type != LYXP_SET_SCNODE_SET)) ||
8351 (!(options & LYXP_SCNODE_ALL) && (set->type != LYXP_SET_NODE_SET))) {
8352 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
8353 rc = LY_EVALID;
8354 goto cleanup;
8355 }
8356
8357 if (all_desc) {
8358 rc = xpath_pi_node(set, LYXP_AXIS_DESCENDANT_OR_SELF, options);
8359 LY_CHECK_GOTO(rc, cleanup);
8360 }
8361 rc = xpath_pi_node(set, LYXP_AXIS_PARENT, options);
8362 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008363 }
aPiecek8b0cc152021-05-31 16:40:31 +02008364 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008365 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008366 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008367 break;
8368
Michal Vasko03ff5a72019-09-11 13:49:33 +02008369 case LYXP_TOKEN_NAMETEST:
Michal Vaskod3678892020-05-21 10:06:58 +02008370 /* evaluate NameTest Predicate* */
Michal Vasko49fec8e2022-05-24 10:28:33 +02008371 rc = eval_name_test_with_predicate(exp, tok_idx, axis, all_desc, set, options);
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008372 if (rc == LY_ENOT) {
8373 assert(options & LYXP_SCNODE_ALL);
8374 /* skip the rest of this path */
8375 rc = LY_SUCCESS;
8376 scnode_skip_path = 1;
8377 options |= LYXP_SKIP_EXPR;
8378 }
8379 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02008380 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008381
Michal Vaskod3678892020-05-21 10:06:58 +02008382 case LYXP_TOKEN_NODETYPE:
8383 /* evaluate NodeType Predicate* */
Michal Vasko49fec8e2022-05-24 10:28:33 +02008384 rc = eval_node_type_with_predicate(exp, tok_idx, axis, all_desc, set, options);
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008385 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008386 break;
8387
8388 default:
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008389 LOGINT(set->ctx);
8390 rc = LY_EINT;
8391 goto cleanup;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008392 }
Michal Vasko004d3152020-06-11 19:59:22 +02008393 } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008394
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008395cleanup:
8396 if (scnode_skip_path) {
8397 options &= ~LYXP_SKIP_EXPR;
8398 }
8399 return rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008400}
8401
8402/**
8403 * @brief Evaluate AbsoluteLocationPath. Logs directly on error.
8404 *
8405 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
8406 *
8407 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008408 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02008409 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008410 * @param[in] options XPath options.
8411 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8412 */
8413static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02008414eval_absolute_location_path(const struct lyxp_expr *exp, uint32_t *tok_idx, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008415{
Radek Krejci857189e2020-09-01 13:26:36 +02008416 ly_bool all_desc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008417
aPiecek8b0cc152021-05-31 16:40:31 +02008418 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008419 /* no matter what tokens follow, we need to be at the root */
Michal Vaskob0099a92020-08-31 14:55:23 +02008420 LY_CHECK_RET(moveto_root(set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008421 }
8422
8423 /* '/' RelativeLocationPath? */
Michal Vasko004d3152020-06-11 19:59:22 +02008424 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008425 /* evaluate '/' - deferred */
8426 all_desc = 0;
aPiecek8b0cc152021-05-31 16:40:31 +02008427 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008428 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008429 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008430
Michal Vasko004d3152020-06-11 19:59:22 +02008431 if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008432 return LY_SUCCESS;
8433 }
Michal Vasko004d3152020-06-11 19:59:22 +02008434 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008435 case LYXP_TOKEN_DOT:
8436 case LYXP_TOKEN_DDOT:
Michal Vasko49fec8e2022-05-24 10:28:33 +02008437 case LYXP_TOKEN_AXISNAME:
Michal Vasko03ff5a72019-09-11 13:49:33 +02008438 case LYXP_TOKEN_AT:
8439 case LYXP_TOKEN_NAMETEST:
8440 case LYXP_TOKEN_NODETYPE:
Michal Vaskob0099a92020-08-31 14:55:23 +02008441 LY_CHECK_RET(eval_relative_location_path(exp, tok_idx, all_desc, set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008442 break;
8443 default:
8444 break;
8445 }
8446
Michal Vasko03ff5a72019-09-11 13:49:33 +02008447 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02008448 /* '//' RelativeLocationPath */
Michal Vasko03ff5a72019-09-11 13:49:33 +02008449 /* evaluate '//' - deferred so as not to waste memory by remembering all the nodes */
8450 all_desc = 1;
aPiecek8b0cc152021-05-31 16:40:31 +02008451 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008452 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008453 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008454
Michal Vaskob0099a92020-08-31 14:55:23 +02008455 LY_CHECK_RET(eval_relative_location_path(exp, tok_idx, all_desc, set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008456 }
8457
8458 return LY_SUCCESS;
8459}
8460
8461/**
8462 * @brief Evaluate FunctionCall. Logs directly on error.
8463 *
Michal Vaskod3678892020-05-21 10:06:58 +02008464 * [11] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02008465 *
8466 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008467 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02008468 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008469 * @param[in] options XPath options.
8470 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8471 */
8472static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02008473eval_function_call(const struct lyxp_expr *exp, uint32_t *tok_idx, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008474{
8475 LY_ERR rc;
Michal Vasko69730152020-10-09 16:30:07 +02008476
Michal Vaskodd528af2022-08-08 14:35:07 +02008477 LY_ERR (*xpath_func)(struct lyxp_set **, uint32_t, struct lyxp_set *, uint32_t) = NULL;
8478 uint32_t arg_count = 0, i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008479 struct lyxp_set **args = NULL, **args_aux;
8480
aPiecek8b0cc152021-05-31 16:40:31 +02008481 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008482 /* FunctionName */
Michal Vasko004d3152020-06-11 19:59:22 +02008483 switch (exp->tok_len[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008484 case 3:
Michal Vasko004d3152020-06-11 19:59:22 +02008485 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008486 xpath_func = &xpath_not;
Michal Vasko004d3152020-06-11 19:59:22 +02008487 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008488 xpath_func = &xpath_sum;
8489 }
8490 break;
8491 case 4:
Michal Vasko004d3152020-06-11 19:59:22 +02008492 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008493 xpath_func = &xpath_lang;
Michal Vasko004d3152020-06-11 19:59:22 +02008494 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008495 xpath_func = &xpath_last;
Michal Vasko004d3152020-06-11 19:59:22 +02008496 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008497 xpath_func = &xpath_name;
Michal Vasko004d3152020-06-11 19:59:22 +02008498 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008499 xpath_func = &xpath_true;
8500 }
8501 break;
8502 case 5:
Michal Vasko004d3152020-06-11 19:59:22 +02008503 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008504 xpath_func = &xpath_count;
Michal Vasko004d3152020-06-11 19:59:22 +02008505 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008506 xpath_func = &xpath_false;
Michal Vasko004d3152020-06-11 19:59:22 +02008507 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008508 xpath_func = &xpath_floor;
Michal Vasko004d3152020-06-11 19:59:22 +02008509 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008510 xpath_func = &xpath_round;
Michal Vasko004d3152020-06-11 19:59:22 +02008511 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008512 xpath_func = &xpath_deref;
8513 }
8514 break;
8515 case 6:
Michal Vasko004d3152020-06-11 19:59:22 +02008516 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008517 xpath_func = &xpath_concat;
Michal Vasko004d3152020-06-11 19:59:22 +02008518 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008519 xpath_func = &xpath_number;
Michal Vasko004d3152020-06-11 19:59:22 +02008520 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008521 xpath_func = &xpath_string;
8522 }
8523 break;
8524 case 7:
Michal Vasko004d3152020-06-11 19:59:22 +02008525 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008526 xpath_func = &xpath_boolean;
Michal Vasko004d3152020-06-11 19:59:22 +02008527 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008528 xpath_func = &xpath_ceiling;
Michal Vasko004d3152020-06-11 19:59:22 +02008529 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008530 xpath_func = &xpath_current;
8531 }
8532 break;
8533 case 8:
Michal Vasko004d3152020-06-11 19:59:22 +02008534 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008535 xpath_func = &xpath_contains;
Michal Vasko004d3152020-06-11 19:59:22 +02008536 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008537 xpath_func = &xpath_position;
Michal Vasko004d3152020-06-11 19:59:22 +02008538 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008539 xpath_func = &xpath_re_match;
8540 }
8541 break;
8542 case 9:
Michal Vasko004d3152020-06-11 19:59:22 +02008543 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008544 xpath_func = &xpath_substring;
Michal Vasko004d3152020-06-11 19:59:22 +02008545 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008546 xpath_func = &xpath_translate;
8547 }
8548 break;
8549 case 10:
Michal Vasko004d3152020-06-11 19:59:22 +02008550 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008551 xpath_func = &xpath_local_name;
Michal Vasko004d3152020-06-11 19:59:22 +02008552 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008553 xpath_func = &xpath_enum_value;
Michal Vasko004d3152020-06-11 19:59:22 +02008554 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008555 xpath_func = &xpath_bit_is_set;
8556 }
8557 break;
8558 case 11:
Michal Vasko004d3152020-06-11 19:59:22 +02008559 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008560 xpath_func = &xpath_starts_with;
8561 }
8562 break;
8563 case 12:
Michal Vasko004d3152020-06-11 19:59:22 +02008564 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008565 xpath_func = &xpath_derived_from;
8566 }
8567 break;
8568 case 13:
Michal Vasko004d3152020-06-11 19:59:22 +02008569 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008570 xpath_func = &xpath_namespace_uri;
Michal Vasko004d3152020-06-11 19:59:22 +02008571 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008572 xpath_func = &xpath_string_length;
8573 }
8574 break;
8575 case 15:
Michal Vasko004d3152020-06-11 19:59:22 +02008576 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008577 xpath_func = &xpath_normalize_space;
Michal Vasko004d3152020-06-11 19:59:22 +02008578 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008579 xpath_func = &xpath_substring_after;
8580 }
8581 break;
8582 case 16:
Michal Vasko004d3152020-06-11 19:59:22 +02008583 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008584 xpath_func = &xpath_substring_before;
8585 }
8586 break;
8587 case 20:
Michal Vasko004d3152020-06-11 19:59:22 +02008588 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008589 xpath_func = &xpath_derived_from_or_self;
8590 }
8591 break;
8592 }
8593
8594 if (!xpath_func) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01008595 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 +02008596 return LY_EVALID;
8597 }
8598 }
8599
aPiecek8b0cc152021-05-31 16:40:31 +02008600 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008601 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008602 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008603
8604 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02008605 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
aPiecek8b0cc152021-05-31 16:40:31 +02008606 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008607 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008608 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008609
8610 /* ( Expr ( ',' Expr )* )? */
Michal Vasko004d3152020-06-11 19:59:22 +02008611 if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) {
aPiecek8b0cc152021-05-31 16:40:31 +02008612 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008613 args = malloc(sizeof *args);
8614 LY_CHECK_ERR_GOTO(!args, LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
8615 arg_count = 1;
8616 args[0] = set_copy(set);
8617 if (!args[0]) {
8618 rc = LY_EMEM;
8619 goto cleanup;
8620 }
8621
Michal Vasko004d3152020-06-11 19:59:22 +02008622 rc = eval_expr_select(exp, tok_idx, 0, args[0], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008623 LY_CHECK_GOTO(rc, cleanup);
8624 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02008625 rc = eval_expr_select(exp, tok_idx, 0, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008626 LY_CHECK_GOTO(rc, cleanup);
8627 }
8628 }
Michal Vasko004d3152020-06-11 19:59:22 +02008629 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) {
aPiecek8b0cc152021-05-31 16:40:31 +02008630 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008631 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008632 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008633
aPiecek8b0cc152021-05-31 16:40:31 +02008634 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008635 ++arg_count;
8636 args_aux = realloc(args, arg_count * sizeof *args);
8637 LY_CHECK_ERR_GOTO(!args_aux, arg_count--; LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
8638 args = args_aux;
8639 args[arg_count - 1] = set_copy(set);
8640 if (!args[arg_count - 1]) {
8641 rc = LY_EMEM;
8642 goto cleanup;
8643 }
8644
Michal Vasko004d3152020-06-11 19:59:22 +02008645 rc = eval_expr_select(exp, tok_idx, 0, args[arg_count - 1], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008646 LY_CHECK_GOTO(rc, cleanup);
8647 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02008648 rc = eval_expr_select(exp, tok_idx, 0, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008649 LY_CHECK_GOTO(rc, cleanup);
8650 }
8651 }
8652
8653 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02008654 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
aPiecek8b0cc152021-05-31 16:40:31 +02008655 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008656 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008657 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008658
aPiecek8b0cc152021-05-31 16:40:31 +02008659 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008660 /* evaluate function */
8661 rc = xpath_func(args, arg_count, set, options);
8662
8663 if (options & LYXP_SCNODE_ALL) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008664 /* merge all nodes from arg evaluations */
8665 for (i = 0; i < arg_count; ++i) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008666 set_scnode_clear_ctx(args[i], LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008667 lyxp_set_scnode_merge(set, args[i]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008668 }
8669 }
8670 } else {
8671 rc = LY_SUCCESS;
8672 }
8673
8674cleanup:
8675 for (i = 0; i < arg_count; ++i) {
8676 lyxp_set_free(args[i]);
8677 }
8678 free(args);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008679 return rc;
8680}
8681
8682/**
8683 * @brief Evaluate Number. Logs directly on error.
8684 *
8685 * @param[in] ctx Context for errors.
8686 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008687 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008688 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8689 * @return LY_ERR
8690 */
8691static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02008692eval_number(struct ly_ctx *ctx, const struct lyxp_expr *exp, uint32_t *tok_idx, struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008693{
8694 long double num;
8695 char *endptr;
8696
8697 if (set) {
8698 errno = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02008699 num = strtold(&exp->expr[exp->tok_pos[*tok_idx]], &endptr);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008700 if (errno) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01008701 LOGVAL(ctx, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]);
8702 LOGVAL(ctx, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double (%s).",
Michal Vasko69730152020-10-09 16:30:07 +02008703 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]], strerror(errno));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008704 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02008705 } else if (endptr - &exp->expr[exp->tok_pos[*tok_idx]] != exp->tok_len[*tok_idx]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01008706 LOGVAL(ctx, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]);
8707 LOGVAL(ctx, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double.",
Michal Vasko69730152020-10-09 16:30:07 +02008708 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008709 return LY_EVALID;
8710 }
8711
8712 set_fill_number(set, num);
8713 }
8714
8715 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008716 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008717 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008718 return LY_SUCCESS;
8719}
8720
aPiecekdf23eee2021-10-07 12:21:50 +02008721LY_ERR
Michal Vasko90189962023-02-28 12:10:34 +01008722lyxp_vars_find(const struct ly_ctx *ctx, const struct lyxp_var *vars, const char *name, size_t name_len,
8723 struct lyxp_var **var)
aPiecekdf23eee2021-10-07 12:21:50 +02008724{
aPiecekdf23eee2021-10-07 12:21:50 +02008725 LY_ARRAY_COUNT_TYPE u;
8726
Michal Vasko90189962023-02-28 12:10:34 +01008727 assert(name);
aPiecekdf23eee2021-10-07 12:21:50 +02008728
Michal Vasko90189962023-02-28 12:10:34 +01008729 if (!name_len) {
8730 name_len = strlen(name);
8731 }
aPiecekdf23eee2021-10-07 12:21:50 +02008732
8733 LY_ARRAY_FOR(vars, u) {
8734 if (!strncmp(vars[u].name, name, name_len)) {
Michal Vasko90189962023-02-28 12:10:34 +01008735 if (var) {
8736 *var = (struct lyxp_var *)&vars[u];
8737 }
8738 return LY_SUCCESS;
aPiecekdf23eee2021-10-07 12:21:50 +02008739 }
8740 }
8741
Michal Vasko90189962023-02-28 12:10:34 +01008742 if (ctx) {
8743 LOGERR(ctx, LY_ENOTFOUND, "Variable \"%.*s\" not defined.", (int)name_len, name);
aPiecekdf23eee2021-10-07 12:21:50 +02008744 }
Michal Vasko90189962023-02-28 12:10:34 +01008745 return LY_ENOTFOUND;
aPiecekdf23eee2021-10-07 12:21:50 +02008746}
8747
Michal Vasko03ff5a72019-09-11 13:49:33 +02008748/**
aPiecekfba75362021-10-07 12:39:48 +02008749 * @brief Evaluate VariableReference.
8750 *
8751 * @param[in] exp Parsed XPath expression.
8752 * @param[in] tok_idx Position in the expression @p exp.
8753 * @param[in] vars [Sized array](@ref sizedarrays) of XPath variables.
8754 * @param[in,out] set Context and result set.
8755 * @param[in] options XPath options.
8756 * @return LY_ERR value.
8757 */
8758static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02008759eval_variable_reference(const struct lyxp_expr *exp, uint32_t *tok_idx, struct lyxp_set *set, uint32_t options)
aPiecekfba75362021-10-07 12:39:48 +02008760{
8761 LY_ERR ret;
8762 const char *name;
8763 struct lyxp_var *var;
aPiecekfba75362021-10-07 12:39:48 +02008764 struct lyxp_expr *tokens = NULL;
Michal Vaskodd528af2022-08-08 14:35:07 +02008765 uint32_t token_index, name_len;
aPiecekfba75362021-10-07 12:39:48 +02008766
Michal Vasko49fec8e2022-05-24 10:28:33 +02008767 /* find out the name and value of the variable */
aPiecekfba75362021-10-07 12:39:48 +02008768 name = &exp->expr[exp->tok_pos[*tok_idx]];
Michal Vaskoe68b5402022-07-29 14:58:15 +02008769 name_len = exp->tok_len[*tok_idx];
Michal Vasko90189962023-02-28 12:10:34 +01008770 ret = lyxp_vars_find(set->ctx, set->vars, name, name_len, &var);
8771 LY_CHECK_RET(ret);
aPiecekfba75362021-10-07 12:39:48 +02008772
Michal Vasko49fec8e2022-05-24 10:28:33 +02008773 /* parse value */
aPiecekfba75362021-10-07 12:39:48 +02008774 ret = lyxp_expr_parse(set->ctx, var->value, 0, 1, &tokens);
8775 LY_CHECK_GOTO(ret, cleanup);
8776
Michal Vasko49fec8e2022-05-24 10:28:33 +02008777 /* evaluate value */
aPiecekfba75362021-10-07 12:39:48 +02008778 token_index = 0;
8779 ret = eval_expr_select(tokens, &token_index, 0, set, options);
8780 LY_CHECK_GOTO(ret, cleanup);
8781
8782cleanup:
8783 lyxp_expr_free(set->ctx, tokens);
8784
8785 return ret;
8786}
8787
8788/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02008789 * @brief Evaluate PathExpr. Logs directly on error.
8790 *
Michal Vaskod3678892020-05-21 10:06:58 +02008791 * [12] PathExpr ::= LocationPath | PrimaryExpr Predicate*
Michal Vasko03ff5a72019-09-11 13:49:33 +02008792 * | PrimaryExpr Predicate* '/' RelativeLocationPath
8793 * | PrimaryExpr Predicate* '//' RelativeLocationPath
8794 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
aPiecekfba75362021-10-07 12:39:48 +02008795 * [10] PrimaryExpr ::= VariableReference | '(' Expr ')' | Literal | Number | FunctionCall
Michal Vasko03ff5a72019-09-11 13:49:33 +02008796 *
8797 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008798 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02008799 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008800 * @param[in] options XPath options.
8801 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8802 */
8803static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02008804eval_path_expr(const struct lyxp_expr *exp, uint32_t *tok_idx, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008805{
Michal Vasko49fec8e2022-05-24 10:28:33 +02008806 ly_bool all_desc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008807 LY_ERR rc;
8808
Michal Vasko004d3152020-06-11 19:59:22 +02008809 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008810 case LYXP_TOKEN_PAR1:
8811 /* '(' Expr ')' */
8812
8813 /* '(' */
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
8818 /* Expr */
Michal Vasko004d3152020-06-11 19:59:22 +02008819 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008820 LY_CHECK_RET(rc);
8821
8822 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02008823 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
aPiecek8b0cc152021-05-31 16:40:31 +02008824 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008825 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008826 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008827
Michal Vasko03ff5a72019-09-11 13:49:33 +02008828 goto predicate;
8829
8830 case LYXP_TOKEN_DOT:
8831 case LYXP_TOKEN_DDOT:
Michal Vasko49fec8e2022-05-24 10:28:33 +02008832 case LYXP_TOKEN_AXISNAME:
Michal Vasko03ff5a72019-09-11 13:49:33 +02008833 case LYXP_TOKEN_AT:
8834 case LYXP_TOKEN_NAMETEST:
8835 case LYXP_TOKEN_NODETYPE:
8836 /* RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02008837 rc = eval_relative_location_path(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008838 LY_CHECK_RET(rc);
8839 break;
8840
aPiecekfba75362021-10-07 12:39:48 +02008841 case LYXP_TOKEN_VARREF:
8842 /* VariableReference */
8843 rc = eval_variable_reference(exp, tok_idx, set, options);
8844 LY_CHECK_RET(rc);
8845 ++(*tok_idx);
8846
aPiecekfba75362021-10-07 12:39:48 +02008847 goto predicate;
8848
Michal Vasko03ff5a72019-09-11 13:49:33 +02008849 case LYXP_TOKEN_FUNCNAME:
8850 /* FunctionCall */
aPiecek8b0cc152021-05-31 16:40:31 +02008851 rc = eval_function_call(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008852 LY_CHECK_RET(rc);
8853
Michal Vasko03ff5a72019-09-11 13:49:33 +02008854 goto predicate;
8855
Michal Vasko3e48bf32020-06-01 08:39:07 +02008856 case LYXP_TOKEN_OPER_PATH:
8857 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02008858 /* AbsoluteLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02008859 rc = eval_absolute_location_path(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008860 LY_CHECK_RET(rc);
8861 break;
8862
8863 case LYXP_TOKEN_LITERAL:
8864 /* Literal */
aPiecek8b0cc152021-05-31 16:40:31 +02008865 if ((options & LYXP_SKIP_EXPR) || (options & LYXP_SCNODE_ALL)) {
8866 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008867 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008868 }
Michal Vasko004d3152020-06-11 19:59:22 +02008869 eval_literal(exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008870 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02008871 eval_literal(exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008872 }
8873
Michal Vasko03ff5a72019-09-11 13:49:33 +02008874 goto predicate;
8875
8876 case LYXP_TOKEN_NUMBER:
8877 /* Number */
aPiecek8b0cc152021-05-31 16:40:31 +02008878 if ((options & LYXP_SKIP_EXPR) || (options & LYXP_SCNODE_ALL)) {
8879 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008880 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008881 }
Michal Vasko004d3152020-06-11 19:59:22 +02008882 rc = eval_number(NULL, exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008883 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02008884 rc = eval_number(set->ctx, exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008885 }
8886 LY_CHECK_RET(rc);
8887
Michal Vasko03ff5a72019-09-11 13:49:33 +02008888 goto predicate;
8889
8890 default:
Michal Vasko49fec8e2022-05-24 10:28:33 +02008891 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 +02008892 return LY_EVALID;
8893 }
8894
8895 return LY_SUCCESS;
8896
8897predicate:
8898 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02008899 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02008900 rc = eval_predicate(exp, tok_idx, set, options, LYXP_AXIS_CHILD);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008901 LY_CHECK_RET(rc);
8902 }
8903
8904 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02008905 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008906
8907 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02008908 if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008909 all_desc = 0;
8910 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008911 all_desc = 1;
8912 }
8913
aPiecek8b0cc152021-05-31 16:40:31 +02008914 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008915 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008916 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008917
Michal Vasko004d3152020-06-11 19:59:22 +02008918 rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008919 LY_CHECK_RET(rc);
8920 }
8921
8922 return LY_SUCCESS;
8923}
8924
8925/**
8926 * @brief Evaluate UnionExpr. Logs directly on error.
8927 *
Michal Vaskod3678892020-05-21 10:06:58 +02008928 * [20] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008929 *
8930 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008931 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008932 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008933 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008934 * @param[in] options XPath options.
8935 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8936 */
8937static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02008938eval_union_expr(const struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t repeat, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008939{
8940 LY_ERR rc = LY_SUCCESS;
8941 struct lyxp_set orig_set, set2;
Michal Vaskodd528af2022-08-08 14:35:07 +02008942 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008943
8944 assert(repeat);
8945
8946 set_init(&orig_set, set);
8947 set_init(&set2, set);
8948
8949 set_fill_set(&orig_set, set);
8950
Michal Vasko004d3152020-06-11 19:59:22 +02008951 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008952 LY_CHECK_GOTO(rc, cleanup);
8953
8954 /* ('|' PathExpr)* */
8955 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008956 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_UNI);
aPiecek8b0cc152021-05-31 16:40:31 +02008957 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008958 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008959 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008960
aPiecek8b0cc152021-05-31 16:40:31 +02008961 if (options & LYXP_SKIP_EXPR) {
8962 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008963 LY_CHECK_GOTO(rc, cleanup);
8964 continue;
8965 }
8966
8967 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008968 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008969 LY_CHECK_GOTO(rc, cleanup);
8970
8971 /* eval */
8972 if (options & LYXP_SCNODE_ALL) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01008973 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008974 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008975 rc = moveto_union(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008976 LY_CHECK_GOTO(rc, cleanup);
8977 }
8978 }
8979
8980cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008981 lyxp_set_free_content(&orig_set);
8982 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008983 return rc;
8984}
8985
8986/**
8987 * @brief Evaluate UnaryExpr. Logs directly on error.
8988 *
Michal Vaskod3678892020-05-21 10:06:58 +02008989 * [19] UnaryExpr ::= UnionExpr | '-' UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008990 *
8991 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008992 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008993 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008994 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008995 * @param[in] options XPath options.
8996 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8997 */
8998static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02008999eval_unary_expr(const struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t repeat, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02009000{
9001 LY_ERR rc;
Michal Vaskodd528af2022-08-08 14:35:07 +02009002 uint32_t this_op, i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009003
9004 assert(repeat);
9005
9006 /* ('-')+ */
Michal Vasko004d3152020-06-11 19:59:22 +02009007 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009008 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02009009 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 +02009010
aPiecek8b0cc152021-05-31 16:40:31 +02009011 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02009012 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02009013 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009014 }
9015
Michal Vasko004d3152020-06-11 19:59:22 +02009016 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNARY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009017 LY_CHECK_RET(rc);
9018
aPiecek8b0cc152021-05-31 16:40:31 +02009019 if (!(options & LYXP_SKIP_EXPR) && (repeat % 2)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02009020 if (options & LYXP_SCNODE_ALL) {
9021 warn_operands(set->ctx, set, NULL, 1, exp->expr, exp->tok_pos[this_op]);
9022 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009023 rc = moveto_op_math(set, NULL, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009024 LY_CHECK_RET(rc);
9025 }
9026 }
9027
9028 return LY_SUCCESS;
9029}
9030
9031/**
9032 * @brief Evaluate MultiplicativeExpr. Logs directly on error.
9033 *
Michal Vaskod3678892020-05-21 10:06:58 +02009034 * [18] MultiplicativeExpr ::= UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02009035 * | MultiplicativeExpr '*' UnaryExpr
9036 * | MultiplicativeExpr 'div' UnaryExpr
9037 * | MultiplicativeExpr 'mod' UnaryExpr
9038 *
9039 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02009040 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009041 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02009042 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009043 * @param[in] options XPath options.
9044 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
9045 */
9046static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02009047eval_multiplicative_expr(const struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t repeat, struct lyxp_set *set,
Michal Vasko40308e72020-10-20 16:38:40 +02009048 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02009049{
9050 LY_ERR rc;
Michal Vaskodd528af2022-08-08 14:35:07 +02009051 uint32_t i, this_op;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009052 struct lyxp_set orig_set, set2;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009053
9054 assert(repeat);
9055
9056 set_init(&orig_set, set);
9057 set_init(&set2, set);
9058
9059 set_fill_set(&orig_set, set);
9060
Michal Vasko004d3152020-06-11 19:59:22 +02009061 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009062 LY_CHECK_GOTO(rc, cleanup);
9063
9064 /* ('*' / 'div' / 'mod' UnaryExpr)* */
9065 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02009066 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009067
Michal Vasko004d3152020-06-11 19:59:22 +02009068 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
aPiecek8b0cc152021-05-31 16:40:31 +02009069 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02009070 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02009071 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009072
aPiecek8b0cc152021-05-31 16:40:31 +02009073 if (options & LYXP_SKIP_EXPR) {
9074 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009075 LY_CHECK_GOTO(rc, cleanup);
9076 continue;
9077 }
9078
9079 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02009080 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009081 LY_CHECK_GOTO(rc, cleanup);
9082
9083 /* eval */
9084 if (options & LYXP_SCNODE_ALL) {
9085 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01009086 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02009087 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009088 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009089 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009090 LY_CHECK_GOTO(rc, cleanup);
9091 }
9092 }
9093
9094cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02009095 lyxp_set_free_content(&orig_set);
9096 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009097 return rc;
9098}
9099
9100/**
9101 * @brief Evaluate AdditiveExpr. Logs directly on error.
9102 *
Michal Vaskod3678892020-05-21 10:06:58 +02009103 * [17] AdditiveExpr ::= MultiplicativeExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02009104 * | AdditiveExpr '+' MultiplicativeExpr
9105 * | AdditiveExpr '-' MultiplicativeExpr
9106 *
9107 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02009108 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009109 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02009110 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009111 * @param[in] options XPath options.
9112 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
9113 */
9114static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02009115eval_additive_expr(const struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t repeat, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02009116{
9117 LY_ERR rc;
Michal Vaskodd528af2022-08-08 14:35:07 +02009118 uint32_t i, this_op;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009119 struct lyxp_set orig_set, set2;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009120
9121 assert(repeat);
9122
9123 set_init(&orig_set, set);
9124 set_init(&set2, set);
9125
9126 set_fill_set(&orig_set, set);
9127
Michal Vasko004d3152020-06-11 19:59:22 +02009128 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009129 LY_CHECK_GOTO(rc, cleanup);
9130
9131 /* ('+' / '-' MultiplicativeExpr)* */
9132 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02009133 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009134
Michal Vasko004d3152020-06-11 19:59:22 +02009135 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009136 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02009137 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02009138 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009139
aPiecek8b0cc152021-05-31 16:40:31 +02009140 if (options & LYXP_SKIP_EXPR) {
9141 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009142 LY_CHECK_GOTO(rc, cleanup);
9143 continue;
9144 }
9145
9146 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02009147 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009148 LY_CHECK_GOTO(rc, cleanup);
9149
9150 /* eval */
9151 if (options & LYXP_SCNODE_ALL) {
9152 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01009153 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02009154 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009155 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009156 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009157 LY_CHECK_GOTO(rc, cleanup);
9158 }
9159 }
9160
9161cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02009162 lyxp_set_free_content(&orig_set);
9163 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009164 return rc;
9165}
9166
9167/**
9168 * @brief Evaluate RelationalExpr. Logs directly on error.
9169 *
Michal Vaskod3678892020-05-21 10:06:58 +02009170 * [16] RelationalExpr ::= AdditiveExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02009171 * | RelationalExpr '<' AdditiveExpr
9172 * | RelationalExpr '>' AdditiveExpr
9173 * | RelationalExpr '<=' AdditiveExpr
9174 * | RelationalExpr '>=' AdditiveExpr
9175 *
9176 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02009177 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009178 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02009179 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009180 * @param[in] options XPath options.
9181 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
9182 */
9183static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02009184eval_relational_expr(const struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t repeat, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02009185{
9186 LY_ERR rc;
Michal Vaskodd528af2022-08-08 14:35:07 +02009187 uint32_t i, this_op;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009188 struct lyxp_set orig_set, set2;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009189
9190 assert(repeat);
9191
9192 set_init(&orig_set, set);
9193 set_init(&set2, set);
9194
9195 set_fill_set(&orig_set, set);
9196
Michal Vasko004d3152020-06-11 19:59:22 +02009197 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009198 LY_CHECK_GOTO(rc, cleanup);
9199
9200 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
9201 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02009202 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009203
Michal Vasko004d3152020-06-11 19:59:22 +02009204 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_COMP);
aPiecek8b0cc152021-05-31 16:40:31 +02009205 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02009206 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02009207 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009208
aPiecek8b0cc152021-05-31 16:40:31 +02009209 if (options & LYXP_SKIP_EXPR) {
9210 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009211 LY_CHECK_GOTO(rc, cleanup);
9212 continue;
9213 }
9214
9215 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02009216 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009217 LY_CHECK_GOTO(rc, cleanup);
9218
9219 /* eval */
9220 if (options & LYXP_SCNODE_ALL) {
9221 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01009222 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02009223 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009224 } else {
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02009225 ly_bool result;
9226
9227 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], &result);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009228 LY_CHECK_GOTO(rc, cleanup);
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02009229 set_fill_boolean(set, result);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009230 }
9231 }
9232
9233cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02009234 lyxp_set_free_content(&orig_set);
9235 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009236 return rc;
9237}
9238
9239/**
9240 * @brief Evaluate EqualityExpr. Logs directly on error.
9241 *
Michal Vaskod3678892020-05-21 10:06:58 +02009242 * [15] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02009243 * | EqualityExpr '!=' RelationalExpr
9244 *
9245 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02009246 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009247 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02009248 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009249 * @param[in] options XPath options.
9250 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
9251 */
9252static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02009253eval_equality_expr(const struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t repeat, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02009254{
9255 LY_ERR rc;
Michal Vaskodd528af2022-08-08 14:35:07 +02009256 uint32_t i, this_op;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009257 struct lyxp_set orig_set, set2;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009258
9259 assert(repeat);
9260
9261 set_init(&orig_set, set);
9262 set_init(&set2, set);
9263
9264 set_fill_set(&orig_set, set);
9265
Michal Vasko004d3152020-06-11 19:59:22 +02009266 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009267 LY_CHECK_GOTO(rc, cleanup);
9268
9269 /* ('=' / '!=' RelationalExpr)* */
9270 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02009271 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009272
Michal Vasko004d3152020-06-11 19:59:22 +02009273 assert((exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_EQUAL) || (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_NEQUAL));
aPiecek8b0cc152021-05-31 16:40:31 +02009274 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02009275 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02009276 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009277
aPiecek8b0cc152021-05-31 16:40:31 +02009278 if (options & LYXP_SKIP_EXPR) {
9279 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009280 LY_CHECK_GOTO(rc, cleanup);
9281 continue;
9282 }
9283
9284 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02009285 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009286 LY_CHECK_GOTO(rc, cleanup);
9287
9288 /* eval */
9289 if (options & LYXP_SCNODE_ALL) {
9290 warn_operands(set->ctx, set, &set2, 0, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vasko004d3152020-06-11 19:59:22 +02009291 warn_equality_value(exp, set, *tok_idx - 1, this_op - 1, *tok_idx - 1);
9292 warn_equality_value(exp, &set2, this_op - 1, this_op - 1, *tok_idx - 1);
Michal Vaskoecd62de2019-11-13 12:35:11 +01009293 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02009294 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009295 } else {
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02009296 ly_bool result;
9297
9298 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], &result);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009299 LY_CHECK_GOTO(rc, cleanup);
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02009300 set_fill_boolean(set, result);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009301 }
9302 }
9303
9304cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02009305 lyxp_set_free_content(&orig_set);
9306 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009307 return rc;
9308}
9309
9310/**
9311 * @brief Evaluate AndExpr. Logs directly on error.
9312 *
Michal Vaskod3678892020-05-21 10:06:58 +02009313 * [14] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02009314 *
9315 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02009316 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009317 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02009318 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009319 * @param[in] options XPath options.
9320 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
9321 */
9322static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02009323eval_and_expr(const struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t repeat, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02009324{
9325 LY_ERR rc;
9326 struct lyxp_set orig_set, set2;
Michal Vaskodd528af2022-08-08 14:35:07 +02009327 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009328
9329 assert(repeat);
9330
9331 set_init(&orig_set, set);
9332 set_init(&set2, set);
9333
9334 set_fill_set(&orig_set, set);
9335
Michal Vasko004d3152020-06-11 19:59:22 +02009336 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009337 LY_CHECK_GOTO(rc, cleanup);
9338
Michal Vasko3ff82dd2023-05-09 14:09:21 +02009339 if (!(options & LYXP_SKIP_EXPR)) {
9340 if (options & LYXP_SCNODE_ALL) {
9341 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
9342 } else {
9343 /* cast to boolean, we know that will be the final result */
9344 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
9345 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02009346 }
9347
9348 /* ('and' EqualityExpr)* */
9349 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02009350 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
aPiecek8b0cc152021-05-31 16:40:31 +02009351 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, ((options & LYXP_SKIP_EXPR) || !set->val.bln ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02009352 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02009353 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009354
9355 /* lazy evaluation */
aPiecek8b0cc152021-05-31 16:40:31 +02009356 if ((options & LYXP_SKIP_EXPR) || ((set->type == LYXP_SET_BOOLEAN) && !set->val.bln)) {
9357 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009358 LY_CHECK_GOTO(rc, cleanup);
9359 continue;
9360 }
9361
9362 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02009363 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009364 LY_CHECK_GOTO(rc, cleanup);
9365
9366 /* eval - just get boolean value actually */
9367 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vasko1a09b212021-05-06 13:00:10 +02009368 set_scnode_clear_ctx(&set2, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskoecd62de2019-11-13 12:35:11 +01009369 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009370 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009371 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009372 set_fill_set(set, &set2);
9373 }
9374 }
9375
9376cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02009377 lyxp_set_free_content(&orig_set);
9378 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009379 return rc;
9380}
9381
9382/**
9383 * @brief Evaluate OrExpr. Logs directly on error.
9384 *
Michal Vaskod3678892020-05-21 10:06:58 +02009385 * [13] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02009386 *
9387 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02009388 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009389 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02009390 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009391 * @param[in] options XPath options.
9392 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
9393 */
9394static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02009395eval_or_expr(const struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t repeat, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02009396{
9397 LY_ERR rc;
9398 struct lyxp_set orig_set, set2;
Michal Vaskodd528af2022-08-08 14:35:07 +02009399 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009400
9401 assert(repeat);
9402
9403 set_init(&orig_set, set);
9404 set_init(&set2, set);
9405
9406 set_fill_set(&orig_set, set);
9407
Michal Vasko004d3152020-06-11 19:59:22 +02009408 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009409 LY_CHECK_GOTO(rc, cleanup);
9410
Michal Vasko3ff82dd2023-05-09 14:09:21 +02009411 if (!(options & LYXP_SKIP_EXPR)) {
9412 if (options & LYXP_SCNODE_ALL) {
9413 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
9414 } else {
9415 /* cast to boolean, we know that will be the final result */
9416 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
9417 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02009418 }
9419
9420 /* ('or' AndExpr)* */
9421 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02009422 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
aPiecek8b0cc152021-05-31 16:40:31 +02009423 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, ((options & LYXP_SKIP_EXPR) || set->val.bln ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02009424 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02009425 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009426
9427 /* lazy evaluation */
aPiecek8b0cc152021-05-31 16:40:31 +02009428 if ((options & LYXP_SKIP_EXPR) || ((set->type == LYXP_SET_BOOLEAN) && set->val.bln)) {
9429 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009430 LY_CHECK_GOTO(rc, cleanup);
9431 continue;
9432 }
9433
9434 set_fill_set(&set2, &orig_set);
9435 /* expr_type cound have been LYXP_EXPR_NONE in all these later calls (except for the first one),
9436 * but it does not matter */
Michal Vasko004d3152020-06-11 19:59:22 +02009437 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009438 LY_CHECK_GOTO(rc, cleanup);
9439
9440 /* eval - just get boolean value actually */
9441 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vasko1a09b212021-05-06 13:00:10 +02009442 set_scnode_clear_ctx(&set2, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskoecd62de2019-11-13 12:35:11 +01009443 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009444 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009445 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009446 set_fill_set(set, &set2);
9447 }
9448 }
9449
9450cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02009451 lyxp_set_free_content(&orig_set);
9452 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009453 return rc;
9454}
9455
9456/**
Michal Vasko004d3152020-06-11 19:59:22 +02009457 * @brief Decide what expression is at the pointer @p tok_idx and evaluate it accordingly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009458 *
9459 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02009460 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009461 * @param[in] etype Expression type to evaluate.
aPiecek8b0cc152021-05-31 16:40:31 +02009462 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009463 * @param[in] options XPath options.
9464 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
9465 */
9466static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02009467eval_expr_select(const struct lyxp_expr *exp, uint32_t *tok_idx, enum lyxp_expr_type etype, struct lyxp_set *set,
Michal Vasko40308e72020-10-20 16:38:40 +02009468 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02009469{
Michal Vaskodd528af2022-08-08 14:35:07 +02009470 uint32_t i, count;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009471 enum lyxp_expr_type next_etype;
9472 LY_ERR rc;
9473
9474 /* process operator repeats */
Michal Vasko004d3152020-06-11 19:59:22 +02009475 if (!exp->repeat[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02009476 next_etype = LYXP_EXPR_NONE;
9477 } else {
9478 /* find etype repeat */
Radek Krejci1e008d22020-08-17 11:37:37 +02009479 for (i = 0; exp->repeat[*tok_idx][i] > etype; ++i) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02009480
9481 /* select one-priority lower because etype expression called us */
9482 if (i) {
Michal Vasko004d3152020-06-11 19:59:22 +02009483 next_etype = exp->repeat[*tok_idx][i - 1];
Michal Vasko03ff5a72019-09-11 13:49:33 +02009484 /* count repeats for that expression */
Radek Krejci1e008d22020-08-17 11:37:37 +02009485 for (count = 0; i && exp->repeat[*tok_idx][i - 1] == next_etype; ++count, --i) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02009486 } else {
9487 next_etype = LYXP_EXPR_NONE;
9488 }
9489 }
9490
9491 /* decide what expression are we parsing based on the repeat */
9492 switch (next_etype) {
9493 case LYXP_EXPR_OR:
Michal Vasko004d3152020-06-11 19:59:22 +02009494 rc = eval_or_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009495 break;
9496 case LYXP_EXPR_AND:
Michal Vasko004d3152020-06-11 19:59:22 +02009497 rc = eval_and_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009498 break;
9499 case LYXP_EXPR_EQUALITY:
Michal Vasko004d3152020-06-11 19:59:22 +02009500 rc = eval_equality_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009501 break;
9502 case LYXP_EXPR_RELATIONAL:
Michal Vasko004d3152020-06-11 19:59:22 +02009503 rc = eval_relational_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009504 break;
9505 case LYXP_EXPR_ADDITIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02009506 rc = eval_additive_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009507 break;
9508 case LYXP_EXPR_MULTIPLICATIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02009509 rc = eval_multiplicative_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009510 break;
9511 case LYXP_EXPR_UNARY:
Michal Vasko004d3152020-06-11 19:59:22 +02009512 rc = eval_unary_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009513 break;
9514 case LYXP_EXPR_UNION:
Michal Vasko004d3152020-06-11 19:59:22 +02009515 rc = eval_union_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009516 break;
9517 case LYXP_EXPR_NONE:
Michal Vasko004d3152020-06-11 19:59:22 +02009518 rc = eval_path_expr(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009519 break;
9520 default:
9521 LOGINT_RET(set->ctx);
9522 }
9523
9524 return rc;
9525}
9526
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009527/**
9528 * @brief Get root type.
9529 *
9530 * @param[in] ctx_node Context node.
9531 * @param[in] ctx_scnode Schema context node.
9532 * @param[in] options XPath options.
9533 * @return Root type.
9534 */
9535static enum lyxp_node_type
Radek Krejci1deb5be2020-08-26 16:43:36 +02009536lyxp_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 +01009537{
Michal Vasko6b26e742020-07-17 15:02:10 +02009538 const struct lysc_node *op;
9539
Michal Vaskoa27245c2022-05-02 09:01:35 +02009540 /* explicit */
9541 if (options & LYXP_ACCESS_TREE_ALL) {
9542 return LYXP_NODE_ROOT;
9543 } else if (options & LYXP_ACCESS_TREE_CONFIG) {
9544 return LYXP_NODE_ROOT_CONFIG;
9545 }
9546
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009547 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009548 /* schema */
Radek Krejci1e008d22020-08-17 11:37:37 +02009549 for (op = ctx_scnode; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02009550
9551 if (op || (options & LYXP_SCNODE)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009552 /* general root that can access everything */
9553 return LYXP_NODE_ROOT;
9554 } else if (!ctx_scnode || (ctx_scnode->flags & LYS_CONFIG_W)) {
9555 /* root context node can access only config data (because we said so, it is unspecified) */
9556 return LYXP_NODE_ROOT_CONFIG;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009557 }
Michal Vasko6b26e742020-07-17 15:02:10 +02009558 return LYXP_NODE_ROOT;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009559 }
9560
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009561 /* data */
Michal Vasko6b26e742020-07-17 15:02:10 +02009562 op = ctx_node ? ctx_node->schema : NULL;
Michal Vaskod989ba02020-08-24 10:59:24 +02009563 for ( ; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02009564
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009565 if (op || !(options & LYXP_SCHEMA)) {
9566 /* general root that can access everything */
9567 return LYXP_NODE_ROOT;
Christian Hoppsb6ecaea2021-02-06 09:45:38 -05009568 } else if (!ctx_node || !ctx_node->schema || (ctx_node->schema->flags & LYS_CONFIG_W)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009569 /* root context node can access only config data (because we said so, it is unspecified) */
9570 return LYXP_NODE_ROOT_CONFIG;
9571 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009572 return LYXP_NODE_ROOT;
9573}
9574
Michal Vasko03ff5a72019-09-11 13:49:33 +02009575LY_ERR
Michal Vasko400e9672021-01-11 13:39:17 +01009576lyxp_eval(const struct ly_ctx *ctx, const struct lyxp_expr *exp, const struct lys_module *cur_mod,
Michal Vaskoa3e92bc2022-07-29 14:56:23 +02009577 LY_VALUE_FORMAT format, void *prefix_data, const struct lyd_node *cur_node, const struct lyd_node *ctx_node,
9578 const struct lyd_node *tree, const struct lyxp_var *vars, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02009579{
Michal Vaskodd528af2022-08-08 14:35:07 +02009580 uint32_t tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009581 LY_ERR rc;
9582
Michal Vasko400e9672021-01-11 13:39:17 +01009583 LY_CHECK_ARG_RET(ctx, ctx, exp, set, LY_EINVAL);
Radek Krejci8df109d2021-04-23 12:19:08 +02009584 if (!cur_mod && ((format == LY_VALUE_SCHEMA) || (format == LY_VALUE_SCHEMA_RESOLVED))) {
Michal Vaskoddd76592022-01-17 13:34:48 +01009585 LOGERR(ctx, LY_EINVAL, "Current module must be set if schema format is used.");
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009586 return LY_EINVAL;
Michal Vasko004d3152020-06-11 19:59:22 +02009587 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02009588
Michal Vaskod3bb12f2020-12-04 14:33:09 +01009589 if (tree) {
9590 /* adjust the pointer to be the first top-level sibling */
9591 while (tree->parent) {
9592 tree = lyd_parent(tree);
9593 }
9594 tree = lyd_first_sibling(tree);
Michal Vaskoddd76592022-01-17 13:34:48 +01009595
Michal Vasko6f78a772022-11-10 08:13:52 +01009596 if (lysc_data_parent(tree->schema)) {
Michal Vaskoddd76592022-01-17 13:34:48 +01009597 /* unable to evaluate absolute paths */
9598 LOGERR(ctx, LY_EINVAL, "Data node \"%s\" has no parent but is not instance of a top-level schema node.",
9599 LYD_NAME(tree));
9600 return LY_EINVAL;
9601 }
Michal Vaskod3bb12f2020-12-04 14:33:09 +01009602 }
9603
Michal Vasko03ff5a72019-09-11 13:49:33 +02009604 /* prepare set for evaluation */
Michal Vasko03ff5a72019-09-11 13:49:33 +02009605 memset(set, 0, sizeof *set);
Michal Vaskod3678892020-05-21 10:06:58 +02009606 set->type = LYXP_SET_NODE_SET;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009607 set->root_type = lyxp_get_root_type(ctx_node, NULL, options);
9608 set_insert_node(set, (struct lyd_node *)ctx_node, 0, ctx_node ? LYXP_NODE_ELEM : set->root_type, 0);
9609
Michal Vasko400e9672021-01-11 13:39:17 +01009610 set->ctx = (struct ly_ctx *)ctx;
Michal Vaskoa3e92bc2022-07-29 14:56:23 +02009611 set->cur_node = cur_node;
9612 for (set->context_op = cur_node ? cur_node->schema : NULL;
Radek Krejci0f969882020-08-21 16:56:47 +02009613 set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF));
9614 set->context_op = set->context_op->parent) {}
Michal Vaskof03ed032020-03-04 13:31:44 +01009615 set->tree = tree;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009616 set->cur_mod = cur_mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009617 set->format = format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009618 set->prefix_data = prefix_data;
aPiecekfba75362021-10-07 12:39:48 +02009619 set->vars = vars;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009620
Radek Krejciddace2c2021-01-08 11:30:56 +01009621 LOG_LOCSET(NULL, set->cur_node, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01009622
Michal Vasko03ff5a72019-09-11 13:49:33 +02009623 /* evaluate */
Michal Vasko004d3152020-06-11 19:59:22 +02009624 rc = eval_expr_select(exp, &tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009625 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02009626 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009627 }
9628
Michal Vasko4a7d4d62021-12-13 17:05:06 +01009629 if (set->cur_node) {
9630 LOG_LOCBACK(0, 1, 0, 0);
9631 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02009632 return rc;
9633}
9634
9635#if 0
9636
9637/* full xml printing of set elements, not used currently */
9638
9639void
9640lyxp_set_print_xml(FILE *f, struct lyxp_set *set)
9641{
9642 uint32_t i;
9643 char *str_num;
9644 struct lyout out;
9645
9646 memset(&out, 0, sizeof out);
9647
9648 out.type = LYOUT_STREAM;
9649 out.method.f = f;
9650
9651 switch (set->type) {
9652 case LYXP_SET_EMPTY:
Michal Vasko5233e962020-08-14 14:26:20 +02009653 ly_print_(&out, "Empty XPath set\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009654 break;
9655 case LYXP_SET_BOOLEAN:
Michal Vasko5233e962020-08-14 14:26:20 +02009656 ly_print_(&out, "Boolean XPath set:\n");
9657 ly_print_(&out, "%s\n\n", set->value.bool ? "true" : "false");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009658 break;
9659 case LYXP_SET_STRING:
Michal Vasko5233e962020-08-14 14:26:20 +02009660 ly_print_(&out, "String XPath set:\n");
9661 ly_print_(&out, "\"%s\"\n\n", set->value.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009662 break;
9663 case LYXP_SET_NUMBER:
Michal Vasko5233e962020-08-14 14:26:20 +02009664 ly_print_(&out, "Number XPath set:\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009665
9666 if (isnan(set->value.num)) {
9667 str_num = strdup("NaN");
9668 } else if ((set->value.num == 0) || (set->value.num == -0.0f)) {
9669 str_num = strdup("0");
9670 } else if (isinf(set->value.num) && !signbit(set->value.num)) {
9671 str_num = strdup("Infinity");
9672 } else if (isinf(set->value.num) && signbit(set->value.num)) {
9673 str_num = strdup("-Infinity");
9674 } else if ((long long)set->value.num == set->value.num) {
9675 if (asprintf(&str_num, "%lld", (long long)set->value.num) == -1) {
9676 str_num = NULL;
9677 }
9678 } else {
9679 if (asprintf(&str_num, "%03.1Lf", set->value.num) == -1) {
9680 str_num = NULL;
9681 }
9682 }
9683 if (!str_num) {
9684 LOGMEM;
9685 return;
9686 }
Michal Vasko5233e962020-08-14 14:26:20 +02009687 ly_print_(&out, "%s\n\n", str_num);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009688 free(str_num);
9689 break;
9690 case LYXP_SET_NODE_SET:
Michal Vasko5233e962020-08-14 14:26:20 +02009691 ly_print_(&out, "Node XPath set:\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009692
9693 for (i = 0; i < set->used; ++i) {
Michal Vasko5233e962020-08-14 14:26:20 +02009694 ly_print_(&out, "%d. ", i + 1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009695 switch (set->node_type[i]) {
9696 case LYXP_NODE_ROOT_ALL:
Michal Vasko5233e962020-08-14 14:26:20 +02009697 ly_print_(&out, "ROOT all\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009698 break;
9699 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko5233e962020-08-14 14:26:20 +02009700 ly_print_(&out, "ROOT config\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009701 break;
9702 case LYXP_NODE_ROOT_STATE:
Michal Vasko5233e962020-08-14 14:26:20 +02009703 ly_print_(&out, "ROOT state\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009704 break;
9705 case LYXP_NODE_ROOT_NOTIF:
Michal Vasko5233e962020-08-14 14:26:20 +02009706 ly_print_(&out, "ROOT notification \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009707 break;
9708 case LYXP_NODE_ROOT_RPC:
Michal Vasko5233e962020-08-14 14:26:20 +02009709 ly_print_(&out, "ROOT rpc \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009710 break;
9711 case LYXP_NODE_ROOT_OUTPUT:
Michal Vasko5233e962020-08-14 14:26:20 +02009712 ly_print_(&out, "ROOT output \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009713 break;
9714 case LYXP_NODE_ELEM:
Michal Vasko5233e962020-08-14 14:26:20 +02009715 ly_print_(&out, "ELEM \"%s\"\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009716 xml_print_node(&out, 1, set->value.nodes[i], 1, LYP_FORMAT);
Michal Vasko5233e962020-08-14 14:26:20 +02009717 ly_print_(&out, "\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009718 break;
9719 case LYXP_NODE_TEXT:
Michal Vasko5233e962020-08-14 14:26:20 +02009720 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 +02009721 break;
9722 case LYXP_NODE_ATTR:
Michal Vasko5233e962020-08-14 14:26:20 +02009723 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 +02009724 break;
9725 }
9726 }
9727 break;
9728 }
9729}
9730
9731#endif
9732
9733LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009734lyxp_set_cast(struct lyxp_set *set, enum lyxp_set_type target)
Michal Vasko03ff5a72019-09-11 13:49:33 +02009735{
9736 long double num;
9737 char *str;
9738 LY_ERR rc;
9739
9740 if (!set || (set->type == target)) {
9741 return LY_SUCCESS;
9742 }
9743
9744 /* it's not possible to convert anything into a node set */
Michal Vaskod3678892020-05-21 10:06:58 +02009745 assert(target != LYXP_SET_NODE_SET);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009746
9747 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vaskod3678892020-05-21 10:06:58 +02009748 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009749 return LY_EINVAL;
9750 }
9751
9752 /* to STRING */
Michal Vaskod3678892020-05-21 10:06:58 +02009753 if ((target == LYXP_SET_STRING) || ((target == LYXP_SET_NUMBER) && (set->type == LYXP_SET_NODE_SET))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02009754 switch (set->type) {
9755 case LYXP_SET_NUMBER:
9756 if (isnan(set->val.num)) {
9757 set->val.str = strdup("NaN");
9758 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
9759 } else if ((set->val.num == 0) || (set->val.num == -0.0f)) {
9760 set->val.str = strdup("0");
9761 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
9762 } else if (isinf(set->val.num) && !signbit(set->val.num)) {
9763 set->val.str = strdup("Infinity");
9764 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
9765 } else if (isinf(set->val.num) && signbit(set->val.num)) {
9766 set->val.str = strdup("-Infinity");
9767 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
9768 } else if ((long long)set->val.num == set->val.num) {
9769 if (asprintf(&str, "%lld", (long long)set->val.num) == -1) {
9770 LOGMEM_RET(set->ctx);
9771 }
9772 set->val.str = str;
9773 } else {
9774 if (asprintf(&str, "%03.1Lf", set->val.num) == -1) {
9775 LOGMEM_RET(set->ctx);
9776 }
9777 set->val.str = str;
9778 }
9779 break;
9780 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02009781 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02009782 set->val.str = strdup("true");
9783 } else {
9784 set->val.str = strdup("false");
9785 }
9786 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM);
9787 break;
9788 case LYXP_SET_NODE_SET:
Michal Vasko03ff5a72019-09-11 13:49:33 +02009789 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009790 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02009791
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009792 rc = cast_node_set_to_string(set, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009793 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02009794 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009795 set->val.str = str;
9796 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009797 default:
9798 LOGINT_RET(set->ctx);
9799 }
9800 set->type = LYXP_SET_STRING;
9801 }
9802
9803 /* to NUMBER */
9804 if (target == LYXP_SET_NUMBER) {
9805 switch (set->type) {
9806 case LYXP_SET_STRING:
9807 num = cast_string_to_number(set->val.str);
Michal Vaskod3678892020-05-21 10:06:58 +02009808 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009809 set->val.num = num;
9810 break;
9811 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02009812 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02009813 set->val.num = 1;
9814 } else {
9815 set->val.num = 0;
9816 }
9817 break;
9818 default:
9819 LOGINT_RET(set->ctx);
9820 }
9821 set->type = LYXP_SET_NUMBER;
9822 }
9823
9824 /* to BOOLEAN */
9825 if (target == LYXP_SET_BOOLEAN) {
9826 switch (set->type) {
9827 case LYXP_SET_NUMBER:
9828 if ((set->val.num == 0) || (set->val.num == -0.0f) || isnan(set->val.num)) {
Michal Vasko004d3152020-06-11 19:59:22 +02009829 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009830 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02009831 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009832 }
9833 break;
9834 case LYXP_SET_STRING:
9835 if (set->val.str[0]) {
Michal Vaskod3678892020-05-21 10:06:58 +02009836 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02009837 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009838 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02009839 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02009840 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009841 }
9842 break;
9843 case LYXP_SET_NODE_SET:
Michal Vaskod3678892020-05-21 10:06:58 +02009844 if (set->used) {
9845 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02009846 set->val.bln = 1;
Michal Vaskod3678892020-05-21 10:06:58 +02009847 } else {
9848 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02009849 set->val.bln = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02009850 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02009851 break;
9852 default:
9853 LOGINT_RET(set->ctx);
9854 }
9855 set->type = LYXP_SET_BOOLEAN;
9856 }
9857
Michal Vasko03ff5a72019-09-11 13:49:33 +02009858 return LY_SUCCESS;
9859}
9860
9861LY_ERR
Michal Vasko400e9672021-01-11 13:39:17 +01009862lyxp_atomize(const struct ly_ctx *ctx, const struct lyxp_expr *exp, const struct lys_module *cur_mod,
Michal Vaskoa3e92bc2022-07-29 14:56:23 +02009863 LY_VALUE_FORMAT format, void *prefix_data, const struct lysc_node *cur_scnode,
9864 const struct lysc_node *ctx_scnode, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02009865{
Radek Krejci2efc45b2020-12-22 16:25:44 +01009866 LY_ERR ret;
Michal Vaskodd528af2022-08-08 14:35:07 +02009867 uint32_t tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009868
Michal Vasko400e9672021-01-11 13:39:17 +01009869 LY_CHECK_ARG_RET(ctx, ctx, exp, set, LY_EINVAL);
Radek Krejci8df109d2021-04-23 12:19:08 +02009870 if (!cur_mod && ((format == LY_VALUE_SCHEMA) || (format == LY_VALUE_SCHEMA_RESOLVED))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009871 LOGARG(NULL, "Current module must be set if schema format is used.");
9872 return LY_EINVAL;
Michal Vasko004d3152020-06-11 19:59:22 +02009873 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02009874
9875 /* prepare set for evaluation */
Michal Vasko03ff5a72019-09-11 13:49:33 +02009876 memset(set, 0, sizeof *set);
9877 set->type = LYXP_SET_SCNODE_SET;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009878 set->root_type = lyxp_get_root_type(NULL, ctx_scnode, options);
Michal Vaskoe4a6d012023-05-22 14:34:52 +02009879 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, ctx_scnode, ctx_scnode ? LYXP_NODE_ELEM : set->root_type, LYXP_AXIS_SELF, NULL));
Radek Krejcif13b87b2020-12-01 22:02:17 +01009880 set->val.scnodes[0].in_ctx = LYXP_SET_SCNODE_START;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009881
Michal Vasko400e9672021-01-11 13:39:17 +01009882 set->ctx = (struct ly_ctx *)ctx;
Michal Vaskoa3e92bc2022-07-29 14:56:23 +02009883 set->cur_scnode = cur_scnode;
9884 for (set->context_op = cur_scnode;
Radek Krejci0f969882020-08-21 16:56:47 +02009885 set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF));
9886 set->context_op = set->context_op->parent) {}
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009887 set->cur_mod = cur_mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009888 set->format = format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009889 set->prefix_data = prefix_data;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009890
Radek Krejciddace2c2021-01-08 11:30:56 +01009891 LOG_LOCSET(set->cur_scnode, NULL, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01009892
Michal Vasko03ff5a72019-09-11 13:49:33 +02009893 /* evaluate */
Radek Krejci2efc45b2020-12-22 16:25:44 +01009894 ret = eval_expr_select(exp, &tok_idx, 0, set, options);
9895
Michal Vaskof69e4552022-12-02 10:27:42 +01009896 LOG_LOCBACK(set->cur_scnode ? 1 : 0, 0, 0, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +01009897 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009898}
Michal Vaskod43d71a2020-08-07 14:54:58 +02009899
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01009900LIBYANG_API_DEF const char *
Michal Vaskod43d71a2020-08-07 14:54:58 +02009901lyxp_get_expr(const struct lyxp_expr *path)
9902{
9903 if (!path) {
9904 return NULL;
9905 }
9906
9907 return path->expr;
9908}