blob: 5375431a9bfc85df82a5e6e12e90999a1453d0b0 [file] [log] [blame]
Radek Krejcib1646a92018-11-02 16:08:26 +01001/**
2 * @file xpath.c
3 * @author Michal Vasko <mvasko@cesnet.cz>
4 * @brief YANG XPath evaluation functions
5 *
Michal Vasko61ac2f62020-05-25 12:39:51 +02006 * Copyright (c) 2015 - 2020 CESNET, z.s.p.o.
Radek Krejcib1646a92018-11-02 16:08:26 +01007 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
Christian Hopps32874e12021-05-01 09:43:54 -040014#define _GNU_SOURCE /* asprintf, strdup */
Radek Krejcib1646a92018-11-02 16:08:26 +010015
Radek Krejci535ea9f2020-05-29 16:01:05 +020016#include "xpath.h"
Radek Krejcib1646a92018-11-02 16:08:26 +010017
Radek Krejci535ea9f2020-05-29 16:01:05 +020018#include <assert.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010019#include <ctype.h>
Radek Krejci535ea9f2020-05-29 16:01:05 +020020#include <errno.h>
Radek Krejci535ea9f2020-05-29 16:01:05 +020021#include <math.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020022#include <stdint.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010023#include <stdio.h>
24#include <stdlib.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010025#include <string.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010026
Radek Krejci535ea9f2020-05-29 16:01:05 +020027#include "common.h"
Michal Vasko5aa44c02020-06-29 11:47:02 +020028#include "compat.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020029#include "context.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020030#include "dict.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020031#include "hash_table.h"
Radek Krejci47fab892020-11-05 17:02:41 +010032#include "out.h"
Radek Krejci7931b192020-06-25 17:05:03 +020033#include "parser_data.h"
Michal Vasko004d3152020-06-11 19:59:22 +020034#include "path.h"
Michal Vasko03ff5a72019-09-11 13:49:33 +020035#include "plugins_types.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020036#include "printer_data.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020037#include "schema_compile_node.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020038#include "tree.h"
Radek Krejci77114102021-03-10 15:21:57 +010039#include "tree_data.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020040#include "tree_data_internal.h"
Radek Krejci859a15a2021-03-05 20:56:59 +010041#include "tree_edit.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020042#include "tree_schema_internal.h"
43#include "xml.h"
Michal Vasko03ff5a72019-09-11 13:49:33 +020044
aPiecekbf968d92021-05-27 14:35:05 +020045static LY_ERR reparse_or_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth);
Michal Vasko40308e72020-10-20 16:38:40 +020046static LY_ERR eval_expr_select(const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_expr_type etype,
47 struct lyxp_set *set, uint32_t options);
Michal Vasko93923692021-05-07 15:28:02 +020048static LY_ERR moveto_resolve_model(const char **qname, uint16_t *qname_len, const struct lyxp_set *set,
49 const struct lysc_node *ctx_scnode, const struct lys_module **moveto_mod);
Michal Vasko03ff5a72019-09-11 13:49:33 +020050
51/**
52 * @brief Print the type of an XPath \p set.
53 *
54 * @param[in] set Set to use.
55 * @return Set type string.
56 */
57static const char *
58print_set_type(struct lyxp_set *set)
59{
60 switch (set->type) {
Michal Vasko03ff5a72019-09-11 13:49:33 +020061 case LYXP_SET_NODE_SET:
62 return "node set";
63 case LYXP_SET_SCNODE_SET:
64 return "schema node set";
65 case LYXP_SET_BOOLEAN:
66 return "boolean";
67 case LYXP_SET_NUMBER:
68 return "number";
69 case LYXP_SET_STRING:
70 return "string";
71 }
72
73 return NULL;
74}
75
Michal Vasko24cddf82020-06-01 08:17:01 +020076const char *
77lyxp_print_token(enum lyxp_token tok)
Michal Vasko03ff5a72019-09-11 13:49:33 +020078{
79 switch (tok) {
80 case LYXP_TOKEN_PAR1:
81 return "(";
82 case LYXP_TOKEN_PAR2:
83 return ")";
84 case LYXP_TOKEN_BRACK1:
85 return "[";
86 case LYXP_TOKEN_BRACK2:
87 return "]";
88 case LYXP_TOKEN_DOT:
89 return ".";
90 case LYXP_TOKEN_DDOT:
91 return "..";
92 case LYXP_TOKEN_AT:
93 return "@";
94 case LYXP_TOKEN_COMMA:
95 return ",";
96 case LYXP_TOKEN_NAMETEST:
97 return "NameTest";
98 case LYXP_TOKEN_NODETYPE:
99 return "NodeType";
100 case LYXP_TOKEN_FUNCNAME:
101 return "FunctionName";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200102 case LYXP_TOKEN_OPER_LOG:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200103 return "Operator(Logic)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200104 case LYXP_TOKEN_OPER_EQUAL:
105 return "Operator(Equal)";
106 case LYXP_TOKEN_OPER_NEQUAL:
107 return "Operator(Non-equal)";
108 case LYXP_TOKEN_OPER_COMP:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200109 return "Operator(Comparison)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200110 case LYXP_TOKEN_OPER_MATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200111 return "Operator(Math)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200112 case LYXP_TOKEN_OPER_UNI:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200113 return "Operator(Union)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200114 case LYXP_TOKEN_OPER_PATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200115 return "Operator(Path)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200116 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko14676352020-05-29 11:35:55 +0200117 return "Operator(Recursive Path)";
Michal Vasko03ff5a72019-09-11 13:49:33 +0200118 case LYXP_TOKEN_LITERAL:
119 return "Literal";
120 case LYXP_TOKEN_NUMBER:
121 return "Number";
122 default:
123 LOGINT(NULL);
124 return "";
125 }
126}
127
128/**
129 * @brief Print the whole expression \p exp to debug output.
130 *
131 * @param[in] exp Expression to use.
132 */
133static void
Michal Vasko40308e72020-10-20 16:38:40 +0200134print_expr_struct_debug(const struct lyxp_expr *exp)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200135{
Radek Krejcif13b87b2020-12-01 22:02:17 +0100136#define MSG_BUFFER_SIZE 128
137 char tmp[MSG_BUFFER_SIZE];
Michal Vasko1fdd8fa2021-01-08 09:21:45 +0100138 uint32_t i, j;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200139
Radek Krejci52b6d512020-10-12 12:33:17 +0200140 if (!exp || (ly_ll < LY_LLDBG)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200141 return;
142 }
143
144 LOGDBG(LY_LDGXPATH, "expression \"%s\":", exp->expr);
145 for (i = 0; i < exp->used; ++i) {
Michal Vasko24cddf82020-06-01 08:17:01 +0200146 sprintf(tmp, "\ttoken %s, in expression \"%.*s\"", lyxp_print_token(exp->tokens[i]), exp->tok_len[i],
Michal Vasko69730152020-10-09 16:30:07 +0200147 &exp->expr[exp->tok_pos[i]]);
Michal Vasko23049552021-03-04 15:57:44 +0100148 if (exp->repeat && exp->repeat[i]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200149 sprintf(tmp + strlen(tmp), " (repeat %d", exp->repeat[i][0]);
150 for (j = 1; exp->repeat[i][j]; ++j) {
151 sprintf(tmp + strlen(tmp), ", %d", exp->repeat[i][j]);
152 }
153 strcat(tmp, ")");
154 }
155 LOGDBG(LY_LDGXPATH, tmp);
156 }
Radek Krejcif13b87b2020-12-01 22:02:17 +0100157#undef MSG_BUFFER_SIZE
Michal Vasko03ff5a72019-09-11 13:49:33 +0200158}
159
160#ifndef NDEBUG
161
162/**
163 * @brief Print XPath set content to debug output.
164 *
165 * @param[in] set Set to print.
166 */
167static void
168print_set_debug(struct lyxp_set *set)
169{
170 uint32_t i;
171 char *str;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200172 struct lyxp_set_node *item;
173 struct lyxp_set_scnode *sitem;
174
Radek Krejci52b6d512020-10-12 12:33:17 +0200175 if (ly_ll < LY_LLDBG) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200176 return;
177 }
178
179 switch (set->type) {
180 case LYXP_SET_NODE_SET:
181 LOGDBG(LY_LDGXPATH, "set NODE SET:");
182 for (i = 0; i < set->used; ++i) {
183 item = &set->val.nodes[i];
184
185 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +0100186 case LYXP_NODE_NONE:
187 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): NONE", i + 1, item->pos);
188 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200189 case LYXP_NODE_ROOT:
190 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT", i + 1, item->pos);
191 break;
192 case LYXP_NODE_ROOT_CONFIG:
193 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT CONFIG", i + 1, item->pos);
194 break;
195 case LYXP_NODE_ELEM:
Michal Vasko9e685082021-01-29 14:49:09 +0100196 if ((item->node->schema->nodetype == LYS_LIST) && (lyd_child(item->node)->schema->nodetype == LYS_LEAF)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200197 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (1st child val: %s)", i + 1, item->pos,
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200198 item->node->schema->name, lyd_get_value(lyd_child(item->node)));
Michal Vasko9e685082021-01-29 14:49:09 +0100199 } else if (item->node->schema->nodetype == LYS_LEAFLIST) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200200 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (val: %s)", i + 1, item->pos,
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200201 item->node->schema->name, lyd_get_value(item->node));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200202 } else {
203 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s", i + 1, item->pos, item->node->schema->name);
204 }
205 break;
206 case LYXP_NODE_TEXT:
207 if (item->node->schema->nodetype & LYS_ANYDATA) {
208 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): TEXT <%s>", i + 1, item->pos,
Michal Vasko69730152020-10-09 16:30:07 +0200209 item->node->schema->nodetype == LYS_ANYXML ? "anyxml" : "anydata");
Michal Vasko03ff5a72019-09-11 13:49:33 +0200210 } else {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200211 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 +0200212 }
213 break;
Michal Vasko9f96a052020-03-10 09:41:45 +0100214 case LYXP_NODE_META:
215 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 +0200216 set->val.meta[i].meta->value);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200217 break;
218 }
219 }
220 break;
221
222 case LYXP_SET_SCNODE_SET:
223 LOGDBG(LY_LDGXPATH, "set SCNODE SET:");
224 for (i = 0; i < set->used; ++i) {
225 sitem = &set->val.scnodes[i];
226
227 switch (sitem->type) {
228 case LYXP_NODE_ROOT:
229 LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT", i + 1, sitem->in_ctx);
230 break;
231 case LYXP_NODE_ROOT_CONFIG:
232 LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT CONFIG", i + 1, sitem->in_ctx);
233 break;
234 case LYXP_NODE_ELEM:
235 LOGDBG(LY_LDGXPATH, "\t%d (%u): ELEM %s", i + 1, sitem->in_ctx, sitem->scnode->name);
236 break;
237 default:
238 LOGINT(NULL);
239 break;
240 }
241 }
242 break;
243
Michal Vasko03ff5a72019-09-11 13:49:33 +0200244 case LYXP_SET_BOOLEAN:
245 LOGDBG(LY_LDGXPATH, "set BOOLEAN");
Michal Vasko004d3152020-06-11 19:59:22 +0200246 LOGDBG(LY_LDGXPATH, "\t%s", (set->val.bln ? "true" : "false"));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200247 break;
248
249 case LYXP_SET_STRING:
250 LOGDBG(LY_LDGXPATH, "set STRING");
251 LOGDBG(LY_LDGXPATH, "\t%s", set->val.str);
252 break;
253
254 case LYXP_SET_NUMBER:
255 LOGDBG(LY_LDGXPATH, "set NUMBER");
256
257 if (isnan(set->val.num)) {
258 str = strdup("NaN");
259 } else if ((set->val.num == 0) || (set->val.num == -0.0f)) {
260 str = strdup("0");
261 } else if (isinf(set->val.num) && !signbit(set->val.num)) {
262 str = strdup("Infinity");
263 } else if (isinf(set->val.num) && signbit(set->val.num)) {
264 str = strdup("-Infinity");
265 } else if ((long long)set->val.num == set->val.num) {
266 if (asprintf(&str, "%lld", (long long)set->val.num) == -1) {
267 str = NULL;
268 }
269 } else {
270 if (asprintf(&str, "%03.1Lf", set->val.num) == -1) {
271 str = NULL;
272 }
273 }
274 LY_CHECK_ERR_RET(!str, LOGMEM(NULL), );
275
276 LOGDBG(LY_LDGXPATH, "\t%s", str);
277 free(str);
278 }
279}
280
281#endif
282
283/**
284 * @brief Realloc the string \p str.
285 *
286 * @param[in] ctx libyang context for logging.
287 * @param[in] needed How much free space is required.
288 * @param[in,out] str Pointer to the string to use.
289 * @param[in,out] used Used bytes in \p str.
290 * @param[in,out] size Allocated bytes in \p str.
291 * @return LY_ERR
292 */
293static LY_ERR
Michal Vasko52927e22020-03-16 17:26:14 +0100294cast_string_realloc(const struct ly_ctx *ctx, uint16_t needed, char **str, uint16_t *used, uint16_t *size)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200295{
296 if (*size - *used < needed) {
297 do {
298 if ((UINT16_MAX - *size) < LYXP_STRING_CAST_SIZE_STEP) {
299 LOGERR(ctx, LY_EINVAL, "XPath string length limit (%u) reached.", UINT16_MAX);
300 return LY_EINVAL;
301 }
302 *size += LYXP_STRING_CAST_SIZE_STEP;
303 } while (*size - *used < needed);
304 *str = ly_realloc(*str, *size * sizeof(char));
305 LY_CHECK_ERR_RET(!(*str), LOGMEM(ctx), LY_EMEM);
306 }
307
308 return LY_SUCCESS;
309}
310
311/**
312 * @brief Cast nodes recursively to one string @p str.
313 *
314 * @param[in] node Node to cast.
315 * @param[in] fake_cont Whether to put the data into a "fake" container.
316 * @param[in] root_type Type of the XPath root.
317 * @param[in] indent Current indent.
318 * @param[in,out] str Resulting string.
319 * @param[in,out] used Used bytes in @p str.
320 * @param[in,out] size Allocated bytes in @p str.
321 * @return LY_ERR
322 */
323static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200324cast_string_recursive(const struct lyd_node *node, ly_bool fake_cont, enum lyxp_node_type root_type, uint16_t indent, char **str,
Radek Krejci0f969882020-08-21 16:56:47 +0200325 uint16_t *used, uint16_t *size)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200326{
Radek Krejci7f769d72020-07-11 23:13:56 +0200327 char *buf, *line, *ptr = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200328 const char *value_str;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200329 const struct lyd_node *child;
Michal Vasko60ea6352020-06-29 13:39:39 +0200330 struct lyd_node *tree;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200331 struct lyd_node_any *any;
332 LY_ERR rc;
333
334 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) {
335 return LY_SUCCESS;
336 }
337
338 if (fake_cont) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200339 rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200340 LY_CHECK_RET(rc);
341 strcpy(*str + (*used - 1), "\n");
342 ++(*used);
343
344 ++indent;
345 }
346
347 switch (node->schema->nodetype) {
348 case LYS_CONTAINER:
349 case LYS_LIST:
350 case LYS_RPC:
351 case LYS_NOTIF:
Michal Vaskob7be7a82020-08-20 09:09:04 +0200352 rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200353 LY_CHECK_RET(rc);
354 strcpy(*str + (*used - 1), "\n");
355 ++(*used);
356
Radek Krejcia1c1e542020-09-29 16:06:52 +0200357 for (child = lyd_child(node); child; child = child->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200358 rc = cast_string_recursive(child, 0, root_type, indent + 1, str, used, size);
359 LY_CHECK_RET(rc);
360 }
361
362 break;
363
364 case LYS_LEAF:
365 case LYS_LEAFLIST:
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200366 value_str = lyd_get_value(node);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200367
368 /* print indent */
Michal Vaskob7be7a82020-08-20 09:09:04 +0200369 LY_CHECK_RET(cast_string_realloc(LYD_CTX(node), indent * 2 + strlen(value_str) + 1, str, used, size));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200370 memset(*str + (*used - 1), ' ', indent * 2);
371 *used += indent * 2;
372
373 /* print value */
374 if (*used == 1) {
375 sprintf(*str + (*used - 1), "%s", value_str);
376 *used += strlen(value_str);
377 } else {
378 sprintf(*str + (*used - 1), "%s\n", value_str);
379 *used += strlen(value_str) + 1;
380 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200381
382 break;
383
384 case LYS_ANYXML:
385 case LYS_ANYDATA:
386 any = (struct lyd_node_any *)node;
387 if (!(void *)any->value.tree) {
388 /* no content */
389 buf = strdup("");
Michal Vaskob7be7a82020-08-20 09:09:04 +0200390 LY_CHECK_ERR_RET(!buf, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200391 } else {
Radek Krejci241f6b52020-05-21 18:13:49 +0200392 struct ly_out *out;
Radek Krejcia5bba312020-01-09 15:41:20 +0100393
Michal Vasko60ea6352020-06-29 13:39:39 +0200394 if (any->value_type == LYD_ANYDATA_LYB) {
395 /* try to parse it into a data tree */
Michal Vaskob7be7a82020-08-20 09:09:04 +0200396 if (lyd_parse_data_mem((struct ly_ctx *)LYD_CTX(node), any->value.mem, LYD_LYB, LYD_PARSE_ONLY | LYD_PARSE_STRICT, 0, &tree) == LY_SUCCESS) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200397 /* successfully parsed */
398 free(any->value.mem);
399 any->value.tree = tree;
400 any->value_type = LYD_ANYDATA_DATATREE;
401 }
Radek Krejci7931b192020-06-25 17:05:03 +0200402 /* error is covered by the following switch where LYD_ANYDATA_LYB causes failure */
Michal Vasko60ea6352020-06-29 13:39:39 +0200403 }
404
Michal Vasko03ff5a72019-09-11 13:49:33 +0200405 switch (any->value_type) {
406 case LYD_ANYDATA_STRING:
407 case LYD_ANYDATA_XML:
408 case LYD_ANYDATA_JSON:
409 buf = strdup(any->value.json);
Michal Vaskob7be7a82020-08-20 09:09:04 +0200410 LY_CHECK_ERR_RET(!buf, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200411 break;
412 case LYD_ANYDATA_DATATREE:
Radek Krejci84ce7b12020-06-11 17:28:25 +0200413 LY_CHECK_RET(ly_out_new_memory(&buf, 0, &out));
Michal Vasko3a41dff2020-07-15 14:30:28 +0200414 rc = lyd_print_all(out, any->value.tree, LYD_XML, 0);
Radek Krejci241f6b52020-05-21 18:13:49 +0200415 ly_out_free(out, NULL, 0);
Radek Krejcia5bba312020-01-09 15:41:20 +0100416 LY_CHECK_RET(rc < 0, -rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200417 break;
Michal Vasko60ea6352020-06-29 13:39:39 +0200418 case LYD_ANYDATA_LYB:
Michal Vaskob7be7a82020-08-20 09:09:04 +0200419 LOGERR(LYD_CTX(node), LY_EINVAL, "Cannot convert LYB anydata into string.");
Michal Vasko60ea6352020-06-29 13:39:39 +0200420 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200421 }
422 }
423
424 line = strtok_r(buf, "\n", &ptr);
425 do {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200426 rc = cast_string_realloc(LYD_CTX(node), indent * 2 + strlen(line) + 1, str, used, size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200427 if (rc != LY_SUCCESS) {
428 free(buf);
429 return rc;
430 }
431 memset(*str + (*used - 1), ' ', indent * 2);
432 *used += indent * 2;
433
434 strcpy(*str + (*used - 1), line);
435 *used += strlen(line);
436
437 strcpy(*str + (*used - 1), "\n");
438 *used += 1;
439 } while ((line = strtok_r(NULL, "\n", &ptr)));
440
441 free(buf);
442 break;
443
444 default:
Michal Vaskob7be7a82020-08-20 09:09:04 +0200445 LOGINT_RET(LYD_CTX(node));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200446 }
447
448 if (fake_cont) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200449 rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200450 LY_CHECK_RET(rc);
451 strcpy(*str + (*used - 1), "\n");
452 ++(*used);
453
454 --indent;
455 }
456
457 return LY_SUCCESS;
458}
459
460/**
461 * @brief Cast an element into a string.
462 *
463 * @param[in] node Node to cast.
464 * @param[in] fake_cont Whether to put the data into a "fake" container.
465 * @param[in] root_type Type of the XPath root.
466 * @param[out] str Element cast to dynamically-allocated string.
467 * @return LY_ERR
468 */
469static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200470cast_string_elem(struct lyd_node *node, ly_bool fake_cont, enum lyxp_node_type root_type, char **str)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200471{
472 uint16_t used, size;
473 LY_ERR rc;
474
475 *str = malloc(LYXP_STRING_CAST_SIZE_START * sizeof(char));
Michal Vaskob7be7a82020-08-20 09:09:04 +0200476 LY_CHECK_ERR_RET(!*str, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200477 (*str)[0] = '\0';
478 used = 1;
479 size = LYXP_STRING_CAST_SIZE_START;
480
481 rc = cast_string_recursive(node, fake_cont, root_type, 0, str, &used, &size);
482 if (rc != LY_SUCCESS) {
483 free(*str);
484 return rc;
485 }
486
487 if (size > used) {
488 *str = ly_realloc(*str, used * sizeof(char));
Michal Vaskob7be7a82020-08-20 09:09:04 +0200489 LY_CHECK_ERR_RET(!*str, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200490 }
491 return LY_SUCCESS;
492}
493
494/**
495 * @brief Cast a LYXP_SET_NODE_SET set into a string.
496 * Context position aware.
497 *
498 * @param[in] set Set to cast.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200499 * @param[out] str Cast dynamically-allocated string.
500 * @return LY_ERR
501 */
502static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100503cast_node_set_to_string(struct lyxp_set *set, char **str)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200504{
Michal Vaskoaa677062021-03-09 13:52:53 +0100505 if (!set->used) {
506 *str = strdup("");
507 if (!*str) {
508 LOGMEM_RET(set->ctx);
509 }
510 return LY_SUCCESS;
511 }
512
Michal Vasko03ff5a72019-09-11 13:49:33 +0200513 switch (set->val.nodes[0].type) {
Michal Vasko2caefc12019-11-14 16:07:56 +0100514 case LYXP_NODE_NONE:
515 /* invalid */
516 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200517 case LYXP_NODE_ROOT:
518 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100519 return cast_string_elem(set->val.nodes[0].node, 1, set->root_type, str);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200520 case LYXP_NODE_ELEM:
521 case LYXP_NODE_TEXT:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100522 return cast_string_elem(set->val.nodes[0].node, 0, set->root_type, str);
Michal Vasko9f96a052020-03-10 09:41:45 +0100523 case LYXP_NODE_META:
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200524 *str = strdup(lyd_get_meta_value(set->val.meta[0].meta));
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200525 if (!*str) {
526 LOGMEM_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200527 }
528 return LY_SUCCESS;
529 }
530
531 LOGINT_RET(set->ctx);
532}
533
534/**
535 * @brief Cast a string into an XPath number.
536 *
537 * @param[in] str String to use.
538 * @return Cast number.
539 */
540static long double
541cast_string_to_number(const char *str)
542{
543 long double num;
544 char *ptr;
545
546 errno = 0;
547 num = strtold(str, &ptr);
548 if (errno || *ptr) {
549 num = NAN;
550 }
551 return num;
552}
553
554/**
555 * @brief Callback for checking value equality.
556 *
Michal Vasko62524a92021-02-26 10:08:50 +0100557 * Implementation of ::lyht_value_equal_cb.
Radek Krejci857189e2020-09-01 13:26:36 +0200558 *
Michal Vasko03ff5a72019-09-11 13:49:33 +0200559 * @param[in] val1_p First value.
560 * @param[in] val2_p Second value.
561 * @param[in] mod Whether hash table is being modified.
562 * @param[in] cb_data Callback data.
Radek Krejci857189e2020-09-01 13:26:36 +0200563 * @return Boolean value whether values are equal or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200564 */
Radek Krejci857189e2020-09-01 13:26:36 +0200565static ly_bool
566set_values_equal_cb(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void *UNUSED(cb_data))
Michal Vasko03ff5a72019-09-11 13:49:33 +0200567{
568 struct lyxp_set_hash_node *val1, *val2;
569
570 val1 = (struct lyxp_set_hash_node *)val1_p;
571 val2 = (struct lyxp_set_hash_node *)val2_p;
572
573 if ((val1->node == val2->node) && (val1->type == val2->type)) {
574 return 1;
575 }
576
577 return 0;
578}
579
580/**
581 * @brief Insert node and its hash into set.
582 *
583 * @param[in] set et to insert to.
584 * @param[in] node Node with hash.
585 * @param[in] type Node type.
586 */
587static void
588set_insert_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type)
589{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200590 LY_ERR r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200591 uint32_t i, hash;
592 struct lyxp_set_hash_node hnode;
593
594 if (!set->ht && (set->used >= LYD_HT_MIN_ITEMS)) {
595 /* create hash table and add all the nodes */
596 set->ht = lyht_new(1, sizeof(struct lyxp_set_hash_node), set_values_equal_cb, NULL, 1);
597 for (i = 0; i < set->used; ++i) {
598 hnode.node = set->val.nodes[i].node;
599 hnode.type = set->val.nodes[i].type;
600
601 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
602 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
603 hash = dict_hash_multi(hash, NULL, 0);
604
605 r = lyht_insert(set->ht, &hnode, hash, NULL);
606 assert(!r);
607 (void)r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200608
Michal Vasko9d6befd2019-12-11 14:56:56 +0100609 if (hnode.node == node) {
610 /* it was just added, do not add it twice */
611 node = NULL;
612 }
613 }
614 }
615
616 if (set->ht && node) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200617 /* add the new node into hash table */
618 hnode.node = node;
619 hnode.type = type;
620
621 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
622 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
623 hash = dict_hash_multi(hash, NULL, 0);
624
625 r = lyht_insert(set->ht, &hnode, hash, NULL);
626 assert(!r);
627 (void)r;
628 }
629}
630
631/**
632 * @brief Remove node and its hash from set.
633 *
634 * @param[in] set Set to remove from.
635 * @param[in] node Node to remove.
636 * @param[in] type Node type.
637 */
638static void
639set_remove_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type)
640{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200641 LY_ERR r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200642 struct lyxp_set_hash_node hnode;
643 uint32_t hash;
644
645 if (set->ht) {
646 hnode.node = node;
647 hnode.type = type;
648
649 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
650 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
651 hash = dict_hash_multi(hash, NULL, 0);
652
653 r = lyht_remove(set->ht, &hnode, hash);
654 assert(!r);
655 (void)r;
656
657 if (!set->ht->used) {
658 lyht_free(set->ht);
659 set->ht = NULL;
660 }
661 }
662}
663
664/**
665 * @brief Check whether node is in set based on its hash.
666 *
667 * @param[in] set Set to search in.
668 * @param[in] node Node to search for.
669 * @param[in] type Node type.
670 * @param[in] skip_idx Index in @p set to skip.
671 * @return LY_ERR
672 */
673static LY_ERR
674set_dup_node_hash_check(const struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type, int skip_idx)
675{
676 struct lyxp_set_hash_node hnode, *match_p;
677 uint32_t hash;
678
679 hnode.node = node;
680 hnode.type = type;
681
682 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
683 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
684 hash = dict_hash_multi(hash, NULL, 0);
685
686 if (!lyht_find(set->ht, &hnode, hash, (void **)&match_p)) {
687 if ((skip_idx > -1) && (set->val.nodes[skip_idx].node == match_p->node) && (set->val.nodes[skip_idx].type == match_p->type)) {
688 /* we found it on the index that should be skipped, find another */
689 hnode = *match_p;
690 if (lyht_find_next(set->ht, &hnode, hash, (void **)&match_p)) {
691 /* none other found */
692 return LY_SUCCESS;
693 }
694 }
695
696 return LY_EEXIST;
697 }
698
699 /* not found */
700 return LY_SUCCESS;
701}
702
Michal Vaskod3678892020-05-21 10:06:58 +0200703void
704lyxp_set_free_content(struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200705{
706 if (!set) {
707 return;
708 }
709
710 if (set->type == LYXP_SET_NODE_SET) {
711 free(set->val.nodes);
712 lyht_free(set->ht);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200713 } else if (set->type == LYXP_SET_SCNODE_SET) {
714 free(set->val.scnodes);
Michal Vaskod3678892020-05-21 10:06:58 +0200715 lyht_free(set->ht);
716 } else {
717 if (set->type == LYXP_SET_STRING) {
718 free(set->val.str);
719 }
720 set->type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200721 }
Michal Vaskod3678892020-05-21 10:06:58 +0200722
723 set->val.nodes = NULL;
724 set->used = 0;
725 set->size = 0;
726 set->ht = NULL;
727 set->ctx_pos = 0;
aPiecek748da732021-06-01 09:56:43 +0200728 set->ctx_size = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200729}
730
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100731/**
732 * @brief Free dynamically-allocated set.
733 *
734 * @param[in] set Set to free.
735 */
736static void
Michal Vasko03ff5a72019-09-11 13:49:33 +0200737lyxp_set_free(struct lyxp_set *set)
738{
739 if (!set) {
740 return;
741 }
742
Michal Vaskod3678892020-05-21 10:06:58 +0200743 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200744 free(set);
745}
746
747/**
748 * @brief Initialize set context.
749 *
750 * @param[in] new Set to initialize.
751 * @param[in] set Arbitrary initialized set.
752 */
753static void
Michal Vasko4c7763f2020-07-27 17:40:37 +0200754set_init(struct lyxp_set *new, const struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200755{
756 memset(new, 0, sizeof *new);
Michal Vasko02a77382019-09-12 11:47:35 +0200757 if (set) {
758 new->ctx = set->ctx;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200759 new->cur_node = set->cur_node;
Michal Vasko588112f2019-12-10 14:51:53 +0100760 new->root_type = set->root_type;
Michal Vasko6b26e742020-07-17 15:02:10 +0200761 new->context_op = set->context_op;
Michal Vaskof03ed032020-03-04 13:31:44 +0100762 new->tree = set->tree;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200763 new->cur_mod = set->cur_mod;
Michal Vasko02a77382019-09-12 11:47:35 +0200764 new->format = set->format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200765 new->prefix_data = set->prefix_data;
Michal Vasko02a77382019-09-12 11:47:35 +0200766 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200767}
768
769/**
770 * @brief Create a deep copy of a set.
771 *
772 * @param[in] set Set to copy.
773 * @return Copy of @p set.
774 */
775static struct lyxp_set *
776set_copy(struct lyxp_set *set)
777{
778 struct lyxp_set *ret;
Michal Vasko1fdd8fa2021-01-08 09:21:45 +0100779 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200780
781 if (!set) {
782 return NULL;
783 }
784
785 ret = malloc(sizeof *ret);
786 LY_CHECK_ERR_RET(!ret, LOGMEM(set->ctx), NULL);
787 set_init(ret, set);
788
789 if (set->type == LYXP_SET_SCNODE_SET) {
790 ret->type = set->type;
791
792 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100793 if ((set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) ||
794 (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +0200795 uint32_t idx;
796 LY_CHECK_ERR_RET(lyxp_set_scnode_insert_node(ret, set->val.scnodes[i].scnode, set->val.scnodes[i].type, &idx),
797 lyxp_set_free(ret), NULL);
Michal Vasko3f27c522020-01-06 08:37:49 +0100798 /* coverity seems to think scnodes can be NULL */
Radek Krejciaa6b53f2020-08-27 15:19:03 +0200799 if (!ret->val.scnodes) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200800 lyxp_set_free(ret);
801 return NULL;
802 }
Michal Vaskoba716542019-12-16 10:01:58 +0100803 ret->val.scnodes[idx].in_ctx = set->val.scnodes[i].in_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200804 }
805 }
806 } else if (set->type == LYXP_SET_NODE_SET) {
807 ret->type = set->type;
Michal Vasko08e9b112021-06-11 15:41:17 +0200808 if (set->used) {
809 ret->val.nodes = malloc(set->used * sizeof *ret->val.nodes);
810 LY_CHECK_ERR_RET(!ret->val.nodes, LOGMEM(set->ctx); free(ret), NULL);
811 memcpy(ret->val.nodes, set->val.nodes, set->used * sizeof *ret->val.nodes);
812 } else {
813 ret->val.nodes = NULL;
814 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200815
816 ret->used = ret->size = set->used;
817 ret->ctx_pos = set->ctx_pos;
818 ret->ctx_size = set->ctx_size;
Michal Vasko4a04e542021-02-01 08:58:30 +0100819 if (set->ht) {
820 ret->ht = lyht_dup(set->ht);
821 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200822 } else {
Radek Krejci0f969882020-08-21 16:56:47 +0200823 memcpy(ret, set, sizeof *ret);
824 if (set->type == LYXP_SET_STRING) {
825 ret->val.str = strdup(set->val.str);
826 LY_CHECK_ERR_RET(!ret->val.str, LOGMEM(set->ctx); free(ret), NULL);
827 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200828 }
829
830 return ret;
831}
832
833/**
834 * @brief Fill XPath set with a string. Any current data are disposed of.
835 *
836 * @param[in] set Set to fill.
837 * @param[in] string String to fill into \p set.
838 * @param[in] str_len Length of \p string. 0 is a valid value!
839 */
840static void
841set_fill_string(struct lyxp_set *set, const char *string, uint16_t str_len)
842{
Michal Vaskod3678892020-05-21 10:06:58 +0200843 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200844
845 set->type = LYXP_SET_STRING;
846 if ((str_len == 0) && (string[0] != '\0')) {
847 string = "";
848 }
849 set->val.str = strndup(string, str_len);
850}
851
852/**
853 * @brief Fill XPath set with a number. Any current data are disposed of.
854 *
855 * @param[in] set Set to fill.
856 * @param[in] number Number to fill into \p set.
857 */
858static void
859set_fill_number(struct lyxp_set *set, long double number)
860{
Michal Vaskod3678892020-05-21 10:06:58 +0200861 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200862
863 set->type = LYXP_SET_NUMBER;
864 set->val.num = number;
865}
866
867/**
868 * @brief Fill XPath set with a boolean. Any current data are disposed of.
869 *
870 * @param[in] set Set to fill.
871 * @param[in] boolean Boolean to fill into \p set.
872 */
873static void
Radek Krejci857189e2020-09-01 13:26:36 +0200874set_fill_boolean(struct lyxp_set *set, ly_bool boolean)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200875{
Michal Vaskod3678892020-05-21 10:06:58 +0200876 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200877
878 set->type = LYXP_SET_BOOLEAN;
Michal Vasko004d3152020-06-11 19:59:22 +0200879 set->val.bln = boolean;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200880}
881
882/**
883 * @brief Fill XPath set with the value from another set (deep assign).
884 * Any current data are disposed of.
885 *
886 * @param[in] trg Set to fill.
887 * @param[in] src Source set to copy into \p trg.
888 */
889static void
Michal Vasko4c7763f2020-07-27 17:40:37 +0200890set_fill_set(struct lyxp_set *trg, const struct lyxp_set *src)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200891{
892 if (!trg || !src) {
893 return;
894 }
895
896 if (trg->type == LYXP_SET_NODE_SET) {
897 free(trg->val.nodes);
898 } else if (trg->type == LYXP_SET_STRING) {
899 free(trg->val.str);
900 }
901 set_init(trg, src);
902
903 if (src->type == LYXP_SET_SCNODE_SET) {
904 trg->type = LYXP_SET_SCNODE_SET;
905 trg->used = src->used;
906 trg->size = src->used;
907
Michal Vasko08e9b112021-06-11 15:41:17 +0200908 if (trg->size) {
909 trg->val.scnodes = ly_realloc(trg->val.scnodes, trg->size * sizeof *trg->val.scnodes);
910 LY_CHECK_ERR_RET(!trg->val.scnodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), );
911 memcpy(trg->val.scnodes, src->val.scnodes, src->used * sizeof *src->val.scnodes);
912 } else {
913 trg->val.scnodes = NULL;
914 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200915 } else if (src->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +0200916 set_fill_boolean(trg, src->val.bln);
Michal Vasko44f3d2c2020-08-24 09:49:38 +0200917 } else if (src->type == LYXP_SET_NUMBER) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200918 set_fill_number(trg, src->val.num);
919 } else if (src->type == LYXP_SET_STRING) {
920 set_fill_string(trg, src->val.str, strlen(src->val.str));
921 } else {
922 if (trg->type == LYXP_SET_NODE_SET) {
923 free(trg->val.nodes);
924 } else if (trg->type == LYXP_SET_STRING) {
925 free(trg->val.str);
926 }
927
Michal Vaskod3678892020-05-21 10:06:58 +0200928 assert(src->type == LYXP_SET_NODE_SET);
929
930 trg->type = LYXP_SET_NODE_SET;
931 trg->used = src->used;
932 trg->size = src->used;
933 trg->ctx_pos = src->ctx_pos;
934 trg->ctx_size = src->ctx_size;
935
Michal Vasko08e9b112021-06-11 15:41:17 +0200936 if (trg->size) {
937 trg->val.nodes = malloc(trg->size * sizeof *trg->val.nodes);
938 LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), );
939 memcpy(trg->val.nodes, src->val.nodes, src->used * sizeof *src->val.nodes);
940 } else {
941 trg->val.nodes = NULL;
942 }
Michal Vaskod3678892020-05-21 10:06:58 +0200943 if (src->ht) {
944 trg->ht = lyht_dup(src->ht);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200945 } else {
Michal Vaskod3678892020-05-21 10:06:58 +0200946 trg->ht = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200947 }
948 }
949}
950
951/**
952 * @brief Clear context of all schema nodes.
953 *
954 * @param[in] set Set to clear.
Michal Vasko1a09b212021-05-06 13:00:10 +0200955 * @param[in] new_ctx New context state for all the nodes currently in the context.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200956 */
957static void
Michal Vasko1a09b212021-05-06 13:00:10 +0200958set_scnode_clear_ctx(struct lyxp_set *set, int32_t new_ctx)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200959{
960 uint32_t i;
961
962 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100963 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko1a09b212021-05-06 13:00:10 +0200964 set->val.scnodes[i].in_ctx = new_ctx;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100965 } else if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START) {
966 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200967 }
968 }
969}
970
971/**
972 * @brief Remove a node from a set. Removing last node changes
973 * set into LYXP_SET_EMPTY. Context position aware.
974 *
975 * @param[in] set Set to use.
976 * @param[in] idx Index from @p set of the node to be removed.
977 */
978static void
979set_remove_node(struct lyxp_set *set, uint32_t idx)
980{
981 assert(set && (set->type == LYXP_SET_NODE_SET));
982 assert(idx < set->used);
983
984 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
985
986 --set->used;
Michal Vasko08e9b112021-06-11 15:41:17 +0200987 if (idx < set->used) {
988 memmove(&set->val.nodes[idx], &set->val.nodes[idx + 1], (set->used - idx) * sizeof *set->val.nodes);
989 } else if (!set->used) {
Michal Vaskod3678892020-05-21 10:06:58 +0200990 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200991 }
992}
993
994/**
Michal Vasko2caefc12019-11-14 16:07:56 +0100995 * @brief Remove a node from a set by setting its type to LYXP_NODE_NONE.
Michal Vasko57eab132019-09-24 11:46:26 +0200996 *
997 * @param[in] set Set to use.
998 * @param[in] idx Index from @p set of the node to be removed.
999 */
1000static void
Michal Vasko2caefc12019-11-14 16:07:56 +01001001set_remove_node_none(struct lyxp_set *set, uint32_t idx)
Michal Vasko57eab132019-09-24 11:46:26 +02001002{
1003 assert(set && (set->type == LYXP_SET_NODE_SET));
1004 assert(idx < set->used);
1005
Michal Vasko2caefc12019-11-14 16:07:56 +01001006 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1007 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1008 }
1009 set->val.nodes[idx].type = LYXP_NODE_NONE;
Michal Vasko57eab132019-09-24 11:46:26 +02001010}
1011
1012/**
Michal Vasko2caefc12019-11-14 16:07:56 +01001013 * @brief Remove all LYXP_NODE_NONE nodes from a set. Removing last node changes
Michal Vasko57eab132019-09-24 11:46:26 +02001014 * set into LYXP_SET_EMPTY. Context position aware.
1015 *
1016 * @param[in] set Set to consolidate.
1017 */
1018static void
Michal Vasko2caefc12019-11-14 16:07:56 +01001019set_remove_nodes_none(struct lyxp_set *set)
Michal Vasko57eab132019-09-24 11:46:26 +02001020{
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01001021 uint32_t i, orig_used, end = 0;
1022 int64_t start;
Michal Vasko57eab132019-09-24 11:46:26 +02001023
Michal Vaskod3678892020-05-21 10:06:58 +02001024 assert(set);
Michal Vasko57eab132019-09-24 11:46:26 +02001025
1026 orig_used = set->used;
1027 set->used = 0;
Michal Vaskod989ba02020-08-24 10:59:24 +02001028 for (i = 0; i < orig_used; ) {
Michal Vasko57eab132019-09-24 11:46:26 +02001029 start = -1;
1030 do {
Michal Vasko2caefc12019-11-14 16:07:56 +01001031 if ((set->val.nodes[i].type != LYXP_NODE_NONE) && (start == -1)) {
Michal Vasko57eab132019-09-24 11:46:26 +02001032 start = i;
Michal Vasko2caefc12019-11-14 16:07:56 +01001033 } else if ((start > -1) && (set->val.nodes[i].type == LYXP_NODE_NONE)) {
Michal Vasko57eab132019-09-24 11:46:26 +02001034 end = i;
1035 ++i;
1036 break;
1037 }
1038
1039 ++i;
1040 if (i == orig_used) {
1041 end = i;
1042 }
1043 } while (i < orig_used);
1044
1045 if (start > -1) {
1046 /* move the whole chunk of valid nodes together */
1047 if (set->used != (unsigned)start) {
1048 memmove(&set->val.nodes[set->used], &set->val.nodes[start], (end - start) * sizeof *set->val.nodes);
1049 }
1050 set->used += end - start;
1051 }
1052 }
Michal Vasko57eab132019-09-24 11:46:26 +02001053}
1054
1055/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02001056 * @brief Check for duplicates in a node set.
1057 *
1058 * @param[in] set Set to check.
1059 * @param[in] node Node to look for in @p set.
1060 * @param[in] node_type Type of @p node.
1061 * @param[in] skip_idx Index from @p set to skip.
1062 * @return LY_ERR
1063 */
1064static LY_ERR
1065set_dup_node_check(const struct lyxp_set *set, const struct lyd_node *node, enum lyxp_node_type node_type, int skip_idx)
1066{
1067 uint32_t i;
1068
Michal Vasko2caefc12019-11-14 16:07:56 +01001069 if (set->ht && node) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001070 return set_dup_node_hash_check(set, (struct lyd_node *)node, node_type, skip_idx);
1071 }
1072
1073 for (i = 0; i < set->used; ++i) {
1074 if ((skip_idx > -1) && (i == (unsigned)skip_idx)) {
1075 continue;
1076 }
1077
1078 if ((set->val.nodes[i].node == node) && (set->val.nodes[i].type == node_type)) {
1079 return LY_EEXIST;
1080 }
1081 }
1082
1083 return LY_SUCCESS;
1084}
1085
Radek Krejci857189e2020-09-01 13:26:36 +02001086ly_bool
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001087lyxp_set_scnode_contains(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type, int skip_idx,
1088 uint32_t *index_p)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001089{
1090 uint32_t i;
1091
1092 for (i = 0; i < set->used; ++i) {
1093 if ((skip_idx > -1) && (i == (unsigned)skip_idx)) {
1094 continue;
1095 }
1096
1097 if ((set->val.scnodes[i].scnode == node) && (set->val.scnodes[i].type == node_type)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001098 if (index_p) {
1099 *index_p = i;
1100 }
1101 return 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001102 }
1103 }
1104
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001105 return 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001106}
1107
Michal Vaskoecd62de2019-11-13 12:35:11 +01001108void
1109lyxp_set_scnode_merge(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001110{
1111 uint32_t orig_used, i, j;
1112
Michal Vaskod3678892020-05-21 10:06:58 +02001113 assert((set1->type == LYXP_SET_SCNODE_SET) && (set2->type == LYXP_SET_SCNODE_SET));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001114
Michal Vaskod3678892020-05-21 10:06:58 +02001115 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001116 return;
1117 }
1118
Michal Vaskod3678892020-05-21 10:06:58 +02001119 if (!set1->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001120 memcpy(set1, set2, sizeof *set1);
1121 return;
1122 }
1123
1124 if (set1->used + set2->used > set1->size) {
1125 set1->size = set1->used + set2->used;
1126 set1->val.scnodes = ly_realloc(set1->val.scnodes, set1->size * sizeof *set1->val.scnodes);
1127 LY_CHECK_ERR_RET(!set1->val.scnodes, LOGMEM(set1->ctx), );
1128 }
1129
1130 orig_used = set1->used;
1131
1132 for (i = 0; i < set2->used; ++i) {
1133 for (j = 0; j < orig_used; ++j) {
1134 /* detect duplicities */
1135 if (set1->val.scnodes[j].scnode == set2->val.scnodes[i].scnode) {
1136 break;
1137 }
1138 }
1139
Michal Vasko0587bce2020-12-10 12:19:21 +01001140 if (j < orig_used) {
1141 /* node is there, but update its status if needed */
1142 if (set1->val.scnodes[j].in_ctx == LYXP_SET_SCNODE_START_USED) {
1143 set1->val.scnodes[j].in_ctx = set2->val.scnodes[i].in_ctx;
Michal Vasko1a09b212021-05-06 13:00:10 +02001144 } else if ((set1->val.scnodes[j].in_ctx == LYXP_SET_SCNODE_ATOM_NODE) &&
1145 (set2->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_VAL)) {
1146 set1->val.scnodes[j].in_ctx = set2->val.scnodes[i].in_ctx;
Michal Vasko0587bce2020-12-10 12:19:21 +01001147 }
1148 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001149 memcpy(&set1->val.scnodes[set1->used], &set2->val.scnodes[i], sizeof *set2->val.scnodes);
1150 ++set1->used;
1151 }
1152 }
1153
Michal Vaskod3678892020-05-21 10:06:58 +02001154 lyxp_set_free_content(set2);
1155 set2->type = LYXP_SET_SCNODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001156}
1157
1158/**
1159 * @brief Insert a node into a set. Context position aware.
1160 *
1161 * @param[in] set Set to use.
1162 * @param[in] node Node to insert to @p set.
1163 * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting.
1164 * @param[in] node_type Node type of @p node.
1165 * @param[in] idx Index in @p set to insert into.
1166 */
1167static void
1168set_insert_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx)
1169{
Michal Vaskod3678892020-05-21 10:06:58 +02001170 assert(set && (set->type == LYXP_SET_NODE_SET));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001171
Michal Vaskod3678892020-05-21 10:06:58 +02001172 if (!set->size) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001173 /* first item */
1174 if (idx) {
1175 /* no real harm done, but it is a bug */
1176 LOGINT(set->ctx);
1177 idx = 0;
1178 }
1179 set->val.nodes = malloc(LYXP_SET_SIZE_START * sizeof *set->val.nodes);
1180 LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), );
1181 set->type = LYXP_SET_NODE_SET;
1182 set->used = 0;
1183 set->size = LYXP_SET_SIZE_START;
1184 set->ctx_pos = 1;
1185 set->ctx_size = 1;
1186 set->ht = NULL;
1187 } else {
1188 /* not an empty set */
1189 if (set->used == set->size) {
1190
1191 /* set is full */
1192 set->val.nodes = ly_realloc(set->val.nodes, (set->size + LYXP_SET_SIZE_STEP) * sizeof *set->val.nodes);
1193 LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), );
1194 set->size += LYXP_SET_SIZE_STEP;
1195 }
1196
1197 if (idx > set->used) {
1198 LOGINT(set->ctx);
1199 idx = set->used;
1200 }
1201
1202 /* make space for the new node */
1203 if (idx < set->used) {
1204 memmove(&set->val.nodes[idx + 1], &set->val.nodes[idx], (set->used - idx) * sizeof *set->val.nodes);
1205 }
1206 }
1207
1208 /* finally assign the value */
1209 set->val.nodes[idx].node = (struct lyd_node *)node;
1210 set->val.nodes[idx].type = node_type;
1211 set->val.nodes[idx].pos = pos;
1212 ++set->used;
1213
Michal Vasko2caefc12019-11-14 16:07:56 +01001214 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1215 set_insert_node_hash(set, (struct lyd_node *)node, node_type);
1216 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001217}
1218
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001219LY_ERR
1220lyxp_set_scnode_insert_node(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type, uint32_t *index_p)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001221{
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001222 uint32_t index;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001223
1224 assert(set->type == LYXP_SET_SCNODE_SET);
1225
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001226 if (lyxp_set_scnode_contains(set, node, node_type, -1, &index)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001227 set->val.scnodes[index].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001228 } else {
1229 if (set->used == set->size) {
1230 set->val.scnodes = ly_realloc(set->val.scnodes, (set->size + LYXP_SET_SIZE_STEP) * sizeof *set->val.scnodes);
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001231 LY_CHECK_ERR_RET(!set->val.scnodes, LOGMEM(set->ctx), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001232 set->size += LYXP_SET_SIZE_STEP;
1233 }
1234
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001235 index = set->used;
1236 set->val.scnodes[index].scnode = (struct lysc_node *)node;
1237 set->val.scnodes[index].type = node_type;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001238 set->val.scnodes[index].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001239 ++set->used;
1240 }
1241
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001242 if (index_p) {
1243 *index_p = index;
1244 }
1245
1246 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001247}
1248
1249/**
1250 * @brief Replace a node in a set with another. Context position aware.
1251 *
1252 * @param[in] set Set to use.
1253 * @param[in] node Node to insert to @p set.
1254 * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting.
1255 * @param[in] node_type Node type of @p node.
1256 * @param[in] idx Index in @p set of the node to replace.
1257 */
1258static void
1259set_replace_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx)
1260{
1261 assert(set && (idx < set->used));
1262
Michal Vasko2caefc12019-11-14 16:07:56 +01001263 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1264 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1265 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001266 set->val.nodes[idx].node = (struct lyd_node *)node;
1267 set->val.nodes[idx].type = node_type;
1268 set->val.nodes[idx].pos = pos;
Michal Vasko2caefc12019-11-14 16:07:56 +01001269 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1270 set_insert_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1271 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001272}
1273
1274/**
1275 * @brief Set all nodes with ctx 1 to a new unique context value.
1276 *
1277 * @param[in] set Set to modify.
1278 * @return New context value.
1279 */
Michal Vasko5c4e5892019-11-14 12:31:38 +01001280static int32_t
Michal Vasko03ff5a72019-09-11 13:49:33 +02001281set_scnode_new_in_ctx(struct lyxp_set *set)
1282{
Michal Vasko5c4e5892019-11-14 12:31:38 +01001283 uint32_t i;
1284 int32_t ret_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001285
1286 assert(set->type == LYXP_SET_SCNODE_SET);
1287
Radek Krejcif13b87b2020-12-01 22:02:17 +01001288 ret_ctx = LYXP_SET_SCNODE_ATOM_PRED_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001289retry:
1290 for (i = 0; i < set->used; ++i) {
1291 if (set->val.scnodes[i].in_ctx >= ret_ctx) {
1292 ret_ctx = set->val.scnodes[i].in_ctx + 1;
1293 goto retry;
1294 }
1295 }
1296 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001297 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001298 set->val.scnodes[i].in_ctx = ret_ctx;
1299 }
1300 }
1301
1302 return ret_ctx;
1303}
1304
1305/**
1306 * @brief Get unique @p node position in the data.
1307 *
1308 * @param[in] node Node to find.
1309 * @param[in] node_type Node type of @p node.
1310 * @param[in] root Root node.
1311 * @param[in] root_type Type of the XPath @p root node.
1312 * @param[in] prev Node that we think is before @p node in DFS from @p root. Can optionally
1313 * be used to increase efficiency and start the DFS from this node.
1314 * @param[in] prev_pos Node @p prev position. Optional, but must be set if @p prev is set.
1315 * @return Node position.
1316 */
1317static uint32_t
1318get_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 +02001319 enum lyxp_node_type root_type, const struct lyd_node **prev, uint32_t *prev_pos)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001320{
Michal Vasko56daf732020-08-10 10:57:18 +02001321 const struct lyd_node *elem = NULL, *top_sibling;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001322 uint32_t pos = 1;
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001323 ly_bool found = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001324
1325 assert(prev && prev_pos && !root->prev->next);
1326
1327 if ((node_type == LYXP_NODE_ROOT) || (node_type == LYXP_NODE_ROOT_CONFIG)) {
1328 return 0;
1329 }
1330
1331 if (*prev) {
1332 /* start from the previous element instead from the root */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001333 pos = *prev_pos;
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001334 for (top_sibling = *prev; top_sibling->parent; top_sibling = lyd_parent(top_sibling)) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001335 goto dfs_search;
1336 }
1337
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001338 LY_LIST_FOR(root, top_sibling) {
Michal Vasko56daf732020-08-10 10:57:18 +02001339 LYD_TREE_DFS_BEGIN(top_sibling, elem) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001340dfs_search:
Michal Vasko56daf732020-08-10 10:57:18 +02001341 if (*prev && !elem) {
1342 /* resume previous DFS */
1343 elem = LYD_TREE_DFS_next = (struct lyd_node *)*prev;
1344 LYD_TREE_DFS_continue = 0;
1345 }
1346
Michal Vasko03ff5a72019-09-11 13:49:33 +02001347 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (elem->schema->flags & LYS_CONFIG_R)) {
Michal Vasko56daf732020-08-10 10:57:18 +02001348 /* skip */
1349 LYD_TREE_DFS_continue = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001350 } else {
Michal Vasko56daf732020-08-10 10:57:18 +02001351 if (elem == node) {
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001352 found = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001353 break;
1354 }
Michal Vasko56daf732020-08-10 10:57:18 +02001355 ++pos;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001356 }
Michal Vasko56daf732020-08-10 10:57:18 +02001357
1358 LYD_TREE_DFS_END(top_sibling, elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001359 }
1360
1361 /* node found */
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001362 if (found) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001363 break;
1364 }
1365 }
1366
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001367 if (!found) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001368 if (!(*prev)) {
1369 /* we went from root and failed to find it, cannot be */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001370 LOGINT(LYD_CTX(node));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001371 return 0;
1372 } else {
Michal Vasko56daf732020-08-10 10:57:18 +02001373 /* start the search again from the beginning */
1374 *prev = root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001375
Michal Vasko56daf732020-08-10 10:57:18 +02001376 top_sibling = root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001377 pos = 1;
1378 goto dfs_search;
1379 }
1380 }
1381
1382 /* remember the last found node for next time */
1383 *prev = node;
1384 *prev_pos = pos;
1385
1386 return pos;
1387}
1388
1389/**
1390 * @brief Assign (fill) missing node positions.
1391 *
1392 * @param[in] set Set to fill positions in.
1393 * @param[in] root Context root node.
1394 * @param[in] root_type Context root type.
1395 * @return LY_ERR
1396 */
1397static LY_ERR
1398set_assign_pos(struct lyxp_set *set, const struct lyd_node *root, enum lyxp_node_type root_type)
1399{
1400 const struct lyd_node *prev = NULL, *tmp_node;
1401 uint32_t i, tmp_pos = 0;
1402
1403 for (i = 0; i < set->used; ++i) {
1404 if (!set->val.nodes[i].pos) {
1405 tmp_node = NULL;
1406 switch (set->val.nodes[i].type) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001407 case LYXP_NODE_META:
1408 tmp_node = set->val.meta[i].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001409 if (!tmp_node) {
1410 LOGINT_RET(root->schema->module->ctx);
1411 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01001412 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001413 case LYXP_NODE_ELEM:
1414 case LYXP_NODE_TEXT:
1415 if (!tmp_node) {
1416 tmp_node = set->val.nodes[i].node;
1417 }
1418 set->val.nodes[i].pos = get_node_pos(tmp_node, set->val.nodes[i].type, root, root_type, &prev, &tmp_pos);
1419 break;
1420 default:
1421 /* all roots have position 0 */
1422 break;
1423 }
1424 }
1425 }
1426
1427 return LY_SUCCESS;
1428}
1429
1430/**
Michal Vasko9f96a052020-03-10 09:41:45 +01001431 * @brief Get unique @p meta position in the parent metadata.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001432 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001433 * @param[in] meta Metadata to use.
1434 * @return Metadata position.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001435 */
1436static uint16_t
Michal Vasko9f96a052020-03-10 09:41:45 +01001437get_meta_pos(struct lyd_meta *meta)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001438{
1439 uint16_t pos = 0;
Michal Vasko9f96a052020-03-10 09:41:45 +01001440 struct lyd_meta *meta2;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001441
Michal Vasko9f96a052020-03-10 09:41:45 +01001442 for (meta2 = meta->parent->meta; meta2 && (meta2 != meta); meta2 = meta2->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001443 ++pos;
1444 }
1445
Michal Vasko9f96a052020-03-10 09:41:45 +01001446 assert(meta2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001447 return pos;
1448}
1449
1450/**
1451 * @brief Compare 2 nodes in respect to XPath document order.
1452 *
1453 * @param[in] item1 1st node.
1454 * @param[in] item2 2nd node.
1455 * @return If 1st > 2nd returns 1, 1st == 2nd returns 0, and 1st < 2nd returns -1.
1456 */
1457static int
1458set_sort_compare(struct lyxp_set_node *item1, struct lyxp_set_node *item2)
1459{
Michal Vasko9f96a052020-03-10 09:41:45 +01001460 uint32_t meta_pos1 = 0, meta_pos2 = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001461
1462 if (item1->pos < item2->pos) {
1463 return -1;
1464 }
1465
1466 if (item1->pos > item2->pos) {
1467 return 1;
1468 }
1469
1470 /* node positions are equal, the fun case */
1471
1472 /* 1st ELEM - == - 2nd TEXT, 1st TEXT - == - 2nd ELEM */
1473 /* special case since text nodes are actually saved as their parents */
1474 if ((item1->node == item2->node) && (item1->type != item2->type)) {
1475 if (item1->type == LYXP_NODE_ELEM) {
1476 assert(item2->type == LYXP_NODE_TEXT);
1477 return -1;
1478 } else {
1479 assert((item1->type == LYXP_NODE_TEXT) && (item2->type == LYXP_NODE_ELEM));
1480 return 1;
1481 }
1482 }
1483
Michal Vasko9f96a052020-03-10 09:41:45 +01001484 /* we need meta positions now */
1485 if (item1->type == LYXP_NODE_META) {
1486 meta_pos1 = get_meta_pos((struct lyd_meta *)item1->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001487 }
Michal Vasko9f96a052020-03-10 09:41:45 +01001488 if (item2->type == LYXP_NODE_META) {
1489 meta_pos2 = get_meta_pos((struct lyd_meta *)item2->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001490 }
1491
Michal Vasko9f96a052020-03-10 09:41:45 +01001492 /* 1st ROOT - 2nd ROOT, 1st ELEM - 2nd ELEM, 1st TEXT - 2nd TEXT, 1st META - =pos= - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001493 /* check for duplicates */
1494 if (item1->node == item2->node) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001495 assert((item1->type == item2->type) && ((item1->type != LYXP_NODE_META) || (meta_pos1 == meta_pos2)));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001496 return 0;
1497 }
1498
Michal Vasko9f96a052020-03-10 09:41:45 +01001499 /* 1st ELEM - 2nd TEXT, 1st ELEM - any pos - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001500 /* elem is always first, 2nd node is after it */
1501 if (item1->type == LYXP_NODE_ELEM) {
1502 assert(item2->type != LYXP_NODE_ELEM);
1503 return -1;
1504 }
1505
Michal Vasko9f96a052020-03-10 09:41:45 +01001506 /* 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 +02001507 /* 2nd is before 1st */
Michal Vasko69730152020-10-09 16:30:07 +02001508 if (((item1->type == LYXP_NODE_TEXT) &&
1509 ((item2->type == LYXP_NODE_ELEM) || (item2->type == LYXP_NODE_META))) ||
1510 ((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_ELEM)) ||
1511 (((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_META)) &&
1512 (meta_pos1 > meta_pos2))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001513 return 1;
1514 }
1515
Michal Vasko9f96a052020-03-10 09:41:45 +01001516 /* 1st META - any pos - 2nd TEXT, 1st META <pos< - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001517 /* 2nd is after 1st */
1518 return -1;
1519}
1520
1521/**
1522 * @brief Set cast for comparisons.
1523 *
1524 * @param[in] trg Target set to cast source into.
1525 * @param[in] src Source set.
1526 * @param[in] type Target set type.
1527 * @param[in] src_idx Source set node index.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001528 * @return LY_ERR
1529 */
1530static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001531set_comp_cast(struct lyxp_set *trg, struct lyxp_set *src, enum lyxp_set_type type, uint32_t src_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001532{
1533 assert(src->type == LYXP_SET_NODE_SET);
1534
1535 set_init(trg, src);
1536
1537 /* insert node into target set */
1538 set_insert_node(trg, src->val.nodes[src_idx].node, src->val.nodes[src_idx].pos, src->val.nodes[src_idx].type, 0);
1539
1540 /* cast target set appropriately */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001541 return lyxp_set_cast(trg, type);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001542}
1543
Michal Vasko4c7763f2020-07-27 17:40:37 +02001544/**
1545 * @brief Set content canonization for comparisons.
1546 *
1547 * @param[in] trg Target set to put the canononized source into.
1548 * @param[in] src Source set.
1549 * @param[in] xp_node Source XPath node/meta to use for canonization.
1550 * @return LY_ERR
1551 */
1552static LY_ERR
1553set_comp_canonize(struct lyxp_set *trg, const struct lyxp_set *src, const struct lyxp_set_node *xp_node)
1554{
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001555 const struct lysc_type *type = NULL;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001556 struct lyd_value val;
1557 struct ly_err_item *err = NULL;
1558 char *str, *ptr;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001559 LY_ERR rc;
1560
1561 /* is there anything to canonize even? */
1562 if ((src->type == LYXP_SET_NUMBER) || (src->type == LYXP_SET_STRING)) {
1563 /* do we have a type to use for canonization? */
1564 if ((xp_node->type == LYXP_NODE_ELEM) && (xp_node->node->schema->nodetype & LYD_NODE_TERM)) {
1565 type = ((struct lyd_node_term *)xp_node->node)->value.realtype;
1566 } else if (xp_node->type == LYXP_NODE_META) {
1567 type = ((struct lyd_meta *)xp_node->node)->value.realtype;
1568 }
1569 }
1570 if (!type) {
1571 goto fill;
1572 }
1573
1574 if (src->type == LYXP_SET_NUMBER) {
1575 /* canonize number */
1576 if (asprintf(&str, "%Lf", src->val.num) == -1) {
1577 LOGMEM(src->ctx);
1578 return LY_EMEM;
1579 }
1580 } else {
1581 /* canonize string */
1582 str = strdup(src->val.str);
1583 }
1584
1585 /* ignore errors, the value may not satisfy schema constraints */
Radek Krejci0b013302021-03-29 15:22:32 +02001586 rc = type->plugin->store(src->ctx, type, str, strlen(str), LYPLG_TYPE_STORE_DYNAMIC, src->format, src->prefix_data,
Michal Vasko405cc9e2020-12-01 12:01:27 +01001587 LYD_HINT_DATA, xp_node->node->schema, &val, NULL, &err);
Michal Vasko4c7763f2020-07-27 17:40:37 +02001588 ly_err_free(err);
1589 if (rc) {
1590 /* invalid value */
Radek IÅ¡a48460222021-03-02 15:18:32 +01001591 /* function store automaticaly dealloc value when fail */
Michal Vasko4c7763f2020-07-27 17:40:37 +02001592 goto fill;
1593 }
1594
Michal Vasko4c7763f2020-07-27 17:40:37 +02001595 /* use the canonized value */
1596 set_init(trg, src);
1597 trg->type = src->type;
1598 if (src->type == LYXP_SET_NUMBER) {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001599 trg->val.num = strtold(type->plugin->print(src->ctx, &val, LY_VALUE_CANON, NULL, NULL, NULL), &ptr);
Michal Vasko4c7763f2020-07-27 17:40:37 +02001600 LY_CHECK_ERR_RET(ptr[0], LOGINT(src->ctx), LY_EINT);
1601 } else {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001602 trg->val.str = strdup(type->plugin->print(src->ctx, &val, LY_VALUE_CANON, NULL, NULL, NULL));
Michal Vasko4c7763f2020-07-27 17:40:37 +02001603 }
1604 type->plugin->free(src->ctx, &val);
1605 return LY_SUCCESS;
1606
1607fill:
1608 /* no canonization needed/possible */
1609 set_fill_set(trg, src);
1610 return LY_SUCCESS;
1611}
1612
Michal Vasko03ff5a72019-09-11 13:49:33 +02001613#ifndef NDEBUG
1614
1615/**
1616 * @brief Bubble sort @p set into XPath document order.
1617 * Context position aware. Unused in the 'Release' build target.
1618 *
1619 * @param[in] set Set to sort.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001620 * @return How many times the whole set was traversed - 1 (if set was sorted, returns 0).
1621 */
1622static int
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001623set_sort(struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001624{
1625 uint32_t i, j;
Radek Krejci1deb5be2020-08-26 16:43:36 +02001626 int ret = 0, cmp;
Radek Krejci857189e2020-09-01 13:26:36 +02001627 ly_bool inverted, change;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001628 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001629 struct lyxp_set_node item;
1630 struct lyxp_set_hash_node hnode;
1631 uint64_t hash;
1632
Michal Vasko3cf8fbf2020-05-27 15:21:21 +02001633 if ((set->type != LYXP_SET_NODE_SET) || (set->used < 2)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001634 return 0;
1635 }
1636
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001637 /* find first top-level node to be used as anchor for positions */
Michal Vasko9e685082021-01-29 14:49:09 +01001638 for (root = set->cur_node; root->parent; root = lyd_parent(root)) {}
Michal Vaskod989ba02020-08-24 10:59:24 +02001639 for ( ; root->prev->next; root = root->prev) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001640
1641 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001642 if (set_assign_pos(set, root, set->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001643 return -1;
1644 }
1645
1646 LOGDBG(LY_LDGXPATH, "SORT BEGIN");
1647 print_set_debug(set);
1648
1649 for (i = 0; i < set->used; ++i) {
1650 inverted = 0;
1651 change = 0;
1652
1653 for (j = 1; j < set->used - i; ++j) {
1654 /* compare node positions */
1655 if (inverted) {
1656 cmp = set_sort_compare(&set->val.nodes[j], &set->val.nodes[j - 1]);
1657 } else {
1658 cmp = set_sort_compare(&set->val.nodes[j - 1], &set->val.nodes[j]);
1659 }
1660
1661 /* swap if needed */
1662 if ((inverted && (cmp < 0)) || (!inverted && (cmp > 0))) {
1663 change = 1;
1664
1665 item = set->val.nodes[j - 1];
1666 set->val.nodes[j - 1] = set->val.nodes[j];
1667 set->val.nodes[j] = item;
1668 } else {
1669 /* whether node_pos1 should be smaller than node_pos2 or the other way around */
1670 inverted = !inverted;
1671 }
1672 }
1673
1674 ++ret;
1675
1676 if (!change) {
1677 break;
1678 }
1679 }
1680
1681 LOGDBG(LY_LDGXPATH, "SORT END %d", ret);
1682 print_set_debug(set);
1683
1684 /* check node hashes */
1685 if (set->used >= LYD_HT_MIN_ITEMS) {
1686 assert(set->ht);
1687 for (i = 0; i < set->used; ++i) {
1688 hnode.node = set->val.nodes[i].node;
1689 hnode.type = set->val.nodes[i].type;
1690
1691 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
1692 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
1693 hash = dict_hash_multi(hash, NULL, 0);
1694
1695 assert(!lyht_find(set->ht, &hnode, hash, NULL));
1696 }
1697 }
1698
1699 return ret - 1;
1700}
1701
1702/**
1703 * @brief Remove duplicate entries in a sorted node set.
1704 *
1705 * @param[in] set Sorted set to check.
1706 * @return LY_ERR (LY_EEXIST if some duplicates are found)
1707 */
1708static LY_ERR
1709set_sorted_dup_node_clean(struct lyxp_set *set)
1710{
1711 uint32_t i = 0;
1712 LY_ERR ret = LY_SUCCESS;
1713
1714 if (set->used > 1) {
1715 while (i < set->used - 1) {
Michal Vasko69730152020-10-09 16:30:07 +02001716 if ((set->val.nodes[i].node == set->val.nodes[i + 1].node) &&
1717 (set->val.nodes[i].type == set->val.nodes[i + 1].type)) {
Michal Vasko2caefc12019-11-14 16:07:56 +01001718 set_remove_node_none(set, i + 1);
Michal Vasko57eab132019-09-24 11:46:26 +02001719 ret = LY_EEXIST;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001720 }
Michal Vasko57eab132019-09-24 11:46:26 +02001721 ++i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001722 }
1723 }
1724
Michal Vasko2caefc12019-11-14 16:07:56 +01001725 set_remove_nodes_none(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001726 return ret;
1727}
1728
1729#endif
1730
1731/**
1732 * @brief Merge 2 sorted sets into one.
1733 *
1734 * @param[in,out] trg Set to merge into. Duplicates are removed.
1735 * @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 +02001736 * @return LY_ERR
1737 */
1738static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001739set_sorted_merge(struct lyxp_set *trg, struct lyxp_set *src)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001740{
1741 uint32_t i, j, k, count, dup_count;
1742 int cmp;
1743 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001744
Michal Vaskod3678892020-05-21 10:06:58 +02001745 if ((trg->type != LYXP_SET_NODE_SET) || (src->type != LYXP_SET_NODE_SET)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001746 return LY_EINVAL;
1747 }
1748
Michal Vaskod3678892020-05-21 10:06:58 +02001749 if (!src->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001750 return LY_SUCCESS;
Michal Vaskod3678892020-05-21 10:06:58 +02001751 } else if (!trg->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001752 set_fill_set(trg, src);
Michal Vaskod3678892020-05-21 10:06:58 +02001753 lyxp_set_free_content(src);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001754 return LY_SUCCESS;
1755 }
1756
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001757 /* find first top-level node to be used as anchor for positions */
Michal Vasko9e685082021-01-29 14:49:09 +01001758 for (root = trg->cur_node; root->parent; root = lyd_parent(root)) {}
Michal Vaskod989ba02020-08-24 10:59:24 +02001759 for ( ; root->prev->next; root = root->prev) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001760
1761 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001762 if (set_assign_pos(trg, root, trg->root_type) || set_assign_pos(src, root, src->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001763 return LY_EINT;
1764 }
1765
1766#ifndef NDEBUG
1767 LOGDBG(LY_LDGXPATH, "MERGE target");
1768 print_set_debug(trg);
1769 LOGDBG(LY_LDGXPATH, "MERGE source");
1770 print_set_debug(src);
1771#endif
1772
1773 /* make memory for the merge (duplicates are not detected yet, so space
1774 * will likely be wasted on them, too bad) */
1775 if (trg->size - trg->used < src->used) {
1776 trg->size = trg->used + src->used;
1777
1778 trg->val.nodes = ly_realloc(trg->val.nodes, trg->size * sizeof *trg->val.nodes);
1779 LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx), LY_EMEM);
1780 }
1781
1782 i = 0;
1783 j = 0;
1784 count = 0;
1785 dup_count = 0;
1786 do {
1787 cmp = set_sort_compare(&src->val.nodes[i], &trg->val.nodes[j]);
1788 if (!cmp) {
1789 if (!count) {
1790 /* duplicate, just skip it */
1791 ++i;
1792 ++j;
1793 } else {
1794 /* we are copying something already, so let's copy the duplicate too,
1795 * we are hoping that afterwards there are some more nodes to
1796 * copy and this way we can copy them all together */
1797 ++count;
1798 ++dup_count;
1799 ++i;
1800 ++j;
1801 }
1802 } else if (cmp < 0) {
1803 /* inserting src node into trg, just remember it for now */
1804 ++count;
1805 ++i;
1806
1807 /* insert the hash now */
1808 set_insert_node_hash(trg, src->val.nodes[i - 1].node, src->val.nodes[i - 1].type);
1809 } else if (count) {
1810copy_nodes:
1811 /* time to actually copy the nodes, we have found the largest block of nodes */
1812 memmove(&trg->val.nodes[j + (count - dup_count)],
1813 &trg->val.nodes[j],
1814 (trg->used - j) * sizeof *trg->val.nodes);
1815 memcpy(&trg->val.nodes[j - dup_count], &src->val.nodes[i - count], count * sizeof *src->val.nodes);
1816
1817 trg->used += count - dup_count;
1818 /* do not change i, except the copying above, we are basically doing exactly what is in the else branch below */
1819 j += count - dup_count;
1820
1821 count = 0;
1822 dup_count = 0;
1823 } else {
1824 ++j;
1825 }
1826 } while ((i < src->used) && (j < trg->used));
1827
1828 if ((i < src->used) || count) {
1829 /* insert all the hashes first */
1830 for (k = i; k < src->used; ++k) {
1831 set_insert_node_hash(trg, src->val.nodes[k].node, src->val.nodes[k].type);
1832 }
1833
1834 /* loop ended, but we need to copy something at trg end */
1835 count += src->used - i;
1836 i = src->used;
1837 goto copy_nodes;
1838 }
1839
1840 /* we are inserting hashes before the actual node insert, which causes
1841 * situations when there were initially not enough items for a hash table,
1842 * but even after some were inserted, hash table was not created (during
1843 * insertion the number of items is not updated yet) */
1844 if (!trg->ht && (trg->used >= LYD_HT_MIN_ITEMS)) {
1845 set_insert_node_hash(trg, NULL, 0);
1846 }
1847
1848#ifndef NDEBUG
1849 LOGDBG(LY_LDGXPATH, "MERGE result");
1850 print_set_debug(trg);
1851#endif
1852
Michal Vaskod3678892020-05-21 10:06:58 +02001853 lyxp_set_free_content(src);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001854 return LY_SUCCESS;
1855}
1856
1857/*
1858 * (re)parse functions
1859 *
1860 * Parse functions parse the expression into
1861 * tokens (syntactic analysis).
1862 *
1863 * Reparse functions perform semantic analysis
1864 * (do not save the result, just a check) of
1865 * the expression and fill repeat indices.
1866 */
1867
Michal Vasko14676352020-05-29 11:35:55 +02001868LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02001869lyxp_check_token(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t tok_idx, enum lyxp_token want_tok)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001870{
Michal Vasko004d3152020-06-11 19:59:22 +02001871 if (exp->used == tok_idx) {
Michal Vasko14676352020-05-29 11:35:55 +02001872 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001873 LOGVAL(ctx, LY_VCODE_XP_EOF);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001874 }
Michal Vasko14676352020-05-29 11:35:55 +02001875 return LY_EINCOMPLETE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001876 }
1877
Michal Vasko004d3152020-06-11 19:59:22 +02001878 if (want_tok && (exp->tokens[tok_idx] != want_tok)) {
Michal Vasko14676352020-05-29 11:35:55 +02001879 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001880 LOGVAL(ctx, LY_VCODE_XP_INTOK2, lyxp_print_token(exp->tokens[tok_idx]),
Michal Vasko0b468e62020-10-19 09:33:04 +02001881 &exp->expr[exp->tok_pos[tok_idx]], lyxp_print_token(want_tok));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001882 }
Michal Vasko14676352020-05-29 11:35:55 +02001883 return LY_ENOT;
1884 }
1885
1886 return LY_SUCCESS;
1887}
1888
Michal Vasko004d3152020-06-11 19:59:22 +02001889LY_ERR
1890lyxp_next_token(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_token want_tok)
1891{
1892 LY_CHECK_RET(lyxp_check_token(ctx, exp, *tok_idx, want_tok));
1893
1894 /* skip the token */
1895 ++(*tok_idx);
1896
1897 return LY_SUCCESS;
1898}
1899
Michal Vasko14676352020-05-29 11:35:55 +02001900/* just like lyxp_check_token() but tests for 2 tokens */
1901static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02001902exp_check_token2(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t tok_idx, enum lyxp_token want_tok1,
Radek Krejci0f969882020-08-21 16:56:47 +02001903 enum lyxp_token want_tok2)
Michal Vasko14676352020-05-29 11:35:55 +02001904{
Michal Vasko004d3152020-06-11 19:59:22 +02001905 if (exp->used == tok_idx) {
Michal Vasko14676352020-05-29 11:35:55 +02001906 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001907 LOGVAL(ctx, LY_VCODE_XP_EOF);
Michal Vasko14676352020-05-29 11:35:55 +02001908 }
1909 return LY_EINCOMPLETE;
1910 }
1911
Michal Vasko004d3152020-06-11 19:59:22 +02001912 if ((exp->tokens[tok_idx] != want_tok1) && (exp->tokens[tok_idx] != want_tok2)) {
Michal Vasko14676352020-05-29 11:35:55 +02001913 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001914 LOGVAL(ctx, LY_VCODE_XP_INTOK, lyxp_print_token(exp->tokens[tok_idx]),
Michal Vasko0b468e62020-10-19 09:33:04 +02001915 &exp->expr[exp->tok_pos[tok_idx]]);
Michal Vasko14676352020-05-29 11:35:55 +02001916 }
1917 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001918 }
1919
1920 return LY_SUCCESS;
1921}
1922
1923/**
1924 * @brief Stack operation push on the repeat array.
1925 *
1926 * @param[in] exp Expression to use.
Michal Vasko004d3152020-06-11 19:59:22 +02001927 * @param[in] tok_idx Position in the expresion \p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001928 * @param[in] repeat_op_idx Index from \p exp of the operator token. This value is pushed.
1929 */
1930static void
Michal Vasko004d3152020-06-11 19:59:22 +02001931exp_repeat_push(struct lyxp_expr *exp, uint16_t tok_idx, uint16_t repeat_op_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001932{
1933 uint16_t i;
1934
Michal Vasko004d3152020-06-11 19:59:22 +02001935 if (exp->repeat[tok_idx]) {
Radek Krejci1e008d22020-08-17 11:37:37 +02001936 for (i = 0; exp->repeat[tok_idx][i]; ++i) {}
Michal Vasko004d3152020-06-11 19:59:22 +02001937 exp->repeat[tok_idx] = realloc(exp->repeat[tok_idx], (i + 2) * sizeof *exp->repeat[tok_idx]);
1938 LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), );
1939 exp->repeat[tok_idx][i] = repeat_op_idx;
1940 exp->repeat[tok_idx][i + 1] = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001941 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02001942 exp->repeat[tok_idx] = calloc(2, sizeof *exp->repeat[tok_idx]);
1943 LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), );
1944 exp->repeat[tok_idx][0] = repeat_op_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001945 }
1946}
1947
1948/**
1949 * @brief Reparse Predicate. Logs directly on error.
1950 *
1951 * [7] Predicate ::= '[' Expr ']'
1952 *
1953 * @param[in] ctx Context for logging.
1954 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02001955 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02001956 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001957 * @return LY_ERR
1958 */
1959static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02001960reparse_predicate(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001961{
1962 LY_ERR rc;
1963
Michal Vasko004d3152020-06-11 19:59:22 +02001964 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001965 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02001966 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001967
aPiecekbf968d92021-05-27 14:35:05 +02001968 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001969 LY_CHECK_RET(rc);
1970
Michal Vasko004d3152020-06-11 19:59:22 +02001971 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001972 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02001973 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001974
1975 return LY_SUCCESS;
1976}
1977
1978/**
1979 * @brief Reparse RelativeLocationPath. Logs directly on error.
1980 *
1981 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
1982 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
1983 * [6] NodeTest ::= NameTest | NodeType '(' ')'
1984 *
1985 * @param[in] ctx Context for logging.
1986 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02001987 * @param[in] tok_idx Position in the expression \p exp.
aPiecekbf968d92021-05-27 14:35:05 +02001988 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001989 * @return LY_ERR (LY_EINCOMPLETE on forward reference)
1990 */
1991static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02001992reparse_relative_location_path(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001993{
1994 LY_ERR rc;
1995
Michal Vasko004d3152020-06-11 19:59:22 +02001996 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001997 LY_CHECK_RET(rc);
1998
1999 goto step;
2000 do {
2001 /* '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02002002 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002003
Michal Vasko004d3152020-06-11 19:59:22 +02002004 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002005 LY_CHECK_RET(rc);
2006step:
2007 /* Step */
Michal Vasko004d3152020-06-11 19:59:22 +02002008 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002009 case LYXP_TOKEN_DOT:
Michal Vasko004d3152020-06-11 19:59:22 +02002010 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002011 break;
2012
2013 case LYXP_TOKEN_DDOT:
Michal Vasko004d3152020-06-11 19:59:22 +02002014 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002015 break;
2016
2017 case LYXP_TOKEN_AT:
Michal Vasko004d3152020-06-11 19:59:22 +02002018 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002019
Michal Vasko004d3152020-06-11 19:59:22 +02002020 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002021 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002022 if ((exp->tokens[*tok_idx] != LYXP_TOKEN_NAMETEST) && (exp->tokens[*tok_idx] != LYXP_TOKEN_NODETYPE)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002023 LOGVAL(ctx, LY_VCODE_XP_INTOK, lyxp_print_token(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002024 return LY_EVALID;
2025 }
Radek Krejci0f969882020-08-21 16:56:47 +02002026 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02002027 case LYXP_TOKEN_NAMETEST:
Michal Vasko004d3152020-06-11 19:59:22 +02002028 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002029 goto reparse_predicate;
2030 break;
2031
2032 case LYXP_TOKEN_NODETYPE:
Michal Vasko004d3152020-06-11 19:59:22 +02002033 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002034
2035 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02002036 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002037 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002038 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002039
2040 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02002041 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002042 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002043 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002044
2045reparse_predicate:
2046 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002047 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
aPiecekbf968d92021-05-27 14:35:05 +02002048 rc = reparse_predicate(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002049 LY_CHECK_RET(rc);
2050 }
2051 break;
2052 default:
Radek Krejci2efc45b2020-12-22 16:25:44 +01002053 LOGVAL(ctx, LY_VCODE_XP_INTOK, lyxp_print_token(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002054 return LY_EVALID;
2055 }
Michal Vasko004d3152020-06-11 19:59:22 +02002056 } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02002057
2058 return LY_SUCCESS;
2059}
2060
2061/**
2062 * @brief Reparse AbsoluteLocationPath. Logs directly on error.
2063 *
2064 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
2065 *
2066 * @param[in] ctx Context for logging.
2067 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002068 * @param[in] tok_idx Position in the expression \p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002069 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002070 * @return LY_ERR
2071 */
2072static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002073reparse_absolute_location_path(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002074{
2075 LY_ERR rc;
2076
Michal Vasko004d3152020-06-11 19:59:22 +02002077 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 +02002078
2079 /* '/' RelativeLocationPath? */
Michal Vasko004d3152020-06-11 19:59:22 +02002080 if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002081 /* '/' */
Michal Vasko004d3152020-06-11 19:59:22 +02002082 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002083
Michal Vasko004d3152020-06-11 19:59:22 +02002084 if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002085 return LY_SUCCESS;
2086 }
Michal Vasko004d3152020-06-11 19:59:22 +02002087 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002088 case LYXP_TOKEN_DOT:
2089 case LYXP_TOKEN_DDOT:
2090 case LYXP_TOKEN_AT:
2091 case LYXP_TOKEN_NAMETEST:
2092 case LYXP_TOKEN_NODETYPE:
aPiecekbf968d92021-05-27 14:35:05 +02002093 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002094 LY_CHECK_RET(rc);
Radek Krejci0f969882020-08-21 16:56:47 +02002095 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02002096 default:
2097 break;
2098 }
2099
Michal Vasko03ff5a72019-09-11 13:49:33 +02002100 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02002101 /* '//' RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002102 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002103
aPiecekbf968d92021-05-27 14:35:05 +02002104 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002105 LY_CHECK_RET(rc);
2106 }
2107
2108 return LY_SUCCESS;
2109}
2110
2111/**
2112 * @brief Reparse FunctionCall. Logs directly on error.
2113 *
2114 * [9] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
2115 *
2116 * @param[in] ctx Context for logging.
2117 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002118 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002119 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002120 * @return LY_ERR
2121 */
2122static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002123reparse_function_call(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002124{
Radek Krejci1deb5be2020-08-26 16:43:36 +02002125 int8_t min_arg_count = -1;
2126 uint32_t arg_count, max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002127 uint16_t func_tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002128 LY_ERR rc;
2129
Michal Vasko004d3152020-06-11 19:59:22 +02002130 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_FUNCNAME);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002131 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002132 func_tok_idx = *tok_idx;
2133 switch (exp->tok_len[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002134 case 3:
Michal Vasko004d3152020-06-11 19:59:22 +02002135 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002136 min_arg_count = 1;
2137 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002138 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002139 min_arg_count = 1;
2140 max_arg_count = 1;
2141 }
2142 break;
2143 case 4:
Michal Vasko004d3152020-06-11 19:59:22 +02002144 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002145 min_arg_count = 1;
2146 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002147 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002148 min_arg_count = 0;
2149 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002150 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002151 min_arg_count = 0;
2152 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002153 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002154 min_arg_count = 0;
2155 max_arg_count = 0;
2156 }
2157 break;
2158 case 5:
Michal Vasko004d3152020-06-11 19:59:22 +02002159 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002160 min_arg_count = 1;
2161 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002162 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002163 min_arg_count = 0;
2164 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002165 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002166 min_arg_count = 1;
2167 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002168 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002169 min_arg_count = 1;
2170 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002171 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002172 min_arg_count = 1;
2173 max_arg_count = 1;
2174 }
2175 break;
2176 case 6:
Michal Vasko004d3152020-06-11 19:59:22 +02002177 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002178 min_arg_count = 2;
Radek Krejci1deb5be2020-08-26 16:43:36 +02002179 max_arg_count = UINT32_MAX;
Michal Vasko004d3152020-06-11 19:59:22 +02002180 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002181 min_arg_count = 0;
2182 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002183 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002184 min_arg_count = 0;
2185 max_arg_count = 1;
2186 }
2187 break;
2188 case 7:
Michal Vasko004d3152020-06-11 19:59:22 +02002189 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002190 min_arg_count = 1;
2191 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002192 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002193 min_arg_count = 1;
2194 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002195 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002196 min_arg_count = 0;
2197 max_arg_count = 0;
2198 }
2199 break;
2200 case 8:
Michal Vasko004d3152020-06-11 19:59:22 +02002201 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002202 min_arg_count = 2;
2203 max_arg_count = 2;
Michal Vasko004d3152020-06-11 19:59:22 +02002204 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002205 min_arg_count = 0;
2206 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002207 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002208 min_arg_count = 2;
2209 max_arg_count = 2;
2210 }
2211 break;
2212 case 9:
Michal Vasko004d3152020-06-11 19:59:22 +02002213 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002214 min_arg_count = 2;
2215 max_arg_count = 3;
Michal Vasko004d3152020-06-11 19:59:22 +02002216 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002217 min_arg_count = 3;
2218 max_arg_count = 3;
2219 }
2220 break;
2221 case 10:
Michal Vasko004d3152020-06-11 19:59:22 +02002222 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002223 min_arg_count = 0;
2224 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002225 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002226 min_arg_count = 1;
2227 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002228 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002229 min_arg_count = 2;
2230 max_arg_count = 2;
2231 }
2232 break;
2233 case 11:
Michal Vasko004d3152020-06-11 19:59:22 +02002234 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002235 min_arg_count = 2;
2236 max_arg_count = 2;
2237 }
2238 break;
2239 case 12:
Michal Vasko004d3152020-06-11 19:59:22 +02002240 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002241 min_arg_count = 2;
2242 max_arg_count = 2;
2243 }
2244 break;
2245 case 13:
Michal Vasko004d3152020-06-11 19:59:22 +02002246 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002247 min_arg_count = 0;
2248 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002249 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002250 min_arg_count = 0;
2251 max_arg_count = 1;
2252 }
2253 break;
2254 case 15:
Michal Vasko004d3152020-06-11 19:59:22 +02002255 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002256 min_arg_count = 0;
2257 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002258 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002259 min_arg_count = 2;
2260 max_arg_count = 2;
2261 }
2262 break;
2263 case 16:
Michal Vasko004d3152020-06-11 19:59:22 +02002264 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002265 min_arg_count = 2;
2266 max_arg_count = 2;
2267 }
2268 break;
2269 case 20:
Michal Vasko004d3152020-06-11 19:59:22 +02002270 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002271 min_arg_count = 2;
2272 max_arg_count = 2;
2273 }
2274 break;
2275 }
2276 if (min_arg_count == -1) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002277 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 +02002278 return LY_EINVAL;
2279 }
Michal Vasko004d3152020-06-11 19:59:22 +02002280 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002281
2282 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02002283 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002284 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002285 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002286
2287 /* ( Expr ( ',' Expr )* )? */
2288 arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002289 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002290 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002291 if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002292 ++arg_count;
aPiecekbf968d92021-05-27 14:35:05 +02002293 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002294 LY_CHECK_RET(rc);
2295 }
Michal Vasko004d3152020-06-11 19:59:22 +02002296 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) {
2297 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002298
2299 ++arg_count;
aPiecekbf968d92021-05-27 14:35:05 +02002300 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002301 LY_CHECK_RET(rc);
2302 }
2303
2304 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02002305 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002306 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002307 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002308
Radek Krejci857189e2020-09-01 13:26:36 +02002309 if ((arg_count < (uint32_t)min_arg_count) || (arg_count > max_arg_count)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002310 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 +02002311 return LY_EVALID;
2312 }
2313
2314 return LY_SUCCESS;
2315}
2316
2317/**
2318 * @brief Reparse PathExpr. Logs directly on error.
2319 *
2320 * [10] PathExpr ::= LocationPath | PrimaryExpr Predicate*
2321 * | PrimaryExpr Predicate* '/' RelativeLocationPath
2322 * | PrimaryExpr Predicate* '//' RelativeLocationPath
2323 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
2324 * [8] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall
2325 *
2326 * @param[in] ctx Context for logging.
2327 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002328 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002329 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002330 * @return LY_ERR
2331 */
2332static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002333reparse_path_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002334{
2335 LY_ERR rc;
2336
Michal Vasko004d3152020-06-11 19:59:22 +02002337 if (lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko14676352020-05-29 11:35:55 +02002338 return LY_EVALID;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002339 }
2340
Michal Vasko004d3152020-06-11 19:59:22 +02002341 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002342 case LYXP_TOKEN_PAR1:
2343 /* '(' Expr ')' Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002344 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002345
aPiecekbf968d92021-05-27 14:35:05 +02002346 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002347 LY_CHECK_RET(rc);
2348
Michal Vasko004d3152020-06-11 19:59:22 +02002349 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002350 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002351 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002352 goto predicate;
2353 break;
2354 case LYXP_TOKEN_DOT:
2355 case LYXP_TOKEN_DDOT:
2356 case LYXP_TOKEN_AT:
2357 case LYXP_TOKEN_NAMETEST:
2358 case LYXP_TOKEN_NODETYPE:
2359 /* RelativeLocationPath */
aPiecekbf968d92021-05-27 14:35:05 +02002360 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002361 LY_CHECK_RET(rc);
2362 break;
2363 case LYXP_TOKEN_FUNCNAME:
2364 /* FunctionCall */
aPiecekbf968d92021-05-27 14:35:05 +02002365 rc = reparse_function_call(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002366 LY_CHECK_RET(rc);
2367 goto predicate;
2368 break;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002369 case LYXP_TOKEN_OPER_PATH:
2370 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02002371 /* AbsoluteLocationPath */
aPiecekbf968d92021-05-27 14:35:05 +02002372 rc = reparse_absolute_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002373 LY_CHECK_RET(rc);
2374 break;
2375 case LYXP_TOKEN_LITERAL:
2376 /* Literal */
Michal Vasko004d3152020-06-11 19:59:22 +02002377 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002378 goto predicate;
2379 break;
2380 case LYXP_TOKEN_NUMBER:
2381 /* Number */
Michal Vasko004d3152020-06-11 19:59:22 +02002382 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002383 goto predicate;
2384 break;
2385 default:
Radek Krejci2efc45b2020-12-22 16:25:44 +01002386 LOGVAL(ctx, LY_VCODE_XP_INTOK, lyxp_print_token(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002387 return LY_EVALID;
2388 }
2389
2390 return LY_SUCCESS;
2391
2392predicate:
2393 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002394 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
aPiecekbf968d92021-05-27 14:35:05 +02002395 rc = reparse_predicate(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002396 LY_CHECK_RET(rc);
2397 }
2398
2399 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002400 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002401
2402 /* '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02002403 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002404
aPiecekbf968d92021-05-27 14:35:05 +02002405 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002406 LY_CHECK_RET(rc);
2407 }
2408
2409 return LY_SUCCESS;
2410}
2411
2412/**
2413 * @brief Reparse UnaryExpr. Logs directly on error.
2414 *
2415 * [17] UnaryExpr ::= UnionExpr | '-' UnaryExpr
2416 * [18] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
2417 *
2418 * @param[in] ctx Context for logging.
2419 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002420 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002421 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002422 * @return LY_ERR
2423 */
2424static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002425reparse_unary_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002426{
2427 uint16_t prev_exp;
2428 LY_ERR rc;
2429
2430 /* ('-')* */
Michal Vasko004d3152020-06-11 19:59:22 +02002431 prev_exp = *tok_idx;
Michal Vasko69730152020-10-09 16:30:07 +02002432 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2433 (exp->expr[exp->tok_pos[*tok_idx]] == '-')) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002434 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNARY);
Michal Vasko004d3152020-06-11 19:59:22 +02002435 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002436 }
2437
2438 /* PathExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002439 prev_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002440 rc = reparse_path_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002441 LY_CHECK_RET(rc);
2442
2443 /* ('|' PathExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002444 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_UNI)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002445 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNION);
Michal Vasko004d3152020-06-11 19:59:22 +02002446 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002447
aPiecekbf968d92021-05-27 14:35:05 +02002448 rc = reparse_path_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002449 LY_CHECK_RET(rc);
2450 }
2451
2452 return LY_SUCCESS;
2453}
2454
2455/**
2456 * @brief Reparse AdditiveExpr. Logs directly on error.
2457 *
2458 * [15] AdditiveExpr ::= MultiplicativeExpr
2459 * | AdditiveExpr '+' MultiplicativeExpr
2460 * | AdditiveExpr '-' MultiplicativeExpr
2461 * [16] MultiplicativeExpr ::= UnaryExpr
2462 * | MultiplicativeExpr '*' UnaryExpr
2463 * | MultiplicativeExpr 'div' UnaryExpr
2464 * | MultiplicativeExpr 'mod' UnaryExpr
2465 *
2466 * @param[in] ctx Context for logging.
2467 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002468 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002469 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002470 * @return LY_ERR
2471 */
2472static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002473reparse_additive_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002474{
2475 uint16_t prev_add_exp, prev_mul_exp;
2476 LY_ERR rc;
2477
Michal Vasko004d3152020-06-11 19:59:22 +02002478 prev_add_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002479 goto reparse_multiplicative_expr;
2480
2481 /* ('+' / '-' MultiplicativeExpr)* */
Michal Vasko69730152020-10-09 16:30:07 +02002482 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2483 ((exp->expr[exp->tok_pos[*tok_idx]] == '+') || (exp->expr[exp->tok_pos[*tok_idx]] == '-'))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002484 exp_repeat_push(exp, prev_add_exp, LYXP_EXPR_ADDITIVE);
Michal Vasko004d3152020-06-11 19:59:22 +02002485 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002486
2487reparse_multiplicative_expr:
2488 /* UnaryExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002489 prev_mul_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002490 rc = reparse_unary_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002491 LY_CHECK_RET(rc);
2492
2493 /* ('*' / 'div' / 'mod' UnaryExpr)* */
Michal Vasko69730152020-10-09 16:30:07 +02002494 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2495 ((exp->expr[exp->tok_pos[*tok_idx]] == '*') || (exp->tok_len[*tok_idx] == 3))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002496 exp_repeat_push(exp, prev_mul_exp, LYXP_EXPR_MULTIPLICATIVE);
Michal Vasko004d3152020-06-11 19:59:22 +02002497 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002498
aPiecekbf968d92021-05-27 14:35:05 +02002499 rc = reparse_unary_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002500 LY_CHECK_RET(rc);
2501 }
2502 }
2503
2504 return LY_SUCCESS;
2505}
2506
2507/**
2508 * @brief Reparse EqualityExpr. Logs directly on error.
2509 *
2510 * [13] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
2511 * | EqualityExpr '!=' RelationalExpr
2512 * [14] RelationalExpr ::= AdditiveExpr
2513 * | RelationalExpr '<' AdditiveExpr
2514 * | RelationalExpr '>' AdditiveExpr
2515 * | RelationalExpr '<=' AdditiveExpr
2516 * | RelationalExpr '>=' AdditiveExpr
2517 *
2518 * @param[in] ctx Context for logging.
2519 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002520 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002521 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002522 * @return LY_ERR
2523 */
2524static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002525reparse_equality_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002526{
2527 uint16_t prev_eq_exp, prev_rel_exp;
2528 LY_ERR rc;
2529
Michal Vasko004d3152020-06-11 19:59:22 +02002530 prev_eq_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002531 goto reparse_additive_expr;
2532
2533 /* ('=' / '!=' RelationalExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002534 while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_EQUAL, LYXP_TOKEN_OPER_NEQUAL)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002535 exp_repeat_push(exp, prev_eq_exp, LYXP_EXPR_EQUALITY);
Michal Vasko004d3152020-06-11 19:59:22 +02002536 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002537
2538reparse_additive_expr:
2539 /* AdditiveExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002540 prev_rel_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002541 rc = reparse_additive_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002542 LY_CHECK_RET(rc);
2543
2544 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002545 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_COMP)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002546 exp_repeat_push(exp, prev_rel_exp, LYXP_EXPR_RELATIONAL);
Michal Vasko004d3152020-06-11 19:59:22 +02002547 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002548
aPiecekbf968d92021-05-27 14:35:05 +02002549 rc = reparse_additive_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002550 LY_CHECK_RET(rc);
2551 }
2552 }
2553
2554 return LY_SUCCESS;
2555}
2556
2557/**
2558 * @brief Reparse OrExpr. Logs directly on error.
2559 *
2560 * [11] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
2561 * [12] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
2562 *
2563 * @param[in] ctx Context for logging.
2564 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002565 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002566 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002567 * @return LY_ERR
2568 */
2569static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002570reparse_or_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002571{
2572 uint16_t prev_or_exp, prev_and_exp;
2573 LY_ERR rc;
2574
aPiecekbf968d92021-05-27 14:35:05 +02002575 ++depth;
2576 LY_CHECK_ERR_RET(depth > LYXP_MAX_BLOCK_DEPTH, LOGVAL(ctx, LY_VCODE_XP_DEPTH), LY_EINVAL);
2577
Michal Vasko004d3152020-06-11 19:59:22 +02002578 prev_or_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002579 goto reparse_equality_expr;
2580
2581 /* ('or' AndExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002582 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_LOG) && (exp->tok_len[*tok_idx] == 2)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002583 exp_repeat_push(exp, prev_or_exp, LYXP_EXPR_OR);
Michal Vasko004d3152020-06-11 19:59:22 +02002584 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002585
2586reparse_equality_expr:
2587 /* EqualityExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002588 prev_and_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002589 rc = reparse_equality_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002590 LY_CHECK_RET(rc);
2591
2592 /* ('and' EqualityExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002593 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 +02002594 exp_repeat_push(exp, prev_and_exp, LYXP_EXPR_AND);
Michal Vasko004d3152020-06-11 19:59:22 +02002595 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002596
aPiecekbf968d92021-05-27 14:35:05 +02002597 rc = reparse_equality_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002598 LY_CHECK_RET(rc);
2599 }
2600 }
2601
2602 return LY_SUCCESS;
2603}
Radek Krejcib1646a92018-11-02 16:08:26 +01002604
2605/**
2606 * @brief Parse NCName.
2607 *
2608 * @param[in] ncname Name to parse.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002609 * @return Length of @p ncname valid bytes.
Radek Krejcib1646a92018-11-02 16:08:26 +01002610 */
Radek Krejcid4270262019-01-07 15:07:25 +01002611static long int
Radek Krejcib1646a92018-11-02 16:08:26 +01002612parse_ncname(const char *ncname)
2613{
Radek Krejci1deb5be2020-08-26 16:43:36 +02002614 uint32_t uc;
Radek Krejcid4270262019-01-07 15:07:25 +01002615 size_t size;
2616 long int len = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002617
2618 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), 0);
2619 if (!is_xmlqnamestartchar(uc) || (uc == ':')) {
2620 return len;
2621 }
2622
2623 do {
2624 len += size;
Radek Krejci9a564c92019-01-07 14:53:57 +01002625 if (!*ncname) {
2626 break;
2627 }
Radek Krejcid4270262019-01-07 15:07:25 +01002628 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), -len);
Radek Krejcib1646a92018-11-02 16:08:26 +01002629 } while (is_xmlqnamechar(uc) && (uc != ':'));
2630
2631 return len;
2632}
2633
2634/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02002635 * @brief Add @p token into the expression @p exp.
Radek Krejcib1646a92018-11-02 16:08:26 +01002636 *
Michal Vasko03ff5a72019-09-11 13:49:33 +02002637 * @param[in] ctx Context for logging.
Radek Krejcib1646a92018-11-02 16:08:26 +01002638 * @param[in] exp Expression to use.
2639 * @param[in] token Token to add.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002640 * @param[in] tok_pos Token position in the XPath expression.
Radek Krejcib1646a92018-11-02 16:08:26 +01002641 * @param[in] tok_len Token length in the XPath expression.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002642 * @return LY_ERR
Radek Krejcib1646a92018-11-02 16:08:26 +01002643 */
2644static LY_ERR
Michal Vasko14676352020-05-29 11:35:55 +02002645exp_add_token(const struct ly_ctx *ctx, struct lyxp_expr *exp, enum lyxp_token token, uint16_t tok_pos, uint16_t tok_len)
Radek Krejcib1646a92018-11-02 16:08:26 +01002646{
2647 uint32_t prev;
2648
2649 if (exp->used == exp->size) {
2650 prev = exp->size;
2651 exp->size += LYXP_EXPR_SIZE_STEP;
2652 if (prev > exp->size) {
2653 LOGINT(ctx);
2654 return LY_EINT;
2655 }
2656
2657 exp->tokens = ly_realloc(exp->tokens, exp->size * sizeof *exp->tokens);
2658 LY_CHECK_ERR_RET(!exp->tokens, LOGMEM(ctx), LY_EMEM);
2659 exp->tok_pos = ly_realloc(exp->tok_pos, exp->size * sizeof *exp->tok_pos);
2660 LY_CHECK_ERR_RET(!exp->tok_pos, LOGMEM(ctx), LY_EMEM);
2661 exp->tok_len = ly_realloc(exp->tok_len, exp->size * sizeof *exp->tok_len);
2662 LY_CHECK_ERR_RET(!exp->tok_len, LOGMEM(ctx), LY_EMEM);
2663 }
2664
2665 exp->tokens[exp->used] = token;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002666 exp->tok_pos[exp->used] = tok_pos;
Radek Krejcib1646a92018-11-02 16:08:26 +01002667 exp->tok_len[exp->used] = tok_len;
2668 ++exp->used;
2669 return LY_SUCCESS;
2670}
2671
2672void
Michal Vasko14676352020-05-29 11:35:55 +02002673lyxp_expr_free(const struct ly_ctx *ctx, struct lyxp_expr *expr)
Radek Krejcib1646a92018-11-02 16:08:26 +01002674{
2675 uint16_t i;
2676
2677 if (!expr) {
2678 return;
2679 }
2680
2681 lydict_remove(ctx, expr->expr);
2682 free(expr->tokens);
2683 free(expr->tok_pos);
2684 free(expr->tok_len);
2685 if (expr->repeat) {
2686 for (i = 0; i < expr->used; ++i) {
2687 free(expr->repeat[i]);
2688 }
2689 }
2690 free(expr->repeat);
2691 free(expr);
2692}
2693
Radek Krejcif03a9e22020-09-18 20:09:31 +02002694LY_ERR
2695lyxp_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 +01002696{
Radek Krejcif03a9e22020-09-18 20:09:31 +02002697 LY_ERR ret = LY_SUCCESS;
2698 struct lyxp_expr *expr;
Radek Krejcid4270262019-01-07 15:07:25 +01002699 size_t parsed = 0, tok_len;
Radek Krejcib1646a92018-11-02 16:08:26 +01002700 enum lyxp_token tok_type;
Radek Krejci857189e2020-09-01 13:26:36 +02002701 ly_bool prev_function_check = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002702 uint16_t tok_idx = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002703
Radek Krejcif03a9e22020-09-18 20:09:31 +02002704 assert(expr_p);
2705
2706 if (!expr_str[0]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002707 LOGVAL(ctx, LY_VCODE_XP_EOF);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002708 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02002709 }
2710
2711 if (!expr_len) {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002712 expr_len = strlen(expr_str);
Michal Vasko004d3152020-06-11 19:59:22 +02002713 }
2714 if (expr_len > UINT16_MAX) {
Michal Vasko623ac7b2021-04-09 12:44:23 +02002715 LOGVAL(ctx, LYVE_XPATH, "XPath expression cannot be longer than %u characters.", UINT16_MAX);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002716 return LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01002717 }
2718
2719 /* init lyxp_expr structure */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002720 expr = calloc(1, sizeof *expr);
2721 LY_CHECK_ERR_GOTO(!expr, LOGMEM(ctx); ret = LY_EMEM, error);
2722 LY_CHECK_GOTO(ret = lydict_insert(ctx, expr_str, expr_len, &expr->expr), error);
2723 expr->used = 0;
2724 expr->size = LYXP_EXPR_SIZE_START;
2725 expr->tokens = malloc(expr->size * sizeof *expr->tokens);
2726 LY_CHECK_ERR_GOTO(!expr->tokens, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002727
Radek Krejcif03a9e22020-09-18 20:09:31 +02002728 expr->tok_pos = malloc(expr->size * sizeof *expr->tok_pos);
2729 LY_CHECK_ERR_GOTO(!expr->tok_pos, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002730
Radek Krejcif03a9e22020-09-18 20:09:31 +02002731 expr->tok_len = malloc(expr->size * sizeof *expr->tok_len);
2732 LY_CHECK_ERR_GOTO(!expr->tok_len, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002733
Michal Vasko004d3152020-06-11 19:59:22 +02002734 /* make expr 0-terminated */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002735 expr_str = expr->expr;
Michal Vasko004d3152020-06-11 19:59:22 +02002736
Radek Krejcif03a9e22020-09-18 20:09:31 +02002737 while (is_xmlws(expr_str[parsed])) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002738 ++parsed;
2739 }
2740
2741 do {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002742 if (expr_str[parsed] == '(') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002743
2744 /* '(' */
2745 tok_len = 1;
2746 tok_type = LYXP_TOKEN_PAR1;
2747
Radek Krejcif03a9e22020-09-18 20:09:31 +02002748 if (prev_function_check && expr->used && (expr->tokens[expr->used - 1] == LYXP_TOKEN_NAMETEST)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002749 /* it is a NodeType/FunctionName after all */
Michal Vasko69730152020-10-09 16:30:07 +02002750 if (((expr->tok_len[expr->used - 1] == 4) &&
2751 (!strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "node", 4) ||
2752 !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "text", 4))) ||
2753 ((expr->tok_len[expr->used - 1] == 7) &&
2754 !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "comment", 7))) {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002755 expr->tokens[expr->used - 1] = LYXP_TOKEN_NODETYPE;
Radek Krejcib1646a92018-11-02 16:08:26 +01002756 } else {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002757 expr->tokens[expr->used - 1] = LYXP_TOKEN_FUNCNAME;
Radek Krejcib1646a92018-11-02 16:08:26 +01002758 }
2759 prev_function_check = 0;
2760 }
2761
Radek Krejcif03a9e22020-09-18 20:09:31 +02002762 } else if (expr_str[parsed] == ')') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002763
2764 /* ')' */
2765 tok_len = 1;
2766 tok_type = LYXP_TOKEN_PAR2;
2767
Radek Krejcif03a9e22020-09-18 20:09:31 +02002768 } else if (expr_str[parsed] == '[') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002769
2770 /* '[' */
2771 tok_len = 1;
2772 tok_type = LYXP_TOKEN_BRACK1;
2773
Radek Krejcif03a9e22020-09-18 20:09:31 +02002774 } else if (expr_str[parsed] == ']') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002775
2776 /* ']' */
2777 tok_len = 1;
2778 tok_type = LYXP_TOKEN_BRACK2;
2779
Radek Krejcif03a9e22020-09-18 20:09:31 +02002780 } else if (!strncmp(&expr_str[parsed], "..", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002781
2782 /* '..' */
2783 tok_len = 2;
2784 tok_type = LYXP_TOKEN_DDOT;
2785
Radek Krejcif03a9e22020-09-18 20:09:31 +02002786 } else if ((expr_str[parsed] == '.') && (!isdigit(expr_str[parsed + 1]))) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002787
2788 /* '.' */
2789 tok_len = 1;
2790 tok_type = LYXP_TOKEN_DOT;
2791
Radek Krejcif03a9e22020-09-18 20:09:31 +02002792 } else if (expr_str[parsed] == '@') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002793
2794 /* '@' */
2795 tok_len = 1;
2796 tok_type = LYXP_TOKEN_AT;
2797
Radek Krejcif03a9e22020-09-18 20:09:31 +02002798 } else if (expr_str[parsed] == ',') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002799
2800 /* ',' */
2801 tok_len = 1;
2802 tok_type = LYXP_TOKEN_COMMA;
2803
Radek Krejcif03a9e22020-09-18 20:09:31 +02002804 } else if (expr_str[parsed] == '\'') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002805
2806 /* Literal with ' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002807 for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\''); ++tok_len) {}
2808 LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0',
Radek Krejci2efc45b2020-12-22 16:25:44 +01002809 LOGVAL(ctx, LY_VCODE_XP_EOE, expr_str[parsed], &expr_str[parsed]); ret = LY_EVALID,
Michal Vasko69730152020-10-09 16:30:07 +02002810 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002811 ++tok_len;
2812 tok_type = LYXP_TOKEN_LITERAL;
2813
Radek Krejcif03a9e22020-09-18 20:09:31 +02002814 } else if (expr_str[parsed] == '\"') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002815
2816 /* Literal with " */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002817 for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\"'); ++tok_len) {}
2818 LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0',
Radek Krejci2efc45b2020-12-22 16:25:44 +01002819 LOGVAL(ctx, LY_VCODE_XP_EOE, expr_str[parsed], &expr_str[parsed]); ret = LY_EVALID,
Michal Vasko69730152020-10-09 16:30:07 +02002820 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002821 ++tok_len;
2822 tok_type = LYXP_TOKEN_LITERAL;
2823
Radek Krejcif03a9e22020-09-18 20:09:31 +02002824 } else if ((expr_str[parsed] == '.') || (isdigit(expr_str[parsed]))) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002825
2826 /* Number */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002827 for (tok_len = 0; isdigit(expr_str[parsed + tok_len]); ++tok_len) {}
2828 if (expr_str[parsed + tok_len] == '.') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002829 ++tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02002830 for ( ; isdigit(expr_str[parsed + tok_len]); ++tok_len) {}
Radek Krejcib1646a92018-11-02 16:08:26 +01002831 }
2832 tok_type = LYXP_TOKEN_NUMBER;
2833
Radek Krejcif03a9e22020-09-18 20:09:31 +02002834 } else if (expr_str[parsed] == '/') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002835
2836 /* Operator '/', '//' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002837 if (!strncmp(&expr_str[parsed], "//", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002838 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002839 tok_type = LYXP_TOKEN_OPER_RPATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002840 } else {
2841 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002842 tok_type = LYXP_TOKEN_OPER_PATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002843 }
Radek Krejcib1646a92018-11-02 16:08:26 +01002844
Radek Krejcif03a9e22020-09-18 20:09:31 +02002845 } else if (!strncmp(&expr_str[parsed], "!=", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002846
Michal Vasko3e48bf32020-06-01 08:39:07 +02002847 /* Operator '!=' */
Radek Krejcib1646a92018-11-02 16:08:26 +01002848 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002849 tok_type = LYXP_TOKEN_OPER_NEQUAL;
2850
Radek Krejcif03a9e22020-09-18 20:09:31 +02002851 } else if (!strncmp(&expr_str[parsed], "<=", 2) || !strncmp(&expr_str[parsed], ">=", 2)) {
Michal Vasko3e48bf32020-06-01 08:39:07 +02002852
2853 /* Operator '<=', '>=' */
2854 tok_len = 2;
2855 tok_type = LYXP_TOKEN_OPER_COMP;
Radek Krejcib1646a92018-11-02 16:08:26 +01002856
Radek Krejcif03a9e22020-09-18 20:09:31 +02002857 } else if (expr_str[parsed] == '|') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002858
2859 /* Operator '|' */
2860 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002861 tok_type = LYXP_TOKEN_OPER_UNI;
Radek Krejcib1646a92018-11-02 16:08:26 +01002862
Radek Krejcif03a9e22020-09-18 20:09:31 +02002863 } else if ((expr_str[parsed] == '+') || (expr_str[parsed] == '-')) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002864
2865 /* Operator '+', '-' */
2866 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002867 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002868
Radek Krejcif03a9e22020-09-18 20:09:31 +02002869 } else if (expr_str[parsed] == '=') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002870
Michal Vasko3e48bf32020-06-01 08:39:07 +02002871 /* Operator '=' */
Radek Krejcib1646a92018-11-02 16:08:26 +01002872 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002873 tok_type = LYXP_TOKEN_OPER_EQUAL;
2874
Radek Krejcif03a9e22020-09-18 20:09:31 +02002875 } else if ((expr_str[parsed] == '<') || (expr_str[parsed] == '>')) {
Michal Vasko3e48bf32020-06-01 08:39:07 +02002876
2877 /* Operator '<', '>' */
2878 tok_len = 1;
2879 tok_type = LYXP_TOKEN_OPER_COMP;
Radek Krejcib1646a92018-11-02 16:08:26 +01002880
Michal Vasko69730152020-10-09 16:30:07 +02002881 } else if (expr->used && (expr->tokens[expr->used - 1] != LYXP_TOKEN_AT) &&
2882 (expr->tokens[expr->used - 1] != LYXP_TOKEN_PAR1) &&
2883 (expr->tokens[expr->used - 1] != LYXP_TOKEN_BRACK1) &&
2884 (expr->tokens[expr->used - 1] != LYXP_TOKEN_COMMA) &&
2885 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_LOG) &&
2886 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_EQUAL) &&
2887 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_NEQUAL) &&
2888 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_COMP) &&
2889 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_MATH) &&
2890 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_UNI) &&
2891 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_PATH) &&
2892 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_RPATH)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002893
2894 /* Operator '*', 'or', 'and', 'mod', or 'div' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002895 if (expr_str[parsed] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002896 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002897 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002898
Radek Krejcif03a9e22020-09-18 20:09:31 +02002899 } else if (!strncmp(&expr_str[parsed], "or", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002900 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002901 tok_type = LYXP_TOKEN_OPER_LOG;
Radek Krejcib1646a92018-11-02 16:08:26 +01002902
Radek Krejcif03a9e22020-09-18 20:09:31 +02002903 } else if (!strncmp(&expr_str[parsed], "and", 3)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002904 tok_len = 3;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002905 tok_type = LYXP_TOKEN_OPER_LOG;
Radek Krejcib1646a92018-11-02 16:08:26 +01002906
Radek Krejcif03a9e22020-09-18 20:09:31 +02002907 } else if (!strncmp(&expr_str[parsed], "mod", 3) || !strncmp(&expr_str[parsed], "div", 3)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002908 tok_len = 3;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002909 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002910
2911 } else if (prev_function_check) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002912 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 +02002913 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 +02002914 ret = LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01002915 goto error;
2916 } else {
Michal Vasko774ce402021-04-14 15:35:06 +02002917 LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed], parsed + 1, expr_str);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002918 ret = LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01002919 goto error;
2920 }
Radek Krejcif03a9e22020-09-18 20:09:31 +02002921 } else if (expr_str[parsed] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002922
2923 /* NameTest '*' */
2924 tok_len = 1;
2925 tok_type = LYXP_TOKEN_NAMETEST;
2926
2927 } else {
2928
2929 /* NameTest (NCName ':' '*' | QName) or NodeType/FunctionName */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002930 long int ncname_len = parse_ncname(&expr_str[parsed]);
Michal Vasko774ce402021-04-14 15:35:06 +02002931 LY_CHECK_ERR_GOTO(ncname_len < 0, LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed - ncname_len],
2932 parsed - ncname_len + 1, expr_str); ret = LY_EVALID, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002933 tok_len = ncname_len;
2934
Radek Krejcif03a9e22020-09-18 20:09:31 +02002935 if (expr_str[parsed + tok_len] == ':') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002936 ++tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02002937 if (expr_str[parsed + tok_len] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002938 ++tok_len;
2939 } else {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002940 ncname_len = parse_ncname(&expr_str[parsed + tok_len]);
Michal Vasko774ce402021-04-14 15:35:06 +02002941 LY_CHECK_ERR_GOTO(ncname_len < 0, LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed - ncname_len],
2942 parsed - ncname_len + 1, expr_str); ret = LY_EVALID, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002943 tok_len += ncname_len;
2944 }
2945 /* remove old flag to prevent ambiguities */
2946 prev_function_check = 0;
2947 tok_type = LYXP_TOKEN_NAMETEST;
2948 } else {
2949 /* there is no prefix so it can still be NodeType/FunctionName, we can't finally decide now */
2950 prev_function_check = 1;
2951 tok_type = LYXP_TOKEN_NAMETEST;
2952 }
2953 }
2954
2955 /* store the token, move on to the next one */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002956 LY_CHECK_GOTO(ret = exp_add_token(ctx, expr, tok_type, parsed, tok_len), error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002957 parsed += tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02002958 while (is_xmlws(expr_str[parsed])) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002959 ++parsed;
2960 }
2961
Radek Krejcif03a9e22020-09-18 20:09:31 +02002962 } while (expr_str[parsed]);
Radek Krejcib1646a92018-11-02 16:08:26 +01002963
Michal Vasko004d3152020-06-11 19:59:22 +02002964 if (reparse) {
2965 /* prealloc repeat */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002966 expr->repeat = calloc(expr->size, sizeof *expr->repeat);
2967 LY_CHECK_ERR_GOTO(!expr->repeat, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002968
Michal Vasko004d3152020-06-11 19:59:22 +02002969 /* fill repeat */
aPiecekbf968d92021-05-27 14:35:05 +02002970 LY_CHECK_ERR_GOTO(reparse_or_expr(ctx, expr, &tok_idx, 0), ret = LY_EVALID, error);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002971 if (expr->used > tok_idx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002972 LOGVAL(ctx, LYVE_XPATH, "Unparsed characters \"%s\" left at the end of an XPath expression.",
Michal Vasko69730152020-10-09 16:30:07 +02002973 &expr->expr[expr->tok_pos[tok_idx]]);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002974 ret = LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02002975 goto error;
2976 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02002977 }
2978
Radek Krejcif03a9e22020-09-18 20:09:31 +02002979 print_expr_struct_debug(expr);
2980 *expr_p = expr;
2981 return LY_SUCCESS;
Radek Krejcib1646a92018-11-02 16:08:26 +01002982
2983error:
Radek Krejcif03a9e22020-09-18 20:09:31 +02002984 lyxp_expr_free(ctx, expr);
2985 return ret;
Radek Krejcib1646a92018-11-02 16:08:26 +01002986}
2987
Michal Vasko1734be92020-09-22 08:55:10 +02002988LY_ERR
2989lyxp_expr_dup(const struct ly_ctx *ctx, const struct lyxp_expr *exp, struct lyxp_expr **dup_p)
Michal Vasko004d3152020-06-11 19:59:22 +02002990{
Michal Vasko1734be92020-09-22 08:55:10 +02002991 LY_ERR ret = LY_SUCCESS;
2992 struct lyxp_expr *dup = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02002993 uint32_t i, j;
2994
Michal Vasko7f45cf22020-10-01 12:49:44 +02002995 if (!exp) {
2996 goto cleanup;
2997 }
2998
Michal Vasko004d3152020-06-11 19:59:22 +02002999 dup = calloc(1, sizeof *dup);
Michal Vasko1734be92020-09-22 08:55:10 +02003000 LY_CHECK_ERR_GOTO(!dup, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02003001
Michal Vasko08e9b112021-06-11 15:41:17 +02003002 if (exp->used) {
3003 dup->tokens = malloc(exp->used * sizeof *dup->tokens);
3004 LY_CHECK_ERR_GOTO(!dup->tokens, LOGMEM(ctx); ret = LY_EMEM, cleanup);
3005 memcpy(dup->tokens, exp->tokens, exp->used * sizeof *dup->tokens);
Michal Vasko004d3152020-06-11 19:59:22 +02003006
Michal Vasko08e9b112021-06-11 15:41:17 +02003007 dup->tok_pos = malloc(exp->used * sizeof *dup->tok_pos);
3008 LY_CHECK_ERR_GOTO(!dup->tok_pos, LOGMEM(ctx); ret = LY_EMEM, cleanup);
3009 memcpy(dup->tok_pos, exp->tok_pos, exp->used * sizeof *dup->tok_pos);
Michal Vasko004d3152020-06-11 19:59:22 +02003010
Michal Vasko08e9b112021-06-11 15:41:17 +02003011 dup->tok_len = malloc(exp->used * sizeof *dup->tok_len);
3012 LY_CHECK_ERR_GOTO(!dup->tok_len, LOGMEM(ctx); ret = LY_EMEM, cleanup);
3013 memcpy(dup->tok_len, exp->tok_len, exp->used * sizeof *dup->tok_len);
Michal Vasko004d3152020-06-11 19:59:22 +02003014
Michal Vasko08e9b112021-06-11 15:41:17 +02003015 dup->repeat = malloc(exp->used * sizeof *dup->repeat);
3016 LY_CHECK_ERR_GOTO(!dup->repeat, LOGMEM(ctx); ret = LY_EMEM, cleanup);
3017 for (i = 0; i < exp->used; ++i) {
3018 if (!exp->repeat[i]) {
3019 dup->repeat[i] = NULL;
3020 } else {
3021 for (j = 0; exp->repeat[i][j]; ++j) {}
3022 /* the ending 0 as well */
3023 ++j;
Michal Vasko004d3152020-06-11 19:59:22 +02003024
Michal Vasko08e9b112021-06-11 15:41:17 +02003025 dup->repeat[i] = malloc(j * sizeof **dup->repeat);
3026 LY_CHECK_ERR_GOTO(!dup->repeat[i], LOGMEM(ctx); ret = LY_EMEM, cleanup);
3027 memcpy(dup->repeat[i], exp->repeat[i], j * sizeof **dup->repeat);
3028 dup->repeat[i][j - 1] = 0;
3029 }
Michal Vasko004d3152020-06-11 19:59:22 +02003030 }
3031 }
3032
3033 dup->used = exp->used;
3034 dup->size = exp->used;
Michal Vasko1734be92020-09-22 08:55:10 +02003035 LY_CHECK_GOTO(ret = lydict_insert(ctx, exp->expr, 0, &dup->expr), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02003036
Michal Vasko1734be92020-09-22 08:55:10 +02003037cleanup:
3038 if (ret) {
3039 lyxp_expr_free(ctx, dup);
3040 } else {
3041 *dup_p = dup;
3042 }
3043 return ret;
Michal Vasko004d3152020-06-11 19:59:22 +02003044}
3045
Michal Vasko03ff5a72019-09-11 13:49:33 +02003046/*
3047 * warn functions
3048 *
3049 * Warn functions check specific reasonable conditions for schema XPath
3050 * and print a warning if they are not satisfied.
3051 */
3052
3053/**
3054 * @brief Get the last-added schema node that is currently in the context.
3055 *
3056 * @param[in] set Set to search in.
3057 * @return Last-added schema context node, NULL if no node is in context.
3058 */
3059static struct lysc_node *
3060warn_get_scnode_in_ctx(struct lyxp_set *set)
3061{
3062 uint32_t i;
3063
3064 if (!set || (set->type != LYXP_SET_SCNODE_SET)) {
3065 return NULL;
3066 }
3067
3068 i = set->used;
3069 do {
3070 --i;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003071 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003072 /* if there are more, simply return the first found (last added) */
3073 return set->val.scnodes[i].scnode;
3074 }
3075 } while (i);
3076
3077 return NULL;
3078}
3079
3080/**
3081 * @brief Test whether a type is numeric - integer type or decimal64.
3082 *
3083 * @param[in] type Type to test.
Radek Krejci857189e2020-09-01 13:26:36 +02003084 * @return Boolean value whether @p type is numeric type or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003085 */
Radek Krejci857189e2020-09-01 13:26:36 +02003086static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003087warn_is_numeric_type(struct lysc_type *type)
3088{
3089 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003090 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003091 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003092
3093 switch (type->basetype) {
3094 case LY_TYPE_DEC64:
3095 case LY_TYPE_INT8:
3096 case LY_TYPE_UINT8:
3097 case LY_TYPE_INT16:
3098 case LY_TYPE_UINT16:
3099 case LY_TYPE_INT32:
3100 case LY_TYPE_UINT32:
3101 case LY_TYPE_INT64:
3102 case LY_TYPE_UINT64:
3103 return 1;
3104 case LY_TYPE_UNION:
3105 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003106 LY_ARRAY_FOR(uni->types, u) {
3107 ret = warn_is_numeric_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003108 if (ret) {
3109 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003110 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003111 }
3112 }
3113 /* did not find any suitable type */
3114 return 0;
3115 case LY_TYPE_LEAFREF:
3116 return warn_is_numeric_type(((struct lysc_type_leafref *)type)->realtype);
3117 default:
3118 return 0;
3119 }
3120}
3121
3122/**
3123 * @brief Test whether a type is string-like - no integers, decimal64 or binary.
3124 *
3125 * @param[in] type Type to test.
Radek Krejci857189e2020-09-01 13:26:36 +02003126 * @return Boolean value whether @p type's basetype is string type or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003127 */
Radek Krejci857189e2020-09-01 13:26:36 +02003128static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003129warn_is_string_type(struct lysc_type *type)
3130{
3131 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003132 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003133 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003134
3135 switch (type->basetype) {
3136 case LY_TYPE_BITS:
3137 case LY_TYPE_ENUM:
3138 case LY_TYPE_IDENT:
3139 case LY_TYPE_INST:
3140 case LY_TYPE_STRING:
3141 return 1;
3142 case LY_TYPE_UNION:
3143 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003144 LY_ARRAY_FOR(uni->types, u) {
3145 ret = warn_is_string_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003146 if (ret) {
3147 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003148 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003149 }
3150 }
3151 /* did not find any suitable type */
3152 return 0;
3153 case LY_TYPE_LEAFREF:
3154 return warn_is_string_type(((struct lysc_type_leafref *)type)->realtype);
3155 default:
3156 return 0;
3157 }
3158}
3159
3160/**
3161 * @brief Test whether a type is one specific type.
3162 *
3163 * @param[in] type Type to test.
3164 * @param[in] base Expected type.
Radek Krejci857189e2020-09-01 13:26:36 +02003165 * @return Boolean value whether the given @p type is of the specific basetype @p base.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003166 */
Radek Krejci857189e2020-09-01 13:26:36 +02003167static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003168warn_is_specific_type(struct lysc_type *type, LY_DATA_TYPE base)
3169{
3170 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003171 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003172 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003173
3174 if (type->basetype == base) {
3175 return 1;
3176 } else if (type->basetype == LY_TYPE_UNION) {
3177 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003178 LY_ARRAY_FOR(uni->types, u) {
3179 ret = warn_is_specific_type(uni->types[u], base);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003180 if (ret) {
3181 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003182 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003183 }
3184 }
3185 /* did not find any suitable type */
3186 return 0;
3187 } else if (type->basetype == LY_TYPE_LEAFREF) {
3188 return warn_is_specific_type(((struct lysc_type_leafref *)type)->realtype, base);
3189 }
3190
3191 return 0;
3192}
3193
3194/**
3195 * @brief Get next type of a (union) type.
3196 *
3197 * @param[in] type Base type.
3198 * @param[in] prev_type Previously returned type.
3199 * @return Next type or NULL.
3200 */
3201static struct lysc_type *
3202warn_is_equal_type_next_type(struct lysc_type *type, struct lysc_type *prev_type)
3203{
3204 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003205 ly_bool found = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003206 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003207
3208 switch (type->basetype) {
3209 case LY_TYPE_UNION:
3210 uni = (struct lysc_type_union *)type;
3211 if (!prev_type) {
3212 return uni->types[0];
3213 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003214 LY_ARRAY_FOR(uni->types, u) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003215 if (found) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003216 return uni->types[u];
Michal Vasko03ff5a72019-09-11 13:49:33 +02003217 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003218 if (prev_type == uni->types[u]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003219 found = 1;
3220 }
3221 }
3222 return NULL;
3223 default:
3224 if (prev_type) {
3225 assert(type == prev_type);
3226 return NULL;
3227 } else {
3228 return type;
3229 }
3230 }
3231}
3232
3233/**
3234 * @brief Test whether 2 types have a common type.
3235 *
3236 * @param[in] type1 First type.
3237 * @param[in] type2 Second type.
3238 * @return 1 if they do, 0 otherwise.
3239 */
3240static int
3241warn_is_equal_type(struct lysc_type *type1, struct lysc_type *type2)
3242{
3243 struct lysc_type *t1, *rt1, *t2, *rt2;
3244
3245 t1 = NULL;
3246 while ((t1 = warn_is_equal_type_next_type(type1, t1))) {
3247 if (t1->basetype == LY_TYPE_LEAFREF) {
3248 rt1 = ((struct lysc_type_leafref *)t1)->realtype;
3249 } else {
3250 rt1 = t1;
3251 }
3252
3253 t2 = NULL;
3254 while ((t2 = warn_is_equal_type_next_type(type2, t2))) {
3255 if (t2->basetype == LY_TYPE_LEAFREF) {
3256 rt2 = ((struct lysc_type_leafref *)t2)->realtype;
3257 } else {
3258 rt2 = t2;
3259 }
3260
3261 if (rt2->basetype == rt1->basetype) {
3262 /* match found */
3263 return 1;
3264 }
3265 }
3266 }
3267
3268 return 0;
3269}
3270
3271/**
3272 * @brief Check both operands of comparison operators.
3273 *
3274 * @param[in] ctx Context for errors.
3275 * @param[in] set1 First operand set.
3276 * @param[in] set2 Second operand set.
3277 * @param[in] numbers_only Whether accept only numbers or other types are fine too (for '=' and '!=').
3278 * @param[in] expr Start of the expression to print with the warning.
3279 * @param[in] tok_pos Token position.
3280 */
3281static void
Radek Krejci857189e2020-09-01 13:26:36 +02003282warn_operands(struct ly_ctx *ctx, struct lyxp_set *set1, struct lyxp_set *set2, ly_bool numbers_only, const char *expr, uint16_t tok_pos)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003283{
3284 struct lysc_node_leaf *node1, *node2;
Radek Krejci857189e2020-09-01 13:26:36 +02003285 ly_bool leaves = 1, warning = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003286
3287 node1 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set1);
3288 node2 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set2);
3289
3290 if (!node1 && !node2) {
3291 /* no node-sets involved, nothing to do */
3292 return;
3293 }
3294
3295 if (node1) {
3296 if (!(node1->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3297 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node1->nodetype), node1->name);
3298 warning = 1;
3299 leaves = 0;
3300 } else if (numbers_only && !warn_is_numeric_type(node1->type)) {
3301 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node1->name);
3302 warning = 1;
3303 }
3304 }
3305
3306 if (node2) {
3307 if (!(node2->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3308 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node2->nodetype), node2->name);
3309 warning = 1;
3310 leaves = 0;
3311 } else if (numbers_only && !warn_is_numeric_type(node2->type)) {
3312 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node2->name);
3313 warning = 1;
3314 }
3315 }
3316
3317 if (node1 && node2 && leaves && !numbers_only) {
Michal Vasko69730152020-10-09 16:30:07 +02003318 if ((warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type)) ||
3319 (!warn_is_numeric_type(node1->type) && warn_is_numeric_type(node2->type)) ||
3320 (!warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type) &&
3321 !warn_is_equal_type(node1->type, node2->type))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003322 LOGWRN(ctx, "Incompatible types of operands \"%s\" and \"%s\" for comparison.", node1->name, node2->name);
3323 warning = 1;
3324 }
3325 }
3326
3327 if (warning) {
3328 LOGWRN(ctx, "Previous warning generated by XPath subexpression[%u] \"%.20s\".", tok_pos, expr + tok_pos);
3329 }
3330}
3331
3332/**
3333 * @brief Check that a value is valid for a leaf. If not applicable, does nothing.
3334 *
3335 * @param[in] exp Parsed XPath expression.
3336 * @param[in] set Set with the leaf/leaf-list.
3337 * @param[in] val_exp Index of the value (literal/number) in @p exp.
3338 * @param[in] equal_exp Index of the start of the equality expression in @p exp.
3339 * @param[in] last_equal_exp Index of the end of the equality expression in @p exp.
3340 */
3341static void
Michal Vasko40308e72020-10-20 16:38:40 +02003342warn_equality_value(const struct lyxp_expr *exp, struct lyxp_set *set, uint16_t val_exp, uint16_t equal_exp,
3343 uint16_t last_equal_exp)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003344{
3345 struct lysc_node *scnode;
3346 struct lysc_type *type;
3347 char *value;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003348 struct lyd_value storage;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003349 LY_ERR rc;
3350 struct ly_err_item *err = NULL;
3351
Michal Vasko69730152020-10-09 16:30:07 +02003352 if ((scnode = warn_get_scnode_in_ctx(set)) && (scnode->nodetype & (LYS_LEAF | LYS_LEAFLIST)) &&
3353 ((exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) || (exp->tokens[val_exp] == LYXP_TOKEN_NUMBER))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003354 /* check that the node can have the specified value */
3355 if (exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) {
3356 value = strndup(exp->expr + exp->tok_pos[val_exp] + 1, exp->tok_len[val_exp] - 2);
3357 } else {
3358 value = strndup(exp->expr + exp->tok_pos[val_exp], exp->tok_len[val_exp]);
3359 }
3360 if (!value) {
3361 LOGMEM(set->ctx);
3362 return;
3363 }
3364
3365 if ((((struct lysc_node_leaf *)scnode)->type->basetype == LY_TYPE_IDENT) && !strchr(value, ':')) {
3366 LOGWRN(set->ctx, "Identityref \"%s\" comparison with identity \"%s\" without prefix, consider adding"
Michal Vasko69730152020-10-09 16:30:07 +02003367 " a prefix or best using \"derived-from(-or-self)()\" functions.", scnode->name, value);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003368 LOGWRN(set->ctx, "Previous warning generated by XPath subexpression[%u] \"%.*s\".", exp->tok_pos[equal_exp],
Radek Krejci0f969882020-08-21 16:56:47 +02003369 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
Michal Vasko69730152020-10-09 16:30:07 +02003370 exp->expr + exp->tok_pos[equal_exp]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003371 }
3372
3373 type = ((struct lysc_node_leaf *)scnode)->type;
3374 if (type->basetype != LY_TYPE_IDENT) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003375 rc = type->plugin->store(set->ctx, type, value, strlen(value), 0, set->format, set->prefix_data,
Michal Vasko405cc9e2020-12-01 12:01:27 +01003376 LYD_HINT_DATA, scnode, &storage, NULL, &err);
Michal Vaskobf42e832020-11-23 16:59:42 +01003377 if (rc == LY_EINCOMPLETE) {
3378 rc = LY_SUCCESS;
3379 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003380
3381 if (err) {
3382 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type (%s).", value, err->msg);
3383 ly_err_free(err);
3384 } else if (rc != LY_SUCCESS) {
3385 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type.", value);
3386 }
3387 if (rc != LY_SUCCESS) {
3388 LOGWRN(set->ctx, "Previous warning generated by XPath subexpression[%u] \"%.*s\".", exp->tok_pos[equal_exp],
Radek Krejci0f969882020-08-21 16:56:47 +02003389 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
Michal Vasko69730152020-10-09 16:30:07 +02003390 exp->expr + exp->tok_pos[equal_exp]);
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003391 } else {
3392 type->plugin->free(set->ctx, &storage);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003393 }
3394 }
3395 free(value);
3396 }
3397}
3398
3399/*
3400 * XPath functions
3401 */
3402
3403/**
3404 * @brief Execute the YANG 1.1 bit-is-set(node-set, string) function. Returns LYXP_SET_BOOLEAN
3405 * depending on whether the first node bit value from the second argument is set.
3406 *
3407 * @param[in] args Array of arguments.
3408 * @param[in] arg_count Count of elements in @p args.
3409 * @param[in,out] set Context and result set at the same time.
3410 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003411 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003412 */
3413static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003414xpath_bit_is_set(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003415{
3416 struct lyd_node_term *leaf;
3417 struct lysc_node_leaf *sleaf;
3418 struct lysc_type_bits *bits;
3419 LY_ERR rc = LY_SUCCESS;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003420 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003421
3422 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003423 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003424 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003425 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3426 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3427 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3428 sleaf->name);
3429 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_BITS)) {
3430 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"bits\".", __func__, sleaf->name);
3431 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003432 }
3433
3434 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3435 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003436 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3437 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003438 } else if (!warn_is_string_type(sleaf->type)) {
3439 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003440 }
3441 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003442 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003443 return rc;
3444 }
3445
Michal Vaskod3678892020-05-21 10:06:58 +02003446 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003447 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 +02003448 return LY_EVALID;
3449 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003450 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003451 LY_CHECK_RET(rc);
3452
3453 set_fill_boolean(set, 0);
Michal Vaskod3678892020-05-21 10:06:58 +02003454 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003455 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
Michal Vasko69730152020-10-09 16:30:07 +02003456 if ((leaf->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) &&
3457 (((struct lysc_node_leaf *)leaf->schema)->type->basetype == LY_TYPE_BITS)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003458 bits = (struct lysc_type_bits *)((struct lysc_node_leaf *)leaf->schema)->type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003459 LY_ARRAY_FOR(bits->bits, u) {
3460 if (!strcmp(bits->bits[u].name, args[1]->val.str)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003461 set_fill_boolean(set, 1);
3462 break;
3463 }
3464 }
3465 }
3466 }
3467
3468 return LY_SUCCESS;
3469}
3470
3471/**
3472 * @brief Execute the XPath boolean(object) function. Returns LYXP_SET_BOOLEAN
3473 * with the argument converted to boolean.
3474 *
3475 * @param[in] args Array of arguments.
3476 * @param[in] arg_count Count of elements in @p args.
3477 * @param[in,out] set Context and result set at the same time.
3478 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003479 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003480 */
3481static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003482xpath_boolean(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003483{
3484 LY_ERR rc;
3485
3486 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02003487 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003488 return LY_SUCCESS;
3489 }
3490
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003491 rc = lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003492 LY_CHECK_RET(rc);
3493 set_fill_set(set, args[0]);
3494
3495 return LY_SUCCESS;
3496}
3497
3498/**
3499 * @brief Execute the XPath ceiling(number) function. Returns LYXP_SET_NUMBER
3500 * with the first argument rounded up to the nearest integer.
3501 *
3502 * @param[in] args Array of arguments.
3503 * @param[in] arg_count Count of elements in @p args.
3504 * @param[in,out] set Context and result set at the same time.
3505 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003506 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003507 */
3508static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003509xpath_ceiling(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003510{
3511 struct lysc_node_leaf *sleaf;
3512 LY_ERR rc = LY_SUCCESS;
3513
3514 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003515 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003516 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003517 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3518 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3519 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3520 sleaf->name);
3521 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
3522 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
3523 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003524 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003525 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003526 return rc;
3527 }
3528
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003529 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003530 LY_CHECK_RET(rc);
3531 if ((long long)args[0]->val.num != args[0]->val.num) {
3532 set_fill_number(set, ((long long)args[0]->val.num) + 1);
3533 } else {
3534 set_fill_number(set, args[0]->val.num);
3535 }
3536
3537 return LY_SUCCESS;
3538}
3539
3540/**
3541 * @brief Execute the XPath concat(string, string, string*) function.
3542 * Returns LYXP_SET_STRING with the concatenation of all the arguments.
3543 *
3544 * @param[in] args Array of arguments.
3545 * @param[in] arg_count Count of elements in @p args.
3546 * @param[in,out] set Context and result set at the same time.
3547 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003548 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003549 */
3550static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003551xpath_concat(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003552{
3553 uint16_t i;
3554 char *str = NULL;
3555 size_t used = 1;
3556 LY_ERR rc = LY_SUCCESS;
3557 struct lysc_node_leaf *sleaf;
3558
3559 if (options & LYXP_SCNODE_ALL) {
3560 for (i = 0; i < arg_count; ++i) {
3561 if ((args[i]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[i]))) {
3562 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3563 LOGWRN(set->ctx, "Argument #%u of %s is a %s node \"%s\".",
Michal Vasko69730152020-10-09 16:30:07 +02003564 i + 1, __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003565 } else if (!warn_is_string_type(sleaf->type)) {
Radek Krejci70124c82020-08-14 22:17:03 +02003566 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 +02003567 }
3568 }
3569 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003570 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003571 return rc;
3572 }
3573
3574 for (i = 0; i < arg_count; ++i) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003575 rc = lyxp_set_cast(args[i], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003576 if (rc != LY_SUCCESS) {
3577 free(str);
3578 return rc;
3579 }
3580
3581 str = ly_realloc(str, (used + strlen(args[i]->val.str)) * sizeof(char));
3582 LY_CHECK_ERR_RET(!str, LOGMEM(set->ctx), LY_EMEM);
3583 strcpy(str + used - 1, args[i]->val.str);
3584 used += strlen(args[i]->val.str);
3585 }
3586
3587 /* free, kind of */
Michal Vaskod3678892020-05-21 10:06:58 +02003588 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003589 set->type = LYXP_SET_STRING;
3590 set->val.str = str;
3591
3592 return LY_SUCCESS;
3593}
3594
3595/**
3596 * @brief Execute the XPath contains(string, string) function.
3597 * Returns LYXP_SET_BOOLEAN whether the second argument can
3598 * be found in the first or not.
3599 *
3600 * @param[in] args Array of arguments.
3601 * @param[in] arg_count Count of elements in @p args.
3602 * @param[in,out] set Context and result set at the same time.
3603 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003604 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003605 */
3606static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003607xpath_contains(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003608{
3609 struct lysc_node_leaf *sleaf;
3610 LY_ERR rc = LY_SUCCESS;
3611
3612 if (options & LYXP_SCNODE_ALL) {
3613 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3614 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003615 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3616 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003617 } else if (!warn_is_string_type(sleaf->type)) {
3618 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003619 }
3620 }
3621
3622 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3623 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003624 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3625 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003626 } else if (!warn_is_string_type(sleaf->type)) {
3627 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003628 }
3629 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003630 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003631 return rc;
3632 }
3633
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003634 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003635 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003636 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003637 LY_CHECK_RET(rc);
3638
3639 if (strstr(args[0]->val.str, args[1]->val.str)) {
3640 set_fill_boolean(set, 1);
3641 } else {
3642 set_fill_boolean(set, 0);
3643 }
3644
3645 return LY_SUCCESS;
3646}
3647
3648/**
3649 * @brief Execute the XPath count(node-set) function. Returns LYXP_SET_NUMBER
3650 * with the size of the node-set from the argument.
3651 *
3652 * @param[in] args Array of arguments.
3653 * @param[in] arg_count Count of elements in @p args.
3654 * @param[in,out] set Context and result set at the same time.
3655 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003656 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003657 */
3658static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003659xpath_count(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003660{
Michal Vasko03ff5a72019-09-11 13:49:33 +02003661 LY_ERR rc = LY_SUCCESS;
3662
3663 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003664 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003665 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003666 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003667 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003668 return rc;
3669 }
3670
Michal Vasko03ff5a72019-09-11 13:49:33 +02003671 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003672 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "count(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003673 return LY_EVALID;
3674 }
3675
3676 set_fill_number(set, args[0]->used);
3677 return LY_SUCCESS;
3678}
3679
3680/**
3681 * @brief Execute the XPath current() function. Returns LYXP_SET_NODE_SET
3682 * with the context with the intial node.
3683 *
3684 * @param[in] args Array of arguments.
3685 * @param[in] arg_count Count of elements in @p args.
3686 * @param[in,out] set Context and result set at the same time.
3687 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003688 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003689 */
3690static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003691xpath_current(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003692{
3693 if (arg_count || args) {
Radek Krejcie87c7dc2021-06-02 21:25:42 +02003694 LOGVAL(set->ctx, LY_VCODE_XP_INARGCOUNT, arg_count, LY_PRI_LENSTR("current()"));
Michal Vasko03ff5a72019-09-11 13:49:33 +02003695 return LY_EVALID;
3696 }
3697
3698 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02003699 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003700
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003701 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, set->cur_scnode, LYXP_NODE_ELEM, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02003702 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02003703 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003704
3705 /* position is filled later */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003706 set_insert_node(set, set->cur_node, 0, LYXP_NODE_ELEM, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003707 }
3708
3709 return LY_SUCCESS;
3710}
3711
3712/**
3713 * @brief Execute the YANG 1.1 deref(node-set) function. Returns LYXP_SET_NODE_SET with either
3714 * leafref or instance-identifier target node(s).
3715 *
3716 * @param[in] args Array of arguments.
3717 * @param[in] arg_count Count of elements in @p args.
3718 * @param[in,out] set Context and result set at the same time.
3719 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003720 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003721 */
3722static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003723xpath_deref(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003724{
3725 struct lyd_node_term *leaf;
Michal Vasko42e497c2020-01-06 08:38:25 +01003726 struct lysc_node_leaf *sleaf = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02003727 struct lysc_type_leafref *lref;
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003728 const struct lysc_node *target;
Michal Vasko004d3152020-06-11 19:59:22 +02003729 struct ly_path *p;
3730 struct lyd_node *node;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003731 char *errmsg = NULL;
Michal Vasko00cbf532020-06-15 13:58:47 +02003732 uint8_t oper;
Michal Vasko741bb562021-06-24 11:59:50 +02003733 LY_ERR r;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003734
3735 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003736 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003737 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003738 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3739 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3740 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3741 sleaf->name);
Michal Vaskoed725d72021-06-23 12:03:45 +02003742 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_LEAFREF) &&
3743 !warn_is_specific_type(sleaf->type, LY_TYPE_INST)) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003744 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"leafref\" nor \"instance-identifier\".",
3745 __func__, sleaf->name);
3746 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003747 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003748 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko42e497c2020-01-06 08:38:25 +01003749 if (sleaf && (sleaf->type->basetype == LY_TYPE_LEAFREF)) {
Michal Vasko004d3152020-06-11 19:59:22 +02003750 lref = (struct lysc_type_leafref *)sleaf->type;
Michal Vaskod1e53b92021-01-28 13:11:06 +01003751 oper = (sleaf->flags & LYS_IS_OUTPUT) ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT;
Michal Vasko004d3152020-06-11 19:59:22 +02003752
3753 /* it was already evaluated on schema, it must succeed */
Michal Vasko741bb562021-06-24 11:59:50 +02003754 r = ly_path_compile_leafref(set->ctx, &sleaf->node, NULL, lref->path, oper, LY_PATH_TARGET_MANY,
Michal Vaskoed725d72021-06-23 12:03:45 +02003755 LY_VALUE_SCHEMA_RESOLVED, lref->prefixes, NULL, &p);
Michal Vasko741bb562021-06-24 11:59:50 +02003756 if (!r) {
3757 /* get the target node */
3758 target = p[LY_ARRAY_COUNT(p) - 1].node;
3759 ly_path_free(set->ctx, p);
Michal Vasko004d3152020-06-11 19:59:22 +02003760
Michal Vasko741bb562021-06-24 11:59:50 +02003761 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, target, LYXP_NODE_ELEM, NULL));
3762 } /* else the target was found before but is disabled so it was removed */
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003763 }
3764
Michal Vasko741bb562021-06-24 11:59:50 +02003765 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003766 }
3767
Michal Vaskod3678892020-05-21 10:06:58 +02003768 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003769 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "deref(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003770 return LY_EVALID;
3771 }
3772
Michal Vaskod3678892020-05-21 10:06:58 +02003773 lyxp_set_free_content(set);
3774 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003775 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
3776 sleaf = (struct lysc_node_leaf *)leaf->schema;
3777 if (sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
3778 if (sleaf->type->basetype == LY_TYPE_LEAFREF) {
3779 /* find leafref target */
Radek Krejci0b013302021-03-29 15:22:32 +02003780 if (lyplg_type_resolve_leafref((struct lysc_type_leafref *)sleaf->type, &leaf->node, &leaf->value, set->tree,
Michal Vasko9e685082021-01-29 14:49:09 +01003781 &node, &errmsg)) {
Michal Vasko004d3152020-06-11 19:59:22 +02003782 LOGERR(set->ctx, LY_EVALID, errmsg);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003783 free(errmsg);
Michal Vasko004d3152020-06-11 19:59:22 +02003784 return LY_EVALID;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003785 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003786 } else {
3787 assert(sleaf->type->basetype == LY_TYPE_INST);
Michal Vasko004d3152020-06-11 19:59:22 +02003788 if (ly_path_eval(leaf->value.target, set->tree, &node)) {
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003789 LOGERR(set->ctx, LY_EVALID, "Invalid instance-identifier \"%s\" value - required instance not found.",
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02003790 lyd_get_value(&leaf->node));
Michal Vasko03ff5a72019-09-11 13:49:33 +02003791 return LY_EVALID;
3792 }
3793 }
Michal Vasko004d3152020-06-11 19:59:22 +02003794
3795 /* insert it */
3796 set_insert_node(set, node, 0, LYXP_NODE_ELEM, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003797 }
3798 }
3799
3800 return LY_SUCCESS;
3801}
3802
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003803static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02003804xpath_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 +02003805{
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01003806 uint32_t i;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003807 LY_ARRAY_COUNT_TYPE u;
3808 struct lyd_node_term *leaf;
3809 struct lysc_node_leaf *sleaf;
3810 struct lyd_meta *meta;
Michal Vasko93923692021-05-07 15:28:02 +02003811 struct lyd_value *val;
3812 const struct lys_module *mod;
3813 const char *id_name;
3814 uint16_t id_len;
3815 struct lysc_ident *id;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003816 LY_ERR rc = LY_SUCCESS;
Radek Krejci857189e2020-09-01 13:26:36 +02003817 ly_bool found;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003818
3819 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003820 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003821 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", func);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003822 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3823 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3824 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype),
3825 sleaf->name);
3826 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_IDENT)) {
3827 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"identityref\".", func, sleaf->name);
3828 }
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003829 }
3830
3831 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3832 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3833 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype),
Michal Vasko69730152020-10-09 16:30:07 +02003834 sleaf->name);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003835 } else if (!warn_is_string_type(sleaf->type)) {
3836 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", func, sleaf->name);
3837 }
3838 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003839 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003840 return rc;
3841 }
3842
3843 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003844 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 +02003845 return LY_EVALID;
3846 }
3847 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
3848 LY_CHECK_RET(rc);
3849
Michal Vasko93923692021-05-07 15:28:02 +02003850 /* parse the identity */
3851 id_name = args[1]->val.str;
3852 id_len = strlen(id_name);
3853 rc = moveto_resolve_model(&id_name, &id_len, set, set->cur_node ? set->cur_node->schema : NULL, &mod);
3854 LY_CHECK_RET(rc);
3855 if (!mod) {
3856 LOGVAL(set->ctx, LYVE_XPATH, "Identity \"%.*s\" without a prefix.", (int)id_len, id_name);
3857 return LY_EVALID;
3858 }
3859
3860 /* find the identity */
3861 found = 0;
3862 LY_ARRAY_FOR(mod->identities, u) {
3863 if (!ly_strncmp(mod->identities[u].name, id_name, id_len)) {
3864 /* we have match */
3865 found = 1;
3866 break;
3867 }
3868 }
3869 if (!found) {
3870 LOGVAL(set->ctx, LYVE_XPATH, "Identity \"%.*s\" not found in module \"%s\".", (int)id_len, id_name, mod->name);
3871 return LY_EVALID;
3872 }
3873 id = &mod->identities[u];
3874
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003875 set_fill_boolean(set, 0);
3876 found = 0;
3877 for (i = 0; i < args[0]->used; ++i) {
3878 if ((args[0]->val.nodes[i].type != LYXP_NODE_ELEM) && (args[0]->val.nodes[i].type != LYXP_NODE_META)) {
3879 continue;
3880 }
3881
3882 if (args[0]->val.nodes[i].type == LYXP_NODE_ELEM) {
3883 leaf = (struct lyd_node_term *)args[0]->val.nodes[i].node;
3884 sleaf = (struct lysc_node_leaf *)leaf->schema;
3885 val = &leaf->value;
3886 if (!(sleaf->nodetype & LYD_NODE_TERM) || (leaf->value.realtype->basetype != LY_TYPE_IDENT)) {
3887 /* uninteresting */
3888 continue;
3889 }
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003890 } else {
3891 meta = args[0]->val.meta[i].meta;
3892 val = &meta->value;
3893 if (val->realtype->basetype != LY_TYPE_IDENT) {
3894 /* uninteresting */
3895 continue;
3896 }
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003897 }
3898
Michal Vasko93923692021-05-07 15:28:02 +02003899 /* check the identity itself */
3900 if (self_match && (id == val->ident)) {
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003901 set_fill_boolean(set, 1);
3902 found = 1;
3903 }
Michal Vasko93923692021-05-07 15:28:02 +02003904 if (!found && !lyplg_type_identity_isderived(id, val->ident)) {
3905 set_fill_boolean(set, 1);
3906 found = 1;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003907 }
3908
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003909 if (found) {
3910 break;
3911 }
3912 }
3913
3914 return LY_SUCCESS;
3915}
3916
Michal Vasko03ff5a72019-09-11 13:49:33 +02003917/**
3918 * @brief Execute the YANG 1.1 derived-from(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
3919 * on whether the first argument nodes contain a node of an identity derived from the second
3920 * argument identity.
3921 *
3922 * @param[in] args Array of arguments.
3923 * @param[in] arg_count Count of elements in @p args.
3924 * @param[in,out] set Context and result set at the same time.
3925 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003926 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003927 */
3928static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003929xpath_derived_from(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003930{
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003931 return xpath_derived_(args, set, options, 0, __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003932}
3933
3934/**
3935 * @brief Execute the YANG 1.1 derived-from-or-self(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
3936 * on whether the first argument nodes contain a node of an identity that either is or is derived from
3937 * the second argument identity.
3938 *
3939 * @param[in] args Array of arguments.
3940 * @param[in] arg_count Count of elements in @p args.
3941 * @param[in,out] set Context and result set at the same time.
3942 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003943 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003944 */
3945static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003946xpath_derived_from_or_self(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003947{
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003948 return xpath_derived_(args, set, options, 1, __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003949}
3950
3951/**
3952 * @brief Execute the YANG 1.1 enum-value(node-set) function. Returns LYXP_SET_NUMBER
3953 * with the integer value of the first node's enum value, otherwise NaN.
3954 *
3955 * @param[in] args Array of arguments.
3956 * @param[in] arg_count Count of elements in @p args.
3957 * @param[in,out] set Context and result set at the same time.
3958 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003959 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003960 */
3961static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003962xpath_enum_value(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003963{
3964 struct lyd_node_term *leaf;
3965 struct lysc_node_leaf *sleaf;
3966 LY_ERR rc = LY_SUCCESS;
3967
3968 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003969 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003970 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003971 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3972 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3973 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3974 sleaf->name);
3975 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_ENUM)) {
3976 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"enumeration\".", __func__, sleaf->name);
3977 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003978 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003979 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003980 return rc;
3981 }
3982
Michal Vaskod3678892020-05-21 10:06:58 +02003983 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003984 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "enum-value(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003985 return LY_EVALID;
3986 }
3987
3988 set_fill_number(set, NAN);
Michal Vaskod3678892020-05-21 10:06:58 +02003989 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003990 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
3991 sleaf = (struct lysc_node_leaf *)leaf->schema;
3992 if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_ENUM)) {
3993 set_fill_number(set, leaf->value.enum_item->value);
3994 }
3995 }
3996
3997 return LY_SUCCESS;
3998}
3999
4000/**
4001 * @brief Execute the XPath false() function. Returns LYXP_SET_BOOLEAN
4002 * with false value.
4003 *
4004 * @param[in] args Array of arguments.
4005 * @param[in] arg_count Count of elements in @p args.
4006 * @param[in,out] set Context and result set at the same time.
4007 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004008 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004009 */
4010static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004011xpath_false(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004012{
4013 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004014 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004015 return LY_SUCCESS;
4016 }
4017
4018 set_fill_boolean(set, 0);
4019 return LY_SUCCESS;
4020}
4021
4022/**
4023 * @brief Execute the XPath floor(number) function. Returns LYXP_SET_NUMBER
4024 * with the first argument floored (truncated).
4025 *
4026 * @param[in] args Array of arguments.
4027 * @param[in] arg_count Count of elements in @p args.
4028 * @param[in,out] set Context and result set at the same time.
4029 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004030 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004031 */
4032static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004033xpath_floor(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t UNUSED(options))
Michal Vasko03ff5a72019-09-11 13:49:33 +02004034{
4035 LY_ERR rc;
4036
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004037 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004038 LY_CHECK_RET(rc);
4039 if (isfinite(args[0]->val.num)) {
4040 set_fill_number(set, (long long)args[0]->val.num);
4041 }
4042
4043 return LY_SUCCESS;
4044}
4045
4046/**
4047 * @brief Execute the XPath lang(string) function. Returns LYXP_SET_BOOLEAN
4048 * whether the language of the text matches the one from the argument.
4049 *
4050 * @param[in] args Array of arguments.
4051 * @param[in] arg_count Count of elements in @p args.
4052 * @param[in,out] set Context and result set at the same time.
4053 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004054 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004055 */
4056static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004057xpath_lang(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004058{
4059 const struct lyd_node *node;
4060 struct lysc_node_leaf *sleaf;
Michal Vasko9f96a052020-03-10 09:41:45 +01004061 struct lyd_meta *meta = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004062 const char *val;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004063 LY_ERR rc = LY_SUCCESS;
4064
4065 if (options & LYXP_SCNODE_ALL) {
4066 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4067 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004068 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4069 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004070 } else if (!warn_is_string_type(sleaf->type)) {
4071 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004072 }
4073 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004074 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004075 return rc;
4076 }
4077
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004078 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004079 LY_CHECK_RET(rc);
4080
Michal Vasko03ff5a72019-09-11 13:49:33 +02004081 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004082 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "lang(string)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004083 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004084 } else if (!set->used) {
4085 set_fill_boolean(set, 0);
4086 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004087 }
4088
4089 switch (set->val.nodes[0].type) {
4090 case LYXP_NODE_ELEM:
4091 case LYXP_NODE_TEXT:
4092 node = set->val.nodes[0].node;
4093 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004094 case LYXP_NODE_META:
4095 node = set->val.meta[0].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004096 break;
4097 default:
4098 /* nothing to do with roots */
4099 set_fill_boolean(set, 0);
4100 return LY_SUCCESS;
4101 }
4102
Michal Vasko9f96a052020-03-10 09:41:45 +01004103 /* find lang metadata */
Michal Vasko9e685082021-01-29 14:49:09 +01004104 for ( ; node; node = lyd_parent(node)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01004105 for (meta = node->meta; meta; meta = meta->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004106 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004107 if (meta->name && !strcmp(meta->name, "lang") && !strcmp(meta->annotation->module->name, "xml")) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004108 break;
4109 }
4110 }
4111
Michal Vasko9f96a052020-03-10 09:41:45 +01004112 if (meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004113 break;
4114 }
4115 }
4116
4117 /* compare languages */
Michal Vasko9f96a052020-03-10 09:41:45 +01004118 if (!meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004119 set_fill_boolean(set, 0);
4120 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004121 uint64_t i;
4122
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02004123 val = lyd_get_meta_value(meta);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004124 for (i = 0; args[0]->val.str[i]; ++i) {
4125 if (tolower(args[0]->val.str[i]) != tolower(val[i])) {
4126 set_fill_boolean(set, 0);
4127 break;
4128 }
4129 }
4130 if (!args[0]->val.str[i]) {
4131 if (!val[i] || (val[i] == '-')) {
4132 set_fill_boolean(set, 1);
4133 } else {
4134 set_fill_boolean(set, 0);
4135 }
4136 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004137 }
4138
4139 return LY_SUCCESS;
4140}
4141
4142/**
4143 * @brief Execute the XPath last() function. Returns LYXP_SET_NUMBER
4144 * with the context size.
4145 *
4146 * @param[in] args Array of arguments.
4147 * @param[in] arg_count Count of elements in @p args.
4148 * @param[in,out] set Context and result set at the same time.
4149 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004150 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004151 */
4152static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004153xpath_last(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004154{
4155 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004156 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004157 return LY_SUCCESS;
4158 }
4159
Michal Vasko03ff5a72019-09-11 13:49:33 +02004160 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004161 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "last()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004162 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004163 } else if (!set->used) {
4164 set_fill_number(set, 0);
4165 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004166 }
4167
4168 set_fill_number(set, set->ctx_size);
4169 return LY_SUCCESS;
4170}
4171
4172/**
4173 * @brief Execute the XPath local-name(node-set?) function. Returns LYXP_SET_STRING
4174 * with the node name without namespace from the argument or the context.
4175 *
4176 * @param[in] args Array of arguments.
4177 * @param[in] arg_count Count of elements in @p args.
4178 * @param[in,out] set Context and result set at the same time.
4179 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004180 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004181 */
4182static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004183xpath_local_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004184{
4185 struct lyxp_set_node *item;
Michal Vasko69730152020-10-09 16:30:07 +02004186
Michal Vasko03ff5a72019-09-11 13:49:33 +02004187 /* suppress unused variable warning */
4188 (void)options;
4189
4190 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004191 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004192 return LY_SUCCESS;
4193 }
4194
4195 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004196 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004197 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004198 "local-name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004199 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004200 } else if (!args[0]->used) {
4201 set_fill_string(set, "", 0);
4202 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004203 }
4204
4205 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004206 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004207
4208 item = &args[0]->val.nodes[0];
4209 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004210 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004211 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "local-name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004212 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004213 } else if (!set->used) {
4214 set_fill_string(set, "", 0);
4215 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004216 }
4217
4218 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004219 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004220
4221 item = &set->val.nodes[0];
4222 }
4223
4224 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004225 case LYXP_NODE_NONE:
4226 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004227 case LYXP_NODE_ROOT:
4228 case LYXP_NODE_ROOT_CONFIG:
4229 case LYXP_NODE_TEXT:
4230 set_fill_string(set, "", 0);
4231 break;
4232 case LYXP_NODE_ELEM:
4233 set_fill_string(set, item->node->schema->name, strlen(item->node->schema->name));
4234 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004235 case LYXP_NODE_META:
4236 set_fill_string(set, ((struct lyd_meta *)item->node)->name, strlen(((struct lyd_meta *)item->node)->name));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004237 break;
4238 }
4239
4240 return LY_SUCCESS;
4241}
4242
4243/**
4244 * @brief Execute the XPath name(node-set?) function. Returns LYXP_SET_STRING
4245 * with the node name fully qualified (with namespace) from the argument or the context.
4246 *
4247 * @param[in] args Array of arguments.
4248 * @param[in] arg_count Count of elements in @p args.
4249 * @param[in,out] set Context and result set at the same time.
4250 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004251 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004252 */
4253static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004254xpath_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004255{
4256 struct lyxp_set_node *item;
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004257 struct lys_module *mod = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004258 char *str;
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004259 const char *name = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004260
4261 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004262 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004263 return LY_SUCCESS;
4264 }
4265
4266 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004267 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004268 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004269 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004270 } else if (!args[0]->used) {
4271 set_fill_string(set, "", 0);
4272 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004273 }
4274
4275 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004276 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004277
4278 item = &args[0]->val.nodes[0];
4279 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004280 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004281 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004282 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004283 } else if (!set->used) {
4284 set_fill_string(set, "", 0);
4285 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004286 }
4287
4288 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004289 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004290
4291 item = &set->val.nodes[0];
4292 }
4293
4294 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004295 case LYXP_NODE_NONE:
4296 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004297 case LYXP_NODE_ROOT:
4298 case LYXP_NODE_ROOT_CONFIG:
4299 case LYXP_NODE_TEXT:
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004300 /* keep NULL */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004301 break;
4302 case LYXP_NODE_ELEM:
4303 mod = item->node->schema->module;
4304 name = item->node->schema->name;
4305 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004306 case LYXP_NODE_META:
4307 mod = ((struct lyd_meta *)item->node)->annotation->module;
4308 name = ((struct lyd_meta *)item->node)->name;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004309 break;
4310 }
4311
4312 if (mod && name) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004313 int rc = asprintf(&str, "%s:%s", ly_get_prefix(mod, set->format, set->prefix_data), name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004314 LY_CHECK_ERR_RET(rc == -1, LOGMEM(set->ctx), LY_EMEM);
4315 set_fill_string(set, str, strlen(str));
4316 free(str);
4317 } else {
4318 set_fill_string(set, "", 0);
4319 }
4320
4321 return LY_SUCCESS;
4322}
4323
4324/**
4325 * @brief Execute the XPath namespace-uri(node-set?) function. Returns LYXP_SET_STRING
4326 * with the namespace of the node from the argument or the context.
4327 *
4328 * @param[in] args Array of arguments.
4329 * @param[in] arg_count Count of elements in @p args.
4330 * @param[in,out] set Context and result set at the same time.
4331 * @param[in] options XPath options.
4332 * @return LY_ERR (LY_EINVAL for wrong arguments on schema)
4333 */
4334static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004335xpath_namespace_uri(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004336{
4337 struct lyxp_set_node *item;
4338 struct lys_module *mod;
Michal Vasko69730152020-10-09 16:30:07 +02004339
Michal Vasko03ff5a72019-09-11 13:49:33 +02004340 /* suppress unused variable warning */
4341 (void)options;
4342
4343 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004344 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004345 return LY_SUCCESS;
4346 }
4347
4348 if (arg_count) {
Michal Vaskod3678892020-05-21 10:06:58 +02004349 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004350 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
Michal Vasko69730152020-10-09 16:30:07 +02004351 "namespace-uri(node-set?)");
Michal Vaskod3678892020-05-21 10:06:58 +02004352 return LY_EVALID;
4353 } else if (!args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004354 set_fill_string(set, "", 0);
4355 return LY_SUCCESS;
4356 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004357
4358 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004359 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004360
4361 item = &args[0]->val.nodes[0];
4362 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004363 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004364 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "namespace-uri(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004365 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004366 } else if (!set->used) {
4367 set_fill_string(set, "", 0);
4368 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004369 }
4370
4371 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004372 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004373
4374 item = &set->val.nodes[0];
4375 }
4376
4377 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004378 case LYXP_NODE_NONE:
4379 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004380 case LYXP_NODE_ROOT:
4381 case LYXP_NODE_ROOT_CONFIG:
4382 case LYXP_NODE_TEXT:
4383 set_fill_string(set, "", 0);
4384 break;
4385 case LYXP_NODE_ELEM:
Michal Vasko9f96a052020-03-10 09:41:45 +01004386 case LYXP_NODE_META:
Michal Vasko03ff5a72019-09-11 13:49:33 +02004387 if (item->type == LYXP_NODE_ELEM) {
4388 mod = item->node->schema->module;
Michal Vasko9f96a052020-03-10 09:41:45 +01004389 } else { /* LYXP_NODE_META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004390 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004391 mod = ((struct lyd_meta *)item->node)->annotation->module;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004392 }
4393
4394 set_fill_string(set, mod->ns, strlen(mod->ns));
4395 break;
4396 }
4397
4398 return LY_SUCCESS;
4399}
4400
4401/**
4402 * @brief Execute the XPath node() function (node type). Returns LYXP_SET_NODE_SET
4403 * with only nodes from the context. In practice it either leaves the context
4404 * as it is or returns an empty node set.
4405 *
4406 * @param[in] args Array of arguments.
4407 * @param[in] arg_count Count of elements in @p args.
4408 * @param[in,out] set Context and result set at the same time.
4409 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004410 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004411 */
4412static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004413xpath_node(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004414{
4415 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004416 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004417 return LY_SUCCESS;
4418 }
4419
4420 if (set->type != LYXP_SET_NODE_SET) {
Michal Vaskod3678892020-05-21 10:06:58 +02004421 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004422 }
4423 return LY_SUCCESS;
4424}
4425
4426/**
4427 * @brief Execute the XPath normalize-space(string?) function. Returns LYXP_SET_STRING
4428 * with normalized value (no leading, trailing, double white spaces) of the node
4429 * from the argument or the context.
4430 *
4431 * @param[in] args Array of arguments.
4432 * @param[in] arg_count Count of elements in @p args.
4433 * @param[in,out] set Context and result set at the same time.
4434 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004435 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004436 */
4437static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004438xpath_normalize_space(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004439{
4440 uint16_t i, new_used;
4441 char *new;
Radek Krejci857189e2020-09-01 13:26:36 +02004442 ly_bool have_spaces = 0, space_before = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004443 struct lysc_node_leaf *sleaf;
4444 LY_ERR rc = LY_SUCCESS;
4445
4446 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004447 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) &&
4448 (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004449 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004450 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4451 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004452 } else if (!warn_is_string_type(sleaf->type)) {
4453 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004454 }
4455 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004456 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004457 return rc;
4458 }
4459
4460 if (arg_count) {
4461 set_fill_set(set, args[0]);
4462 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004463 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004464 LY_CHECK_RET(rc);
4465
4466 /* is there any normalization necessary? */
4467 for (i = 0; set->val.str[i]; ++i) {
4468 if (is_xmlws(set->val.str[i])) {
4469 if ((i == 0) || space_before || (!set->val.str[i + 1])) {
4470 have_spaces = 1;
4471 break;
4472 }
4473 space_before = 1;
4474 } else {
4475 space_before = 0;
4476 }
4477 }
4478
4479 /* yep, there is */
4480 if (have_spaces) {
4481 /* it's enough, at least one character will go, makes space for ending '\0' */
4482 new = malloc(strlen(set->val.str) * sizeof(char));
4483 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4484 new_used = 0;
4485
4486 space_before = 0;
4487 for (i = 0; set->val.str[i]; ++i) {
4488 if (is_xmlws(set->val.str[i])) {
4489 if ((i == 0) || space_before) {
4490 space_before = 1;
4491 continue;
4492 } else {
4493 space_before = 1;
4494 }
4495 } else {
4496 space_before = 0;
4497 }
4498
4499 new[new_used] = (space_before ? ' ' : set->val.str[i]);
4500 ++new_used;
4501 }
4502
4503 /* at worst there is one trailing space now */
4504 if (new_used && is_xmlws(new[new_used - 1])) {
4505 --new_used;
4506 }
4507
4508 new = ly_realloc(new, (new_used + 1) * sizeof(char));
4509 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4510 new[new_used] = '\0';
4511
4512 free(set->val.str);
4513 set->val.str = new;
4514 }
4515
4516 return LY_SUCCESS;
4517}
4518
4519/**
4520 * @brief Execute the XPath not(boolean) function. Returns LYXP_SET_BOOLEAN
4521 * with the argument converted to boolean and logically inverted.
4522 *
4523 * @param[in] args Array of arguments.
4524 * @param[in] arg_count Count of elements in @p args.
4525 * @param[in,out] set Context and result set at the same time.
4526 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004527 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004528 */
4529static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004530xpath_not(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004531{
4532 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004533 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004534 return LY_SUCCESS;
4535 }
4536
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004537 lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02004538 if (args[0]->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004539 set_fill_boolean(set, 0);
4540 } else {
4541 set_fill_boolean(set, 1);
4542 }
4543
4544 return LY_SUCCESS;
4545}
4546
4547/**
4548 * @brief Execute the XPath number(object?) function. Returns LYXP_SET_NUMBER
4549 * with the number representation of either the argument or the context.
4550 *
4551 * @param[in] args Array of arguments.
4552 * @param[in] arg_count Count of elements in @p args.
4553 * @param[in,out] set Context and result set at the same time.
4554 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004555 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004556 */
4557static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004558xpath_number(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004559{
4560 LY_ERR rc;
4561
4562 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004563 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004564 return LY_SUCCESS;
4565 }
4566
4567 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004568 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004569 LY_CHECK_RET(rc);
4570 set_fill_set(set, args[0]);
4571 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004572 rc = lyxp_set_cast(set, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004573 LY_CHECK_RET(rc);
4574 }
4575
4576 return LY_SUCCESS;
4577}
4578
4579/**
4580 * @brief Execute the XPath position() function. Returns LYXP_SET_NUMBER
4581 * with the context position.
4582 *
4583 * @param[in] args Array of arguments.
4584 * @param[in] arg_count Count of elements in @p args.
4585 * @param[in,out] set Context and result set at the same time.
4586 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004587 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004588 */
4589static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004590xpath_position(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004591{
4592 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004593 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004594 return LY_SUCCESS;
4595 }
4596
Michal Vasko03ff5a72019-09-11 13:49:33 +02004597 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004598 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "position()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004599 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004600 } else if (!set->used) {
4601 set_fill_number(set, 0);
4602 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004603 }
4604
4605 set_fill_number(set, set->ctx_pos);
4606
4607 /* UNUSED in 'Release' build type */
4608 (void)options;
4609 return LY_SUCCESS;
4610}
4611
4612/**
4613 * @brief Execute the YANG 1.1 re-match(string, string) function. Returns LYXP_SET_BOOLEAN
4614 * depending on whether the second argument regex matches the first argument string. For details refer to
4615 * YANG 1.1 RFC section 10.2.1.
4616 *
4617 * @param[in] args Array of arguments.
4618 * @param[in] arg_count Count of elements in @p args.
4619 * @param[in,out] set Context and result set at the same time.
4620 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004621 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004622 */
4623static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004624xpath_re_match(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004625{
4626 struct lysc_pattern **patterns = NULL, **pattern;
4627 struct lysc_node_leaf *sleaf;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004628 LY_ERR rc = LY_SUCCESS;
4629 struct ly_err_item *err;
4630
4631 if (options & LYXP_SCNODE_ALL) {
4632 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4633 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4634 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 +02004635 } else if (!warn_is_string_type(sleaf->type)) {
4636 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004637 }
4638 }
4639
4640 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4641 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4642 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 +02004643 } else if (!warn_is_string_type(sleaf->type)) {
4644 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004645 }
4646 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004647 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004648 return rc;
4649 }
4650
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004651 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004652 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004653 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004654 LY_CHECK_RET(rc);
4655
4656 LY_ARRAY_NEW_RET(set->ctx, patterns, pattern, LY_EMEM);
Radek IÅ¡a45802b52021-02-09 09:21:58 +01004657 *pattern = calloc(1, sizeof **pattern);
Radek Krejciddace2c2021-01-08 11:30:56 +01004658 LOG_LOCSET(NULL, set->cur_node, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01004659 rc = lys_compile_type_pattern_check(set->ctx, args[1]->val.str, &(*pattern)->code);
Radek Krejciddace2c2021-01-08 11:30:56 +01004660 LOG_LOCBACK(0, 1, 0, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004661 if (rc != LY_SUCCESS) {
4662 LY_ARRAY_FREE(patterns);
4663 return rc;
4664 }
4665
Radek Krejci0b013302021-03-29 15:22:32 +02004666 rc = lyplg_type_validate_patterns(patterns, args[0]->val.str, strlen(args[0]->val.str), &err);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004667 pcre2_code_free((*pattern)->code);
4668 free(*pattern);
4669 LY_ARRAY_FREE(patterns);
4670 if (rc && (rc != LY_EVALID)) {
Michal Vasko177d0ed2020-11-23 16:43:03 +01004671 ly_err_print(set->ctx, err);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004672 ly_err_free(err);
4673 return rc;
4674 }
4675
4676 if (rc == LY_EVALID) {
4677 ly_err_free(err);
4678 set_fill_boolean(set, 0);
4679 } else {
4680 set_fill_boolean(set, 1);
4681 }
4682
4683 return LY_SUCCESS;
4684}
4685
4686/**
4687 * @brief Execute the XPath round(number) function. Returns LYXP_SET_NUMBER
4688 * with the rounded first argument. For details refer to
4689 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-round.
4690 *
4691 * @param[in] args Array of arguments.
4692 * @param[in] arg_count Count of elements in @p args.
4693 * @param[in,out] set Context and result set at the same time.
4694 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004695 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004696 */
4697static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004698xpath_round(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004699{
4700 struct lysc_node_leaf *sleaf;
4701 LY_ERR rc = LY_SUCCESS;
4702
4703 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02004704 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004705 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02004706 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4707 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4708 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4709 sleaf->name);
4710 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
4711 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
4712 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004713 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004714 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004715 return rc;
4716 }
4717
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004718 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004719 LY_CHECK_RET(rc);
4720
4721 /* cover only the cases where floor can't be used */
4722 if ((args[0]->val.num == -0.0f) || ((args[0]->val.num < 0) && (args[0]->val.num >= -0.5))) {
4723 set_fill_number(set, -0.0f);
4724 } else {
4725 args[0]->val.num += 0.5;
4726 rc = xpath_floor(args, 1, args[0], options);
4727 LY_CHECK_RET(rc);
4728 set_fill_number(set, args[0]->val.num);
4729 }
4730
4731 return LY_SUCCESS;
4732}
4733
4734/**
4735 * @brief Execute the XPath starts-with(string, string) function.
4736 * Returns LYXP_SET_BOOLEAN whether the second argument is
4737 * the prefix of the first or not.
4738 *
4739 * @param[in] args Array of arguments.
4740 * @param[in] arg_count Count of elements in @p args.
4741 * @param[in,out] set Context and result set at the same time.
4742 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004743 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004744 */
4745static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004746xpath_starts_with(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004747{
4748 struct lysc_node_leaf *sleaf;
4749 LY_ERR rc = LY_SUCCESS;
4750
4751 if (options & LYXP_SCNODE_ALL) {
4752 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4753 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4754 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 +02004755 } else if (!warn_is_string_type(sleaf->type)) {
4756 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004757 }
4758 }
4759
4760 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4761 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4762 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 +02004763 } else if (!warn_is_string_type(sleaf->type)) {
4764 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004765 }
4766 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004767 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004768 return rc;
4769 }
4770
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004771 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004772 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004773 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004774 LY_CHECK_RET(rc);
4775
4776 if (strncmp(args[0]->val.str, args[1]->val.str, strlen(args[1]->val.str))) {
4777 set_fill_boolean(set, 0);
4778 } else {
4779 set_fill_boolean(set, 1);
4780 }
4781
4782 return LY_SUCCESS;
4783}
4784
4785/**
4786 * @brief Execute the XPath string(object?) function. Returns LYXP_SET_STRING
4787 * with the string representation of either the argument or the context.
4788 *
4789 * @param[in] args Array of arguments.
4790 * @param[in] arg_count Count of elements in @p args.
4791 * @param[in,out] set Context and result set at the same time.
4792 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004793 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004794 */
4795static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004796xpath_string(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004797{
4798 LY_ERR rc;
4799
4800 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004801 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004802 return LY_SUCCESS;
4803 }
4804
4805 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004806 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004807 LY_CHECK_RET(rc);
4808 set_fill_set(set, args[0]);
4809 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004810 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004811 LY_CHECK_RET(rc);
4812 }
4813
4814 return LY_SUCCESS;
4815}
4816
4817/**
4818 * @brief Execute the XPath string-length(string?) function. Returns LYXP_SET_NUMBER
4819 * with the length of the string in either the argument or the context.
4820 *
4821 * @param[in] args Array of arguments.
4822 * @param[in] arg_count Count of elements in @p args.
4823 * @param[in,out] set Context and result set at the same time.
4824 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004825 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004826 */
4827static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004828xpath_string_length(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004829{
4830 struct lysc_node_leaf *sleaf;
4831 LY_ERR rc = LY_SUCCESS;
4832
4833 if (options & LYXP_SCNODE_ALL) {
4834 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4835 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4836 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 +02004837 } else if (!warn_is_string_type(sleaf->type)) {
4838 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004839 }
4840 }
4841 if (!arg_count && (set->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set))) {
4842 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4843 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 +02004844 } else if (!warn_is_string_type(sleaf->type)) {
4845 LOGWRN(set->ctx, "Argument #0 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004846 }
4847 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004848 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004849 return rc;
4850 }
4851
4852 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004853 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004854 LY_CHECK_RET(rc);
4855 set_fill_number(set, strlen(args[0]->val.str));
4856 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004857 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004858 LY_CHECK_RET(rc);
4859 set_fill_number(set, strlen(set->val.str));
4860 }
4861
4862 return LY_SUCCESS;
4863}
4864
4865/**
4866 * @brief Execute the XPath substring(string, number, number?) function.
4867 * Returns LYXP_SET_STRING substring of the first argument starting
4868 * on the second argument index ending on the third argument index,
4869 * indexed from 1. For exact definition refer to
4870 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-substring.
4871 *
4872 * @param[in] args Array of arguments.
4873 * @param[in] arg_count Count of elements in @p args.
4874 * @param[in,out] set Context and result set at the same time.
4875 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004876 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004877 */
4878static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004879xpath_substring(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004880{
Radek Krejci1deb5be2020-08-26 16:43:36 +02004881 int32_t start, len;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004882 uint16_t str_start, str_len, pos;
4883 struct lysc_node_leaf *sleaf;
4884 LY_ERR rc = LY_SUCCESS;
4885
4886 if (options & LYXP_SCNODE_ALL) {
4887 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4888 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4889 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 +02004890 } else if (!warn_is_string_type(sleaf->type)) {
4891 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004892 }
4893 }
4894
4895 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4896 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4897 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 +02004898 } else if (!warn_is_numeric_type(sleaf->type)) {
4899 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004900 }
4901 }
4902
Michal Vasko69730152020-10-09 16:30:07 +02004903 if ((arg_count == 3) && (args[2]->type == LYXP_SET_SCNODE_SET) &&
4904 (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004905 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4906 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 +02004907 } else if (!warn_is_numeric_type(sleaf->type)) {
4908 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004909 }
4910 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004911 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004912 return rc;
4913 }
4914
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004915 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004916 LY_CHECK_RET(rc);
4917
4918 /* start */
4919 if (xpath_round(&args[1], 1, args[1], options)) {
4920 return -1;
4921 }
4922 if (isfinite(args[1]->val.num)) {
4923 start = args[1]->val.num - 1;
4924 } else if (isinf(args[1]->val.num) && signbit(args[1]->val.num)) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004925 start = INT32_MIN;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004926 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004927 start = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004928 }
4929
4930 /* len */
4931 if (arg_count == 3) {
4932 rc = xpath_round(&args[2], 1, args[2], options);
4933 LY_CHECK_RET(rc);
Radek Krejci1deb5be2020-08-26 16:43:36 +02004934 if (isnan(args[2]->val.num) || signbit(args[2]->val.num)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004935 len = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02004936 } else if (isfinite(args[2]->val.num)) {
4937 len = args[2]->val.num;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004938 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004939 len = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004940 }
4941 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004942 len = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004943 }
4944
4945 /* find matching character positions */
4946 str_start = 0;
4947 str_len = 0;
4948 for (pos = 0; args[0]->val.str[pos]; ++pos) {
4949 if (pos < start) {
4950 ++str_start;
4951 } else if (pos < start + len) {
4952 ++str_len;
4953 } else {
4954 break;
4955 }
4956 }
4957
4958 set_fill_string(set, args[0]->val.str + str_start, str_len);
4959 return LY_SUCCESS;
4960}
4961
4962/**
4963 * @brief Execute the XPath substring-after(string, string) function.
4964 * Returns LYXP_SET_STRING with the string succeeding the occurance
4965 * of the second argument in the first or an empty string.
4966 *
4967 * @param[in] args Array of arguments.
4968 * @param[in] arg_count Count of elements in @p args.
4969 * @param[in,out] set Context and result set at the same time.
4970 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004971 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004972 */
4973static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004974xpath_substring_after(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004975{
4976 char *ptr;
4977 struct lysc_node_leaf *sleaf;
4978 LY_ERR rc = LY_SUCCESS;
4979
4980 if (options & LYXP_SCNODE_ALL) {
4981 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4982 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4983 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 +02004984 } else if (!warn_is_string_type(sleaf->type)) {
4985 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004986 }
4987 }
4988
4989 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4990 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4991 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 +02004992 } else if (!warn_is_string_type(sleaf->type)) {
4993 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004994 }
4995 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004996 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004997 return rc;
4998 }
4999
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005000 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005001 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005002 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005003 LY_CHECK_RET(rc);
5004
5005 ptr = strstr(args[0]->val.str, args[1]->val.str);
5006 if (ptr) {
5007 set_fill_string(set, ptr + strlen(args[1]->val.str), strlen(ptr + strlen(args[1]->val.str)));
5008 } else {
5009 set_fill_string(set, "", 0);
5010 }
5011
5012 return LY_SUCCESS;
5013}
5014
5015/**
5016 * @brief Execute the XPath substring-before(string, string) function.
5017 * Returns LYXP_SET_STRING with the string preceding the occurance
5018 * of the second argument in the first or an empty string.
5019 *
5020 * @param[in] args Array of arguments.
5021 * @param[in] arg_count Count of elements in @p args.
5022 * @param[in,out] set Context and result set at the same time.
5023 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005024 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005025 */
5026static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005027xpath_substring_before(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005028{
5029 char *ptr;
5030 struct lysc_node_leaf *sleaf;
5031 LY_ERR rc = LY_SUCCESS;
5032
5033 if (options & LYXP_SCNODE_ALL) {
5034 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5035 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5036 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 +02005037 } else if (!warn_is_string_type(sleaf->type)) {
5038 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005039 }
5040 }
5041
5042 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5043 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5044 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 +02005045 } else if (!warn_is_string_type(sleaf->type)) {
5046 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005047 }
5048 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005049 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005050 return rc;
5051 }
5052
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005053 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005054 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005055 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005056 LY_CHECK_RET(rc);
5057
5058 ptr = strstr(args[0]->val.str, args[1]->val.str);
5059 if (ptr) {
5060 set_fill_string(set, args[0]->val.str, ptr - args[0]->val.str);
5061 } else {
5062 set_fill_string(set, "", 0);
5063 }
5064
5065 return LY_SUCCESS;
5066}
5067
5068/**
5069 * @brief Execute the XPath sum(node-set) function. Returns LYXP_SET_NUMBER
5070 * with the sum of all the nodes in the context.
5071 *
5072 * @param[in] args Array of arguments.
5073 * @param[in] arg_count Count of elements in @p args.
5074 * @param[in,out] set Context and result set at the same time.
5075 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005076 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005077 */
5078static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005079xpath_sum(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005080{
5081 long double num;
5082 char *str;
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01005083 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005084 struct lyxp_set set_item;
5085 struct lysc_node_leaf *sleaf;
5086 LY_ERR rc = LY_SUCCESS;
5087
5088 if (options & LYXP_SCNODE_ALL) {
5089 if (args[0]->type == LYXP_SET_SCNODE_SET) {
5090 for (i = 0; i < args[0]->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005091 if (args[0]->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005092 sleaf = (struct lysc_node_leaf *)args[0]->val.scnodes[i].scnode;
5093 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5094 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__,
Michal Vasko69730152020-10-09 16:30:07 +02005095 lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005096 } else if (!warn_is_numeric_type(sleaf->type)) {
5097 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005098 }
5099 }
5100 }
5101 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005102 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005103 return rc;
5104 }
5105
5106 set_fill_number(set, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005107
5108 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005109 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "sum(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02005110 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02005111 } else if (!args[0]->used) {
5112 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005113 }
5114
Michal Vasko5c4e5892019-11-14 12:31:38 +01005115 set_init(&set_item, set);
5116
Michal Vasko03ff5a72019-09-11 13:49:33 +02005117 set_item.type = LYXP_SET_NODE_SET;
5118 set_item.val.nodes = malloc(sizeof *set_item.val.nodes);
5119 LY_CHECK_ERR_RET(!set_item.val.nodes, LOGMEM(set->ctx), LY_EMEM);
5120
5121 set_item.used = 1;
5122 set_item.size = 1;
5123
5124 for (i = 0; i < args[0]->used; ++i) {
5125 set_item.val.nodes[0] = args[0]->val.nodes[i];
5126
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005127 rc = cast_node_set_to_string(&set_item, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005128 LY_CHECK_RET(rc);
5129 num = cast_string_to_number(str);
5130 free(str);
5131 set->val.num += num;
5132 }
5133
5134 free(set_item.val.nodes);
5135
5136 return LY_SUCCESS;
5137}
5138
5139/**
5140 * @brief Execute the XPath text() function (node type). Returns LYXP_SET_NODE_SET
5141 * with the text content of the nodes in the context.
5142 *
5143 * @param[in] args Array of arguments.
5144 * @param[in] arg_count Count of elements in @p args.
5145 * @param[in,out] set Context and result set at the same time.
5146 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005147 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005148 */
5149static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005150xpath_text(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005151{
5152 uint32_t i;
5153
5154 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02005155 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005156 return LY_SUCCESS;
5157 }
5158
Michal Vasko03ff5a72019-09-11 13:49:33 +02005159 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005160 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "text()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02005161 return LY_EVALID;
5162 }
5163
Michal Vaskod989ba02020-08-24 10:59:24 +02005164 for (i = 0; i < set->used; ) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005165 switch (set->val.nodes[i].type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01005166 case LYXP_NODE_NONE:
5167 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005168 case LYXP_NODE_ELEM:
Michal Vasko03ff5a72019-09-11 13:49:33 +02005169 if (set->val.nodes[i].node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
5170 set->val.nodes[i].type = LYXP_NODE_TEXT;
5171 ++i;
5172 break;
5173 }
Radek Krejci0f969882020-08-21 16:56:47 +02005174 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005175 case LYXP_NODE_ROOT:
5176 case LYXP_NODE_ROOT_CONFIG:
5177 case LYXP_NODE_TEXT:
Michal Vasko9f96a052020-03-10 09:41:45 +01005178 case LYXP_NODE_META:
Michal Vasko03ff5a72019-09-11 13:49:33 +02005179 set_remove_node(set, i);
5180 break;
5181 }
5182 }
5183
5184 return LY_SUCCESS;
5185}
5186
5187/**
5188 * @brief Execute the XPath translate(string, string, string) function.
5189 * Returns LYXP_SET_STRING with the first argument with the characters
5190 * from the second argument replaced by those on the corresponding
5191 * positions in the third argument.
5192 *
5193 * @param[in] args Array of arguments.
5194 * @param[in] arg_count Count of elements in @p args.
5195 * @param[in,out] set Context and result set at the same time.
5196 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005197 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005198 */
5199static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005200xpath_translate(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005201{
5202 uint16_t i, j, new_used;
5203 char *new;
Radek Krejci857189e2020-09-01 13:26:36 +02005204 ly_bool have_removed;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005205 struct lysc_node_leaf *sleaf;
5206 LY_ERR rc = LY_SUCCESS;
5207
5208 if (options & LYXP_SCNODE_ALL) {
5209 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5210 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5211 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 +02005212 } else if (!warn_is_string_type(sleaf->type)) {
5213 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005214 }
5215 }
5216
5217 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5218 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5219 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 +02005220 } else if (!warn_is_string_type(sleaf->type)) {
5221 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005222 }
5223 }
5224
5225 if ((args[2]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
5226 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5227 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 +02005228 } else if (!warn_is_string_type(sleaf->type)) {
5229 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005230 }
5231 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005232 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005233 return rc;
5234 }
5235
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005236 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005237 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005238 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005239 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005240 rc = lyxp_set_cast(args[2], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005241 LY_CHECK_RET(rc);
5242
5243 new = malloc((strlen(args[0]->val.str) + 1) * sizeof(char));
5244 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5245 new_used = 0;
5246
5247 have_removed = 0;
5248 for (i = 0; args[0]->val.str[i]; ++i) {
Radek Krejci857189e2020-09-01 13:26:36 +02005249 ly_bool found = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005250
5251 for (j = 0; args[1]->val.str[j]; ++j) {
5252 if (args[0]->val.str[i] == args[1]->val.str[j]) {
5253 /* removing this char */
5254 if (j >= strlen(args[2]->val.str)) {
5255 have_removed = 1;
5256 found = 1;
5257 break;
5258 }
5259 /* replacing this char */
5260 new[new_used] = args[2]->val.str[j];
5261 ++new_used;
5262 found = 1;
5263 break;
5264 }
5265 }
5266
5267 /* copying this char */
5268 if (!found) {
5269 new[new_used] = args[0]->val.str[i];
5270 ++new_used;
5271 }
5272 }
5273
5274 if (have_removed) {
5275 new = ly_realloc(new, (new_used + 1) * sizeof(char));
5276 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5277 }
5278 new[new_used] = '\0';
5279
Michal Vaskod3678892020-05-21 10:06:58 +02005280 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005281 set->type = LYXP_SET_STRING;
5282 set->val.str = new;
5283
5284 return LY_SUCCESS;
5285}
5286
5287/**
5288 * @brief Execute the XPath true() function. Returns LYXP_SET_BOOLEAN
5289 * with true value.
5290 *
5291 * @param[in] args Array of arguments.
5292 * @param[in] arg_count Count of elements in @p args.
5293 * @param[in,out] set Context and result set at the same time.
5294 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005295 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005296 */
5297static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005298xpath_true(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005299{
5300 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02005301 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005302 return LY_SUCCESS;
5303 }
5304
5305 set_fill_boolean(set, 1);
5306 return LY_SUCCESS;
5307}
5308
5309/*
5310 * moveto functions
5311 *
5312 * They and only they actually change the context (set).
5313 */
5314
5315/**
Michal Vasko6346ece2019-09-24 13:12:53 +02005316 * @brief Skip prefix and return corresponding model if there is a prefix. Logs directly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005317 *
Michal Vasko2104e9f2020-03-06 08:23:25 +01005318 * XPath @p set is expected to be a (sc)node set!
5319 *
Michal Vasko6346ece2019-09-24 13:12:53 +02005320 * @param[in,out] qname Qualified node name. If includes prefix, it is skipped.
5321 * @param[in,out] qname_len Length of @p qname, is updated accordingly.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005322 * @param[in] set Set with general XPath context.
5323 * @param[in] ctx_scnode Context node to inherit module for unprefixed node for ::LY_PREF_JSON.
Michal Vasko6346ece2019-09-24 13:12:53 +02005324 * @param[out] moveto_mod Expected module of a matching node.
5325 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005326 */
Michal Vasko6346ece2019-09-24 13:12:53 +02005327static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005328moveto_resolve_model(const char **qname, uint16_t *qname_len, const struct lyxp_set *set,
5329 const struct lysc_node *ctx_scnode, const struct lys_module **moveto_mod)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005330{
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02005331 const struct lys_module *mod = NULL;
Michal Vasko6346ece2019-09-24 13:12:53 +02005332 const char *ptr;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005333 size_t pref_len;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005334
Michal Vasko2104e9f2020-03-06 08:23:25 +01005335 assert((set->type == LYXP_SET_NODE_SET) || (set->type == LYXP_SET_SCNODE_SET));
5336
Michal Vasko6346ece2019-09-24 13:12:53 +02005337 if ((ptr = ly_strnchr(*qname, ':', *qname_len))) {
5338 /* specific module */
5339 pref_len = ptr - *qname;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005340 mod = ly_resolve_prefix(set->ctx, *qname, pref_len, set->format, set->prefix_data);
Michal Vasko6346ece2019-09-24 13:12:53 +02005341
Michal Vasko004d3152020-06-11 19:59:22 +02005342 /* check for errors and non-implemented modules, as they are not valid */
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005343 if (!mod || !mod->implemented) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005344 LOGVAL(set->ctx, LY_VCODE_XP_INMOD, pref_len, *qname);
Michal Vasko6346ece2019-09-24 13:12:53 +02005345 return LY_EVALID;
5346 }
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005347
Michal Vasko6346ece2019-09-24 13:12:53 +02005348 *qname += pref_len + 1;
5349 *qname_len -= pref_len + 1;
5350 } else if (((*qname)[0] == '*') && (*qname_len == 1)) {
5351 /* all modules - special case */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005352 mod = NULL;
Michal Vasko6346ece2019-09-24 13:12:53 +02005353 } else {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005354 switch (set->format) {
Radek Krejci8df109d2021-04-23 12:19:08 +02005355 case LY_VALUE_SCHEMA:
5356 case LY_VALUE_SCHEMA_RESOLVED:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005357 /* current module */
5358 mod = set->cur_mod;
5359 break;
Radek Krejci224d4b42021-04-23 13:54:59 +02005360 case LY_VALUE_CANON:
Radek Krejci8df109d2021-04-23 12:19:08 +02005361 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +02005362 case LY_VALUE_LYB:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005363 /* inherit parent (context node) module */
5364 if (ctx_scnode) {
5365 mod = ctx_scnode->module;
5366 } else {
5367 mod = NULL;
5368 }
5369 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02005370 case LY_VALUE_XML:
Michal Vasko52143d12021-04-14 15:36:39 +02005371 /* all nodes need to be prefixed */
5372 LOGVAL(set->ctx, LYVE_DATA, "Non-prefixed node \"%.*s\" in XML xpath found.", *qname_len, *qname);
5373 return LY_EVALID;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005374 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005375 }
5376
Michal Vasko6346ece2019-09-24 13:12:53 +02005377 *moveto_mod = mod;
5378 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005379}
5380
5381/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02005382 * @brief Move context @p set to the root. Handles absolute path.
5383 * Result is LYXP_SET_NODE_SET.
5384 *
5385 * @param[in,out] set Set to use.
5386 * @param[in] options Xpath options.
Michal Vaskob0099a92020-08-31 14:55:23 +02005387 * @return LY_ERR value.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005388 */
Michal Vaskob0099a92020-08-31 14:55:23 +02005389static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005390moveto_root(struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005391{
aPiecek8b0cc152021-05-31 16:40:31 +02005392 assert(!(options & LYXP_SKIP_EXPR));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005393
5394 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02005395 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskob0099a92020-08-31 14:55:23 +02005396 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, NULL, set->root_type, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005397 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005398 set->type = LYXP_SET_NODE_SET;
5399 set->used = 0;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005400 set_insert_node(set, NULL, 0, set->root_type, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005401 }
Michal Vaskob0099a92020-08-31 14:55:23 +02005402
5403 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005404}
5405
5406/**
5407 * @brief Check @p node as a part of NameTest processing.
5408 *
5409 * @param[in] node Node to check.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005410 * @param[in] ctx_node Context node.
5411 * @param[in] set Set to read general context from.
Michal Vaskod3678892020-05-21 10:06:58 +02005412 * @param[in] node_name Node name in the dictionary to move to, NULL for any node.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005413 * @param[in] moveto_mod Expected module of the node, NULL for no prefix.
Michal Vaskocdad7122020-11-09 21:04:44 +01005414 * @param[in] options XPath options.
Michal Vasko6346ece2019-09-24 13:12:53 +02005415 * @return LY_ERR (LY_ENOT if node does not match, LY_EINCOMPLETE on unresolved when,
5416 * LY_EINVAL if netither node nor any children match)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005417 */
5418static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005419moveto_node_check(const struct lyd_node *node, const struct lyd_node *ctx_node, const struct lyxp_set *set,
Michal Vaskocdad7122020-11-09 21:04:44 +01005420 const char *node_name, const struct lys_module *moveto_mod, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005421{
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005422 if (!moveto_mod && (!node_name || strcmp(node_name, "*"))) {
5423 switch (set->format) {
Radek Krejci8df109d2021-04-23 12:19:08 +02005424 case LY_VALUE_SCHEMA:
5425 case LY_VALUE_SCHEMA_RESOLVED:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005426 /* use current module */
5427 moveto_mod = set->cur_mod;
5428 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02005429 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +02005430 case LY_VALUE_LYB:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005431 /* inherit module of the context node, if any */
5432 if (ctx_node) {
5433 moveto_mod = ctx_node->schema->module;
5434 }
5435 break;
Radek Krejci224d4b42021-04-23 13:54:59 +02005436 case LY_VALUE_CANON:
Radek Krejci8df109d2021-04-23 12:19:08 +02005437 case LY_VALUE_XML:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005438 /* not defined */
5439 LOGINT(set->ctx);
5440 return LY_EINVAL;
5441 }
5442 }
5443
Michal Vasko03ff5a72019-09-11 13:49:33 +02005444 /* module check */
5445 if (moveto_mod && (node->schema->module != moveto_mod)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005446 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005447 }
5448
Michal Vasko5c4e5892019-11-14 12:31:38 +01005449 /* context check */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005450 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005451 return LY_EINVAL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005452 } else if (set->context_op && (node->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) &&
5453 (node->schema != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005454 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005455 }
5456
5457 /* name check */
Michal Vasko61ac2f62020-05-25 12:39:51 +02005458 if (node_name && strcmp(node_name, "*") && (node->schema->name != node_name)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005459 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005460 }
5461
Michal Vaskoa1424542019-11-14 16:08:52 +01005462 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01005463 if (!(options & LYXP_IGNORE_WHEN) && lysc_has_when(node->schema) && !(node->flags & LYD_WHEN_TRUE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005464 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01005465 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005466
5467 /* match */
5468 return LY_SUCCESS;
5469}
5470
5471/**
5472 * @brief Check @p node as a part of schema NameTest processing.
5473 *
5474 * @param[in] node Schema node to check.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005475 * @param[in] ctx_scnode Context node.
5476 * @param[in] set Set to read general context from.
Michal Vaskod3678892020-05-21 10:06:58 +02005477 * @param[in] node_name Node name in the dictionary to move to, NULL for any nodes.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005478 * @param[in] moveto_mod Expected module of the node, NULL for no prefix.
Michal Vasko6346ece2019-09-24 13:12:53 +02005479 * @return LY_ERR (LY_ENOT if node does not match, LY_EINVAL if neither node nor any children match)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005480 */
5481static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005482moveto_scnode_check(const struct lysc_node *node, const struct lysc_node *ctx_scnode, const struct lyxp_set *set,
Radek Krejci0f969882020-08-21 16:56:47 +02005483 const char *node_name, const struct lys_module *moveto_mod)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005484{
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005485 if (!moveto_mod && (!node_name || strcmp(node_name, "*"))) {
5486 switch (set->format) {
Radek Krejci8df109d2021-04-23 12:19:08 +02005487 case LY_VALUE_SCHEMA:
5488 case LY_VALUE_SCHEMA_RESOLVED:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005489 /* use current module */
5490 moveto_mod = set->cur_mod;
5491 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02005492 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +02005493 case LY_VALUE_LYB:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005494 /* inherit module of the context node, if any */
5495 if (ctx_scnode) {
5496 moveto_mod = ctx_scnode->module;
5497 }
5498 break;
Radek Krejci224d4b42021-04-23 13:54:59 +02005499 case LY_VALUE_CANON:
Radek Krejci8df109d2021-04-23 12:19:08 +02005500 case LY_VALUE_XML:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005501 /* not defined */
5502 LOGINT(set->ctx);
5503 return LY_EINVAL;
5504 }
5505 }
5506
Michal Vasko03ff5a72019-09-11 13:49:33 +02005507 /* module check */
Michal Vaskod3678892020-05-21 10:06:58 +02005508 if (moveto_mod && (node->module != moveto_mod)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005509 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005510 }
5511
5512 /* context check */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005513 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (node->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005514 return LY_EINVAL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005515 } else if (set->context_op && (node->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && (node != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005516 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005517 }
5518
5519 /* name check */
Michal Vasko61ac2f62020-05-25 12:39:51 +02005520 if (node_name && strcmp(node_name, "*") && (node->name != node_name)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005521 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005522 }
5523
5524 /* match */
5525 return LY_SUCCESS;
5526}
5527
5528/**
Michal Vaskod3678892020-05-21 10:06:58 +02005529 * @brief Move context @p set to a node. Result is LYXP_SET_NODE_SET (or LYXP_SET_EMPTY). Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005530 *
5531 * @param[in,out] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005532 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005533 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01005534 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005535 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5536 */
5537static LY_ERR
Michal Vaskocdad7122020-11-09 21:04:44 +01005538moveto_node(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005539{
Michal Vaskof03ed032020-03-04 13:31:44 +01005540 uint32_t i;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005541 const struct lyd_node *siblings, *sub, *ctx_node;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005542 LY_ERR rc;
5543
aPiecek8b0cc152021-05-31 16:40:31 +02005544 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005545 return LY_SUCCESS;
5546 }
5547
5548 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005549 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005550 return LY_EVALID;
5551 }
5552
Michal Vasko03ff5a72019-09-11 13:49:33 +02005553 for (i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02005554 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005555
5556 if ((set->val.nodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.nodes[i].type == LYXP_NODE_ROOT)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005557 assert(!set->val.nodes[i].node);
Michal Vaskod3678892020-05-21 10:06:58 +02005558
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005559 /* search in all the trees */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005560 ctx_node = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02005561 siblings = set->tree;
Michal Vasko5bfd4be2020-06-23 13:26:19 +02005562 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005563 /* search in children */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005564 ctx_node = set->val.nodes[i].node;
5565 siblings = lyd_child(ctx_node);
Michal Vaskod3678892020-05-21 10:06:58 +02005566 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005567
Michal Vaskod3678892020-05-21 10:06:58 +02005568 for (sub = siblings; sub; sub = sub->next) {
Michal Vaskocdad7122020-11-09 21:04:44 +01005569 rc = moveto_node_check(sub, ctx_node, set, ncname, moveto_mod, options);
Michal Vaskod3678892020-05-21 10:06:58 +02005570 if (rc == LY_SUCCESS) {
5571 if (!replaced) {
5572 set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i);
5573 replaced = 1;
5574 } else {
5575 set_insert_node(set, sub, 0, LYXP_NODE_ELEM, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005576 }
Michal Vaskod3678892020-05-21 10:06:58 +02005577 ++i;
5578 } else if (rc == LY_EINCOMPLETE) {
5579 return rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005580 }
5581 }
5582
5583 if (!replaced) {
5584 /* no match */
5585 set_remove_node(set, i);
5586 }
5587 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005588
5589 return LY_SUCCESS;
5590}
5591
5592/**
Michal Vaskod3678892020-05-21 10:06:58 +02005593 * @brief Move context @p set to a node using hashes. Result is LYXP_SET_NODE_SET (or LYXP_SET_EMPTY).
5594 * Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005595 *
5596 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005597 * @param[in] scnode Matching node schema.
Michal Vasko004d3152020-06-11 19:59:22 +02005598 * @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 +01005599 * @param[in] options XPath options.
Michal Vaskod3678892020-05-21 10:06:58 +02005600 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5601 */
5602static LY_ERR
Michal Vaskocdad7122020-11-09 21:04:44 +01005603moveto_node_hash(struct lyxp_set *set, const struct lysc_node *scnode, const struct ly_path_predicate *predicates,
5604 uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02005605{
Michal Vasko004d3152020-06-11 19:59:22 +02005606 LY_ERR ret = LY_SUCCESS;
Michal Vaskod3678892020-05-21 10:06:58 +02005607 uint32_t i;
Michal Vaskod3678892020-05-21 10:06:58 +02005608 const struct lyd_node *siblings;
Michal Vasko004d3152020-06-11 19:59:22 +02005609 struct lyd_node *sub, *inst = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02005610
Michal Vasko004d3152020-06-11 19:59:22 +02005611 assert(scnode && (!(scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)) || predicates));
Michal Vaskod3678892020-05-21 10:06:58 +02005612
aPiecek8b0cc152021-05-31 16:40:31 +02005613 if (options & LYXP_SKIP_EXPR) {
Michal Vasko004d3152020-06-11 19:59:22 +02005614 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005615 }
5616
5617 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005618 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko004d3152020-06-11 19:59:22 +02005619 ret = LY_EVALID;
5620 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005621 }
5622
5623 /* context check for all the nodes since we have the schema node */
5624 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) {
5625 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02005626 goto cleanup;
Michal Vasko69730152020-10-09 16:30:07 +02005627 } else if (set->context_op && (scnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) &&
5628 (scnode != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005629 lyxp_set_free_content(set);
5630 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +02005631 }
5632
5633 /* create specific data instance if needed */
5634 if (scnode->nodetype == LYS_LIST) {
5635 LY_CHECK_GOTO(ret = lyd_create_list(scnode, predicates, &inst), cleanup);
5636 } else if (scnode->nodetype == LYS_LEAFLIST) {
5637 LY_CHECK_GOTO(ret = lyd_create_term2(scnode, &predicates[0].value, &inst), cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02005638 }
5639
5640 for (i = 0; i < set->used; ) {
Michal Vaskod3678892020-05-21 10:06:58 +02005641 siblings = NULL;
5642
5643 if ((set->val.nodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.nodes[i].type == LYXP_NODE_ROOT)) {
5644 assert(!set->val.nodes[i].node);
5645
5646 /* search in all the trees */
5647 siblings = set->tree;
5648 } else if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
5649 /* search in children */
Radek Krejcia1c1e542020-09-29 16:06:52 +02005650 siblings = lyd_child(set->val.nodes[i].node);
Michal Vaskod3678892020-05-21 10:06:58 +02005651 }
5652
5653 /* find the node using hashes */
Michal Vasko004d3152020-06-11 19:59:22 +02005654 if (inst) {
5655 lyd_find_sibling_first(siblings, inst, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02005656 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02005657 lyd_find_sibling_val(siblings, scnode, NULL, 0, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02005658 }
5659
5660 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01005661 if (!(options & LYXP_IGNORE_WHEN) && sub && lysc_has_when(sub->schema) && !(sub->flags & LYD_WHEN_TRUE)) {
Michal Vasko004d3152020-06-11 19:59:22 +02005662 ret = LY_EINCOMPLETE;
5663 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005664 }
5665
5666 if (sub) {
5667 /* pos filled later */
Michal Vaskocb1b7c02020-07-03 13:38:12 +02005668 set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i);
Michal Vaskod3678892020-05-21 10:06:58 +02005669 ++i;
Michal Vaskocb1b7c02020-07-03 13:38:12 +02005670 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005671 /* no match */
5672 set_remove_node(set, i);
5673 }
5674 }
5675
Michal Vasko004d3152020-06-11 19:59:22 +02005676cleanup:
5677 lyd_free_tree(inst);
5678 return ret;
Michal Vaskod3678892020-05-21 10:06:58 +02005679}
5680
5681/**
5682 * @brief Move context @p set to a schema node. Result is LYXP_SET_SCNODE_SET (or LYXP_SET_EMPTY).
5683 *
5684 * @param[in,out] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005685 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005686 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005687 * @param[in] options XPath options.
5688 * @return LY_ERR
5689 */
5690static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005691moveto_scnode(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005692{
Radek Krejci857189e2020-09-01 13:26:36 +02005693 ly_bool temp_ctx = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005694 uint32_t getnext_opts;
5695 uint32_t orig_used, i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005696 uint32_t mod_idx;
Michal Vasko519fd602020-05-26 12:17:39 +02005697 const struct lysc_node *iter, *start_parent;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005698 const struct lys_module *mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005699
aPiecek8b0cc152021-05-31 16:40:31 +02005700 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005701 return LY_SUCCESS;
5702 }
5703
5704 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005705 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005706 return LY_EVALID;
5707 }
5708
Michal Vaskocafad9d2019-11-07 15:20:03 +01005709 /* getnext opts */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01005710 getnext_opts = 0;
Michal Vaskocafad9d2019-11-07 15:20:03 +01005711 if (options & LYXP_SCNODE_OUTPUT) {
5712 getnext_opts |= LYS_GETNEXT_OUTPUT;
5713 }
5714
Michal Vasko03ff5a72019-09-11 13:49:33 +02005715 orig_used = set->used;
5716 for (i = 0; i < orig_used; ++i) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005717 uint32_t idx;
5718
Radek Krejcif13b87b2020-12-01 22:02:17 +01005719 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
5720 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01005721 continue;
5722 }
5723
5724 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01005725 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vaskoec4df482019-12-16 10:02:18 +01005726 } else {
Michal Vasko1a09b212021-05-06 13:00:10 +02005727 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005728 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005729
5730 start_parent = set->val.scnodes[i].scnode;
5731
5732 if ((set->val.scnodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.scnodes[i].type == LYXP_NODE_ROOT)) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005733 /* it can actually be in any module, it's all <running>, and even if it's moveto_mod (if set),
Michal Vasko9e9f26d2020-10-12 16:31:33 +02005734 * it can be in a top-level augment (the root node itself is useless in this case) */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005735 mod_idx = 0;
Michal Vasko9e9f26d2020-10-12 16:31:33 +02005736 while ((mod = (struct lys_module *)ly_ctx_get_module_iter(set->ctx, &mod_idx))) {
Michal Vasko519fd602020-05-26 12:17:39 +02005737 iter = NULL;
Michal Vasko509de4d2019-12-10 14:51:30 +01005738 /* module may not be implemented */
Michal Vasko519fd602020-05-26 12:17:39 +02005739 while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005740 if (!moveto_scnode_check(iter, NULL, set, ncname, moveto_mod)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005741 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, &idx));
5742
Michal Vasko03ff5a72019-09-11 13:49:33 +02005743 /* we need to prevent these nodes from being considered in this moveto */
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005744 if ((idx < orig_used) && (idx > i)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005745 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005746 temp_ctx = 1;
5747 }
5748 }
5749 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005750 }
5751
Michal Vasko519fd602020-05-26 12:17:39 +02005752 } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
5753 iter = NULL;
5754 while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005755 if (!moveto_scnode_check(iter, start_parent, set, ncname, moveto_mod)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005756 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, &idx));
5757
5758 if ((idx < orig_used) && (idx > i)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005759 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005760 temp_ctx = 1;
5761 }
5762 }
5763 }
5764 }
5765 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005766
5767 /* correct temporary in_ctx values */
5768 if (temp_ctx) {
5769 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005770 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_NEW_CTX) {
5771 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005772 }
5773 }
5774 }
5775
5776 return LY_SUCCESS;
5777}
5778
5779/**
Michal Vaskod3678892020-05-21 10:06:58 +02005780 * @brief Move context @p set to a node and all its descendants. Result is LYXP_SET_NODE_SET (or LYXP_SET_EMPTY).
Michal Vasko03ff5a72019-09-11 13:49:33 +02005781 * Context position aware.
5782 *
5783 * @param[in] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005784 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005785 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01005786 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005787 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5788 */
5789static LY_ERR
Michal Vaskocdad7122020-11-09 21:04:44 +01005790moveto_node_alldesc(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005791{
5792 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005793 const struct lyd_node *next, *elem, *start;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005794 struct lyxp_set ret_set;
5795 LY_ERR rc;
5796
aPiecek8b0cc152021-05-31 16:40:31 +02005797 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005798 return LY_SUCCESS;
5799 }
5800
5801 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005802 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005803 return LY_EVALID;
5804 }
5805
Michal Vasko9f96a052020-03-10 09:41:45 +01005806 /* replace the original nodes (and throws away all text and meta nodes, root is replaced by a child) */
Michal Vaskocdad7122020-11-09 21:04:44 +01005807 rc = moveto_node(set, NULL, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005808 LY_CHECK_RET(rc);
5809
Michal Vasko6346ece2019-09-24 13:12:53 +02005810 /* this loop traverses all the nodes in the set and adds/keeps only those that match qname */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005811 set_init(&ret_set, set);
5812 for (i = 0; i < set->used; ++i) {
5813
5814 /* TREE DFS */
5815 start = set->val.nodes[i].node;
5816 for (elem = next = start; elem; elem = next) {
Michal Vaskocdad7122020-11-09 21:04:44 +01005817 rc = moveto_node_check(elem, start, set, ncname, moveto_mod, options);
Michal Vasko6346ece2019-09-24 13:12:53 +02005818 if (!rc) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005819 /* add matching node into result set */
5820 set_insert_node(&ret_set, elem, 0, LYXP_NODE_ELEM, ret_set.used);
5821 if (set_dup_node_check(set, elem, LYXP_NODE_ELEM, i)) {
5822 /* the node is a duplicate, we'll process it later in the set */
5823 goto skip_children;
5824 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005825 } else if (rc == LY_EINCOMPLETE) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005826 return rc;
5827 } else if (rc == LY_EINVAL) {
5828 goto skip_children;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005829 }
5830
5831 /* TREE DFS NEXT ELEM */
5832 /* select element for the next run - children first */
Radek Krejcia1c1e542020-09-29 16:06:52 +02005833 next = lyd_child(elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005834 if (!next) {
5835skip_children:
5836 /* no children, so try siblings, but only if it's not the start,
5837 * that is considered to be the root and it's siblings are not traversed */
5838 if (elem != start) {
5839 next = elem->next;
5840 } else {
5841 break;
5842 }
5843 }
5844 while (!next) {
5845 /* no siblings, go back through the parents */
Michal Vasko9e685082021-01-29 14:49:09 +01005846 if (lyd_parent(elem) == start) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005847 /* we are done, no next element to process */
5848 break;
5849 }
5850 /* parent is already processed, go to its sibling */
Michal Vasko9e685082021-01-29 14:49:09 +01005851 elem = lyd_parent(elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005852 next = elem->next;
5853 }
5854 }
5855 }
5856
5857 /* make the temporary set the current one */
5858 ret_set.ctx_pos = set->ctx_pos;
5859 ret_set.ctx_size = set->ctx_size;
Michal Vaskod3678892020-05-21 10:06:58 +02005860 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005861 memcpy(set, &ret_set, sizeof *set);
5862
5863 return LY_SUCCESS;
5864}
5865
5866/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005867 * @brief Move context @p set to a schema node and all its descendants. Result is LYXP_SET_NODE_SET.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005868 *
5869 * @param[in] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005870 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005871 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005872 * @param[in] options XPath options.
5873 * @return LY_ERR
5874 */
5875static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005876moveto_scnode_alldesc(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005877{
Radek Krejci1deb5be2020-08-26 16:43:36 +02005878 uint32_t i, orig_used;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005879 const struct lysc_node *next, *elem, *start;
Michal Vasko6346ece2019-09-24 13:12:53 +02005880 LY_ERR rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005881
aPiecek8b0cc152021-05-31 16:40:31 +02005882 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005883 return LY_SUCCESS;
5884 }
5885
5886 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005887 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005888 return LY_EVALID;
5889 }
5890
Michal Vasko03ff5a72019-09-11 13:49:33 +02005891 orig_used = set->used;
5892 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005893 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
5894 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01005895 continue;
5896 }
5897
5898 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01005899 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vaskoec4df482019-12-16 10:02:18 +01005900 } else {
Michal Vasko1a09b212021-05-06 13:00:10 +02005901 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005902 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005903
5904 /* TREE DFS */
5905 start = set->val.scnodes[i].scnode;
5906 for (elem = next = start; elem; elem = next) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005907 if ((elem == start) || (elem->nodetype & (LYS_CHOICE | LYS_CASE))) {
5908 /* schema-only nodes, skip root */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005909 goto next_iter;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005910 }
5911
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005912 rc = moveto_scnode_check(elem, start, set, ncname, moveto_mod);
Michal Vasko6346ece2019-09-24 13:12:53 +02005913 if (!rc) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005914 uint32_t idx;
5915
5916 if (lyxp_set_scnode_contains(set, elem, LYXP_NODE_ELEM, i, &idx)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005917 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005918 if ((uint32_t)idx > i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005919 /* we will process it later in the set */
5920 goto skip_children;
5921 }
5922 } else {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005923 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, elem, LYXP_NODE_ELEM, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005924 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005925 } else if (rc == LY_EINVAL) {
5926 goto skip_children;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005927 }
5928
5929next_iter:
5930 /* TREE DFS NEXT ELEM */
5931 /* select element for the next run - children first */
Michal Vasko544e58a2021-01-28 14:33:41 +01005932 next = lysc_node_child(elem);
5933 if (next && (next->nodetype == LYS_INPUT) && (options & LYXP_SCNODE_OUTPUT)) {
5934 next = next->next;
5935 } else if (next && (next->nodetype == LYS_OUTPUT) && !(options & LYXP_SCNODE_OUTPUT)) {
5936 next = next->next;
5937 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005938 if (!next) {
5939skip_children:
5940 /* no children, so try siblings, but only if it's not the start,
5941 * that is considered to be the root and it's siblings are not traversed */
5942 if (elem != start) {
5943 next = elem->next;
5944 } else {
5945 break;
5946 }
5947 }
5948 while (!next) {
5949 /* no siblings, go back through the parents */
5950 if (elem->parent == start) {
5951 /* we are done, no next element to process */
5952 break;
5953 }
5954 /* parent is already processed, go to its sibling */
5955 elem = elem->parent;
5956 next = elem->next;
5957 }
5958 }
5959 }
5960
5961 return LY_SUCCESS;
5962}
5963
5964/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005965 * @brief Move context @p set to an attribute. Result is LYXP_SET_NODE_SET.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005966 * Indirectly context position aware.
5967 *
5968 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005969 * @param[in] mod Matching metadata module, NULL for any.
5970 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
aPiecek8b0cc152021-05-31 16:40:31 +02005971 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005972 * @return LY_ERR
5973 */
5974static LY_ERR
aPiecek8b0cc152021-05-31 16:40:31 +02005975moveto_attr(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005976{
Michal Vasko9f96a052020-03-10 09:41:45 +01005977 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005978
aPiecek8b0cc152021-05-31 16:40:31 +02005979 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005980 return LY_SUCCESS;
5981 }
5982
5983 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005984 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005985 return LY_EVALID;
5986 }
5987
Radek Krejci1deb5be2020-08-26 16:43:36 +02005988 for (uint32_t i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02005989 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005990
5991 /* only attributes of an elem (not dummy) can be in the result, skip all the rest;
5992 * our attributes are always qualified */
Michal Vasko5c4e5892019-11-14 12:31:38 +01005993 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01005994 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005995
5996 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02005997 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005998 continue;
5999 }
6000
Michal Vaskod3678892020-05-21 10:06:58 +02006001 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006002 /* match */
6003 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01006004 set->val.meta[i].meta = sub;
6005 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006006 /* pos does not change */
6007 replaced = 1;
6008 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01006009 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 +02006010 }
6011 ++i;
6012 }
6013 }
6014 }
6015
6016 if (!replaced) {
6017 /* no match */
6018 set_remove_node(set, i);
6019 }
6020 }
6021
6022 return LY_SUCCESS;
6023}
6024
6025/**
6026 * @brief Move context @p set1 to union with @p set2. @p set2 is emptied afterwards.
Michal Vasko61ac2f62020-05-25 12:39:51 +02006027 * Result is LYXP_SET_NODE_SET. Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006028 *
6029 * @param[in,out] set1 Set to use for the result.
6030 * @param[in] set2 Set that is copied to @p set1.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006031 * @return LY_ERR
6032 */
6033static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006034moveto_union(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006035{
6036 LY_ERR rc;
6037
Michal Vaskod3678892020-05-21 10:06:58 +02006038 if ((set1->type != LYXP_SET_NODE_SET) || (set2->type != LYXP_SET_NODE_SET)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006039 LOGVAL(set1->ctx, LY_VCODE_XP_INOP_2, "union", print_set_type(set1), print_set_type(set2));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006040 return LY_EVALID;
6041 }
6042
6043 /* set2 is empty or both set1 and set2 */
Michal Vaskod3678892020-05-21 10:06:58 +02006044 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006045 return LY_SUCCESS;
6046 }
6047
Michal Vaskod3678892020-05-21 10:06:58 +02006048 if (!set1->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006049 memcpy(set1, set2, sizeof *set1);
6050 /* dynamic memory belongs to set1 now, do not free */
Michal Vaskod3678892020-05-21 10:06:58 +02006051 memset(set2, 0, sizeof *set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006052 return LY_SUCCESS;
6053 }
6054
6055 /* we assume sets are sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006056 assert(!set_sort(set1) && !set_sort(set2));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006057
6058 /* sort, remove duplicates */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006059 rc = set_sorted_merge(set1, set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006060 LY_CHECK_RET(rc);
6061
6062 /* final set must be sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006063 assert(!set_sort(set1));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006064
6065 return LY_SUCCESS;
6066}
6067
6068/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02006069 * @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 +02006070 * Context position aware.
6071 *
6072 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02006073 * @param[in] mod Matching metadata module, NULL for any.
6074 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01006075 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006076 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6077 */
6078static int
Michal Vaskocdad7122020-11-09 21:04:44 +01006079moveto_attr_alldesc(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006080{
Michal Vasko9f96a052020-03-10 09:41:45 +01006081 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006082 struct lyxp_set *set_all_desc = NULL;
6083 LY_ERR rc;
6084
aPiecek8b0cc152021-05-31 16:40:31 +02006085 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006086 return LY_SUCCESS;
6087 }
6088
6089 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006090 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006091 return LY_EVALID;
6092 }
6093
Michal Vasko03ff5a72019-09-11 13:49:33 +02006094 /* can be optimized similarly to moveto_node_alldesc() and save considerable amount of memory,
6095 * but it likely won't be used much, so it's a waste of time */
6096 /* copy the context */
6097 set_all_desc = set_copy(set);
6098 /* get all descendant nodes (the original context nodes are removed) */
Michal Vaskocdad7122020-11-09 21:04:44 +01006099 rc = moveto_node_alldesc(set_all_desc, NULL, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006100 if (rc != LY_SUCCESS) {
6101 lyxp_set_free(set_all_desc);
6102 return rc;
6103 }
6104 /* prepend the original context nodes */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006105 rc = moveto_union(set, set_all_desc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006106 if (rc != LY_SUCCESS) {
6107 lyxp_set_free(set_all_desc);
6108 return rc;
6109 }
6110 lyxp_set_free(set_all_desc);
6111
Radek Krejci1deb5be2020-08-26 16:43:36 +02006112 for (uint32_t i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02006113 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006114
6115 /* only attributes of an elem can be in the result, skip all the rest,
6116 * we have all attributes qualified in lyd tree */
6117 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01006118 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006119 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02006120 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006121 continue;
6122 }
6123
Michal Vaskod3678892020-05-21 10:06:58 +02006124 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006125 /* match */
6126 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01006127 set->val.meta[i].meta = sub;
6128 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006129 /* pos does not change */
6130 replaced = 1;
6131 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01006132 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 +02006133 }
6134 ++i;
6135 }
6136 }
6137 }
6138
6139 if (!replaced) {
6140 /* no match */
6141 set_remove_node(set, i);
6142 }
6143 }
6144
6145 return LY_SUCCESS;
6146}
6147
6148/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02006149 * @brief Move context @p set to self and al chilren, recursively. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET.
6150 * Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006151 *
6152 * @param[in] parent Current parent.
6153 * @param[in] parent_pos Position of @p parent.
6154 * @param[in] parent_type Node type of @p parent.
6155 * @param[in,out] to_set Set to use.
6156 * @param[in] dup_check_set Set for checking duplicities.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006157 * @param[in] options XPath options.
6158 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6159 */
6160static LY_ERR
6161moveto_self_add_children_r(const struct lyd_node *parent, uint32_t parent_pos, enum lyxp_node_type parent_type,
Radek Krejci1deb5be2020-08-26 16:43:36 +02006162 struct lyxp_set *to_set, const struct lyxp_set *dup_check_set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006163{
Michal Vasko61ac2f62020-05-25 12:39:51 +02006164 const struct lyd_node *iter, *first;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006165 LY_ERR rc;
6166
6167 switch (parent_type) {
6168 case LYXP_NODE_ROOT:
6169 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko61ac2f62020-05-25 12:39:51 +02006170 assert(!parent);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006171
Michal Vasko61ac2f62020-05-25 12:39:51 +02006172 /* add all top-level nodes as elements */
6173 first = to_set->tree;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006174 break;
6175 case LYXP_NODE_ELEM:
Michal Vasko61ac2f62020-05-25 12:39:51 +02006176 /* add just the text node of this term element node */
6177 if (parent->schema->nodetype & (LYD_NODE_TERM | LYD_NODE_ANY)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006178 if (!set_dup_node_check(dup_check_set, parent, LYXP_NODE_TEXT, -1)) {
6179 set_insert_node(to_set, parent, parent_pos, LYXP_NODE_TEXT, to_set->used);
6180 }
Michal Vasko61ac2f62020-05-25 12:39:51 +02006181 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006182 }
Michal Vasko61ac2f62020-05-25 12:39:51 +02006183
6184 /* add all the children of this node */
Radek Krejcia1c1e542020-09-29 16:06:52 +02006185 first = lyd_child(parent);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006186 break;
6187 default:
6188 LOGINT_RET(parent->schema->module->ctx);
6189 }
6190
Michal Vasko61ac2f62020-05-25 12:39:51 +02006191 /* add all top-level nodes as elements */
6192 LY_LIST_FOR(first, iter) {
6193 /* context check */
6194 if ((parent_type == LYXP_NODE_ROOT_CONFIG) && (iter->schema->flags & LYS_CONFIG_R)) {
6195 continue;
6196 }
6197
6198 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01006199 if (!(options & LYXP_IGNORE_WHEN) && lysc_has_when(iter->schema) && !(iter->flags & LYD_WHEN_TRUE)) {
Michal Vasko61ac2f62020-05-25 12:39:51 +02006200 return LY_EINCOMPLETE;
6201 }
6202
6203 if (!set_dup_node_check(dup_check_set, iter, LYXP_NODE_ELEM, -1)) {
6204 set_insert_node(to_set, iter, 0, LYXP_NODE_ELEM, to_set->used);
6205
6206 /* also add all the children of this node, recursively */
6207 rc = moveto_self_add_children_r(iter, 0, LYXP_NODE_ELEM, to_set, dup_check_set, options);
6208 LY_CHECK_RET(rc);
6209 }
6210 }
6211
Michal Vasko03ff5a72019-09-11 13:49:33 +02006212 return LY_SUCCESS;
6213}
6214
6215/**
6216 * @brief Move context @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET
6217 * (or LYXP_SET_EMPTY). Context position aware.
6218 *
6219 * @param[in,out] set Set to use.
6220 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6221 * @param[in] options XPath options.
6222 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6223 */
6224static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006225moveto_self(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006226{
Michal Vasko03ff5a72019-09-11 13:49:33 +02006227 struct lyxp_set ret_set;
6228 LY_ERR rc;
6229
aPiecek8b0cc152021-05-31 16:40:31 +02006230 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006231 return LY_SUCCESS;
6232 }
6233
6234 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006235 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006236 return LY_EVALID;
6237 }
6238
6239 /* nothing to do */
6240 if (!all_desc) {
6241 return LY_SUCCESS;
6242 }
6243
Michal Vasko03ff5a72019-09-11 13:49:33 +02006244 /* add all the children, they get added recursively */
6245 set_init(&ret_set, set);
Radek Krejci1deb5be2020-08-26 16:43:36 +02006246 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006247 /* copy the current node to tmp */
6248 set_insert_node(&ret_set, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, ret_set.used);
6249
6250 /* do not touch attributes and text nodes */
Michal Vasko9f96a052020-03-10 09:41:45 +01006251 if ((set->val.nodes[i].type == LYXP_NODE_TEXT) || (set->val.nodes[i].type == LYXP_NODE_META)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006252 continue;
6253 }
6254
Michal Vasko03ff5a72019-09-11 13:49:33 +02006255 /* add all the children */
6256 rc = moveto_self_add_children_r(set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, &ret_set,
Michal Vasko69730152020-10-09 16:30:07 +02006257 set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006258 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006259 lyxp_set_free_content(&ret_set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006260 return rc;
6261 }
6262 }
6263
6264 /* use the temporary set as the current one */
6265 ret_set.ctx_pos = set->ctx_pos;
6266 ret_set.ctx_size = set->ctx_size;
Michal Vaskod3678892020-05-21 10:06:58 +02006267 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006268 memcpy(set, &ret_set, sizeof *set);
6269
6270 return LY_SUCCESS;
6271}
6272
6273/**
6274 * @brief Move context schema @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_SCNODE_SET
6275 * (or LYXP_SET_EMPTY).
6276 *
6277 * @param[in,out] set Set to use.
6278 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6279 * @param[in] options XPath options.
6280 * @return LY_ERR
6281 */
6282static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006283moveto_scnode_self(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006284{
Radek Krejci1deb5be2020-08-26 16:43:36 +02006285 uint32_t getnext_opts;
6286 uint32_t mod_idx;
Michal Vasko519fd602020-05-26 12:17:39 +02006287 const struct lysc_node *iter, *start_parent;
6288 const struct lys_module *mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006289
aPiecek8b0cc152021-05-31 16:40:31 +02006290 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006291 return LY_SUCCESS;
6292 }
6293
6294 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006295 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006296 return LY_EVALID;
6297 }
6298
Michal Vasko519fd602020-05-26 12:17:39 +02006299 /* getnext opts */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01006300 getnext_opts = 0;
Michal Vasko519fd602020-05-26 12:17:39 +02006301 if (options & LYXP_SCNODE_OUTPUT) {
6302 getnext_opts |= LYS_GETNEXT_OUTPUT;
6303 }
6304
6305 /* add all the children, recursively as they are being added into the same set */
Radek Krejci1deb5be2020-08-26 16:43:36 +02006306 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vaskoe657f962020-12-10 12:19:48 +01006307 if (!all_desc) {
6308 /* traverse the start node */
6309 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START) {
6310 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
6311 }
6312 continue;
6313 }
6314
Radek Krejcif13b87b2020-12-01 22:02:17 +01006315 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
6316 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006317 continue;
6318 }
6319
Michal Vasko519fd602020-05-26 12:17:39 +02006320 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01006321 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vasko519fd602020-05-26 12:17:39 +02006322 } else {
Michal Vasko1a09b212021-05-06 13:00:10 +02006323 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006324 }
6325
Michal Vasko519fd602020-05-26 12:17:39 +02006326 start_parent = set->val.scnodes[i].scnode;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006327
Michal Vasko519fd602020-05-26 12:17:39 +02006328 if ((set->val.scnodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.scnodes[i].type == LYXP_NODE_ROOT)) {
6329 /* it can actually be in any module, it's all <running> */
6330 mod_idx = 0;
6331 while ((mod = (struct lys_module *)ly_ctx_get_module_iter(set->ctx, &mod_idx))) {
6332 iter = NULL;
6333 /* module may not be implemented */
6334 while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) {
6335 /* context check */
6336 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) {
6337 continue;
6338 }
6339
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006340 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, NULL));
Michal Vasko519fd602020-05-26 12:17:39 +02006341 /* throw away the insert index, we want to consider that node again, recursively */
6342 }
6343 }
6344
6345 } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
6346 iter = NULL;
6347 while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006348 /* context check */
Michal Vasko519fd602020-05-26 12:17:39 +02006349 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006350 continue;
6351 }
6352
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006353 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006354 }
6355 }
6356 }
6357
6358 return LY_SUCCESS;
6359}
6360
6361/**
6362 * @brief Move context @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_NODE_SET
6363 * (or LYXP_SET_EMPTY). Context position aware.
6364 *
6365 * @param[in] set Set to use.
6366 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6367 * @param[in] options XPath options.
6368 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6369 */
6370static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006371moveto_parent(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006372{
6373 LY_ERR rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006374 struct lyd_node *node, *new_node;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006375 enum lyxp_node_type new_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006376
aPiecek8b0cc152021-05-31 16:40:31 +02006377 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006378 return LY_SUCCESS;
6379 }
6380
6381 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006382 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006383 return LY_EVALID;
6384 }
6385
6386 if (all_desc) {
6387 /* <path>//.. == <path>//./.. */
6388 rc = moveto_self(set, 1, options);
6389 LY_CHECK_RET(rc);
6390 }
6391
Radek Krejci1deb5be2020-08-26 16:43:36 +02006392 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006393 node = set->val.nodes[i].node;
6394
6395 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9e685082021-01-29 14:49:09 +01006396 new_node = lyd_parent(node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006397 } else if (set->val.nodes[i].type == LYXP_NODE_TEXT) {
6398 new_node = node;
Michal Vasko9f96a052020-03-10 09:41:45 +01006399 } else if (set->val.nodes[i].type == LYXP_NODE_META) {
6400 new_node = set->val.meta[i].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006401 if (!new_node) {
6402 LOGINT_RET(set->ctx);
6403 }
6404 } else {
6405 /* root does not have a parent */
Michal Vasko2caefc12019-11-14 16:07:56 +01006406 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006407 continue;
6408 }
6409
Michal Vaskoa1424542019-11-14 16:08:52 +01006410 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01006411 if (!(options & LYXP_IGNORE_WHEN) && new_node && lysc_has_when(new_node->schema) && !(new_node->flags & LYD_WHEN_TRUE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006412 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01006413 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006414
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006415 if (!new_node) {
Radek Krejcif6a11002020-08-21 13:29:07 +02006416 /* node already there can also be the root */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006417 new_type = set->root_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006418
Michal Vasko03ff5a72019-09-11 13:49:33 +02006419 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02006420 /* node has a standard parent (it can equal the root, it's not the root yet since they are fake) */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006421 new_type = LYXP_NODE_ELEM;
6422 }
6423
Michal Vasko03ff5a72019-09-11 13:49:33 +02006424 if (set_dup_node_check(set, new_node, new_type, -1)) {
Michal Vasko2caefc12019-11-14 16:07:56 +01006425 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006426 } else {
6427 set_replace_node(set, new_node, 0, new_type, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006428 }
6429 }
6430
Michal Vasko2caefc12019-11-14 16:07:56 +01006431 set_remove_nodes_none(set);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006432 assert(!set_sort(set) && !set_sorted_dup_node_clean(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006433
6434 return LY_SUCCESS;
6435}
6436
6437/**
6438 * @brief Move context schema @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_SCNODE_SET
6439 * (or LYXP_SET_EMPTY).
6440 *
6441 * @param[in] set Set to use.
6442 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6443 * @param[in] options XPath options.
6444 * @return LY_ERR
6445 */
6446static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006447moveto_scnode_parent(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006448{
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006449 uint32_t i, orig_used, idx;
Radek Krejci857189e2020-09-01 13:26:36 +02006450 ly_bool temp_ctx = 0;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006451 const struct lysc_node *node, *new_node;
6452 enum lyxp_node_type new_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006453
aPiecek8b0cc152021-05-31 16:40:31 +02006454 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006455 return LY_SUCCESS;
6456 }
6457
6458 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006459 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006460 return LY_EVALID;
6461 }
6462
6463 if (all_desc) {
6464 /* <path>//.. == <path>//./.. */
Radek Krejci1deb5be2020-08-26 16:43:36 +02006465 LY_CHECK_RET(moveto_scnode_self(set, 1, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006466 }
6467
Michal Vasko03ff5a72019-09-11 13:49:33 +02006468 orig_used = set->used;
6469 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006470 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
6471 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006472 continue;
6473 }
6474
6475 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01006476 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vaskoec4df482019-12-16 10:02:18 +01006477 } else {
Michal Vasko1a09b212021-05-06 13:00:10 +02006478 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006479 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006480
6481 node = set->val.scnodes[i].scnode;
6482
6483 if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
Michal Vaskod3678892020-05-21 10:06:58 +02006484 new_node = lysc_data_parent(node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006485 } else {
6486 /* root does not have a parent */
6487 continue;
6488 }
6489
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006490 if (!new_node) {
Radek Krejcif6a11002020-08-21 13:29:07 +02006491 /* node has no parent */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006492 new_type = set->root_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006493
Michal Vasko03ff5a72019-09-11 13:49:33 +02006494 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02006495 /* node has a standard parent (it can equal the root, it's not the root yet since they are fake) */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006496 new_type = LYXP_NODE_ELEM;
6497 }
6498
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006499 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, new_node, new_type, &idx));
6500 if ((idx < orig_used) && (idx > i)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006501 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006502 temp_ctx = 1;
6503 }
6504 }
6505
6506 if (temp_ctx) {
6507 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006508 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_NEW_CTX) {
6509 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006510 }
6511 }
6512 }
6513
6514 return LY_SUCCESS;
6515}
6516
6517/**
6518 * @brief Move context @p set to the result of a comparison. Handles '=', '!=', '<=', '<', '>=', or '>'.
6519 * Result is LYXP_SET_BOOLEAN. Indirectly context position aware.
6520 *
6521 * @param[in,out] set1 Set to use for the result.
6522 * @param[in] set2 Set acting as the second operand for @p op.
6523 * @param[in] op Comparison operator to process.
6524 * @param[in] options XPath options.
6525 * @return LY_ERR
6526 */
6527static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02006528moveto_op_comp(struct lyxp_set *set1, struct lyxp_set *set2, const char *op, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006529{
6530 /*
6531 * NODE SET + NODE SET = NODE SET + STRING /(1 NODE SET) 2 STRING
6532 * NODE SET + STRING = STRING + STRING /1 STRING (2 STRING)
6533 * NODE SET + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6534 * NODE SET + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6535 * STRING + NODE SET = STRING + STRING /(1 STRING) 2 STRING
6536 * NUMBER + NODE SET = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6537 * BOOLEAN + NODE SET = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6538 *
6539 * '=' or '!='
6540 * BOOLEAN + BOOLEAN
6541 * BOOLEAN + STRING = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6542 * BOOLEAN + NUMBER = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6543 * STRING + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6544 * NUMBER + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6545 * NUMBER + NUMBER
6546 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6547 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6548 * STRING + STRING
6549 *
6550 * '<=', '<', '>=', '>'
6551 * NUMBER + NUMBER
6552 * BOOLEAN + BOOLEAN = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6553 * BOOLEAN + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6554 * BOOLEAN + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6555 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6556 * STRING + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6557 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6558 * NUMBER + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6559 * STRING + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6560 */
6561 struct lyxp_set iter1, iter2;
6562 int result;
6563 int64_t i;
6564 LY_ERR rc;
6565
Michal Vasko004d3152020-06-11 19:59:22 +02006566 memset(&iter1, 0, sizeof iter1);
6567 memset(&iter2, 0, sizeof iter2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006568
6569 /* iterative evaluation with node-sets */
6570 if ((set1->type == LYXP_SET_NODE_SET) || (set2->type == LYXP_SET_NODE_SET)) {
6571 if (set1->type == LYXP_SET_NODE_SET) {
6572 for (i = 0; i < set1->used; ++i) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006573 /* cast set1 */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006574 switch (set2->type) {
6575 case LYXP_SET_NUMBER:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006576 rc = set_comp_cast(&iter1, set1, LYXP_SET_NUMBER, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006577 break;
6578 case LYXP_SET_BOOLEAN:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006579 rc = set_comp_cast(&iter1, set1, LYXP_SET_BOOLEAN, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006580 break;
6581 default:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006582 rc = set_comp_cast(&iter1, set1, LYXP_SET_STRING, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006583 break;
6584 }
6585 LY_CHECK_RET(rc);
6586
Michal Vasko4c7763f2020-07-27 17:40:37 +02006587 /* canonize set2 */
6588 LY_CHECK_ERR_RET(rc = set_comp_canonize(&iter2, set2, &set1->val.nodes[i]), lyxp_set_free_content(&iter1), rc);
6589
6590 /* compare recursively */
6591 rc = moveto_op_comp(&iter1, &iter2, op, options);
6592 lyxp_set_free_content(&iter2);
6593 LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006594
6595 /* lazy evaluation until true */
Michal Vasko004d3152020-06-11 19:59:22 +02006596 if (iter1.val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006597 set_fill_boolean(set1, 1);
6598 return LY_SUCCESS;
6599 }
6600 }
6601 } else {
6602 for (i = 0; i < set2->used; ++i) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006603 /* set set2 */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006604 switch (set1->type) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006605 case LYXP_SET_NUMBER:
6606 rc = set_comp_cast(&iter2, set2, LYXP_SET_NUMBER, i);
6607 break;
6608 case LYXP_SET_BOOLEAN:
6609 rc = set_comp_cast(&iter2, set2, LYXP_SET_BOOLEAN, i);
6610 break;
6611 default:
6612 rc = set_comp_cast(&iter2, set2, LYXP_SET_STRING, i);
6613 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006614 }
6615 LY_CHECK_RET(rc);
6616
Michal Vasko4c7763f2020-07-27 17:40:37 +02006617 /* canonize set1 */
6618 LY_CHECK_ERR_RET(rc = set_comp_canonize(&iter1, set1, &set2->val.nodes[i]), lyxp_set_free_content(&iter2), rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006619
Michal Vasko4c7763f2020-07-27 17:40:37 +02006620 /* compare recursively */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006621 rc = moveto_op_comp(&iter1, &iter2, op, options);
Michal Vaskod3678892020-05-21 10:06:58 +02006622 lyxp_set_free_content(&iter2);
Michal Vasko4c7763f2020-07-27 17:40:37 +02006623 LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006624
6625 /* lazy evaluation until true */
Michal Vasko004d3152020-06-11 19:59:22 +02006626 if (iter1.val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006627 set_fill_boolean(set1, 1);
6628 return LY_SUCCESS;
6629 }
6630 }
6631 }
6632
6633 /* false for all nodes */
6634 set_fill_boolean(set1, 0);
6635 return LY_SUCCESS;
6636 }
6637
6638 /* first convert properly */
6639 if ((op[0] == '=') || (op[0] == '!')) {
6640 if ((set1->type == LYXP_SET_BOOLEAN) || (set2->type == LYXP_SET_BOOLEAN)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006641 lyxp_set_cast(set1, LYXP_SET_BOOLEAN);
6642 lyxp_set_cast(set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006643 } else if ((set1->type == LYXP_SET_NUMBER) || (set2->type == LYXP_SET_NUMBER)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006644 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006645 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006646 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006647 LY_CHECK_RET(rc);
6648 } /* else we have 2 strings */
6649 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006650 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006651 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006652 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006653 LY_CHECK_RET(rc);
6654 }
6655
6656 assert(set1->type == set2->type);
6657
6658 /* compute result */
6659 if (op[0] == '=') {
6660 if (set1->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02006661 result = (set1->val.bln == set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006662 } else if (set1->type == LYXP_SET_NUMBER) {
6663 result = (set1->val.num == set2->val.num);
6664 } else {
6665 assert(set1->type == LYXP_SET_STRING);
Michal Vaskoac6c72f2019-11-14 16:09:34 +01006666 result = !strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006667 }
6668 } else if (op[0] == '!') {
6669 if (set1->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02006670 result = (set1->val.bln != set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006671 } else if (set1->type == LYXP_SET_NUMBER) {
6672 result = (set1->val.num != set2->val.num);
6673 } else {
6674 assert(set1->type == LYXP_SET_STRING);
Michal Vaskoc2058432020-11-06 17:26:21 +01006675 result = strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006676 }
6677 } else {
6678 assert(set1->type == LYXP_SET_NUMBER);
6679 if (op[0] == '<') {
6680 if (op[1] == '=') {
6681 result = (set1->val.num <= set2->val.num);
6682 } else {
6683 result = (set1->val.num < set2->val.num);
6684 }
6685 } else {
6686 if (op[1] == '=') {
6687 result = (set1->val.num >= set2->val.num);
6688 } else {
6689 result = (set1->val.num > set2->val.num);
6690 }
6691 }
6692 }
6693
6694 /* assign result */
6695 if (result) {
6696 set_fill_boolean(set1, 1);
6697 } else {
6698 set_fill_boolean(set1, 0);
6699 }
6700
6701 return LY_SUCCESS;
6702}
6703
6704/**
6705 * @brief Move context @p set to the result of a basic operation. Handles '+', '-', unary '-', '*', 'div',
6706 * or 'mod'. Result is LYXP_SET_NUMBER. Indirectly context position aware.
6707 *
6708 * @param[in,out] set1 Set to use for the result.
6709 * @param[in] set2 Set acting as the second operand for @p op.
6710 * @param[in] op Operator to process.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006711 * @return LY_ERR
6712 */
6713static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006714moveto_op_math(struct lyxp_set *set1, struct lyxp_set *set2, const char *op)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006715{
6716 LY_ERR rc;
6717
6718 /* unary '-' */
6719 if (!set2 && (op[0] == '-')) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006720 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006721 LY_CHECK_RET(rc);
6722 set1->val.num *= -1;
6723 lyxp_set_free(set2);
6724 return LY_SUCCESS;
6725 }
6726
6727 assert(set1 && set2);
6728
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006729 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006730 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006731 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006732 LY_CHECK_RET(rc);
6733
6734 switch (op[0]) {
6735 /* '+' */
6736 case '+':
6737 set1->val.num += set2->val.num;
6738 break;
6739
6740 /* '-' */
6741 case '-':
6742 set1->val.num -= set2->val.num;
6743 break;
6744
6745 /* '*' */
6746 case '*':
6747 set1->val.num *= set2->val.num;
6748 break;
6749
6750 /* 'div' */
6751 case 'd':
6752 set1->val.num /= set2->val.num;
6753 break;
6754
6755 /* 'mod' */
6756 case 'm':
6757 set1->val.num = ((long long)set1->val.num) % ((long long)set2->val.num);
6758 break;
6759
6760 default:
6761 LOGINT_RET(set1->ctx);
6762 }
6763
6764 return LY_SUCCESS;
6765}
6766
6767/*
6768 * eval functions
6769 *
6770 * They execute a parsed XPath expression on some data subtree.
6771 */
6772
6773/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02006774 * @brief Evaluate Predicate. Logs directly on error.
6775 *
Michal Vaskod3678892020-05-21 10:06:58 +02006776 * [9] Predicate ::= '[' Expr ']'
Michal Vasko03ff5a72019-09-11 13:49:33 +02006777 *
6778 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02006779 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02006780 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006781 * @param[in] options XPath options.
6782 * @param[in] parent_pos_pred Whether parent predicate was a positional one.
6783 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6784 */
6785static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02006786eval_predicate(const struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set, uint32_t options, ly_bool parent_pos_pred)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006787{
6788 LY_ERR rc;
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01006789 uint16_t orig_exp;
6790 uint32_t i, orig_pos, orig_size;
Michal Vasko5c4e5892019-11-14 12:31:38 +01006791 int32_t pred_in_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006792 struct lyxp_set set2;
6793 struct lyd_node *orig_parent;
6794
6795 /* '[' */
aPiecek8b0cc152021-05-31 16:40:31 +02006796 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02006797 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02006798 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006799
aPiecek8b0cc152021-05-31 16:40:31 +02006800 if (options & LYXP_SKIP_EXPR) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006801only_parse:
aPiecek8b0cc152021-05-31 16:40:31 +02006802 rc = eval_expr_select(exp, tok_idx, 0, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006803 LY_CHECK_RET(rc);
6804 } else if (set->type == LYXP_SET_NODE_SET) {
6805 /* 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 +01006806 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006807
6808 /* empty set, nothing to evaluate */
6809 if (!set->used) {
6810 goto only_parse;
6811 }
6812
Michal Vasko004d3152020-06-11 19:59:22 +02006813 orig_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006814 orig_pos = 0;
6815 orig_size = set->used;
6816 orig_parent = NULL;
Michal Vasko39dbf352020-05-21 10:08:59 +02006817 for (i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006818 set_init(&set2, set);
6819 set_insert_node(&set2, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, 0);
6820 /* remember the node context position for position() and context size for last(),
6821 * predicates should always be evaluated with respect to the child axis (since we do
6822 * not support explicit axes) so we assign positions based on their parents */
Michal Vasko9e685082021-01-29 14:49:09 +01006823 if (parent_pos_pred && (lyd_parent(set->val.nodes[i].node) != orig_parent)) {
6824 orig_parent = lyd_parent(set->val.nodes[i].node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006825 orig_pos = 1;
6826 } else {
6827 ++orig_pos;
6828 }
6829
6830 set2.ctx_pos = orig_pos;
6831 set2.ctx_size = orig_size;
Michal Vasko004d3152020-06-11 19:59:22 +02006832 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006833
Michal Vasko004d3152020-06-11 19:59:22 +02006834 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006835 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006836 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006837 return rc;
6838 }
6839
6840 /* number is a position */
6841 if (set2.type == LYXP_SET_NUMBER) {
6842 if ((long long)set2.val.num == orig_pos) {
6843 set2.val.num = 1;
6844 } else {
6845 set2.val.num = 0;
6846 }
6847 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006848 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006849
6850 /* predicate satisfied or not? */
Michal Vasko004d3152020-06-11 19:59:22 +02006851 if (!set2.val.bln) {
Michal Vasko2caefc12019-11-14 16:07:56 +01006852 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006853 }
6854 }
Michal Vasko2caefc12019-11-14 16:07:56 +01006855 set_remove_nodes_none(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006856
6857 } else if (set->type == LYXP_SET_SCNODE_SET) {
6858 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006859 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006860 /* there is a currently-valid node */
6861 break;
6862 }
6863 }
6864 /* empty set, nothing to evaluate */
6865 if (i == set->used) {
6866 goto only_parse;
6867 }
6868
Michal Vasko004d3152020-06-11 19:59:22 +02006869 orig_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006870
Michal Vasko03ff5a72019-09-11 13:49:33 +02006871 /* set special in_ctx to all the valid snodes */
6872 pred_in_ctx = set_scnode_new_in_ctx(set);
6873
6874 /* use the valid snodes one-by-one */
6875 for (i = 0; i < set->used; ++i) {
6876 if (set->val.scnodes[i].in_ctx != pred_in_ctx) {
6877 continue;
6878 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01006879 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006880
Michal Vasko004d3152020-06-11 19:59:22 +02006881 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006882
Michal Vasko004d3152020-06-11 19:59:22 +02006883 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006884 LY_CHECK_RET(rc);
6885
6886 set->val.scnodes[i].in_ctx = pred_in_ctx;
6887 }
6888
6889 /* restore the state as it was before the predicate */
6890 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006891 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko1a09b212021-05-06 13:00:10 +02006892 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006893 } else if (set->val.scnodes[i].in_ctx == pred_in_ctx) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006894 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006895 }
6896 }
6897
6898 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02006899 set2.type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006900 set_fill_set(&set2, set);
6901
Michal Vasko004d3152020-06-11 19:59:22 +02006902 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006903 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006904 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006905 return rc;
6906 }
6907
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006908 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02006909 if (!set2.val.bln) {
Michal Vaskod3678892020-05-21 10:06:58 +02006910 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006911 }
Michal Vaskod3678892020-05-21 10:06:58 +02006912 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006913 }
6914
6915 /* ']' */
Michal Vasko004d3152020-06-11 19:59:22 +02006916 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_BRACK2);
aPiecek8b0cc152021-05-31 16:40:31 +02006917 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02006918 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02006919 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006920
6921 return LY_SUCCESS;
6922}
6923
6924/**
Michal Vaskod3678892020-05-21 10:06:58 +02006925 * @brief Evaluate Literal. Logs directly on error.
6926 *
6927 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02006928 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02006929 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
6930 */
6931static void
Michal Vasko40308e72020-10-20 16:38:40 +02006932eval_literal(const struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set)
Michal Vaskod3678892020-05-21 10:06:58 +02006933{
6934 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02006935 if (exp->tok_len[*tok_idx] == 2) {
Michal Vaskod3678892020-05-21 10:06:58 +02006936 set_fill_string(set, "", 0);
6937 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02006938 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 +02006939 }
6940 }
6941 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02006942 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02006943 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02006944}
6945
6946/**
Michal Vasko004d3152020-06-11 19:59:22 +02006947 * @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 +02006948 *
Michal Vasko004d3152020-06-11 19:59:22 +02006949 * @param[in] exp Full parsed XPath expression.
6950 * @param[in,out] tok_idx Index in @p exp at the beginning of the predicate, is updated on success.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006951 * @param[in] ctx_node Found schema node as the context for the predicate.
6952 * @param[in] cur_mod Current module for the expression.
6953 * @param[in] cur_node Current (original context) node.
Michal Vasko004d3152020-06-11 19:59:22 +02006954 * @param[in] format Format of any prefixes in key names/values.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006955 * @param[in] prefix_data Format-specific prefix data (see ::ly_resolve_prefix).
Michal Vasko004d3152020-06-11 19:59:22 +02006956 * @param[out] predicates Parsed predicates.
6957 * @param[out] pred_type Type of @p predicates.
6958 * @return LY_SUCCESS on success,
6959 * @return LY_ERR on any error.
Michal Vaskod3678892020-05-21 10:06:58 +02006960 */
6961static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02006962eval_name_test_try_compile_predicates(const struct lyxp_expr *exp, uint16_t *tok_idx, const struct lysc_node *ctx_node,
Radek Krejci8df109d2021-04-23 12:19:08 +02006963 const struct lys_module *cur_mod, const struct lysc_node *cur_node, LY_VALUE_FORMAT format, void *prefix_data,
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006964 struct ly_path_predicate **predicates, enum ly_path_pred_type *pred_type)
Michal Vaskod3678892020-05-21 10:06:58 +02006965{
Michal Vasko004d3152020-06-11 19:59:22 +02006966 LY_ERR ret = LY_SUCCESS;
6967 uint16_t key_count, e_idx, pred_idx = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02006968 const struct lysc_node *key;
Michal Vasko004d3152020-06-11 19:59:22 +02006969 size_t pred_len;
Radek Krejci1deb5be2020-08-26 16:43:36 +02006970 uint32_t prev_lo;
Michal Vasko004d3152020-06-11 19:59:22 +02006971 struct lyxp_expr *exp2 = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02006972
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006973 assert(ctx_node->nodetype & (LYS_LIST | LYS_LEAFLIST));
Michal Vaskod3678892020-05-21 10:06:58 +02006974
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006975 if (ctx_node->nodetype == LYS_LIST) {
Michal Vasko004d3152020-06-11 19:59:22 +02006976 /* get key count */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006977 if (ctx_node->flags & LYS_KEYLESS) {
Michal Vasko004d3152020-06-11 19:59:22 +02006978 return LY_EINVAL;
6979 }
Michal Vasko544e58a2021-01-28 14:33:41 +01006980 for (key_count = 0, key = lysc_node_child(ctx_node); key && (key->flags & LYS_KEY); key = key->next, ++key_count) {}
Michal Vasko004d3152020-06-11 19:59:22 +02006981 assert(key_count);
Michal Vaskod3678892020-05-21 10:06:58 +02006982
Michal Vasko004d3152020-06-11 19:59:22 +02006983 /* learn where the predicates end */
6984 e_idx = *tok_idx;
6985 while (key_count) {
6986 /* '[' */
6987 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
6988 return LY_EINVAL;
6989 }
6990 ++e_idx;
6991
Michal Vasko3354d272021-04-06 09:40:06 +02006992 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_NAMETEST)) {
6993 /* definitely not a key */
6994 return LY_EINVAL;
6995 }
6996
Michal Vasko004d3152020-06-11 19:59:22 +02006997 /* ']' */
6998 while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
6999 ++e_idx;
7000 }
7001 ++e_idx;
7002
7003 /* another presumably key predicate parsed */
7004 --key_count;
7005 }
Michal Vasko004d3152020-06-11 19:59:22 +02007006 } else {
7007 /* learn just where this single predicate ends */
7008 e_idx = *tok_idx;
7009
Michal Vaskod3678892020-05-21 10:06:58 +02007010 /* '[' */
Michal Vasko004d3152020-06-11 19:59:22 +02007011 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
7012 return LY_EINVAL;
7013 }
7014 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02007015
Michal Vasko3354d272021-04-06 09:40:06 +02007016 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_DOT)) {
7017 /* definitely not the value */
7018 return LY_EINVAL;
7019 }
7020
Michal Vaskod3678892020-05-21 10:06:58 +02007021 /* ']' */
Michal Vasko004d3152020-06-11 19:59:22 +02007022 while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
7023 ++e_idx;
7024 }
7025 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02007026 }
7027
Michal Vasko004d3152020-06-11 19:59:22 +02007028 /* get the joined length of all the keys predicates/of the single leaf-list predicate */
7029 pred_len = (exp->tok_pos[e_idx - 1] + exp->tok_len[e_idx - 1]) - exp->tok_pos[*tok_idx];
7030
7031 /* turn logging off */
7032 prev_lo = ly_log_options(0);
7033
7034 /* parse the predicate(s) */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007035 LY_CHECK_GOTO(ret = ly_path_parse_predicate(ctx_node->module->ctx, ctx_node, exp->expr + exp->tok_pos[*tok_idx],
7036 pred_len, LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &exp2), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02007037
7038 /* compile */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007039 ret = ly_path_compile_predicate(ctx_node->module->ctx, cur_node, cur_mod, ctx_node, exp2, &pred_idx, format,
7040 prefix_data, predicates, pred_type);
Michal Vasko004d3152020-06-11 19:59:22 +02007041 LY_CHECK_GOTO(ret, cleanup);
7042
7043 /* success, the predicate must include all the needed information for hash-based search */
7044 *tok_idx = e_idx;
7045
7046cleanup:
7047 ly_log_options(prev_lo);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007048 lyxp_expr_free(ctx_node->module->ctx, exp2);
Michal Vasko004d3152020-06-11 19:59:22 +02007049 return ret;
Michal Vaskod3678892020-05-21 10:06:58 +02007050}
7051
7052/**
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007053 * @brief Search for/check the next schema node that could be the only matching schema node meaning the
7054 * data node(s) could be found using a single hash-based search.
7055 *
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007056 * @param[in] ctx libyang context.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007057 * @param[in] node Next context node to check.
7058 * @param[in] name Expected node name.
7059 * @param[in] name_len Length of @p name.
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007060 * @param[in] moveto_mod Expected node module, can be NULL for JSON format with no prefix.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007061 * @param[in] root_type XPath root type.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007062 * @param[in] format Prefix format.
7063 * @param[in,out] found Previously found node, is updated.
7064 * @return LY_SUCCESS on success,
7065 * @return LY_ENOT if the whole check failed and hashes cannot be used.
7066 */
7067static LY_ERR
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007068eval_name_test_with_predicate_get_scnode(const struct ly_ctx *ctx, const struct lyd_node *node, const char *name,
Radek Krejci8df109d2021-04-23 12:19:08 +02007069 uint16_t name_len, const struct lys_module *moveto_mod, enum lyxp_node_type root_type, LY_VALUE_FORMAT format,
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007070 const struct lysc_node **found)
7071{
7072 const struct lysc_node *scnode;
7073 const struct lys_module *mod;
7074 uint32_t idx = 0;
7075
Radek Krejci8df109d2021-04-23 12:19:08 +02007076 assert((format == LY_VALUE_JSON) || moveto_mod);
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007077
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007078continue_search:
Michal Vasko7d1d0912020-10-16 08:38:30 +02007079 scnode = NULL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007080 if (!node) {
Radek Krejci8df109d2021-04-23 12:19:08 +02007081 if ((format == LY_VALUE_JSON) && !moveto_mod) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007082 /* search all modules for a single match */
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007083 while ((mod = ly_ctx_get_module_iter(ctx, &idx))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007084 scnode = lys_find_child(NULL, mod, name, name_len, 0, 0);
7085 if (scnode) {
7086 /* we have found a match */
7087 break;
7088 }
7089 }
7090
Michal Vasko7d1d0912020-10-16 08:38:30 +02007091 if (!scnode) {
7092 /* all modules searched */
7093 idx = 0;
7094 }
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007095 } else {
7096 /* search in top-level */
7097 scnode = lys_find_child(NULL, moveto_mod, name, name_len, 0, 0);
7098 }
7099 } else if (!*found || (lysc_data_parent(*found) != node->schema)) {
Radek Krejci8df109d2021-04-23 12:19:08 +02007100 if ((format == LY_VALUE_JSON) && !moveto_mod) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007101 /* we must adjust the module to inherit the one from the context node */
7102 moveto_mod = node->schema->module;
7103 }
7104
7105 /* search in children, do not repeat the same search */
7106 scnode = lys_find_child(node->schema, moveto_mod, name, name_len, 0, 0);
Michal Vasko7d1d0912020-10-16 08:38:30 +02007107 } /* else skip redundant search */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007108
7109 /* additional context check */
7110 if (scnode && (root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) {
7111 scnode = NULL;
7112 }
7113
7114 if (scnode) {
7115 if (*found) {
7116 /* we found a schema node with the same name but at different level, give up, too complicated
7117 * (more hash-based searches would be required, not supported) */
7118 return LY_ENOT;
7119 } else {
7120 /* remember the found schema node and continue to make sure it can be used */
7121 *found = scnode;
7122 }
7123 }
7124
7125 if (idx) {
7126 /* continue searching all the following models */
7127 goto continue_search;
7128 }
7129
7130 return LY_SUCCESS;
7131}
7132
7133/**
Michal Vaskod3678892020-05-21 10:06:58 +02007134 * @brief Evaluate NameTest and any following Predicates. Logs directly on error.
7135 *
7136 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
7137 * [6] NodeTest ::= NameTest | NodeType '(' ')'
7138 * [7] NameTest ::= '*' | NCName ':' '*' | QName
7139 *
7140 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007141 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02007142 * @param[in] attr_axis Whether to search attributes or standard nodes.
7143 * @param[in] all_desc Whether to search all the descendants or children only.
aPiecek8b0cc152021-05-31 16:40:31 +02007144 * @param[in,out] set Context and result set.
Michal Vaskod3678892020-05-21 10:06:58 +02007145 * @param[in] options XPath options.
7146 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7147 */
7148static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007149eval_name_test_with_predicate(const struct lyxp_expr *exp, uint16_t *tok_idx, ly_bool attr_axis, ly_bool all_desc,
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007150 struct lyxp_set *set, uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02007151{
Michal Vaskod3678892020-05-21 10:06:58 +02007152 char *path;
Michal Vasko004d3152020-06-11 19:59:22 +02007153 const char *ncname, *ncname_dict = NULL;
7154 uint16_t ncname_len;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007155 const struct lys_module *moveto_mod = NULL;
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007156 const struct lysc_node *scnode = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02007157 struct ly_path_predicate *predicates = NULL;
7158 enum ly_path_pred_type pred_type = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02007159 LY_ERR rc = LY_SUCCESS;
7160
aPiecek8b0cc152021-05-31 16:40:31 +02007161 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007162 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007163 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007164
aPiecek8b0cc152021-05-31 16:40:31 +02007165 if (options & LYXP_SKIP_EXPR) {
Michal Vaskod3678892020-05-21 10:06:58 +02007166 goto moveto;
7167 }
7168
Michal Vasko004d3152020-06-11 19:59:22 +02007169 ncname = &exp->expr[exp->tok_pos[*tok_idx - 1]];
7170 ncname_len = exp->tok_len[*tok_idx - 1];
Michal Vaskod3678892020-05-21 10:06:58 +02007171
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007172 if ((ncname[0] == '*') && (ncname_len == 1)) {
7173 /* all nodes will match */
7174 goto moveto;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007175 }
7176
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007177 /* parse (and skip) module name */
7178 rc = moveto_resolve_model(&ncname, &ncname_len, set, NULL, &moveto_mod);
Michal Vaskod3678892020-05-21 10:06:58 +02007179 LY_CHECK_GOTO(rc, cleanup);
7180
Radek Krejci8df109d2021-04-23 12:19:08 +02007181 if (((set->format == LY_VALUE_JSON) || moveto_mod) && !attr_axis && !all_desc && (set->type == LYXP_SET_NODE_SET)) {
Michal Vaskod3678892020-05-21 10:06:58 +02007182 /* find the matching schema node in some parent in the context */
Radek Krejci1deb5be2020-08-26 16:43:36 +02007183 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007184 if (eval_name_test_with_predicate_get_scnode(set->ctx, set->val.nodes[i].node, ncname, ncname_len,
7185 moveto_mod, set->root_type, set->format, &scnode)) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007186 /* check failed */
7187 scnode = NULL;
7188 break;
Michal Vaskod3678892020-05-21 10:06:58 +02007189 }
7190 }
7191
Michal Vasko004d3152020-06-11 19:59:22 +02007192 if (scnode && (scnode->nodetype & (LYS_LIST | LYS_LEAFLIST))) {
7193 /* try to create the predicates */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007194 if (eval_name_test_try_compile_predicates(exp, tok_idx, scnode, set->cur_mod, set->cur_node ?
7195 set->cur_node->schema : NULL, set->format, set->prefix_data, &predicates, &pred_type)) {
Michal Vasko004d3152020-06-11 19:59:22 +02007196 /* hashes cannot be used */
Michal Vaskod3678892020-05-21 10:06:58 +02007197 scnode = NULL;
7198 }
7199 }
7200 }
7201
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007202 if (!scnode) {
7203 /* we are not able to match based on a schema node and not all the modules match ("*"),
Michal Vasko004d3152020-06-11 19:59:22 +02007204 * use dictionary for efficient comparison */
Radek Krejci011e4aa2020-09-04 15:22:31 +02007205 LY_CHECK_GOTO(rc = lydict_insert(set->ctx, ncname, ncname_len, &ncname_dict), cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02007206 }
7207
7208moveto:
7209 /* move to the attribute(s), data node(s), or schema node(s) */
7210 if (attr_axis) {
aPiecek8b0cc152021-05-31 16:40:31 +02007211 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007212 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskod3678892020-05-21 10:06:58 +02007213 } else {
7214 if (all_desc) {
Michal Vaskocdad7122020-11-09 21:04:44 +01007215 rc = moveto_attr_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007216 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02007217 rc = moveto_attr(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007218 }
7219 LY_CHECK_GOTO(rc, cleanup);
7220 }
7221 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02007222 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02007223 int64_t i;
7224
Michal Vaskod3678892020-05-21 10:06:58 +02007225 if (all_desc) {
Michal Vasko004d3152020-06-11 19:59:22 +02007226 rc = moveto_scnode_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007227 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007228 rc = moveto_scnode(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007229 }
7230 LY_CHECK_GOTO(rc, cleanup);
7231
7232 for (i = set->used - 1; i > -1; --i) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007233 if (set->val.scnodes[i].in_ctx > LYXP_SET_SCNODE_ATOM_NODE) {
Michal Vaskod3678892020-05-21 10:06:58 +02007234 break;
7235 }
7236 }
7237 if (i == -1) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007238 path = lysc_path(set->cur_scnode, LYSC_PATH_LOG, NULL, 0);
Michal Vasko90f12dc2020-12-03 14:20:42 +01007239 LOGWRN(set->ctx, "Schema node \"%.*s\" not found (\"%.*s\") with context node \"%s\".",
7240 ncname_len, ncname, (ncname - exp->expr) + ncname_len, exp->expr, path);
Michal Vaskod3678892020-05-21 10:06:58 +02007241 free(path);
7242 }
7243 } else {
7244 if (all_desc) {
Michal Vaskocdad7122020-11-09 21:04:44 +01007245 rc = moveto_node_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007246 } else {
7247 if (scnode) {
7248 /* we can find the nodes using hashes */
Michal Vaskocdad7122020-11-09 21:04:44 +01007249 rc = moveto_node_hash(set, scnode, predicates, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007250 } else {
Michal Vaskocdad7122020-11-09 21:04:44 +01007251 rc = moveto_node(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007252 }
7253 }
7254 LY_CHECK_GOTO(rc, cleanup);
7255 }
7256 }
7257
7258 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007259 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7260 rc = eval_predicate(exp, tok_idx, set, options, 1);
Michal Vaskod3678892020-05-21 10:06:58 +02007261 LY_CHECK_RET(rc);
7262 }
7263
7264cleanup:
aPiecek8b0cc152021-05-31 16:40:31 +02007265 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko004d3152020-06-11 19:59:22 +02007266 lydict_remove(set->ctx, ncname_dict);
Michal Vaskof7e16e22020-10-21 09:24:39 +02007267 ly_path_predicates_free(set->ctx, pred_type, predicates);
Michal Vaskodb51a8d2020-05-27 15:22:29 +02007268 }
Michal Vaskod3678892020-05-21 10:06:58 +02007269 return rc;
7270}
7271
7272/**
7273 * @brief Evaluate NodeType and any following Predicates. Logs directly on error.
7274 *
7275 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
7276 * [6] NodeTest ::= NameTest | NodeType '(' ')'
7277 * [8] NodeType ::= 'text' | 'node'
7278 *
7279 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007280 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02007281 * @param[in] attr_axis Whether to search attributes or standard nodes.
7282 * @param[in] all_desc Whether to search all the descendants or children only.
aPiecek8b0cc152021-05-31 16:40:31 +02007283 * @param[in,out] set Context and result set.
Michal Vaskod3678892020-05-21 10:06:58 +02007284 * @param[in] options XPath options.
7285 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7286 */
7287static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007288eval_node_type_with_predicate(const struct lyxp_expr *exp, uint16_t *tok_idx, ly_bool attr_axis, ly_bool all_desc,
Radek Krejci1deb5be2020-08-26 16:43:36 +02007289 struct lyxp_set *set, uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02007290{
7291 LY_ERR rc;
7292
7293 /* TODO */
7294 (void)attr_axis;
7295 (void)all_desc;
7296
aPiecek8b0cc152021-05-31 16:40:31 +02007297 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko004d3152020-06-11 19:59:22 +02007298 assert(exp->tok_len[*tok_idx] == 4);
Michal Vaskod3678892020-05-21 10:06:58 +02007299 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007300 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
aPiecek8b0cc152021-05-31 16:40:31 +02007301 options |= LYXP_SKIP_EXPR;
Michal Vaskod3678892020-05-21 10:06:58 +02007302 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007303 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "node", 4)) {
Michal Vaskod3678892020-05-21 10:06:58 +02007304 rc = xpath_node(NULL, 0, set, options);
7305 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007306 assert(!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "text", 4));
Michal Vaskod3678892020-05-21 10:06:58 +02007307 rc = xpath_text(NULL, 0, set, options);
7308 }
7309 LY_CHECK_RET(rc);
7310 }
7311 }
aPiecek8b0cc152021-05-31 16:40:31 +02007312 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007313 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007314 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007315
7316 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02007317 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
aPiecek8b0cc152021-05-31 16:40:31 +02007318 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007319 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007320 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007321
7322 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007323 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
aPiecek8b0cc152021-05-31 16:40:31 +02007324 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007325 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007326 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007327
7328 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007329 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7330 rc = eval_predicate(exp, tok_idx, set, options, 1);
Michal Vaskod3678892020-05-21 10:06:58 +02007331 LY_CHECK_RET(rc);
7332 }
7333
7334 return LY_SUCCESS;
7335}
7336
7337/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02007338 * @brief Evaluate RelativeLocationPath. Logs directly on error.
7339 *
7340 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
7341 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
Michal Vaskod3678892020-05-21 10:06:58 +02007342 * [6] NodeTest ::= NameTest | NodeType '(' ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007343 *
7344 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007345 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007346 * @param[in] all_desc Whether to search all the descendants or children only.
aPiecek8b0cc152021-05-31 16:40:31 +02007347 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007348 * @param[in] options XPath options.
7349 * @return LY_ERR (YL_EINCOMPLETE on unresolved when)
7350 */
7351static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007352eval_relative_location_path(const struct lyxp_expr *exp, uint16_t *tok_idx, ly_bool all_desc, struct lyxp_set *set,
7353 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007354{
Radek Krejci857189e2020-09-01 13:26:36 +02007355 ly_bool attr_axis;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007356 LY_ERR rc;
7357
7358 goto step;
7359 do {
7360 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02007361 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007362 all_desc = 0;
7363 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007364 assert(exp->tok_len[*tok_idx] == 2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007365 all_desc = 1;
7366 }
aPiecek8b0cc152021-05-31 16:40:31 +02007367 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007368 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007369 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007370
7371step:
Michal Vaskod3678892020-05-21 10:06:58 +02007372 /* evaluate abbreviated axis '@'? if any */
Michal Vasko004d3152020-06-11 19:59:22 +02007373 if (exp->tokens[*tok_idx] == LYXP_TOKEN_AT) {
Michal Vaskod3678892020-05-21 10:06:58 +02007374 attr_axis = 1;
7375
aPiecek8b0cc152021-05-31 16:40:31 +02007376 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007377 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007378 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007379 } else {
7380 attr_axis = 0;
7381 }
7382
Michal Vasko03ff5a72019-09-11 13:49:33 +02007383 /* Step */
Michal Vasko004d3152020-06-11 19:59:22 +02007384 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007385 case LYXP_TOKEN_DOT:
7386 /* evaluate '.' */
aPiecek8b0cc152021-05-31 16:40:31 +02007387 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007388 rc = moveto_scnode_self(set, all_desc, options);
7389 } else {
7390 rc = moveto_self(set, all_desc, options);
7391 }
7392 LY_CHECK_RET(rc);
7393 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007394 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007395 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007396 break;
7397
7398 case LYXP_TOKEN_DDOT:
7399 /* evaluate '..' */
aPiecek8b0cc152021-05-31 16:40:31 +02007400 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007401 rc = moveto_scnode_parent(set, all_desc, options);
7402 } else {
7403 rc = moveto_parent(set, all_desc, options);
7404 }
7405 LY_CHECK_RET(rc);
aPiecek8b0cc152021-05-31 16:40:31 +02007406 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007407 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007408 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007409 break;
7410
Michal Vasko03ff5a72019-09-11 13:49:33 +02007411 case LYXP_TOKEN_NAMETEST:
Michal Vaskod3678892020-05-21 10:06:58 +02007412 /* evaluate NameTest Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007413 rc = eval_name_test_with_predicate(exp, tok_idx, attr_axis, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007414 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02007415 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007416
Michal Vaskod3678892020-05-21 10:06:58 +02007417 case LYXP_TOKEN_NODETYPE:
7418 /* evaluate NodeType Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007419 rc = eval_node_type_with_predicate(exp, tok_idx, attr_axis, all_desc, set, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007420 LY_CHECK_RET(rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007421 break;
7422
7423 default:
aPiecek8b0cc152021-05-31 16:40:31 +02007424 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007425 }
Michal Vasko004d3152020-06-11 19:59:22 +02007426 } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007427
7428 return LY_SUCCESS;
7429}
7430
7431/**
7432 * @brief Evaluate AbsoluteLocationPath. Logs directly on error.
7433 *
7434 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
7435 *
7436 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007437 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02007438 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007439 * @param[in] options XPath options.
7440 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7441 */
7442static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007443eval_absolute_location_path(const struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007444{
Radek Krejci857189e2020-09-01 13:26:36 +02007445 ly_bool all_desc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007446
aPiecek8b0cc152021-05-31 16:40:31 +02007447 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007448 /* no matter what tokens follow, we need to be at the root */
Michal Vaskob0099a92020-08-31 14:55:23 +02007449 LY_CHECK_RET(moveto_root(set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007450 }
7451
7452 /* '/' RelativeLocationPath? */
Michal Vasko004d3152020-06-11 19:59:22 +02007453 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007454 /* evaluate '/' - deferred */
7455 all_desc = 0;
aPiecek8b0cc152021-05-31 16:40:31 +02007456 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007457 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007458 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007459
Michal Vasko004d3152020-06-11 19:59:22 +02007460 if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007461 return LY_SUCCESS;
7462 }
Michal Vasko004d3152020-06-11 19:59:22 +02007463 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007464 case LYXP_TOKEN_DOT:
7465 case LYXP_TOKEN_DDOT:
7466 case LYXP_TOKEN_AT:
7467 case LYXP_TOKEN_NAMETEST:
7468 case LYXP_TOKEN_NODETYPE:
Michal Vaskob0099a92020-08-31 14:55:23 +02007469 LY_CHECK_RET(eval_relative_location_path(exp, tok_idx, all_desc, set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007470 break;
7471 default:
7472 break;
7473 }
7474
Michal Vasko03ff5a72019-09-11 13:49:33 +02007475 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02007476 /* '//' RelativeLocationPath */
Michal Vasko03ff5a72019-09-11 13:49:33 +02007477 /* evaluate '//' - deferred so as not to waste memory by remembering all the nodes */
7478 all_desc = 1;
aPiecek8b0cc152021-05-31 16:40:31 +02007479 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007480 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007481 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007482
Michal Vaskob0099a92020-08-31 14:55:23 +02007483 LY_CHECK_RET(eval_relative_location_path(exp, tok_idx, all_desc, set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007484 }
7485
7486 return LY_SUCCESS;
7487}
7488
7489/**
7490 * @brief Evaluate FunctionCall. Logs directly on error.
7491 *
Michal Vaskod3678892020-05-21 10:06:58 +02007492 * [11] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007493 *
7494 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007495 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02007496 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007497 * @param[in] options XPath options.
7498 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7499 */
7500static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007501eval_function_call(const struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007502{
7503 LY_ERR rc;
Michal Vasko69730152020-10-09 16:30:07 +02007504
Radek Krejci1deb5be2020-08-26 16:43:36 +02007505 LY_ERR (*xpath_func)(struct lyxp_set **, uint16_t, struct lyxp_set *, uint32_t) = NULL;
Michal Vasko0cbf54f2019-12-16 10:01:06 +01007506 uint16_t arg_count = 0, i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007507 struct lyxp_set **args = NULL, **args_aux;
7508
aPiecek8b0cc152021-05-31 16:40:31 +02007509 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007510 /* FunctionName */
Michal Vasko004d3152020-06-11 19:59:22 +02007511 switch (exp->tok_len[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007512 case 3:
Michal Vasko004d3152020-06-11 19:59:22 +02007513 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007514 xpath_func = &xpath_not;
Michal Vasko004d3152020-06-11 19:59:22 +02007515 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007516 xpath_func = &xpath_sum;
7517 }
7518 break;
7519 case 4:
Michal Vasko004d3152020-06-11 19:59:22 +02007520 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007521 xpath_func = &xpath_lang;
Michal Vasko004d3152020-06-11 19:59:22 +02007522 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007523 xpath_func = &xpath_last;
Michal Vasko004d3152020-06-11 19:59:22 +02007524 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007525 xpath_func = &xpath_name;
Michal Vasko004d3152020-06-11 19:59:22 +02007526 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007527 xpath_func = &xpath_true;
7528 }
7529 break;
7530 case 5:
Michal Vasko004d3152020-06-11 19:59:22 +02007531 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007532 xpath_func = &xpath_count;
Michal Vasko004d3152020-06-11 19:59:22 +02007533 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007534 xpath_func = &xpath_false;
Michal Vasko004d3152020-06-11 19:59:22 +02007535 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007536 xpath_func = &xpath_floor;
Michal Vasko004d3152020-06-11 19:59:22 +02007537 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007538 xpath_func = &xpath_round;
Michal Vasko004d3152020-06-11 19:59:22 +02007539 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007540 xpath_func = &xpath_deref;
7541 }
7542 break;
7543 case 6:
Michal Vasko004d3152020-06-11 19:59:22 +02007544 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007545 xpath_func = &xpath_concat;
Michal Vasko004d3152020-06-11 19:59:22 +02007546 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007547 xpath_func = &xpath_number;
Michal Vasko004d3152020-06-11 19:59:22 +02007548 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007549 xpath_func = &xpath_string;
7550 }
7551 break;
7552 case 7:
Michal Vasko004d3152020-06-11 19:59:22 +02007553 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007554 xpath_func = &xpath_boolean;
Michal Vasko004d3152020-06-11 19:59:22 +02007555 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007556 xpath_func = &xpath_ceiling;
Michal Vasko004d3152020-06-11 19:59:22 +02007557 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007558 xpath_func = &xpath_current;
7559 }
7560 break;
7561 case 8:
Michal Vasko004d3152020-06-11 19:59:22 +02007562 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007563 xpath_func = &xpath_contains;
Michal Vasko004d3152020-06-11 19:59:22 +02007564 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007565 xpath_func = &xpath_position;
Michal Vasko004d3152020-06-11 19:59:22 +02007566 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007567 xpath_func = &xpath_re_match;
7568 }
7569 break;
7570 case 9:
Michal Vasko004d3152020-06-11 19:59:22 +02007571 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007572 xpath_func = &xpath_substring;
Michal Vasko004d3152020-06-11 19:59:22 +02007573 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007574 xpath_func = &xpath_translate;
7575 }
7576 break;
7577 case 10:
Michal Vasko004d3152020-06-11 19:59:22 +02007578 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007579 xpath_func = &xpath_local_name;
Michal Vasko004d3152020-06-11 19:59:22 +02007580 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007581 xpath_func = &xpath_enum_value;
Michal Vasko004d3152020-06-11 19:59:22 +02007582 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007583 xpath_func = &xpath_bit_is_set;
7584 }
7585 break;
7586 case 11:
Michal Vasko004d3152020-06-11 19:59:22 +02007587 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007588 xpath_func = &xpath_starts_with;
7589 }
7590 break;
7591 case 12:
Michal Vasko004d3152020-06-11 19:59:22 +02007592 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007593 xpath_func = &xpath_derived_from;
7594 }
7595 break;
7596 case 13:
Michal Vasko004d3152020-06-11 19:59:22 +02007597 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007598 xpath_func = &xpath_namespace_uri;
Michal Vasko004d3152020-06-11 19:59:22 +02007599 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007600 xpath_func = &xpath_string_length;
7601 }
7602 break;
7603 case 15:
Michal Vasko004d3152020-06-11 19:59:22 +02007604 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007605 xpath_func = &xpath_normalize_space;
Michal Vasko004d3152020-06-11 19:59:22 +02007606 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007607 xpath_func = &xpath_substring_after;
7608 }
7609 break;
7610 case 16:
Michal Vasko004d3152020-06-11 19:59:22 +02007611 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007612 xpath_func = &xpath_substring_before;
7613 }
7614 break;
7615 case 20:
Michal Vasko004d3152020-06-11 19:59:22 +02007616 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007617 xpath_func = &xpath_derived_from_or_self;
7618 }
7619 break;
7620 }
7621
7622 if (!xpath_func) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01007623 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 +02007624 return LY_EVALID;
7625 }
7626 }
7627
aPiecek8b0cc152021-05-31 16:40:31 +02007628 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007629 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007630 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007631
7632 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02007633 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
aPiecek8b0cc152021-05-31 16:40:31 +02007634 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007635 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007636 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007637
7638 /* ( Expr ( ',' Expr )* )? */
Michal Vasko004d3152020-06-11 19:59:22 +02007639 if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) {
aPiecek8b0cc152021-05-31 16:40:31 +02007640 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007641 args = malloc(sizeof *args);
7642 LY_CHECK_ERR_GOTO(!args, LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7643 arg_count = 1;
7644 args[0] = set_copy(set);
7645 if (!args[0]) {
7646 rc = LY_EMEM;
7647 goto cleanup;
7648 }
7649
Michal Vasko004d3152020-06-11 19:59:22 +02007650 rc = eval_expr_select(exp, tok_idx, 0, args[0], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007651 LY_CHECK_GOTO(rc, cleanup);
7652 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02007653 rc = eval_expr_select(exp, tok_idx, 0, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007654 LY_CHECK_GOTO(rc, cleanup);
7655 }
7656 }
Michal Vasko004d3152020-06-11 19:59:22 +02007657 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) {
aPiecek8b0cc152021-05-31 16:40:31 +02007658 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007659 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007660 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007661
aPiecek8b0cc152021-05-31 16:40:31 +02007662 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007663 ++arg_count;
7664 args_aux = realloc(args, arg_count * sizeof *args);
7665 LY_CHECK_ERR_GOTO(!args_aux, arg_count--; LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7666 args = args_aux;
7667 args[arg_count - 1] = set_copy(set);
7668 if (!args[arg_count - 1]) {
7669 rc = LY_EMEM;
7670 goto cleanup;
7671 }
7672
Michal Vasko004d3152020-06-11 19:59:22 +02007673 rc = eval_expr_select(exp, tok_idx, 0, args[arg_count - 1], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007674 LY_CHECK_GOTO(rc, cleanup);
7675 } else {
aPiecek8b0cc152021-05-31 16:40:31 +02007676 rc = eval_expr_select(exp, tok_idx, 0, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007677 LY_CHECK_GOTO(rc, cleanup);
7678 }
7679 }
7680
7681 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007682 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
aPiecek8b0cc152021-05-31 16:40:31 +02007683 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007684 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007685 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007686
aPiecek8b0cc152021-05-31 16:40:31 +02007687 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007688 /* evaluate function */
7689 rc = xpath_func(args, arg_count, set, options);
7690
7691 if (options & LYXP_SCNODE_ALL) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007692 /* merge all nodes from arg evaluations */
7693 for (i = 0; i < arg_count; ++i) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007694 set_scnode_clear_ctx(args[i], LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007695 lyxp_set_scnode_merge(set, args[i]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007696 }
7697 }
7698 } else {
7699 rc = LY_SUCCESS;
7700 }
7701
7702cleanup:
7703 for (i = 0; i < arg_count; ++i) {
7704 lyxp_set_free(args[i]);
7705 }
7706 free(args);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007707 return rc;
7708}
7709
7710/**
7711 * @brief Evaluate Number. Logs directly on error.
7712 *
7713 * @param[in] ctx Context for errors.
7714 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007715 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007716 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7717 * @return LY_ERR
7718 */
7719static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007720eval_number(struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007721{
7722 long double num;
7723 char *endptr;
7724
7725 if (set) {
7726 errno = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02007727 num = strtold(&exp->expr[exp->tok_pos[*tok_idx]], &endptr);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007728 if (errno) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01007729 LOGVAL(ctx, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]);
7730 LOGVAL(ctx, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double (%s).",
Michal Vasko69730152020-10-09 16:30:07 +02007731 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]], strerror(errno));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007732 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02007733 } else if (endptr - &exp->expr[exp->tok_pos[*tok_idx]] != exp->tok_len[*tok_idx]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01007734 LOGVAL(ctx, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]);
7735 LOGVAL(ctx, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double.",
Michal Vasko69730152020-10-09 16:30:07 +02007736 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007737 return LY_EVALID;
7738 }
7739
7740 set_fill_number(set, num);
7741 }
7742
7743 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007744 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007745 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007746 return LY_SUCCESS;
7747}
7748
7749/**
7750 * @brief Evaluate PathExpr. Logs directly on error.
7751 *
Michal Vaskod3678892020-05-21 10:06:58 +02007752 * [12] PathExpr ::= LocationPath | PrimaryExpr Predicate*
Michal Vasko03ff5a72019-09-11 13:49:33 +02007753 * | PrimaryExpr Predicate* '/' RelativeLocationPath
7754 * | PrimaryExpr Predicate* '//' RelativeLocationPath
7755 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
Michal Vaskod3678892020-05-21 10:06:58 +02007756 * [10] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall
Michal Vasko03ff5a72019-09-11 13:49:33 +02007757 *
7758 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007759 * @param[in] tok_idx Position in the expression @p exp.
aPiecek8b0cc152021-05-31 16:40:31 +02007760 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007761 * @param[in] options XPath options.
7762 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7763 */
7764static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007765eval_path_expr(const struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007766{
Radek Krejci857189e2020-09-01 13:26:36 +02007767 ly_bool all_desc, parent_pos_pred;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007768 LY_ERR rc;
7769
Michal Vasko004d3152020-06-11 19:59:22 +02007770 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007771 case LYXP_TOKEN_PAR1:
7772 /* '(' Expr ')' */
7773
7774 /* '(' */
aPiecek8b0cc152021-05-31 16:40:31 +02007775 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007776 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007777 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007778
7779 /* Expr */
Michal Vasko004d3152020-06-11 19:59:22 +02007780 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007781 LY_CHECK_RET(rc);
7782
7783 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007784 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
aPiecek8b0cc152021-05-31 16:40:31 +02007785 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007786 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007787 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007788
7789 parent_pos_pred = 0;
7790 goto predicate;
7791
7792 case LYXP_TOKEN_DOT:
7793 case LYXP_TOKEN_DDOT:
7794 case LYXP_TOKEN_AT:
7795 case LYXP_TOKEN_NAMETEST:
7796 case LYXP_TOKEN_NODETYPE:
7797 /* RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007798 rc = eval_relative_location_path(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007799 LY_CHECK_RET(rc);
7800 break;
7801
7802 case LYXP_TOKEN_FUNCNAME:
7803 /* FunctionCall */
aPiecek8b0cc152021-05-31 16:40:31 +02007804 rc = eval_function_call(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007805 LY_CHECK_RET(rc);
7806
7807 parent_pos_pred = 1;
7808 goto predicate;
7809
Michal Vasko3e48bf32020-06-01 08:39:07 +02007810 case LYXP_TOKEN_OPER_PATH:
7811 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02007812 /* AbsoluteLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007813 rc = eval_absolute_location_path(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007814 LY_CHECK_RET(rc);
7815 break;
7816
7817 case LYXP_TOKEN_LITERAL:
7818 /* Literal */
aPiecek8b0cc152021-05-31 16:40:31 +02007819 if ((options & LYXP_SKIP_EXPR) || (options & LYXP_SCNODE_ALL)) {
7820 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007821 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007822 }
Michal Vasko004d3152020-06-11 19:59:22 +02007823 eval_literal(exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007824 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007825 eval_literal(exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007826 }
7827
7828 parent_pos_pred = 1;
7829 goto predicate;
7830
7831 case LYXP_TOKEN_NUMBER:
7832 /* Number */
aPiecek8b0cc152021-05-31 16:40:31 +02007833 if ((options & LYXP_SKIP_EXPR) || (options & LYXP_SCNODE_ALL)) {
7834 if (!(options & LYXP_SKIP_EXPR)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007835 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007836 }
Michal Vasko004d3152020-06-11 19:59:22 +02007837 rc = eval_number(NULL, exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007838 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007839 rc = eval_number(set->ctx, exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007840 }
7841 LY_CHECK_RET(rc);
7842
7843 parent_pos_pred = 1;
7844 goto predicate;
7845
7846 default:
Radek Krejci2efc45b2020-12-22 16:25:44 +01007847 LOGVAL(set->ctx, LY_VCODE_XP_INTOK, lyxp_print_token(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007848 return LY_EVALID;
7849 }
7850
7851 return LY_SUCCESS;
7852
7853predicate:
7854 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007855 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7856 rc = eval_predicate(exp, tok_idx, set, options, parent_pos_pred);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007857 LY_CHECK_RET(rc);
7858 }
7859
7860 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007861 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007862
7863 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02007864 if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007865 all_desc = 0;
7866 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007867 all_desc = 1;
7868 }
7869
aPiecek8b0cc152021-05-31 16:40:31 +02007870 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007871 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007872 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007873
Michal Vasko004d3152020-06-11 19:59:22 +02007874 rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007875 LY_CHECK_RET(rc);
7876 }
7877
7878 return LY_SUCCESS;
7879}
7880
7881/**
7882 * @brief Evaluate UnionExpr. Logs directly on error.
7883 *
Michal Vaskod3678892020-05-21 10:06:58 +02007884 * [20] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007885 *
7886 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007887 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007888 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02007889 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007890 * @param[in] options XPath options.
7891 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7892 */
7893static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007894eval_union_expr(const struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007895{
7896 LY_ERR rc = LY_SUCCESS;
7897 struct lyxp_set orig_set, set2;
7898 uint16_t i;
7899
7900 assert(repeat);
7901
7902 set_init(&orig_set, set);
7903 set_init(&set2, set);
7904
7905 set_fill_set(&orig_set, set);
7906
Michal Vasko004d3152020-06-11 19:59:22 +02007907 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007908 LY_CHECK_GOTO(rc, cleanup);
7909
7910 /* ('|' PathExpr)* */
7911 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007912 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_UNI);
aPiecek8b0cc152021-05-31 16:40:31 +02007913 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007914 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007915 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007916
aPiecek8b0cc152021-05-31 16:40:31 +02007917 if (options & LYXP_SKIP_EXPR) {
7918 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007919 LY_CHECK_GOTO(rc, cleanup);
7920 continue;
7921 }
7922
7923 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02007924 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007925 LY_CHECK_GOTO(rc, cleanup);
7926
7927 /* eval */
7928 if (options & LYXP_SCNODE_ALL) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01007929 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007930 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007931 rc = moveto_union(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007932 LY_CHECK_GOTO(rc, cleanup);
7933 }
7934 }
7935
7936cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007937 lyxp_set_free_content(&orig_set);
7938 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007939 return rc;
7940}
7941
7942/**
7943 * @brief Evaluate UnaryExpr. Logs directly on error.
7944 *
Michal Vaskod3678892020-05-21 10:06:58 +02007945 * [19] UnaryExpr ::= UnionExpr | '-' UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007946 *
7947 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007948 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007949 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02007950 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007951 * @param[in] options XPath options.
7952 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7953 */
7954static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007955eval_unary_expr(const struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007956{
7957 LY_ERR rc;
7958 uint16_t this_op, i;
7959
7960 assert(repeat);
7961
7962 /* ('-')+ */
Michal Vasko004d3152020-06-11 19:59:22 +02007963 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007964 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007965 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 +02007966
aPiecek8b0cc152021-05-31 16:40:31 +02007967 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02007968 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007969 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007970 }
7971
Michal Vasko004d3152020-06-11 19:59:22 +02007972 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNARY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007973 LY_CHECK_RET(rc);
7974
aPiecek8b0cc152021-05-31 16:40:31 +02007975 if (!(options & LYXP_SKIP_EXPR) && (repeat % 2)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007976 if (options & LYXP_SCNODE_ALL) {
7977 warn_operands(set->ctx, set, NULL, 1, exp->expr, exp->tok_pos[this_op]);
7978 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007979 rc = moveto_op_math(set, NULL, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007980 LY_CHECK_RET(rc);
7981 }
7982 }
7983
7984 return LY_SUCCESS;
7985}
7986
7987/**
7988 * @brief Evaluate MultiplicativeExpr. Logs directly on error.
7989 *
Michal Vaskod3678892020-05-21 10:06:58 +02007990 * [18] MultiplicativeExpr ::= UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007991 * | MultiplicativeExpr '*' UnaryExpr
7992 * | MultiplicativeExpr 'div' UnaryExpr
7993 * | MultiplicativeExpr 'mod' UnaryExpr
7994 *
7995 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007996 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007997 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02007998 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007999 * @param[in] options XPath options.
8000 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8001 */
8002static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008003eval_multiplicative_expr(const struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set,
8004 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008005{
8006 LY_ERR rc;
8007 uint16_t this_op;
8008 struct lyxp_set orig_set, set2;
8009 uint16_t i;
8010
8011 assert(repeat);
8012
8013 set_init(&orig_set, set);
8014 set_init(&set2, set);
8015
8016 set_fill_set(&orig_set, set);
8017
Michal Vasko004d3152020-06-11 19:59:22 +02008018 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008019 LY_CHECK_GOTO(rc, cleanup);
8020
8021 /* ('*' / 'div' / 'mod' UnaryExpr)* */
8022 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008023 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008024
Michal Vasko004d3152020-06-11 19:59:22 +02008025 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
aPiecek8b0cc152021-05-31 16:40:31 +02008026 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008027 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008028 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008029
aPiecek8b0cc152021-05-31 16:40:31 +02008030 if (options & LYXP_SKIP_EXPR) {
8031 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008032 LY_CHECK_GOTO(rc, cleanup);
8033 continue;
8034 }
8035
8036 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008037 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008038 LY_CHECK_GOTO(rc, cleanup);
8039
8040 /* eval */
8041 if (options & LYXP_SCNODE_ALL) {
8042 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008043 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02008044 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008045 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008046 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008047 LY_CHECK_GOTO(rc, cleanup);
8048 }
8049 }
8050
8051cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008052 lyxp_set_free_content(&orig_set);
8053 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008054 return rc;
8055}
8056
8057/**
8058 * @brief Evaluate AdditiveExpr. Logs directly on error.
8059 *
Michal Vaskod3678892020-05-21 10:06:58 +02008060 * [17] AdditiveExpr ::= MultiplicativeExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008061 * | AdditiveExpr '+' MultiplicativeExpr
8062 * | AdditiveExpr '-' MultiplicativeExpr
8063 *
8064 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008065 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008066 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008067 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008068 * @param[in] options XPath options.
8069 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8070 */
8071static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008072eval_additive_expr(const struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008073{
8074 LY_ERR rc;
8075 uint16_t this_op;
8076 struct lyxp_set orig_set, set2;
8077 uint16_t i;
8078
8079 assert(repeat);
8080
8081 set_init(&orig_set, set);
8082 set_init(&set2, set);
8083
8084 set_fill_set(&orig_set, set);
8085
Michal Vasko004d3152020-06-11 19:59:22 +02008086 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008087 LY_CHECK_GOTO(rc, cleanup);
8088
8089 /* ('+' / '-' MultiplicativeExpr)* */
8090 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008091 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008092
Michal Vasko004d3152020-06-11 19:59:22 +02008093 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008094 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02008095 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008096 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008097
aPiecek8b0cc152021-05-31 16:40:31 +02008098 if (options & LYXP_SKIP_EXPR) {
8099 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008100 LY_CHECK_GOTO(rc, cleanup);
8101 continue;
8102 }
8103
8104 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008105 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008106 LY_CHECK_GOTO(rc, cleanup);
8107
8108 /* eval */
8109 if (options & LYXP_SCNODE_ALL) {
8110 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008111 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02008112 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008113 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008114 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008115 LY_CHECK_GOTO(rc, cleanup);
8116 }
8117 }
8118
8119cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008120 lyxp_set_free_content(&orig_set);
8121 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008122 return rc;
8123}
8124
8125/**
8126 * @brief Evaluate RelationalExpr. Logs directly on error.
8127 *
Michal Vaskod3678892020-05-21 10:06:58 +02008128 * [16] RelationalExpr ::= AdditiveExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008129 * | RelationalExpr '<' AdditiveExpr
8130 * | RelationalExpr '>' AdditiveExpr
8131 * | RelationalExpr '<=' AdditiveExpr
8132 * | RelationalExpr '>=' AdditiveExpr
8133 *
8134 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008135 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008136 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008137 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008138 * @param[in] options XPath options.
8139 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8140 */
8141static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008142eval_relational_expr(const struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008143{
8144 LY_ERR rc;
8145 uint16_t this_op;
8146 struct lyxp_set orig_set, set2;
8147 uint16_t i;
8148
8149 assert(repeat);
8150
8151 set_init(&orig_set, set);
8152 set_init(&set2, set);
8153
8154 set_fill_set(&orig_set, set);
8155
Michal Vasko004d3152020-06-11 19:59:22 +02008156 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008157 LY_CHECK_GOTO(rc, cleanup);
8158
8159 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
8160 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008161 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008162
Michal Vasko004d3152020-06-11 19:59:22 +02008163 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_COMP);
aPiecek8b0cc152021-05-31 16:40:31 +02008164 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008165 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008166 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008167
aPiecek8b0cc152021-05-31 16:40:31 +02008168 if (options & LYXP_SKIP_EXPR) {
8169 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008170 LY_CHECK_GOTO(rc, cleanup);
8171 continue;
8172 }
8173
8174 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008175 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008176 LY_CHECK_GOTO(rc, cleanup);
8177
8178 /* eval */
8179 if (options & LYXP_SCNODE_ALL) {
8180 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008181 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02008182 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008183 } else {
8184 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options);
8185 LY_CHECK_GOTO(rc, cleanup);
8186 }
8187 }
8188
8189cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008190 lyxp_set_free_content(&orig_set);
8191 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008192 return rc;
8193}
8194
8195/**
8196 * @brief Evaluate EqualityExpr. Logs directly on error.
8197 *
Michal Vaskod3678892020-05-21 10:06:58 +02008198 * [15] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008199 * | EqualityExpr '!=' RelationalExpr
8200 *
8201 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008202 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008203 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008204 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008205 * @param[in] options XPath options.
8206 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8207 */
8208static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008209eval_equality_expr(const struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008210{
8211 LY_ERR rc;
8212 uint16_t this_op;
8213 struct lyxp_set orig_set, set2;
8214 uint16_t i;
8215
8216 assert(repeat);
8217
8218 set_init(&orig_set, set);
8219 set_init(&set2, set);
8220
8221 set_fill_set(&orig_set, set);
8222
Michal Vasko004d3152020-06-11 19:59:22 +02008223 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008224 LY_CHECK_GOTO(rc, cleanup);
8225
8226 /* ('=' / '!=' RelationalExpr)* */
8227 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008228 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008229
Michal Vasko004d3152020-06-11 19:59:22 +02008230 assert((exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_EQUAL) || (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_NEQUAL));
aPiecek8b0cc152021-05-31 16:40:31 +02008231 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (options & LYXP_SKIP_EXPR ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008232 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008233 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008234
aPiecek8b0cc152021-05-31 16:40:31 +02008235 if (options & LYXP_SKIP_EXPR) {
8236 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008237 LY_CHECK_GOTO(rc, cleanup);
8238 continue;
8239 }
8240
8241 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008242 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008243 LY_CHECK_GOTO(rc, cleanup);
8244
8245 /* eval */
8246 if (options & LYXP_SCNODE_ALL) {
8247 warn_operands(set->ctx, set, &set2, 0, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vasko004d3152020-06-11 19:59:22 +02008248 warn_equality_value(exp, set, *tok_idx - 1, this_op - 1, *tok_idx - 1);
8249 warn_equality_value(exp, &set2, this_op - 1, this_op - 1, *tok_idx - 1);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008250 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02008251 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008252 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008253 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options);
8254 LY_CHECK_GOTO(rc, cleanup);
8255 }
8256 }
8257
8258cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008259 lyxp_set_free_content(&orig_set);
8260 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008261 return rc;
8262}
8263
8264/**
8265 * @brief Evaluate AndExpr. Logs directly on error.
8266 *
Michal Vaskod3678892020-05-21 10:06:58 +02008267 * [14] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008268 *
8269 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008270 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008271 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008272 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008273 * @param[in] options XPath options.
8274 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8275 */
8276static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008277eval_and_expr(const struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008278{
8279 LY_ERR rc;
8280 struct lyxp_set orig_set, set2;
8281 uint16_t i;
8282
8283 assert(repeat);
8284
8285 set_init(&orig_set, set);
8286 set_init(&set2, set);
8287
8288 set_fill_set(&orig_set, set);
8289
Michal Vasko004d3152020-06-11 19:59:22 +02008290 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008291 LY_CHECK_GOTO(rc, cleanup);
8292
8293 /* cast to boolean, we know that will be the final result */
aPiecek8b0cc152021-05-31 16:40:31 +02008294 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008295 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008296 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008297 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008298 }
8299
8300 /* ('and' EqualityExpr)* */
8301 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008302 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
aPiecek8b0cc152021-05-31 16:40:31 +02008303 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, ((options & LYXP_SKIP_EXPR) || !set->val.bln ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008304 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008305 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008306
8307 /* lazy evaluation */
aPiecek8b0cc152021-05-31 16:40:31 +02008308 if ((options & LYXP_SKIP_EXPR) || ((set->type == LYXP_SET_BOOLEAN) && !set->val.bln)) {
8309 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008310 LY_CHECK_GOTO(rc, cleanup);
8311 continue;
8312 }
8313
8314 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008315 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008316 LY_CHECK_GOTO(rc, cleanup);
8317
8318 /* eval - just get boolean value actually */
8319 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008320 set_scnode_clear_ctx(&set2, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008321 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008322 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008323 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008324 set_fill_set(set, &set2);
8325 }
8326 }
8327
8328cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008329 lyxp_set_free_content(&orig_set);
8330 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008331 return rc;
8332}
8333
8334/**
8335 * @brief Evaluate OrExpr. Logs directly on error.
8336 *
Michal Vaskod3678892020-05-21 10:06:58 +02008337 * [13] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008338 *
8339 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008340 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008341 * @param[in] repeat How many times this expression is repeated.
aPiecek8b0cc152021-05-31 16:40:31 +02008342 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008343 * @param[in] options XPath options.
8344 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8345 */
8346static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008347eval_or_expr(const struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008348{
8349 LY_ERR rc;
8350 struct lyxp_set orig_set, set2;
8351 uint16_t i;
8352
8353 assert(repeat);
8354
8355 set_init(&orig_set, set);
8356 set_init(&set2, set);
8357
8358 set_fill_set(&orig_set, set);
8359
Michal Vasko004d3152020-06-11 19:59:22 +02008360 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008361 LY_CHECK_GOTO(rc, cleanup);
8362
8363 /* cast to boolean, we know that will be the final result */
aPiecek8b0cc152021-05-31 16:40:31 +02008364 if (!(options & LYXP_SKIP_EXPR) && (options & LYXP_SCNODE_ALL)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008365 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008366 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008367 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008368 }
8369
8370 /* ('or' AndExpr)* */
8371 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008372 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
aPiecek8b0cc152021-05-31 16:40:31 +02008373 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, ((options & LYXP_SKIP_EXPR) || set->val.bln ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008374 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008375 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008376
8377 /* lazy evaluation */
aPiecek8b0cc152021-05-31 16:40:31 +02008378 if ((options & LYXP_SKIP_EXPR) || ((set->type == LYXP_SET_BOOLEAN) && set->val.bln)) {
8379 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options | LYXP_SKIP_EXPR);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008380 LY_CHECK_GOTO(rc, cleanup);
8381 continue;
8382 }
8383
8384 set_fill_set(&set2, &orig_set);
8385 /* expr_type cound have been LYXP_EXPR_NONE in all these later calls (except for the first one),
8386 * but it does not matter */
Michal Vasko004d3152020-06-11 19:59:22 +02008387 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008388 LY_CHECK_GOTO(rc, cleanup);
8389
8390 /* eval - just get boolean value actually */
8391 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008392 set_scnode_clear_ctx(&set2, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008393 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008394 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008395 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008396 set_fill_set(set, &set2);
8397 }
8398 }
8399
8400cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008401 lyxp_set_free_content(&orig_set);
8402 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008403 return rc;
8404}
8405
8406/**
Michal Vasko004d3152020-06-11 19:59:22 +02008407 * @brief Decide what expression is at the pointer @p tok_idx and evaluate it accordingly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008408 *
8409 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008410 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008411 * @param[in] etype Expression type to evaluate.
aPiecek8b0cc152021-05-31 16:40:31 +02008412 * @param[in,out] set Context and result set.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008413 * @param[in] options XPath options.
8414 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8415 */
8416static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008417eval_expr_select(const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_expr_type etype, struct lyxp_set *set,
8418 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008419{
8420 uint16_t i, count;
8421 enum lyxp_expr_type next_etype;
8422 LY_ERR rc;
8423
8424 /* process operator repeats */
Michal Vasko004d3152020-06-11 19:59:22 +02008425 if (!exp->repeat[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008426 next_etype = LYXP_EXPR_NONE;
8427 } else {
8428 /* find etype repeat */
Radek Krejci1e008d22020-08-17 11:37:37 +02008429 for (i = 0; exp->repeat[*tok_idx][i] > etype; ++i) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02008430
8431 /* select one-priority lower because etype expression called us */
8432 if (i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008433 next_etype = exp->repeat[*tok_idx][i - 1];
Michal Vasko03ff5a72019-09-11 13:49:33 +02008434 /* count repeats for that expression */
Radek Krejci1e008d22020-08-17 11:37:37 +02008435 for (count = 0; i && exp->repeat[*tok_idx][i - 1] == next_etype; ++count, --i) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02008436 } else {
8437 next_etype = LYXP_EXPR_NONE;
8438 }
8439 }
8440
8441 /* decide what expression are we parsing based on the repeat */
8442 switch (next_etype) {
8443 case LYXP_EXPR_OR:
Michal Vasko004d3152020-06-11 19:59:22 +02008444 rc = eval_or_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008445 break;
8446 case LYXP_EXPR_AND:
Michal Vasko004d3152020-06-11 19:59:22 +02008447 rc = eval_and_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008448 break;
8449 case LYXP_EXPR_EQUALITY:
Michal Vasko004d3152020-06-11 19:59:22 +02008450 rc = eval_equality_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008451 break;
8452 case LYXP_EXPR_RELATIONAL:
Michal Vasko004d3152020-06-11 19:59:22 +02008453 rc = eval_relational_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008454 break;
8455 case LYXP_EXPR_ADDITIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02008456 rc = eval_additive_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008457 break;
8458 case LYXP_EXPR_MULTIPLICATIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02008459 rc = eval_multiplicative_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008460 break;
8461 case LYXP_EXPR_UNARY:
Michal Vasko004d3152020-06-11 19:59:22 +02008462 rc = eval_unary_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008463 break;
8464 case LYXP_EXPR_UNION:
Michal Vasko004d3152020-06-11 19:59:22 +02008465 rc = eval_union_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008466 break;
8467 case LYXP_EXPR_NONE:
Michal Vasko004d3152020-06-11 19:59:22 +02008468 rc = eval_path_expr(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008469 break;
8470 default:
8471 LOGINT_RET(set->ctx);
8472 }
8473
8474 return rc;
8475}
8476
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008477/**
8478 * @brief Get root type.
8479 *
8480 * @param[in] ctx_node Context node.
8481 * @param[in] ctx_scnode Schema context node.
8482 * @param[in] options XPath options.
8483 * @return Root type.
8484 */
8485static enum lyxp_node_type
Radek Krejci1deb5be2020-08-26 16:43:36 +02008486lyxp_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 +01008487{
Michal Vasko6b26e742020-07-17 15:02:10 +02008488 const struct lysc_node *op;
8489
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008490 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008491 /* schema */
Radek Krejci1e008d22020-08-17 11:37:37 +02008492 for (op = ctx_scnode; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02008493
8494 if (op || (options & LYXP_SCNODE)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008495 /* general root that can access everything */
8496 return LYXP_NODE_ROOT;
8497 } else if (!ctx_scnode || (ctx_scnode->flags & LYS_CONFIG_W)) {
8498 /* root context node can access only config data (because we said so, it is unspecified) */
8499 return LYXP_NODE_ROOT_CONFIG;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008500 }
Michal Vasko6b26e742020-07-17 15:02:10 +02008501 return LYXP_NODE_ROOT;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008502 }
8503
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008504 /* data */
Michal Vasko6b26e742020-07-17 15:02:10 +02008505 op = ctx_node ? ctx_node->schema : NULL;
Michal Vaskod989ba02020-08-24 10:59:24 +02008506 for ( ; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02008507
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008508 if (op || !(options & LYXP_SCHEMA)) {
8509 /* general root that can access everything */
8510 return LYXP_NODE_ROOT;
Christian Hoppsb6ecaea2021-02-06 09:45:38 -05008511 } else if (!ctx_node || !ctx_node->schema || (ctx_node->schema->flags & LYS_CONFIG_W)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008512 /* root context node can access only config data (because we said so, it is unspecified) */
8513 return LYXP_NODE_ROOT_CONFIG;
8514 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008515 return LYXP_NODE_ROOT;
8516}
8517
Michal Vasko03ff5a72019-09-11 13:49:33 +02008518LY_ERR
Michal Vasko400e9672021-01-11 13:39:17 +01008519lyxp_eval(const struct ly_ctx *ctx, const struct lyxp_expr *exp, const struct lys_module *cur_mod,
Radek Krejci8df109d2021-04-23 12:19:08 +02008520 LY_VALUE_FORMAT format, void *prefix_data, const struct lyd_node *ctx_node, const struct lyd_node *tree,
Michal Vasko400e9672021-01-11 13:39:17 +01008521 struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008522{
Michal Vasko004d3152020-06-11 19:59:22 +02008523 uint16_t tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008524 LY_ERR rc;
8525
Michal Vasko400e9672021-01-11 13:39:17 +01008526 LY_CHECK_ARG_RET(ctx, ctx, exp, set, LY_EINVAL);
Radek Krejci8df109d2021-04-23 12:19:08 +02008527 if (!cur_mod && ((format == LY_VALUE_SCHEMA) || (format == LY_VALUE_SCHEMA_RESOLVED))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008528 LOGARG(NULL, "Current module must be set if schema format is used.");
8529 return LY_EINVAL;
Michal Vasko004d3152020-06-11 19:59:22 +02008530 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008531
Michal Vaskod3bb12f2020-12-04 14:33:09 +01008532 if (tree) {
8533 /* adjust the pointer to be the first top-level sibling */
8534 while (tree->parent) {
8535 tree = lyd_parent(tree);
8536 }
8537 tree = lyd_first_sibling(tree);
8538 }
8539
Michal Vasko03ff5a72019-09-11 13:49:33 +02008540 /* prepare set for evaluation */
Michal Vasko03ff5a72019-09-11 13:49:33 +02008541 memset(set, 0, sizeof *set);
Michal Vaskod3678892020-05-21 10:06:58 +02008542 set->type = LYXP_SET_NODE_SET;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008543 set->root_type = lyxp_get_root_type(ctx_node, NULL, options);
8544 set_insert_node(set, (struct lyd_node *)ctx_node, 0, ctx_node ? LYXP_NODE_ELEM : set->root_type, 0);
8545
Michal Vasko400e9672021-01-11 13:39:17 +01008546 set->ctx = (struct ly_ctx *)ctx;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008547 set->cur_node = ctx_node;
8548 for (set->context_op = ctx_node ? ctx_node->schema : NULL;
Radek Krejci0f969882020-08-21 16:56:47 +02008549 set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF));
8550 set->context_op = set->context_op->parent) {}
Michal Vaskof03ed032020-03-04 13:31:44 +01008551 set->tree = tree;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008552 set->cur_mod = cur_mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008553 set->format = format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008554 set->prefix_data = prefix_data;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008555
Radek Krejciddace2c2021-01-08 11:30:56 +01008556 LOG_LOCSET(NULL, set->cur_node, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01008557
Michal Vasko03ff5a72019-09-11 13:49:33 +02008558 /* evaluate */
Michal Vasko004d3152020-06-11 19:59:22 +02008559 rc = eval_expr_select(exp, &tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008560 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02008561 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008562 }
8563
Radek Krejciddace2c2021-01-08 11:30:56 +01008564 LOG_LOCBACK(0, 1, 0, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008565 return rc;
8566}
8567
8568#if 0
8569
8570/* full xml printing of set elements, not used currently */
8571
8572void
8573lyxp_set_print_xml(FILE *f, struct lyxp_set *set)
8574{
8575 uint32_t i;
8576 char *str_num;
8577 struct lyout out;
8578
8579 memset(&out, 0, sizeof out);
8580
8581 out.type = LYOUT_STREAM;
8582 out.method.f = f;
8583
8584 switch (set->type) {
8585 case LYXP_SET_EMPTY:
Michal Vasko5233e962020-08-14 14:26:20 +02008586 ly_print_(&out, "Empty XPath set\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008587 break;
8588 case LYXP_SET_BOOLEAN:
Michal Vasko5233e962020-08-14 14:26:20 +02008589 ly_print_(&out, "Boolean XPath set:\n");
8590 ly_print_(&out, "%s\n\n", set->value.bool ? "true" : "false");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008591 break;
8592 case LYXP_SET_STRING:
Michal Vasko5233e962020-08-14 14:26:20 +02008593 ly_print_(&out, "String XPath set:\n");
8594 ly_print_(&out, "\"%s\"\n\n", set->value.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008595 break;
8596 case LYXP_SET_NUMBER:
Michal Vasko5233e962020-08-14 14:26:20 +02008597 ly_print_(&out, "Number XPath set:\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008598
8599 if (isnan(set->value.num)) {
8600 str_num = strdup("NaN");
8601 } else if ((set->value.num == 0) || (set->value.num == -0.0f)) {
8602 str_num = strdup("0");
8603 } else if (isinf(set->value.num) && !signbit(set->value.num)) {
8604 str_num = strdup("Infinity");
8605 } else if (isinf(set->value.num) && signbit(set->value.num)) {
8606 str_num = strdup("-Infinity");
8607 } else if ((long long)set->value.num == set->value.num) {
8608 if (asprintf(&str_num, "%lld", (long long)set->value.num) == -1) {
8609 str_num = NULL;
8610 }
8611 } else {
8612 if (asprintf(&str_num, "%03.1Lf", set->value.num) == -1) {
8613 str_num = NULL;
8614 }
8615 }
8616 if (!str_num) {
8617 LOGMEM;
8618 return;
8619 }
Michal Vasko5233e962020-08-14 14:26:20 +02008620 ly_print_(&out, "%s\n\n", str_num);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008621 free(str_num);
8622 break;
8623 case LYXP_SET_NODE_SET:
Michal Vasko5233e962020-08-14 14:26:20 +02008624 ly_print_(&out, "Node XPath set:\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008625
8626 for (i = 0; i < set->used; ++i) {
Michal Vasko5233e962020-08-14 14:26:20 +02008627 ly_print_(&out, "%d. ", i + 1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008628 switch (set->node_type[i]) {
8629 case LYXP_NODE_ROOT_ALL:
Michal Vasko5233e962020-08-14 14:26:20 +02008630 ly_print_(&out, "ROOT all\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008631 break;
8632 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko5233e962020-08-14 14:26:20 +02008633 ly_print_(&out, "ROOT config\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008634 break;
8635 case LYXP_NODE_ROOT_STATE:
Michal Vasko5233e962020-08-14 14:26:20 +02008636 ly_print_(&out, "ROOT state\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008637 break;
8638 case LYXP_NODE_ROOT_NOTIF:
Michal Vasko5233e962020-08-14 14:26:20 +02008639 ly_print_(&out, "ROOT notification \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008640 break;
8641 case LYXP_NODE_ROOT_RPC:
Michal Vasko5233e962020-08-14 14:26:20 +02008642 ly_print_(&out, "ROOT rpc \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008643 break;
8644 case LYXP_NODE_ROOT_OUTPUT:
Michal Vasko5233e962020-08-14 14:26:20 +02008645 ly_print_(&out, "ROOT output \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008646 break;
8647 case LYXP_NODE_ELEM:
Michal Vasko5233e962020-08-14 14:26:20 +02008648 ly_print_(&out, "ELEM \"%s\"\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008649 xml_print_node(&out, 1, set->value.nodes[i], 1, LYP_FORMAT);
Michal Vasko5233e962020-08-14 14:26:20 +02008650 ly_print_(&out, "\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008651 break;
8652 case LYXP_NODE_TEXT:
Michal Vasko5233e962020-08-14 14:26:20 +02008653 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 +02008654 break;
8655 case LYXP_NODE_ATTR:
Michal Vasko5233e962020-08-14 14:26:20 +02008656 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 +02008657 break;
8658 }
8659 }
8660 break;
8661 }
8662}
8663
8664#endif
8665
8666LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008667lyxp_set_cast(struct lyxp_set *set, enum lyxp_set_type target)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008668{
8669 long double num;
8670 char *str;
8671 LY_ERR rc;
8672
8673 if (!set || (set->type == target)) {
8674 return LY_SUCCESS;
8675 }
8676
8677 /* it's not possible to convert anything into a node set */
Michal Vaskod3678892020-05-21 10:06:58 +02008678 assert(target != LYXP_SET_NODE_SET);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008679
8680 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vaskod3678892020-05-21 10:06:58 +02008681 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008682 return LY_EINVAL;
8683 }
8684
8685 /* to STRING */
Michal Vaskod3678892020-05-21 10:06:58 +02008686 if ((target == LYXP_SET_STRING) || ((target == LYXP_SET_NUMBER) && (set->type == LYXP_SET_NODE_SET))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008687 switch (set->type) {
8688 case LYXP_SET_NUMBER:
8689 if (isnan(set->val.num)) {
8690 set->val.str = strdup("NaN");
8691 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8692 } else if ((set->val.num == 0) || (set->val.num == -0.0f)) {
8693 set->val.str = strdup("0");
8694 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8695 } else if (isinf(set->val.num) && !signbit(set->val.num)) {
8696 set->val.str = strdup("Infinity");
8697 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8698 } else if (isinf(set->val.num) && signbit(set->val.num)) {
8699 set->val.str = strdup("-Infinity");
8700 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8701 } else if ((long long)set->val.num == set->val.num) {
8702 if (asprintf(&str, "%lld", (long long)set->val.num) == -1) {
8703 LOGMEM_RET(set->ctx);
8704 }
8705 set->val.str = str;
8706 } else {
8707 if (asprintf(&str, "%03.1Lf", set->val.num) == -1) {
8708 LOGMEM_RET(set->ctx);
8709 }
8710 set->val.str = str;
8711 }
8712 break;
8713 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02008714 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008715 set->val.str = strdup("true");
8716 } else {
8717 set->val.str = strdup("false");
8718 }
8719 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM);
8720 break;
8721 case LYXP_SET_NODE_SET:
Michal Vasko03ff5a72019-09-11 13:49:33 +02008722 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008723 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008724
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008725 rc = cast_node_set_to_string(set, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008726 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02008727 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008728 set->val.str = str;
8729 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008730 default:
8731 LOGINT_RET(set->ctx);
8732 }
8733 set->type = LYXP_SET_STRING;
8734 }
8735
8736 /* to NUMBER */
8737 if (target == LYXP_SET_NUMBER) {
8738 switch (set->type) {
8739 case LYXP_SET_STRING:
8740 num = cast_string_to_number(set->val.str);
Michal Vaskod3678892020-05-21 10:06:58 +02008741 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008742 set->val.num = num;
8743 break;
8744 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02008745 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008746 set->val.num = 1;
8747 } else {
8748 set->val.num = 0;
8749 }
8750 break;
8751 default:
8752 LOGINT_RET(set->ctx);
8753 }
8754 set->type = LYXP_SET_NUMBER;
8755 }
8756
8757 /* to BOOLEAN */
8758 if (target == LYXP_SET_BOOLEAN) {
8759 switch (set->type) {
8760 case LYXP_SET_NUMBER:
8761 if ((set->val.num == 0) || (set->val.num == -0.0f) || isnan(set->val.num)) {
Michal Vasko004d3152020-06-11 19:59:22 +02008762 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008763 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02008764 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008765 }
8766 break;
8767 case LYXP_SET_STRING:
8768 if (set->val.str[0]) {
Michal Vaskod3678892020-05-21 10:06:58 +02008769 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008770 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008771 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02008772 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008773 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008774 }
8775 break;
8776 case LYXP_SET_NODE_SET:
Michal Vaskod3678892020-05-21 10:06:58 +02008777 if (set->used) {
8778 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008779 set->val.bln = 1;
Michal Vaskod3678892020-05-21 10:06:58 +02008780 } else {
8781 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008782 set->val.bln = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02008783 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008784 break;
8785 default:
8786 LOGINT_RET(set->ctx);
8787 }
8788 set->type = LYXP_SET_BOOLEAN;
8789 }
8790
Michal Vasko03ff5a72019-09-11 13:49:33 +02008791 return LY_SUCCESS;
8792}
8793
8794LY_ERR
Michal Vasko400e9672021-01-11 13:39:17 +01008795lyxp_atomize(const struct ly_ctx *ctx, const struct lyxp_expr *exp, const struct lys_module *cur_mod,
Radek Krejci8df109d2021-04-23 12:19:08 +02008796 LY_VALUE_FORMAT format, void *prefix_data, const struct lysc_node *ctx_scnode, struct lyxp_set *set,
Michal Vasko400e9672021-01-11 13:39:17 +01008797 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008798{
Radek Krejci2efc45b2020-12-22 16:25:44 +01008799 LY_ERR ret;
Michal Vasko004d3152020-06-11 19:59:22 +02008800 uint16_t tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008801
Michal Vasko400e9672021-01-11 13:39:17 +01008802 LY_CHECK_ARG_RET(ctx, ctx, exp, set, LY_EINVAL);
Radek Krejci8df109d2021-04-23 12:19:08 +02008803 if (!cur_mod && ((format == LY_VALUE_SCHEMA) || (format == LY_VALUE_SCHEMA_RESOLVED))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008804 LOGARG(NULL, "Current module must be set if schema format is used.");
8805 return LY_EINVAL;
Michal Vasko004d3152020-06-11 19:59:22 +02008806 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008807
8808 /* prepare set for evaluation */
Michal Vasko03ff5a72019-09-11 13:49:33 +02008809 memset(set, 0, sizeof *set);
8810 set->type = LYXP_SET_SCNODE_SET;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008811 set->root_type = lyxp_get_root_type(NULL, ctx_scnode, options);
8812 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, ctx_scnode, ctx_scnode ? LYXP_NODE_ELEM : set->root_type, NULL));
Radek Krejcif13b87b2020-12-01 22:02:17 +01008813 set->val.scnodes[0].in_ctx = LYXP_SET_SCNODE_START;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008814
Michal Vasko400e9672021-01-11 13:39:17 +01008815 set->ctx = (struct ly_ctx *)ctx;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008816 set->cur_scnode = ctx_scnode;
Michal Vasko6b26e742020-07-17 15:02:10 +02008817 for (set->context_op = ctx_scnode;
Radek Krejci0f969882020-08-21 16:56:47 +02008818 set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF));
8819 set->context_op = set->context_op->parent) {}
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008820 set->cur_mod = cur_mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008821 set->format = format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008822 set->prefix_data = prefix_data;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008823
Radek Krejciddace2c2021-01-08 11:30:56 +01008824 LOG_LOCSET(set->cur_scnode, NULL, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01008825
Michal Vasko03ff5a72019-09-11 13:49:33 +02008826 /* evaluate */
Radek Krejci2efc45b2020-12-22 16:25:44 +01008827 ret = eval_expr_select(exp, &tok_idx, 0, set, options);
8828
Radek Krejciddace2c2021-01-08 11:30:56 +01008829 LOG_LOCBACK(1, 0, 0, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +01008830 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008831}
Michal Vaskod43d71a2020-08-07 14:54:58 +02008832
8833API const char *
8834lyxp_get_expr(const struct lyxp_expr *path)
8835{
8836 if (!path) {
8837 return NULL;
8838 }
8839
8840 return path->expr;
8841}