blob: 66a9dbfe5c587071107d571ba3c201b1574aabb8 [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 Vasko7333cb32022-07-29 16:30:29 +020046static LY_ERR set_scnode_insert_node(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type,
47 enum lyxp_axis axis, uint32_t *index_p);
Michal Vaskodd528af2022-08-08 14:35:07 +020048static LY_ERR reparse_or_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t depth);
49static 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 +020050 struct lyxp_set *set, uint32_t options);
Michal Vaskofe1af042022-07-29 14:58:59 +020051static LY_ERR moveto_resolve_model(const char **qname, uint32_t *qname_len, const struct lyxp_set *set,
Michal Vasko93923692021-05-07 15:28:02 +020052 const struct lysc_node *ctx_scnode, const struct lys_module **moveto_mod);
Michal Vasko47da6562022-07-14 15:43:15 +020053static LY_ERR moveto_axis_node_next(const struct lyd_node **iter, enum lyxp_node_type *iter_type,
54 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 +020055static LY_ERR moveto_node(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname,
56 enum lyxp_axis axis, uint32_t options);
57static LY_ERR moveto_scnode(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname,
58 enum lyxp_axis axis, uint32_t options);
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +020059static 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 +020060
aPiecek96dc1e32021-10-08 15:45:59 +020061/* Functions are divided into the following basic classes:
62 *
63 * (re)parse functions:
64 * Parse functions parse the expression into
65 * tokens (syntactic analysis).
66 * Reparse functions perform semantic analysis
67 * (do not save the result, just a check) of
68 * the expression and fill repeat indices.
69 *
70 * warn functions:
71 * Warn functions check specific reasonable conditions for schema XPath
72 * and print a warning if they are not satisfied.
73 *
74 * moveto functions:
75 * They and only they actually change the context (set).
76 *
77 * eval functions:
78 * They execute a parsed XPath expression on some data subtree.
79 */
80
Michal Vasko03ff5a72019-09-11 13:49:33 +020081/**
82 * @brief Print the type of an XPath \p set.
83 *
84 * @param[in] set Set to use.
85 * @return Set type string.
86 */
87static const char *
88print_set_type(struct lyxp_set *set)
89{
90 switch (set->type) {
Michal Vasko03ff5a72019-09-11 13:49:33 +020091 case LYXP_SET_NODE_SET:
92 return "node set";
93 case LYXP_SET_SCNODE_SET:
94 return "schema node set";
95 case LYXP_SET_BOOLEAN:
96 return "boolean";
97 case LYXP_SET_NUMBER:
98 return "number";
99 case LYXP_SET_STRING:
100 return "string";
101 }
102
103 return NULL;
104}
105
Michal Vasko24cddf82020-06-01 08:17:01 +0200106const char *
Michal Vasko49fec8e2022-05-24 10:28:33 +0200107lyxp_token2str(enum lyxp_token tok)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200108{
109 switch (tok) {
110 case LYXP_TOKEN_PAR1:
111 return "(";
112 case LYXP_TOKEN_PAR2:
113 return ")";
114 case LYXP_TOKEN_BRACK1:
115 return "[";
116 case LYXP_TOKEN_BRACK2:
117 return "]";
118 case LYXP_TOKEN_DOT:
119 return ".";
120 case LYXP_TOKEN_DDOT:
121 return "..";
122 case LYXP_TOKEN_AT:
123 return "@";
124 case LYXP_TOKEN_COMMA:
125 return ",";
126 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 Vasko03ff5a72019-09-11 13:49:33 +0200150 case LYXP_TOKEN_LITERAL:
151 return "Literal";
152 case LYXP_TOKEN_NUMBER:
153 return "Number";
154 default:
155 LOGINT(NULL);
156 return "";
157 }
158}
159
160/**
Michal Vasko49fec8e2022-05-24 10:28:33 +0200161 * @brief Transform string into an axis.
162 *
163 * @param[in] str String to transform.
164 * @param[in] str_len Length of @p str.
165 * @return Transformed axis.
166 */
167static enum lyxp_axis
Michal Vaskodd528af2022-08-08 14:35:07 +0200168str2axis(const char *str, uint32_t str_len)
Michal Vasko49fec8e2022-05-24 10:28:33 +0200169{
170 switch (str_len) {
171 case 4:
172 assert(!strncmp("self", str, str_len));
173 return LYXP_AXIS_SELF;
174 case 5:
175 assert(!strncmp("child", str, str_len));
176 return LYXP_AXIS_CHILD;
177 case 6:
178 assert(!strncmp("parent", str, str_len));
179 return LYXP_AXIS_PARENT;
180 case 8:
181 assert(!strncmp("ancestor", str, str_len));
182 return LYXP_AXIS_ANCESTOR;
183 case 9:
184 if (str[0] == 'a') {
185 assert(!strncmp("attribute", str, str_len));
186 return LYXP_AXIS_ATTRIBUTE;
187 } else if (str[0] == 'f') {
188 assert(!strncmp("following", str, str_len));
189 return LYXP_AXIS_FOLLOWING;
190 } else {
191 assert(!strncmp("preceding", str, str_len));
192 return LYXP_AXIS_PRECEDING;
193 }
194 break;
195 case 10:
196 assert(!strncmp("descendant", str, str_len));
197 return LYXP_AXIS_DESCENDANT;
198 case 16:
199 assert(!strncmp("ancestor-or-self", str, str_len));
200 return LYXP_AXIS_ANCESTOR_OR_SELF;
201 case 17:
202 if (str[0] == 'f') {
203 assert(!strncmp("following-sibling", str, str_len));
204 return LYXP_AXIS_FOLLOWING_SIBLING;
205 } else {
206 assert(!strncmp("preceding-sibling", str, str_len));
207 return LYXP_AXIS_PRECEDING_SIBLING;
208 }
209 break;
210 case 18:
211 assert(!strncmp("descendant-or-self", str, str_len));
212 return LYXP_AXIS_DESCENDANT_OR_SELF;
213 }
214
215 LOGINT(NULL);
216 return 0;
217}
218
219/**
Michal Vasko03ff5a72019-09-11 13:49:33 +0200220 * @brief Print the whole expression \p exp to debug output.
221 *
222 * @param[in] exp Expression to use.
223 */
224static void
Michal Vasko40308e72020-10-20 16:38:40 +0200225print_expr_struct_debug(const struct lyxp_expr *exp)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200226{
Radek Krejcif13b87b2020-12-01 22:02:17 +0100227#define MSG_BUFFER_SIZE 128
228 char tmp[MSG_BUFFER_SIZE];
Michal Vasko1fdd8fa2021-01-08 09:21:45 +0100229 uint32_t i, j;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200230
Radek Krejci52b6d512020-10-12 12:33:17 +0200231 if (!exp || (ly_ll < LY_LLDBG)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200232 return;
233 }
234
235 LOGDBG(LY_LDGXPATH, "expression \"%s\":", exp->expr);
236 for (i = 0; i < exp->used; ++i) {
Michal Vasko49fec8e2022-05-24 10:28:33 +0200237 sprintf(tmp, "\ttoken %s, in expression \"%.*s\"", lyxp_token2str(exp->tokens[i]), exp->tok_len[i],
Michal Vasko69730152020-10-09 16:30:07 +0200238 &exp->expr[exp->tok_pos[i]]);
Michal Vasko23049552021-03-04 15:57:44 +0100239 if (exp->repeat && exp->repeat[i]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200240 sprintf(tmp + strlen(tmp), " (repeat %d", exp->repeat[i][0]);
241 for (j = 1; exp->repeat[i][j]; ++j) {
242 sprintf(tmp + strlen(tmp), ", %d", exp->repeat[i][j]);
243 }
244 strcat(tmp, ")");
245 }
246 LOGDBG(LY_LDGXPATH, tmp);
247 }
Radek Krejcif13b87b2020-12-01 22:02:17 +0100248#undef MSG_BUFFER_SIZE
Michal Vasko03ff5a72019-09-11 13:49:33 +0200249}
250
251#ifndef NDEBUG
252
253/**
254 * @brief Print XPath set content to debug output.
255 *
256 * @param[in] set Set to print.
257 */
258static void
259print_set_debug(struct lyxp_set *set)
260{
261 uint32_t i;
262 char *str;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200263 struct lyxp_set_node *item;
264 struct lyxp_set_scnode *sitem;
265
Radek Krejci52b6d512020-10-12 12:33:17 +0200266 if (ly_ll < LY_LLDBG) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200267 return;
268 }
269
270 switch (set->type) {
271 case LYXP_SET_NODE_SET:
272 LOGDBG(LY_LDGXPATH, "set NODE SET:");
273 for (i = 0; i < set->used; ++i) {
274 item = &set->val.nodes[i];
275
276 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +0100277 case LYXP_NODE_NONE:
278 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): NONE", i + 1, item->pos);
279 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200280 case LYXP_NODE_ROOT:
281 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT", i + 1, item->pos);
282 break;
283 case LYXP_NODE_ROOT_CONFIG:
284 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT CONFIG", i + 1, item->pos);
285 break;
286 case LYXP_NODE_ELEM:
Michal Vasko9e685082021-01-29 14:49:09 +0100287 if ((item->node->schema->nodetype == LYS_LIST) && (lyd_child(item->node)->schema->nodetype == LYS_LEAF)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200288 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (1st child val: %s)", i + 1, item->pos,
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200289 item->node->schema->name, lyd_get_value(lyd_child(item->node)));
Michal Vasko9e685082021-01-29 14:49:09 +0100290 } else if (item->node->schema->nodetype == LYS_LEAFLIST) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200291 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (val: %s)", i + 1, item->pos,
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200292 item->node->schema->name, lyd_get_value(item->node));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200293 } else {
294 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s", i + 1, item->pos, item->node->schema->name);
295 }
296 break;
297 case LYXP_NODE_TEXT:
298 if (item->node->schema->nodetype & LYS_ANYDATA) {
299 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): TEXT <%s>", i + 1, item->pos,
Michal Vasko69730152020-10-09 16:30:07 +0200300 item->node->schema->nodetype == LYS_ANYXML ? "anyxml" : "anydata");
Michal Vasko03ff5a72019-09-11 13:49:33 +0200301 } else {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200302 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 +0200303 }
304 break;
Michal Vasko9f96a052020-03-10 09:41:45 +0100305 case LYXP_NODE_META:
306 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 +0200307 set->val.meta[i].meta->value);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200308 break;
309 }
310 }
311 break;
312
313 case LYXP_SET_SCNODE_SET:
314 LOGDBG(LY_LDGXPATH, "set SCNODE SET:");
315 for (i = 0; i < set->used; ++i) {
316 sitem = &set->val.scnodes[i];
317
318 switch (sitem->type) {
319 case LYXP_NODE_ROOT:
320 LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT", i + 1, sitem->in_ctx);
321 break;
322 case LYXP_NODE_ROOT_CONFIG:
323 LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT CONFIG", i + 1, sitem->in_ctx);
324 break;
325 case LYXP_NODE_ELEM:
326 LOGDBG(LY_LDGXPATH, "\t%d (%u): ELEM %s", i + 1, sitem->in_ctx, sitem->scnode->name);
327 break;
328 default:
329 LOGINT(NULL);
330 break;
331 }
332 }
333 break;
334
Michal Vasko03ff5a72019-09-11 13:49:33 +0200335 case LYXP_SET_BOOLEAN:
336 LOGDBG(LY_LDGXPATH, "set BOOLEAN");
Michal Vasko004d3152020-06-11 19:59:22 +0200337 LOGDBG(LY_LDGXPATH, "\t%s", (set->val.bln ? "true" : "false"));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200338 break;
339
340 case LYXP_SET_STRING:
341 LOGDBG(LY_LDGXPATH, "set STRING");
342 LOGDBG(LY_LDGXPATH, "\t%s", set->val.str);
343 break;
344
345 case LYXP_SET_NUMBER:
346 LOGDBG(LY_LDGXPATH, "set NUMBER");
347
348 if (isnan(set->val.num)) {
349 str = strdup("NaN");
350 } else if ((set->val.num == 0) || (set->val.num == -0.0f)) {
351 str = strdup("0");
352 } else if (isinf(set->val.num) && !signbit(set->val.num)) {
353 str = strdup("Infinity");
354 } else if (isinf(set->val.num) && signbit(set->val.num)) {
355 str = strdup("-Infinity");
356 } else if ((long long)set->val.num == set->val.num) {
357 if (asprintf(&str, "%lld", (long long)set->val.num) == -1) {
358 str = NULL;
359 }
360 } else {
361 if (asprintf(&str, "%03.1Lf", set->val.num) == -1) {
362 str = NULL;
363 }
364 }
365 LY_CHECK_ERR_RET(!str, LOGMEM(NULL), );
366
367 LOGDBG(LY_LDGXPATH, "\t%s", str);
368 free(str);
369 }
370}
371
372#endif
373
374/**
375 * @brief Realloc the string \p str.
376 *
377 * @param[in] ctx libyang context for logging.
378 * @param[in] needed How much free space is required.
379 * @param[in,out] str Pointer to the string to use.
380 * @param[in,out] used Used bytes in \p str.
381 * @param[in,out] size Allocated bytes in \p str.
382 * @return LY_ERR
383 */
384static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +0200385cast_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 +0200386{
Michal Vasko2291ab92022-08-08 08:53:12 +0200387 if (*size - (unsigned)*used < needed) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200388 do {
Michal Vaskodd528af2022-08-08 14:35:07 +0200389 if ((UINT32_MAX - *size) < LYXP_STRING_CAST_SIZE_STEP) {
390 LOGERR(ctx, LY_EINVAL, "XPath string length limit (%" PRIu32 ") reached.", UINT32_MAX);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200391 return LY_EINVAL;
392 }
393 *size += LYXP_STRING_CAST_SIZE_STEP;
Michal Vasko2291ab92022-08-08 08:53:12 +0200394 } while (*size - (unsigned)*used < needed);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200395 *str = ly_realloc(*str, *size * sizeof(char));
396 LY_CHECK_ERR_RET(!(*str), LOGMEM(ctx), LY_EMEM);
397 }
398
399 return LY_SUCCESS;
400}
401
402/**
403 * @brief Cast nodes recursively to one string @p str.
404 *
Michal Vasko47da6562022-07-14 15:43:15 +0200405 * @param[in] node Node to cast, NULL if root.
406 * @param[in] set XPath set.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200407 * @param[in] indent Current indent.
408 * @param[in,out] str Resulting string.
409 * @param[in,out] used Used bytes in @p str.
410 * @param[in,out] size Allocated bytes in @p str.
Michal Vasko47da6562022-07-14 15:43:15 +0200411 * @return LY_ERR value.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200412 */
413static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +0200414cast_string_recursive(const struct lyd_node *node, struct lyxp_set *set, uint32_t indent, char **str, uint32_t *used,
415 uint32_t *size)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200416{
Radek Krejci7f769d72020-07-11 23:13:56 +0200417 char *buf, *line, *ptr = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200418 const char *value_str;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200419 const struct lyd_node *child;
Michal Vasko47da6562022-07-14 15:43:15 +0200420 enum lyxp_node_type child_type;
Michal Vasko60ea6352020-06-29 13:39:39 +0200421 struct lyd_node *tree;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200422 struct lyd_node_any *any;
423 LY_ERR rc;
424
Michal Vasko47da6562022-07-14 15:43:15 +0200425 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && node && (node->schema->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200426 return LY_SUCCESS;
427 }
428
Michal Vasko47da6562022-07-14 15:43:15 +0200429 if (!node) {
430 /* fake container */
431 LY_CHECK_RET(cast_string_realloc(set->ctx, 1, str, used, size));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200432 strcpy(*str + (*used - 1), "\n");
433 ++(*used);
434
435 ++indent;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200436
Michal Vasko47da6562022-07-14 15:43:15 +0200437 /* print all the top-level nodes */
438 child = NULL;
439 child_type = 0;
440 while (!moveto_axis_node_next(&child, &child_type, NULL, set->root_type, LYXP_AXIS_CHILD, set)) {
441 LY_CHECK_RET(cast_string_recursive(child, set, indent, str, used, size));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200442 }
443
Michal Vasko47da6562022-07-14 15:43:15 +0200444 /* end fake container */
445 LY_CHECK_RET(cast_string_realloc(set->ctx, 1, str, used, size));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200446 strcpy(*str + (*used - 1), "\n");
447 ++(*used);
448
449 --indent;
Michal Vasko47da6562022-07-14 15:43:15 +0200450 } else {
451 switch (node->schema->nodetype) {
452 case LYS_CONTAINER:
453 case LYS_LIST:
454 case LYS_RPC:
455 case LYS_NOTIF:
456 LY_CHECK_RET(cast_string_realloc(set->ctx, 1, str, used, size));
457 strcpy(*str + (*used - 1), "\n");
458 ++(*used);
459
460 for (child = lyd_child(node); child; child = child->next) {
461 LY_CHECK_RET(cast_string_recursive(child, set, indent + 1, str, used, size));
462 }
463
464 break;
465
466 case LYS_LEAF:
467 case LYS_LEAFLIST:
468 value_str = lyd_get_value(node);
469
470 /* print indent */
471 LY_CHECK_RET(cast_string_realloc(set->ctx, indent * 2 + strlen(value_str) + 1, str, used, size));
472 memset(*str + (*used - 1), ' ', indent * 2);
473 *used += indent * 2;
474
475 /* print value */
476 if (*used == 1) {
477 sprintf(*str + (*used - 1), "%s", value_str);
478 *used += strlen(value_str);
479 } else {
480 sprintf(*str + (*used - 1), "%s\n", value_str);
481 *used += strlen(value_str) + 1;
482 }
483
484 break;
485
486 case LYS_ANYXML:
487 case LYS_ANYDATA:
488 any = (struct lyd_node_any *)node;
489 if (!(void *)any->value.tree) {
490 /* no content */
491 buf = strdup("");
492 LY_CHECK_ERR_RET(!buf, LOGMEM(set->ctx), LY_EMEM);
493 } else {
494 struct ly_out *out;
495
496 if (any->value_type == LYD_ANYDATA_LYB) {
497 /* try to parse it into a data tree */
498 if (lyd_parse_data_mem((struct ly_ctx *)set->ctx, any->value.mem, LYD_LYB,
499 LYD_PARSE_ONLY | LYD_PARSE_STRICT, 0, &tree) == LY_SUCCESS) {
500 /* successfully parsed */
501 free(any->value.mem);
502 any->value.tree = tree;
503 any->value_type = LYD_ANYDATA_DATATREE;
504 }
505 /* error is covered by the following switch where LYD_ANYDATA_LYB causes failure */
506 }
507
508 switch (any->value_type) {
509 case LYD_ANYDATA_STRING:
510 case LYD_ANYDATA_XML:
511 case LYD_ANYDATA_JSON:
512 buf = strdup(any->value.json);
513 LY_CHECK_ERR_RET(!buf, LOGMEM(set->ctx), LY_EMEM);
514 break;
515 case LYD_ANYDATA_DATATREE:
516 LY_CHECK_RET(ly_out_new_memory(&buf, 0, &out));
517 rc = lyd_print_all(out, any->value.tree, LYD_XML, 0);
518 ly_out_free(out, NULL, 0);
519 LY_CHECK_RET(rc < 0, -rc);
520 break;
521 case LYD_ANYDATA_LYB:
522 LOGERR(set->ctx, LY_EINVAL, "Cannot convert LYB anydata into string.");
523 return LY_EINVAL;
524 }
525 }
526
527 line = strtok_r(buf, "\n", &ptr);
528 do {
529 rc = cast_string_realloc(set->ctx, indent * 2 + strlen(line) + 1, str, used, size);
530 if (rc != LY_SUCCESS) {
531 free(buf);
532 return rc;
533 }
534 memset(*str + (*used - 1), ' ', indent * 2);
535 *used += indent * 2;
536
537 strcpy(*str + (*used - 1), line);
538 *used += strlen(line);
539
540 strcpy(*str + (*used - 1), "\n");
541 *used += 1;
542 } while ((line = strtok_r(NULL, "\n", &ptr)));
543
544 free(buf);
545 break;
546
547 default:
548 LOGINT_RET(set->ctx);
549 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200550 }
551
552 return LY_SUCCESS;
553}
554
555/**
556 * @brief Cast an element into a string.
557 *
Michal Vasko47da6562022-07-14 15:43:15 +0200558 * @param[in] node Node to cast, NULL if root.
559 * @param[in] set XPath set.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200560 * @param[out] str Element cast to dynamically-allocated string.
561 * @return LY_ERR
562 */
563static LY_ERR
Michal Vasko47da6562022-07-14 15:43:15 +0200564cast_string_elem(const struct lyd_node *node, struct lyxp_set *set, char **str)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200565{
Michal Vaskodd528af2022-08-08 14:35:07 +0200566 uint32_t used, size;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200567 LY_ERR rc;
568
569 *str = malloc(LYXP_STRING_CAST_SIZE_START * sizeof(char));
Michal Vasko47da6562022-07-14 15:43:15 +0200570 LY_CHECK_ERR_RET(!*str, LOGMEM(set->ctx), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200571 (*str)[0] = '\0';
572 used = 1;
573 size = LYXP_STRING_CAST_SIZE_START;
574
Michal Vasko47da6562022-07-14 15:43:15 +0200575 rc = cast_string_recursive(node, set, 0, str, &used, &size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200576 if (rc != LY_SUCCESS) {
577 free(*str);
578 return rc;
579 }
580
581 if (size > used) {
582 *str = ly_realloc(*str, used * sizeof(char));
Michal Vasko47da6562022-07-14 15:43:15 +0200583 LY_CHECK_ERR_RET(!*str, LOGMEM(set->ctx), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200584 }
585 return LY_SUCCESS;
586}
587
588/**
589 * @brief Cast a LYXP_SET_NODE_SET set into a string.
590 * Context position aware.
591 *
592 * @param[in] set Set to cast.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200593 * @param[out] str Cast dynamically-allocated string.
594 * @return LY_ERR
595 */
596static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100597cast_node_set_to_string(struct lyxp_set *set, char **str)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200598{
Michal Vaskoaa677062021-03-09 13:52:53 +0100599 if (!set->used) {
600 *str = strdup("");
601 if (!*str) {
602 LOGMEM_RET(set->ctx);
603 }
604 return LY_SUCCESS;
605 }
606
Michal Vasko03ff5a72019-09-11 13:49:33 +0200607 switch (set->val.nodes[0].type) {
Michal Vasko2caefc12019-11-14 16:07:56 +0100608 case LYXP_NODE_NONE:
609 /* invalid */
610 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200611 case LYXP_NODE_ROOT:
612 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200613 case LYXP_NODE_ELEM:
614 case LYXP_NODE_TEXT:
Michal Vasko47da6562022-07-14 15:43:15 +0200615 return cast_string_elem(set->val.nodes[0].node, set, str);
Michal Vasko9f96a052020-03-10 09:41:45 +0100616 case LYXP_NODE_META:
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200617 *str = strdup(lyd_get_meta_value(set->val.meta[0].meta));
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200618 if (!*str) {
619 LOGMEM_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200620 }
621 return LY_SUCCESS;
622 }
623
624 LOGINT_RET(set->ctx);
625}
626
627/**
628 * @brief Cast a string into an XPath number.
629 *
630 * @param[in] str String to use.
631 * @return Cast number.
632 */
633static long double
634cast_string_to_number(const char *str)
635{
636 long double num;
637 char *ptr;
638
639 errno = 0;
640 num = strtold(str, &ptr);
641 if (errno || *ptr) {
642 num = NAN;
643 }
644 return num;
645}
646
647/**
648 * @brief Callback for checking value equality.
649 *
Michal Vasko62524a92021-02-26 10:08:50 +0100650 * Implementation of ::lyht_value_equal_cb.
Radek Krejci857189e2020-09-01 13:26:36 +0200651 *
Michal Vasko03ff5a72019-09-11 13:49:33 +0200652 * @param[in] val1_p First value.
653 * @param[in] val2_p Second value.
654 * @param[in] mod Whether hash table is being modified.
655 * @param[in] cb_data Callback data.
Radek Krejci857189e2020-09-01 13:26:36 +0200656 * @return Boolean value whether values are equal or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200657 */
Radek Krejci857189e2020-09-01 13:26:36 +0200658static ly_bool
659set_values_equal_cb(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void *UNUSED(cb_data))
Michal Vasko03ff5a72019-09-11 13:49:33 +0200660{
661 struct lyxp_set_hash_node *val1, *val2;
662
663 val1 = (struct lyxp_set_hash_node *)val1_p;
664 val2 = (struct lyxp_set_hash_node *)val2_p;
665
666 if ((val1->node == val2->node) && (val1->type == val2->type)) {
667 return 1;
668 }
669
670 return 0;
671}
672
673/**
674 * @brief Insert node and its hash into set.
675 *
676 * @param[in] set et to insert to.
677 * @param[in] node Node with hash.
678 * @param[in] type Node type.
679 */
680static void
681set_insert_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type)
682{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200683 LY_ERR r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200684 uint32_t i, hash;
685 struct lyxp_set_hash_node hnode;
686
687 if (!set->ht && (set->used >= LYD_HT_MIN_ITEMS)) {
688 /* create hash table and add all the nodes */
689 set->ht = lyht_new(1, sizeof(struct lyxp_set_hash_node), set_values_equal_cb, NULL, 1);
690 for (i = 0; i < set->used; ++i) {
691 hnode.node = set->val.nodes[i].node;
692 hnode.type = set->val.nodes[i].type;
693
694 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
695 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
696 hash = dict_hash_multi(hash, NULL, 0);
697
698 r = lyht_insert(set->ht, &hnode, hash, NULL);
699 assert(!r);
700 (void)r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200701
Michal Vasko9d6befd2019-12-11 14:56:56 +0100702 if (hnode.node == node) {
703 /* it was just added, do not add it twice */
704 node = NULL;
705 }
706 }
707 }
708
709 if (set->ht && node) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200710 /* add the new node into hash table */
711 hnode.node = node;
712 hnode.type = type;
713
714 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
715 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
716 hash = dict_hash_multi(hash, NULL, 0);
717
718 r = lyht_insert(set->ht, &hnode, hash, NULL);
719 assert(!r);
720 (void)r;
721 }
722}
723
724/**
725 * @brief Remove node and its hash from set.
726 *
727 * @param[in] set Set to remove from.
728 * @param[in] node Node to remove.
729 * @param[in] type Node type.
730 */
731static void
732set_remove_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type)
733{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200734 LY_ERR r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200735 struct lyxp_set_hash_node hnode;
736 uint32_t hash;
737
738 if (set->ht) {
739 hnode.node = node;
740 hnode.type = type;
741
742 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
743 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
744 hash = dict_hash_multi(hash, NULL, 0);
745
746 r = lyht_remove(set->ht, &hnode, hash);
747 assert(!r);
748 (void)r;
749
750 if (!set->ht->used) {
751 lyht_free(set->ht);
752 set->ht = NULL;
753 }
754 }
755}
756
757/**
758 * @brief Check whether node is in set based on its hash.
759 *
760 * @param[in] set Set to search in.
761 * @param[in] node Node to search for.
762 * @param[in] type Node type.
763 * @param[in] skip_idx Index in @p set to skip.
764 * @return LY_ERR
765 */
766static LY_ERR
767set_dup_node_hash_check(const struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type, int skip_idx)
768{
769 struct lyxp_set_hash_node hnode, *match_p;
770 uint32_t hash;
771
772 hnode.node = node;
773 hnode.type = type;
774
775 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
776 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
777 hash = dict_hash_multi(hash, NULL, 0);
778
779 if (!lyht_find(set->ht, &hnode, hash, (void **)&match_p)) {
780 if ((skip_idx > -1) && (set->val.nodes[skip_idx].node == match_p->node) && (set->val.nodes[skip_idx].type == match_p->type)) {
781 /* we found it on the index that should be skipped, find another */
782 hnode = *match_p;
783 if (lyht_find_next(set->ht, &hnode, hash, (void **)&match_p)) {
784 /* none other found */
785 return LY_SUCCESS;
786 }
787 }
788
789 return LY_EEXIST;
790 }
791
792 /* not found */
793 return LY_SUCCESS;
794}
795
Michal Vaskod3678892020-05-21 10:06:58 +0200796void
797lyxp_set_free_content(struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200798{
799 if (!set) {
800 return;
801 }
802
803 if (set->type == LYXP_SET_NODE_SET) {
804 free(set->val.nodes);
805 lyht_free(set->ht);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200806 } else if (set->type == LYXP_SET_SCNODE_SET) {
807 free(set->val.scnodes);
Michal Vaskod3678892020-05-21 10:06:58 +0200808 lyht_free(set->ht);
809 } else {
810 if (set->type == LYXP_SET_STRING) {
811 free(set->val.str);
812 }
813 set->type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200814 }
Michal Vaskod3678892020-05-21 10:06:58 +0200815
816 set->val.nodes = NULL;
817 set->used = 0;
818 set->size = 0;
819 set->ht = NULL;
820 set->ctx_pos = 0;
aPiecek748da732021-06-01 09:56:43 +0200821 set->ctx_size = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200822}
823
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100824/**
825 * @brief Free dynamically-allocated set.
826 *
827 * @param[in] set Set to free.
828 */
829static void
Michal Vasko03ff5a72019-09-11 13:49:33 +0200830lyxp_set_free(struct lyxp_set *set)
831{
832 if (!set) {
833 return;
834 }
835
Michal Vaskod3678892020-05-21 10:06:58 +0200836 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200837 free(set);
838}
839
840/**
841 * @brief Initialize set context.
842 *
843 * @param[in] new Set to initialize.
844 * @param[in] set Arbitrary initialized set.
845 */
846static void
Michal Vasko4c7763f2020-07-27 17:40:37 +0200847set_init(struct lyxp_set *new, const struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200848{
849 memset(new, 0, sizeof *new);
Michal Vasko02a77382019-09-12 11:47:35 +0200850 if (set) {
Michal Vasko306e2832022-07-25 09:15:17 +0200851 new->non_child_axis = set->non_child_axis;
Michal Vasko02a77382019-09-12 11:47:35 +0200852 new->ctx = set->ctx;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200853 new->cur_node = set->cur_node;
Michal Vasko588112f2019-12-10 14:51:53 +0100854 new->root_type = set->root_type;
Michal Vasko6b26e742020-07-17 15:02:10 +0200855 new->context_op = set->context_op;
Michal Vaskof03ed032020-03-04 13:31:44 +0100856 new->tree = set->tree;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200857 new->cur_mod = set->cur_mod;
Michal Vasko02a77382019-09-12 11:47:35 +0200858 new->format = set->format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200859 new->prefix_data = set->prefix_data;
aPiecekfba75362021-10-07 12:39:48 +0200860 new->vars = set->vars;
Michal Vasko02a77382019-09-12 11:47:35 +0200861 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200862}
863
864/**
865 * @brief Create a deep copy of a set.
866 *
867 * @param[in] set Set to copy.
868 * @return Copy of @p set.
869 */
870static struct lyxp_set *
871set_copy(struct lyxp_set *set)
872{
873 struct lyxp_set *ret;
Michal Vasko1fdd8fa2021-01-08 09:21:45 +0100874 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200875
876 if (!set) {
877 return NULL;
878 }
879
880 ret = malloc(sizeof *ret);
881 LY_CHECK_ERR_RET(!ret, LOGMEM(set->ctx), NULL);
882 set_init(ret, set);
883
884 if (set->type == LYXP_SET_SCNODE_SET) {
885 ret->type = set->type;
886
887 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100888 if ((set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) ||
889 (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +0200890 uint32_t idx;
Michal Vasko26bbb272022-08-02 14:54:33 +0200891
Michal Vasko7333cb32022-07-29 16:30:29 +0200892 LY_CHECK_ERR_RET(set_scnode_insert_node(ret, set->val.scnodes[i].scnode, set->val.scnodes[i].type,
893 set->val.scnodes[i].axis, &idx), lyxp_set_free(ret), NULL);
Michal Vasko3f27c522020-01-06 08:37:49 +0100894 /* coverity seems to think scnodes can be NULL */
Radek Krejciaa6b53f2020-08-27 15:19:03 +0200895 if (!ret->val.scnodes) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200896 lyxp_set_free(ret);
897 return NULL;
898 }
Michal Vaskoba716542019-12-16 10:01:58 +0100899 ret->val.scnodes[idx].in_ctx = set->val.scnodes[i].in_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200900 }
901 }
902 } else if (set->type == LYXP_SET_NODE_SET) {
903 ret->type = set->type;
Michal Vasko08e9b112021-06-11 15:41:17 +0200904 if (set->used) {
905 ret->val.nodes = malloc(set->used * sizeof *ret->val.nodes);
906 LY_CHECK_ERR_RET(!ret->val.nodes, LOGMEM(set->ctx); free(ret), NULL);
907 memcpy(ret->val.nodes, set->val.nodes, set->used * sizeof *ret->val.nodes);
908 } else {
909 ret->val.nodes = NULL;
910 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200911
912 ret->used = ret->size = set->used;
913 ret->ctx_pos = set->ctx_pos;
914 ret->ctx_size = set->ctx_size;
Michal Vasko4a04e542021-02-01 08:58:30 +0100915 if (set->ht) {
916 ret->ht = lyht_dup(set->ht);
917 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200918 } else {
Radek Krejci0f969882020-08-21 16:56:47 +0200919 memcpy(ret, set, sizeof *ret);
920 if (set->type == LYXP_SET_STRING) {
921 ret->val.str = strdup(set->val.str);
922 LY_CHECK_ERR_RET(!ret->val.str, LOGMEM(set->ctx); free(ret), NULL);
923 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200924 }
925
926 return ret;
927}
928
929/**
930 * @brief Fill XPath set with a string. Any current data are disposed of.
931 *
932 * @param[in] set Set to fill.
933 * @param[in] string String to fill into \p set.
934 * @param[in] str_len Length of \p string. 0 is a valid value!
935 */
936static void
Michal Vaskodd528af2022-08-08 14:35:07 +0200937set_fill_string(struct lyxp_set *set, const char *string, uint32_t str_len)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200938{
Michal Vaskod3678892020-05-21 10:06:58 +0200939 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200940
941 set->type = LYXP_SET_STRING;
942 if ((str_len == 0) && (string[0] != '\0')) {
943 string = "";
944 }
945 set->val.str = strndup(string, str_len);
946}
947
948/**
949 * @brief Fill XPath set with a number. Any current data are disposed of.
950 *
951 * @param[in] set Set to fill.
952 * @param[in] number Number to fill into \p set.
953 */
954static void
955set_fill_number(struct lyxp_set *set, long double number)
956{
Michal Vaskod3678892020-05-21 10:06:58 +0200957 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200958
959 set->type = LYXP_SET_NUMBER;
960 set->val.num = number;
961}
962
963/**
964 * @brief Fill XPath set with a boolean. Any current data are disposed of.
965 *
966 * @param[in] set Set to fill.
967 * @param[in] boolean Boolean to fill into \p set.
968 */
969static void
Radek Krejci857189e2020-09-01 13:26:36 +0200970set_fill_boolean(struct lyxp_set *set, ly_bool boolean)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200971{
Michal Vaskod3678892020-05-21 10:06:58 +0200972 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200973
974 set->type = LYXP_SET_BOOLEAN;
Michal Vasko004d3152020-06-11 19:59:22 +0200975 set->val.bln = boolean;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200976}
977
978/**
979 * @brief Fill XPath set with the value from another set (deep assign).
980 * Any current data are disposed of.
981 *
982 * @param[in] trg Set to fill.
983 * @param[in] src Source set to copy into \p trg.
984 */
985static void
Michal Vasko4c7763f2020-07-27 17:40:37 +0200986set_fill_set(struct lyxp_set *trg, const struct lyxp_set *src)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200987{
988 if (!trg || !src) {
989 return;
990 }
991
992 if (trg->type == LYXP_SET_NODE_SET) {
993 free(trg->val.nodes);
994 } else if (trg->type == LYXP_SET_STRING) {
995 free(trg->val.str);
996 }
997 set_init(trg, src);
998
999 if (src->type == LYXP_SET_SCNODE_SET) {
1000 trg->type = LYXP_SET_SCNODE_SET;
1001 trg->used = src->used;
1002 trg->size = src->used;
1003
Michal Vasko08e9b112021-06-11 15:41:17 +02001004 if (trg->size) {
1005 trg->val.scnodes = ly_realloc(trg->val.scnodes, trg->size * sizeof *trg->val.scnodes);
1006 LY_CHECK_ERR_RET(!trg->val.scnodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), );
1007 memcpy(trg->val.scnodes, src->val.scnodes, src->used * sizeof *src->val.scnodes);
1008 } else {
1009 trg->val.scnodes = NULL;
1010 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001011 } else if (src->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02001012 set_fill_boolean(trg, src->val.bln);
Michal Vasko44f3d2c2020-08-24 09:49:38 +02001013 } else if (src->type == LYXP_SET_NUMBER) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001014 set_fill_number(trg, src->val.num);
1015 } else if (src->type == LYXP_SET_STRING) {
1016 set_fill_string(trg, src->val.str, strlen(src->val.str));
1017 } else {
1018 if (trg->type == LYXP_SET_NODE_SET) {
1019 free(trg->val.nodes);
1020 } else if (trg->type == LYXP_SET_STRING) {
1021 free(trg->val.str);
1022 }
1023
Michal Vaskod3678892020-05-21 10:06:58 +02001024 assert(src->type == LYXP_SET_NODE_SET);
1025
1026 trg->type = LYXP_SET_NODE_SET;
1027 trg->used = src->used;
1028 trg->size = src->used;
1029 trg->ctx_pos = src->ctx_pos;
1030 trg->ctx_size = src->ctx_size;
1031
Michal Vasko08e9b112021-06-11 15:41:17 +02001032 if (trg->size) {
1033 trg->val.nodes = malloc(trg->size * sizeof *trg->val.nodes);
1034 LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), );
1035 memcpy(trg->val.nodes, src->val.nodes, src->used * sizeof *src->val.nodes);
1036 } else {
1037 trg->val.nodes = NULL;
1038 }
Michal Vaskod3678892020-05-21 10:06:58 +02001039 if (src->ht) {
1040 trg->ht = lyht_dup(src->ht);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001041 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02001042 trg->ht = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001043 }
1044 }
1045}
1046
1047/**
1048 * @brief Clear context of all schema nodes.
1049 *
1050 * @param[in] set Set to clear.
Michal Vasko1a09b212021-05-06 13:00:10 +02001051 * @param[in] new_ctx New context state for all the nodes currently in the context.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001052 */
1053static void
Michal Vasko1a09b212021-05-06 13:00:10 +02001054set_scnode_clear_ctx(struct lyxp_set *set, int32_t new_ctx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001055{
1056 uint32_t i;
1057
1058 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001059 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko1a09b212021-05-06 13:00:10 +02001060 set->val.scnodes[i].in_ctx = new_ctx;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001061 } else if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START) {
1062 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001063 }
1064 }
1065}
1066
1067/**
1068 * @brief Remove a node from a set. Removing last node changes
1069 * set into LYXP_SET_EMPTY. Context position aware.
1070 *
1071 * @param[in] set Set to use.
1072 * @param[in] idx Index from @p set of the node to be removed.
1073 */
1074static void
1075set_remove_node(struct lyxp_set *set, uint32_t idx)
1076{
1077 assert(set && (set->type == LYXP_SET_NODE_SET));
1078 assert(idx < set->used);
1079
1080 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1081
1082 --set->used;
Michal Vasko08e9b112021-06-11 15:41:17 +02001083 if (idx < set->used) {
1084 memmove(&set->val.nodes[idx], &set->val.nodes[idx + 1], (set->used - idx) * sizeof *set->val.nodes);
1085 } else if (!set->used) {
Michal Vaskod3678892020-05-21 10:06:58 +02001086 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001087 }
1088}
1089
1090/**
Michal Vasko2caefc12019-11-14 16:07:56 +01001091 * @brief Remove a node from a set by setting its type to LYXP_NODE_NONE.
Michal Vasko57eab132019-09-24 11:46:26 +02001092 *
1093 * @param[in] set Set to use.
1094 * @param[in] idx Index from @p set of the node to be removed.
1095 */
1096static void
Michal Vasko2caefc12019-11-14 16:07:56 +01001097set_remove_node_none(struct lyxp_set *set, uint32_t idx)
Michal Vasko57eab132019-09-24 11:46:26 +02001098{
1099 assert(set && (set->type == LYXP_SET_NODE_SET));
1100 assert(idx < set->used);
1101
Michal Vasko2caefc12019-11-14 16:07:56 +01001102 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1103 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1104 }
1105 set->val.nodes[idx].type = LYXP_NODE_NONE;
Michal Vasko57eab132019-09-24 11:46:26 +02001106}
1107
1108/**
Michal Vasko2caefc12019-11-14 16:07:56 +01001109 * @brief Remove all LYXP_NODE_NONE nodes from a set. Removing last node changes
Michal Vasko57eab132019-09-24 11:46:26 +02001110 * set into LYXP_SET_EMPTY. Context position aware.
1111 *
1112 * @param[in] set Set to consolidate.
1113 */
1114static void
Michal Vasko2caefc12019-11-14 16:07:56 +01001115set_remove_nodes_none(struct lyxp_set *set)
Michal Vasko57eab132019-09-24 11:46:26 +02001116{
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01001117 uint32_t i, orig_used, end = 0;
1118 int64_t start;
Michal Vasko57eab132019-09-24 11:46:26 +02001119
Michal Vaskod3678892020-05-21 10:06:58 +02001120 assert(set);
Michal Vasko57eab132019-09-24 11:46:26 +02001121
1122 orig_used = set->used;
1123 set->used = 0;
Michal Vaskod989ba02020-08-24 10:59:24 +02001124 for (i = 0; i < orig_used; ) {
Michal Vasko57eab132019-09-24 11:46:26 +02001125 start = -1;
1126 do {
Michal Vasko2caefc12019-11-14 16:07:56 +01001127 if ((set->val.nodes[i].type != LYXP_NODE_NONE) && (start == -1)) {
Michal Vasko57eab132019-09-24 11:46:26 +02001128 start = i;
Michal Vasko2caefc12019-11-14 16:07:56 +01001129 } else if ((start > -1) && (set->val.nodes[i].type == LYXP_NODE_NONE)) {
Michal Vasko57eab132019-09-24 11:46:26 +02001130 end = i;
1131 ++i;
1132 break;
1133 }
1134
1135 ++i;
1136 if (i == orig_used) {
1137 end = i;
1138 }
1139 } while (i < orig_used);
1140
1141 if (start > -1) {
1142 /* move the whole chunk of valid nodes together */
1143 if (set->used != (unsigned)start) {
1144 memmove(&set->val.nodes[set->used], &set->val.nodes[start], (end - start) * sizeof *set->val.nodes);
1145 }
1146 set->used += end - start;
1147 }
1148 }
Michal Vasko57eab132019-09-24 11:46:26 +02001149}
1150
1151/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02001152 * @brief Check for duplicates in a node set.
1153 *
1154 * @param[in] set Set to check.
1155 * @param[in] node Node to look for in @p set.
1156 * @param[in] node_type Type of @p node.
1157 * @param[in] skip_idx Index from @p set to skip.
1158 * @return LY_ERR
1159 */
1160static LY_ERR
1161set_dup_node_check(const struct lyxp_set *set, const struct lyd_node *node, enum lyxp_node_type node_type, int skip_idx)
1162{
1163 uint32_t i;
1164
Michal Vasko2caefc12019-11-14 16:07:56 +01001165 if (set->ht && node) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001166 return set_dup_node_hash_check(set, (struct lyd_node *)node, node_type, skip_idx);
1167 }
1168
1169 for (i = 0; i < set->used; ++i) {
1170 if ((skip_idx > -1) && (i == (unsigned)skip_idx)) {
1171 continue;
1172 }
1173
1174 if ((set->val.nodes[i].node == node) && (set->val.nodes[i].type == node_type)) {
1175 return LY_EEXIST;
1176 }
1177 }
1178
1179 return LY_SUCCESS;
1180}
1181
Radek Krejci857189e2020-09-01 13:26:36 +02001182ly_bool
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001183lyxp_set_scnode_contains(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type, int skip_idx,
1184 uint32_t *index_p)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001185{
1186 uint32_t i;
1187
1188 for (i = 0; i < set->used; ++i) {
1189 if ((skip_idx > -1) && (i == (unsigned)skip_idx)) {
1190 continue;
1191 }
1192
1193 if ((set->val.scnodes[i].scnode == node) && (set->val.scnodes[i].type == node_type)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001194 if (index_p) {
1195 *index_p = i;
1196 }
1197 return 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001198 }
1199 }
1200
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001201 return 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001202}
1203
Michal Vaskoecd62de2019-11-13 12:35:11 +01001204void
1205lyxp_set_scnode_merge(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001206{
1207 uint32_t orig_used, i, j;
1208
Michal Vaskod3678892020-05-21 10:06:58 +02001209 assert((set1->type == LYXP_SET_SCNODE_SET) && (set2->type == LYXP_SET_SCNODE_SET));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001210
Michal Vaskod3678892020-05-21 10:06:58 +02001211 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001212 return;
1213 }
1214
Michal Vaskod3678892020-05-21 10:06:58 +02001215 if (!set1->used) {
aPiecekadc1e4f2021-10-07 11:15:12 +02001216 /* release hidden allocated data (lyxp_set.size) */
1217 lyxp_set_free_content(set1);
1218 /* direct copying of the entire structure */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001219 memcpy(set1, set2, sizeof *set1);
1220 return;
1221 }
1222
1223 if (set1->used + set2->used > set1->size) {
1224 set1->size = set1->used + set2->used;
1225 set1->val.scnodes = ly_realloc(set1->val.scnodes, set1->size * sizeof *set1->val.scnodes);
1226 LY_CHECK_ERR_RET(!set1->val.scnodes, LOGMEM(set1->ctx), );
1227 }
1228
1229 orig_used = set1->used;
1230
1231 for (i = 0; i < set2->used; ++i) {
1232 for (j = 0; j < orig_used; ++j) {
1233 /* detect duplicities */
1234 if (set1->val.scnodes[j].scnode == set2->val.scnodes[i].scnode) {
1235 break;
1236 }
1237 }
1238
Michal Vasko0587bce2020-12-10 12:19:21 +01001239 if (j < orig_used) {
1240 /* node is there, but update its status if needed */
1241 if (set1->val.scnodes[j].in_ctx == LYXP_SET_SCNODE_START_USED) {
1242 set1->val.scnodes[j].in_ctx = set2->val.scnodes[i].in_ctx;
Michal Vasko1a09b212021-05-06 13:00:10 +02001243 } else if ((set1->val.scnodes[j].in_ctx == LYXP_SET_SCNODE_ATOM_NODE) &&
1244 (set2->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_VAL)) {
1245 set1->val.scnodes[j].in_ctx = set2->val.scnodes[i].in_ctx;
Michal Vasko0587bce2020-12-10 12:19:21 +01001246 }
1247 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001248 memcpy(&set1->val.scnodes[set1->used], &set2->val.scnodes[i], sizeof *set2->val.scnodes);
1249 ++set1->used;
1250 }
1251 }
1252
Michal Vaskod3678892020-05-21 10:06:58 +02001253 lyxp_set_free_content(set2);
1254 set2->type = LYXP_SET_SCNODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001255}
1256
1257/**
1258 * @brief Insert a node into a set. Context position aware.
1259 *
1260 * @param[in] set Set to use.
1261 * @param[in] node Node to insert to @p set.
1262 * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting.
1263 * @param[in] node_type Node type of @p node.
1264 * @param[in] idx Index in @p set to insert into.
1265 */
1266static void
1267set_insert_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx)
1268{
Michal Vaskod3678892020-05-21 10:06:58 +02001269 assert(set && (set->type == LYXP_SET_NODE_SET));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001270
Michal Vaskod3678892020-05-21 10:06:58 +02001271 if (!set->size) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001272 /* first item */
1273 if (idx) {
1274 /* no real harm done, but it is a bug */
1275 LOGINT(set->ctx);
1276 idx = 0;
1277 }
1278 set->val.nodes = malloc(LYXP_SET_SIZE_START * sizeof *set->val.nodes);
1279 LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), );
1280 set->type = LYXP_SET_NODE_SET;
1281 set->used = 0;
1282 set->size = LYXP_SET_SIZE_START;
1283 set->ctx_pos = 1;
1284 set->ctx_size = 1;
1285 set->ht = NULL;
1286 } else {
1287 /* not an empty set */
1288 if (set->used == set->size) {
1289
1290 /* set is full */
Michal Vasko871df522022-04-06 12:14:41 +02001291 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 +02001292 LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), );
Michal Vasko871df522022-04-06 12:14:41 +02001293 set->size *= LYXP_SET_SIZE_MUL_STEP;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001294 }
1295
1296 if (idx > set->used) {
1297 LOGINT(set->ctx);
1298 idx = set->used;
1299 }
1300
1301 /* make space for the new node */
1302 if (idx < set->used) {
1303 memmove(&set->val.nodes[idx + 1], &set->val.nodes[idx], (set->used - idx) * sizeof *set->val.nodes);
1304 }
1305 }
1306
1307 /* finally assign the value */
1308 set->val.nodes[idx].node = (struct lyd_node *)node;
1309 set->val.nodes[idx].type = node_type;
1310 set->val.nodes[idx].pos = pos;
1311 ++set->used;
1312
Michal Vasko2416fdd2021-10-01 11:07:10 +02001313 /* add into hash table */
1314 set_insert_node_hash(set, (struct lyd_node *)node, node_type);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001315}
1316
Michal Vasko7333cb32022-07-29 16:30:29 +02001317/**
1318 * @brief Insert schema node into set.
1319 *
1320 * @param[in] set Set to insert into.
1321 * @param[in] node Node to insert.
1322 * @param[in] node_type Node type of @p node.
1323 * @param[in] axis Axis that @p node was reached on.
1324 * @param[out] index_p Optional pointer to store index if the inserted @p node.
1325 * @return LY_SUCCESS on success.
1326 * @return LY_EMEM on memory allocation failure.
1327 */
1328static LY_ERR
1329set_scnode_insert_node(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type,
1330 enum lyxp_axis axis, uint32_t *index_p)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001331{
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001332 uint32_t index;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001333
1334 assert(set->type == LYXP_SET_SCNODE_SET);
1335
Michal Vasko871df522022-04-06 12:14:41 +02001336 if (!set->size) {
1337 /* first item */
1338 set->val.scnodes = malloc(LYXP_SET_SIZE_START * sizeof *set->val.scnodes);
1339 LY_CHECK_ERR_RET(!set->val.scnodes, LOGMEM(set->ctx), LY_EMEM);
1340 set->type = LYXP_SET_SCNODE_SET;
1341 set->used = 0;
1342 set->size = LYXP_SET_SIZE_START;
1343 set->ctx_pos = 1;
1344 set->ctx_size = 1;
1345 set->ht = NULL;
1346 }
1347
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001348 if (lyxp_set_scnode_contains(set, node, node_type, -1, &index)) {
Michal Vasko7333cb32022-07-29 16:30:29 +02001349 /* BUG if axes differs, this new one is thrown away */
Radek Krejcif13b87b2020-12-01 22:02:17 +01001350 set->val.scnodes[index].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001351 } else {
1352 if (set->used == set->size) {
Michal Vasko871df522022-04-06 12:14:41 +02001353 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 +02001354 LY_CHECK_ERR_RET(!set->val.scnodes, LOGMEM(set->ctx), LY_EMEM);
Michal Vasko871df522022-04-06 12:14:41 +02001355 set->size *= LYXP_SET_SIZE_MUL_STEP;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001356 }
1357
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001358 index = set->used;
1359 set->val.scnodes[index].scnode = (struct lysc_node *)node;
1360 set->val.scnodes[index].type = node_type;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001361 set->val.scnodes[index].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko7333cb32022-07-29 16:30:29 +02001362 set->val.scnodes[index].axis = axis;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001363 ++set->used;
1364 }
1365
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001366 if (index_p) {
1367 *index_p = index;
1368 }
1369
1370 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001371}
1372
1373/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02001374 * @brief Set all nodes with ctx 1 to a new unique context value.
1375 *
1376 * @param[in] set Set to modify.
1377 * @return New context value.
1378 */
Michal Vasko5c4e5892019-11-14 12:31:38 +01001379static int32_t
Michal Vasko03ff5a72019-09-11 13:49:33 +02001380set_scnode_new_in_ctx(struct lyxp_set *set)
1381{
Michal Vasko5c4e5892019-11-14 12:31:38 +01001382 uint32_t i;
1383 int32_t ret_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001384
1385 assert(set->type == LYXP_SET_SCNODE_SET);
1386
Radek Krejcif13b87b2020-12-01 22:02:17 +01001387 ret_ctx = LYXP_SET_SCNODE_ATOM_PRED_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001388retry:
1389 for (i = 0; i < set->used; ++i) {
1390 if (set->val.scnodes[i].in_ctx >= ret_ctx) {
1391 ret_ctx = set->val.scnodes[i].in_ctx + 1;
1392 goto retry;
1393 }
1394 }
1395 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001396 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001397 set->val.scnodes[i].in_ctx = ret_ctx;
1398 }
1399 }
1400
1401 return ret_ctx;
1402}
1403
1404/**
1405 * @brief Get unique @p node position in the data.
1406 *
1407 * @param[in] node Node to find.
1408 * @param[in] node_type Node type of @p node.
1409 * @param[in] root Root node.
1410 * @param[in] root_type Type of the XPath @p root node.
1411 * @param[in] prev Node that we think is before @p node in DFS from @p root. Can optionally
1412 * be used to increase efficiency and start the DFS from this node.
1413 * @param[in] prev_pos Node @p prev position. Optional, but must be set if @p prev is set.
1414 * @return Node position.
1415 */
1416static uint32_t
1417get_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 +02001418 enum lyxp_node_type root_type, const struct lyd_node **prev, uint32_t *prev_pos)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001419{
Michal Vasko56daf732020-08-10 10:57:18 +02001420 const struct lyd_node *elem = NULL, *top_sibling;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001421 uint32_t pos = 1;
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001422 ly_bool found = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001423
1424 assert(prev && prev_pos && !root->prev->next);
1425
1426 if ((node_type == LYXP_NODE_ROOT) || (node_type == LYXP_NODE_ROOT_CONFIG)) {
1427 return 0;
1428 }
1429
1430 if (*prev) {
1431 /* start from the previous element instead from the root */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001432 pos = *prev_pos;
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001433 for (top_sibling = *prev; top_sibling->parent; top_sibling = lyd_parent(top_sibling)) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001434 goto dfs_search;
1435 }
1436
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001437 LY_LIST_FOR(root, top_sibling) {
Michal Vasko56daf732020-08-10 10:57:18 +02001438 LYD_TREE_DFS_BEGIN(top_sibling, elem) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001439dfs_search:
Michal Vaskoa9309bb2021-07-09 09:31:55 +02001440 LYD_TREE_DFS_continue = 0;
1441
Michal Vasko56daf732020-08-10 10:57:18 +02001442 if (*prev && !elem) {
1443 /* resume previous DFS */
1444 elem = LYD_TREE_DFS_next = (struct lyd_node *)*prev;
1445 LYD_TREE_DFS_continue = 0;
1446 }
1447
Michal Vasko482a6822022-05-06 08:56:12 +02001448 if (!elem->schema || ((root_type == LYXP_NODE_ROOT_CONFIG) && (elem->schema->flags & LYS_CONFIG_R))) {
Michal Vasko56daf732020-08-10 10:57:18 +02001449 /* skip */
1450 LYD_TREE_DFS_continue = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001451 } else {
Michal Vasko56daf732020-08-10 10:57:18 +02001452 if (elem == node) {
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001453 found = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001454 break;
1455 }
Michal Vasko56daf732020-08-10 10:57:18 +02001456 ++pos;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001457 }
Michal Vasko56daf732020-08-10 10:57:18 +02001458
1459 LYD_TREE_DFS_END(top_sibling, elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001460 }
1461
1462 /* node found */
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001463 if (found) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001464 break;
1465 }
1466 }
1467
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001468 if (!found) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001469 if (!(*prev)) {
1470 /* we went from root and failed to find it, cannot be */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001471 LOGINT(LYD_CTX(node));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001472 return 0;
1473 } else {
Michal Vasko56daf732020-08-10 10:57:18 +02001474 /* start the search again from the beginning */
1475 *prev = root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001476
Michal Vasko56daf732020-08-10 10:57:18 +02001477 top_sibling = root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001478 pos = 1;
1479 goto dfs_search;
1480 }
1481 }
1482
1483 /* remember the last found node for next time */
1484 *prev = node;
1485 *prev_pos = pos;
1486
1487 return pos;
1488}
1489
1490/**
1491 * @brief Assign (fill) missing node positions.
1492 *
1493 * @param[in] set Set to fill positions in.
1494 * @param[in] root Context root node.
1495 * @param[in] root_type Context root type.
1496 * @return LY_ERR
1497 */
1498static LY_ERR
1499set_assign_pos(struct lyxp_set *set, const struct lyd_node *root, enum lyxp_node_type root_type)
1500{
1501 const struct lyd_node *prev = NULL, *tmp_node;
1502 uint32_t i, tmp_pos = 0;
1503
1504 for (i = 0; i < set->used; ++i) {
1505 if (!set->val.nodes[i].pos) {
1506 tmp_node = NULL;
1507 switch (set->val.nodes[i].type) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001508 case LYXP_NODE_META:
1509 tmp_node = set->val.meta[i].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001510 if (!tmp_node) {
1511 LOGINT_RET(root->schema->module->ctx);
1512 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01001513 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001514 case LYXP_NODE_ELEM:
1515 case LYXP_NODE_TEXT:
1516 if (!tmp_node) {
1517 tmp_node = set->val.nodes[i].node;
1518 }
1519 set->val.nodes[i].pos = get_node_pos(tmp_node, set->val.nodes[i].type, root, root_type, &prev, &tmp_pos);
1520 break;
1521 default:
1522 /* all roots have position 0 */
1523 break;
1524 }
1525 }
1526 }
1527
1528 return LY_SUCCESS;
1529}
1530
1531/**
Michal Vasko9f96a052020-03-10 09:41:45 +01001532 * @brief Get unique @p meta position in the parent metadata.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001533 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001534 * @param[in] meta Metadata to use.
1535 * @return Metadata position.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001536 */
Michal Vaskodd528af2022-08-08 14:35:07 +02001537static uint32_t
Michal Vasko9f96a052020-03-10 09:41:45 +01001538get_meta_pos(struct lyd_meta *meta)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001539{
Michal Vaskodd528af2022-08-08 14:35:07 +02001540 uint32_t pos = 0;
Michal Vasko9f96a052020-03-10 09:41:45 +01001541 struct lyd_meta *meta2;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001542
Michal Vasko9f96a052020-03-10 09:41:45 +01001543 for (meta2 = meta->parent->meta; meta2 && (meta2 != meta); meta2 = meta2->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001544 ++pos;
1545 }
1546
Michal Vasko9f96a052020-03-10 09:41:45 +01001547 assert(meta2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001548 return pos;
1549}
1550
1551/**
1552 * @brief Compare 2 nodes in respect to XPath document order.
1553 *
1554 * @param[in] item1 1st node.
1555 * @param[in] item2 2nd node.
1556 * @return If 1st > 2nd returns 1, 1st == 2nd returns 0, and 1st < 2nd returns -1.
1557 */
1558static int
1559set_sort_compare(struct lyxp_set_node *item1, struct lyxp_set_node *item2)
1560{
Michal Vasko9f96a052020-03-10 09:41:45 +01001561 uint32_t meta_pos1 = 0, meta_pos2 = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001562
1563 if (item1->pos < item2->pos) {
1564 return -1;
1565 }
1566
1567 if (item1->pos > item2->pos) {
1568 return 1;
1569 }
1570
1571 /* node positions are equal, the fun case */
1572
1573 /* 1st ELEM - == - 2nd TEXT, 1st TEXT - == - 2nd ELEM */
1574 /* special case since text nodes are actually saved as their parents */
1575 if ((item1->node == item2->node) && (item1->type != item2->type)) {
1576 if (item1->type == LYXP_NODE_ELEM) {
1577 assert(item2->type == LYXP_NODE_TEXT);
1578 return -1;
1579 } else {
1580 assert((item1->type == LYXP_NODE_TEXT) && (item2->type == LYXP_NODE_ELEM));
1581 return 1;
1582 }
1583 }
1584
Michal Vasko9f96a052020-03-10 09:41:45 +01001585 /* we need meta positions now */
1586 if (item1->type == LYXP_NODE_META) {
1587 meta_pos1 = get_meta_pos((struct lyd_meta *)item1->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001588 }
Michal Vasko9f96a052020-03-10 09:41:45 +01001589 if (item2->type == LYXP_NODE_META) {
1590 meta_pos2 = get_meta_pos((struct lyd_meta *)item2->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001591 }
1592
Michal Vasko9f96a052020-03-10 09:41:45 +01001593 /* 1st ROOT - 2nd ROOT, 1st ELEM - 2nd ELEM, 1st TEXT - 2nd TEXT, 1st META - =pos= - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001594 /* check for duplicates */
1595 if (item1->node == item2->node) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001596 assert((item1->type == item2->type) && ((item1->type != LYXP_NODE_META) || (meta_pos1 == meta_pos2)));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001597 return 0;
1598 }
1599
Michal Vasko9f96a052020-03-10 09:41:45 +01001600 /* 1st ELEM - 2nd TEXT, 1st ELEM - any pos - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001601 /* elem is always first, 2nd node is after it */
1602 if (item1->type == LYXP_NODE_ELEM) {
1603 assert(item2->type != LYXP_NODE_ELEM);
1604 return -1;
1605 }
1606
Michal Vasko9f96a052020-03-10 09:41:45 +01001607 /* 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 +02001608 /* 2nd is before 1st */
Michal Vasko69730152020-10-09 16:30:07 +02001609 if (((item1->type == LYXP_NODE_TEXT) &&
1610 ((item2->type == LYXP_NODE_ELEM) || (item2->type == LYXP_NODE_META))) ||
1611 ((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_ELEM)) ||
1612 (((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_META)) &&
1613 (meta_pos1 > meta_pos2))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001614 return 1;
1615 }
1616
Michal Vasko9f96a052020-03-10 09:41:45 +01001617 /* 1st META - any pos - 2nd TEXT, 1st META <pos< - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001618 /* 2nd is after 1st */
1619 return -1;
1620}
1621
1622/**
1623 * @brief Set cast for comparisons.
1624 *
Michal Vasko8abcecc2022-07-28 09:55:01 +02001625 * @param[in,out] trg Target set to cast source into.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001626 * @param[in] src Source set.
1627 * @param[in] type Target set type.
1628 * @param[in] src_idx Source set node index.
Michal Vasko8abcecc2022-07-28 09:55:01 +02001629 * @return LY_SUCCESS on success.
1630 * @return LY_ERR value on error.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001631 */
1632static LY_ERR
Michal Vasko8abcecc2022-07-28 09:55:01 +02001633set_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 +02001634{
1635 assert(src->type == LYXP_SET_NODE_SET);
1636
1637 set_init(trg, src);
1638
1639 /* insert node into target set */
1640 set_insert_node(trg, src->val.nodes[src_idx].node, src->val.nodes[src_idx].pos, src->val.nodes[src_idx].type, 0);
1641
1642 /* cast target set appropriately */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001643 return lyxp_set_cast(trg, type);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001644}
1645
Michal Vasko4c7763f2020-07-27 17:40:37 +02001646/**
1647 * @brief Set content canonization for comparisons.
1648 *
Michal Vasko8abcecc2022-07-28 09:55:01 +02001649 * @param[in,out] set Set to canonize.
Michal Vasko4c7763f2020-07-27 17:40:37 +02001650 * @param[in] xp_node Source XPath node/meta to use for canonization.
Michal Vasko8abcecc2022-07-28 09:55:01 +02001651 * @return LY_SUCCESS on success.
1652 * @return LY_ERR value on error.
Michal Vasko4c7763f2020-07-27 17:40:37 +02001653 */
1654static LY_ERR
Michal Vasko8abcecc2022-07-28 09:55:01 +02001655set_comp_canonize(struct lyxp_set *set, const struct lyxp_set_node *xp_node)
Michal Vasko4c7763f2020-07-27 17:40:37 +02001656{
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001657 const struct lysc_type *type = NULL;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001658 struct lyd_value val;
1659 struct ly_err_item *err = NULL;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001660 LY_ERR rc;
1661
1662 /* is there anything to canonize even? */
Michal Vasko8abcecc2022-07-28 09:55:01 +02001663 if (set->type == LYXP_SET_STRING) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02001664 /* do we have a type to use for canonization? */
1665 if ((xp_node->type == LYXP_NODE_ELEM) && (xp_node->node->schema->nodetype & LYD_NODE_TERM)) {
1666 type = ((struct lyd_node_term *)xp_node->node)->value.realtype;
1667 } else if (xp_node->type == LYXP_NODE_META) {
1668 type = ((struct lyd_meta *)xp_node->node)->value.realtype;
1669 }
1670 }
1671 if (!type) {
Michal Vasko8abcecc2022-07-28 09:55:01 +02001672 /* no canonization needed/possible */
1673 return LY_SUCCESS;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001674 }
1675
Michal Vasko8abcecc2022-07-28 09:55:01 +02001676 /* check for built-in types without required canonization */
1677 if ((type->basetype == LY_TYPE_STRING) && (type->plugin->store == lyplg_type_store_string)) {
1678 /* string */
1679 return LY_SUCCESS;
1680 }
1681 if ((type->basetype == LY_TYPE_BOOL) && (type->plugin->store == lyplg_type_store_boolean)) {
1682 /* boolean */
1683 return LY_SUCCESS;
1684 }
1685 if ((type->basetype == LY_TYPE_ENUM) && (type->plugin->store == lyplg_type_store_enum)) {
1686 /* enumeration */
1687 return LY_SUCCESS;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001688 }
1689
Michal Vasko8abcecc2022-07-28 09:55:01 +02001690 /* print canonized string, ignore errors, the value may not satisfy schema constraints */
1691 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 +01001692 LYD_HINT_DATA, xp_node->node->schema, &val, NULL, &err);
Michal Vasko4c7763f2020-07-27 17:40:37 +02001693 ly_err_free(err);
1694 if (rc) {
Michal Vasko8abcecc2022-07-28 09:55:01 +02001695 /* invalid value, function store automaticaly dealloc value when fail */
1696 return LY_SUCCESS;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001697 }
1698
Michal Vasko8abcecc2022-07-28 09:55:01 +02001699 /* use the canonized string value */
1700 free(set->val.str);
1701 set->val.str = strdup(lyd_value_get_canonical(set->ctx, &val));
1702 type->plugin->free(set->ctx, &val);
1703 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM);
Michal Vasko4c7763f2020-07-27 17:40:37 +02001704
Michal Vasko4c7763f2020-07-27 17:40:37 +02001705 return LY_SUCCESS;
1706}
1707
Michal Vasko03ff5a72019-09-11 13:49:33 +02001708/**
1709 * @brief Bubble sort @p set into XPath document order.
Michal Vasko49fec8e2022-05-24 10:28:33 +02001710 * Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001711 *
1712 * @param[in] set Set to sort.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001713 * @return How many times the whole set was traversed - 1 (if set was sorted, returns 0).
1714 */
1715static int
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001716set_sort(struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001717{
1718 uint32_t i, j;
Radek Krejci1deb5be2020-08-26 16:43:36 +02001719 int ret = 0, cmp;
Radek Krejci857189e2020-09-01 13:26:36 +02001720 ly_bool inverted, change;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001721 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001722 struct lyxp_set_node item;
1723 struct lyxp_set_hash_node hnode;
1724 uint64_t hash;
1725
Michal Vasko3cf8fbf2020-05-27 15:21:21 +02001726 if ((set->type != LYXP_SET_NODE_SET) || (set->used < 2)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001727 return 0;
1728 }
1729
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001730 /* find first top-level node to be used as anchor for positions */
Michal Vasko4a7d4d62021-12-13 17:05:06 +01001731 for (root = set->tree; root->parent; root = lyd_parent(root)) {}
Michal Vaskod989ba02020-08-24 10:59:24 +02001732 for ( ; root->prev->next; root = root->prev) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001733
1734 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001735 if (set_assign_pos(set, root, set->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001736 return -1;
1737 }
1738
Michal Vasko88a9e802022-05-24 10:50:28 +02001739#ifndef NDEBUG
Michal Vasko03ff5a72019-09-11 13:49:33 +02001740 LOGDBG(LY_LDGXPATH, "SORT BEGIN");
1741 print_set_debug(set);
Michal Vasko88a9e802022-05-24 10:50:28 +02001742#endif
Michal Vasko03ff5a72019-09-11 13:49:33 +02001743
1744 for (i = 0; i < set->used; ++i) {
1745 inverted = 0;
1746 change = 0;
1747
1748 for (j = 1; j < set->used - i; ++j) {
1749 /* compare node positions */
1750 if (inverted) {
1751 cmp = set_sort_compare(&set->val.nodes[j], &set->val.nodes[j - 1]);
1752 } else {
1753 cmp = set_sort_compare(&set->val.nodes[j - 1], &set->val.nodes[j]);
1754 }
1755
1756 /* swap if needed */
1757 if ((inverted && (cmp < 0)) || (!inverted && (cmp > 0))) {
1758 change = 1;
1759
1760 item = set->val.nodes[j - 1];
1761 set->val.nodes[j - 1] = set->val.nodes[j];
1762 set->val.nodes[j] = item;
1763 } else {
1764 /* whether node_pos1 should be smaller than node_pos2 or the other way around */
1765 inverted = !inverted;
1766 }
1767 }
1768
1769 ++ret;
1770
1771 if (!change) {
1772 break;
1773 }
1774 }
1775
Michal Vasko88a9e802022-05-24 10:50:28 +02001776#ifndef NDEBUG
Michal Vasko03ff5a72019-09-11 13:49:33 +02001777 LOGDBG(LY_LDGXPATH, "SORT END %d", ret);
1778 print_set_debug(set);
Michal Vasko88a9e802022-05-24 10:50:28 +02001779#endif
Michal Vasko03ff5a72019-09-11 13:49:33 +02001780
1781 /* check node hashes */
1782 if (set->used >= LYD_HT_MIN_ITEMS) {
1783 assert(set->ht);
1784 for (i = 0; i < set->used; ++i) {
1785 hnode.node = set->val.nodes[i].node;
1786 hnode.type = set->val.nodes[i].type;
1787
1788 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
1789 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
1790 hash = dict_hash_multi(hash, NULL, 0);
1791
1792 assert(!lyht_find(set->ht, &hnode, hash, NULL));
1793 }
1794 }
1795
1796 return ret - 1;
1797}
1798
Michal Vasko03ff5a72019-09-11 13:49:33 +02001799/**
1800 * @brief Merge 2 sorted sets into one.
1801 *
1802 * @param[in,out] trg Set to merge into. Duplicates are removed.
1803 * @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 +02001804 * @return LY_ERR
1805 */
1806static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001807set_sorted_merge(struct lyxp_set *trg, struct lyxp_set *src)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001808{
1809 uint32_t i, j, k, count, dup_count;
1810 int cmp;
1811 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001812
Michal Vaskod3678892020-05-21 10:06:58 +02001813 if ((trg->type != LYXP_SET_NODE_SET) || (src->type != LYXP_SET_NODE_SET)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001814 return LY_EINVAL;
1815 }
1816
Michal Vaskod3678892020-05-21 10:06:58 +02001817 if (!src->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001818 return LY_SUCCESS;
Michal Vaskod3678892020-05-21 10:06:58 +02001819 } else if (!trg->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001820 set_fill_set(trg, src);
Michal Vaskod3678892020-05-21 10:06:58 +02001821 lyxp_set_free_content(src);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001822 return LY_SUCCESS;
1823 }
1824
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001825 /* find first top-level node to be used as anchor for positions */
Michal Vasko0143b682021-12-13 17:13:09 +01001826 for (root = trg->tree; root->parent; root = lyd_parent(root)) {}
Michal Vaskod989ba02020-08-24 10:59:24 +02001827 for ( ; root->prev->next; root = root->prev) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001828
1829 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001830 if (set_assign_pos(trg, root, trg->root_type) || set_assign_pos(src, root, src->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001831 return LY_EINT;
1832 }
1833
1834#ifndef NDEBUG
1835 LOGDBG(LY_LDGXPATH, "MERGE target");
1836 print_set_debug(trg);
1837 LOGDBG(LY_LDGXPATH, "MERGE source");
1838 print_set_debug(src);
1839#endif
1840
1841 /* make memory for the merge (duplicates are not detected yet, so space
1842 * will likely be wasted on them, too bad) */
1843 if (trg->size - trg->used < src->used) {
1844 trg->size = trg->used + src->used;
1845
1846 trg->val.nodes = ly_realloc(trg->val.nodes, trg->size * sizeof *trg->val.nodes);
1847 LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx), LY_EMEM);
1848 }
1849
1850 i = 0;
1851 j = 0;
1852 count = 0;
1853 dup_count = 0;
1854 do {
1855 cmp = set_sort_compare(&src->val.nodes[i], &trg->val.nodes[j]);
1856 if (!cmp) {
1857 if (!count) {
1858 /* duplicate, just skip it */
1859 ++i;
1860 ++j;
1861 } else {
1862 /* we are copying something already, so let's copy the duplicate too,
1863 * we are hoping that afterwards there are some more nodes to
1864 * copy and this way we can copy them all together */
1865 ++count;
1866 ++dup_count;
1867 ++i;
1868 ++j;
1869 }
1870 } else if (cmp < 0) {
1871 /* inserting src node into trg, just remember it for now */
1872 ++count;
1873 ++i;
1874
1875 /* insert the hash now */
1876 set_insert_node_hash(trg, src->val.nodes[i - 1].node, src->val.nodes[i - 1].type);
1877 } else if (count) {
1878copy_nodes:
1879 /* time to actually copy the nodes, we have found the largest block of nodes */
1880 memmove(&trg->val.nodes[j + (count - dup_count)],
1881 &trg->val.nodes[j],
1882 (trg->used - j) * sizeof *trg->val.nodes);
1883 memcpy(&trg->val.nodes[j - dup_count], &src->val.nodes[i - count], count * sizeof *src->val.nodes);
1884
1885 trg->used += count - dup_count;
1886 /* do not change i, except the copying above, we are basically doing exactly what is in the else branch below */
1887 j += count - dup_count;
1888
1889 count = 0;
1890 dup_count = 0;
1891 } else {
1892 ++j;
1893 }
1894 } while ((i < src->used) && (j < trg->used));
1895
1896 if ((i < src->used) || count) {
1897 /* insert all the hashes first */
1898 for (k = i; k < src->used; ++k) {
1899 set_insert_node_hash(trg, src->val.nodes[k].node, src->val.nodes[k].type);
1900 }
1901
1902 /* loop ended, but we need to copy something at trg end */
1903 count += src->used - i;
1904 i = src->used;
1905 goto copy_nodes;
1906 }
1907
1908 /* we are inserting hashes before the actual node insert, which causes
1909 * situations when there were initially not enough items for a hash table,
1910 * but even after some were inserted, hash table was not created (during
1911 * insertion the number of items is not updated yet) */
1912 if (!trg->ht && (trg->used >= LYD_HT_MIN_ITEMS)) {
1913 set_insert_node_hash(trg, NULL, 0);
1914 }
1915
1916#ifndef NDEBUG
1917 LOGDBG(LY_LDGXPATH, "MERGE result");
1918 print_set_debug(trg);
1919#endif
1920
Michal Vaskod3678892020-05-21 10:06:58 +02001921 lyxp_set_free_content(src);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001922 return LY_SUCCESS;
1923}
1924
Michal Vasko14676352020-05-29 11:35:55 +02001925LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02001926lyxp_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 +02001927{
Michal Vasko004d3152020-06-11 19:59:22 +02001928 if (exp->used == tok_idx) {
Michal Vasko14676352020-05-29 11:35:55 +02001929 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001930 LOGVAL(ctx, LY_VCODE_XP_EOF);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001931 }
Michal Vasko14676352020-05-29 11:35:55 +02001932 return LY_EINCOMPLETE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001933 }
1934
Michal Vasko004d3152020-06-11 19:59:22 +02001935 if (want_tok && (exp->tokens[tok_idx] != want_tok)) {
Michal Vasko14676352020-05-29 11:35:55 +02001936 if (ctx) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02001937 LOGVAL(ctx, LY_VCODE_XP_INTOK2, lyxp_token2str(exp->tokens[tok_idx]),
1938 &exp->expr[exp->tok_pos[tok_idx]], lyxp_token2str(want_tok));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001939 }
Michal Vasko14676352020-05-29 11:35:55 +02001940 return LY_ENOT;
1941 }
1942
1943 return LY_SUCCESS;
1944}
1945
Michal Vasko004d3152020-06-11 19:59:22 +02001946LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02001947lyxp_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 +02001948{
1949 LY_CHECK_RET(lyxp_check_token(ctx, exp, *tok_idx, want_tok));
1950
1951 /* skip the token */
1952 ++(*tok_idx);
1953
1954 return LY_SUCCESS;
1955}
1956
Michal Vasko14676352020-05-29 11:35:55 +02001957/* just like lyxp_check_token() but tests for 2 tokens */
1958static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02001959exp_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 +02001960 enum lyxp_token want_tok2)
Michal Vasko14676352020-05-29 11:35:55 +02001961{
Michal Vasko004d3152020-06-11 19:59:22 +02001962 if (exp->used == tok_idx) {
Michal Vasko14676352020-05-29 11:35:55 +02001963 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001964 LOGVAL(ctx, LY_VCODE_XP_EOF);
Michal Vasko14676352020-05-29 11:35:55 +02001965 }
1966 return LY_EINCOMPLETE;
1967 }
1968
Michal Vasko004d3152020-06-11 19:59:22 +02001969 if ((exp->tokens[tok_idx] != want_tok1) && (exp->tokens[tok_idx] != want_tok2)) {
Michal Vasko14676352020-05-29 11:35:55 +02001970 if (ctx) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02001971 LOGVAL(ctx, LY_VCODE_XP_INTOK, lyxp_token2str(exp->tokens[tok_idx]),
Michal Vasko0b468e62020-10-19 09:33:04 +02001972 &exp->expr[exp->tok_pos[tok_idx]]);
Michal Vasko14676352020-05-29 11:35:55 +02001973 }
1974 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001975 }
1976
1977 return LY_SUCCESS;
1978}
1979
Michal Vasko4911eeb2021-06-28 11:23:05 +02001980LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02001981lyxp_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 +02001982 enum lyxp_token want_tok2)
1983{
1984 LY_CHECK_RET(exp_check_token2(ctx, exp, *tok_idx, want_tok1, want_tok2));
1985
1986 /* skip the token */
1987 ++(*tok_idx);
1988
1989 return LY_SUCCESS;
1990}
1991
Michal Vasko03ff5a72019-09-11 13:49:33 +02001992/**
1993 * @brief Stack operation push on the repeat array.
1994 *
1995 * @param[in] exp Expression to use.
Michal Vasko004d3152020-06-11 19:59:22 +02001996 * @param[in] tok_idx Position in the expresion \p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001997 * @param[in] repeat_op_idx Index from \p exp of the operator token. This value is pushed.
1998 */
1999static void
Michal Vaskodd528af2022-08-08 14:35:07 +02002000exp_repeat_push(struct lyxp_expr *exp, uint32_t tok_idx, uint32_t repeat_op_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002001{
Michal Vaskodd528af2022-08-08 14:35:07 +02002002 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002003
Michal Vasko004d3152020-06-11 19:59:22 +02002004 if (exp->repeat[tok_idx]) {
Radek Krejci1e008d22020-08-17 11:37:37 +02002005 for (i = 0; exp->repeat[tok_idx][i]; ++i) {}
Michal Vasko004d3152020-06-11 19:59:22 +02002006 exp->repeat[tok_idx] = realloc(exp->repeat[tok_idx], (i + 2) * sizeof *exp->repeat[tok_idx]);
2007 LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), );
2008 exp->repeat[tok_idx][i] = repeat_op_idx;
2009 exp->repeat[tok_idx][i + 1] = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002010 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02002011 exp->repeat[tok_idx] = calloc(2, sizeof *exp->repeat[tok_idx]);
2012 LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), );
2013 exp->repeat[tok_idx][0] = repeat_op_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002014 }
2015}
2016
2017/**
2018 * @brief Reparse Predicate. Logs directly on error.
2019 *
2020 * [7] Predicate ::= '[' Expr ']'
2021 *
2022 * @param[in] ctx Context for logging.
2023 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002024 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002025 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002026 * @return LY_ERR
2027 */
2028static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02002029reparse_predicate(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint32_t *tok_idx, uint32_t depth)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002030{
2031 LY_ERR rc;
2032
Michal Vasko004d3152020-06-11 19:59:22 +02002033 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002034 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002035 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002036
aPiecekbf968d92021-05-27 14:35:05 +02002037 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002038 LY_CHECK_RET(rc);
2039
Michal Vasko004d3152020-06-11 19:59:22 +02002040 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002041 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002042 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002043
2044 return LY_SUCCESS;
2045}
2046
2047/**
2048 * @brief Reparse RelativeLocationPath. Logs directly on error.
2049 *
2050 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
2051 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
2052 * [6] NodeTest ::= NameTest | NodeType '(' ')'
2053 *
2054 * @param[in] ctx Context for logging.
2055 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002056 * @param[in] tok_idx Position in the expression \p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002057 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002058 * @return LY_ERR (LY_EINCOMPLETE on forward reference)
2059 */
2060static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02002061reparse_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 +02002062{
2063 LY_ERR rc;
2064
Michal Vasko004d3152020-06-11 19:59:22 +02002065 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002066 LY_CHECK_RET(rc);
2067
2068 goto step;
2069 do {
2070 /* '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02002071 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002072
Michal Vasko004d3152020-06-11 19:59:22 +02002073 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002074 LY_CHECK_RET(rc);
2075step:
2076 /* Step */
Michal Vasko004d3152020-06-11 19:59:22 +02002077 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002078 case LYXP_TOKEN_DOT:
Michal Vasko004d3152020-06-11 19:59:22 +02002079 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002080 break;
2081
2082 case LYXP_TOKEN_DDOT:
Michal Vasko004d3152020-06-11 19:59:22 +02002083 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002084 break;
2085
Michal Vasko49fec8e2022-05-24 10:28:33 +02002086 case LYXP_TOKEN_AXISNAME:
2087 ++(*tok_idx);
2088
2089 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_DCOLON);
2090 LY_CHECK_RET(rc);
2091
2092 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02002093 case LYXP_TOKEN_AT:
Michal Vasko004d3152020-06-11 19:59:22 +02002094 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002095
Michal Vasko004d3152020-06-11 19:59:22 +02002096 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002097 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002098 if ((exp->tokens[*tok_idx] != LYXP_TOKEN_NAMETEST) && (exp->tokens[*tok_idx] != LYXP_TOKEN_NODETYPE)) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02002099 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 +02002100 return LY_EVALID;
2101 }
Michal Vasko49fec8e2022-05-24 10:28:33 +02002102 if (exp->tokens[*tok_idx] == LYXP_TOKEN_NODETYPE) {
2103 goto reparse_nodetype;
2104 }
Radek Krejci0f969882020-08-21 16:56:47 +02002105 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02002106 case LYXP_TOKEN_NAMETEST:
Michal Vasko004d3152020-06-11 19:59:22 +02002107 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002108 goto reparse_predicate;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002109
2110 case LYXP_TOKEN_NODETYPE:
Michal Vasko49fec8e2022-05-24 10:28:33 +02002111reparse_nodetype:
Michal Vasko004d3152020-06-11 19:59:22 +02002112 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002113
2114 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02002115 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002116 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002117 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002118
2119 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02002120 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002121 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002122 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002123
2124reparse_predicate:
2125 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002126 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
aPiecekbf968d92021-05-27 14:35:05 +02002127 rc = reparse_predicate(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002128 LY_CHECK_RET(rc);
2129 }
2130 break;
2131 default:
Michal Vasko49fec8e2022-05-24 10:28:33 +02002132 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 +02002133 return LY_EVALID;
2134 }
Michal Vasko004d3152020-06-11 19:59:22 +02002135 } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02002136
2137 return LY_SUCCESS;
2138}
2139
2140/**
2141 * @brief Reparse AbsoluteLocationPath. Logs directly on error.
2142 *
2143 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
2144 *
2145 * @param[in] ctx Context for logging.
2146 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002147 * @param[in] tok_idx Position in the expression \p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002148 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002149 * @return LY_ERR
2150 */
2151static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02002152reparse_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 +02002153{
2154 LY_ERR rc;
2155
Michal Vasko004d3152020-06-11 19:59:22 +02002156 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 +02002157
2158 /* '/' RelativeLocationPath? */
Michal Vasko004d3152020-06-11 19:59:22 +02002159 if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002160 /* '/' */
Michal Vasko004d3152020-06-11 19:59:22 +02002161 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002162
Michal Vasko004d3152020-06-11 19:59:22 +02002163 if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002164 return LY_SUCCESS;
2165 }
Michal Vasko004d3152020-06-11 19:59:22 +02002166 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002167 case LYXP_TOKEN_DOT:
2168 case LYXP_TOKEN_DDOT:
Michal Vasko49fec8e2022-05-24 10:28:33 +02002169 case LYXP_TOKEN_AXISNAME:
Michal Vasko03ff5a72019-09-11 13:49:33 +02002170 case LYXP_TOKEN_AT:
2171 case LYXP_TOKEN_NAMETEST:
2172 case LYXP_TOKEN_NODETYPE:
aPiecekbf968d92021-05-27 14:35:05 +02002173 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002174 LY_CHECK_RET(rc);
Radek Krejci0f969882020-08-21 16:56:47 +02002175 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02002176 default:
2177 break;
2178 }
2179
Michal Vasko03ff5a72019-09-11 13:49:33 +02002180 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02002181 /* '//' RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002182 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002183
aPiecekbf968d92021-05-27 14:35:05 +02002184 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002185 LY_CHECK_RET(rc);
2186 }
2187
2188 return LY_SUCCESS;
2189}
2190
2191/**
2192 * @brief Reparse FunctionCall. Logs directly on error.
2193 *
2194 * [9] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
2195 *
2196 * @param[in] ctx Context for logging.
2197 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002198 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002199 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002200 * @return LY_ERR
2201 */
2202static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02002203reparse_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 +02002204{
Radek Krejci1deb5be2020-08-26 16:43:36 +02002205 int8_t min_arg_count = -1;
Michal Vaskodd528af2022-08-08 14:35:07 +02002206 uint32_t arg_count, max_arg_count = 0, func_tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002207 LY_ERR rc;
2208
Michal Vasko004d3152020-06-11 19:59:22 +02002209 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_FUNCNAME);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002210 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002211 func_tok_idx = *tok_idx;
2212 switch (exp->tok_len[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002213 case 3:
Michal Vasko004d3152020-06-11 19:59:22 +02002214 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002215 min_arg_count = 1;
2216 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002217 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002218 min_arg_count = 1;
2219 max_arg_count = 1;
2220 }
2221 break;
2222 case 4:
Michal Vasko004d3152020-06-11 19:59:22 +02002223 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002224 min_arg_count = 1;
2225 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002226 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002227 min_arg_count = 0;
2228 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002229 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002230 min_arg_count = 0;
2231 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002232 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002233 min_arg_count = 0;
2234 max_arg_count = 0;
2235 }
2236 break;
2237 case 5:
Michal Vasko004d3152020-06-11 19:59:22 +02002238 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002239 min_arg_count = 1;
2240 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002241 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002242 min_arg_count = 0;
2243 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002244 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 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]], "round", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002248 min_arg_count = 1;
2249 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002250 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002251 min_arg_count = 1;
2252 max_arg_count = 1;
2253 }
2254 break;
2255 case 6:
Michal Vasko004d3152020-06-11 19:59:22 +02002256 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002257 min_arg_count = 2;
Radek Krejci1deb5be2020-08-26 16:43:36 +02002258 max_arg_count = UINT32_MAX;
Michal Vasko004d3152020-06-11 19:59:22 +02002259 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002260 min_arg_count = 0;
2261 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002262 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002263 min_arg_count = 0;
2264 max_arg_count = 1;
2265 }
2266 break;
2267 case 7:
Michal Vasko004d3152020-06-11 19:59:22 +02002268 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 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]], "ceiling", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002272 min_arg_count = 1;
2273 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002274 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002275 min_arg_count = 0;
2276 max_arg_count = 0;
2277 }
2278 break;
2279 case 8:
Michal Vasko004d3152020-06-11 19:59:22 +02002280 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002281 min_arg_count = 2;
2282 max_arg_count = 2;
Michal Vasko004d3152020-06-11 19:59:22 +02002283 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002284 min_arg_count = 0;
2285 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002286 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002287 min_arg_count = 2;
2288 max_arg_count = 2;
2289 }
2290 break;
2291 case 9:
Michal Vasko004d3152020-06-11 19:59:22 +02002292 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002293 min_arg_count = 2;
2294 max_arg_count = 3;
Michal Vasko004d3152020-06-11 19:59:22 +02002295 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002296 min_arg_count = 3;
2297 max_arg_count = 3;
2298 }
2299 break;
2300 case 10:
Michal Vasko004d3152020-06-11 19:59:22 +02002301 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002302 min_arg_count = 0;
2303 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002304 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002305 min_arg_count = 1;
2306 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002307 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002308 min_arg_count = 2;
2309 max_arg_count = 2;
2310 }
2311 break;
2312 case 11:
Michal Vasko004d3152020-06-11 19:59:22 +02002313 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002314 min_arg_count = 2;
2315 max_arg_count = 2;
2316 }
2317 break;
2318 case 12:
Michal Vasko004d3152020-06-11 19:59:22 +02002319 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002320 min_arg_count = 2;
2321 max_arg_count = 2;
2322 }
2323 break;
2324 case 13:
Michal Vasko004d3152020-06-11 19:59:22 +02002325 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002326 min_arg_count = 0;
2327 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002328 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002329 min_arg_count = 0;
2330 max_arg_count = 1;
2331 }
2332 break;
2333 case 15:
Michal Vasko004d3152020-06-11 19:59:22 +02002334 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002335 min_arg_count = 0;
2336 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002337 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002338 min_arg_count = 2;
2339 max_arg_count = 2;
2340 }
2341 break;
2342 case 16:
Michal Vasko004d3152020-06-11 19:59:22 +02002343 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002344 min_arg_count = 2;
2345 max_arg_count = 2;
2346 }
2347 break;
2348 case 20:
Michal Vasko004d3152020-06-11 19:59:22 +02002349 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002350 min_arg_count = 2;
2351 max_arg_count = 2;
2352 }
2353 break;
2354 }
2355 if (min_arg_count == -1) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002356 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 +02002357 return LY_EINVAL;
2358 }
Michal Vasko004d3152020-06-11 19:59:22 +02002359 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002360
2361 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02002362 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002363 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002364 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002365
2366 /* ( Expr ( ',' Expr )* )? */
2367 arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002368 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002369 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002370 if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002371 ++arg_count;
aPiecekbf968d92021-05-27 14:35:05 +02002372 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002373 LY_CHECK_RET(rc);
2374 }
Michal Vasko004d3152020-06-11 19:59:22 +02002375 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) {
2376 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002377
2378 ++arg_count;
aPiecekbf968d92021-05-27 14:35:05 +02002379 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002380 LY_CHECK_RET(rc);
2381 }
2382
2383 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02002384 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002385 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002386 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002387
Radek Krejci857189e2020-09-01 13:26:36 +02002388 if ((arg_count < (uint32_t)min_arg_count) || (arg_count > max_arg_count)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002389 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 +02002390 return LY_EVALID;
2391 }
2392
2393 return LY_SUCCESS;
2394}
2395
2396/**
2397 * @brief Reparse PathExpr. Logs directly on error.
2398 *
2399 * [10] PathExpr ::= LocationPath | PrimaryExpr Predicate*
2400 * | PrimaryExpr Predicate* '/' RelativeLocationPath
2401 * | PrimaryExpr Predicate* '//' RelativeLocationPath
2402 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
aPiecekfba75362021-10-07 12:39:48 +02002403 * [8] PrimaryExpr ::= VariableReference | '(' Expr ')' | Literal | Number | FunctionCall
Michal Vasko03ff5a72019-09-11 13:49:33 +02002404 *
2405 * @param[in] ctx Context for logging.
2406 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002407 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002408 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002409 * @return LY_ERR
2410 */
2411static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02002412reparse_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 +02002413{
2414 LY_ERR rc;
2415
Michal Vasko004d3152020-06-11 19:59:22 +02002416 if (lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko14676352020-05-29 11:35:55 +02002417 return LY_EVALID;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002418 }
2419
Michal Vasko004d3152020-06-11 19:59:22 +02002420 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002421 case LYXP_TOKEN_PAR1:
2422 /* '(' Expr ')' Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002423 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002424
aPiecekbf968d92021-05-27 14:35:05 +02002425 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002426 LY_CHECK_RET(rc);
2427
Michal Vasko004d3152020-06-11 19:59:22 +02002428 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002429 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002430 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002431 goto predicate;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002432 case LYXP_TOKEN_DOT:
2433 case LYXP_TOKEN_DDOT:
Michal Vasko49fec8e2022-05-24 10:28:33 +02002434 case LYXP_TOKEN_AXISNAME:
Michal Vasko03ff5a72019-09-11 13:49:33 +02002435 case LYXP_TOKEN_AT:
2436 case LYXP_TOKEN_NAMETEST:
2437 case LYXP_TOKEN_NODETYPE:
2438 /* RelativeLocationPath */
aPiecekbf968d92021-05-27 14:35:05 +02002439 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002440 LY_CHECK_RET(rc);
2441 break;
aPiecekfba75362021-10-07 12:39:48 +02002442 case LYXP_TOKEN_VARREF:
2443 /* VariableReference */
2444 ++(*tok_idx);
2445 goto predicate;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002446 case LYXP_TOKEN_FUNCNAME:
2447 /* FunctionCall */
aPiecekbf968d92021-05-27 14:35:05 +02002448 rc = reparse_function_call(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002449 LY_CHECK_RET(rc);
2450 goto predicate;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002451 case LYXP_TOKEN_OPER_PATH:
2452 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02002453 /* AbsoluteLocationPath */
aPiecekbf968d92021-05-27 14:35:05 +02002454 rc = reparse_absolute_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002455 LY_CHECK_RET(rc);
2456 break;
2457 case LYXP_TOKEN_LITERAL:
2458 /* Literal */
Michal Vasko004d3152020-06-11 19:59:22 +02002459 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002460 goto predicate;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002461 case LYXP_TOKEN_NUMBER:
2462 /* Number */
Michal Vasko004d3152020-06-11 19:59:22 +02002463 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002464 goto predicate;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002465 default:
Michal Vasko49fec8e2022-05-24 10:28:33 +02002466 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 +02002467 return LY_EVALID;
2468 }
2469
2470 return LY_SUCCESS;
2471
2472predicate:
2473 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002474 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
aPiecekbf968d92021-05-27 14:35:05 +02002475 rc = reparse_predicate(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002476 LY_CHECK_RET(rc);
2477 }
2478
2479 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002480 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002481
2482 /* '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02002483 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002484
aPiecekbf968d92021-05-27 14:35:05 +02002485 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002486 LY_CHECK_RET(rc);
2487 }
2488
2489 return LY_SUCCESS;
2490}
2491
2492/**
2493 * @brief Reparse UnaryExpr. Logs directly on error.
2494 *
2495 * [17] UnaryExpr ::= UnionExpr | '-' UnaryExpr
2496 * [18] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
2497 *
2498 * @param[in] ctx Context for logging.
2499 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002500 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002501 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002502 * @return LY_ERR
2503 */
2504static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02002505reparse_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 +02002506{
Michal Vaskodd528af2022-08-08 14:35:07 +02002507 uint32_t prev_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002508 LY_ERR rc;
2509
2510 /* ('-')* */
Michal Vasko004d3152020-06-11 19:59:22 +02002511 prev_exp = *tok_idx;
Michal Vasko69730152020-10-09 16:30:07 +02002512 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2513 (exp->expr[exp->tok_pos[*tok_idx]] == '-')) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002514 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNARY);
Michal Vasko004d3152020-06-11 19:59:22 +02002515 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002516 }
2517
2518 /* PathExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002519 prev_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002520 rc = reparse_path_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002521 LY_CHECK_RET(rc);
2522
2523 /* ('|' PathExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002524 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_UNI)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002525 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNION);
Michal Vasko004d3152020-06-11 19:59:22 +02002526 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002527
aPiecekbf968d92021-05-27 14:35:05 +02002528 rc = reparse_path_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002529 LY_CHECK_RET(rc);
2530 }
2531
2532 return LY_SUCCESS;
2533}
2534
2535/**
2536 * @brief Reparse AdditiveExpr. Logs directly on error.
2537 *
2538 * [15] AdditiveExpr ::= MultiplicativeExpr
2539 * | AdditiveExpr '+' MultiplicativeExpr
2540 * | AdditiveExpr '-' MultiplicativeExpr
2541 * [16] MultiplicativeExpr ::= UnaryExpr
2542 * | MultiplicativeExpr '*' UnaryExpr
2543 * | MultiplicativeExpr 'div' UnaryExpr
2544 * | MultiplicativeExpr 'mod' UnaryExpr
2545 *
2546 * @param[in] ctx Context for logging.
2547 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002548 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002549 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002550 * @return LY_ERR
2551 */
2552static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02002553reparse_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 +02002554{
Michal Vaskodd528af2022-08-08 14:35:07 +02002555 uint32_t prev_add_exp, prev_mul_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002556 LY_ERR rc;
2557
Michal Vasko004d3152020-06-11 19:59:22 +02002558 prev_add_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002559 goto reparse_multiplicative_expr;
2560
2561 /* ('+' / '-' MultiplicativeExpr)* */
Michal Vasko69730152020-10-09 16:30:07 +02002562 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2563 ((exp->expr[exp->tok_pos[*tok_idx]] == '+') || (exp->expr[exp->tok_pos[*tok_idx]] == '-'))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002564 exp_repeat_push(exp, prev_add_exp, LYXP_EXPR_ADDITIVE);
Michal Vasko004d3152020-06-11 19:59:22 +02002565 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002566
2567reparse_multiplicative_expr:
2568 /* UnaryExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002569 prev_mul_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002570 rc = reparse_unary_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002571 LY_CHECK_RET(rc);
2572
2573 /* ('*' / 'div' / 'mod' UnaryExpr)* */
Michal Vasko69730152020-10-09 16:30:07 +02002574 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2575 ((exp->expr[exp->tok_pos[*tok_idx]] == '*') || (exp->tok_len[*tok_idx] == 3))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002576 exp_repeat_push(exp, prev_mul_exp, LYXP_EXPR_MULTIPLICATIVE);
Michal Vasko004d3152020-06-11 19:59:22 +02002577 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002578
aPiecekbf968d92021-05-27 14:35:05 +02002579 rc = reparse_unary_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002580 LY_CHECK_RET(rc);
2581 }
2582 }
2583
2584 return LY_SUCCESS;
2585}
2586
2587/**
2588 * @brief Reparse EqualityExpr. Logs directly on error.
2589 *
2590 * [13] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
2591 * | EqualityExpr '!=' RelationalExpr
2592 * [14] RelationalExpr ::= AdditiveExpr
2593 * | RelationalExpr '<' AdditiveExpr
2594 * | RelationalExpr '>' AdditiveExpr
2595 * | RelationalExpr '<=' AdditiveExpr
2596 * | RelationalExpr '>=' AdditiveExpr
2597 *
2598 * @param[in] ctx Context for logging.
2599 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002600 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002601 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002602 * @return LY_ERR
2603 */
2604static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02002605reparse_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 +02002606{
Michal Vaskodd528af2022-08-08 14:35:07 +02002607 uint32_t prev_eq_exp, prev_rel_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002608 LY_ERR rc;
2609
Michal Vasko004d3152020-06-11 19:59:22 +02002610 prev_eq_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002611 goto reparse_additive_expr;
2612
2613 /* ('=' / '!=' RelationalExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002614 while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_EQUAL, LYXP_TOKEN_OPER_NEQUAL)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002615 exp_repeat_push(exp, prev_eq_exp, LYXP_EXPR_EQUALITY);
Michal Vasko004d3152020-06-11 19:59:22 +02002616 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002617
2618reparse_additive_expr:
2619 /* AdditiveExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002620 prev_rel_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002621 rc = reparse_additive_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002622 LY_CHECK_RET(rc);
2623
2624 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002625 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_COMP)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002626 exp_repeat_push(exp, prev_rel_exp, LYXP_EXPR_RELATIONAL);
Michal Vasko004d3152020-06-11 19:59:22 +02002627 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002628
aPiecekbf968d92021-05-27 14:35:05 +02002629 rc = reparse_additive_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002630 LY_CHECK_RET(rc);
2631 }
2632 }
2633
2634 return LY_SUCCESS;
2635}
2636
2637/**
2638 * @brief Reparse OrExpr. Logs directly on error.
2639 *
2640 * [11] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
2641 * [12] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
2642 *
2643 * @param[in] ctx Context for logging.
2644 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002645 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002646 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002647 * @return LY_ERR
2648 */
2649static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02002650reparse_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 +02002651{
Michal Vaskodd528af2022-08-08 14:35:07 +02002652 uint32_t prev_or_exp, prev_and_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002653 LY_ERR rc;
2654
aPiecekbf968d92021-05-27 14:35:05 +02002655 ++depth;
2656 LY_CHECK_ERR_RET(depth > LYXP_MAX_BLOCK_DEPTH, LOGVAL(ctx, LY_VCODE_XP_DEPTH), LY_EINVAL);
2657
Michal Vasko004d3152020-06-11 19:59:22 +02002658 prev_or_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002659 goto reparse_equality_expr;
2660
2661 /* ('or' AndExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002662 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 +02002663 exp_repeat_push(exp, prev_or_exp, LYXP_EXPR_OR);
Michal Vasko004d3152020-06-11 19:59:22 +02002664 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002665
2666reparse_equality_expr:
2667 /* EqualityExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002668 prev_and_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002669 rc = reparse_equality_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002670 LY_CHECK_RET(rc);
2671
2672 /* ('and' EqualityExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002673 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 +02002674 exp_repeat_push(exp, prev_and_exp, LYXP_EXPR_AND);
Michal Vasko004d3152020-06-11 19:59:22 +02002675 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002676
aPiecekbf968d92021-05-27 14:35:05 +02002677 rc = reparse_equality_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002678 LY_CHECK_RET(rc);
2679 }
2680 }
2681
2682 return LY_SUCCESS;
2683}
Radek Krejcib1646a92018-11-02 16:08:26 +01002684
2685/**
2686 * @brief Parse NCName.
2687 *
2688 * @param[in] ncname Name to parse.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002689 * @return Length of @p ncname valid bytes.
Radek Krejcib1646a92018-11-02 16:08:26 +01002690 */
Michal Vasko49fec8e2022-05-24 10:28:33 +02002691static ssize_t
Radek Krejcib1646a92018-11-02 16:08:26 +01002692parse_ncname(const char *ncname)
2693{
Radek Krejci1deb5be2020-08-26 16:43:36 +02002694 uint32_t uc;
Radek Krejcid4270262019-01-07 15:07:25 +01002695 size_t size;
Michal Vasko49fec8e2022-05-24 10:28:33 +02002696 ssize_t len = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002697
2698 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), 0);
2699 if (!is_xmlqnamestartchar(uc) || (uc == ':')) {
2700 return len;
2701 }
2702
2703 do {
2704 len += size;
Radek Krejci9a564c92019-01-07 14:53:57 +01002705 if (!*ncname) {
2706 break;
2707 }
Radek Krejcid4270262019-01-07 15:07:25 +01002708 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), -len);
Radek Krejcib1646a92018-11-02 16:08:26 +01002709 } while (is_xmlqnamechar(uc) && (uc != ':'));
2710
2711 return len;
2712}
2713
2714/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02002715 * @brief Add @p token into the expression @p exp.
Radek Krejcib1646a92018-11-02 16:08:26 +01002716 *
Michal Vasko03ff5a72019-09-11 13:49:33 +02002717 * @param[in] ctx Context for logging.
Radek Krejcib1646a92018-11-02 16:08:26 +01002718 * @param[in] exp Expression to use.
2719 * @param[in] token Token to add.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002720 * @param[in] tok_pos Token position in the XPath expression.
Radek Krejcib1646a92018-11-02 16:08:26 +01002721 * @param[in] tok_len Token length in the XPath expression.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002722 * @return LY_ERR
Radek Krejcib1646a92018-11-02 16:08:26 +01002723 */
2724static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02002725exp_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 +01002726{
2727 uint32_t prev;
2728
2729 if (exp->used == exp->size) {
2730 prev = exp->size;
2731 exp->size += LYXP_EXPR_SIZE_STEP;
2732 if (prev > exp->size) {
2733 LOGINT(ctx);
2734 return LY_EINT;
2735 }
2736
2737 exp->tokens = ly_realloc(exp->tokens, exp->size * sizeof *exp->tokens);
2738 LY_CHECK_ERR_RET(!exp->tokens, LOGMEM(ctx), LY_EMEM);
2739 exp->tok_pos = ly_realloc(exp->tok_pos, exp->size * sizeof *exp->tok_pos);
2740 LY_CHECK_ERR_RET(!exp->tok_pos, LOGMEM(ctx), LY_EMEM);
2741 exp->tok_len = ly_realloc(exp->tok_len, exp->size * sizeof *exp->tok_len);
2742 LY_CHECK_ERR_RET(!exp->tok_len, LOGMEM(ctx), LY_EMEM);
2743 }
2744
2745 exp->tokens[exp->used] = token;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002746 exp->tok_pos[exp->used] = tok_pos;
Radek Krejcib1646a92018-11-02 16:08:26 +01002747 exp->tok_len[exp->used] = tok_len;
2748 ++exp->used;
2749 return LY_SUCCESS;
2750}
2751
2752void
Michal Vasko14676352020-05-29 11:35:55 +02002753lyxp_expr_free(const struct ly_ctx *ctx, struct lyxp_expr *expr)
Radek Krejcib1646a92018-11-02 16:08:26 +01002754{
Michal Vaskodd528af2022-08-08 14:35:07 +02002755 uint32_t i;
Radek Krejcib1646a92018-11-02 16:08:26 +01002756
2757 if (!expr) {
2758 return;
2759 }
2760
2761 lydict_remove(ctx, expr->expr);
2762 free(expr->tokens);
2763 free(expr->tok_pos);
2764 free(expr->tok_len);
2765 if (expr->repeat) {
2766 for (i = 0; i < expr->used; ++i) {
2767 free(expr->repeat[i]);
2768 }
2769 }
2770 free(expr->repeat);
2771 free(expr);
2772}
2773
Michal Vasko49fec8e2022-05-24 10:28:33 +02002774/**
2775 * @brief Parse Axis name.
2776 *
2777 * @param[in] str String to parse.
2778 * @param[in] str_len Length of @p str.
2779 * @return LY_SUCCESS if an axis.
2780 * @return LY_ENOT otherwise.
2781 */
2782static LY_ERR
2783expr_parse_axis(const char *str, size_t str_len)
2784{
2785 switch (str_len) {
2786 case 4:
2787 if (!strncmp("self", str, str_len)) {
2788 return LY_SUCCESS;
2789 }
2790 break;
2791 case 5:
2792 if (!strncmp("child", str, str_len)) {
2793 return LY_SUCCESS;
2794 }
2795 break;
2796 case 6:
2797 if (!strncmp("parent", str, str_len)) {
2798 return LY_SUCCESS;
2799 }
2800 break;
2801 case 8:
2802 if (!strncmp("ancestor", str, str_len)) {
2803 return LY_SUCCESS;
2804 }
2805 break;
2806 case 9:
2807 if (!strncmp("attribute", str, str_len)) {
2808 return LY_SUCCESS;
2809 } else if (!strncmp("following", str, str_len)) {
2810 return LY_SUCCESS;
2811 } else if (!strncmp("namespace", str, str_len)) {
2812 LOGERR(NULL, LY_EINVAL, "Axis \"namespace\" not supported.");
2813 return LY_ENOT;
2814 } else if (!strncmp("preceding", str, str_len)) {
2815 return LY_SUCCESS;
2816 }
2817 break;
2818 case 10:
2819 if (!strncmp("descendant", str, str_len)) {
2820 return LY_SUCCESS;
2821 }
2822 break;
2823 case 16:
2824 if (!strncmp("ancestor-or-self", str, str_len)) {
2825 return LY_SUCCESS;
2826 }
2827 break;
2828 case 17:
2829 if (!strncmp("following-sibling", str, str_len)) {
2830 return LY_SUCCESS;
2831 } else if (!strncmp("preceding-sibling", str, str_len)) {
2832 return LY_SUCCESS;
2833 }
2834 break;
2835 case 18:
2836 if (!strncmp("descendant-or-self", str, str_len)) {
2837 return LY_SUCCESS;
2838 }
2839 break;
2840 }
2841
2842 return LY_ENOT;
2843}
2844
Radek Krejcif03a9e22020-09-18 20:09:31 +02002845LY_ERR
2846lyxp_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 +01002847{
Radek Krejcif03a9e22020-09-18 20:09:31 +02002848 LY_ERR ret = LY_SUCCESS;
2849 struct lyxp_expr *expr;
Radek Krejcid4270262019-01-07 15:07:25 +01002850 size_t parsed = 0, tok_len;
Radek Krejcib1646a92018-11-02 16:08:26 +01002851 enum lyxp_token tok_type;
Michal Vasko49fec8e2022-05-24 10:28:33 +02002852 ly_bool prev_func_check = 0, prev_ntype_check = 0, has_axis;
Michal Vaskodd528af2022-08-08 14:35:07 +02002853 uint32_t tok_idx = 0;
Michal Vasko49fec8e2022-05-24 10:28:33 +02002854 ssize_t ncname_len;
Radek Krejcib1646a92018-11-02 16:08:26 +01002855
Radek Krejcif03a9e22020-09-18 20:09:31 +02002856 assert(expr_p);
2857
2858 if (!expr_str[0]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002859 LOGVAL(ctx, LY_VCODE_XP_EOF);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002860 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02002861 }
2862
2863 if (!expr_len) {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002864 expr_len = strlen(expr_str);
Michal Vasko004d3152020-06-11 19:59:22 +02002865 }
Michal Vaskodd528af2022-08-08 14:35:07 +02002866 if (expr_len > UINT32_MAX) {
2867 LOGVAL(ctx, LYVE_XPATH, "XPath expression cannot be longer than %" PRIu32 " characters.", UINT32_MAX);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002868 return LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01002869 }
2870
2871 /* init lyxp_expr structure */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002872 expr = calloc(1, sizeof *expr);
2873 LY_CHECK_ERR_GOTO(!expr, LOGMEM(ctx); ret = LY_EMEM, error);
2874 LY_CHECK_GOTO(ret = lydict_insert(ctx, expr_str, expr_len, &expr->expr), error);
2875 expr->used = 0;
2876 expr->size = LYXP_EXPR_SIZE_START;
2877 expr->tokens = malloc(expr->size * sizeof *expr->tokens);
2878 LY_CHECK_ERR_GOTO(!expr->tokens, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002879
Radek Krejcif03a9e22020-09-18 20:09:31 +02002880 expr->tok_pos = malloc(expr->size * sizeof *expr->tok_pos);
2881 LY_CHECK_ERR_GOTO(!expr->tok_pos, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002882
Radek Krejcif03a9e22020-09-18 20:09:31 +02002883 expr->tok_len = malloc(expr->size * sizeof *expr->tok_len);
2884 LY_CHECK_ERR_GOTO(!expr->tok_len, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002885
Michal Vasko004d3152020-06-11 19:59:22 +02002886 /* make expr 0-terminated */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002887 expr_str = expr->expr;
Michal Vasko004d3152020-06-11 19:59:22 +02002888
Radek Krejcif03a9e22020-09-18 20:09:31 +02002889 while (is_xmlws(expr_str[parsed])) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002890 ++parsed;
2891 }
2892
2893 do {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002894 if (expr_str[parsed] == '(') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002895
2896 /* '(' */
2897 tok_len = 1;
2898 tok_type = LYXP_TOKEN_PAR1;
2899
Michal Vasko49fec8e2022-05-24 10:28:33 +02002900 if (prev_ntype_check && expr->used && (expr->tokens[expr->used - 1] == LYXP_TOKEN_NAMETEST) &&
2901 (((expr->tok_len[expr->used - 1] == 4) &&
2902 (!strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "node", 4) ||
2903 !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "text", 4))) ||
2904 ((expr->tok_len[expr->used - 1] == 7) &&
2905 !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "comment", 7)))) {
2906 /* it is NodeType after all */
2907 expr->tokens[expr->used - 1] = LYXP_TOKEN_NODETYPE;
2908
2909 prev_ntype_check = 0;
2910 prev_func_check = 0;
2911 } else if (prev_func_check && expr->used && (expr->tokens[expr->used - 1] == LYXP_TOKEN_NAMETEST)) {
2912 /* it is FunctionName after all */
2913 expr->tokens[expr->used - 1] = LYXP_TOKEN_FUNCNAME;
2914
2915 prev_ntype_check = 0;
2916 prev_func_check = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002917 }
2918
Radek Krejcif03a9e22020-09-18 20:09:31 +02002919 } else if (expr_str[parsed] == ')') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002920
2921 /* ')' */
2922 tok_len = 1;
2923 tok_type = LYXP_TOKEN_PAR2;
2924
Radek Krejcif03a9e22020-09-18 20:09:31 +02002925 } else if (expr_str[parsed] == '[') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002926
2927 /* '[' */
2928 tok_len = 1;
2929 tok_type = LYXP_TOKEN_BRACK1;
2930
Radek Krejcif03a9e22020-09-18 20:09:31 +02002931 } else if (expr_str[parsed] == ']') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002932
2933 /* ']' */
2934 tok_len = 1;
2935 tok_type = LYXP_TOKEN_BRACK2;
2936
Radek Krejcif03a9e22020-09-18 20:09:31 +02002937 } else if (!strncmp(&expr_str[parsed], "..", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002938
2939 /* '..' */
2940 tok_len = 2;
2941 tok_type = LYXP_TOKEN_DDOT;
2942
Radek Krejcif03a9e22020-09-18 20:09:31 +02002943 } else if ((expr_str[parsed] == '.') && (!isdigit(expr_str[parsed + 1]))) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002944
2945 /* '.' */
2946 tok_len = 1;
2947 tok_type = LYXP_TOKEN_DOT;
2948
Radek Krejcif03a9e22020-09-18 20:09:31 +02002949 } else if (expr_str[parsed] == '@') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002950
2951 /* '@' */
2952 tok_len = 1;
2953 tok_type = LYXP_TOKEN_AT;
2954
Radek Krejcif03a9e22020-09-18 20:09:31 +02002955 } else if (expr_str[parsed] == ',') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002956
2957 /* ',' */
2958 tok_len = 1;
2959 tok_type = LYXP_TOKEN_COMMA;
2960
Radek Krejcif03a9e22020-09-18 20:09:31 +02002961 } else if (expr_str[parsed] == '\'') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002962
2963 /* Literal with ' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002964 for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\''); ++tok_len) {}
2965 LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0',
Radek Krejci2efc45b2020-12-22 16:25:44 +01002966 LOGVAL(ctx, LY_VCODE_XP_EOE, expr_str[parsed], &expr_str[parsed]); ret = LY_EVALID,
Michal Vasko69730152020-10-09 16:30:07 +02002967 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002968 ++tok_len;
2969 tok_type = LYXP_TOKEN_LITERAL;
2970
Radek Krejcif03a9e22020-09-18 20:09:31 +02002971 } else if (expr_str[parsed] == '\"') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002972
2973 /* Literal with " */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002974 for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\"'); ++tok_len) {}
2975 LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0',
Radek Krejci2efc45b2020-12-22 16:25:44 +01002976 LOGVAL(ctx, LY_VCODE_XP_EOE, expr_str[parsed], &expr_str[parsed]); ret = LY_EVALID,
Michal Vasko69730152020-10-09 16:30:07 +02002977 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002978 ++tok_len;
2979 tok_type = LYXP_TOKEN_LITERAL;
2980
Radek Krejcif03a9e22020-09-18 20:09:31 +02002981 } else if ((expr_str[parsed] == '.') || (isdigit(expr_str[parsed]))) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002982
2983 /* Number */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002984 for (tok_len = 0; isdigit(expr_str[parsed + tok_len]); ++tok_len) {}
2985 if (expr_str[parsed + tok_len] == '.') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002986 ++tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02002987 for ( ; isdigit(expr_str[parsed + tok_len]); ++tok_len) {}
Radek Krejcib1646a92018-11-02 16:08:26 +01002988 }
2989 tok_type = LYXP_TOKEN_NUMBER;
2990
aPiecekfba75362021-10-07 12:39:48 +02002991 } else if (expr_str[parsed] == '$') {
2992
2993 /* VariableReference */
2994 parsed++;
Michal Vasko49fec8e2022-05-24 10:28:33 +02002995 ncname_len = parse_ncname(&expr_str[parsed]);
aPiecekfba75362021-10-07 12:39:48 +02002996 LY_CHECK_ERR_GOTO(ncname_len < 1, LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed - ncname_len],
2997 parsed - ncname_len + 1, expr_str); ret = LY_EVALID, error);
2998 tok_len = ncname_len;
2999 LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == ':',
3000 LOGVAL(ctx, LYVE_XPATH, "Variable with prefix is not supported."); ret = LY_EVALID,
3001 error);
3002 tok_type = LYXP_TOKEN_VARREF;
3003
Radek Krejcif03a9e22020-09-18 20:09:31 +02003004 } else if (expr_str[parsed] == '/') {
Radek Krejcib1646a92018-11-02 16:08:26 +01003005
3006 /* Operator '/', '//' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02003007 if (!strncmp(&expr_str[parsed], "//", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01003008 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003009 tok_type = LYXP_TOKEN_OPER_RPATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01003010 } else {
3011 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003012 tok_type = LYXP_TOKEN_OPER_PATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01003013 }
Radek Krejcib1646a92018-11-02 16:08:26 +01003014
Radek Krejcif03a9e22020-09-18 20:09:31 +02003015 } else if (!strncmp(&expr_str[parsed], "!=", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01003016
Michal Vasko3e48bf32020-06-01 08:39:07 +02003017 /* Operator '!=' */
Radek Krejcib1646a92018-11-02 16:08:26 +01003018 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003019 tok_type = LYXP_TOKEN_OPER_NEQUAL;
3020
Radek Krejcif03a9e22020-09-18 20:09:31 +02003021 } else if (!strncmp(&expr_str[parsed], "<=", 2) || !strncmp(&expr_str[parsed], ">=", 2)) {
Michal Vasko3e48bf32020-06-01 08:39:07 +02003022
3023 /* Operator '<=', '>=' */
3024 tok_len = 2;
3025 tok_type = LYXP_TOKEN_OPER_COMP;
Radek Krejcib1646a92018-11-02 16:08:26 +01003026
Radek Krejcif03a9e22020-09-18 20:09:31 +02003027 } else if (expr_str[parsed] == '|') {
Radek Krejcib1646a92018-11-02 16:08:26 +01003028
3029 /* Operator '|' */
3030 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003031 tok_type = LYXP_TOKEN_OPER_UNI;
Radek Krejcib1646a92018-11-02 16:08:26 +01003032
Radek Krejcif03a9e22020-09-18 20:09:31 +02003033 } else if ((expr_str[parsed] == '+') || (expr_str[parsed] == '-')) {
Radek Krejcib1646a92018-11-02 16:08:26 +01003034
3035 /* Operator '+', '-' */
3036 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003037 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01003038
Radek Krejcif03a9e22020-09-18 20:09:31 +02003039 } else if (expr_str[parsed] == '=') {
Radek Krejcib1646a92018-11-02 16:08:26 +01003040
Michal Vasko3e48bf32020-06-01 08:39:07 +02003041 /* Operator '=' */
Radek Krejcib1646a92018-11-02 16:08:26 +01003042 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003043 tok_type = LYXP_TOKEN_OPER_EQUAL;
3044
Radek Krejcif03a9e22020-09-18 20:09:31 +02003045 } else if ((expr_str[parsed] == '<') || (expr_str[parsed] == '>')) {
Michal Vasko3e48bf32020-06-01 08:39:07 +02003046
3047 /* Operator '<', '>' */
3048 tok_len = 1;
3049 tok_type = LYXP_TOKEN_OPER_COMP;
Radek Krejcib1646a92018-11-02 16:08:26 +01003050
Michal Vasko69730152020-10-09 16:30:07 +02003051 } else if (expr->used && (expr->tokens[expr->used - 1] != LYXP_TOKEN_AT) &&
3052 (expr->tokens[expr->used - 1] != LYXP_TOKEN_PAR1) &&
3053 (expr->tokens[expr->used - 1] != LYXP_TOKEN_BRACK1) &&
3054 (expr->tokens[expr->used - 1] != LYXP_TOKEN_COMMA) &&
3055 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_LOG) &&
3056 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_EQUAL) &&
3057 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_NEQUAL) &&
3058 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_COMP) &&
3059 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_MATH) &&
3060 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_UNI) &&
3061 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_PATH) &&
3062 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_RPATH)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01003063
3064 /* Operator '*', 'or', 'and', 'mod', or 'div' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02003065 if (expr_str[parsed] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01003066 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003067 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01003068
Radek Krejcif03a9e22020-09-18 20:09:31 +02003069 } else if (!strncmp(&expr_str[parsed], "or", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01003070 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003071 tok_type = LYXP_TOKEN_OPER_LOG;
Radek Krejcib1646a92018-11-02 16:08:26 +01003072
Radek Krejcif03a9e22020-09-18 20:09:31 +02003073 } else if (!strncmp(&expr_str[parsed], "and", 3)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01003074 tok_len = 3;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003075 tok_type = LYXP_TOKEN_OPER_LOG;
Radek Krejcib1646a92018-11-02 16:08:26 +01003076
Radek Krejcif03a9e22020-09-18 20:09:31 +02003077 } else if (!strncmp(&expr_str[parsed], "mod", 3) || !strncmp(&expr_str[parsed], "div", 3)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01003078 tok_len = 3;
Michal Vasko3e48bf32020-06-01 08:39:07 +02003079 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01003080
Michal Vasko49fec8e2022-05-24 10:28:33 +02003081 } else if (prev_ntype_check || prev_func_check) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003082 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 +02003083 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 +02003084 ret = LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01003085 goto error;
3086 } else {
Michal Vasko774ce402021-04-14 15:35:06 +02003087 LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed], parsed + 1, expr_str);
Radek Krejcif03a9e22020-09-18 20:09:31 +02003088 ret = LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01003089 goto error;
3090 }
Radek Krejcib1646a92018-11-02 16:08:26 +01003091 } else {
3092
Michal Vasko49fec8e2022-05-24 10:28:33 +02003093 /* (AxisName '::')? ((NCName ':')? '*' | QName) or NodeType/FunctionName */
3094 if (expr_str[parsed] == '*') {
3095 ncname_len = 1;
3096 } else {
3097 ncname_len = parse_ncname(&expr_str[parsed]);
3098 LY_CHECK_ERR_GOTO(ncname_len < 1, LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed - ncname_len],
3099 parsed - ncname_len + 1, expr_str); ret = LY_EVALID, error);
3100 }
Radek Krejcib1646a92018-11-02 16:08:26 +01003101 tok_len = ncname_len;
3102
Michal Vasko49fec8e2022-05-24 10:28:33 +02003103 has_axis = 0;
3104 if (!strncmp(&expr_str[parsed + tok_len], "::", 2)) {
3105 /* axis */
3106 LY_CHECK_ERR_GOTO(expr_parse_axis(&expr_str[parsed], ncname_len),
3107 LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed], parsed + 1, expr_str); ret = LY_EVALID, error);
3108 tok_type = LYXP_TOKEN_AXISNAME;
3109
3110 LY_CHECK_GOTO(ret = exp_add_token(ctx, expr, tok_type, parsed, tok_len), error);
3111 parsed += tok_len;
3112
3113 /* '::' */
3114 tok_len = 2;
3115 tok_type = LYXP_TOKEN_DCOLON;
3116
3117 LY_CHECK_GOTO(ret = exp_add_token(ctx, expr, tok_type, parsed, tok_len), error);
3118 parsed += tok_len;
3119
3120 if (expr_str[parsed] == '*') {
3121 ncname_len = 1;
3122 } else {
3123 ncname_len = parse_ncname(&expr_str[parsed]);
3124 LY_CHECK_ERR_GOTO(ncname_len < 1, LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed - ncname_len],
3125 parsed - ncname_len + 1, expr_str); ret = LY_EVALID, error);
3126 }
3127 tok_len = ncname_len;
3128
3129 has_axis = 1;
3130 }
3131
Radek Krejcif03a9e22020-09-18 20:09:31 +02003132 if (expr_str[parsed + tok_len] == ':') {
Radek Krejcib1646a92018-11-02 16:08:26 +01003133 ++tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02003134 if (expr_str[parsed + tok_len] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01003135 ++tok_len;
3136 } else {
Radek Krejcif03a9e22020-09-18 20:09:31 +02003137 ncname_len = parse_ncname(&expr_str[parsed + tok_len]);
Michal Vaskoe2be5462021-08-04 10:49:42 +02003138 LY_CHECK_ERR_GOTO(ncname_len < 1, LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed - ncname_len],
Michal Vasko774ce402021-04-14 15:35:06 +02003139 parsed - ncname_len + 1, expr_str); ret = LY_EVALID, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01003140 tok_len += ncname_len;
3141 }
Michal Vasko49fec8e2022-05-24 10:28:33 +02003142 /* remove old flags to prevent ambiguities */
3143 prev_ntype_check = 0;
3144 prev_func_check = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01003145 tok_type = LYXP_TOKEN_NAMETEST;
3146 } else {
Michal Vasko49fec8e2022-05-24 10:28:33 +02003147 /* if not '*', there is no prefix so it can still be NodeType/FunctionName, we can't finally decide now */
3148 prev_ntype_check = (expr_str[parsed] == '*') ? 0 : 1;
3149 prev_func_check = (prev_ntype_check && !has_axis) ? 1 : 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01003150 tok_type = LYXP_TOKEN_NAMETEST;
3151 }
3152 }
3153
3154 /* store the token, move on to the next one */
Radek Krejcif03a9e22020-09-18 20:09:31 +02003155 LY_CHECK_GOTO(ret = exp_add_token(ctx, expr, tok_type, parsed, tok_len), error);
Radek Krejcib1646a92018-11-02 16:08:26 +01003156 parsed += tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02003157 while (is_xmlws(expr_str[parsed])) {
Radek Krejcib1646a92018-11-02 16:08:26 +01003158 ++parsed;
3159 }
3160
Radek Krejcif03a9e22020-09-18 20:09:31 +02003161 } while (expr_str[parsed]);
Radek Krejcib1646a92018-11-02 16:08:26 +01003162
Michal Vasko004d3152020-06-11 19:59:22 +02003163 if (reparse) {
3164 /* prealloc repeat */
Radek Krejcif03a9e22020-09-18 20:09:31 +02003165 expr->repeat = calloc(expr->size, sizeof *expr->repeat);
3166 LY_CHECK_ERR_GOTO(!expr->repeat, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01003167
Michal Vasko004d3152020-06-11 19:59:22 +02003168 /* fill repeat */
aPiecekbf968d92021-05-27 14:35:05 +02003169 LY_CHECK_ERR_GOTO(reparse_or_expr(ctx, expr, &tok_idx, 0), ret = LY_EVALID, error);
Radek Krejcif03a9e22020-09-18 20:09:31 +02003170 if (expr->used > tok_idx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003171 LOGVAL(ctx, LYVE_XPATH, "Unparsed characters \"%s\" left at the end of an XPath expression.",
Michal Vasko69730152020-10-09 16:30:07 +02003172 &expr->expr[expr->tok_pos[tok_idx]]);
Radek Krejcif03a9e22020-09-18 20:09:31 +02003173 ret = LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02003174 goto error;
3175 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003176 }
3177
Radek Krejcif03a9e22020-09-18 20:09:31 +02003178 print_expr_struct_debug(expr);
3179 *expr_p = expr;
3180 return LY_SUCCESS;
Radek Krejcib1646a92018-11-02 16:08:26 +01003181
3182error:
Radek Krejcif03a9e22020-09-18 20:09:31 +02003183 lyxp_expr_free(ctx, expr);
3184 return ret;
Radek Krejcib1646a92018-11-02 16:08:26 +01003185}
3186
Michal Vasko1734be92020-09-22 08:55:10 +02003187LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02003188lyxp_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 +02003189 struct lyxp_expr **dup_p)
Michal Vasko004d3152020-06-11 19:59:22 +02003190{
Michal Vasko1734be92020-09-22 08:55:10 +02003191 LY_ERR ret = LY_SUCCESS;
3192 struct lyxp_expr *dup = NULL;
Michal Vaskod59039d2022-09-09 09:07:30 +02003193 uint32_t used = 0, i, j, expr_len;
Michal Vaskoe33134a2022-07-29 14:54:40 +02003194 const char *expr_start;
3195
3196 assert((!start_idx && !end_idx) || ((start_idx < exp->used) && (end_idx < exp->used) && (start_idx <= end_idx)));
Michal Vasko004d3152020-06-11 19:59:22 +02003197
Michal Vasko7f45cf22020-10-01 12:49:44 +02003198 if (!exp) {
3199 goto cleanup;
3200 }
3201
Michal Vaskoe33134a2022-07-29 14:54:40 +02003202 if (!start_idx && !end_idx) {
3203 end_idx = exp->used - 1;
3204 }
3205
3206 expr_start = exp->expr + exp->tok_pos[start_idx];
3207 expr_len = (exp->tok_pos[end_idx] + exp->tok_len[end_idx]) - exp->tok_pos[start_idx];
3208
Michal Vasko004d3152020-06-11 19:59:22 +02003209 dup = calloc(1, sizeof *dup);
Michal Vasko1734be92020-09-22 08:55:10 +02003210 LY_CHECK_ERR_GOTO(!dup, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02003211
Michal Vasko08e9b112021-06-11 15:41:17 +02003212 if (exp->used) {
Michal Vaskoe33134a2022-07-29 14:54:40 +02003213 used = (end_idx - start_idx) + 1;
3214
3215 dup->tokens = malloc(used * sizeof *dup->tokens);
Michal Vasko08e9b112021-06-11 15:41:17 +02003216 LY_CHECK_ERR_GOTO(!dup->tokens, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vaskoe33134a2022-07-29 14:54:40 +02003217 memcpy(dup->tokens, exp->tokens + start_idx, used * sizeof *dup->tokens);
Michal Vasko004d3152020-06-11 19:59:22 +02003218
Michal Vaskoe33134a2022-07-29 14:54:40 +02003219 dup->tok_pos = malloc(used * sizeof *dup->tok_pos);
Michal Vasko08e9b112021-06-11 15:41:17 +02003220 LY_CHECK_ERR_GOTO(!dup->tok_pos, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vaskoe33134a2022-07-29 14:54:40 +02003221 memcpy(dup->tok_pos, exp->tok_pos + start_idx, used * sizeof *dup->tok_pos);
Michal Vasko004d3152020-06-11 19:59:22 +02003222
Michal Vaskoe33134a2022-07-29 14:54:40 +02003223 if (start_idx) {
3224 /* fix the indices in the expression */
3225 for (i = 0; i < used; ++i) {
3226 dup->tok_pos[i] -= expr_start - exp->expr;
3227 }
3228 }
3229
3230 dup->tok_len = malloc(used * sizeof *dup->tok_len);
Michal Vasko08e9b112021-06-11 15:41:17 +02003231 LY_CHECK_ERR_GOTO(!dup->tok_len, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vaskoe33134a2022-07-29 14:54:40 +02003232 memcpy(dup->tok_len, exp->tok_len + start_idx, used * sizeof *dup->tok_len);
Michal Vasko004d3152020-06-11 19:59:22 +02003233
Michal Vasko79a7a872022-06-17 09:00:48 +02003234 if (exp->repeat) {
Michal Vaskoe33134a2022-07-29 14:54:40 +02003235 dup->repeat = malloc(used * sizeof *dup->repeat);
Michal Vasko79a7a872022-06-17 09:00:48 +02003236 LY_CHECK_ERR_GOTO(!dup->repeat, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vaskoe33134a2022-07-29 14:54:40 +02003237 for (i = start_idx; i <= end_idx; ++i) {
Michal Vasko79a7a872022-06-17 09:00:48 +02003238 if (!exp->repeat[i]) {
Michal Vaskoe33134a2022-07-29 14:54:40 +02003239 dup->repeat[i - start_idx] = NULL;
Michal Vasko79a7a872022-06-17 09:00:48 +02003240 } else {
3241 for (j = 0; exp->repeat[i][j]; ++j) {}
3242 /* the ending 0 as well */
3243 ++j;
Michal Vasko004d3152020-06-11 19:59:22 +02003244
Michal Vaskoe33134a2022-07-29 14:54:40 +02003245 dup->repeat[i - start_idx] = malloc(j * sizeof **dup->repeat);
3246 LY_CHECK_ERR_GOTO(!dup->repeat[i - start_idx], LOGMEM(ctx); ret = LY_EMEM, cleanup);
3247 memcpy(dup->repeat[i - start_idx], exp->repeat[i], j * sizeof **dup->repeat);
Michal Vasko79a7a872022-06-17 09:00:48 +02003248 }
Michal Vasko08e9b112021-06-11 15:41:17 +02003249 }
Michal Vasko004d3152020-06-11 19:59:22 +02003250 }
3251 }
3252
Michal Vaskoe33134a2022-07-29 14:54:40 +02003253 dup->used = used;
3254 dup->size = used;
3255
3256 /* copy only subexpression */
3257 LY_CHECK_GOTO(ret = lydict_insert(ctx, expr_start, expr_len, &dup->expr), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02003258
Michal Vasko1734be92020-09-22 08:55:10 +02003259cleanup:
3260 if (ret) {
3261 lyxp_expr_free(ctx, dup);
3262 } else {
3263 *dup_p = dup;
3264 }
3265 return ret;
Michal Vasko004d3152020-06-11 19:59:22 +02003266}
3267
Michal Vasko03ff5a72019-09-11 13:49:33 +02003268/**
3269 * @brief Get the last-added schema node that is currently in the context.
3270 *
3271 * @param[in] set Set to search in.
3272 * @return Last-added schema context node, NULL if no node is in context.
3273 */
3274static struct lysc_node *
3275warn_get_scnode_in_ctx(struct lyxp_set *set)
3276{
3277 uint32_t i;
3278
3279 if (!set || (set->type != LYXP_SET_SCNODE_SET)) {
3280 return NULL;
3281 }
3282
3283 i = set->used;
3284 do {
3285 --i;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003286 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003287 /* if there are more, simply return the first found (last added) */
3288 return set->val.scnodes[i].scnode;
3289 }
3290 } while (i);
3291
3292 return NULL;
3293}
3294
3295/**
3296 * @brief Test whether a type is numeric - integer type or decimal64.
3297 *
3298 * @param[in] type Type to test.
Radek Krejci857189e2020-09-01 13:26:36 +02003299 * @return Boolean value whether @p type is numeric type or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003300 */
Radek Krejci857189e2020-09-01 13:26:36 +02003301static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003302warn_is_numeric_type(struct lysc_type *type)
3303{
3304 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003305 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003306 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003307
3308 switch (type->basetype) {
3309 case LY_TYPE_DEC64:
3310 case LY_TYPE_INT8:
3311 case LY_TYPE_UINT8:
3312 case LY_TYPE_INT16:
3313 case LY_TYPE_UINT16:
3314 case LY_TYPE_INT32:
3315 case LY_TYPE_UINT32:
3316 case LY_TYPE_INT64:
3317 case LY_TYPE_UINT64:
3318 return 1;
3319 case LY_TYPE_UNION:
3320 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003321 LY_ARRAY_FOR(uni->types, u) {
3322 ret = warn_is_numeric_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003323 if (ret) {
3324 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003325 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003326 }
3327 }
3328 /* did not find any suitable type */
3329 return 0;
3330 case LY_TYPE_LEAFREF:
3331 return warn_is_numeric_type(((struct lysc_type_leafref *)type)->realtype);
3332 default:
3333 return 0;
3334 }
3335}
3336
3337/**
3338 * @brief Test whether a type is string-like - no integers, decimal64 or binary.
3339 *
3340 * @param[in] type Type to test.
Radek Krejci857189e2020-09-01 13:26:36 +02003341 * @return Boolean value whether @p type's basetype is string type or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003342 */
Radek Krejci857189e2020-09-01 13:26:36 +02003343static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003344warn_is_string_type(struct lysc_type *type)
3345{
3346 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003347 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003348 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003349
3350 switch (type->basetype) {
3351 case LY_TYPE_BITS:
3352 case LY_TYPE_ENUM:
3353 case LY_TYPE_IDENT:
3354 case LY_TYPE_INST:
3355 case LY_TYPE_STRING:
3356 return 1;
3357 case LY_TYPE_UNION:
3358 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003359 LY_ARRAY_FOR(uni->types, u) {
3360 ret = warn_is_string_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003361 if (ret) {
3362 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003363 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003364 }
3365 }
3366 /* did not find any suitable type */
3367 return 0;
3368 case LY_TYPE_LEAFREF:
3369 return warn_is_string_type(((struct lysc_type_leafref *)type)->realtype);
3370 default:
3371 return 0;
3372 }
3373}
3374
3375/**
3376 * @brief Test whether a type is one specific type.
3377 *
3378 * @param[in] type Type to test.
3379 * @param[in] base Expected type.
Radek Krejci857189e2020-09-01 13:26:36 +02003380 * @return Boolean value whether the given @p type is of the specific basetype @p base.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003381 */
Radek Krejci857189e2020-09-01 13:26:36 +02003382static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003383warn_is_specific_type(struct lysc_type *type, LY_DATA_TYPE base)
3384{
3385 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003386 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003387 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003388
3389 if (type->basetype == base) {
3390 return 1;
3391 } else if (type->basetype == LY_TYPE_UNION) {
3392 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003393 LY_ARRAY_FOR(uni->types, u) {
3394 ret = warn_is_specific_type(uni->types[u], base);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003395 if (ret) {
3396 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003397 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003398 }
3399 }
3400 /* did not find any suitable type */
3401 return 0;
3402 } else if (type->basetype == LY_TYPE_LEAFREF) {
3403 return warn_is_specific_type(((struct lysc_type_leafref *)type)->realtype, base);
3404 }
3405
3406 return 0;
3407}
3408
3409/**
3410 * @brief Get next type of a (union) type.
3411 *
3412 * @param[in] type Base type.
3413 * @param[in] prev_type Previously returned type.
3414 * @return Next type or NULL.
3415 */
3416static struct lysc_type *
3417warn_is_equal_type_next_type(struct lysc_type *type, struct lysc_type *prev_type)
3418{
3419 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003420 ly_bool found = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003421 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003422
3423 switch (type->basetype) {
3424 case LY_TYPE_UNION:
3425 uni = (struct lysc_type_union *)type;
3426 if (!prev_type) {
3427 return uni->types[0];
3428 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003429 LY_ARRAY_FOR(uni->types, u) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003430 if (found) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003431 return uni->types[u];
Michal Vasko03ff5a72019-09-11 13:49:33 +02003432 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003433 if (prev_type == uni->types[u]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003434 found = 1;
3435 }
3436 }
3437 return NULL;
3438 default:
3439 if (prev_type) {
3440 assert(type == prev_type);
3441 return NULL;
3442 } else {
3443 return type;
3444 }
3445 }
3446}
3447
3448/**
3449 * @brief Test whether 2 types have a common type.
3450 *
3451 * @param[in] type1 First type.
3452 * @param[in] type2 Second type.
3453 * @return 1 if they do, 0 otherwise.
3454 */
3455static int
3456warn_is_equal_type(struct lysc_type *type1, struct lysc_type *type2)
3457{
3458 struct lysc_type *t1, *rt1, *t2, *rt2;
3459
3460 t1 = NULL;
3461 while ((t1 = warn_is_equal_type_next_type(type1, t1))) {
3462 if (t1->basetype == LY_TYPE_LEAFREF) {
3463 rt1 = ((struct lysc_type_leafref *)t1)->realtype;
3464 } else {
3465 rt1 = t1;
3466 }
3467
3468 t2 = NULL;
3469 while ((t2 = warn_is_equal_type_next_type(type2, t2))) {
3470 if (t2->basetype == LY_TYPE_LEAFREF) {
3471 rt2 = ((struct lysc_type_leafref *)t2)->realtype;
3472 } else {
3473 rt2 = t2;
3474 }
3475
3476 if (rt2->basetype == rt1->basetype) {
3477 /* match found */
3478 return 1;
3479 }
3480 }
3481 }
3482
3483 return 0;
3484}
3485
3486/**
Michal Vaskoaa956522021-11-11 10:45:34 +01003487 * @brief Print warning with information about the XPath subexpression that caused previous warning.
3488 *
3489 * @param[in] ctx Context for logging.
3490 * @param[in] tok_pos Index of the subexpression in the whole expression.
3491 * @param[in] subexpr Subexpression start.
3492 * @param[in] subexpr_len Length of @p subexpr to print.
3493 * @param[in] cur_scnode Expression context node.
3494 */
3495static void
Michal Vaskodd528af2022-08-08 14:35:07 +02003496warn_subexpr_log(const struct ly_ctx *ctx, uint32_t tok_pos, const char *subexpr, int subexpr_len,
Michal Vaskoaa956522021-11-11 10:45:34 +01003497 const struct lysc_node *cur_scnode)
3498{
3499 char *path;
3500
3501 path = lysc_path(cur_scnode, LYSC_PATH_LOG, NULL, 0);
Michal Vaskodd528af2022-08-08 14:35:07 +02003502 LOGWRN(ctx, "Previous warning generated by XPath subexpression[%" PRIu32 "] \"%.*s\" with context node \"%s\".",
Michal Vaskoaa956522021-11-11 10:45:34 +01003503 tok_pos, subexpr_len, subexpr, path);
3504 free(path);
3505}
3506
3507/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02003508 * @brief Check both operands of comparison operators.
3509 *
3510 * @param[in] ctx Context for errors.
3511 * @param[in] set1 First operand set.
3512 * @param[in] set2 Second operand set.
3513 * @param[in] numbers_only Whether accept only numbers or other types are fine too (for '=' and '!=').
3514 * @param[in] expr Start of the expression to print with the warning.
3515 * @param[in] tok_pos Token position.
3516 */
3517static void
Michal Vaskoaa956522021-11-11 10:45:34 +01003518warn_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 +02003519 uint32_t tok_pos)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003520{
3521 struct lysc_node_leaf *node1, *node2;
Radek Krejci857189e2020-09-01 13:26:36 +02003522 ly_bool leaves = 1, warning = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003523
3524 node1 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set1);
3525 node2 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set2);
3526
3527 if (!node1 && !node2) {
3528 /* no node-sets involved, nothing to do */
3529 return;
3530 }
3531
3532 if (node1) {
3533 if (!(node1->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3534 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node1->nodetype), node1->name);
3535 warning = 1;
3536 leaves = 0;
3537 } else if (numbers_only && !warn_is_numeric_type(node1->type)) {
3538 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node1->name);
3539 warning = 1;
3540 }
3541 }
3542
3543 if (node2) {
3544 if (!(node2->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3545 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node2->nodetype), node2->name);
3546 warning = 1;
3547 leaves = 0;
3548 } else if (numbers_only && !warn_is_numeric_type(node2->type)) {
3549 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node2->name);
3550 warning = 1;
3551 }
3552 }
3553
3554 if (node1 && node2 && leaves && !numbers_only) {
Michal Vasko69730152020-10-09 16:30:07 +02003555 if ((warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type)) ||
3556 (!warn_is_numeric_type(node1->type) && warn_is_numeric_type(node2->type)) ||
3557 (!warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type) &&
3558 !warn_is_equal_type(node1->type, node2->type))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003559 LOGWRN(ctx, "Incompatible types of operands \"%s\" and \"%s\" for comparison.", node1->name, node2->name);
3560 warning = 1;
3561 }
3562 }
3563
3564 if (warning) {
Michal Vaskoaa956522021-11-11 10:45:34 +01003565 warn_subexpr_log(ctx, tok_pos, expr + tok_pos, 20, set1->cur_scnode);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003566 }
3567}
3568
3569/**
3570 * @brief Check that a value is valid for a leaf. If not applicable, does nothing.
3571 *
3572 * @param[in] exp Parsed XPath expression.
3573 * @param[in] set Set with the leaf/leaf-list.
3574 * @param[in] val_exp Index of the value (literal/number) in @p exp.
3575 * @param[in] equal_exp Index of the start of the equality expression in @p exp.
3576 * @param[in] last_equal_exp Index of the end of the equality expression in @p exp.
3577 */
3578static void
Michal Vaskodd528af2022-08-08 14:35:07 +02003579warn_equality_value(const struct lyxp_expr *exp, struct lyxp_set *set, uint32_t val_exp, uint32_t equal_exp,
3580 uint32_t last_equal_exp)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003581{
3582 struct lysc_node *scnode;
3583 struct lysc_type *type;
3584 char *value;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003585 struct lyd_value storage;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003586 LY_ERR rc;
3587 struct ly_err_item *err = NULL;
3588
Michal Vasko69730152020-10-09 16:30:07 +02003589 if ((scnode = warn_get_scnode_in_ctx(set)) && (scnode->nodetype & (LYS_LEAF | LYS_LEAFLIST)) &&
3590 ((exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) || (exp->tokens[val_exp] == LYXP_TOKEN_NUMBER))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003591 /* check that the node can have the specified value */
3592 if (exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) {
3593 value = strndup(exp->expr + exp->tok_pos[val_exp] + 1, exp->tok_len[val_exp] - 2);
3594 } else {
3595 value = strndup(exp->expr + exp->tok_pos[val_exp], exp->tok_len[val_exp]);
3596 }
3597 if (!value) {
3598 LOGMEM(set->ctx);
3599 return;
3600 }
3601
3602 if ((((struct lysc_node_leaf *)scnode)->type->basetype == LY_TYPE_IDENT) && !strchr(value, ':')) {
3603 LOGWRN(set->ctx, "Identityref \"%s\" comparison with identity \"%s\" without prefix, consider adding"
Michal Vasko69730152020-10-09 16:30:07 +02003604 " a prefix or best using \"derived-from(-or-self)()\" functions.", scnode->name, value);
Michal Vaskoaa956522021-11-11 10:45:34 +01003605 warn_subexpr_log(set->ctx, exp->tok_pos[equal_exp], exp->expr + exp->tok_pos[equal_exp],
Radek Krejci0f969882020-08-21 16:56:47 +02003606 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
Michal Vaskoaa956522021-11-11 10:45:34 +01003607 set->cur_scnode);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003608 }
3609
3610 type = ((struct lysc_node_leaf *)scnode)->type;
3611 if (type->basetype != LY_TYPE_IDENT) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003612 rc = type->plugin->store(set->ctx, type, value, strlen(value), 0, set->format, set->prefix_data,
Michal Vasko405cc9e2020-12-01 12:01:27 +01003613 LYD_HINT_DATA, scnode, &storage, NULL, &err);
Michal Vaskobf42e832020-11-23 16:59:42 +01003614 if (rc == LY_EINCOMPLETE) {
3615 rc = LY_SUCCESS;
3616 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003617
3618 if (err) {
3619 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type (%s).", value, err->msg);
3620 ly_err_free(err);
3621 } else if (rc != LY_SUCCESS) {
3622 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type.", value);
3623 }
3624 if (rc != LY_SUCCESS) {
Michal Vaskoaa956522021-11-11 10:45:34 +01003625 warn_subexpr_log(set->ctx, exp->tok_pos[equal_exp], exp->expr + exp->tok_pos[equal_exp],
Radek Krejci0f969882020-08-21 16:56:47 +02003626 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
Michal Vaskoaa956522021-11-11 10:45:34 +01003627 set->cur_scnode);
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003628 } else {
3629 type->plugin->free(set->ctx, &storage);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003630 }
3631 }
3632 free(value);
3633 }
3634}
3635
3636/*
3637 * XPath functions
3638 */
3639
3640/**
3641 * @brief Execute the YANG 1.1 bit-is-set(node-set, string) function. Returns LYXP_SET_BOOLEAN
3642 * depending on whether the first node bit value from the second argument is set.
3643 *
3644 * @param[in] args Array of arguments.
3645 * @param[in] arg_count Count of elements in @p args.
3646 * @param[in,out] set Context and result set at the same time.
3647 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003648 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003649 */
3650static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02003651xpath_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 +02003652{
3653 struct lyd_node_term *leaf;
3654 struct lysc_node_leaf *sleaf;
Michal Vasko2588b952021-07-29 07:43:26 +02003655 struct lyd_value_bits *bits;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003656 LY_ERR rc = LY_SUCCESS;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003657 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003658
3659 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003660 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003661 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003662 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3663 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3664 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3665 sleaf->name);
3666 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_BITS)) {
3667 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"bits\".", __func__, sleaf->name);
3668 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003669 }
3670
3671 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3672 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003673 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3674 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003675 } else if (!warn_is_string_type(sleaf->type)) {
3676 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003677 }
3678 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003679 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003680 return rc;
3681 }
3682
Michal Vaskod3678892020-05-21 10:06:58 +02003683 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003684 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 +02003685 return LY_EVALID;
3686 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003687 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003688 LY_CHECK_RET(rc);
3689
3690 set_fill_boolean(set, 0);
Michal Vaskod3678892020-05-21 10:06:58 +02003691 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003692 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
Michal Vasko2588b952021-07-29 07:43:26 +02003693 if ((leaf->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (leaf->value.realtype->basetype == LY_TYPE_BITS)) {
3694 LYD_VALUE_GET(&leaf->value, bits);
3695 LY_ARRAY_FOR(bits->items, u) {
3696 if (!strcmp(bits->items[u]->name, args[1]->val.str)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003697 set_fill_boolean(set, 1);
3698 break;
3699 }
3700 }
3701 }
3702 }
3703
3704 return LY_SUCCESS;
3705}
3706
3707/**
3708 * @brief Execute the XPath boolean(object) function. Returns LYXP_SET_BOOLEAN
3709 * with the argument converted to boolean.
3710 *
3711 * @param[in] args Array of arguments.
3712 * @param[in] arg_count Count of elements in @p args.
3713 * @param[in,out] set Context and result set at the same time.
3714 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003715 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003716 */
3717static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02003718xpath_boolean(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003719{
3720 LY_ERR rc;
3721
3722 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02003723 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003724 return LY_SUCCESS;
3725 }
3726
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003727 rc = lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003728 LY_CHECK_RET(rc);
3729 set_fill_set(set, args[0]);
3730
3731 return LY_SUCCESS;
3732}
3733
3734/**
3735 * @brief Execute the XPath ceiling(number) function. Returns LYXP_SET_NUMBER
3736 * with the first argument rounded up to the nearest integer.
3737 *
3738 * @param[in] args Array of arguments.
3739 * @param[in] arg_count Count of elements in @p args.
3740 * @param[in,out] set Context and result set at the same time.
3741 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003742 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003743 */
3744static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02003745xpath_ceiling(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003746{
3747 struct lysc_node_leaf *sleaf;
3748 LY_ERR rc = LY_SUCCESS;
3749
3750 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003751 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003752 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003753 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3754 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3755 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3756 sleaf->name);
3757 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
3758 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
3759 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003760 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003761 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003762 return rc;
3763 }
3764
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003765 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003766 LY_CHECK_RET(rc);
3767 if ((long long)args[0]->val.num != args[0]->val.num) {
3768 set_fill_number(set, ((long long)args[0]->val.num) + 1);
3769 } else {
3770 set_fill_number(set, args[0]->val.num);
3771 }
3772
3773 return LY_SUCCESS;
3774}
3775
3776/**
3777 * @brief Execute the XPath concat(string, string, string*) function.
3778 * Returns LYXP_SET_STRING with the concatenation of all the arguments.
3779 *
3780 * @param[in] args Array of arguments.
3781 * @param[in] arg_count Count of elements in @p args.
3782 * @param[in,out] set Context and result set at the same time.
3783 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003784 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003785 */
3786static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02003787xpath_concat(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003788{
Michal Vaskodd528af2022-08-08 14:35:07 +02003789 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003790 char *str = NULL;
3791 size_t used = 1;
3792 LY_ERR rc = LY_SUCCESS;
3793 struct lysc_node_leaf *sleaf;
3794
3795 if (options & LYXP_SCNODE_ALL) {
3796 for (i = 0; i < arg_count; ++i) {
3797 if ((args[i]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[i]))) {
3798 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3799 LOGWRN(set->ctx, "Argument #%u of %s is a %s node \"%s\".",
Michal Vasko69730152020-10-09 16:30:07 +02003800 i + 1, __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003801 } else if (!warn_is_string_type(sleaf->type)) {
Radek Krejci70124c82020-08-14 22:17:03 +02003802 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 +02003803 }
3804 }
3805 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003806 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003807 return rc;
3808 }
3809
3810 for (i = 0; i < arg_count; ++i) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003811 rc = lyxp_set_cast(args[i], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003812 if (rc != LY_SUCCESS) {
3813 free(str);
3814 return rc;
3815 }
3816
3817 str = ly_realloc(str, (used + strlen(args[i]->val.str)) * sizeof(char));
3818 LY_CHECK_ERR_RET(!str, LOGMEM(set->ctx), LY_EMEM);
3819 strcpy(str + used - 1, args[i]->val.str);
3820 used += strlen(args[i]->val.str);
3821 }
3822
3823 /* free, kind of */
Michal Vaskod3678892020-05-21 10:06:58 +02003824 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003825 set->type = LYXP_SET_STRING;
3826 set->val.str = str;
3827
3828 return LY_SUCCESS;
3829}
3830
3831/**
3832 * @brief Execute the XPath contains(string, string) function.
3833 * Returns LYXP_SET_BOOLEAN whether the second argument can
3834 * be found in the first or not.
3835 *
3836 * @param[in] args Array of arguments.
3837 * @param[in] arg_count Count of elements in @p args.
3838 * @param[in,out] set Context and result set at the same time.
3839 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003840 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003841 */
3842static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02003843xpath_contains(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003844{
3845 struct lysc_node_leaf *sleaf;
3846 LY_ERR rc = LY_SUCCESS;
3847
3848 if (options & LYXP_SCNODE_ALL) {
3849 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3850 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003851 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3852 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003853 } else if (!warn_is_string_type(sleaf->type)) {
3854 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003855 }
3856 }
3857
3858 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3859 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003860 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3861 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003862 } else if (!warn_is_string_type(sleaf->type)) {
3863 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003864 }
3865 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003866 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003867 return rc;
3868 }
3869
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003870 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003871 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003872 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003873 LY_CHECK_RET(rc);
3874
3875 if (strstr(args[0]->val.str, args[1]->val.str)) {
3876 set_fill_boolean(set, 1);
3877 } else {
3878 set_fill_boolean(set, 0);
3879 }
3880
3881 return LY_SUCCESS;
3882}
3883
3884/**
3885 * @brief Execute the XPath count(node-set) function. Returns LYXP_SET_NUMBER
3886 * with the size of the node-set from the argument.
3887 *
3888 * @param[in] args Array of arguments.
3889 * @param[in] arg_count Count of elements in @p args.
3890 * @param[in,out] set Context and result set at the same time.
3891 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003892 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003893 */
3894static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02003895xpath_count(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003896{
Michal Vasko03ff5a72019-09-11 13:49:33 +02003897 LY_ERR rc = LY_SUCCESS;
3898
3899 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003900 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003901 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003902 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003903 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003904 return rc;
3905 }
3906
Michal Vasko03ff5a72019-09-11 13:49:33 +02003907 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003908 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "count(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003909 return LY_EVALID;
3910 }
3911
3912 set_fill_number(set, args[0]->used);
3913 return LY_SUCCESS;
3914}
3915
3916/**
3917 * @brief Execute the XPath current() function. Returns LYXP_SET_NODE_SET
3918 * with the context with the intial node.
3919 *
3920 * @param[in] args Array of arguments.
3921 * @param[in] arg_count Count of elements in @p args.
3922 * @param[in,out] set Context and result set at the same time.
3923 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003924 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003925 */
3926static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02003927xpath_current(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003928{
3929 if (arg_count || args) {
Radek Krejcie87c7dc2021-06-02 21:25:42 +02003930 LOGVAL(set->ctx, LY_VCODE_XP_INARGCOUNT, arg_count, LY_PRI_LENSTR("current()"));
Michal Vasko03ff5a72019-09-11 13:49:33 +02003931 return LY_EVALID;
3932 }
3933
3934 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02003935 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003936
Michal Vasko296dfaf2021-12-13 16:57:42 +01003937 if (set->cur_scnode) {
Michal Vasko7333cb32022-07-29 16:30:29 +02003938 LY_CHECK_RET(set_scnode_insert_node(set, set->cur_scnode, LYXP_NODE_ELEM, LYXP_AXIS_SELF, NULL));
Michal Vasko296dfaf2021-12-13 16:57:42 +01003939 } else {
3940 /* root node */
Michal Vasko7333cb32022-07-29 16:30:29 +02003941 LY_CHECK_RET(set_scnode_insert_node(set, NULL, set->root_type, LYXP_AXIS_SELF, NULL));
Michal Vasko296dfaf2021-12-13 16:57:42 +01003942 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003943 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02003944 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003945
Michal Vasko296dfaf2021-12-13 16:57:42 +01003946 if (set->cur_node) {
3947 /* position is filled later */
3948 set_insert_node(set, set->cur_node, 0, LYXP_NODE_ELEM, 0);
3949 } else {
3950 /* root node */
3951 set_insert_node(set, NULL, 0, set->root_type, 0);
3952 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003953 }
3954
3955 return LY_SUCCESS;
3956}
3957
3958/**
3959 * @brief Execute the YANG 1.1 deref(node-set) function. Returns LYXP_SET_NODE_SET with either
3960 * leafref or instance-identifier target node(s).
3961 *
3962 * @param[in] args Array of arguments.
3963 * @param[in] arg_count Count of elements in @p args.
3964 * @param[in,out] set Context and result set at the same time.
3965 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003966 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003967 */
3968static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02003969xpath_deref(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003970{
3971 struct lyd_node_term *leaf;
Michal Vasko42e497c2020-01-06 08:38:25 +01003972 struct lysc_node_leaf *sleaf = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02003973 struct lysc_type_leafref *lref;
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003974 const struct lysc_node *target;
Michal Vasko004d3152020-06-11 19:59:22 +02003975 struct ly_path *p;
3976 struct lyd_node *node;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003977 char *errmsg = NULL;
Michal Vasko00cbf532020-06-15 13:58:47 +02003978 uint8_t oper;
Michal Vasko741bb562021-06-24 11:59:50 +02003979 LY_ERR r;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003980
3981 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003982 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003983 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003984 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
Michal Vasko423ba3f2021-07-19 13:08:50 +02003985 if (!(sleaf->nodetype & LYD_NODE_TERM)) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003986 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3987 sleaf->name);
Michal Vaskoed725d72021-06-23 12:03:45 +02003988 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_LEAFREF) &&
3989 !warn_is_specific_type(sleaf->type, LY_TYPE_INST)) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003990 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"leafref\" nor \"instance-identifier\".",
3991 __func__, sleaf->name);
3992 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003993 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003994 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko423ba3f2021-07-19 13:08:50 +02003995 if (sleaf && (sleaf->nodetype & LYD_NODE_TERM) && (sleaf->type->basetype == LY_TYPE_LEAFREF)) {
Michal Vasko004d3152020-06-11 19:59:22 +02003996 lref = (struct lysc_type_leafref *)sleaf->type;
Michal Vaskod1e53b92021-01-28 13:11:06 +01003997 oper = (sleaf->flags & LYS_IS_OUTPUT) ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT;
Michal Vasko004d3152020-06-11 19:59:22 +02003998
3999 /* it was already evaluated on schema, it must succeed */
Michal Vasko741bb562021-06-24 11:59:50 +02004000 r = ly_path_compile_leafref(set->ctx, &sleaf->node, NULL, lref->path, oper, LY_PATH_TARGET_MANY,
Michal Vasko24fc4d12021-07-12 14:41:20 +02004001 LY_VALUE_SCHEMA_RESOLVED, lref->prefixes, &p);
Michal Vasko741bb562021-06-24 11:59:50 +02004002 if (!r) {
4003 /* get the target node */
4004 target = p[LY_ARRAY_COUNT(p) - 1].node;
4005 ly_path_free(set->ctx, p);
Michal Vasko004d3152020-06-11 19:59:22 +02004006
Michal Vasko7333cb32022-07-29 16:30:29 +02004007 LY_CHECK_RET(set_scnode_insert_node(set, target, LYXP_NODE_ELEM, LYXP_AXIS_SELF, NULL));
Michal Vasko741bb562021-06-24 11:59:50 +02004008 } /* else the target was found before but is disabled so it was removed */
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02004009 }
4010
Michal Vasko741bb562021-06-24 11:59:50 +02004011 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004012 }
4013
Michal Vaskod3678892020-05-21 10:06:58 +02004014 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004015 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "deref(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004016 return LY_EVALID;
4017 }
4018
Michal Vaskod3678892020-05-21 10:06:58 +02004019 lyxp_set_free_content(set);
4020 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004021 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
4022 sleaf = (struct lysc_node_leaf *)leaf->schema;
4023 if (sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
4024 if (sleaf->type->basetype == LY_TYPE_LEAFREF) {
4025 /* find leafref target */
Radek Krejci0b013302021-03-29 15:22:32 +02004026 if (lyplg_type_resolve_leafref((struct lysc_type_leafref *)sleaf->type, &leaf->node, &leaf->value, set->tree,
Michal Vasko9e685082021-01-29 14:49:09 +01004027 &node, &errmsg)) {
Michal Vasko004d3152020-06-11 19:59:22 +02004028 LOGERR(set->ctx, LY_EVALID, errmsg);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004029 free(errmsg);
Michal Vasko004d3152020-06-11 19:59:22 +02004030 return LY_EVALID;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004031 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004032 } else {
4033 assert(sleaf->type->basetype == LY_TYPE_INST);
Michal Vasko004d3152020-06-11 19:59:22 +02004034 if (ly_path_eval(leaf->value.target, set->tree, &node)) {
Michal Vaskoba99a3e2020-08-18 15:50:05 +02004035 LOGERR(set->ctx, LY_EVALID, "Invalid instance-identifier \"%s\" value - required instance not found.",
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02004036 lyd_get_value(&leaf->node));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004037 return LY_EVALID;
4038 }
4039 }
Michal Vasko004d3152020-06-11 19:59:22 +02004040
4041 /* insert it */
4042 set_insert_node(set, node, 0, LYXP_NODE_ELEM, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004043 }
4044 }
4045
4046 return LY_SUCCESS;
4047}
4048
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004049static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02004050xpath_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 +02004051{
Michal Vaskofe1af042022-07-29 14:58:59 +02004052 uint32_t i, id_len;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004053 LY_ARRAY_COUNT_TYPE u;
4054 struct lyd_node_term *leaf;
4055 struct lysc_node_leaf *sleaf;
4056 struct lyd_meta *meta;
Michal Vasko93923692021-05-07 15:28:02 +02004057 struct lyd_value *val;
4058 const struct lys_module *mod;
4059 const char *id_name;
Michal Vasko93923692021-05-07 15:28:02 +02004060 struct lysc_ident *id;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004061 LY_ERR rc = LY_SUCCESS;
Radek Krejci857189e2020-09-01 13:26:36 +02004062 ly_bool found;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004063
4064 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02004065 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004066 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", func);
Michal Vasko5676f4e2021-04-06 17:14:45 +02004067 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4068 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4069 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype),
4070 sleaf->name);
4071 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_IDENT)) {
4072 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"identityref\".", func, sleaf->name);
4073 }
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004074 }
4075
4076 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4077 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4078 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype),
Michal Vasko69730152020-10-09 16:30:07 +02004079 sleaf->name);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004080 } else if (!warn_is_string_type(sleaf->type)) {
4081 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", func, sleaf->name);
4082 }
4083 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004084 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004085 return rc;
4086 }
4087
4088 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004089 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 +02004090 return LY_EVALID;
4091 }
4092 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
4093 LY_CHECK_RET(rc);
4094
Michal Vasko93923692021-05-07 15:28:02 +02004095 /* parse the identity */
4096 id_name = args[1]->val.str;
4097 id_len = strlen(id_name);
4098 rc = moveto_resolve_model(&id_name, &id_len, set, set->cur_node ? set->cur_node->schema : NULL, &mod);
4099 LY_CHECK_RET(rc);
4100 if (!mod) {
4101 LOGVAL(set->ctx, LYVE_XPATH, "Identity \"%.*s\" without a prefix.", (int)id_len, id_name);
4102 return LY_EVALID;
4103 }
4104
4105 /* find the identity */
4106 found = 0;
4107 LY_ARRAY_FOR(mod->identities, u) {
4108 if (!ly_strncmp(mod->identities[u].name, id_name, id_len)) {
4109 /* we have match */
4110 found = 1;
4111 break;
4112 }
4113 }
4114 if (!found) {
4115 LOGVAL(set->ctx, LYVE_XPATH, "Identity \"%.*s\" not found in module \"%s\".", (int)id_len, id_name, mod->name);
4116 return LY_EVALID;
4117 }
4118 id = &mod->identities[u];
4119
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004120 set_fill_boolean(set, 0);
4121 found = 0;
4122 for (i = 0; i < args[0]->used; ++i) {
4123 if ((args[0]->val.nodes[i].type != LYXP_NODE_ELEM) && (args[0]->val.nodes[i].type != LYXP_NODE_META)) {
4124 continue;
4125 }
4126
4127 if (args[0]->val.nodes[i].type == LYXP_NODE_ELEM) {
4128 leaf = (struct lyd_node_term *)args[0]->val.nodes[i].node;
4129 sleaf = (struct lysc_node_leaf *)leaf->schema;
4130 val = &leaf->value;
4131 if (!(sleaf->nodetype & LYD_NODE_TERM) || (leaf->value.realtype->basetype != LY_TYPE_IDENT)) {
4132 /* uninteresting */
4133 continue;
4134 }
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004135 } else {
4136 meta = args[0]->val.meta[i].meta;
4137 val = &meta->value;
4138 if (val->realtype->basetype != LY_TYPE_IDENT) {
4139 /* uninteresting */
4140 continue;
4141 }
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004142 }
4143
Michal Vasko93923692021-05-07 15:28:02 +02004144 /* check the identity itself */
4145 if (self_match && (id == val->ident)) {
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004146 set_fill_boolean(set, 1);
4147 found = 1;
4148 }
Michal Vasko93923692021-05-07 15:28:02 +02004149 if (!found && !lyplg_type_identity_isderived(id, val->ident)) {
4150 set_fill_boolean(set, 1);
4151 found = 1;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004152 }
4153
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004154 if (found) {
4155 break;
4156 }
4157 }
4158
4159 return LY_SUCCESS;
4160}
4161
Michal Vasko03ff5a72019-09-11 13:49:33 +02004162/**
4163 * @brief Execute the YANG 1.1 derived-from(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
4164 * on whether the first argument nodes contain a node of an identity derived from the second
4165 * argument identity.
4166 *
4167 * @param[in] args Array of arguments.
4168 * @param[in] arg_count Count of elements in @p args.
4169 * @param[in,out] set Context and result set at the same time.
4170 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004171 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004172 */
4173static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004174xpath_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 +02004175{
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004176 return xpath_derived_(args, set, options, 0, __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004177}
4178
4179/**
4180 * @brief Execute the YANG 1.1 derived-from-or-self(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
4181 * on whether the first argument nodes contain a node of an identity that either is or is derived from
4182 * the second argument identity.
4183 *
4184 * @param[in] args Array of arguments.
4185 * @param[in] arg_count Count of elements in @p args.
4186 * @param[in,out] set Context and result set at the same time.
4187 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004188 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004189 */
4190static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004191xpath_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 +02004192{
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02004193 return xpath_derived_(args, set, options, 1, __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004194}
4195
4196/**
4197 * @brief Execute the YANG 1.1 enum-value(node-set) function. Returns LYXP_SET_NUMBER
4198 * with the integer value of the first node's enum value, otherwise NaN.
4199 *
4200 * @param[in] args Array of arguments.
4201 * @param[in] arg_count Count of elements in @p args.
4202 * @param[in,out] set Context and result set at the same time.
4203 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004204 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004205 */
4206static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004207xpath_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 +02004208{
4209 struct lyd_node_term *leaf;
4210 struct lysc_node_leaf *sleaf;
4211 LY_ERR rc = LY_SUCCESS;
4212
4213 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02004214 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004215 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02004216 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4217 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4218 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4219 sleaf->name);
4220 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_ENUM)) {
4221 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"enumeration\".", __func__, sleaf->name);
4222 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004223 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004224 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004225 return rc;
4226 }
4227
Michal Vaskod3678892020-05-21 10:06:58 +02004228 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004229 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "enum-value(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004230 return LY_EVALID;
4231 }
4232
4233 set_fill_number(set, NAN);
Michal Vaskod3678892020-05-21 10:06:58 +02004234 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004235 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
4236 sleaf = (struct lysc_node_leaf *)leaf->schema;
4237 if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_ENUM)) {
4238 set_fill_number(set, leaf->value.enum_item->value);
4239 }
4240 }
4241
4242 return LY_SUCCESS;
4243}
4244
4245/**
4246 * @brief Execute the XPath false() function. Returns LYXP_SET_BOOLEAN
4247 * with false value.
4248 *
4249 * @param[in] args Array of arguments.
4250 * @param[in] arg_count Count of elements in @p args.
4251 * @param[in,out] set Context and result set at the same time.
4252 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004253 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004254 */
4255static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004256xpath_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 +02004257{
4258 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004259 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004260 return LY_SUCCESS;
4261 }
4262
4263 set_fill_boolean(set, 0);
4264 return LY_SUCCESS;
4265}
4266
4267/**
4268 * @brief Execute the XPath floor(number) function. Returns LYXP_SET_NUMBER
4269 * with the first argument floored (truncated).
4270 *
4271 * @param[in] args Array of arguments.
4272 * @param[in] arg_count Count of elements in @p args.
4273 * @param[in,out] set Context and result set at the same time.
4274 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004275 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004276 */
4277static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004278xpath_floor(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t UNUSED(options))
Michal Vasko03ff5a72019-09-11 13:49:33 +02004279{
4280 LY_ERR rc;
4281
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004282 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004283 LY_CHECK_RET(rc);
4284 if (isfinite(args[0]->val.num)) {
4285 set_fill_number(set, (long long)args[0]->val.num);
4286 }
4287
4288 return LY_SUCCESS;
4289}
4290
4291/**
4292 * @brief Execute the XPath lang(string) function. Returns LYXP_SET_BOOLEAN
4293 * whether the language of the text matches the one from the argument.
4294 *
4295 * @param[in] args Array of arguments.
4296 * @param[in] arg_count Count of elements in @p args.
4297 * @param[in,out] set Context and result set at the same time.
4298 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004299 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004300 */
4301static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004302xpath_lang(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004303{
4304 const struct lyd_node *node;
4305 struct lysc_node_leaf *sleaf;
Michal Vasko9f96a052020-03-10 09:41:45 +01004306 struct lyd_meta *meta = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004307 const char *val;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004308 LY_ERR rc = LY_SUCCESS;
4309
4310 if (options & LYXP_SCNODE_ALL) {
4311 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4312 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004313 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4314 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004315 } else if (!warn_is_string_type(sleaf->type)) {
4316 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004317 }
4318 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004319 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004320 return rc;
4321 }
4322
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004323 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004324 LY_CHECK_RET(rc);
4325
Michal Vasko03ff5a72019-09-11 13:49:33 +02004326 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004327 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "lang(string)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004328 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004329 } else if (!set->used) {
4330 set_fill_boolean(set, 0);
4331 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004332 }
4333
4334 switch (set->val.nodes[0].type) {
4335 case LYXP_NODE_ELEM:
4336 case LYXP_NODE_TEXT:
4337 node = set->val.nodes[0].node;
4338 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004339 case LYXP_NODE_META:
4340 node = set->val.meta[0].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004341 break;
4342 default:
4343 /* nothing to do with roots */
4344 set_fill_boolean(set, 0);
4345 return LY_SUCCESS;
4346 }
4347
Michal Vasko9f96a052020-03-10 09:41:45 +01004348 /* find lang metadata */
Michal Vasko9e685082021-01-29 14:49:09 +01004349 for ( ; node; node = lyd_parent(node)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01004350 for (meta = node->meta; meta; meta = meta->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004351 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004352 if (meta->name && !strcmp(meta->name, "lang") && !strcmp(meta->annotation->module->name, "xml")) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004353 break;
4354 }
4355 }
4356
Michal Vasko9f96a052020-03-10 09:41:45 +01004357 if (meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004358 break;
4359 }
4360 }
4361
4362 /* compare languages */
Michal Vasko9f96a052020-03-10 09:41:45 +01004363 if (!meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004364 set_fill_boolean(set, 0);
4365 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004366 uint64_t i;
4367
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02004368 val = lyd_get_meta_value(meta);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004369 for (i = 0; args[0]->val.str[i]; ++i) {
4370 if (tolower(args[0]->val.str[i]) != tolower(val[i])) {
4371 set_fill_boolean(set, 0);
4372 break;
4373 }
4374 }
4375 if (!args[0]->val.str[i]) {
4376 if (!val[i] || (val[i] == '-')) {
4377 set_fill_boolean(set, 1);
4378 } else {
4379 set_fill_boolean(set, 0);
4380 }
4381 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004382 }
4383
4384 return LY_SUCCESS;
4385}
4386
4387/**
4388 * @brief Execute the XPath last() function. Returns LYXP_SET_NUMBER
4389 * with the context size.
4390 *
4391 * @param[in] args Array of arguments.
4392 * @param[in] arg_count Count of elements in @p args.
4393 * @param[in,out] set Context and result set at the same time.
4394 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004395 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004396 */
4397static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004398xpath_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 +02004399{
4400 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004401 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004402 return LY_SUCCESS;
4403 }
4404
Michal Vasko03ff5a72019-09-11 13:49:33 +02004405 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004406 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "last()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004407 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004408 } else if (!set->used) {
4409 set_fill_number(set, 0);
4410 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004411 }
4412
4413 set_fill_number(set, set->ctx_size);
4414 return LY_SUCCESS;
4415}
4416
4417/**
4418 * @brief Execute the XPath local-name(node-set?) function. Returns LYXP_SET_STRING
4419 * with the node name without namespace from the argument or the context.
4420 *
4421 * @param[in] args Array of arguments.
4422 * @param[in] arg_count Count of elements in @p args.
4423 * @param[in,out] set Context and result set at the same time.
4424 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004425 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004426 */
4427static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004428xpath_local_name(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004429{
4430 struct lyxp_set_node *item;
Michal Vasko69730152020-10-09 16:30:07 +02004431
Michal Vasko03ff5a72019-09-11 13:49:33 +02004432 /* suppress unused variable warning */
4433 (void)options;
4434
4435 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004436 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004437 return LY_SUCCESS;
4438 }
4439
4440 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004441 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004442 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004443 "local-name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004444 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004445 } else if (!args[0]->used) {
4446 set_fill_string(set, "", 0);
4447 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004448 }
4449
4450 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004451 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004452
4453 item = &args[0]->val.nodes[0];
4454 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004455 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004456 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "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 (!set->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(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004465
4466 item = &set->val.nodes[0];
4467 }
4468
4469 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004470 case LYXP_NODE_NONE:
4471 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004472 case LYXP_NODE_ROOT:
4473 case LYXP_NODE_ROOT_CONFIG:
4474 case LYXP_NODE_TEXT:
4475 set_fill_string(set, "", 0);
4476 break;
4477 case LYXP_NODE_ELEM:
4478 set_fill_string(set, item->node->schema->name, strlen(item->node->schema->name));
4479 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004480 case LYXP_NODE_META:
4481 set_fill_string(set, ((struct lyd_meta *)item->node)->name, strlen(((struct lyd_meta *)item->node)->name));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004482 break;
4483 }
4484
4485 return LY_SUCCESS;
4486}
4487
4488/**
4489 * @brief Execute the XPath name(node-set?) function. Returns LYXP_SET_STRING
4490 * with the node name fully qualified (with namespace) from the argument or the context.
4491 *
4492 * @param[in] args Array of arguments.
4493 * @param[in] arg_count Count of elements in @p args.
4494 * @param[in,out] set Context and result set at the same time.
4495 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004496 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004497 */
4498static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004499xpath_name(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004500{
4501 struct lyxp_set_node *item;
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004502 struct lys_module *mod = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004503 char *str;
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004504 const char *name = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004505
4506 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004507 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004508 return LY_SUCCESS;
4509 }
4510
4511 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004512 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004513 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004514 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004515 } else if (!args[0]->used) {
4516 set_fill_string(set, "", 0);
4517 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004518 }
4519
4520 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004521 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004522
4523 item = &args[0]->val.nodes[0];
4524 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004525 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004526 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004527 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004528 } else if (!set->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(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004535
4536 item = &set->val.nodes[0];
4537 }
4538
4539 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004540 case LYXP_NODE_NONE:
4541 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004542 case LYXP_NODE_ROOT:
4543 case LYXP_NODE_ROOT_CONFIG:
4544 case LYXP_NODE_TEXT:
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004545 /* keep NULL */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004546 break;
4547 case LYXP_NODE_ELEM:
4548 mod = item->node->schema->module;
4549 name = item->node->schema->name;
4550 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004551 case LYXP_NODE_META:
4552 mod = ((struct lyd_meta *)item->node)->annotation->module;
4553 name = ((struct lyd_meta *)item->node)->name;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004554 break;
4555 }
4556
4557 if (mod && name) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004558 int rc = asprintf(&str, "%s:%s", ly_get_prefix(mod, set->format, set->prefix_data), name);
Michal Vasko26bbb272022-08-02 14:54:33 +02004559
Michal Vasko03ff5a72019-09-11 13:49:33 +02004560 LY_CHECK_ERR_RET(rc == -1, LOGMEM(set->ctx), LY_EMEM);
4561 set_fill_string(set, str, strlen(str));
4562 free(str);
4563 } else {
4564 set_fill_string(set, "", 0);
4565 }
4566
4567 return LY_SUCCESS;
4568}
4569
4570/**
4571 * @brief Execute the XPath namespace-uri(node-set?) function. Returns LYXP_SET_STRING
4572 * with the namespace of the node from the argument or the context.
4573 *
4574 * @param[in] args Array of arguments.
4575 * @param[in] arg_count Count of elements in @p args.
4576 * @param[in,out] set Context and result set at the same time.
4577 * @param[in] options XPath options.
4578 * @return LY_ERR (LY_EINVAL for wrong arguments on schema)
4579 */
4580static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004581xpath_namespace_uri(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004582{
4583 struct lyxp_set_node *item;
4584 struct lys_module *mod;
Michal Vasko69730152020-10-09 16:30:07 +02004585
Michal Vasko03ff5a72019-09-11 13:49:33 +02004586 /* suppress unused variable warning */
4587 (void)options;
4588
4589 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004590 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004591 return LY_SUCCESS;
4592 }
4593
4594 if (arg_count) {
Michal Vaskod3678892020-05-21 10:06:58 +02004595 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004596 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
Michal Vasko69730152020-10-09 16:30:07 +02004597 "namespace-uri(node-set?)");
Michal Vaskod3678892020-05-21 10:06:58 +02004598 return LY_EVALID;
4599 } else if (!args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004600 set_fill_string(set, "", 0);
4601 return LY_SUCCESS;
4602 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004603
4604 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004605 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004606
4607 item = &args[0]->val.nodes[0];
4608 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004609 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004610 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "namespace-uri(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004611 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004612 } else if (!set->used) {
4613 set_fill_string(set, "", 0);
4614 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004615 }
4616
4617 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004618 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004619
4620 item = &set->val.nodes[0];
4621 }
4622
4623 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004624 case LYXP_NODE_NONE:
4625 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004626 case LYXP_NODE_ROOT:
4627 case LYXP_NODE_ROOT_CONFIG:
4628 case LYXP_NODE_TEXT:
4629 set_fill_string(set, "", 0);
4630 break;
4631 case LYXP_NODE_ELEM:
Michal Vasko9f96a052020-03-10 09:41:45 +01004632 case LYXP_NODE_META:
Michal Vasko03ff5a72019-09-11 13:49:33 +02004633 if (item->type == LYXP_NODE_ELEM) {
4634 mod = item->node->schema->module;
Michal Vasko9f96a052020-03-10 09:41:45 +01004635 } else { /* LYXP_NODE_META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004636 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004637 mod = ((struct lyd_meta *)item->node)->annotation->module;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004638 }
4639
4640 set_fill_string(set, mod->ns, strlen(mod->ns));
4641 break;
4642 }
4643
4644 return LY_SUCCESS;
4645}
4646
4647/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02004648 * @brief Execute the XPath normalize-space(string?) function. Returns LYXP_SET_STRING
4649 * with normalized value (no leading, trailing, double white spaces) of the node
4650 * from the argument or the context.
4651 *
4652 * @param[in] args Array of arguments.
4653 * @param[in] arg_count Count of elements in @p args.
4654 * @param[in,out] set Context and result set at the same time.
4655 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004656 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004657 */
4658static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004659xpath_normalize_space(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004660{
Michal Vaskodd528af2022-08-08 14:35:07 +02004661 uint32_t i, new_used;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004662 char *new;
Radek Krejci857189e2020-09-01 13:26:36 +02004663 ly_bool have_spaces = 0, space_before = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004664 struct lysc_node_leaf *sleaf;
4665 LY_ERR rc = LY_SUCCESS;
4666
4667 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004668 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) &&
4669 (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004670 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004671 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4672 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004673 } else if (!warn_is_string_type(sleaf->type)) {
4674 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004675 }
4676 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004677 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004678 return rc;
4679 }
4680
4681 if (arg_count) {
4682 set_fill_set(set, args[0]);
4683 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004684 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004685 LY_CHECK_RET(rc);
4686
4687 /* is there any normalization necessary? */
4688 for (i = 0; set->val.str[i]; ++i) {
4689 if (is_xmlws(set->val.str[i])) {
4690 if ((i == 0) || space_before || (!set->val.str[i + 1])) {
4691 have_spaces = 1;
4692 break;
4693 }
4694 space_before = 1;
4695 } else {
4696 space_before = 0;
4697 }
4698 }
4699
4700 /* yep, there is */
4701 if (have_spaces) {
4702 /* it's enough, at least one character will go, makes space for ending '\0' */
4703 new = malloc(strlen(set->val.str) * sizeof(char));
4704 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4705 new_used = 0;
4706
4707 space_before = 0;
4708 for (i = 0; set->val.str[i]; ++i) {
4709 if (is_xmlws(set->val.str[i])) {
4710 if ((i == 0) || space_before) {
4711 space_before = 1;
4712 continue;
4713 } else {
4714 space_before = 1;
4715 }
4716 } else {
4717 space_before = 0;
4718 }
4719
4720 new[new_used] = (space_before ? ' ' : set->val.str[i]);
4721 ++new_used;
4722 }
4723
4724 /* at worst there is one trailing space now */
4725 if (new_used && is_xmlws(new[new_used - 1])) {
4726 --new_used;
4727 }
4728
4729 new = ly_realloc(new, (new_used + 1) * sizeof(char));
4730 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4731 new[new_used] = '\0';
4732
4733 free(set->val.str);
4734 set->val.str = new;
4735 }
4736
4737 return LY_SUCCESS;
4738}
4739
4740/**
4741 * @brief Execute the XPath not(boolean) function. Returns LYXP_SET_BOOLEAN
4742 * with the argument converted to boolean and logically inverted.
4743 *
4744 * @param[in] args Array of arguments.
4745 * @param[in] arg_count Count of elements in @p args.
4746 * @param[in,out] set Context and result set at the same time.
4747 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004748 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004749 */
4750static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004751xpath_not(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004752{
4753 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004754 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004755 return LY_SUCCESS;
4756 }
4757
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004758 lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02004759 if (args[0]->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004760 set_fill_boolean(set, 0);
4761 } else {
4762 set_fill_boolean(set, 1);
4763 }
4764
4765 return LY_SUCCESS;
4766}
4767
4768/**
4769 * @brief Execute the XPath number(object?) function. Returns LYXP_SET_NUMBER
4770 * with the number representation of either the argument or the context.
4771 *
4772 * @param[in] args Array of arguments.
4773 * @param[in] arg_count Count of elements in @p args.
4774 * @param[in,out] set Context and result set at the same time.
4775 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004776 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004777 */
4778static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004779xpath_number(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004780{
4781 LY_ERR rc;
4782
4783 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004784 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004785 return LY_SUCCESS;
4786 }
4787
4788 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004789 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004790 LY_CHECK_RET(rc);
4791 set_fill_set(set, args[0]);
4792 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004793 rc = lyxp_set_cast(set, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004794 LY_CHECK_RET(rc);
4795 }
4796
4797 return LY_SUCCESS;
4798}
4799
4800/**
4801 * @brief Execute the XPath position() function. Returns LYXP_SET_NUMBER
4802 * with the context position.
4803 *
4804 * @param[in] args Array of arguments.
4805 * @param[in] arg_count Count of elements in @p args.
4806 * @param[in,out] set Context and result set at the same time.
4807 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004808 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004809 */
4810static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004811xpath_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 +02004812{
4813 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004814 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004815 return LY_SUCCESS;
4816 }
4817
Michal Vasko03ff5a72019-09-11 13:49:33 +02004818 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004819 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "position()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004820 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004821 } else if (!set->used) {
4822 set_fill_number(set, 0);
4823 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004824 }
4825
4826 set_fill_number(set, set->ctx_pos);
4827
4828 /* UNUSED in 'Release' build type */
4829 (void)options;
4830 return LY_SUCCESS;
4831}
4832
4833/**
4834 * @brief Execute the YANG 1.1 re-match(string, string) function. Returns LYXP_SET_BOOLEAN
4835 * depending on whether the second argument regex matches the first argument string. For details refer to
4836 * YANG 1.1 RFC section 10.2.1.
4837 *
4838 * @param[in] args Array of arguments.
4839 * @param[in] arg_count Count of elements in @p args.
4840 * @param[in,out] set Context and result set at the same time.
4841 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004842 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004843 */
4844static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004845xpath_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 +02004846{
4847 struct lysc_pattern **patterns = NULL, **pattern;
4848 struct lysc_node_leaf *sleaf;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004849 LY_ERR rc = LY_SUCCESS;
4850 struct ly_err_item *err;
4851
4852 if (options & LYXP_SCNODE_ALL) {
4853 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4854 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4855 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 +02004856 } else if (!warn_is_string_type(sleaf->type)) {
4857 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004858 }
4859 }
4860
4861 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4862 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4863 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 +02004864 } else if (!warn_is_string_type(sleaf->type)) {
4865 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004866 }
4867 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004868 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004869 return rc;
4870 }
4871
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004872 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004873 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004874 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004875 LY_CHECK_RET(rc);
4876
4877 LY_ARRAY_NEW_RET(set->ctx, patterns, pattern, LY_EMEM);
Radek Iša45802b52021-02-09 09:21:58 +01004878 *pattern = calloc(1, sizeof **pattern);
Radek Krejciddace2c2021-01-08 11:30:56 +01004879 LOG_LOCSET(NULL, set->cur_node, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01004880 rc = lys_compile_type_pattern_check(set->ctx, args[1]->val.str, &(*pattern)->code);
Michal Vasko4a7d4d62021-12-13 17:05:06 +01004881 if (set->cur_node) {
4882 LOG_LOCBACK(0, 1, 0, 0);
4883 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004884 if (rc != LY_SUCCESS) {
4885 LY_ARRAY_FREE(patterns);
4886 return rc;
4887 }
4888
Radek Krejci0b013302021-03-29 15:22:32 +02004889 rc = lyplg_type_validate_patterns(patterns, args[0]->val.str, strlen(args[0]->val.str), &err);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004890 pcre2_code_free((*pattern)->code);
4891 free(*pattern);
4892 LY_ARRAY_FREE(patterns);
4893 if (rc && (rc != LY_EVALID)) {
Michal Vasko177d0ed2020-11-23 16:43:03 +01004894 ly_err_print(set->ctx, err);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004895 ly_err_free(err);
4896 return rc;
4897 }
4898
4899 if (rc == LY_EVALID) {
4900 ly_err_free(err);
4901 set_fill_boolean(set, 0);
4902 } else {
4903 set_fill_boolean(set, 1);
4904 }
4905
4906 return LY_SUCCESS;
4907}
4908
4909/**
4910 * @brief Execute the XPath round(number) function. Returns LYXP_SET_NUMBER
4911 * with the rounded first argument. For details refer to
4912 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-round.
4913 *
4914 * @param[in] args Array of arguments.
4915 * @param[in] arg_count Count of elements in @p args.
4916 * @param[in,out] set Context and result set at the same time.
4917 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004918 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004919 */
4920static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004921xpath_round(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004922{
4923 struct lysc_node_leaf *sleaf;
4924 LY_ERR rc = LY_SUCCESS;
4925
4926 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02004927 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004928 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02004929 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4930 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4931 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4932 sleaf->name);
4933 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
4934 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
4935 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004936 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004937 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004938 return rc;
4939 }
4940
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004941 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004942 LY_CHECK_RET(rc);
4943
4944 /* cover only the cases where floor can't be used */
4945 if ((args[0]->val.num == -0.0f) || ((args[0]->val.num < 0) && (args[0]->val.num >= -0.5))) {
4946 set_fill_number(set, -0.0f);
4947 } else {
4948 args[0]->val.num += 0.5;
4949 rc = xpath_floor(args, 1, args[0], options);
4950 LY_CHECK_RET(rc);
4951 set_fill_number(set, args[0]->val.num);
4952 }
4953
4954 return LY_SUCCESS;
4955}
4956
4957/**
4958 * @brief Execute the XPath starts-with(string, string) function.
4959 * Returns LYXP_SET_BOOLEAN whether the second argument is
4960 * the prefix of the first or not.
4961 *
4962 * @param[in] args Array of arguments.
4963 * @param[in] arg_count Count of elements in @p args.
4964 * @param[in,out] set Context and result set at the same time.
4965 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004966 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004967 */
4968static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02004969xpath_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 +02004970{
4971 struct lysc_node_leaf *sleaf;
4972 LY_ERR rc = LY_SUCCESS;
4973
4974 if (options & LYXP_SCNODE_ALL) {
4975 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4976 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4977 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 +02004978 } else if (!warn_is_string_type(sleaf->type)) {
4979 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004980 }
4981 }
4982
4983 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4984 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4985 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 +02004986 } else if (!warn_is_string_type(sleaf->type)) {
4987 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004988 }
4989 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004990 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004991 return rc;
4992 }
4993
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004994 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004995 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004996 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004997 LY_CHECK_RET(rc);
4998
4999 if (strncmp(args[0]->val.str, args[1]->val.str, strlen(args[1]->val.str))) {
5000 set_fill_boolean(set, 0);
5001 } else {
5002 set_fill_boolean(set, 1);
5003 }
5004
5005 return LY_SUCCESS;
5006}
5007
5008/**
5009 * @brief Execute the XPath string(object?) function. Returns LYXP_SET_STRING
5010 * with the string representation of either the argument or the context.
5011 *
5012 * @param[in] args Array of arguments.
5013 * @param[in] arg_count Count of elements in @p args.
5014 * @param[in,out] set Context and result set at the same time.
5015 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005016 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005017 */
5018static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02005019xpath_string(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005020{
5021 LY_ERR rc;
5022
5023 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02005024 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005025 return LY_SUCCESS;
5026 }
5027
5028 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005029 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005030 LY_CHECK_RET(rc);
5031 set_fill_set(set, args[0]);
5032 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005033 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005034 LY_CHECK_RET(rc);
5035 }
5036
5037 return LY_SUCCESS;
5038}
5039
5040/**
5041 * @brief Execute the XPath string-length(string?) function. Returns LYXP_SET_NUMBER
5042 * with the length of the string in either the argument or the context.
5043 *
5044 * @param[in] args Array of arguments.
5045 * @param[in] arg_count Count of elements in @p args.
5046 * @param[in,out] set Context and result set at the same time.
5047 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005048 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005049 */
5050static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02005051xpath_string_length(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005052{
5053 struct lysc_node_leaf *sleaf;
5054 LY_ERR rc = LY_SUCCESS;
5055
5056 if (options & LYXP_SCNODE_ALL) {
5057 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5058 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5059 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 +02005060 } else if (!warn_is_string_type(sleaf->type)) {
5061 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005062 }
5063 }
5064 if (!arg_count && (set->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set))) {
5065 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5066 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 +02005067 } else if (!warn_is_string_type(sleaf->type)) {
5068 LOGWRN(set->ctx, "Argument #0 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005069 }
5070 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005071 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005072 return rc;
5073 }
5074
5075 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005076 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005077 LY_CHECK_RET(rc);
5078 set_fill_number(set, strlen(args[0]->val.str));
5079 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005080 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005081 LY_CHECK_RET(rc);
5082 set_fill_number(set, strlen(set->val.str));
5083 }
5084
5085 return LY_SUCCESS;
5086}
5087
5088/**
5089 * @brief Execute the XPath substring(string, number, number?) function.
5090 * Returns LYXP_SET_STRING substring of the first argument starting
5091 * on the second argument index ending on the third argument index,
5092 * indexed from 1. For exact definition refer to
5093 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-substring.
5094 *
5095 * @param[in] args Array of arguments.
5096 * @param[in] arg_count Count of elements in @p args.
5097 * @param[in,out] set Context and result set at the same time.
5098 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005099 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005100 */
5101static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02005102xpath_substring(struct lyxp_set **args, uint32_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005103{
Michal Vasko6db996e2022-07-28 10:28:04 +02005104 int64_t start;
5105 int32_t len;
Michal Vaskodd528af2022-08-08 14:35:07 +02005106 uint32_t str_start, str_len, pos;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005107 struct lysc_node_leaf *sleaf;
5108 LY_ERR rc = LY_SUCCESS;
5109
5110 if (options & LYXP_SCNODE_ALL) {
5111 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5112 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5113 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 +02005114 } else if (!warn_is_string_type(sleaf->type)) {
5115 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005116 }
5117 }
5118
5119 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5120 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5121 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 +02005122 } else if (!warn_is_numeric_type(sleaf->type)) {
5123 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005124 }
5125 }
5126
Michal Vasko69730152020-10-09 16:30:07 +02005127 if ((arg_count == 3) && (args[2]->type == LYXP_SET_SCNODE_SET) &&
5128 (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005129 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5130 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 +02005131 } else if (!warn_is_numeric_type(sleaf->type)) {
5132 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005133 }
5134 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005135 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005136 return rc;
5137 }
5138
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005139 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005140 LY_CHECK_RET(rc);
5141
5142 /* start */
5143 if (xpath_round(&args[1], 1, args[1], options)) {
5144 return -1;
5145 }
5146 if (isfinite(args[1]->val.num)) {
5147 start = args[1]->val.num - 1;
5148 } else if (isinf(args[1]->val.num) && signbit(args[1]->val.num)) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02005149 start = INT32_MIN;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005150 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02005151 start = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005152 }
5153
5154 /* len */
5155 if (arg_count == 3) {
5156 rc = xpath_round(&args[2], 1, args[2], options);
5157 LY_CHECK_RET(rc);
Radek Krejci1deb5be2020-08-26 16:43:36 +02005158 if (isnan(args[2]->val.num) || signbit(args[2]->val.num)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005159 len = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005160 } else if (isfinite(args[2]->val.num)) {
5161 len = args[2]->val.num;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005162 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02005163 len = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005164 }
5165 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02005166 len = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005167 }
5168
5169 /* find matching character positions */
5170 str_start = 0;
5171 str_len = 0;
5172 for (pos = 0; args[0]->val.str[pos]; ++pos) {
5173 if (pos < start) {
5174 ++str_start;
5175 } else if (pos < start + len) {
5176 ++str_len;
5177 } else {
5178 break;
5179 }
5180 }
5181
5182 set_fill_string(set, args[0]->val.str + str_start, str_len);
5183 return LY_SUCCESS;
5184}
5185
5186/**
5187 * @brief Execute the XPath substring-after(string, string) function.
5188 * Returns LYXP_SET_STRING with the string succeeding the occurance
5189 * of the second argument in the first or an empty string.
5190 *
5191 * @param[in] args Array of arguments.
5192 * @param[in] arg_count Count of elements in @p args.
5193 * @param[in,out] set Context and result set at the same time.
5194 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005195 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005196 */
5197static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02005198xpath_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 +02005199{
5200 char *ptr;
5201 struct lysc_node_leaf *sleaf;
5202 LY_ERR rc = LY_SUCCESS;
5203
5204 if (options & LYXP_SCNODE_ALL) {
5205 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5206 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5207 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 +02005208 } else if (!warn_is_string_type(sleaf->type)) {
5209 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005210 }
5211 }
5212
5213 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5214 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5215 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 +02005216 } else if (!warn_is_string_type(sleaf->type)) {
5217 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005218 }
5219 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005220 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005221 return rc;
5222 }
5223
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005224 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005225 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005226 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005227 LY_CHECK_RET(rc);
5228
5229 ptr = strstr(args[0]->val.str, args[1]->val.str);
5230 if (ptr) {
5231 set_fill_string(set, ptr + strlen(args[1]->val.str), strlen(ptr + strlen(args[1]->val.str)));
5232 } else {
5233 set_fill_string(set, "", 0);
5234 }
5235
5236 return LY_SUCCESS;
5237}
5238
5239/**
5240 * @brief Execute the XPath substring-before(string, string) function.
5241 * Returns LYXP_SET_STRING with the string preceding the occurance
5242 * of the second argument in the first or an empty string.
5243 *
5244 * @param[in] args Array of arguments.
5245 * @param[in] arg_count Count of elements in @p args.
5246 * @param[in,out] set Context and result set at the same time.
5247 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005248 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005249 */
5250static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02005251xpath_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 +02005252{
5253 char *ptr;
5254 struct lysc_node_leaf *sleaf;
5255 LY_ERR rc = LY_SUCCESS;
5256
5257 if (options & LYXP_SCNODE_ALL) {
5258 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5259 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5260 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 +02005261 } else if (!warn_is_string_type(sleaf->type)) {
5262 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005263 }
5264 }
5265
5266 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5267 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5268 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 +02005269 } else if (!warn_is_string_type(sleaf->type)) {
5270 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005271 }
5272 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005273 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005274 return rc;
5275 }
5276
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005277 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005278 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005279 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005280 LY_CHECK_RET(rc);
5281
5282 ptr = strstr(args[0]->val.str, args[1]->val.str);
5283 if (ptr) {
5284 set_fill_string(set, args[0]->val.str, ptr - args[0]->val.str);
5285 } else {
5286 set_fill_string(set, "", 0);
5287 }
5288
5289 return LY_SUCCESS;
5290}
5291
5292/**
5293 * @brief Execute the XPath sum(node-set) function. Returns LYXP_SET_NUMBER
5294 * with the sum of all the nodes in the context.
5295 *
5296 * @param[in] args Array of arguments.
5297 * @param[in] arg_count Count of elements in @p args.
5298 * @param[in,out] set Context and result set at the same time.
5299 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005300 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005301 */
5302static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02005303xpath_sum(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005304{
5305 long double num;
5306 char *str;
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01005307 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005308 struct lyxp_set set_item;
5309 struct lysc_node_leaf *sleaf;
5310 LY_ERR rc = LY_SUCCESS;
5311
5312 if (options & LYXP_SCNODE_ALL) {
5313 if (args[0]->type == LYXP_SET_SCNODE_SET) {
5314 for (i = 0; i < args[0]->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005315 if (args[0]->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005316 sleaf = (struct lysc_node_leaf *)args[0]->val.scnodes[i].scnode;
5317 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5318 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__,
Michal Vasko69730152020-10-09 16:30:07 +02005319 lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005320 } else if (!warn_is_numeric_type(sleaf->type)) {
5321 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005322 }
5323 }
5324 }
5325 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005326 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005327 return rc;
5328 }
5329
5330 set_fill_number(set, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005331
5332 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005333 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "sum(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02005334 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02005335 } else if (!args[0]->used) {
5336 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005337 }
5338
Michal Vasko5c4e5892019-11-14 12:31:38 +01005339 set_init(&set_item, set);
5340
Michal Vasko03ff5a72019-09-11 13:49:33 +02005341 set_item.type = LYXP_SET_NODE_SET;
Michal Vasko41decbf2021-11-02 11:50:21 +01005342 set_item.val.nodes = calloc(1, sizeof *set_item.val.nodes);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005343 LY_CHECK_ERR_RET(!set_item.val.nodes, LOGMEM(set->ctx), LY_EMEM);
5344
5345 set_item.used = 1;
5346 set_item.size = 1;
5347
5348 for (i = 0; i < args[0]->used; ++i) {
5349 set_item.val.nodes[0] = args[0]->val.nodes[i];
5350
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005351 rc = cast_node_set_to_string(&set_item, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005352 LY_CHECK_RET(rc);
5353 num = cast_string_to_number(str);
5354 free(str);
5355 set->val.num += num;
5356 }
5357
5358 free(set_item.val.nodes);
5359
5360 return LY_SUCCESS;
5361}
5362
5363/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02005364 * @brief Execute the XPath translate(string, string, string) function.
5365 * Returns LYXP_SET_STRING with the first argument with the characters
5366 * from the second argument replaced by those on the corresponding
5367 * positions in the third argument.
5368 *
5369 * @param[in] args Array of arguments.
5370 * @param[in] arg_count Count of elements in @p args.
5371 * @param[in,out] set Context and result set at the same time.
5372 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005373 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005374 */
5375static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02005376xpath_translate(struct lyxp_set **args, uint32_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005377{
Michal Vaskodd528af2022-08-08 14:35:07 +02005378 uint32_t i, j, new_used;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005379 char *new;
Radek Krejci857189e2020-09-01 13:26:36 +02005380 ly_bool have_removed;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005381 struct lysc_node_leaf *sleaf;
5382 LY_ERR rc = LY_SUCCESS;
5383
5384 if (options & LYXP_SCNODE_ALL) {
5385 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5386 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5387 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 +02005388 } else if (!warn_is_string_type(sleaf->type)) {
5389 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005390 }
5391 }
5392
5393 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5394 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5395 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 +02005396 } else if (!warn_is_string_type(sleaf->type)) {
5397 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005398 }
5399 }
5400
5401 if ((args[2]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
5402 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5403 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 +02005404 } else if (!warn_is_string_type(sleaf->type)) {
5405 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005406 }
5407 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005408 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005409 return rc;
5410 }
5411
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005412 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005413 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005414 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005415 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005416 rc = lyxp_set_cast(args[2], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005417 LY_CHECK_RET(rc);
5418
5419 new = malloc((strlen(args[0]->val.str) + 1) * sizeof(char));
5420 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5421 new_used = 0;
5422
5423 have_removed = 0;
5424 for (i = 0; args[0]->val.str[i]; ++i) {
Radek Krejci857189e2020-09-01 13:26:36 +02005425 ly_bool found = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005426
5427 for (j = 0; args[1]->val.str[j]; ++j) {
5428 if (args[0]->val.str[i] == args[1]->val.str[j]) {
5429 /* removing this char */
5430 if (j >= strlen(args[2]->val.str)) {
5431 have_removed = 1;
5432 found = 1;
5433 break;
5434 }
5435 /* replacing this char */
5436 new[new_used] = args[2]->val.str[j];
5437 ++new_used;
5438 found = 1;
5439 break;
5440 }
5441 }
5442
5443 /* copying this char */
5444 if (!found) {
5445 new[new_used] = args[0]->val.str[i];
5446 ++new_used;
5447 }
5448 }
5449
5450 if (have_removed) {
5451 new = ly_realloc(new, (new_used + 1) * sizeof(char));
5452 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5453 }
5454 new[new_used] = '\0';
5455
Michal Vaskod3678892020-05-21 10:06:58 +02005456 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005457 set->type = LYXP_SET_STRING;
5458 set->val.str = new;
5459
5460 return LY_SUCCESS;
5461}
5462
5463/**
5464 * @brief Execute the XPath true() function. Returns LYXP_SET_BOOLEAN
5465 * with true value.
5466 *
5467 * @param[in] args Array of arguments.
5468 * @param[in] arg_count Count of elements in @p args.
5469 * @param[in,out] set Context and result set at the same time.
5470 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005471 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005472 */
5473static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02005474xpath_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 +02005475{
5476 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02005477 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005478 return LY_SUCCESS;
5479 }
5480
5481 set_fill_boolean(set, 1);
5482 return LY_SUCCESS;
5483}
5484
Michal Vasko03ff5a72019-09-11 13:49:33 +02005485/**
Michal Vasko49fec8e2022-05-24 10:28:33 +02005486 * @brief Execute the XPath node() processing instruction (node type). Returns LYXP_SET_NODE_SET
5487 * with only nodes from the context.
5488 *
5489 * @param[in,out] set Context and result set at the same time.
5490 * @param[in] axis Axis to search on.
5491 * @param[in] options XPath options.
5492 * @return LY_ERR
5493 */
5494static LY_ERR
5495xpath_pi_node(struct lyxp_set *set, enum lyxp_axis axis, uint32_t options)
5496{
5497 if (options & LYXP_SCNODE_ALL) {
5498 return moveto_scnode(set, NULL, NULL, axis, options);
5499 }
5500
5501 if (set->type != LYXP_SET_NODE_SET) {
5502 lyxp_set_free_content(set);
5503 return LY_SUCCESS;
5504 }
5505
5506 /* just like moving to a node with no restrictions */
5507 return moveto_node(set, NULL, NULL, axis, options);
5508}
5509
5510/**
5511 * @brief Execute the XPath text() processing instruction (node type). Returns LYXP_SET_NODE_SET
5512 * with the text content of the nodes in the context.
5513 *
5514 * @param[in,out] set Context and result set at the same time.
5515 * @param[in] axis Axis to search on.
5516 * @param[in] options XPath options.
5517 * @return LY_ERR
5518 */
5519static LY_ERR
5520xpath_pi_text(struct lyxp_set *set, enum lyxp_axis axis, uint32_t options)
5521{
5522 uint32_t i;
5523
5524 if (options & LYXP_SCNODE_ALL) {
5525 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
5526 return LY_SUCCESS;
5527 }
5528
5529 if (set->type != LYXP_SET_NODE_SET) {
5530 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "text()");
5531 return LY_EVALID;
5532 }
5533
5534 if (axis != LYXP_AXIS_CHILD) {
5535 /* even following and preceding axescan return text nodes, but whatever */
5536 lyxp_set_free_content(set);
5537 return LY_SUCCESS;
5538 }
5539
5540 for (i = 0; i < set->used; ++i) {
5541 switch (set->val.nodes[i].type) {
5542 case LYXP_NODE_NONE:
5543 LOGINT_RET(set->ctx);
5544 case LYXP_NODE_ELEM:
5545 if (set->val.nodes[i].node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
5546 set->val.nodes[i].type = LYXP_NODE_TEXT;
5547 break;
5548 }
5549 /* fall through */
5550 case LYXP_NODE_ROOT:
5551 case LYXP_NODE_ROOT_CONFIG:
5552 case LYXP_NODE_TEXT:
5553 case LYXP_NODE_META:
5554 set_remove_node_none(set, i);
5555 break;
5556 }
5557 }
5558 set_remove_nodes_none(set);
5559
5560 return LY_SUCCESS;
5561}
5562
5563/**
Michal Vasko6346ece2019-09-24 13:12:53 +02005564 * @brief Skip prefix and return corresponding model if there is a prefix. Logs directly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005565 *
Michal Vasko2104e9f2020-03-06 08:23:25 +01005566 * XPath @p set is expected to be a (sc)node set!
5567 *
Michal Vasko6346ece2019-09-24 13:12:53 +02005568 * @param[in,out] qname Qualified node name. If includes prefix, it is skipped.
5569 * @param[in,out] qname_len Length of @p qname, is updated accordingly.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005570 * @param[in] set Set with general XPath context.
5571 * @param[in] ctx_scnode Context node to inherit module for unprefixed node for ::LY_PREF_JSON.
Michal Vasko6346ece2019-09-24 13:12:53 +02005572 * @param[out] moveto_mod Expected module of a matching node.
5573 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005574 */
Michal Vasko6346ece2019-09-24 13:12:53 +02005575static LY_ERR
Michal Vaskofe1af042022-07-29 14:58:59 +02005576moveto_resolve_model(const char **qname, uint32_t *qname_len, const struct lyxp_set *set,
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005577 const struct lysc_node *ctx_scnode, const struct lys_module **moveto_mod)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005578{
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02005579 const struct lys_module *mod = NULL;
Michal Vasko6346ece2019-09-24 13:12:53 +02005580 const char *ptr;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005581 size_t pref_len;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005582
Michal Vasko2104e9f2020-03-06 08:23:25 +01005583 assert((set->type == LYXP_SET_NODE_SET) || (set->type == LYXP_SET_SCNODE_SET));
5584
Michal Vasko6346ece2019-09-24 13:12:53 +02005585 if ((ptr = ly_strnchr(*qname, ':', *qname_len))) {
5586 /* specific module */
5587 pref_len = ptr - *qname;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005588 mod = ly_resolve_prefix(set->ctx, *qname, pref_len, set->format, set->prefix_data);
Michal Vasko6346ece2019-09-24 13:12:53 +02005589
Michal Vasko004d3152020-06-11 19:59:22 +02005590 /* check for errors and non-implemented modules, as they are not valid */
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005591 if (!mod || !mod->implemented) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005592 LOGVAL(set->ctx, LY_VCODE_XP_INMOD, pref_len, *qname);
Michal Vasko6346ece2019-09-24 13:12:53 +02005593 return LY_EVALID;
5594 }
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005595
Michal Vasko6346ece2019-09-24 13:12:53 +02005596 *qname += pref_len + 1;
5597 *qname_len -= pref_len + 1;
5598 } else if (((*qname)[0] == '*') && (*qname_len == 1)) {
5599 /* all modules - special case */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005600 mod = NULL;
Michal Vasko6346ece2019-09-24 13:12:53 +02005601 } else {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005602 switch (set->format) {
Radek Krejci8df109d2021-04-23 12:19:08 +02005603 case LY_VALUE_SCHEMA:
5604 case LY_VALUE_SCHEMA_RESOLVED:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005605 /* current module */
5606 mod = set->cur_mod;
5607 break;
Radek Krejci224d4b42021-04-23 13:54:59 +02005608 case LY_VALUE_CANON:
Radek Krejci8df109d2021-04-23 12:19:08 +02005609 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +02005610 case LY_VALUE_LYB:
Michal Vaskoddd76592022-01-17 13:34:48 +01005611 case LY_VALUE_STR_NS:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005612 /* inherit parent (context node) module */
5613 if (ctx_scnode) {
5614 mod = ctx_scnode->module;
5615 } else {
5616 mod = NULL;
5617 }
5618 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02005619 case LY_VALUE_XML:
Michal Vasko52143d12021-04-14 15:36:39 +02005620 /* all nodes need to be prefixed */
5621 LOGVAL(set->ctx, LYVE_DATA, "Non-prefixed node \"%.*s\" in XML xpath found.", *qname_len, *qname);
5622 return LY_EVALID;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005623 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005624 }
5625
Michal Vasko6346ece2019-09-24 13:12:53 +02005626 *moveto_mod = mod;
5627 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005628}
5629
5630/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02005631 * @brief Move context @p set to the root. Handles absolute path.
5632 * Result is LYXP_SET_NODE_SET.
5633 *
5634 * @param[in,out] set Set to use.
5635 * @param[in] options Xpath options.
Michal Vaskob0099a92020-08-31 14:55:23 +02005636 * @return LY_ERR value.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005637 */
Michal Vaskob0099a92020-08-31 14:55:23 +02005638static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005639moveto_root(struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005640{
aPiecek8b0cc152021-05-31 16:40:31 +02005641 assert(!(options & LYXP_SKIP_EXPR));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005642
5643 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02005644 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko7333cb32022-07-29 16:30:29 +02005645 LY_CHECK_RET(set_scnode_insert_node(set, NULL, set->root_type, LYXP_AXIS_SELF, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005646 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005647 set->type = LYXP_SET_NODE_SET;
5648 set->used = 0;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005649 set_insert_node(set, NULL, 0, set->root_type, 0);
Michal Vasko306e2832022-07-25 09:15:17 +02005650 set->non_child_axis = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005651 }
Michal Vaskob0099a92020-08-31 14:55:23 +02005652
5653 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005654}
5655
5656/**
5657 * @brief Check @p node as a part of NameTest processing.
5658 *
5659 * @param[in] node Node to check.
Michal Vasko49fec8e2022-05-24 10:28:33 +02005660 * @param[in] node_type Node type of @p node.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005661 * @param[in] set Set to read general context from.
Michal Vaskod3678892020-05-21 10:06:58 +02005662 * @param[in] node_name Node name in the dictionary to move to, NULL for any node.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005663 * @param[in] moveto_mod Expected module of the node, NULL for no prefix.
Michal Vaskocdad7122020-11-09 21:04:44 +01005664 * @param[in] options XPath options.
Michal Vasko6346ece2019-09-24 13:12:53 +02005665 * @return LY_ERR (LY_ENOT if node does not match, LY_EINCOMPLETE on unresolved when,
Michal Vaskocd2c88a2022-06-07 10:54:34 +02005666 * LY_EINVAL if neither node nor any children match)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005667 */
5668static LY_ERR
Michal Vasko49fec8e2022-05-24 10:28:33 +02005669moveto_node_check(const struct lyd_node *node, enum lyxp_node_type node_type, const struct lyxp_set *set,
5670 const char *node_name, const struct lys_module *moveto_mod, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005671{
Michal Vasko49fec8e2022-05-24 10:28:33 +02005672 if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT)) {
5673 assert(node_type == set->root_type);
5674
5675 if (node_name || moveto_mod) {
5676 /* root will not match a specific node */
5677 return LY_ENOT;
5678 }
5679 return LY_SUCCESS;
5680 } else if (node_type != LYXP_NODE_ELEM) {
5681 /* other types will not match */
5682 return LY_ENOT;
5683 }
5684
Michal Vaskodca9f122021-07-16 13:56:22 +02005685 if (!node->schema) {
5686 /* opaque node never matches */
5687 return LY_ENOT;
5688 }
5689
Michal Vasko03ff5a72019-09-11 13:49:33 +02005690 /* module check */
Michal Vasko19089f02022-06-07 11:02:11 +02005691 if (moveto_mod) {
Michal Vasko0a1eb752022-09-29 11:00:34 +02005692 if ((set->ctx == LYD_CTX(node)) && (node->schema->module != moveto_mod)) {
Michal Vasko19089f02022-06-07 11:02:11 +02005693 return LY_ENOT;
Michal Vasko0a1eb752022-09-29 11:00:34 +02005694 } else if ((set->ctx != LYD_CTX(node)) && strcmp(node->schema->module->name, moveto_mod->name)) {
Michal Vasko19089f02022-06-07 11:02:11 +02005695 return LY_ENOT;
5696 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005697 }
5698
Michal Vasko5c4e5892019-11-14 12:31:38 +01005699 /* context check */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005700 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005701 return LY_EINVAL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005702 } else if (set->context_op && (node->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) &&
5703 (node->schema != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005704 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005705 }
5706
5707 /* name check */
Michal Vasko19089f02022-06-07 11:02:11 +02005708 if (node_name) {
Michal Vasko0a1eb752022-09-29 11:00:34 +02005709 if ((set->ctx == LYD_CTX(node)) && (node->schema->name != node_name)) {
Michal Vasko19089f02022-06-07 11:02:11 +02005710 return LY_ENOT;
Michal Vasko0a1eb752022-09-29 11:00:34 +02005711 } else if ((set->ctx != LYD_CTX(node)) && strcmp(node->schema->name, node_name)) {
Michal Vasko19089f02022-06-07 11:02:11 +02005712 return LY_ENOT;
5713 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005714 }
5715
Michal Vasko6d657122022-10-19 14:38:35 +02005716 /* when check, accept the context node because it should only be the path ".", we have checked the when is valid before */
5717 if (!(options & LYXP_IGNORE_WHEN) && lysc_has_when(node->schema) && !(node->flags & LYD_WHEN_TRUE) &&
5718 (node != set->cur_node)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005719 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01005720 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005721
5722 /* match */
5723 return LY_SUCCESS;
5724}
5725
5726/**
Michal Vasko49fec8e2022-05-24 10:28:33 +02005727 * @brief Get the next node in a forward DFS.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005728 *
Michal Vasko49fec8e2022-05-24 10:28:33 +02005729 * @param[in] iter Last returned node.
5730 * @param[in] stop Node to stop the search on and not return.
5731 * @return Next node, NULL if there are no more.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005732 */
Michal Vasko49fec8e2022-05-24 10:28:33 +02005733static const struct lyd_node *
5734moveto_axis_node_next_dfs_forward(const struct lyd_node *iter, const struct lyd_node *stop)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005735{
Michal Vasko49fec8e2022-05-24 10:28:33 +02005736 const struct lyd_node *next = NULL;
5737
5738 /* 1) child */
5739 next = lyd_child(iter);
5740 if (!next) {
5741 if (iter == stop) {
5742 /* reached stop, no more descendants */
5743 return NULL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005744 }
Michal Vasko49fec8e2022-05-24 10:28:33 +02005745 /* 2) child next sibling */
5746 next = iter->next;
5747 }
5748 while (!next) {
5749 iter = lyd_parent(iter);
5750 if ((!stop && !iter) || (stop && (lyd_parent(iter) == lyd_parent(stop)))) {
5751 return NULL;
5752 }
5753 next = iter->next;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005754 }
5755
Michal Vasko49fec8e2022-05-24 10:28:33 +02005756 return next;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005757}
5758
5759/**
Michal Vasko49fec8e2022-05-24 10:28:33 +02005760 * @brief Get the next node in a backward DFS.
5761 *
5762 * @param[in] iter Last returned node.
5763 * @param[in] stop Node to stop the search on and not return.
5764 * @return Next node, NULL if there are no more.
5765 */
5766static const struct lyd_node *
5767moveto_axis_node_next_dfs_backward(const struct lyd_node *iter, const struct lyd_node *stop)
5768{
5769 const struct lyd_node *next = NULL;
5770
5771 /* 1) previous sibling innermost last child */
5772 next = iter->prev->next ? iter->prev : NULL;
5773 while (next && lyd_child(next)) {
5774 next = lyd_child(next);
5775 next = next->prev;
5776 }
5777
5778 if (!next) {
5779 /* 2) parent */
5780 iter = lyd_parent(iter);
5781 if ((!stop && !iter) || (stop && (lyd_parent(iter) == lyd_parent(stop)))) {
5782 return NULL;
5783 }
5784 next = iter;
5785 }
5786
5787 return next;
5788}
5789
5790/**
5791 * @brief Get the first node on an axis for a context node.
5792 *
5793 * @param[in,out] iter NULL, updated to the next node.
5794 * @param[in,out] iter_type Node type 0 of @p iter, updated to the node type of the next node.
5795 * @param[in] node Context node.
5796 * @param[in] node_type Type of @p node.
5797 * @param[in] axis Axis to use.
5798 * @param[in] set XPath set with the general context.
5799 * @return LY_SUCCESS on success.
5800 * @return LY_ENOTFOUND if no next node found.
5801 */
5802static LY_ERR
5803moveto_axis_node_next_first(const struct lyd_node **iter, enum lyxp_node_type *iter_type, const struct lyd_node *node,
5804 enum lyxp_node_type node_type, enum lyxp_axis axis, struct lyxp_set *set)
5805{
5806 const struct lyd_node *next = NULL;
5807 enum lyxp_node_type next_type = 0;
5808
5809 assert(!*iter);
5810 assert(!*iter_type);
5811
5812 switch (axis) {
5813 case LYXP_AXIS_ANCESTOR_OR_SELF:
5814 case LYXP_AXIS_DESCENDANT_OR_SELF:
5815 case LYXP_AXIS_SELF:
5816 /* return the context node */
5817 next = node;
5818 next_type = node_type;
5819 break;
5820
5821 case LYXP_AXIS_ANCESTOR:
5822 case LYXP_AXIS_PARENT:
5823 if (node_type == LYXP_NODE_ELEM) {
5824 next = lyd_parent(node);
5825 next_type = next ? LYXP_NODE_ELEM : set->root_type;
5826 } else if (node_type == LYXP_NODE_TEXT) {
5827 next = node;
5828 next_type = LYXP_NODE_ELEM;
5829 } else if (node_type == LYXP_NODE_META) {
5830 next = ((struct lyd_meta *)node)->parent;
5831 next_type = LYXP_NODE_ELEM;
5832 } /* else root does not have a parent */
5833 break;
5834
5835 case LYXP_AXIS_CHILD:
5836 if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT)) {
5837 assert(!node);
5838
5839 /* search in all the trees */
5840 next = set->tree;
5841 next_type = next ? LYXP_NODE_ELEM : 0;
5842 } else {
5843 /* search in children */
5844 next = lyd_child(node);
5845 next_type = next ? LYXP_NODE_ELEM : 0;
5846 }
5847 break;
5848
5849 case LYXP_AXIS_DESCENDANT:
5850 if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT)) {
5851 /* top-level nodes */
5852 next = set->tree;
5853 next_type = LYXP_NODE_ELEM;
5854 } else if (node_type == LYXP_NODE_ELEM) {
5855 /* start from the context node */
5856 next = moveto_axis_node_next_dfs_forward(node, node);
5857 next_type = next ? LYXP_NODE_ELEM : 0;
5858 } /* else no children */
5859 break;
5860
5861 case LYXP_AXIS_FOLLOWING:
5862 case LYXP_AXIS_FOLLOWING_SIBLING:
5863 if (node_type == LYXP_NODE_ELEM) {
5864 /* first next sibling */
5865 next = node->next;
5866 next_type = next ? LYXP_NODE_ELEM : 0;
5867 } /* else no sibling */
5868 break;
5869
5870 case LYXP_AXIS_PRECEDING:
5871 if ((node_type == LYXP_NODE_ELEM) && node->prev->next) {
5872 /* skip ancestors */
5873 next = moveto_axis_node_next_dfs_backward(node, NULL);
5874 assert(next);
5875 next_type = LYXP_NODE_ELEM;
5876 } /* else no sibling */
5877 break;
5878
5879 case LYXP_AXIS_PRECEDING_SIBLING:
5880 if (node_type == LYXP_NODE_ELEM) {
5881 /* first previous sibling */
5882 next = node->prev->next ? node->prev : NULL;
5883 next_type = next ? LYXP_NODE_ELEM : 0;
5884 } /* else no sibling */
5885 break;
5886
5887 case LYXP_AXIS_ATTRIBUTE:
5888 /* handled specially */
5889 assert(0);
5890 LOGINT(set->ctx);
5891 break;
5892 }
5893
5894 *iter = next;
5895 *iter_type = next_type;
5896 return next_type ? LY_SUCCESS : LY_ENOTFOUND;
5897}
5898
5899/**
5900 * @brief Iterate over all nodes on an axis for a context node.
5901 *
5902 * @param[in,out] iter Last returned node, start with NULL, updated to the next node.
5903 * @param[in,out] iter_type Node type of @p iter, start with 0, updated to the node type of the next node.
5904 * @param[in] node Context node.
5905 * @param[in] node_type Type of @p node.
5906 * @param[in] axis Axis to use.
5907 * @param[in] set XPath set with the general context.
5908 * @return LY_SUCCESS on success.
5909 * @return LY_ENOTFOUND if no next node found.
5910 */
5911static LY_ERR
5912moveto_axis_node_next(const struct lyd_node **iter, enum lyxp_node_type *iter_type, const struct lyd_node *node,
5913 enum lyxp_node_type node_type, enum lyxp_axis axis, struct lyxp_set *set)
5914{
5915 const struct lyd_node *next = NULL;
5916 enum lyxp_node_type next_type = 0;
5917
5918 if (!*iter_type) {
5919 /* first returned node */
5920 return moveto_axis_node_next_first(iter, iter_type, node, node_type, axis, set);
5921 }
5922
5923 switch (axis) {
5924 case LYXP_AXIS_ANCESTOR_OR_SELF:
5925 if ((*iter == node) && (*iter_type == node_type)) {
5926 /* fake first ancestor, we returned self before */
5927 *iter = NULL;
5928 *iter_type = 0;
5929 return moveto_axis_node_next_first(iter, iter_type, node, node_type, LYXP_AXIS_ANCESTOR, set);
5930 } /* else continue ancestor */
5931
5932 /* fallthrough */
5933 case LYXP_AXIS_ANCESTOR:
5934 if (*iter_type == LYXP_NODE_ELEM) {
5935 /* iter parent */
5936 next = lyd_parent(*iter);
5937 next_type = next ? LYXP_NODE_ELEM : set->root_type;
5938 } /* else root, no ancestors */
5939 break;
5940
5941 case LYXP_AXIS_CHILD:
5942 assert(*iter_type == LYXP_NODE_ELEM);
5943
5944 /* next sibling (child) */
5945 next = (*iter)->next;
5946 next_type = next ? LYXP_NODE_ELEM : 0;
5947 break;
5948
5949 case LYXP_AXIS_DESCENDANT_OR_SELF:
5950 if ((*iter == node) && (*iter_type == node_type)) {
5951 /* fake first descendant, we returned self before */
5952 *iter = NULL;
5953 *iter_type = 0;
5954 return moveto_axis_node_next_first(iter, iter_type, node, node_type, LYXP_AXIS_DESCENDANT, set);
5955 } /* else continue descendant */
5956
5957 /* fallthrough */
5958 case LYXP_AXIS_DESCENDANT:
5959 assert(*iter_type == LYXP_NODE_ELEM);
5960 next = moveto_axis_node_next_dfs_forward(*iter, node);
5961 next_type = next ? LYXP_NODE_ELEM : 0;
5962 break;
5963
5964 case LYXP_AXIS_FOLLOWING:
5965 assert(*iter_type == LYXP_NODE_ELEM);
5966 next = moveto_axis_node_next_dfs_forward(*iter, NULL);
5967 next_type = next ? LYXP_NODE_ELEM : 0;
5968 break;
5969
5970 case LYXP_AXIS_FOLLOWING_SIBLING:
5971 assert(*iter_type == LYXP_NODE_ELEM);
5972
5973 /* next sibling */
5974 next = (*iter)->next;
5975 next_type = next ? LYXP_NODE_ELEM : 0;
5976 break;
5977
5978 case LYXP_AXIS_PARENT:
5979 case LYXP_AXIS_SELF:
5980 /* parent/self was returned before */
5981 break;
5982
5983 case LYXP_AXIS_PRECEDING:
5984 assert(*iter_type == LYXP_NODE_ELEM);
5985 next = moveto_axis_node_next_dfs_backward(*iter, NULL);
5986 next_type = next ? LYXP_NODE_ELEM : 0;
5987 break;
5988
5989 case LYXP_AXIS_PRECEDING_SIBLING:
5990 assert(*iter_type == LYXP_NODE_ELEM);
5991
5992 /* previous sibling */
5993 next = (*iter)->prev->next ? (*iter)->prev : NULL;
5994 next_type = next ? LYXP_NODE_ELEM : 0;
5995 break;
5996
5997 case LYXP_AXIS_ATTRIBUTE:
5998 /* handled specially */
5999 assert(0);
6000 LOGINT(set->ctx);
6001 break;
6002 }
6003
6004 *iter = next;
6005 *iter_type = next_type;
6006 return next_type ? LY_SUCCESS : LY_ENOTFOUND;
6007}
6008
6009/**
6010 * @brief Move context @p set to a node. Result is LYXP_SET_NODE_SET. Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006011 *
6012 * @param[in,out] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006013 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02006014 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko49fec8e2022-05-24 10:28:33 +02006015 * @param[in] axis Axis to search on.
Michal Vaskocdad7122020-11-09 21:04:44 +01006016 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006017 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6018 */
6019static LY_ERR
Michal Vasko49fec8e2022-05-24 10:28:33 +02006020moveto_node(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, enum lyxp_axis axis,
6021 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006022{
Michal Vasko230cf972021-12-02 12:31:00 +01006023 LY_ERR r, rc = LY_SUCCESS;
Michal Vasko49fec8e2022-05-24 10:28:33 +02006024 const struct lyd_node *iter;
6025 enum lyxp_node_type iter_type;
Michal Vasko230cf972021-12-02 12:31:00 +01006026 struct lyxp_set result;
Michal Vasko49fec8e2022-05-24 10:28:33 +02006027 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006028
aPiecek8b0cc152021-05-31 16:40:31 +02006029 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006030 return LY_SUCCESS;
6031 }
6032
6033 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006034 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006035 return LY_EVALID;
6036 }
6037
Michal Vasko230cf972021-12-02 12:31:00 +01006038 /* init result set */
6039 set_init(&result, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006040
Michal Vasko49fec8e2022-05-24 10:28:33 +02006041 for (i = 0; i < set->used; ++i) {
6042 /* iterate over all the nodes on the axis of the node */
6043 iter = NULL;
6044 iter_type = 0;
6045 while (!moveto_axis_node_next(&iter, &iter_type, set->val.nodes[i].node, set->val.nodes[i].type, axis, set)) {
6046 r = moveto_node_check(iter, iter_type, set, ncname, moveto_mod, options);
6047 if (r == LY_EINCOMPLETE) {
Michal Vasko230cf972021-12-02 12:31:00 +01006048 rc = r;
6049 goto cleanup;
Michal Vasko49fec8e2022-05-24 10:28:33 +02006050 } else if (r) {
6051 continue;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006052 }
Michal Vasko49fec8e2022-05-24 10:28:33 +02006053
6054 /* check for duplicates if they are possible */
6055 switch (axis) {
6056 case LYXP_AXIS_ANCESTOR:
6057 case LYXP_AXIS_ANCESTOR_OR_SELF:
6058 case LYXP_AXIS_DESCENDANT:
6059 case LYXP_AXIS_DESCENDANT_OR_SELF:
6060 case LYXP_AXIS_FOLLOWING:
6061 case LYXP_AXIS_FOLLOWING_SIBLING:
6062 case LYXP_AXIS_PARENT:
6063 case LYXP_AXIS_PRECEDING:
6064 case LYXP_AXIS_PRECEDING_SIBLING:
Michal Vasko306e2832022-07-25 09:15:17 +02006065 result.non_child_axis = 1;
Michal Vasko49fec8e2022-05-24 10:28:33 +02006066 if (set_dup_node_check(&result, iter, iter_type, -1)) {
6067 continue;
6068 }
6069 break;
6070 case LYXP_AXIS_CHILD:
6071 case LYXP_AXIS_SELF:
6072 break;
6073 case LYXP_AXIS_ATTRIBUTE:
6074 /* handled specially */
6075 assert(0);
6076 LOGINT(set->ctx);
6077 break;
6078 }
6079
6080 /* matching node */
6081 set_insert_node(&result, iter, 0, iter_type, result.used);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006082 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006083 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006084
Michal Vasko230cf972021-12-02 12:31:00 +01006085 /* move result to the set */
6086 lyxp_set_free_content(set);
6087 *set = result;
6088 result.type = LYXP_SET_NUMBER;
Michal Vasko49fec8e2022-05-24 10:28:33 +02006089
Michal Vasko306e2832022-07-25 09:15:17 +02006090 /* sort the final set if the document order could have been broken */
6091 if (set->non_child_axis) {
6092 set_sort(set);
6093 } else {
6094 assert(!set_sort(set));
6095 }
Michal Vasko230cf972021-12-02 12:31:00 +01006096
6097cleanup:
6098 lyxp_set_free_content(&result);
6099 return rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006100}
6101
6102/**
Michal Vasko49fec8e2022-05-24 10:28:33 +02006103 * @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 +02006104 *
6105 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02006106 * @param[in] scnode Matching node schema.
Michal Vasko004d3152020-06-11 19:59:22 +02006107 * @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 +01006108 * @param[in] options XPath options.
Michal Vaskod3678892020-05-21 10:06:58 +02006109 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6110 */
6111static LY_ERR
Michal Vasko49fec8e2022-05-24 10:28:33 +02006112moveto_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 +01006113 uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02006114{
Michal Vaskoddd76592022-01-17 13:34:48 +01006115 LY_ERR ret = LY_SUCCESS, r;
Michal Vaskod3678892020-05-21 10:06:58 +02006116 uint32_t i;
Michal Vaskod3678892020-05-21 10:06:58 +02006117 const struct lyd_node *siblings;
Michal Vasko230cf972021-12-02 12:31:00 +01006118 struct lyxp_set result;
Michal Vasko004d3152020-06-11 19:59:22 +02006119 struct lyd_node *sub, *inst = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02006120
Michal Vasko004d3152020-06-11 19:59:22 +02006121 assert(scnode && (!(scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)) || predicates));
Michal Vaskod3678892020-05-21 10:06:58 +02006122
Michal Vasko50aaa072021-12-02 13:11:56 +01006123 /* init result set */
6124 set_init(&result, set);
6125
aPiecek8b0cc152021-05-31 16:40:31 +02006126 if (options & LYXP_SKIP_EXPR) {
Michal Vasko004d3152020-06-11 19:59:22 +02006127 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02006128 }
6129
6130 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006131 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko004d3152020-06-11 19:59:22 +02006132 ret = LY_EVALID;
6133 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02006134 }
6135
6136 /* context check for all the nodes since we have the schema node */
6137 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) {
6138 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02006139 goto cleanup;
Michal Vasko69730152020-10-09 16:30:07 +02006140 } else if (set->context_op && (scnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) &&
6141 (scnode != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02006142 lyxp_set_free_content(set);
6143 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +02006144 }
6145
6146 /* create specific data instance if needed */
6147 if (scnode->nodetype == LYS_LIST) {
6148 LY_CHECK_GOTO(ret = lyd_create_list(scnode, predicates, &inst), cleanup);
6149 } else if (scnode->nodetype == LYS_LEAFLIST) {
6150 LY_CHECK_GOTO(ret = lyd_create_term2(scnode, &predicates[0].value, &inst), cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02006151 }
6152
Michal Vasko230cf972021-12-02 12:31:00 +01006153 for (i = 0; i < set->used; ++i) {
Michal Vaskod3678892020-05-21 10:06:58 +02006154 siblings = NULL;
6155
6156 if ((set->val.nodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.nodes[i].type == LYXP_NODE_ROOT)) {
6157 assert(!set->val.nodes[i].node);
6158
6159 /* search in all the trees */
6160 siblings = set->tree;
6161 } else if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
6162 /* search in children */
Radek Krejcia1c1e542020-09-29 16:06:52 +02006163 siblings = lyd_child(set->val.nodes[i].node);
Michal Vaskod3678892020-05-21 10:06:58 +02006164 }
6165
6166 /* find the node using hashes */
Michal Vasko004d3152020-06-11 19:59:22 +02006167 if (inst) {
Michal Vaskoddd76592022-01-17 13:34:48 +01006168 r = lyd_find_sibling_first(siblings, inst, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02006169 } else {
Michal Vaskoddd76592022-01-17 13:34:48 +01006170 r = lyd_find_sibling_val(siblings, scnode, NULL, 0, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02006171 }
Michal Vaskoddd76592022-01-17 13:34:48 +01006172 LY_CHECK_ERR_GOTO(r && (r != LY_ENOTFOUND), ret = r, cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02006173
6174 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01006175 if (!(options & LYXP_IGNORE_WHEN) && sub && lysc_has_when(sub->schema) && !(sub->flags & LYD_WHEN_TRUE)) {
Michal Vasko004d3152020-06-11 19:59:22 +02006176 ret = LY_EINCOMPLETE;
6177 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02006178 }
6179
6180 if (sub) {
6181 /* pos filled later */
Michal Vasko230cf972021-12-02 12:31:00 +01006182 set_insert_node(&result, sub, 0, LYXP_NODE_ELEM, result.used);
Michal Vaskod3678892020-05-21 10:06:58 +02006183 }
6184 }
6185
Michal Vasko230cf972021-12-02 12:31:00 +01006186 /* move result to the set */
6187 lyxp_set_free_content(set);
6188 *set = result;
6189 result.type = LYXP_SET_NUMBER;
6190 assert(!set_sort(set));
6191
Michal Vasko004d3152020-06-11 19:59:22 +02006192cleanup:
Michal Vasko230cf972021-12-02 12:31:00 +01006193 lyxp_set_free_content(&result);
Michal Vasko004d3152020-06-11 19:59:22 +02006194 lyd_free_tree(inst);
6195 return ret;
Michal Vaskod3678892020-05-21 10:06:58 +02006196}
6197
6198/**
Michal Vasko49fec8e2022-05-24 10:28:33 +02006199 * @brief Check @p node as a part of schema NameTest processing.
6200 *
6201 * @param[in] node Schema node to check.
6202 * @param[in] ctx_scnode Context node.
6203 * @param[in] set Set to read general context from.
6204 * @param[in] node_name Node name in the dictionary to move to, NULL for any nodes.
6205 * @param[in] moveto_mod Expected module of the node, NULL for no prefix.
6206 * @return LY_ERR (LY_ENOT if node does not match, LY_EINVAL if neither node nor any children match)
6207 */
6208static LY_ERR
6209moveto_scnode_check(const struct lysc_node *node, const struct lysc_node *ctx_scnode, const struct lyxp_set *set,
6210 const char *node_name, const struct lys_module *moveto_mod)
6211{
6212 if (!moveto_mod && node_name) {
6213 switch (set->format) {
6214 case LY_VALUE_SCHEMA:
6215 case LY_VALUE_SCHEMA_RESOLVED:
6216 /* use current module */
6217 moveto_mod = set->cur_mod;
6218 break;
6219 case LY_VALUE_JSON:
6220 case LY_VALUE_LYB:
6221 case LY_VALUE_STR_NS:
6222 /* inherit module of the context node, if any */
6223 if (ctx_scnode) {
6224 moveto_mod = ctx_scnode->module;
6225 }
6226 break;
6227 case LY_VALUE_CANON:
6228 case LY_VALUE_XML:
6229 /* not defined */
6230 LOGINT(set->ctx);
6231 return LY_EINVAL;
6232 }
6233 }
6234
6235 if (!node) {
6236 /* root will not match a specific node */
6237 if (node_name || moveto_mod) {
6238 return LY_ENOT;
6239 }
6240 return LY_SUCCESS;
6241 }
6242
6243 /* module check */
6244 if (moveto_mod && (node->module != moveto_mod)) {
6245 return LY_ENOT;
6246 }
6247
6248 /* context check */
6249 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (node->flags & LYS_CONFIG_R)) {
6250 return LY_EINVAL;
6251 } else if (set->context_op && (node->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && (node != set->context_op)) {
6252 return LY_EINVAL;
6253 }
6254
6255 /* name check */
6256 if (node_name && (node->name != node_name)) {
6257 return LY_ENOT;
6258 }
6259
6260 /* match */
6261 return LY_SUCCESS;
6262}
6263
6264/**
6265 * @brief Get the next node in a forward schema node DFS.
6266 *
6267 * @param[in] iter Last returned node.
6268 * @param[in] stop Node to stop the search on and not return.
6269 * @param[in] getnext_opts Options for ::lys_getnext().
6270 * @return Next node, NULL if there are no more.
6271 */
6272static const struct lysc_node *
6273moveto_axis_scnode_next_dfs_forward(const struct lysc_node *iter, const struct lysc_node *stop, uint32_t getnext_opts)
6274{
6275 const struct lysc_node *next = NULL;
6276
6277 next = lysc_node_child(iter);
6278 if (!next) {
6279 /* no children, try siblings */
Michal Vasko34a22fe2022-06-15 07:58:55 +02006280 if ((iter == stop) || !lysc_data_parent(iter)) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02006281 /* we are done, no next element to process */
6282 return NULL;
6283 }
6284
6285 next = lys_getnext(iter, lysc_data_parent(iter), NULL, getnext_opts);
6286 }
6287 while (!next && iter) {
6288 /* parent is already processed, go to its sibling */
6289 iter = iter->parent;
Michal Vasko34a22fe2022-06-15 07:58:55 +02006290 if ((iter == stop) || !lysc_data_parent(iter)) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02006291 /* we are done, no next element to process */
6292 return NULL;
6293 }
6294 next = lys_getnext(iter, lysc_data_parent(iter), NULL, getnext_opts);
6295 }
6296
6297 return next;
6298}
6299
6300/**
6301 * @brief Consider schema node based on its in_ctx enum value.
6302 *
6303 * @param[in,out] in_ctx In_ctx enum of the schema node, may be updated.
6304 * @param[in] axis Axis to use.
6305 * @return LY_SUCCESS on success.
6306 * @return LY_ENOT if the node should not be returned.
6307 */
6308static LY_ERR
6309moveto_axis_scnode_next_in_ctx(int32_t *in_ctx, enum lyxp_axis axis)
6310{
6311 switch (axis) {
6312 case LYXP_AXIS_SELF:
6313 if ((*in_ctx == LYXP_SET_SCNODE_START) || (*in_ctx == LYXP_SET_SCNODE_ATOM_CTX)) {
6314 /* additionally put the start node into context */
6315 *in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
6316 return LY_SUCCESS;
6317 }
6318 break;
6319 case LYXP_AXIS_PARENT:
6320 case LYXP_AXIS_ANCESTOR_OR_SELF:
6321 case LYXP_AXIS_ANCESTOR:
6322 case LYXP_AXIS_DESCENDANT_OR_SELF:
6323 case LYXP_AXIS_DESCENDANT:
6324 case LYXP_AXIS_FOLLOWING:
6325 case LYXP_AXIS_FOLLOWING_SIBLING:
6326 case LYXP_AXIS_PRECEDING:
6327 case LYXP_AXIS_PRECEDING_SIBLING:
6328 case LYXP_AXIS_CHILD:
6329 if (*in_ctx == LYXP_SET_SCNODE_START) {
6330 /* remember that context node was used */
6331 *in_ctx = LYXP_SET_SCNODE_START_USED;
6332 return LY_SUCCESS;
6333 } else if (*in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
6334 /* traversed */
6335 *in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
6336 return LY_SUCCESS;
6337 }
6338 break;
6339 case LYXP_AXIS_ATTRIBUTE:
6340 /* unreachable */
6341 assert(0);
6342 LOGINT(NULL);
6343 break;
6344 }
6345
6346 return LY_ENOT;
6347}
6348
6349/**
6350 * @brief Get previous sibling for a schema node.
6351 *
6352 * @param[in] scnode Schema node.
6353 * @param[in] getnext_opts Options for ::lys_getnext().
6354 * @return Previous sibling, NULL if none.
6355 */
6356static const struct lysc_node *
6357moveto_axis_scnode_preceding_sibling(const struct lysc_node *scnode, uint32_t getnext_opts)
6358{
6359 const struct lysc_node *next = NULL, *prev = NULL;
6360
6361 while ((next = lys_getnext(next, lysc_data_parent(scnode), scnode->module->compiled, getnext_opts))) {
6362 if (next == scnode) {
6363 break;
6364 }
6365
6366 prev = next;
6367 }
6368
6369 return prev;
6370}
6371
6372/**
6373 * @brief Get the first schema node on an axis for a context node.
6374 *
6375 * @param[in,out] iter Last returned node, start with NULL, updated to the next node.
6376 * @param[in,out] iter_type Node type of @p iter, start with 0, updated to the node type of the next node.
6377 * @param[in,out] iter_mod Internal module iterator, do not change.
6378 * @param[in,out] iter_mod_idx Internal module index iterator, do not change.
6379 * @param[in] scnode Context node.
6380 * @param[in] node_type Type of @p scnode.
6381 * @param[in] in_ctx In_ctx enum of @p scnode.
6382 * @param[in] axis Axis to use.
6383 * @param[in] set XPath set with the general context.
6384 * @param[in] getnext_opts Options for ::lys_getnext().
6385 * @return LY_SUCCESS on success.
6386 * @return LY_ENOTFOUND if no next node found.
6387 */
6388static LY_ERR
6389moveto_axis_scnode_next_first(const struct lysc_node **iter, enum lyxp_node_type *iter_type, const struct lys_module **iter_mod,
6390 uint32_t *iter_mod_idx, const struct lysc_node *scnode, enum lyxp_node_type node_type, enum lyxp_axis axis,
6391 struct lyxp_set *set, uint32_t getnext_opts)
6392{
6393 const struct lysc_node *next = NULL;
6394 enum lyxp_node_type next_type = 0;
6395
6396 assert(!*iter);
6397 assert(!*iter_type);
6398
6399 *iter_mod = NULL;
6400 *iter_mod_idx = 0;
6401
6402 switch (axis) {
6403 case LYXP_AXIS_ANCESTOR_OR_SELF:
6404 case LYXP_AXIS_DESCENDANT_OR_SELF:
6405 case LYXP_AXIS_SELF:
6406 if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT) || (node_type == LYXP_NODE_ELEM)) {
6407 /* just return the node */
6408 next = scnode;
6409 next_type = node_type;
6410 }
6411 break;
6412
6413 case LYXP_AXIS_ANCESTOR:
6414 case LYXP_AXIS_PARENT:
6415 if (node_type == LYXP_NODE_ELEM) {
6416 next = lysc_data_parent(scnode);
6417 next_type = next ? LYXP_NODE_ELEM : set->root_type;
6418 } /* else no parent */
6419 break;
6420
6421 case LYXP_AXIS_DESCENDANT:
6422 case LYXP_AXIS_CHILD:
6423 if ((node_type == LYXP_NODE_ROOT_CONFIG) || (node_type == LYXP_NODE_ROOT)) {
6424 /* it can actually be in any module, it's all <running>, and even if it's moveto_mod (if set),
6425 * it can be in a top-level augment */
6426 while ((*iter_mod = ly_ctx_get_module_iter(set->ctx, iter_mod_idx))) {
6427 /* module may not be implemented or not compiled yet */
6428 if (!(*iter_mod)->compiled) {
6429 continue;
6430 }
6431
6432 /* get next node */
6433 if ((next = lys_getnext(NULL, NULL, (*iter_mod)->compiled, getnext_opts))) {
6434 next_type = LYXP_NODE_ELEM;
6435 break;
6436 }
6437 }
6438 } else if (node_type == LYXP_NODE_ELEM) {
6439 /* get next node */
6440 next = lys_getnext(NULL, scnode, NULL, getnext_opts);
6441 next_type = next ? LYXP_NODE_ELEM : 0;
6442 }
6443 break;
6444
6445 case LYXP_AXIS_FOLLOWING:
6446 case LYXP_AXIS_FOLLOWING_SIBLING:
6447 if (node_type == LYXP_NODE_ELEM) {
6448 /* first next sibling */
6449 next = lys_getnext(scnode, lysc_data_parent(scnode), scnode->module->compiled, getnext_opts);
6450 next_type = next ? LYXP_NODE_ELEM : 0;
6451 } /* else no sibling */
6452 break;
6453
6454 case LYXP_AXIS_PRECEDING:
6455 case LYXP_AXIS_PRECEDING_SIBLING:
6456 if (node_type == LYXP_NODE_ELEM) {
6457 /* first parent sibling */
6458 next = lys_getnext(NULL, lysc_data_parent(scnode), scnode->module->compiled, getnext_opts);
6459 if (next == scnode) {
6460 /* no preceding sibling */
6461 next = NULL;
6462 }
6463 next_type = next ? LYXP_NODE_ELEM : 0;
6464 } /* else no sibling */
6465 break;
6466
6467 case LYXP_AXIS_ATTRIBUTE:
6468 /* unreachable */
6469 assert(0);
6470 LOGINT(set->ctx);
6471 break;
6472 }
6473
6474 *iter = next;
6475 *iter_type = next_type;
6476 return next_type ? LY_SUCCESS : LY_ENOTFOUND;
6477}
6478
6479/**
6480 * @brief Iterate over all schema nodes on an axis for a context node.
6481 *
6482 * @param[in,out] iter Last returned node, start with NULL, updated to the next node.
6483 * @param[in,out] iter_type Node type of @p iter, start with 0, updated to the node type of the next node.
6484 * @param[in,out] iter_mod Internal module iterator, do not change.
6485 * @param[in,out] iter_mod_idx Internal module index iterator, do not change.
6486 * @param[in] scnode Context node.
6487 * @param[in] node_type Type of @p scnode.
6488 * @param[in] axis Axis to use.
6489 * @param[in] set XPath set with the general context.
6490 * @param[in] getnext_opts Options for ::lys_getnext().
6491 * @return LY_SUCCESS on success.
6492 * @return LY_ENOTFOUND if no next node found.
6493 */
6494static LY_ERR
6495moveto_axis_scnode_next(const struct lysc_node **iter, enum lyxp_node_type *iter_type, const struct lys_module **iter_mod,
6496 uint32_t *iter_mod_idx, const struct lysc_node *scnode, enum lyxp_node_type node_type, enum lyxp_axis axis,
6497 struct lyxp_set *set, uint32_t getnext_opts)
6498{
6499 const struct lysc_node *next = NULL, *dfs_stop;
6500 enum lyxp_node_type next_type = 0;
6501
6502 if (!*iter_type) {
6503 /* first returned node */
6504 return moveto_axis_scnode_next_first(iter, iter_type, iter_mod, iter_mod_idx, scnode, node_type, axis, set,
6505 getnext_opts);
6506 }
6507
6508 switch (axis) {
6509 case LYXP_AXIS_PARENT:
6510 case LYXP_AXIS_SELF:
6511 /* parent/self was returned before */
6512 break;
6513
6514 case LYXP_AXIS_ANCESTOR_OR_SELF:
6515 if ((*iter == scnode) && (*iter_type == node_type)) {
6516 /* fake first ancestor, we returned self before */
6517 *iter = NULL;
6518 *iter_type = 0;
6519 return moveto_axis_scnode_next_first(iter, iter_type, iter_mod, iter_mod_idx, scnode, node_type,
6520 LYXP_AXIS_ANCESTOR, set, getnext_opts);
6521 } /* else continue ancestor */
6522
6523 /* fallthrough */
6524 case LYXP_AXIS_ANCESTOR:
6525 if (*iter_type == LYXP_NODE_ELEM) {
6526 next = lysc_data_parent(*iter);
6527 next_type = next ? LYXP_NODE_ELEM : set->root_type;
6528 } /* else no ancestor */
6529 break;
6530
6531 case LYXP_AXIS_DESCENDANT_OR_SELF:
6532 if ((*iter == scnode) && (*iter_type == node_type)) {
6533 /* fake first descendant, we returned self before */
6534 *iter = NULL;
6535 *iter_type = 0;
6536 return moveto_axis_scnode_next_first(iter, iter_type, iter_mod, iter_mod_idx, scnode, node_type,
6537 LYXP_AXIS_DESCENDANT, set, getnext_opts);
6538 } /* else DFS until context node */
6539 dfs_stop = scnode;
6540
6541 /* fallthrough */
6542 case LYXP_AXIS_DESCENDANT:
6543 if (axis == LYXP_AXIS_DESCENDANT) {
6544 /* DFS until the context node */
6545 dfs_stop = scnode;
6546 }
6547
6548 /* fallthrough */
6549 case LYXP_AXIS_PRECEDING:
6550 if (axis == LYXP_AXIS_PRECEDING) {
6551 /* DFS until the previous sibling */
6552 dfs_stop = moveto_axis_scnode_preceding_sibling(scnode, getnext_opts);
6553 assert(dfs_stop);
6554
6555 if (*iter == dfs_stop) {
6556 /* we are done */
6557 break;
6558 }
6559 }
6560
6561 /* fallthrough */
6562 case LYXP_AXIS_FOLLOWING:
6563 if (axis == LYXP_AXIS_FOLLOWING) {
6564 /* DFS through the whole module */
6565 dfs_stop = NULL;
6566 }
6567
6568 /* nested nodes */
6569 assert(*iter);
6570 next = moveto_axis_scnode_next_dfs_forward(*iter, dfs_stop, getnext_opts);
6571 if (next) {
6572 next_type = LYXP_NODE_ELEM;
6573 break;
6574 } /* else get next top-level node just like a child */
6575
6576 /* fallthrough */
6577 case LYXP_AXIS_CHILD:
6578 case LYXP_AXIS_FOLLOWING_SIBLING:
6579 if (!*iter_mod) {
6580 /* nodes from a single module */
6581 if ((next = lys_getnext(*iter, lysc_data_parent(*iter), (*iter)->module->compiled, getnext_opts))) {
6582 next_type = LYXP_NODE_ELEM;
6583 break;
6584 }
6585
6586 assert(scnode);
Michal Vaskoa353cce2022-11-14 10:09:55 +01006587 if ((axis != LYXP_AXIS_CHILD) && !lysc_data_parent(scnode)) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02006588 /* iterating over top-level nodes, find next */
6589 while (lysc_data_parent(*iter)) {
6590 *iter = lysc_data_parent(*iter);
6591 }
6592 if ((next = lys_getnext(*iter, NULL, (*iter)->module->compiled, getnext_opts))) {
6593 next_type = LYXP_NODE_ELEM;
6594 break;
6595 }
6596 }
6597 }
6598
6599 while (*iter_mod) {
6600 /* module top-level nodes */
6601 if ((next = lys_getnext(*iter, NULL, (*iter_mod)->compiled, getnext_opts))) {
6602 next_type = LYXP_NODE_ELEM;
6603 break;
6604 }
6605
6606 /* get next module */
6607 while ((*iter_mod = ly_ctx_get_module_iter(set->ctx, iter_mod_idx))) {
6608 /* module may not be implemented or not compiled yet */
6609 if ((*iter_mod)->compiled) {
6610 break;
6611 }
6612 }
6613
6614 /* new module, start over */
6615 *iter = NULL;
6616 }
6617 break;
6618
6619 case LYXP_AXIS_PRECEDING_SIBLING:
6620 assert(*iter);
6621
6622 /* next parent sibling until scnode */
6623 next = lys_getnext(*iter, lysc_data_parent(*iter), (*iter)->module->compiled, getnext_opts);
6624 if (next == scnode) {
6625 /* no previous sibling */
6626 next = NULL;
6627 }
6628 next_type = next ? LYXP_NODE_ELEM : 0;
6629 break;
6630
6631 case LYXP_AXIS_ATTRIBUTE:
6632 /* unreachable */
6633 assert(0);
6634 LOGINT(set->ctx);
6635 break;
6636 }
6637
6638 *iter = next;
6639 *iter_type = next_type;
6640 return next_type ? LY_SUCCESS : LY_ENOTFOUND;
6641}
6642
6643/**
Michal Vaskod3678892020-05-21 10:06:58 +02006644 * @brief Move context @p set to a schema node. Result is LYXP_SET_SCNODE_SET (or LYXP_SET_EMPTY).
6645 *
6646 * @param[in,out] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006647 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02006648 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko49fec8e2022-05-24 10:28:33 +02006649 * @param[in] axis Axis to search on.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006650 * @param[in] options XPath options.
6651 * @return LY_ERR
6652 */
6653static LY_ERR
Michal Vasko49fec8e2022-05-24 10:28:33 +02006654moveto_scnode(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, enum lyxp_axis axis,
6655 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006656{
Radek Krejci857189e2020-09-01 13:26:36 +02006657 ly_bool temp_ctx = 0;
Michal Vasko49fec8e2022-05-24 10:28:33 +02006658 uint32_t getnext_opts, orig_used, i, mod_idx, idx;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006659 const struct lys_module *mod;
Michal Vasko49fec8e2022-05-24 10:28:33 +02006660 const struct lysc_node *iter;
6661 enum lyxp_node_type iter_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006662
aPiecek8b0cc152021-05-31 16:40:31 +02006663 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006664 return LY_SUCCESS;
6665 }
6666
6667 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006668 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006669 return LY_EVALID;
6670 }
6671
Michal Vaskocafad9d2019-11-07 15:20:03 +01006672 /* getnext opts */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01006673 getnext_opts = 0;
Michal Vaskocafad9d2019-11-07 15:20:03 +01006674 if (options & LYXP_SCNODE_OUTPUT) {
6675 getnext_opts |= LYS_GETNEXT_OUTPUT;
6676 }
6677
Michal Vasko03ff5a72019-09-11 13:49:33 +02006678 orig_used = set->used;
6679 for (i = 0; i < orig_used; ++i) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02006680 /* update in_ctx first */
6681 if (moveto_axis_scnode_next_in_ctx(&set->val.scnodes[i].in_ctx, axis)) {
6682 /* not usable, skip */
6683 continue;
6684 }
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006685
Michal Vasko49fec8e2022-05-24 10:28:33 +02006686 iter = NULL;
6687 iter_type = 0;
6688 while (!moveto_axis_scnode_next(&iter, &iter_type, &mod, &mod_idx, set->val.scnodes[i].scnode,
6689 set->val.scnodes[i].type, axis, set, getnext_opts)) {
6690 if (moveto_scnode_check(iter, NULL, set, ncname, moveto_mod)) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006691 continue;
6692 }
6693
Michal Vasko49fec8e2022-05-24 10:28:33 +02006694 /* insert */
Michal Vasko7333cb32022-07-29 16:30:29 +02006695 LY_CHECK_RET(set_scnode_insert_node(set, iter, iter_type, axis, &idx));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006696
Michal Vasko49fec8e2022-05-24 10:28:33 +02006697 /* we need to prevent these nodes from being considered in this moveto */
6698 if ((idx < orig_used) && (idx > i)) {
6699 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX;
6700 temp_ctx = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006701 }
6702 }
Michal Vasko66330fc2022-11-21 15:52:24 +01006703
6704 if (moveto_mod && ncname && ((axis == LYXP_AXIS_DESCENDANT) || (axis == LYXP_AXIS_CHILD)) &&
6705 (set->val.scnodes[i].type == LYXP_NODE_ELEM) && !ly_nested_ext_schema(NULL, set->val.scnodes[i].scnode,
6706 moveto_mod->name, strlen(moveto_mod->name), LY_VALUE_JSON, NULL, ncname, strlen(ncname), &iter, NULL)) {
6707 /* there is a matching node from an extension, use it */
6708 LY_CHECK_RET(set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, axis, &idx));
6709 if ((idx < orig_used) && (idx > i)) {
6710 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX;
6711 temp_ctx = 1;
6712 }
6713 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006714 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006715
6716 /* correct temporary in_ctx values */
6717 if (temp_ctx) {
6718 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006719 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_NEW_CTX) {
6720 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006721 }
6722 }
6723 }
6724
6725 return LY_SUCCESS;
6726}
6727
6728/**
Michal Vasko49fec8e2022-05-24 10:28:33 +02006729 * @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 +02006730 * Context position aware.
6731 *
6732 * @param[in] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006733 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02006734 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01006735 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006736 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6737 */
6738static LY_ERR
Michal Vasko49fec8e2022-05-24 10:28:33 +02006739moveto_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 +02006740{
6741 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006742 const struct lyd_node *next, *elem, *start;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006743 struct lyxp_set ret_set;
6744 LY_ERR rc;
6745
aPiecek8b0cc152021-05-31 16:40:31 +02006746 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006747 return LY_SUCCESS;
6748 }
6749
6750 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006751 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006752 return LY_EVALID;
6753 }
6754
Michal Vasko9f96a052020-03-10 09:41:45 +01006755 /* 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 +02006756 rc = xpath_pi_node(set, LYXP_AXIS_CHILD, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006757 LY_CHECK_RET(rc);
6758
Michal Vasko6346ece2019-09-24 13:12:53 +02006759 /* this loop traverses all the nodes in the set and adds/keeps only those that match qname */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006760 set_init(&ret_set, set);
6761 for (i = 0; i < set->used; ++i) {
6762
6763 /* TREE DFS */
6764 start = set->val.nodes[i].node;
6765 for (elem = next = start; elem; elem = next) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02006766 rc = moveto_node_check(elem, LYXP_NODE_ELEM, set, ncname, moveto_mod, options);
Michal Vasko6346ece2019-09-24 13:12:53 +02006767 if (!rc) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006768 /* add matching node into result set */
6769 set_insert_node(&ret_set, elem, 0, LYXP_NODE_ELEM, ret_set.used);
6770 if (set_dup_node_check(set, elem, LYXP_NODE_ELEM, i)) {
6771 /* the node is a duplicate, we'll process it later in the set */
6772 goto skip_children;
6773 }
Michal Vasko6346ece2019-09-24 13:12:53 +02006774 } else if (rc == LY_EINCOMPLETE) {
Michal Vasko6346ece2019-09-24 13:12:53 +02006775 return rc;
6776 } else if (rc == LY_EINVAL) {
6777 goto skip_children;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006778 }
6779
6780 /* TREE DFS NEXT ELEM */
6781 /* select element for the next run - children first */
Radek Krejcia1c1e542020-09-29 16:06:52 +02006782 next = lyd_child(elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006783 if (!next) {
6784skip_children:
6785 /* no children, so try siblings, but only if it's not the start,
6786 * that is considered to be the root and it's siblings are not traversed */
6787 if (elem != start) {
6788 next = elem->next;
6789 } else {
6790 break;
6791 }
6792 }
6793 while (!next) {
6794 /* no siblings, go back through the parents */
Michal Vasko9e685082021-01-29 14:49:09 +01006795 if (lyd_parent(elem) == start) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006796 /* we are done, no next element to process */
6797 break;
6798 }
6799 /* parent is already processed, go to its sibling */
Michal Vasko9e685082021-01-29 14:49:09 +01006800 elem = lyd_parent(elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006801 next = elem->next;
6802 }
6803 }
6804 }
6805
6806 /* make the temporary set the current one */
6807 ret_set.ctx_pos = set->ctx_pos;
6808 ret_set.ctx_size = set->ctx_size;
Michal Vaskod3678892020-05-21 10:06:58 +02006809 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006810 memcpy(set, &ret_set, sizeof *set);
Michal Vasko306e2832022-07-25 09:15:17 +02006811 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006812
6813 return LY_SUCCESS;
6814}
6815
6816/**
Michal Vasko49fec8e2022-05-24 10:28:33 +02006817 * @brief Move context @p set to a child schema node and all its descendants starting from a node.
6818 * Result is LYXP_SET_NODE_SET.
6819 *
6820 * @param[in] set Set to use.
6821 * @param[in] start Start node whose subtree to add.
6822 * @param[in] start_idx Index of @p start in @p set.
6823 * @param[in] moveto_mod Matching node module, NULL for no prefix.
6824 * @param[in] ncname Matching node name in the dictionary, NULL for any.
6825 * @param[in] options XPath options.
6826 * @return LY_ERR value.
6827 */
6828static LY_ERR
6829moveto_scnode_dfs(struct lyxp_set *set, const struct lysc_node *start, uint32_t start_idx,
6830 const struct lys_module *moveto_mod, const char *ncname, uint32_t options)
6831{
6832 const struct lysc_node *next, *elem;
6833 uint32_t idx;
6834 LY_ERR rc;
6835
6836 /* TREE DFS */
6837 for (elem = next = start; elem; elem = next) {
6838 if ((elem == start) || (elem->nodetype & (LYS_CHOICE | LYS_CASE))) {
6839 /* schema-only nodes, skip root */
6840 goto next_iter;
6841 }
6842
6843 rc = moveto_scnode_check(elem, start, set, ncname, moveto_mod);
6844 if (!rc) {
6845 if (lyxp_set_scnode_contains(set, elem, LYXP_NODE_ELEM, start_idx, &idx)) {
6846 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
6847 if (idx > start_idx) {
6848 /* we will process it later in the set */
6849 goto skip_children;
6850 }
6851 } else {
Michal Vasko7333cb32022-07-29 16:30:29 +02006852 LY_CHECK_RET(set_scnode_insert_node(set, elem, LYXP_NODE_ELEM, LYXP_AXIS_DESCENDANT, NULL));
Michal Vasko49fec8e2022-05-24 10:28:33 +02006853 }
6854 } else if (rc == LY_EINVAL) {
6855 goto skip_children;
6856 }
6857
6858next_iter:
6859 /* TREE DFS NEXT ELEM */
6860 /* select element for the next run - children first */
6861 next = lysc_node_child(elem);
6862 if (next && (next->nodetype == LYS_INPUT) && (options & LYXP_SCNODE_OUTPUT)) {
6863 next = next->next;
6864 } else if (next && (next->nodetype == LYS_OUTPUT) && !(options & LYXP_SCNODE_OUTPUT)) {
6865 next = next->next;
6866 }
6867 if (!next) {
6868skip_children:
6869 /* no children, so try siblings, but only if it's not the start,
6870 * that is considered to be the root and it's siblings are not traversed */
6871 if (elem != start) {
6872 next = elem->next;
6873 } else {
6874 break;
6875 }
6876 }
6877 while (!next) {
6878 /* no siblings, go back through the parents */
6879 if (elem->parent == start) {
6880 /* we are done, no next element to process */
6881 break;
6882 }
6883 /* parent is already processed, go to its sibling */
6884 elem = elem->parent;
6885 next = elem->next;
6886 }
6887 }
6888
6889 return LY_SUCCESS;
6890}
6891
6892/**
6893 * @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 +02006894 *
6895 * @param[in] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006896 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02006897 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006898 * @param[in] options XPath options.
Michal Vasko49fec8e2022-05-24 10:28:33 +02006899 * @return LY_ERR value.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006900 */
6901static LY_ERR
Michal Vasko49fec8e2022-05-24 10:28:33 +02006902moveto_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 +02006903{
Michal Vasko49fec8e2022-05-24 10:28:33 +02006904 uint32_t i, orig_used, mod_idx;
6905 const struct lys_module *mod;
6906 const struct lysc_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006907
aPiecek8b0cc152021-05-31 16:40:31 +02006908 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006909 return LY_SUCCESS;
6910 }
6911
6912 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006913 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006914 return LY_EVALID;
6915 }
6916
Michal Vasko03ff5a72019-09-11 13:49:33 +02006917 orig_used = set->used;
6918 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006919 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
6920 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006921 continue;
6922 }
6923
6924 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01006925 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vaskoec4df482019-12-16 10:02:18 +01006926 } else {
Michal Vasko1a09b212021-05-06 13:00:10 +02006927 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006928 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006929
Michal Vasko49fec8e2022-05-24 10:28:33 +02006930 if ((set->val.scnodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.scnodes[i].type == LYXP_NODE_ROOT)) {
6931 /* traverse all top-level nodes in all the modules */
6932 mod_idx = 0;
6933 while ((mod = ly_ctx_get_module_iter(set->ctx, &mod_idx))) {
6934 /* module may not be implemented or not compiled yet */
6935 if (!mod->compiled) {
6936 continue;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006937 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006938
Michal Vasko49fec8e2022-05-24 10:28:33 +02006939 root = NULL;
6940 /* no getnext opts needed */
6941 while ((root = lys_getnext(root, NULL, mod->compiled, 0))) {
6942 LY_CHECK_RET(moveto_scnode_dfs(set, root, i, moveto_mod, ncname, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006943 }
6944 }
Michal Vasko49fec8e2022-05-24 10:28:33 +02006945
6946 } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
6947 /* add all the descendants recursively */
6948 LY_CHECK_RET(moveto_scnode_dfs(set, set->val.scnodes[i].scnode, i, moveto_mod, ncname, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006949 }
6950 }
6951
6952 return LY_SUCCESS;
6953}
6954
6955/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02006956 * @brief Move context @p set to an attribute. Result is LYXP_SET_NODE_SET.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006957 * Indirectly context position aware.
6958 *
6959 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02006960 * @param[in] mod Matching metadata module, NULL for any.
6961 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
aPiecek8b0cc152021-05-31 16:40:31 +02006962 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006963 * @return LY_ERR
6964 */
6965static LY_ERR
aPiecek8b0cc152021-05-31 16:40:31 +02006966moveto_attr(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006967{
Michal Vasko9f96a052020-03-10 09:41:45 +01006968 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006969
aPiecek8b0cc152021-05-31 16:40:31 +02006970 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006971 return LY_SUCCESS;
6972 }
6973
6974 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006975 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006976 return LY_EVALID;
6977 }
6978
Radek Krejci1deb5be2020-08-26 16:43:36 +02006979 for (uint32_t i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02006980 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006981
6982 /* only attributes of an elem (not dummy) can be in the result, skip all the rest;
6983 * our attributes are always qualified */
Michal Vasko5c4e5892019-11-14 12:31:38 +01006984 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01006985 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006986
6987 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02006988 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006989 continue;
6990 }
6991
Michal Vaskod3678892020-05-21 10:06:58 +02006992 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006993 /* match */
6994 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01006995 set->val.meta[i].meta = sub;
6996 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006997 /* pos does not change */
6998 replaced = 1;
6999 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01007000 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 +02007001 }
7002 ++i;
7003 }
7004 }
7005 }
7006
7007 if (!replaced) {
7008 /* no match */
7009 set_remove_node(set, i);
7010 }
7011 }
7012
7013 return LY_SUCCESS;
7014}
7015
7016/**
7017 * @brief Move context @p set1 to union with @p set2. @p set2 is emptied afterwards.
Michal Vasko61ac2f62020-05-25 12:39:51 +02007018 * Result is LYXP_SET_NODE_SET. Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007019 *
7020 * @param[in,out] set1 Set to use for the result.
7021 * @param[in] set2 Set that is copied to @p set1.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007022 * @return LY_ERR
7023 */
7024static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007025moveto_union(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007026{
7027 LY_ERR rc;
7028
Michal Vaskod3678892020-05-21 10:06:58 +02007029 if ((set1->type != LYXP_SET_NODE_SET) || (set2->type != LYXP_SET_NODE_SET)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01007030 LOGVAL(set1->ctx, LY_VCODE_XP_INOP_2, "union", print_set_type(set1), print_set_type(set2));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007031 return LY_EVALID;
7032 }
7033
7034 /* set2 is empty or both set1 and set2 */
Michal Vaskod3678892020-05-21 10:06:58 +02007035 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007036 return LY_SUCCESS;
7037 }
7038
Michal Vaskod3678892020-05-21 10:06:58 +02007039 if (!set1->used) {
aPiecekadc1e4f2021-10-07 11:15:12 +02007040 /* release hidden allocated data (lyxp_set.size) */
7041 lyxp_set_free_content(set1);
7042 /* direct copying of the entire structure */
Michal Vasko03ff5a72019-09-11 13:49:33 +02007043 memcpy(set1, set2, sizeof *set1);
7044 /* dynamic memory belongs to set1 now, do not free */
Michal Vaskod3678892020-05-21 10:06:58 +02007045 memset(set2, 0, sizeof *set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007046 return LY_SUCCESS;
7047 }
7048
7049 /* we assume sets are sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007050 assert(!set_sort(set1) && !set_sort(set2));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007051
7052 /* sort, remove duplicates */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007053 rc = set_sorted_merge(set1, set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007054 LY_CHECK_RET(rc);
7055
7056 /* final set must be sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007057 assert(!set_sort(set1));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007058
7059 return LY_SUCCESS;
7060}
7061
7062/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02007063 * @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 +02007064 * Context position aware.
7065 *
7066 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02007067 * @param[in] mod Matching metadata module, NULL for any.
7068 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01007069 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007070 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7071 */
7072static int
Michal Vaskocdad7122020-11-09 21:04:44 +01007073moveto_attr_alldesc(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007074{
Michal Vasko9f96a052020-03-10 09:41:45 +01007075 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007076 struct lyxp_set *set_all_desc = NULL;
7077 LY_ERR rc;
7078
aPiecek8b0cc152021-05-31 16:40:31 +02007079 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007080 return LY_SUCCESS;
7081 }
7082
7083 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01007084 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007085 return LY_EVALID;
7086 }
7087
Michal Vasko03ff5a72019-09-11 13:49:33 +02007088 /* can be optimized similarly to moveto_node_alldesc() and save considerable amount of memory,
7089 * but it likely won't be used much, so it's a waste of time */
7090 /* copy the context */
7091 set_all_desc = set_copy(set);
7092 /* get all descendant nodes (the original context nodes are removed) */
Michal Vasko49fec8e2022-05-24 10:28:33 +02007093 rc = moveto_node_alldesc_child(set_all_desc, NULL, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007094 if (rc != LY_SUCCESS) {
7095 lyxp_set_free(set_all_desc);
7096 return rc;
7097 }
7098 /* prepend the original context nodes */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007099 rc = moveto_union(set, set_all_desc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007100 if (rc != LY_SUCCESS) {
7101 lyxp_set_free(set_all_desc);
7102 return rc;
7103 }
7104 lyxp_set_free(set_all_desc);
7105
Radek Krejci1deb5be2020-08-26 16:43:36 +02007106 for (uint32_t i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02007107 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007108
7109 /* only attributes of an elem can be in the result, skip all the rest,
7110 * we have all attributes qualified in lyd tree */
7111 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01007112 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007113 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02007114 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007115 continue;
7116 }
7117
Michal Vaskod3678892020-05-21 10:06:58 +02007118 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007119 /* match */
7120 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01007121 set->val.meta[i].meta = sub;
7122 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007123 /* pos does not change */
7124 replaced = 1;
7125 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01007126 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 +02007127 }
7128 ++i;
7129 }
7130 }
7131 }
7132
7133 if (!replaced) {
7134 /* no match */
7135 set_remove_node(set, i);
7136 }
7137 }
7138
7139 return LY_SUCCESS;
7140}
7141
7142/**
Michal Vasko8abcecc2022-07-28 09:55:01 +02007143 * @brief Move context @p set1 single item to the result of a comparison.
7144 *
7145 * @param[in] set1 First set with the item to compare.
7146 * @param[in] idx1 Index of the item in @p set1.
7147 * @param[in] set2 Second set.
7148 * @param[in] op Comparison operator to process.
7149 * @param[in] switch_operands Whether to switch sets as operands; whether it is `set1 op set2` or `set2 op set1`.
7150 * @param[out] result Result of the comparison.
7151 * @return LY_ERR value.
7152 */
7153static LY_ERR
7154moveto_op_comp_item(const struct lyxp_set *set1, uint32_t idx1, struct lyxp_set *set2, const char *op,
7155 ly_bool switch_operands, ly_bool *result)
7156{
7157 struct lyxp_set tmp1 = {0};
7158 LY_ERR rc = LY_SUCCESS;
7159
7160 assert(set1->type == LYXP_SET_NODE_SET);
7161
7162 /* cast set1 */
7163 switch (set2->type) {
7164 case LYXP_SET_NUMBER:
7165 rc = set_comp_cast(&tmp1, set1, LYXP_SET_NUMBER, idx1);
7166 break;
7167 case LYXP_SET_BOOLEAN:
7168 rc = set_comp_cast(&tmp1, set1, LYXP_SET_BOOLEAN, idx1);
7169 break;
7170 default:
7171 rc = set_comp_cast(&tmp1, set1, LYXP_SET_STRING, idx1);
7172 break;
7173 }
7174 LY_CHECK_GOTO(rc, cleanup);
7175
7176 /* canonize set2 */
7177 LY_CHECK_GOTO(rc = set_comp_canonize(set2, &set1->val.nodes[idx1]), cleanup);
7178
7179 /* compare recursively and store the result */
7180 if (switch_operands) {
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007181 LY_CHECK_GOTO(rc = moveto_op_comp(set2, &tmp1, op, result), cleanup);
Michal Vasko8abcecc2022-07-28 09:55:01 +02007182 } else {
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007183 LY_CHECK_GOTO(rc = moveto_op_comp(&tmp1, set2, op, result), cleanup);
Michal Vasko8abcecc2022-07-28 09:55:01 +02007184 }
7185
7186cleanup:
7187 lyxp_set_free_content(&tmp1);
7188 return rc;
7189}
7190
7191/**
7192 * @brief Move context @p set1 to the result of a comparison. Handles '=', '!=', '<=', '<', '>=', or '>'.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007193 * Result is LYXP_SET_BOOLEAN. Indirectly context position aware.
7194 *
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007195 * @param[in] set1 Set acting as the first operand for @p op.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007196 * @param[in] set2 Set acting as the second operand for @p op.
7197 * @param[in] op Comparison operator to process.
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007198 * @param[out] result Result of the comparison.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007199 * @return LY_ERR
7200 */
7201static LY_ERR
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007202moveto_op_comp(struct lyxp_set *set1, struct lyxp_set *set2, const char *op, ly_bool *result)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007203{
7204 /*
7205 * NODE SET + NODE SET = NODE SET + STRING /(1 NODE SET) 2 STRING
7206 * NODE SET + STRING = STRING + STRING /1 STRING (2 STRING)
7207 * NODE SET + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
7208 * NODE SET + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
7209 * STRING + NODE SET = STRING + STRING /(1 STRING) 2 STRING
7210 * NUMBER + NODE SET = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
7211 * BOOLEAN + NODE SET = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
7212 *
7213 * '=' or '!='
7214 * BOOLEAN + BOOLEAN
7215 * BOOLEAN + STRING = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
7216 * BOOLEAN + NUMBER = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
7217 * STRING + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
7218 * NUMBER + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
7219 * NUMBER + NUMBER
7220 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
7221 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
7222 * STRING + STRING
7223 *
7224 * '<=', '<', '>=', '>'
7225 * NUMBER + NUMBER
7226 * BOOLEAN + BOOLEAN = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
7227 * BOOLEAN + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
7228 * BOOLEAN + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
7229 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
7230 * STRING + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
7231 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
7232 * NUMBER + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
7233 * STRING + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
7234 */
Michal Vasko8abcecc2022-07-28 09:55:01 +02007235 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007236 LY_ERR rc;
7237
Michal Vasko03ff5a72019-09-11 13:49:33 +02007238 /* iterative evaluation with node-sets */
7239 if ((set1->type == LYXP_SET_NODE_SET) || (set2->type == LYXP_SET_NODE_SET)) {
7240 if (set1->type == LYXP_SET_NODE_SET) {
7241 for (i = 0; i < set1->used; ++i) {
Michal Vasko8abcecc2022-07-28 09:55:01 +02007242 /* evaluate for the single item */
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007243 LY_CHECK_RET(moveto_op_comp_item(set1, i, set2, op, 0, result));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007244
7245 /* lazy evaluation until true */
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007246 if (*result) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007247 return LY_SUCCESS;
7248 }
7249 }
7250 } else {
7251 for (i = 0; i < set2->used; ++i) {
Michal Vasko8abcecc2022-07-28 09:55:01 +02007252 /* evaluate for the single item */
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007253 LY_CHECK_RET(moveto_op_comp_item(set2, i, set1, op, 1, result));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007254
7255 /* lazy evaluation until true */
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007256 if (*result) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007257 return LY_SUCCESS;
7258 }
7259 }
7260 }
7261
Michal Vasko8abcecc2022-07-28 09:55:01 +02007262 /* false for all the nodes */
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007263 *result = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007264 return LY_SUCCESS;
7265 }
7266
7267 /* first convert properly */
7268 if ((op[0] == '=') || (op[0] == '!')) {
7269 if ((set1->type == LYXP_SET_BOOLEAN) || (set2->type == LYXP_SET_BOOLEAN)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007270 lyxp_set_cast(set1, LYXP_SET_BOOLEAN);
7271 lyxp_set_cast(set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007272 } else if ((set1->type == LYXP_SET_NUMBER) || (set2->type == LYXP_SET_NUMBER)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007273 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007274 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007275 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007276 LY_CHECK_RET(rc);
7277 } /* else we have 2 strings */
7278 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007279 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007280 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007281 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007282 LY_CHECK_RET(rc);
7283 }
7284
7285 assert(set1->type == set2->type);
7286
7287 /* compute result */
7288 if (op[0] == '=') {
7289 if (set1->type == LYXP_SET_BOOLEAN) {
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007290 *result = (set1->val.bln == set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007291 } else if (set1->type == LYXP_SET_NUMBER) {
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007292 *result = (set1->val.num == set2->val.num);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007293 } else {
7294 assert(set1->type == LYXP_SET_STRING);
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007295 *result = strcmp(set1->val.str, set2->val.str) ? 0 : 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007296 }
7297 } else if (op[0] == '!') {
7298 if (set1->type == LYXP_SET_BOOLEAN) {
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007299 *result = (set1->val.bln != set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007300 } else if (set1->type == LYXP_SET_NUMBER) {
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007301 *result = (set1->val.num != set2->val.num);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007302 } else {
7303 assert(set1->type == LYXP_SET_STRING);
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007304 *result = strcmp(set1->val.str, set2->val.str) ? 1 : 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007305 }
7306 } else {
7307 assert(set1->type == LYXP_SET_NUMBER);
7308 if (op[0] == '<') {
7309 if (op[1] == '=') {
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007310 *result = (set1->val.num <= set2->val.num);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007311 } else {
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007312 *result = (set1->val.num < set2->val.num);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007313 }
7314 } else {
7315 if (op[1] == '=') {
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007316 *result = (set1->val.num >= set2->val.num);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007317 } else {
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02007318 *result = (set1->val.num > set2->val.num);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007319 }
7320 }
7321 }
7322
Michal Vasko03ff5a72019-09-11 13:49:33 +02007323 return LY_SUCCESS;
7324}
7325
7326/**
7327 * @brief Move context @p set to the result of a basic operation. Handles '+', '-', unary '-', '*', 'div',
7328 * or 'mod'. Result is LYXP_SET_NUMBER. Indirectly context position aware.
7329 *
7330 * @param[in,out] set1 Set to use for the result.
7331 * @param[in] set2 Set acting as the second operand for @p op.
7332 * @param[in] op Operator to process.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007333 * @return LY_ERR
7334 */
7335static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007336moveto_op_math(struct lyxp_set *set1, struct lyxp_set *set2, const char *op)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007337{
7338 LY_ERR rc;
7339
7340 /* unary '-' */
7341 if (!set2 && (op[0] == '-')) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007342 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007343 LY_CHECK_RET(rc);
7344 set1->val.num *= -1;
7345 lyxp_set_free(set2);
7346 return LY_SUCCESS;
7347 }
7348
7349 assert(set1 && set2);
7350
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007351 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007352 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007353 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007354 LY_CHECK_RET(rc);
7355
7356 switch (op[0]) {
7357 /* '+' */
7358 case '+':
7359 set1->val.num += set2->val.num;
7360 break;
7361
7362 /* '-' */
7363 case '-':
7364 set1->val.num -= set2->val.num;
7365 break;
7366
7367 /* '*' */
7368 case '*':
7369 set1->val.num *= set2->val.num;
7370 break;
7371
7372 /* 'div' */
7373 case 'd':
7374 set1->val.num /= set2->val.num;
7375 break;
7376
7377 /* 'mod' */
7378 case 'm':
7379 set1->val.num = ((long long)set1->val.num) % ((long long)set2->val.num);
7380 break;
7381
7382 default:
7383 LOGINT_RET(set1->ctx);
7384 }
7385
7386 return LY_SUCCESS;
7387}
7388
Michal Vasko03ff5a72019-09-11 13:49:33 +02007389/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02007390 * @brief Evaluate Predicate. Logs directly on error.
7391 *
Michal Vaskod3678892020-05-21 10:06:58 +02007392 * [9] Predicate ::= '[' Expr ']'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007393 *
7394 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007395 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02007396 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007397 * @param[in] options XPath options.
Michal Vasko49fec8e2022-05-24 10:28:33 +02007398 * @param[in] axis Axis to search on.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007399 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7400 */
7401static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02007402eval_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 +02007403{
7404 LY_ERR rc;
Michal Vaskodd528af2022-08-08 14:35:07 +02007405 uint32_t i, orig_exp, orig_pos, orig_size;
Michal Vasko5c4e5892019-11-14 12:31:38 +01007406 int32_t pred_in_ctx;
Michal Vasko88a9e802022-05-24 10:50:28 +02007407 ly_bool reverse_axis = 0;
aPiecekfff4dca2021-10-07 10:59:53 +02007408 struct lyxp_set set2 = {0};
Michal Vasko03ff5a72019-09-11 13:49:33 +02007409
7410 /* '[' */
aPiecek8b0cc152021-05-31 16:40:31 +02007411 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02007412 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007413 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007414
aPiecek8b0cc152021-05-31 16:40:31 +02007415 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007416only_parse:
aPiecek8b0cc152021-05-31 16:40:31 +02007417 rc = eval_expr_select(exp, tok_idx, 0, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007418 LY_CHECK_RET(rc);
7419 } else if (set->type == LYXP_SET_NODE_SET) {
7420 /* 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 +01007421 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007422
7423 /* empty set, nothing to evaluate */
7424 if (!set->used) {
7425 goto only_parse;
7426 }
7427
Michal Vasko49fec8e2022-05-24 10:28:33 +02007428 /* decide forward or reverse axis */
7429 switch (axis) {
7430 case LYXP_AXIS_ANCESTOR:
7431 case LYXP_AXIS_ANCESTOR_OR_SELF:
7432 case LYXP_AXIS_PRECEDING:
7433 case LYXP_AXIS_PRECEDING_SIBLING:
7434 reverse_axis = 1;
7435 break;
7436 case LYXP_AXIS_DESCENDANT:
7437 case LYXP_AXIS_DESCENDANT_OR_SELF:
7438 case LYXP_AXIS_FOLLOWING:
7439 case LYXP_AXIS_FOLLOWING_SIBLING:
7440 case LYXP_AXIS_PARENT:
7441 case LYXP_AXIS_CHILD:
7442 case LYXP_AXIS_SELF:
7443 case LYXP_AXIS_ATTRIBUTE:
7444 reverse_axis = 0;
7445 break;
7446 }
7447
Michal Vasko004d3152020-06-11 19:59:22 +02007448 orig_exp = *tok_idx;
Michal Vasko49fec8e2022-05-24 10:28:33 +02007449 orig_pos = reverse_axis ? set->used + 1 : 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007450 orig_size = set->used;
Michal Vasko39dbf352020-05-21 10:08:59 +02007451 for (i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007452 set_init(&set2, set);
7453 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 +02007454
7455 /* remember the node context position for position() and context size for last() */
7456 orig_pos += reverse_axis ? -1 : 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007457
7458 set2.ctx_pos = orig_pos;
7459 set2.ctx_size = orig_size;
Michal Vasko004d3152020-06-11 19:59:22 +02007460 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007461
Michal Vasko004d3152020-06-11 19:59:22 +02007462 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007463 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02007464 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007465 return rc;
7466 }
7467
Michal Vasko49fec8e2022-05-24 10:28:33 +02007468 /* number is a proximity position */
Michal Vasko03ff5a72019-09-11 13:49:33 +02007469 if (set2.type == LYXP_SET_NUMBER) {
7470 if ((long long)set2.val.num == orig_pos) {
7471 set2.val.num = 1;
7472 } else {
7473 set2.val.num = 0;
7474 }
7475 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007476 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007477
7478 /* predicate satisfied or not? */
Michal Vasko004d3152020-06-11 19:59:22 +02007479 if (!set2.val.bln) {
Michal Vasko2caefc12019-11-14 16:07:56 +01007480 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007481 }
7482 }
Michal Vasko2caefc12019-11-14 16:07:56 +01007483 set_remove_nodes_none(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007484
7485 } else if (set->type == LYXP_SET_SCNODE_SET) {
7486 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01007487 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007488 /* there is a currently-valid node */
7489 break;
7490 }
7491 }
7492 /* empty set, nothing to evaluate */
7493 if (i == set->used) {
7494 goto only_parse;
7495 }
7496
Michal Vasko004d3152020-06-11 19:59:22 +02007497 orig_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007498
Michal Vasko03ff5a72019-09-11 13:49:33 +02007499 /* set special in_ctx to all the valid snodes */
7500 pred_in_ctx = set_scnode_new_in_ctx(set);
7501
7502 /* use the valid snodes one-by-one */
7503 for (i = 0; i < set->used; ++i) {
7504 if (set->val.scnodes[i].in_ctx != pred_in_ctx) {
7505 continue;
7506 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01007507 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007508
Michal Vasko004d3152020-06-11 19:59:22 +02007509 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007510
Michal Vasko004d3152020-06-11 19:59:22 +02007511 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007512 LY_CHECK_RET(rc);
7513
7514 set->val.scnodes[i].in_ctx = pred_in_ctx;
7515 }
7516
7517 /* restore the state as it was before the predicate */
7518 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01007519 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007520 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007521 } else if (set->val.scnodes[i].in_ctx == pred_in_ctx) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01007522 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007523 }
7524 }
7525
7526 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02007527 set2.type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007528 set_fill_set(&set2, set);
7529
Michal Vasko004d3152020-06-11 19:59:22 +02007530 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007531 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02007532 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007533 return rc;
7534 }
7535
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007536 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02007537 if (!set2.val.bln) {
Michal Vaskod3678892020-05-21 10:06:58 +02007538 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007539 }
Michal Vaskod3678892020-05-21 10:06:58 +02007540 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007541 }
7542
7543 /* ']' */
Michal Vasko004d3152020-06-11 19:59:22 +02007544 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_BRACK2);
aPiecek8b0cc152021-05-31 16:40:31 +02007545 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02007546 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007547 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007548
7549 return LY_SUCCESS;
7550}
7551
7552/**
Michal Vaskod3678892020-05-21 10:06:58 +02007553 * @brief Evaluate Literal. Logs directly on error.
7554 *
7555 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007556 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02007557 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7558 */
7559static void
Michal Vaskodd528af2022-08-08 14:35:07 +02007560eval_literal(const struct lyxp_expr *exp, uint32_t *tok_idx, struct lyxp_set *set)
Michal Vaskod3678892020-05-21 10:06:58 +02007561{
7562 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007563 if (exp->tok_len[*tok_idx] == 2) {
Michal Vaskod3678892020-05-21 10:06:58 +02007564 set_fill_string(set, "", 0);
7565 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007566 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 +02007567 }
7568 }
7569 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02007570 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007571 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007572}
7573
7574/**
Michal Vasko3d969ff2022-07-29 15:02:08 +02007575 * @brief Check that a nametest in a predicate matches a key node.
7576 *
7577 * @param[in] nametest Nametest to check.
7578 * @param[in] len Length of @p nametest.
7579 * @param[in] ctx_scnode Found schema node as the context for the predicate.
7580 * @param[in] set Context set.
7581 * @param[in] key Expected key node.
7582 * @return LY_SUCCESS on success,
7583 * @return LY_ENOT if a predicate could not be compiled.
7584 * @return LY_ERR on any error.
7585 */
7586static LY_ERR
7587eval_name_test_try_compile_predicate_key(const char *nametest, uint32_t len, const struct lysc_node *ctx_scnode,
7588 const struct lyxp_set *set, const struct lysc_node *key)
7589{
7590 const struct lys_module *mod;
7591
7592 /* prefix (module) */
7593 LY_CHECK_RET(moveto_resolve_model(&nametest, &len, set, ctx_scnode, &mod));
7594 if (mod != key->module) {
7595 return LY_ENOT;
7596 }
7597
7598 /* node name */
7599 if (ly_strncmp(key->name, nametest, len)) {
7600 return LY_ENOT;
7601 }
7602
7603 return LY_SUCCESS;
7604}
7605
7606/**
7607 * @brief Append a simple predicate for the node.
7608 *
7609 * @param[in] exp Full parsed XPath expression.
7610 * @param[in] tok_idx Predicate start index in @p exp.
7611 * @param[in] end_tok_idx Predicate end index in @p exp.
7612 * @param[in] ctx_scnode Found schema node as the context for the predicate.
7613 * @param[in] set Context set.
7614 * @param[in] pred_node Node with the value referenced in the predicate.
7615 * @param[in,out] pred Predicate to append to.
7616 * @param[in,out] pred_len Length of @p pred, is updated.
7617 * @return LY_SUCCESS on success,
7618 * @return LY_ENOT if a predicate could not be compiled.
7619 * @return LY_ERR on any error.
7620 */
7621static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02007622eval_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 +02007623 const struct lysc_node *ctx_scnode, const struct lyxp_set *set, const struct lysc_node *pred_node, char **pred,
7624 uint32_t *pred_len)
7625{
7626 LY_ERR rc = LY_SUCCESS;
7627 uint32_t i;
7628 const struct lyd_node *siblings;
7629 struct lyd_node *ctx_node;
Michal Vasko5fb92a22022-08-02 09:21:37 +02007630 const struct lysc_node *sparent, *cur_scnode;
Michal Vasko3d969ff2022-07-29 15:02:08 +02007631 struct lyxp_expr *val_exp = NULL;
7632 struct lyxp_set set2 = {0};
7633 char quot;
7634
7635 /* duplicate the value expression */
7636 LY_CHECK_GOTO(rc = lyxp_expr_dup(set->ctx, exp, tok_idx, end_tok_idx, &val_exp), cleanup);
7637
7638 /* get its atoms */
Michal Vasko5fb92a22022-08-02 09:21:37 +02007639 cur_scnode = set->cur_node ? set->cur_node->schema : NULL;
7640 LY_CHECK_GOTO(rc = lyxp_atomize(set->ctx, val_exp, set->cur_mod, set->format, set->prefix_data, cur_scnode,
7641 ctx_scnode, &set2, LYXP_SCNODE), cleanup);
Michal Vasko3d969ff2022-07-29 15:02:08 +02007642
7643 /* check whether we can compile a single predicate (evaluation result value is always the same) */
7644 for (i = 0; i < set2.used; ++i) {
7645 if ((set2.val.scnodes[i].type != LYXP_NODE_ELEM) || (set2.val.scnodes[i].in_ctx < LYXP_SET_SCNODE_ATOM_NODE)) {
7646 /* skip root and context node */
7647 continue;
7648 }
7649
Michal Vasko5fb92a22022-08-02 09:21:37 +02007650 /* 1) context node descendants are traversed - do best-effort detection of the value dependency on the
7651 * context node instance */
7652 if ((set2.val.scnodes[i].axis == LYXP_AXIS_CHILD) && (set2.val.scnodes[i].scnode->parent == ctx_scnode)) {
7653 /* 1.1) context node child was accessed on the child axis, certain dependency */
Michal Vasko3d969ff2022-07-29 15:02:08 +02007654 rc = LY_ENOT;
7655 goto cleanup;
7656 }
Michal Vasko5fb92a22022-08-02 09:21:37 +02007657 if ((set2.val.scnodes[i].axis == LYXP_AXIS_DESCENDANT) || (set2.val.scnodes[i].axis == LYXP_AXIS_DESCENDANT_OR_SELF)) {
7658 for (sparent = set2.val.scnodes[i].scnode->parent; sparent && (sparent != ctx_scnode); sparent = sparent->parent) {}
7659 if (sparent) {
7660 /* 1.2) context node descendant was accessed on the descendant axis, probable dependency */
7661 rc = LY_ENOT;
7662 goto cleanup;
7663 }
7664 }
Michal Vasko3d969ff2022-07-29 15:02:08 +02007665
Michal Vasko5fb92a22022-08-02 09:21:37 +02007666 /* 2) multi-instance nodes (list or leaf-list) are traversed - all the instances need to be considered,
7667 * but the current node can be safely ignored, it is always the same data instance */
7668 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 +02007669 rc = LY_ENOT;
7670 goto cleanup;
7671 }
Michal Vasko3d969ff2022-07-29 15:02:08 +02007672 }
7673
7674 /* get any data instance of the context node, we checked it makes no difference */
7675 siblings = set->val.nodes[0].node ? lyd_child(set->val.nodes[0].node) : set->tree;
7676 LY_CHECK_GOTO(rc = lyd_find_sibling_schema(siblings, ctx_scnode, &ctx_node), cleanup);
7677
7678 /* evaluate the value subexpression with the root context node */
7679 lyxp_set_free_content(&set2);
7680 LY_CHECK_GOTO(rc = lyxp_eval(set->ctx, val_exp, set->cur_mod, set->format, set->prefix_data, set->cur_node,
7681 ctx_node, set->tree, NULL, &set2, 0), cleanup);
7682
7683 /* cast it into a string */
7684 LY_CHECK_GOTO(rc = lyxp_set_cast(&set2, LYXP_SET_STRING), cleanup);
7685
7686 /* append the JSON predicate */
7687 *pred = ly_realloc(*pred, *pred_len + 1 + strlen(pred_node->name) + 2 + strlen(set2.val.str) + 3);
7688 LY_CHECK_ERR_GOTO(!*pred, LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7689 quot = strchr(set2.val.str, '\'') ? '\"' : '\'';
7690 *pred_len += sprintf(*pred + *pred_len, "[%s=%c%s%c]", pred_node->name, quot, set2.val.str, quot);
7691
7692cleanup:
7693 lyxp_expr_free(set->ctx, val_exp);
7694 lyxp_set_free_content(&set2);
7695 return rc;
7696}
7697
7698/**
Michal Vasko004d3152020-06-11 19:59:22 +02007699 * @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 +02007700 *
Michal Vasko004d3152020-06-11 19:59:22 +02007701 * @param[in] exp Full parsed XPath expression.
7702 * @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 +02007703 * @param[in] ctx_scnode Found schema node as the context for the predicate.
7704 * @param[in] set Context set.
Michal Vasko004d3152020-06-11 19:59:22 +02007705 * @param[out] predicates Parsed predicates.
7706 * @param[out] pred_type Type of @p predicates.
7707 * @return LY_SUCCESS on success,
Michal Vasko3d969ff2022-07-29 15:02:08 +02007708 * @return LY_ENOT if a predicate could not be compiled.
Michal Vasko004d3152020-06-11 19:59:22 +02007709 * @return LY_ERR on any error.
Michal Vaskod3678892020-05-21 10:06:58 +02007710 */
7711static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02007712eval_name_test_try_compile_predicates(const struct lyxp_expr *exp, uint32_t *tok_idx, const struct lysc_node *ctx_scnode,
Michal Vasko3d969ff2022-07-29 15:02:08 +02007713 const struct lyxp_set *set, struct ly_path_predicate **predicates, enum ly_path_pred_type *pred_type)
Michal Vaskod3678892020-05-21 10:06:58 +02007714{
Michal Vasko3d969ff2022-07-29 15:02:08 +02007715 LY_ERR rc = LY_SUCCESS;
Michal Vasko3ef7e252022-11-16 08:29:49 +01007716 uint32_t e_idx, val_start_idx, pred_idx = 0, prev_lo, pred_len = 0, nested_pred;
Michal Vaskod3678892020-05-21 10:06:58 +02007717 const struct lysc_node *key;
Michal Vasko3d969ff2022-07-29 15:02:08 +02007718 char *pred = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02007719 struct lyxp_expr *exp2 = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02007720
Michal Vasko3d969ff2022-07-29 15:02:08 +02007721 assert(ctx_scnode->nodetype & (LYS_LIST | LYS_LEAFLIST));
Michal Vaskod3678892020-05-21 10:06:58 +02007722
Michal Vasko3d969ff2022-07-29 15:02:08 +02007723 /* turn logging off */
7724 prev_lo = ly_log_options(0);
7725
7726 if (ctx_scnode->nodetype == LYS_LIST) {
7727 /* check for predicates "[key1=...][key2=...]..." */
7728
Michal Vasko004d3152020-06-11 19:59:22 +02007729 /* get key count */
Michal Vasko3d969ff2022-07-29 15:02:08 +02007730 if (ctx_scnode->flags & LYS_KEYLESS) {
7731 rc = LY_ENOT;
7732 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +02007733 }
Michal Vaskod3678892020-05-21 10:06:58 +02007734
Michal Vasko004d3152020-06-11 19:59:22 +02007735 /* learn where the predicates end */
7736 e_idx = *tok_idx;
Michal Vasko3d969ff2022-07-29 15:02:08 +02007737 for (key = lysc_node_child(ctx_scnode); key && (key->flags & LYS_KEY); key = key->next) {
Michal Vasko004d3152020-06-11 19:59:22 +02007738 /* '[' */
7739 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
Michal Vasko3d969ff2022-07-29 15:02:08 +02007740 rc = LY_ENOT;
7741 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +02007742 }
7743 ++e_idx;
7744
Michal Vasko3354d272021-04-06 09:40:06 +02007745 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_NAMETEST)) {
Michal Vasko3d969ff2022-07-29 15:02:08 +02007746 /* not a key */
7747 rc = LY_ENOT;
7748 goto cleanup;
Michal Vasko3354d272021-04-06 09:40:06 +02007749 }
7750
Michal Vasko3d969ff2022-07-29 15:02:08 +02007751 /* check key */
7752 LY_CHECK_GOTO(rc = eval_name_test_try_compile_predicate_key(exp->expr + exp->tok_pos[e_idx],
7753 exp->tok_len[e_idx], ctx_scnode, set, key), cleanup);
7754
7755 ++e_idx;
7756
7757 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_OPER_EQUAL)) {
7758 /* not '=' */
7759 rc = LY_ENOT;
7760 goto cleanup;
7761 }
7762 ++e_idx;
7763
7764 /* value start */
7765 val_start_idx = e_idx;
7766
Michal Vasko004d3152020-06-11 19:59:22 +02007767 /* ']' */
Michal Vasko3ef7e252022-11-16 08:29:49 +01007768 nested_pred = 1;
7769 do {
7770 ++e_idx;
7771
7772 if ((nested_pred == 1) && !lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_OPER_LOG)) {
Michal Vaskob8ee3562022-08-02 10:43:17 +02007773 /* higher priority than '=' */
7774 rc = LY_ENOT;
7775 goto cleanup;
Michal Vasko3ef7e252022-11-16 08:29:49 +01007776 } else if (!lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
7777 /* nested predicate */
7778 ++nested_pred;
7779 } else if (!lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
7780 /* predicate end */
7781 --nested_pred;
Michal Vaskob8ee3562022-08-02 10:43:17 +02007782 }
Michal Vasko3ef7e252022-11-16 08:29:49 +01007783 } while (nested_pred);
Michal Vasko004d3152020-06-11 19:59:22 +02007784
Michal Vasko3d969ff2022-07-29 15:02:08 +02007785 /* try to evaluate the value */
7786 LY_CHECK_GOTO(rc = eval_name_test_try_compile_predicate_append(exp, val_start_idx, e_idx - 1, ctx_scnode,
7787 set, key, &pred, &pred_len), cleanup);
7788
7789 ++e_idx;
Michal Vasko004d3152020-06-11 19:59:22 +02007790 }
Michal Vasko004d3152020-06-11 19:59:22 +02007791 } else {
Michal Vasko3d969ff2022-07-29 15:02:08 +02007792 /* check for predicate "[.=...]" */
7793
Michal Vasko004d3152020-06-11 19:59:22 +02007794 /* learn just where this single predicate ends */
7795 e_idx = *tok_idx;
7796
Michal Vaskod3678892020-05-21 10:06:58 +02007797 /* '[' */
Michal Vasko004d3152020-06-11 19:59:22 +02007798 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
Michal Vasko3d969ff2022-07-29 15:02:08 +02007799 rc = LY_ENOT;
7800 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +02007801 }
7802 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02007803
Michal Vasko3354d272021-04-06 09:40:06 +02007804 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_DOT)) {
Michal Vasko3d969ff2022-07-29 15:02:08 +02007805 /* not the node value */
7806 rc = LY_ENOT;
7807 goto cleanup;
Michal Vasko3354d272021-04-06 09:40:06 +02007808 }
Michal Vasko3d969ff2022-07-29 15:02:08 +02007809 ++e_idx;
7810
7811 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_OPER_EQUAL)) {
7812 /* not '=' */
7813 rc = LY_ENOT;
7814 goto cleanup;
7815 }
7816 ++e_idx;
7817
7818 /* value start */
7819 val_start_idx = e_idx;
Michal Vasko3354d272021-04-06 09:40:06 +02007820
Michal Vaskod3678892020-05-21 10:06:58 +02007821 /* ']' */
Michal Vasko31f19632022-11-16 09:38:40 +01007822 nested_pred = 1;
7823 do {
7824 ++e_idx;
7825
7826 if ((nested_pred == 1) && !lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_OPER_LOG)) {
Michal Vaskob8ee3562022-08-02 10:43:17 +02007827 /* higher priority than '=' */
7828 rc = LY_ENOT;
7829 goto cleanup;
Michal Vasko31f19632022-11-16 09:38:40 +01007830 } else if (!lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
7831 /* nested predicate */
7832 ++nested_pred;
7833 } else if (!lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
7834 /* predicate end */
7835 --nested_pred;
Michal Vaskob8ee3562022-08-02 10:43:17 +02007836 }
Michal Vasko31f19632022-11-16 09:38:40 +01007837 } while (nested_pred);
Michal Vasko3d969ff2022-07-29 15:02:08 +02007838
7839 /* try to evaluate the value */
7840 LY_CHECK_GOTO(rc = eval_name_test_try_compile_predicate_append(exp, val_start_idx, e_idx - 1, ctx_scnode, set,
7841 ctx_scnode, &pred, &pred_len), cleanup);
7842
Michal Vasko004d3152020-06-11 19:59:22 +02007843 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02007844 }
7845
Michal Vasko004d3152020-06-11 19:59:22 +02007846 /* parse the predicate(s) */
Michal Vasko3d969ff2022-07-29 15:02:08 +02007847 LY_CHECK_GOTO(rc = ly_path_parse_predicate(set->ctx, ctx_scnode, pred, pred_len, LY_PATH_PREFIX_OPTIONAL,
7848 LY_PATH_PRED_SIMPLE, &exp2), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02007849
7850 /* compile */
Michal Vasko3d969ff2022-07-29 15:02:08 +02007851 rc = ly_path_compile_predicate(set->ctx, set->cur_node ? set->cur_node->schema : NULL, set->cur_mod, ctx_scnode, exp2,
7852 &pred_idx, LY_VALUE_JSON, NULL, predicates, pred_type);
7853 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02007854
7855 /* success, the predicate must include all the needed information for hash-based search */
7856 *tok_idx = e_idx;
7857
7858cleanup:
7859 ly_log_options(prev_lo);
Michal Vasko3d969ff2022-07-29 15:02:08 +02007860 lyxp_expr_free(set->ctx, exp2);
7861 free(pred);
7862 return rc;
Michal Vaskod3678892020-05-21 10:06:58 +02007863}
7864
7865/**
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007866 * @brief Search for/check the next schema node that could be the only matching schema node meaning the
7867 * data node(s) could be found using a single hash-based search.
7868 *
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007869 * @param[in] ctx libyang context.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007870 * @param[in] node Next context node to check.
7871 * @param[in] name Expected node name.
7872 * @param[in] name_len Length of @p name.
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007873 * @param[in] moveto_mod Expected node module, can be NULL for JSON format with no prefix.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007874 * @param[in] root_type XPath root type.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007875 * @param[in] format Prefix format.
7876 * @param[in,out] found Previously found node, is updated.
7877 * @return LY_SUCCESS on success,
7878 * @return LY_ENOT if the whole check failed and hashes cannot be used.
7879 */
7880static LY_ERR
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007881eval_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 +02007882 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 +02007883 const struct lysc_node **found)
7884{
7885 const struct lysc_node *scnode;
7886 const struct lys_module *mod;
7887 uint32_t idx = 0;
7888
Radek Krejci8df109d2021-04-23 12:19:08 +02007889 assert((format == LY_VALUE_JSON) || moveto_mod);
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007890
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007891continue_search:
Michal Vasko7d1d0912020-10-16 08:38:30 +02007892 scnode = NULL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007893 if (!node) {
Radek Krejci8df109d2021-04-23 12:19:08 +02007894 if ((format == LY_VALUE_JSON) && !moveto_mod) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007895 /* search all modules for a single match */
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007896 while ((mod = ly_ctx_get_module_iter(ctx, &idx))) {
Michal Vasko35a3b1d2021-07-14 09:34:16 +02007897 if (!mod->implemented) {
7898 continue;
7899 }
7900
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007901 scnode = lys_find_child(NULL, mod, name, name_len, 0, 0);
7902 if (scnode) {
7903 /* we have found a match */
7904 break;
7905 }
7906 }
7907
Michal Vasko7d1d0912020-10-16 08:38:30 +02007908 if (!scnode) {
7909 /* all modules searched */
7910 idx = 0;
7911 }
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007912 } else {
7913 /* search in top-level */
7914 scnode = lys_find_child(NULL, moveto_mod, name, name_len, 0, 0);
7915 }
7916 } else if (!*found || (lysc_data_parent(*found) != node->schema)) {
Radek Krejci8df109d2021-04-23 12:19:08 +02007917 if ((format == LY_VALUE_JSON) && !moveto_mod) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007918 /* we must adjust the module to inherit the one from the context node */
7919 moveto_mod = node->schema->module;
7920 }
7921
7922 /* search in children, do not repeat the same search */
7923 scnode = lys_find_child(node->schema, moveto_mod, name, name_len, 0, 0);
Michal Vasko7d1d0912020-10-16 08:38:30 +02007924 } /* else skip redundant search */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007925
7926 /* additional context check */
7927 if (scnode && (root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) {
7928 scnode = NULL;
7929 }
7930
7931 if (scnode) {
7932 if (*found) {
7933 /* we found a schema node with the same name but at different level, give up, too complicated
7934 * (more hash-based searches would be required, not supported) */
7935 return LY_ENOT;
7936 } else {
7937 /* remember the found schema node and continue to make sure it can be used */
7938 *found = scnode;
7939 }
7940 }
7941
7942 if (idx) {
7943 /* continue searching all the following models */
7944 goto continue_search;
7945 }
7946
7947 return LY_SUCCESS;
7948}
7949
7950/**
Michal Vasko4ad69e72021-10-26 16:25:55 +02007951 * @brief Generate message when no matching schema nodes were found for a path segment.
7952 *
7953 * @param[in] set XPath set.
7954 * @param[in] scparent Previous schema parent in the context, if only one.
7955 * @param[in] ncname XPath NCName being evaluated.
7956 * @param[in] ncname_len Length of @p ncname.
7957 * @param[in] expr Whole XPath expression.
7958 * @param[in] options XPath options.
7959 */
7960static void
7961eval_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 +02007962 uint32_t ncname_len, const char *expr, uint32_t options)
Michal Vasko4ad69e72021-10-26 16:25:55 +02007963{
7964 const char *format;
7965 char *path = NULL, *ppath = NULL;
7966
7967 path = lysc_path(set->cur_scnode, LYSC_PATH_LOG, NULL, 0);
7968 if (scparent) {
7969 /* generate path for the parent */
7970 if (scparent->type == LYXP_NODE_ELEM) {
7971 ppath = lysc_path(scparent->scnode, LYSC_PATH_LOG, NULL, 0);
7972 } else if (scparent->type == LYXP_NODE_ROOT) {
7973 ppath = strdup("<root>");
7974 } else if (scparent->type == LYXP_NODE_ROOT_CONFIG) {
7975 ppath = strdup("<config-root>");
7976 }
7977 }
7978 if (ppath) {
7979 format = "Schema node \"%.*s\" for parent \"%s\" not found; in expr \"%.*s\" with context node \"%s\".";
7980 if (options & LYXP_SCNODE_ERROR) {
7981 LOGERR(set->ctx, LY_EVALID, format, ncname_len, ncname, ppath, (ncname - expr) + ncname_len, expr, path);
7982 } else {
7983 LOGWRN(set->ctx, format, ncname_len, ncname, ppath, (ncname - expr) + ncname_len, expr, path);
7984 }
7985 } else {
7986 format = "Schema node \"%.*s\" not found; in expr \"%.*s\" with context node \"%s\".";
7987 if (options & LYXP_SCNODE_ERROR) {
7988 LOGERR(set->ctx, LY_EVALID, format, ncname_len, ncname, (ncname - expr) + ncname_len, expr, path);
7989 } else {
7990 LOGWRN(set->ctx, format, ncname_len, ncname, (ncname - expr) + ncname_len, expr, path);
7991 }
7992 }
7993 free(path);
7994 free(ppath);
7995}
7996
7997/**
Michal Vaskod3678892020-05-21 10:06:58 +02007998 * @brief Evaluate NameTest and any following Predicates. Logs directly on error.
7999 *
8000 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
8001 * [6] NodeTest ::= NameTest | NodeType '(' ')'
8002 * [7] NameTest ::= '*' | NCName ':' '*' | QName
8003 *
8004 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008005 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko49fec8e2022-05-24 10:28:33 +02008006 * @param[in] axis What axis to search on.
Michal Vaskod3678892020-05-21 10:06:58 +02008007 * @param[in] all_desc Whether to search all the descendants or children only.
aPiecek8b0cc152021-05-31 16:40:31 +02008008 * @param[in,out] set Context and result set.
Michal Vaskod3678892020-05-21 10:06:58 +02008009 * @param[in] options XPath options.
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008010 * @return LY_ERR (LY_EINCOMPLETE on unresolved when, LY_ENOT for not found schema node)
Michal Vaskod3678892020-05-21 10:06:58 +02008011 */
8012static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02008013eval_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 +02008014 struct lyxp_set *set, uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02008015{
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008016 LY_ERR rc = LY_SUCCESS, r;
Michal Vasko004d3152020-06-11 19:59:22 +02008017 const char *ncname, *ncname_dict = NULL;
Michal Vasko56c008c2022-07-29 14:57:47 +02008018 uint32_t i, ncname_len;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008019 const struct lys_module *moveto_mod = NULL;
Michal Vaskoc71c37b2021-01-11 13:40:02 +01008020 const struct lysc_node *scnode = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02008021 struct ly_path_predicate *predicates = NULL;
8022 enum ly_path_pred_type pred_type = 0;
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008023 int scnode_skip_pred = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02008024
aPiecek8b0cc152021-05-31 16:40:31 +02008025 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008026 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008027 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02008028
aPiecek8b0cc152021-05-31 16:40:31 +02008029 if (options & LYXP_SKIP_EXPR) {
Michal Vaskod3678892020-05-21 10:06:58 +02008030 goto moveto;
8031 }
8032
Michal Vasko004d3152020-06-11 19:59:22 +02008033 ncname = &exp->expr[exp->tok_pos[*tok_idx - 1]];
8034 ncname_len = exp->tok_len[*tok_idx - 1];
Michal Vaskod3678892020-05-21 10:06:58 +02008035
Michal Vaskoc71c37b2021-01-11 13:40:02 +01008036 if ((ncname[0] == '*') && (ncname_len == 1)) {
8037 /* all nodes will match */
8038 goto moveto;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008039 }
8040
Michal Vaskoc71c37b2021-01-11 13:40:02 +01008041 /* parse (and skip) module name */
8042 rc = moveto_resolve_model(&ncname, &ncname_len, set, NULL, &moveto_mod);
Michal Vaskod3678892020-05-21 10:06:58 +02008043 LY_CHECK_GOTO(rc, cleanup);
8044
Michal Vasko49fec8e2022-05-24 10:28:33 +02008045 if ((ncname[0] == '*') && (ncname_len == 1)) {
8046 /* all nodes from the module will match */
8047 goto moveto;
8048 }
8049
8050 if (((set->format == LY_VALUE_JSON) || moveto_mod) && (axis == LYXP_AXIS_CHILD) && !all_desc &&
8051 (set->type == LYXP_SET_NODE_SET)) {
Michal Vaskod3678892020-05-21 10:06:58 +02008052 /* find the matching schema node in some parent in the context */
Michal Vasko56c008c2022-07-29 14:57:47 +02008053 for (i = 0; i < set->used; ++i) {
Michal Vaskoc71c37b2021-01-11 13:40:02 +01008054 if (eval_name_test_with_predicate_get_scnode(set->ctx, set->val.nodes[i].node, ncname, ncname_len,
8055 moveto_mod, set->root_type, set->format, &scnode)) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008056 /* check failed */
8057 scnode = NULL;
8058 break;
Michal Vaskod3678892020-05-21 10:06:58 +02008059 }
8060 }
8061
Michal Vasko004d3152020-06-11 19:59:22 +02008062 if (scnode && (scnode->nodetype & (LYS_LIST | LYS_LEAFLIST))) {
8063 /* try to create the predicates */
Michal Vasko3d969ff2022-07-29 15:02:08 +02008064 if (eval_name_test_try_compile_predicates(exp, tok_idx, scnode, set, &predicates, &pred_type)) {
Michal Vasko004d3152020-06-11 19:59:22 +02008065 /* hashes cannot be used */
Michal Vaskod3678892020-05-21 10:06:58 +02008066 scnode = NULL;
8067 }
8068 }
8069 }
8070
Michal Vaskoc71c37b2021-01-11 13:40:02 +01008071 if (!scnode) {
8072 /* we are not able to match based on a schema node and not all the modules match ("*"),
Michal Vasko004d3152020-06-11 19:59:22 +02008073 * use dictionary for efficient comparison */
Radek Krejci011e4aa2020-09-04 15:22:31 +02008074 LY_CHECK_GOTO(rc = lydict_insert(set->ctx, ncname, ncname_len, &ncname_dict), cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02008075 }
8076
8077moveto:
8078 /* move to the attribute(s), data node(s), or schema node(s) */
Michal Vasko49fec8e2022-05-24 10:28:33 +02008079 if (axis == LYXP_AXIS_ATTRIBUTE) {
aPiecek8b0cc152021-05-31 16:40:31 +02008080 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008081 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskod3678892020-05-21 10:06:58 +02008082 } else {
8083 if (all_desc) {
Michal Vaskocdad7122020-11-09 21:04:44 +01008084 rc = moveto_attr_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02008085 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02008086 rc = moveto_attr(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02008087 }
8088 LY_CHECK_GOTO(rc, cleanup);
8089 }
8090 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02008091 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008092 const struct lyxp_set_scnode *scparent = NULL;
Michal Vasko56c008c2022-07-29 14:57:47 +02008093 ly_bool found = 0;
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008094
8095 /* remember parent if there is only one, to print in the warning */
8096 for (i = 0; i < set->used; ++i) {
8097 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
8098 if (!scparent) {
8099 /* remember the context node */
8100 scparent = &set->val.scnodes[i];
8101 } else {
8102 /* several context nodes, no reasonable error possible */
8103 scparent = NULL;
8104 break;
8105 }
8106 }
8107 }
Radek Krejci1deb5be2020-08-26 16:43:36 +02008108
Michal Vasko49fec8e2022-05-24 10:28:33 +02008109 if (all_desc && (axis == LYXP_AXIS_CHILD)) {
8110 /* efficient evaluation that does not add all the descendants into the set */
8111 rc = moveto_scnode_alldesc_child(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02008112 } else {
Michal Vasko49fec8e2022-05-24 10:28:33 +02008113 if (all_desc) {
8114 /* "//" == "/descendant-or-self::node()/" */
8115 rc = xpath_pi_node(set, LYXP_AXIS_DESCENDANT_OR_SELF, options);
8116 LY_CHECK_GOTO(rc, cleanup);
8117 }
8118 rc = moveto_scnode(set, moveto_mod, ncname_dict, axis, options);
Michal Vaskod3678892020-05-21 10:06:58 +02008119 }
8120 LY_CHECK_GOTO(rc, cleanup);
8121
Michal Vasko56c008c2022-07-29 14:57:47 +02008122 i = set->used;
8123 do {
8124 --i;
Michal Vasko1a09b212021-05-06 13:00:10 +02008125 if (set->val.scnodes[i].in_ctx > LYXP_SET_SCNODE_ATOM_NODE) {
Michal Vasko56c008c2022-07-29 14:57:47 +02008126 found = 1;
Michal Vaskod3678892020-05-21 10:06:58 +02008127 break;
8128 }
Michal Vasko56c008c2022-07-29 14:57:47 +02008129 } while (i);
8130 if (!found) {
Michal Vasko4ad69e72021-10-26 16:25:55 +02008131 /* generate message */
8132 eval_name_test_scnode_no_match_msg(set, scparent, ncname, ncname_len, exp->expr, options);
8133
8134 if (options & LYXP_SCNODE_ERROR) {
8135 /* error */
8136 rc = LY_EVALID;
8137 goto cleanup;
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008138 }
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008139
8140 /* skip the predicates and the rest of this path to not generate invalid warnings */
8141 rc = LY_ENOT;
8142 scnode_skip_pred = 1;
Michal Vaskod3678892020-05-21 10:06:58 +02008143 }
8144 } else {
Michal Vasko49fec8e2022-05-24 10:28:33 +02008145 if (all_desc && (axis == LYXP_AXIS_CHILD)) {
8146 /* efficient evaluation */
8147 rc = moveto_node_alldesc_child(set, moveto_mod, ncname_dict, options);
8148 } else if (scnode && (axis == LYXP_AXIS_CHILD)) {
8149 /* we can find the child nodes using hashes */
8150 rc = moveto_node_hash_child(set, scnode, predicates, options);
Michal Vaskod3678892020-05-21 10:06:58 +02008151 } else {
Michal Vasko49fec8e2022-05-24 10:28:33 +02008152 if (all_desc) {
8153 /* "//" == "/descendant-or-self::node()/" */
8154 rc = xpath_pi_node(set, LYXP_AXIS_DESCENDANT_OR_SELF, options);
8155 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02008156 }
Michal Vasko49fec8e2022-05-24 10:28:33 +02008157 rc = moveto_node(set, moveto_mod, ncname_dict, axis, options);
Michal Vaskod3678892020-05-21 10:06:58 +02008158 }
8159 LY_CHECK_GOTO(rc, cleanup);
8160 }
8161 }
8162
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008163 if (scnode_skip_pred) {
8164 /* skip predicates */
8165 options |= LYXP_SKIP_EXPR;
8166 }
8167
Michal Vaskod3678892020-05-21 10:06:58 +02008168 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02008169 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02008170 r = eval_predicate(exp, tok_idx, set, options, axis);
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008171 LY_CHECK_ERR_GOTO(r, rc = r, cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02008172 }
8173
8174cleanup:
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008175 if (scnode_skip_pred) {
8176 /* restore options */
8177 options &= ~LYXP_SKIP_EXPR;
8178 }
aPiecek8b0cc152021-05-31 16:40:31 +02008179 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko004d3152020-06-11 19:59:22 +02008180 lydict_remove(set->ctx, ncname_dict);
Michal Vaskof7e16e22020-10-21 09:24:39 +02008181 ly_path_predicates_free(set->ctx, pred_type, predicates);
Michal Vaskodb51a8d2020-05-27 15:22:29 +02008182 }
Michal Vaskod3678892020-05-21 10:06:58 +02008183 return rc;
8184}
8185
8186/**
8187 * @brief Evaluate NodeType and any following Predicates. Logs directly on error.
8188 *
8189 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
8190 * [6] NodeTest ::= NameTest | NodeType '(' ')'
8191 * [8] NodeType ::= 'text' | 'node'
8192 *
8193 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008194 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko49fec8e2022-05-24 10:28:33 +02008195 * @param[in] axis Axis to search on.
8196 * @param[in] all_desc Whether to search all the descendants or axis only.
aPiecek8b0cc152021-05-31 16:40:31 +02008197 * @param[in,out] set Context and result set.
Michal Vaskod3678892020-05-21 10:06:58 +02008198 * @param[in] options XPath options.
8199 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8200 */
8201static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02008202eval_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 +02008203 struct lyxp_set *set, uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02008204{
8205 LY_ERR rc;
8206
Michal Vaskod3678892020-05-21 10:06:58 +02008207 (void)all_desc;
8208
aPiecek8b0cc152021-05-31 16:40:31 +02008209 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko004d3152020-06-11 19:59:22 +02008210 assert(exp->tok_len[*tok_idx] == 4);
Michal Vasko49fec8e2022-05-24 10:28:33 +02008211 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "node", 4)) {
8212 rc = xpath_pi_node(set, axis, options);
Michal Vaskod3678892020-05-21 10:06:58 +02008213 } else {
Michal Vasko49fec8e2022-05-24 10:28:33 +02008214 assert(!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "text", 4));
8215 rc = xpath_pi_text(set, axis, options);
Michal Vaskod3678892020-05-21 10:06:58 +02008216 }
Michal Vasko49fec8e2022-05-24 10:28:33 +02008217 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02008218 }
aPiecek8b0cc152021-05-31 16:40:31 +02008219 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008220 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008221 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02008222
8223 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02008224 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
aPiecek8b0cc152021-05-31 16:40:31 +02008225 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008226 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008227 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02008228
8229 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02008230 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
aPiecek8b0cc152021-05-31 16:40:31 +02008231 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008232 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008233 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02008234
8235 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02008236 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02008237 rc = eval_predicate(exp, tok_idx, set, options, axis);
Michal Vaskod3678892020-05-21 10:06:58 +02008238 LY_CHECK_RET(rc);
8239 }
8240
8241 return LY_SUCCESS;
8242}
8243
8244/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02008245 * @brief Evaluate RelativeLocationPath. Logs directly on error.
8246 *
8247 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
8248 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
Michal Vaskod3678892020-05-21 10:06:58 +02008249 * [6] NodeTest ::= NameTest | NodeType '(' ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02008250 *
8251 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008252 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008253 * @param[in] all_desc Whether to search all the descendants or children only.
aPiecek8b0cc152021-05-31 16:40:31 +02008254 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008255 * @param[in] options XPath options.
8256 * @return LY_ERR (YL_EINCOMPLETE on unresolved when)
8257 */
8258static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02008259eval_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 +02008260 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008261{
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008262 LY_ERR rc = LY_SUCCESS;
Michal Vasko49fec8e2022-05-24 10:28:33 +02008263 enum lyxp_axis axis;
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008264 int scnode_skip_path = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008265
8266 goto step;
8267 do {
8268 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02008269 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008270 all_desc = 0;
8271 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02008272 assert(exp->tok_len[*tok_idx] == 2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008273 all_desc = 1;
8274 }
aPiecek8b0cc152021-05-31 16:40:31 +02008275 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008276 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008277 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008278
8279step:
Michal Vasko49fec8e2022-05-24 10:28:33 +02008280 /* AxisSpecifier */
8281 if (exp->tokens[*tok_idx] == LYXP_TOKEN_AXISNAME) {
8282 axis = str2axis(exp->expr + exp->tok_pos[*tok_idx], exp->tok_len[*tok_idx]);
Michal Vaskod3678892020-05-21 10:06:58 +02008283
aPiecek8b0cc152021-05-31 16:40:31 +02008284 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008285 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
8286 ++(*tok_idx);
8287
8288 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_DCOLON);
8289 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
8290 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
8291 ++(*tok_idx);
8292 } else if (exp->tokens[*tok_idx] == LYXP_TOKEN_AT) {
8293 axis = LYXP_AXIS_ATTRIBUTE;
8294
8295 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
8296 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008297 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02008298 } else {
Michal Vasko49fec8e2022-05-24 10:28:33 +02008299 /* default */
8300 axis = LYXP_AXIS_CHILD;
Michal Vaskod3678892020-05-21 10:06:58 +02008301 }
8302
Michal Vasko49fec8e2022-05-24 10:28:33 +02008303 /* NodeTest Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02008304 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008305 case LYXP_TOKEN_DOT:
8306 /* evaluate '.' */
Michal Vasko49fec8e2022-05-24 10:28:33 +02008307 if (!(options & LYXP_SKIP_EXPR)) {
8308 if (((options & LYXP_SCNODE_ALL) && (set->type != LYXP_SET_SCNODE_SET)) ||
8309 (!(options & LYXP_SCNODE_ALL) && (set->type != LYXP_SET_NODE_SET))) {
8310 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
8311 rc = LY_EVALID;
8312 goto cleanup;
8313 }
8314
8315 if (all_desc) {
8316 rc = xpath_pi_node(set, LYXP_AXIS_DESCENDANT_OR_SELF, options);
8317 LY_CHECK_GOTO(rc, cleanup);
8318 }
8319 rc = xpath_pi_node(set, LYXP_AXIS_SELF, options);
8320 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008321 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008322 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008323 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008324 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008325 break;
8326
8327 case LYXP_TOKEN_DDOT:
8328 /* evaluate '..' */
Michal Vasko49fec8e2022-05-24 10:28:33 +02008329 if (!(options & LYXP_SKIP_EXPR)) {
8330 if (((options & LYXP_SCNODE_ALL) && (set->type != LYXP_SET_SCNODE_SET)) ||
8331 (!(options & LYXP_SCNODE_ALL) && (set->type != LYXP_SET_NODE_SET))) {
8332 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
8333 rc = LY_EVALID;
8334 goto cleanup;
8335 }
8336
8337 if (all_desc) {
8338 rc = xpath_pi_node(set, LYXP_AXIS_DESCENDANT_OR_SELF, options);
8339 LY_CHECK_GOTO(rc, cleanup);
8340 }
8341 rc = xpath_pi_node(set, LYXP_AXIS_PARENT, options);
8342 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008343 }
aPiecek8b0cc152021-05-31 16:40:31 +02008344 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008345 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008346 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008347 break;
8348
Michal Vasko03ff5a72019-09-11 13:49:33 +02008349 case LYXP_TOKEN_NAMETEST:
Michal Vaskod3678892020-05-21 10:06:58 +02008350 /* evaluate NameTest Predicate* */
Michal Vasko49fec8e2022-05-24 10:28:33 +02008351 rc = eval_name_test_with_predicate(exp, tok_idx, axis, all_desc, set, options);
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008352 if (rc == LY_ENOT) {
8353 assert(options & LYXP_SCNODE_ALL);
8354 /* skip the rest of this path */
8355 rc = LY_SUCCESS;
8356 scnode_skip_path = 1;
8357 options |= LYXP_SKIP_EXPR;
8358 }
8359 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02008360 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008361
Michal Vaskod3678892020-05-21 10:06:58 +02008362 case LYXP_TOKEN_NODETYPE:
8363 /* evaluate NodeType Predicate* */
Michal Vasko49fec8e2022-05-24 10:28:33 +02008364 rc = eval_node_type_with_predicate(exp, tok_idx, axis, all_desc, set, options);
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008365 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008366 break;
8367
8368 default:
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008369 LOGINT(set->ctx);
8370 rc = LY_EINT;
8371 goto cleanup;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008372 }
Michal Vasko004d3152020-06-11 19:59:22 +02008373 } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008374
Michal Vaskoa036a6b2021-10-12 16:05:23 +02008375cleanup:
8376 if (scnode_skip_path) {
8377 options &= ~LYXP_SKIP_EXPR;
8378 }
8379 return rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008380}
8381
8382/**
8383 * @brief Evaluate AbsoluteLocationPath. Logs directly on error.
8384 *
8385 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
8386 *
8387 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008388 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02008389 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008390 * @param[in] options XPath options.
8391 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8392 */
8393static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02008394eval_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 +02008395{
Radek Krejci857189e2020-09-01 13:26:36 +02008396 ly_bool all_desc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008397
aPiecek8b0cc152021-05-31 16:40:31 +02008398 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008399 /* no matter what tokens follow, we need to be at the root */
Michal Vaskob0099a92020-08-31 14:55:23 +02008400 LY_CHECK_RET(moveto_root(set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008401 }
8402
8403 /* '/' RelativeLocationPath? */
Michal Vasko004d3152020-06-11 19:59:22 +02008404 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008405 /* evaluate '/' - deferred */
8406 all_desc = 0;
aPiecek8b0cc152021-05-31 16:40:31 +02008407 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008408 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008409 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008410
Michal Vasko004d3152020-06-11 19:59:22 +02008411 if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008412 return LY_SUCCESS;
8413 }
Michal Vasko004d3152020-06-11 19:59:22 +02008414 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008415 case LYXP_TOKEN_DOT:
8416 case LYXP_TOKEN_DDOT:
Michal Vasko49fec8e2022-05-24 10:28:33 +02008417 case LYXP_TOKEN_AXISNAME:
Michal Vasko03ff5a72019-09-11 13:49:33 +02008418 case LYXP_TOKEN_AT:
8419 case LYXP_TOKEN_NAMETEST:
8420 case LYXP_TOKEN_NODETYPE:
Michal Vaskob0099a92020-08-31 14:55:23 +02008421 LY_CHECK_RET(eval_relative_location_path(exp, tok_idx, all_desc, set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008422 break;
8423 default:
8424 break;
8425 }
8426
Michal Vasko03ff5a72019-09-11 13:49:33 +02008427 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02008428 /* '//' RelativeLocationPath */
Michal Vasko03ff5a72019-09-11 13:49:33 +02008429 /* evaluate '//' - deferred so as not to waste memory by remembering all the nodes */
8430 all_desc = 1;
aPiecek8b0cc152021-05-31 16:40:31 +02008431 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008432 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008433 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008434
Michal Vaskob0099a92020-08-31 14:55:23 +02008435 LY_CHECK_RET(eval_relative_location_path(exp, tok_idx, all_desc, set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008436 }
8437
8438 return LY_SUCCESS;
8439}
8440
8441/**
8442 * @brief Evaluate FunctionCall. Logs directly on error.
8443 *
Michal Vaskod3678892020-05-21 10:06:58 +02008444 * [11] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02008445 *
8446 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008447 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02008448 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008449 * @param[in] options XPath options.
8450 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8451 */
8452static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02008453eval_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 +02008454{
8455 LY_ERR rc;
Michal Vasko69730152020-10-09 16:30:07 +02008456
Michal Vaskodd528af2022-08-08 14:35:07 +02008457 LY_ERR (*xpath_func)(struct lyxp_set **, uint32_t, struct lyxp_set *, uint32_t) = NULL;
8458 uint32_t arg_count = 0, i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008459 struct lyxp_set **args = NULL, **args_aux;
8460
aPiecek8b0cc152021-05-31 16:40:31 +02008461 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008462 /* FunctionName */
Michal Vasko004d3152020-06-11 19:59:22 +02008463 switch (exp->tok_len[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008464 case 3:
Michal Vasko004d3152020-06-11 19:59:22 +02008465 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008466 xpath_func = &xpath_not;
Michal Vasko004d3152020-06-11 19:59:22 +02008467 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008468 xpath_func = &xpath_sum;
8469 }
8470 break;
8471 case 4:
Michal Vasko004d3152020-06-11 19:59:22 +02008472 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008473 xpath_func = &xpath_lang;
Michal Vasko004d3152020-06-11 19:59:22 +02008474 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008475 xpath_func = &xpath_last;
Michal Vasko004d3152020-06-11 19:59:22 +02008476 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008477 xpath_func = &xpath_name;
Michal Vasko004d3152020-06-11 19:59:22 +02008478 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008479 xpath_func = &xpath_true;
8480 }
8481 break;
8482 case 5:
Michal Vasko004d3152020-06-11 19:59:22 +02008483 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008484 xpath_func = &xpath_count;
Michal Vasko004d3152020-06-11 19:59:22 +02008485 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008486 xpath_func = &xpath_false;
Michal Vasko004d3152020-06-11 19:59:22 +02008487 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008488 xpath_func = &xpath_floor;
Michal Vasko004d3152020-06-11 19:59:22 +02008489 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008490 xpath_func = &xpath_round;
Michal Vasko004d3152020-06-11 19:59:22 +02008491 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008492 xpath_func = &xpath_deref;
8493 }
8494 break;
8495 case 6:
Michal Vasko004d3152020-06-11 19:59:22 +02008496 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008497 xpath_func = &xpath_concat;
Michal Vasko004d3152020-06-11 19:59:22 +02008498 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008499 xpath_func = &xpath_number;
Michal Vasko004d3152020-06-11 19:59:22 +02008500 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008501 xpath_func = &xpath_string;
8502 }
8503 break;
8504 case 7:
Michal Vasko004d3152020-06-11 19:59:22 +02008505 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008506 xpath_func = &xpath_boolean;
Michal Vasko004d3152020-06-11 19:59:22 +02008507 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008508 xpath_func = &xpath_ceiling;
Michal Vasko004d3152020-06-11 19:59:22 +02008509 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008510 xpath_func = &xpath_current;
8511 }
8512 break;
8513 case 8:
Michal Vasko004d3152020-06-11 19:59:22 +02008514 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008515 xpath_func = &xpath_contains;
Michal Vasko004d3152020-06-11 19:59:22 +02008516 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008517 xpath_func = &xpath_position;
Michal Vasko004d3152020-06-11 19:59:22 +02008518 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008519 xpath_func = &xpath_re_match;
8520 }
8521 break;
8522 case 9:
Michal Vasko004d3152020-06-11 19:59:22 +02008523 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008524 xpath_func = &xpath_substring;
Michal Vasko004d3152020-06-11 19:59:22 +02008525 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008526 xpath_func = &xpath_translate;
8527 }
8528 break;
8529 case 10:
Michal Vasko004d3152020-06-11 19:59:22 +02008530 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008531 xpath_func = &xpath_local_name;
Michal Vasko004d3152020-06-11 19:59:22 +02008532 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008533 xpath_func = &xpath_enum_value;
Michal Vasko004d3152020-06-11 19:59:22 +02008534 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008535 xpath_func = &xpath_bit_is_set;
8536 }
8537 break;
8538 case 11:
Michal Vasko004d3152020-06-11 19:59:22 +02008539 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008540 xpath_func = &xpath_starts_with;
8541 }
8542 break;
8543 case 12:
Michal Vasko004d3152020-06-11 19:59:22 +02008544 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008545 xpath_func = &xpath_derived_from;
8546 }
8547 break;
8548 case 13:
Michal Vasko004d3152020-06-11 19:59:22 +02008549 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008550 xpath_func = &xpath_namespace_uri;
Michal Vasko004d3152020-06-11 19:59:22 +02008551 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008552 xpath_func = &xpath_string_length;
8553 }
8554 break;
8555 case 15:
Michal Vasko004d3152020-06-11 19:59:22 +02008556 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008557 xpath_func = &xpath_normalize_space;
Michal Vasko004d3152020-06-11 19:59:22 +02008558 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008559 xpath_func = &xpath_substring_after;
8560 }
8561 break;
8562 case 16:
Michal Vasko004d3152020-06-11 19:59:22 +02008563 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008564 xpath_func = &xpath_substring_before;
8565 }
8566 break;
8567 case 20:
Michal Vasko004d3152020-06-11 19:59:22 +02008568 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008569 xpath_func = &xpath_derived_from_or_self;
8570 }
8571 break;
8572 }
8573
8574 if (!xpath_func) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01008575 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 +02008576 return LY_EVALID;
8577 }
8578 }
8579
aPiecek8b0cc152021-05-31 16:40:31 +02008580 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008581 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008582 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008583
8584 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02008585 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
aPiecek8b0cc152021-05-31 16:40:31 +02008586 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008587 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008588 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008589
8590 /* ( Expr ( ',' Expr )* )? */
Michal Vasko004d3152020-06-11 19:59:22 +02008591 if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) {
aPiecek8b0cc152021-05-31 16:40:31 +02008592 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008593 args = malloc(sizeof *args);
8594 LY_CHECK_ERR_GOTO(!args, LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
8595 arg_count = 1;
8596 args[0] = set_copy(set);
8597 if (!args[0]) {
8598 rc = LY_EMEM;
8599 goto cleanup;
8600 }
8601
Michal Vasko004d3152020-06-11 19:59:22 +02008602 rc = eval_expr_select(exp, tok_idx, 0, args[0], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008603 LY_CHECK_GOTO(rc, cleanup);
8604 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02008605 rc = eval_expr_select(exp, tok_idx, 0, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008606 LY_CHECK_GOTO(rc, cleanup);
8607 }
8608 }
Michal Vasko004d3152020-06-11 19:59:22 +02008609 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) {
aPiecek8b0cc152021-05-31 16:40:31 +02008610 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008611 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008612 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008613
aPiecek8b0cc152021-05-31 16:40:31 +02008614 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008615 ++arg_count;
8616 args_aux = realloc(args, arg_count * sizeof *args);
8617 LY_CHECK_ERR_GOTO(!args_aux, arg_count--; LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
8618 args = args_aux;
8619 args[arg_count - 1] = set_copy(set);
8620 if (!args[arg_count - 1]) {
8621 rc = LY_EMEM;
8622 goto cleanup;
8623 }
8624
Michal Vasko004d3152020-06-11 19:59:22 +02008625 rc = eval_expr_select(exp, tok_idx, 0, args[arg_count - 1], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008626 LY_CHECK_GOTO(rc, cleanup);
8627 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02008628 rc = eval_expr_select(exp, tok_idx, 0, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008629 LY_CHECK_GOTO(rc, cleanup);
8630 }
8631 }
8632
8633 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02008634 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
aPiecek8b0cc152021-05-31 16:40:31 +02008635 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008636 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008637 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008638
aPiecek8b0cc152021-05-31 16:40:31 +02008639 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008640 /* evaluate function */
8641 rc = xpath_func(args, arg_count, set, options);
8642
8643 if (options & LYXP_SCNODE_ALL) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008644 /* merge all nodes from arg evaluations */
8645 for (i = 0; i < arg_count; ++i) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008646 set_scnode_clear_ctx(args[i], LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008647 lyxp_set_scnode_merge(set, args[i]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008648 }
8649 }
8650 } else {
8651 rc = LY_SUCCESS;
8652 }
8653
8654cleanup:
8655 for (i = 0; i < arg_count; ++i) {
8656 lyxp_set_free(args[i]);
8657 }
8658 free(args);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008659 return rc;
8660}
8661
8662/**
8663 * @brief Evaluate Number. Logs directly on error.
8664 *
8665 * @param[in] ctx Context for errors.
8666 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008667 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008668 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8669 * @return LY_ERR
8670 */
8671static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02008672eval_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 +02008673{
8674 long double num;
8675 char *endptr;
8676
8677 if (set) {
8678 errno = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02008679 num = strtold(&exp->expr[exp->tok_pos[*tok_idx]], &endptr);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008680 if (errno) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01008681 LOGVAL(ctx, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]);
8682 LOGVAL(ctx, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double (%s).",
Michal Vasko69730152020-10-09 16:30:07 +02008683 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]], strerror(errno));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008684 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02008685 } else if (endptr - &exp->expr[exp->tok_pos[*tok_idx]] != exp->tok_len[*tok_idx]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01008686 LOGVAL(ctx, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]);
8687 LOGVAL(ctx, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double.",
Michal Vasko69730152020-10-09 16:30:07 +02008688 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008689 return LY_EVALID;
8690 }
8691
8692 set_fill_number(set, num);
8693 }
8694
8695 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008696 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008697 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008698 return LY_SUCCESS;
8699}
8700
aPiecekdf23eee2021-10-07 12:21:50 +02008701LY_ERR
8702lyxp_vars_find(struct lyxp_var *vars, const char *name, size_t name_len, struct lyxp_var **var)
8703{
8704 LY_ERR ret = LY_ENOTFOUND;
8705 LY_ARRAY_COUNT_TYPE u;
8706
8707 assert(vars && name);
8708
8709 name_len = name_len ? name_len : strlen(name);
8710
8711 LY_ARRAY_FOR(vars, u) {
8712 if (!strncmp(vars[u].name, name, name_len)) {
8713 ret = LY_SUCCESS;
8714 break;
8715 }
8716 }
8717
8718 if (var && !ret) {
8719 *var = &vars[u];
8720 }
8721
8722 return ret;
8723}
8724
Michal Vasko03ff5a72019-09-11 13:49:33 +02008725/**
aPiecekfba75362021-10-07 12:39:48 +02008726 * @brief Evaluate VariableReference.
8727 *
8728 * @param[in] exp Parsed XPath expression.
8729 * @param[in] tok_idx Position in the expression @p exp.
8730 * @param[in] vars [Sized array](@ref sizedarrays) of XPath variables.
8731 * @param[in,out] set Context and result set.
8732 * @param[in] options XPath options.
8733 * @return LY_ERR value.
8734 */
8735static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02008736eval_variable_reference(const struct lyxp_expr *exp, uint32_t *tok_idx, struct lyxp_set *set, uint32_t options)
aPiecekfba75362021-10-07 12:39:48 +02008737{
8738 LY_ERR ret;
8739 const char *name;
8740 struct lyxp_var *var;
8741 const struct lyxp_var *vars;
8742 struct lyxp_expr *tokens = NULL;
Michal Vaskodd528af2022-08-08 14:35:07 +02008743 uint32_t token_index, name_len;
aPiecekfba75362021-10-07 12:39:48 +02008744
8745 vars = set->vars;
8746
Michal Vasko49fec8e2022-05-24 10:28:33 +02008747 /* find out the name and value of the variable */
aPiecekfba75362021-10-07 12:39:48 +02008748 name = &exp->expr[exp->tok_pos[*tok_idx]];
Michal Vaskoe68b5402022-07-29 14:58:15 +02008749 name_len = exp->tok_len[*tok_idx];
8750 ret = lyxp_vars_find((struct lyxp_var *)vars, name, name_len, &var);
8751 LY_CHECK_ERR_RET(ret, LOGERR(set->ctx, ret, "XPath variable \"%.*s\" not defined.", (int)name_len, name), ret);
aPiecekfba75362021-10-07 12:39:48 +02008752
Michal Vasko49fec8e2022-05-24 10:28:33 +02008753 /* parse value */
aPiecekfba75362021-10-07 12:39:48 +02008754 ret = lyxp_expr_parse(set->ctx, var->value, 0, 1, &tokens);
8755 LY_CHECK_GOTO(ret, cleanup);
8756
Michal Vasko49fec8e2022-05-24 10:28:33 +02008757 /* evaluate value */
aPiecekfba75362021-10-07 12:39:48 +02008758 token_index = 0;
8759 ret = eval_expr_select(tokens, &token_index, 0, set, options);
8760 LY_CHECK_GOTO(ret, cleanup);
8761
8762cleanup:
8763 lyxp_expr_free(set->ctx, tokens);
8764
8765 return ret;
8766}
8767
8768/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02008769 * @brief Evaluate PathExpr. Logs directly on error.
8770 *
Michal Vaskod3678892020-05-21 10:06:58 +02008771 * [12] PathExpr ::= LocationPath | PrimaryExpr Predicate*
Michal Vasko03ff5a72019-09-11 13:49:33 +02008772 * | PrimaryExpr Predicate* '/' RelativeLocationPath
8773 * | PrimaryExpr Predicate* '//' RelativeLocationPath
8774 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
aPiecekfba75362021-10-07 12:39:48 +02008775 * [10] PrimaryExpr ::= VariableReference | '(' Expr ')' | Literal | Number | FunctionCall
Michal Vasko03ff5a72019-09-11 13:49:33 +02008776 *
8777 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008778 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02008779 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008780 * @param[in] options XPath options.
8781 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8782 */
8783static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02008784eval_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 +02008785{
Michal Vasko49fec8e2022-05-24 10:28:33 +02008786 ly_bool all_desc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008787 LY_ERR rc;
8788
Michal Vasko004d3152020-06-11 19:59:22 +02008789 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008790 case LYXP_TOKEN_PAR1:
8791 /* '(' Expr ')' */
8792
8793 /* '(' */
aPiecek8b0cc152021-05-31 16:40:31 +02008794 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008795 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008796 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008797
8798 /* Expr */
Michal Vasko004d3152020-06-11 19:59:22 +02008799 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008800 LY_CHECK_RET(rc);
8801
8802 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02008803 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
aPiecek8b0cc152021-05-31 16:40:31 +02008804 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008805 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008806 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008807
Michal Vasko03ff5a72019-09-11 13:49:33 +02008808 goto predicate;
8809
8810 case LYXP_TOKEN_DOT:
8811 case LYXP_TOKEN_DDOT:
Michal Vasko49fec8e2022-05-24 10:28:33 +02008812 case LYXP_TOKEN_AXISNAME:
Michal Vasko03ff5a72019-09-11 13:49:33 +02008813 case LYXP_TOKEN_AT:
8814 case LYXP_TOKEN_NAMETEST:
8815 case LYXP_TOKEN_NODETYPE:
8816 /* RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02008817 rc = eval_relative_location_path(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008818 LY_CHECK_RET(rc);
8819 break;
8820
aPiecekfba75362021-10-07 12:39:48 +02008821 case LYXP_TOKEN_VARREF:
8822 /* VariableReference */
8823 rc = eval_variable_reference(exp, tok_idx, set, options);
8824 LY_CHECK_RET(rc);
8825 ++(*tok_idx);
8826
aPiecekfba75362021-10-07 12:39:48 +02008827 goto predicate;
8828
Michal Vasko03ff5a72019-09-11 13:49:33 +02008829 case LYXP_TOKEN_FUNCNAME:
8830 /* FunctionCall */
aPiecek8b0cc152021-05-31 16:40:31 +02008831 rc = eval_function_call(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008832 LY_CHECK_RET(rc);
8833
Michal Vasko03ff5a72019-09-11 13:49:33 +02008834 goto predicate;
8835
Michal Vasko3e48bf32020-06-01 08:39:07 +02008836 case LYXP_TOKEN_OPER_PATH:
8837 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02008838 /* AbsoluteLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02008839 rc = eval_absolute_location_path(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008840 LY_CHECK_RET(rc);
8841 break;
8842
8843 case LYXP_TOKEN_LITERAL:
8844 /* Literal */
aPiecek8b0cc152021-05-31 16:40:31 +02008845 if ((options & LYXP_SKIP_EXPR) || (options & LYXP_SCNODE_ALL)) {
8846 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008847 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008848 }
Michal Vasko004d3152020-06-11 19:59:22 +02008849 eval_literal(exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008850 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02008851 eval_literal(exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008852 }
8853
Michal Vasko03ff5a72019-09-11 13:49:33 +02008854 goto predicate;
8855
8856 case LYXP_TOKEN_NUMBER:
8857 /* Number */
aPiecek8b0cc152021-05-31 16:40:31 +02008858 if ((options & LYXP_SKIP_EXPR) || (options & LYXP_SCNODE_ALL)) {
8859 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008860 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008861 }
Michal Vasko004d3152020-06-11 19:59:22 +02008862 rc = eval_number(NULL, exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008863 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02008864 rc = eval_number(set->ctx, exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008865 }
8866 LY_CHECK_RET(rc);
8867
Michal Vasko03ff5a72019-09-11 13:49:33 +02008868 goto predicate;
8869
8870 default:
Michal Vasko49fec8e2022-05-24 10:28:33 +02008871 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 +02008872 return LY_EVALID;
8873 }
8874
8875 return LY_SUCCESS;
8876
8877predicate:
8878 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02008879 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
Michal Vasko49fec8e2022-05-24 10:28:33 +02008880 rc = eval_predicate(exp, tok_idx, set, options, LYXP_AXIS_CHILD);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008881 LY_CHECK_RET(rc);
8882 }
8883
8884 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02008885 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008886
8887 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02008888 if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008889 all_desc = 0;
8890 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008891 all_desc = 1;
8892 }
8893
aPiecek8b0cc152021-05-31 16:40:31 +02008894 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008895 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008896 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008897
Michal Vasko004d3152020-06-11 19:59:22 +02008898 rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008899 LY_CHECK_RET(rc);
8900 }
8901
8902 return LY_SUCCESS;
8903}
8904
8905/**
8906 * @brief Evaluate UnionExpr. Logs directly on error.
8907 *
Michal Vaskod3678892020-05-21 10:06:58 +02008908 * [20] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008909 *
8910 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008911 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008912 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008913 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008914 * @param[in] options XPath options.
8915 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8916 */
8917static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02008918eval_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 +02008919{
8920 LY_ERR rc = LY_SUCCESS;
8921 struct lyxp_set orig_set, set2;
Michal Vaskodd528af2022-08-08 14:35:07 +02008922 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008923
8924 assert(repeat);
8925
8926 set_init(&orig_set, set);
8927 set_init(&set2, set);
8928
8929 set_fill_set(&orig_set, set);
8930
Michal Vasko004d3152020-06-11 19:59:22 +02008931 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008932 LY_CHECK_GOTO(rc, cleanup);
8933
8934 /* ('|' PathExpr)* */
8935 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008936 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_UNI);
aPiecek8b0cc152021-05-31 16:40:31 +02008937 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008938 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008939 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008940
aPiecek8b0cc152021-05-31 16:40:31 +02008941 if (options & LYXP_SKIP_EXPR) {
8942 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008943 LY_CHECK_GOTO(rc, cleanup);
8944 continue;
8945 }
8946
8947 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008948 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008949 LY_CHECK_GOTO(rc, cleanup);
8950
8951 /* eval */
8952 if (options & LYXP_SCNODE_ALL) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01008953 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008954 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008955 rc = moveto_union(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008956 LY_CHECK_GOTO(rc, cleanup);
8957 }
8958 }
8959
8960cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008961 lyxp_set_free_content(&orig_set);
8962 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008963 return rc;
8964}
8965
8966/**
8967 * @brief Evaluate UnaryExpr. Logs directly on error.
8968 *
Michal Vaskod3678892020-05-21 10:06:58 +02008969 * [19] UnaryExpr ::= UnionExpr | '-' UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008970 *
8971 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008972 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008973 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008974 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008975 * @param[in] options XPath options.
8976 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8977 */
8978static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02008979eval_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 +02008980{
8981 LY_ERR rc;
Michal Vaskodd528af2022-08-08 14:35:07 +02008982 uint32_t this_op, i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008983
8984 assert(repeat);
8985
8986 /* ('-')+ */
Michal Vasko004d3152020-06-11 19:59:22 +02008987 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008988 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008989 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 +02008990
aPiecek8b0cc152021-05-31 16:40:31 +02008991 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02008992 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008993 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008994 }
8995
Michal Vasko004d3152020-06-11 19:59:22 +02008996 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNARY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008997 LY_CHECK_RET(rc);
8998
aPiecek8b0cc152021-05-31 16:40:31 +02008999 if (!(options & LYXP_SKIP_EXPR) && (repeat % 2)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02009000 if (options & LYXP_SCNODE_ALL) {
9001 warn_operands(set->ctx, set, NULL, 1, exp->expr, exp->tok_pos[this_op]);
9002 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009003 rc = moveto_op_math(set, NULL, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009004 LY_CHECK_RET(rc);
9005 }
9006 }
9007
9008 return LY_SUCCESS;
9009}
9010
9011/**
9012 * @brief Evaluate MultiplicativeExpr. Logs directly on error.
9013 *
Michal Vaskod3678892020-05-21 10:06:58 +02009014 * [18] MultiplicativeExpr ::= UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02009015 * | MultiplicativeExpr '*' UnaryExpr
9016 * | MultiplicativeExpr 'div' UnaryExpr
9017 * | MultiplicativeExpr 'mod' UnaryExpr
9018 *
9019 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02009020 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009021 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02009022 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009023 * @param[in] options XPath options.
9024 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
9025 */
9026static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02009027eval_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 +02009028 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02009029{
9030 LY_ERR rc;
Michal Vaskodd528af2022-08-08 14:35:07 +02009031 uint32_t i, this_op;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009032 struct lyxp_set orig_set, set2;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009033
9034 assert(repeat);
9035
9036 set_init(&orig_set, set);
9037 set_init(&set2, set);
9038
9039 set_fill_set(&orig_set, set);
9040
Michal Vasko004d3152020-06-11 19:59:22 +02009041 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009042 LY_CHECK_GOTO(rc, cleanup);
9043
9044 /* ('*' / 'div' / 'mod' UnaryExpr)* */
9045 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02009046 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009047
Michal Vasko004d3152020-06-11 19:59:22 +02009048 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
aPiecek8b0cc152021-05-31 16:40:31 +02009049 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02009050 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02009051 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009052
aPiecek8b0cc152021-05-31 16:40:31 +02009053 if (options & LYXP_SKIP_EXPR) {
9054 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009055 LY_CHECK_GOTO(rc, cleanup);
9056 continue;
9057 }
9058
9059 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02009060 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009061 LY_CHECK_GOTO(rc, cleanup);
9062
9063 /* eval */
9064 if (options & LYXP_SCNODE_ALL) {
9065 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01009066 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02009067 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009068 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009069 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009070 LY_CHECK_GOTO(rc, cleanup);
9071 }
9072 }
9073
9074cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02009075 lyxp_set_free_content(&orig_set);
9076 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009077 return rc;
9078}
9079
9080/**
9081 * @brief Evaluate AdditiveExpr. Logs directly on error.
9082 *
Michal Vaskod3678892020-05-21 10:06:58 +02009083 * [17] AdditiveExpr ::= MultiplicativeExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02009084 * | AdditiveExpr '+' MultiplicativeExpr
9085 * | AdditiveExpr '-' MultiplicativeExpr
9086 *
9087 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02009088 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009089 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02009090 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009091 * @param[in] options XPath options.
9092 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
9093 */
9094static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02009095eval_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 +02009096{
9097 LY_ERR rc;
Michal Vaskodd528af2022-08-08 14:35:07 +02009098 uint32_t i, this_op;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009099 struct lyxp_set orig_set, set2;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009100
9101 assert(repeat);
9102
9103 set_init(&orig_set, set);
9104 set_init(&set2, set);
9105
9106 set_fill_set(&orig_set, set);
9107
Michal Vasko004d3152020-06-11 19:59:22 +02009108 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009109 LY_CHECK_GOTO(rc, cleanup);
9110
9111 /* ('+' / '-' MultiplicativeExpr)* */
9112 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02009113 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009114
Michal Vasko004d3152020-06-11 19:59:22 +02009115 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009116 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02009117 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02009118 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009119
aPiecek8b0cc152021-05-31 16:40:31 +02009120 if (options & LYXP_SKIP_EXPR) {
9121 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009122 LY_CHECK_GOTO(rc, cleanup);
9123 continue;
9124 }
9125
9126 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02009127 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009128 LY_CHECK_GOTO(rc, cleanup);
9129
9130 /* eval */
9131 if (options & LYXP_SCNODE_ALL) {
9132 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01009133 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02009134 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009135 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009136 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009137 LY_CHECK_GOTO(rc, cleanup);
9138 }
9139 }
9140
9141cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02009142 lyxp_set_free_content(&orig_set);
9143 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009144 return rc;
9145}
9146
9147/**
9148 * @brief Evaluate RelationalExpr. Logs directly on error.
9149 *
Michal Vaskod3678892020-05-21 10:06:58 +02009150 * [16] RelationalExpr ::= AdditiveExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02009151 * | RelationalExpr '<' AdditiveExpr
9152 * | RelationalExpr '>' AdditiveExpr
9153 * | RelationalExpr '<=' AdditiveExpr
9154 * | RelationalExpr '>=' AdditiveExpr
9155 *
9156 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02009157 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009158 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02009159 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009160 * @param[in] options XPath options.
9161 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
9162 */
9163static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02009164eval_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 +02009165{
9166 LY_ERR rc;
Michal Vaskodd528af2022-08-08 14:35:07 +02009167 uint32_t i, this_op;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009168 struct lyxp_set orig_set, set2;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009169
9170 assert(repeat);
9171
9172 set_init(&orig_set, set);
9173 set_init(&set2, set);
9174
9175 set_fill_set(&orig_set, set);
9176
Michal Vasko004d3152020-06-11 19:59:22 +02009177 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009178 LY_CHECK_GOTO(rc, cleanup);
9179
9180 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
9181 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02009182 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009183
Michal Vasko004d3152020-06-11 19:59:22 +02009184 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_COMP);
aPiecek8b0cc152021-05-31 16:40:31 +02009185 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02009186 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02009187 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009188
aPiecek8b0cc152021-05-31 16:40:31 +02009189 if (options & LYXP_SKIP_EXPR) {
9190 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009191 LY_CHECK_GOTO(rc, cleanup);
9192 continue;
9193 }
9194
9195 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02009196 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009197 LY_CHECK_GOTO(rc, cleanup);
9198
9199 /* eval */
9200 if (options & LYXP_SCNODE_ALL) {
9201 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01009202 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02009203 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009204 } else {
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02009205 ly_bool result;
9206
9207 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], &result);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009208 LY_CHECK_GOTO(rc, cleanup);
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02009209 set_fill_boolean(set, result);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009210 }
9211 }
9212
9213cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02009214 lyxp_set_free_content(&orig_set);
9215 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009216 return rc;
9217}
9218
9219/**
9220 * @brief Evaluate EqualityExpr. Logs directly on error.
9221 *
Michal Vaskod3678892020-05-21 10:06:58 +02009222 * [15] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02009223 * | EqualityExpr '!=' RelationalExpr
9224 *
9225 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02009226 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009227 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02009228 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009229 * @param[in] options XPath options.
9230 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
9231 */
9232static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02009233eval_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 +02009234{
9235 LY_ERR rc;
Michal Vaskodd528af2022-08-08 14:35:07 +02009236 uint32_t i, this_op;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009237 struct lyxp_set orig_set, set2;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009238
9239 assert(repeat);
9240
9241 set_init(&orig_set, set);
9242 set_init(&set2, set);
9243
9244 set_fill_set(&orig_set, set);
9245
Michal Vasko004d3152020-06-11 19:59:22 +02009246 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009247 LY_CHECK_GOTO(rc, cleanup);
9248
9249 /* ('=' / '!=' RelationalExpr)* */
9250 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02009251 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009252
Michal Vasko004d3152020-06-11 19:59:22 +02009253 assert((exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_EQUAL) || (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_NEQUAL));
aPiecek8b0cc152021-05-31 16:40:31 +02009254 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02009255 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02009256 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009257
aPiecek8b0cc152021-05-31 16:40:31 +02009258 if (options & LYXP_SKIP_EXPR) {
9259 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009260 LY_CHECK_GOTO(rc, cleanup);
9261 continue;
9262 }
9263
9264 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02009265 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009266 LY_CHECK_GOTO(rc, cleanup);
9267
9268 /* eval */
9269 if (options & LYXP_SCNODE_ALL) {
9270 warn_operands(set->ctx, set, &set2, 0, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vasko004d3152020-06-11 19:59:22 +02009271 warn_equality_value(exp, set, *tok_idx - 1, this_op - 1, *tok_idx - 1);
9272 warn_equality_value(exp, &set2, this_op - 1, this_op - 1, *tok_idx - 1);
Michal Vaskoecd62de2019-11-13 12:35:11 +01009273 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02009274 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009275 } else {
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02009276 ly_bool result;
9277
9278 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], &result);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009279 LY_CHECK_GOTO(rc, cleanup);
Haithem Ben Ghorbal1ac219d2022-09-07 12:30:06 +02009280 set_fill_boolean(set, result);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009281 }
9282 }
9283
9284cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02009285 lyxp_set_free_content(&orig_set);
9286 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009287 return rc;
9288}
9289
9290/**
9291 * @brief Evaluate AndExpr. Logs directly on error.
9292 *
Michal Vaskod3678892020-05-21 10:06:58 +02009293 * [14] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02009294 *
9295 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02009296 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009297 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02009298 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009299 * @param[in] options XPath options.
9300 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
9301 */
9302static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02009303eval_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 +02009304{
9305 LY_ERR rc;
9306 struct lyxp_set orig_set, set2;
Michal Vaskodd528af2022-08-08 14:35:07 +02009307 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009308
9309 assert(repeat);
9310
9311 set_init(&orig_set, set);
9312 set_init(&set2, set);
9313
9314 set_fill_set(&orig_set, set);
9315
Michal Vasko004d3152020-06-11 19:59:22 +02009316 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009317 LY_CHECK_GOTO(rc, cleanup);
9318
9319 /* cast to boolean, we know that will be the final result */
aPiecek8b0cc152021-05-31 16:40:31 +02009320 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02009321 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009322 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009323 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009324 }
9325
9326 /* ('and' EqualityExpr)* */
9327 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02009328 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
aPiecek8b0cc152021-05-31 16:40:31 +02009329 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, ((options & LYXP_SKIP_EXPR) || !set->val.bln ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02009330 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02009331 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009332
9333 /* lazy evaluation */
aPiecek8b0cc152021-05-31 16:40:31 +02009334 if ((options & LYXP_SKIP_EXPR) || ((set->type == LYXP_SET_BOOLEAN) && !set->val.bln)) {
9335 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009336 LY_CHECK_GOTO(rc, cleanup);
9337 continue;
9338 }
9339
9340 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02009341 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009342 LY_CHECK_GOTO(rc, cleanup);
9343
9344 /* eval - just get boolean value actually */
9345 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vasko1a09b212021-05-06 13:00:10 +02009346 set_scnode_clear_ctx(&set2, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskoecd62de2019-11-13 12:35:11 +01009347 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009348 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009349 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009350 set_fill_set(set, &set2);
9351 }
9352 }
9353
9354cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02009355 lyxp_set_free_content(&orig_set);
9356 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009357 return rc;
9358}
9359
9360/**
9361 * @brief Evaluate OrExpr. Logs directly on error.
9362 *
Michal Vaskod3678892020-05-21 10:06:58 +02009363 * [13] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02009364 *
9365 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02009366 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009367 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02009368 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009369 * @param[in] options XPath options.
9370 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
9371 */
9372static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02009373eval_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 +02009374{
9375 LY_ERR rc;
9376 struct lyxp_set orig_set, set2;
Michal Vaskodd528af2022-08-08 14:35:07 +02009377 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009378
9379 assert(repeat);
9380
9381 set_init(&orig_set, set);
9382 set_init(&set2, set);
9383
9384 set_fill_set(&orig_set, set);
9385
Michal Vasko004d3152020-06-11 19:59:22 +02009386 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009387 LY_CHECK_GOTO(rc, cleanup);
9388
9389 /* cast to boolean, we know that will be the final result */
aPiecek8b0cc152021-05-31 16:40:31 +02009390 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02009391 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009392 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009393 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009394 }
9395
9396 /* ('or' AndExpr)* */
9397 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02009398 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
aPiecek8b0cc152021-05-31 16:40:31 +02009399 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, ((options & LYXP_SKIP_EXPR) || set->val.bln ? "skipped" : "parsed"),
Michal Vasko49fec8e2022-05-24 10:28:33 +02009400 lyxp_token2str(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02009401 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009402
9403 /* lazy evaluation */
aPiecek8b0cc152021-05-31 16:40:31 +02009404 if ((options & LYXP_SKIP_EXPR) || ((set->type == LYXP_SET_BOOLEAN) && set->val.bln)) {
9405 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009406 LY_CHECK_GOTO(rc, cleanup);
9407 continue;
9408 }
9409
9410 set_fill_set(&set2, &orig_set);
9411 /* expr_type cound have been LYXP_EXPR_NONE in all these later calls (except for the first one),
9412 * but it does not matter */
Michal Vasko004d3152020-06-11 19:59:22 +02009413 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009414 LY_CHECK_GOTO(rc, cleanup);
9415
9416 /* eval - just get boolean value actually */
9417 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vasko1a09b212021-05-06 13:00:10 +02009418 set_scnode_clear_ctx(&set2, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskoecd62de2019-11-13 12:35:11 +01009419 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009420 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009421 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009422 set_fill_set(set, &set2);
9423 }
9424 }
9425
9426cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02009427 lyxp_set_free_content(&orig_set);
9428 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009429 return rc;
9430}
9431
9432/**
Michal Vasko004d3152020-06-11 19:59:22 +02009433 * @brief Decide what expression is at the pointer @p tok_idx and evaluate it accordingly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009434 *
9435 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02009436 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009437 * @param[in] etype Expression type to evaluate.
aPiecek8b0cc152021-05-31 16:40:31 +02009438 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02009439 * @param[in] options XPath options.
9440 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
9441 */
9442static LY_ERR
Michal Vaskodd528af2022-08-08 14:35:07 +02009443eval_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 +02009444 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02009445{
Michal Vaskodd528af2022-08-08 14:35:07 +02009446 uint32_t i, count;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009447 enum lyxp_expr_type next_etype;
9448 LY_ERR rc;
9449
9450 /* process operator repeats */
Michal Vasko004d3152020-06-11 19:59:22 +02009451 if (!exp->repeat[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02009452 next_etype = LYXP_EXPR_NONE;
9453 } else {
9454 /* find etype repeat */
Radek Krejci1e008d22020-08-17 11:37:37 +02009455 for (i = 0; exp->repeat[*tok_idx][i] > etype; ++i) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02009456
9457 /* select one-priority lower because etype expression called us */
9458 if (i) {
Michal Vasko004d3152020-06-11 19:59:22 +02009459 next_etype = exp->repeat[*tok_idx][i - 1];
Michal Vasko03ff5a72019-09-11 13:49:33 +02009460 /* count repeats for that expression */
Radek Krejci1e008d22020-08-17 11:37:37 +02009461 for (count = 0; i && exp->repeat[*tok_idx][i - 1] == next_etype; ++count, --i) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02009462 } else {
9463 next_etype = LYXP_EXPR_NONE;
9464 }
9465 }
9466
9467 /* decide what expression are we parsing based on the repeat */
9468 switch (next_etype) {
9469 case LYXP_EXPR_OR:
Michal Vasko004d3152020-06-11 19:59:22 +02009470 rc = eval_or_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009471 break;
9472 case LYXP_EXPR_AND:
Michal Vasko004d3152020-06-11 19:59:22 +02009473 rc = eval_and_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009474 break;
9475 case LYXP_EXPR_EQUALITY:
Michal Vasko004d3152020-06-11 19:59:22 +02009476 rc = eval_equality_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009477 break;
9478 case LYXP_EXPR_RELATIONAL:
Michal Vasko004d3152020-06-11 19:59:22 +02009479 rc = eval_relational_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009480 break;
9481 case LYXP_EXPR_ADDITIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02009482 rc = eval_additive_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009483 break;
9484 case LYXP_EXPR_MULTIPLICATIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02009485 rc = eval_multiplicative_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009486 break;
9487 case LYXP_EXPR_UNARY:
Michal Vasko004d3152020-06-11 19:59:22 +02009488 rc = eval_unary_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009489 break;
9490 case LYXP_EXPR_UNION:
Michal Vasko004d3152020-06-11 19:59:22 +02009491 rc = eval_union_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009492 break;
9493 case LYXP_EXPR_NONE:
Michal Vasko004d3152020-06-11 19:59:22 +02009494 rc = eval_path_expr(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009495 break;
9496 default:
9497 LOGINT_RET(set->ctx);
9498 }
9499
9500 return rc;
9501}
9502
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009503/**
9504 * @brief Get root type.
9505 *
9506 * @param[in] ctx_node Context node.
9507 * @param[in] ctx_scnode Schema context node.
9508 * @param[in] options XPath options.
9509 * @return Root type.
9510 */
9511static enum lyxp_node_type
Radek Krejci1deb5be2020-08-26 16:43:36 +02009512lyxp_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 +01009513{
Michal Vasko6b26e742020-07-17 15:02:10 +02009514 const struct lysc_node *op;
9515
Michal Vaskoa27245c2022-05-02 09:01:35 +02009516 /* explicit */
9517 if (options & LYXP_ACCESS_TREE_ALL) {
9518 return LYXP_NODE_ROOT;
9519 } else if (options & LYXP_ACCESS_TREE_CONFIG) {
9520 return LYXP_NODE_ROOT_CONFIG;
9521 }
9522
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009523 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009524 /* schema */
Radek Krejci1e008d22020-08-17 11:37:37 +02009525 for (op = ctx_scnode; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02009526
9527 if (op || (options & LYXP_SCNODE)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009528 /* general root that can access everything */
9529 return LYXP_NODE_ROOT;
9530 } else if (!ctx_scnode || (ctx_scnode->flags & LYS_CONFIG_W)) {
9531 /* root context node can access only config data (because we said so, it is unspecified) */
9532 return LYXP_NODE_ROOT_CONFIG;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009533 }
Michal Vasko6b26e742020-07-17 15:02:10 +02009534 return LYXP_NODE_ROOT;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009535 }
9536
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009537 /* data */
Michal Vasko6b26e742020-07-17 15:02:10 +02009538 op = ctx_node ? ctx_node->schema : NULL;
Michal Vaskod989ba02020-08-24 10:59:24 +02009539 for ( ; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02009540
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009541 if (op || !(options & LYXP_SCHEMA)) {
9542 /* general root that can access everything */
9543 return LYXP_NODE_ROOT;
Christian Hoppsb6ecaea2021-02-06 09:45:38 -05009544 } else if (!ctx_node || !ctx_node->schema || (ctx_node->schema->flags & LYS_CONFIG_W)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009545 /* root context node can access only config data (because we said so, it is unspecified) */
9546 return LYXP_NODE_ROOT_CONFIG;
9547 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009548 return LYXP_NODE_ROOT;
9549}
9550
Michal Vasko03ff5a72019-09-11 13:49:33 +02009551LY_ERR
Michal Vasko400e9672021-01-11 13:39:17 +01009552lyxp_eval(const struct ly_ctx *ctx, const struct lyxp_expr *exp, const struct lys_module *cur_mod,
Michal Vaskoa3e92bc2022-07-29 14:56:23 +02009553 LY_VALUE_FORMAT format, void *prefix_data, const struct lyd_node *cur_node, const struct lyd_node *ctx_node,
9554 const struct lyd_node *tree, const struct lyxp_var *vars, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02009555{
Michal Vaskodd528af2022-08-08 14:35:07 +02009556 uint32_t tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009557 LY_ERR rc;
9558
Michal Vasko400e9672021-01-11 13:39:17 +01009559 LY_CHECK_ARG_RET(ctx, ctx, exp, set, LY_EINVAL);
Radek Krejci8df109d2021-04-23 12:19:08 +02009560 if (!cur_mod && ((format == LY_VALUE_SCHEMA) || (format == LY_VALUE_SCHEMA_RESOLVED))) {
Michal Vaskoddd76592022-01-17 13:34:48 +01009561 LOGERR(ctx, LY_EINVAL, "Current module must be set if schema format is used.");
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009562 return LY_EINVAL;
Michal Vasko004d3152020-06-11 19:59:22 +02009563 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02009564
Michal Vaskod3bb12f2020-12-04 14:33:09 +01009565 if (tree) {
9566 /* adjust the pointer to be the first top-level sibling */
9567 while (tree->parent) {
9568 tree = lyd_parent(tree);
9569 }
9570 tree = lyd_first_sibling(tree);
Michal Vaskoddd76592022-01-17 13:34:48 +01009571
Michal Vasko6f78a772022-11-10 08:13:52 +01009572 if (lysc_data_parent(tree->schema)) {
Michal Vaskoddd76592022-01-17 13:34:48 +01009573 /* unable to evaluate absolute paths */
9574 LOGERR(ctx, LY_EINVAL, "Data node \"%s\" has no parent but is not instance of a top-level schema node.",
9575 LYD_NAME(tree));
9576 return LY_EINVAL;
9577 }
Michal Vaskod3bb12f2020-12-04 14:33:09 +01009578 }
9579
Michal Vasko03ff5a72019-09-11 13:49:33 +02009580 /* prepare set for evaluation */
Michal Vasko03ff5a72019-09-11 13:49:33 +02009581 memset(set, 0, sizeof *set);
Michal Vaskod3678892020-05-21 10:06:58 +02009582 set->type = LYXP_SET_NODE_SET;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009583 set->root_type = lyxp_get_root_type(ctx_node, NULL, options);
9584 set_insert_node(set, (struct lyd_node *)ctx_node, 0, ctx_node ? LYXP_NODE_ELEM : set->root_type, 0);
9585
Michal Vasko400e9672021-01-11 13:39:17 +01009586 set->ctx = (struct ly_ctx *)ctx;
Michal Vaskoa3e92bc2022-07-29 14:56:23 +02009587 set->cur_node = cur_node;
9588 for (set->context_op = cur_node ? cur_node->schema : NULL;
Radek Krejci0f969882020-08-21 16:56:47 +02009589 set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF));
9590 set->context_op = set->context_op->parent) {}
Michal Vaskof03ed032020-03-04 13:31:44 +01009591 set->tree = tree;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009592 set->cur_mod = cur_mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009593 set->format = format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009594 set->prefix_data = prefix_data;
aPiecekfba75362021-10-07 12:39:48 +02009595 set->vars = vars;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009596
Radek Krejciddace2c2021-01-08 11:30:56 +01009597 LOG_LOCSET(NULL, set->cur_node, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01009598
Michal Vasko03ff5a72019-09-11 13:49:33 +02009599 /* evaluate */
Michal Vasko004d3152020-06-11 19:59:22 +02009600 rc = eval_expr_select(exp, &tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009601 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02009602 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009603 }
9604
Michal Vasko4a7d4d62021-12-13 17:05:06 +01009605 if (set->cur_node) {
9606 LOG_LOCBACK(0, 1, 0, 0);
9607 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02009608 return rc;
9609}
9610
9611#if 0
9612
9613/* full xml printing of set elements, not used currently */
9614
9615void
9616lyxp_set_print_xml(FILE *f, struct lyxp_set *set)
9617{
9618 uint32_t i;
9619 char *str_num;
9620 struct lyout out;
9621
9622 memset(&out, 0, sizeof out);
9623
9624 out.type = LYOUT_STREAM;
9625 out.method.f = f;
9626
9627 switch (set->type) {
9628 case LYXP_SET_EMPTY:
Michal Vasko5233e962020-08-14 14:26:20 +02009629 ly_print_(&out, "Empty XPath set\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009630 break;
9631 case LYXP_SET_BOOLEAN:
Michal Vasko5233e962020-08-14 14:26:20 +02009632 ly_print_(&out, "Boolean XPath set:\n");
9633 ly_print_(&out, "%s\n\n", set->value.bool ? "true" : "false");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009634 break;
9635 case LYXP_SET_STRING:
Michal Vasko5233e962020-08-14 14:26:20 +02009636 ly_print_(&out, "String XPath set:\n");
9637 ly_print_(&out, "\"%s\"\n\n", set->value.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009638 break;
9639 case LYXP_SET_NUMBER:
Michal Vasko5233e962020-08-14 14:26:20 +02009640 ly_print_(&out, "Number XPath set:\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009641
9642 if (isnan(set->value.num)) {
9643 str_num = strdup("NaN");
9644 } else if ((set->value.num == 0) || (set->value.num == -0.0f)) {
9645 str_num = strdup("0");
9646 } else if (isinf(set->value.num) && !signbit(set->value.num)) {
9647 str_num = strdup("Infinity");
9648 } else if (isinf(set->value.num) && signbit(set->value.num)) {
9649 str_num = strdup("-Infinity");
9650 } else if ((long long)set->value.num == set->value.num) {
9651 if (asprintf(&str_num, "%lld", (long long)set->value.num) == -1) {
9652 str_num = NULL;
9653 }
9654 } else {
9655 if (asprintf(&str_num, "%03.1Lf", set->value.num) == -1) {
9656 str_num = NULL;
9657 }
9658 }
9659 if (!str_num) {
9660 LOGMEM;
9661 return;
9662 }
Michal Vasko5233e962020-08-14 14:26:20 +02009663 ly_print_(&out, "%s\n\n", str_num);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009664 free(str_num);
9665 break;
9666 case LYXP_SET_NODE_SET:
Michal Vasko5233e962020-08-14 14:26:20 +02009667 ly_print_(&out, "Node XPath set:\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009668
9669 for (i = 0; i < set->used; ++i) {
Michal Vasko5233e962020-08-14 14:26:20 +02009670 ly_print_(&out, "%d. ", i + 1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009671 switch (set->node_type[i]) {
9672 case LYXP_NODE_ROOT_ALL:
Michal Vasko5233e962020-08-14 14:26:20 +02009673 ly_print_(&out, "ROOT all\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009674 break;
9675 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko5233e962020-08-14 14:26:20 +02009676 ly_print_(&out, "ROOT config\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009677 break;
9678 case LYXP_NODE_ROOT_STATE:
Michal Vasko5233e962020-08-14 14:26:20 +02009679 ly_print_(&out, "ROOT state\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009680 break;
9681 case LYXP_NODE_ROOT_NOTIF:
Michal Vasko5233e962020-08-14 14:26:20 +02009682 ly_print_(&out, "ROOT notification \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009683 break;
9684 case LYXP_NODE_ROOT_RPC:
Michal Vasko5233e962020-08-14 14:26:20 +02009685 ly_print_(&out, "ROOT rpc \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009686 break;
9687 case LYXP_NODE_ROOT_OUTPUT:
Michal Vasko5233e962020-08-14 14:26:20 +02009688 ly_print_(&out, "ROOT output \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009689 break;
9690 case LYXP_NODE_ELEM:
Michal Vasko5233e962020-08-14 14:26:20 +02009691 ly_print_(&out, "ELEM \"%s\"\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009692 xml_print_node(&out, 1, set->value.nodes[i], 1, LYP_FORMAT);
Michal Vasko5233e962020-08-14 14:26:20 +02009693 ly_print_(&out, "\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02009694 break;
9695 case LYXP_NODE_TEXT:
Michal Vasko5233e962020-08-14 14:26:20 +02009696 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 +02009697 break;
9698 case LYXP_NODE_ATTR:
Michal Vasko5233e962020-08-14 14:26:20 +02009699 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 +02009700 break;
9701 }
9702 }
9703 break;
9704 }
9705}
9706
9707#endif
9708
9709LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009710lyxp_set_cast(struct lyxp_set *set, enum lyxp_set_type target)
Michal Vasko03ff5a72019-09-11 13:49:33 +02009711{
9712 long double num;
9713 char *str;
9714 LY_ERR rc;
9715
9716 if (!set || (set->type == target)) {
9717 return LY_SUCCESS;
9718 }
9719
9720 /* it's not possible to convert anything into a node set */
Michal Vaskod3678892020-05-21 10:06:58 +02009721 assert(target != LYXP_SET_NODE_SET);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009722
9723 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vaskod3678892020-05-21 10:06:58 +02009724 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009725 return LY_EINVAL;
9726 }
9727
9728 /* to STRING */
Michal Vaskod3678892020-05-21 10:06:58 +02009729 if ((target == LYXP_SET_STRING) || ((target == LYXP_SET_NUMBER) && (set->type == LYXP_SET_NODE_SET))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02009730 switch (set->type) {
9731 case LYXP_SET_NUMBER:
9732 if (isnan(set->val.num)) {
9733 set->val.str = strdup("NaN");
9734 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
9735 } else if ((set->val.num == 0) || (set->val.num == -0.0f)) {
9736 set->val.str = strdup("0");
9737 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
9738 } else if (isinf(set->val.num) && !signbit(set->val.num)) {
9739 set->val.str = strdup("Infinity");
9740 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
9741 } else if (isinf(set->val.num) && signbit(set->val.num)) {
9742 set->val.str = strdup("-Infinity");
9743 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
9744 } else if ((long long)set->val.num == set->val.num) {
9745 if (asprintf(&str, "%lld", (long long)set->val.num) == -1) {
9746 LOGMEM_RET(set->ctx);
9747 }
9748 set->val.str = str;
9749 } else {
9750 if (asprintf(&str, "%03.1Lf", set->val.num) == -1) {
9751 LOGMEM_RET(set->ctx);
9752 }
9753 set->val.str = str;
9754 }
9755 break;
9756 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02009757 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02009758 set->val.str = strdup("true");
9759 } else {
9760 set->val.str = strdup("false");
9761 }
9762 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM);
9763 break;
9764 case LYXP_SET_NODE_SET:
Michal Vasko03ff5a72019-09-11 13:49:33 +02009765 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009766 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02009767
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01009768 rc = cast_node_set_to_string(set, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009769 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02009770 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009771 set->val.str = str;
9772 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009773 default:
9774 LOGINT_RET(set->ctx);
9775 }
9776 set->type = LYXP_SET_STRING;
9777 }
9778
9779 /* to NUMBER */
9780 if (target == LYXP_SET_NUMBER) {
9781 switch (set->type) {
9782 case LYXP_SET_STRING:
9783 num = cast_string_to_number(set->val.str);
Michal Vaskod3678892020-05-21 10:06:58 +02009784 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02009785 set->val.num = num;
9786 break;
9787 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02009788 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02009789 set->val.num = 1;
9790 } else {
9791 set->val.num = 0;
9792 }
9793 break;
9794 default:
9795 LOGINT_RET(set->ctx);
9796 }
9797 set->type = LYXP_SET_NUMBER;
9798 }
9799
9800 /* to BOOLEAN */
9801 if (target == LYXP_SET_BOOLEAN) {
9802 switch (set->type) {
9803 case LYXP_SET_NUMBER:
9804 if ((set->val.num == 0) || (set->val.num == -0.0f) || isnan(set->val.num)) {
Michal Vasko004d3152020-06-11 19:59:22 +02009805 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009806 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02009807 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009808 }
9809 break;
9810 case LYXP_SET_STRING:
9811 if (set->val.str[0]) {
Michal Vaskod3678892020-05-21 10:06:58 +02009812 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02009813 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009814 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02009815 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02009816 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009817 }
9818 break;
9819 case LYXP_SET_NODE_SET:
Michal Vaskod3678892020-05-21 10:06:58 +02009820 if (set->used) {
9821 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02009822 set->val.bln = 1;
Michal Vaskod3678892020-05-21 10:06:58 +02009823 } else {
9824 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02009825 set->val.bln = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02009826 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02009827 break;
9828 default:
9829 LOGINT_RET(set->ctx);
9830 }
9831 set->type = LYXP_SET_BOOLEAN;
9832 }
9833
Michal Vasko03ff5a72019-09-11 13:49:33 +02009834 return LY_SUCCESS;
9835}
9836
9837LY_ERR
Michal Vasko400e9672021-01-11 13:39:17 +01009838lyxp_atomize(const struct ly_ctx *ctx, const struct lyxp_expr *exp, const struct lys_module *cur_mod,
Michal Vaskoa3e92bc2022-07-29 14:56:23 +02009839 LY_VALUE_FORMAT format, void *prefix_data, const struct lysc_node *cur_scnode,
9840 const struct lysc_node *ctx_scnode, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02009841{
Radek Krejci2efc45b2020-12-22 16:25:44 +01009842 LY_ERR ret;
Michal Vaskodd528af2022-08-08 14:35:07 +02009843 uint32_t tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009844
Michal Vasko400e9672021-01-11 13:39:17 +01009845 LY_CHECK_ARG_RET(ctx, ctx, exp, set, LY_EINVAL);
Radek Krejci8df109d2021-04-23 12:19:08 +02009846 if (!cur_mod && ((format == LY_VALUE_SCHEMA) || (format == LY_VALUE_SCHEMA_RESOLVED))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009847 LOGARG(NULL, "Current module must be set if schema format is used.");
9848 return LY_EINVAL;
Michal Vasko004d3152020-06-11 19:59:22 +02009849 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02009850
9851 /* prepare set for evaluation */
Michal Vasko03ff5a72019-09-11 13:49:33 +02009852 memset(set, 0, sizeof *set);
9853 set->type = LYXP_SET_SCNODE_SET;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009854 set->root_type = lyxp_get_root_type(NULL, ctx_scnode, options);
Michal Vasko7333cb32022-07-29 16:30:29 +02009855 LY_CHECK_RET(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 +01009856 set->val.scnodes[0].in_ctx = LYXP_SET_SCNODE_START;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009857
Michal Vasko400e9672021-01-11 13:39:17 +01009858 set->ctx = (struct ly_ctx *)ctx;
Michal Vaskoa3e92bc2022-07-29 14:56:23 +02009859 set->cur_scnode = cur_scnode;
9860 for (set->context_op = cur_scnode;
Radek Krejci0f969882020-08-21 16:56:47 +02009861 set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF));
9862 set->context_op = set->context_op->parent) {}
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009863 set->cur_mod = cur_mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009864 set->format = format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02009865 set->prefix_data = prefix_data;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009866
Radek Krejciddace2c2021-01-08 11:30:56 +01009867 LOG_LOCSET(set->cur_scnode, NULL, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01009868
Michal Vasko03ff5a72019-09-11 13:49:33 +02009869 /* evaluate */
Radek Krejci2efc45b2020-12-22 16:25:44 +01009870 ret = eval_expr_select(exp, &tok_idx, 0, set, options);
9871
Michal Vaskof69e4552022-12-02 10:27:42 +01009872 LOG_LOCBACK(set->cur_scnode ? 1 : 0, 0, 0, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +01009873 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02009874}
Michal Vaskod43d71a2020-08-07 14:54:58 +02009875
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01009876LIBYANG_API_DEF const char *
Michal Vaskod43d71a2020-08-07 14:54:58 +02009877lyxp_get_expr(const struct lyxp_expr *path)
9878{
9879 if (!path) {
9880 return NULL;
9881 }
9882
9883 return path->expr;
9884}