blob: d65819363db83e3c5cab21ad332d84c3431e3ded [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 */
15#include <sys/cdefs.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010016
Radek Krejci535ea9f2020-05-29 16:01:05 +020017#include "xpath.h"
Radek Krejcib1646a92018-11-02 16:08:26 +010018
Radek Krejci535ea9f2020-05-29 16:01:05 +020019#include <assert.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010020#include <ctype.h>
Radek Krejci535ea9f2020-05-29 16:01:05 +020021#include <errno.h>
Radek Krejci535ea9f2020-05-29 16:01:05 +020022#include <math.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020023#include <stdint.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010024#include <stdio.h>
25#include <stdlib.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010026#include <string.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010027
Radek Krejci535ea9f2020-05-29 16:01:05 +020028#include "common.h"
Michal Vasko5aa44c02020-06-29 11:47:02 +020029#include "compat.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020030#include "context.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020031#include "dict.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020032#include "hash_table.h"
Radek Krejci47fab892020-11-05 17:02:41 +010033#include "out.h"
Radek Krejci7931b192020-06-25 17:05:03 +020034#include "parser_data.h"
Michal Vasko004d3152020-06-11 19:59:22 +020035#include "path.h"
Michal Vasko03ff5a72019-09-11 13:49:33 +020036#include "plugins_types.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020037#include "printer_data.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020038#include "schema_compile_node.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020039#include "tree.h"
Radek Krejci77114102021-03-10 15:21:57 +010040#include "tree_data.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020041#include "tree_data_internal.h"
Radek Krejci859a15a2021-03-05 20:56:59 +010042#include "tree_edit.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020043#include "tree_schema_internal.h"
44#include "xml.h"
Michal Vasko03ff5a72019-09-11 13:49:33 +020045
aPiecekbf968d92021-05-27 14:35:05 +020046static 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 +020047static LY_ERR eval_expr_select(const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_expr_type etype,
48 struct lyxp_set *set, uint32_t options);
Michal Vasko93923692021-05-07 15:28:02 +020049static LY_ERR moveto_resolve_model(const char **qname, uint16_t *qname_len, const struct lyxp_set *set,
50 const struct lysc_node *ctx_scnode, const struct lys_module **moveto_mod);
Michal Vasko03ff5a72019-09-11 13:49:33 +020051
52/**
53 * @brief Print the type of an XPath \p set.
54 *
55 * @param[in] set Set to use.
56 * @return Set type string.
57 */
58static const char *
59print_set_type(struct lyxp_set *set)
60{
61 switch (set->type) {
Michal Vasko03ff5a72019-09-11 13:49:33 +020062 case LYXP_SET_NODE_SET:
63 return "node set";
64 case LYXP_SET_SCNODE_SET:
65 return "schema node set";
66 case LYXP_SET_BOOLEAN:
67 return "boolean";
68 case LYXP_SET_NUMBER:
69 return "number";
70 case LYXP_SET_STRING:
71 return "string";
72 }
73
74 return NULL;
75}
76
Michal Vasko24cddf82020-06-01 08:17:01 +020077const char *
78lyxp_print_token(enum lyxp_token tok)
Michal Vasko03ff5a72019-09-11 13:49:33 +020079{
80 switch (tok) {
81 case LYXP_TOKEN_PAR1:
82 return "(";
83 case LYXP_TOKEN_PAR2:
84 return ")";
85 case LYXP_TOKEN_BRACK1:
86 return "[";
87 case LYXP_TOKEN_BRACK2:
88 return "]";
89 case LYXP_TOKEN_DOT:
90 return ".";
91 case LYXP_TOKEN_DDOT:
92 return "..";
93 case LYXP_TOKEN_AT:
94 return "@";
95 case LYXP_TOKEN_COMMA:
96 return ",";
97 case LYXP_TOKEN_NAMETEST:
98 return "NameTest";
99 case LYXP_TOKEN_NODETYPE:
100 return "NodeType";
101 case LYXP_TOKEN_FUNCNAME:
102 return "FunctionName";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200103 case LYXP_TOKEN_OPER_LOG:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200104 return "Operator(Logic)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200105 case LYXP_TOKEN_OPER_EQUAL:
106 return "Operator(Equal)";
107 case LYXP_TOKEN_OPER_NEQUAL:
108 return "Operator(Non-equal)";
109 case LYXP_TOKEN_OPER_COMP:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200110 return "Operator(Comparison)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200111 case LYXP_TOKEN_OPER_MATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200112 return "Operator(Math)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200113 case LYXP_TOKEN_OPER_UNI:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200114 return "Operator(Union)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200115 case LYXP_TOKEN_OPER_PATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200116 return "Operator(Path)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200117 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko14676352020-05-29 11:35:55 +0200118 return "Operator(Recursive Path)";
Michal Vasko03ff5a72019-09-11 13:49:33 +0200119 case LYXP_TOKEN_LITERAL:
120 return "Literal";
121 case LYXP_TOKEN_NUMBER:
122 return "Number";
123 default:
124 LOGINT(NULL);
125 return "";
126 }
127}
128
129/**
130 * @brief Print the whole expression \p exp to debug output.
131 *
132 * @param[in] exp Expression to use.
133 */
134static void
Michal Vasko40308e72020-10-20 16:38:40 +0200135print_expr_struct_debug(const struct lyxp_expr *exp)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200136{
Radek Krejcif13b87b2020-12-01 22:02:17 +0100137#define MSG_BUFFER_SIZE 128
138 char tmp[MSG_BUFFER_SIZE];
Michal Vasko1fdd8fa2021-01-08 09:21:45 +0100139 uint32_t i, j;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200140
Radek Krejci52b6d512020-10-12 12:33:17 +0200141 if (!exp || (ly_ll < LY_LLDBG)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200142 return;
143 }
144
145 LOGDBG(LY_LDGXPATH, "expression \"%s\":", exp->expr);
146 for (i = 0; i < exp->used; ++i) {
Michal Vasko24cddf82020-06-01 08:17:01 +0200147 sprintf(tmp, "\ttoken %s, in expression \"%.*s\"", lyxp_print_token(exp->tokens[i]), exp->tok_len[i],
Michal Vasko69730152020-10-09 16:30:07 +0200148 &exp->expr[exp->tok_pos[i]]);
Michal Vasko23049552021-03-04 15:57:44 +0100149 if (exp->repeat && exp->repeat[i]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200150 sprintf(tmp + strlen(tmp), " (repeat %d", exp->repeat[i][0]);
151 for (j = 1; exp->repeat[i][j]; ++j) {
152 sprintf(tmp + strlen(tmp), ", %d", exp->repeat[i][j]);
153 }
154 strcat(tmp, ")");
155 }
156 LOGDBG(LY_LDGXPATH, tmp);
157 }
Radek Krejcif13b87b2020-12-01 22:02:17 +0100158#undef MSG_BUFFER_SIZE
Michal Vasko03ff5a72019-09-11 13:49:33 +0200159}
160
161#ifndef NDEBUG
162
163/**
164 * @brief Print XPath set content to debug output.
165 *
166 * @param[in] set Set to print.
167 */
168static void
169print_set_debug(struct lyxp_set *set)
170{
171 uint32_t i;
172 char *str;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200173 struct lyxp_set_node *item;
174 struct lyxp_set_scnode *sitem;
175
Radek Krejci52b6d512020-10-12 12:33:17 +0200176 if (ly_ll < LY_LLDBG) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200177 return;
178 }
179
180 switch (set->type) {
181 case LYXP_SET_NODE_SET:
182 LOGDBG(LY_LDGXPATH, "set NODE SET:");
183 for (i = 0; i < set->used; ++i) {
184 item = &set->val.nodes[i];
185
186 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +0100187 case LYXP_NODE_NONE:
188 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): NONE", i + 1, item->pos);
189 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200190 case LYXP_NODE_ROOT:
191 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT", i + 1, item->pos);
192 break;
193 case LYXP_NODE_ROOT_CONFIG:
194 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT CONFIG", i + 1, item->pos);
195 break;
196 case LYXP_NODE_ELEM:
Michal Vasko9e685082021-01-29 14:49:09 +0100197 if ((item->node->schema->nodetype == LYS_LIST) && (lyd_child(item->node)->schema->nodetype == LYS_LEAF)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200198 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (1st child val: %s)", i + 1, item->pos,
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200199 item->node->schema->name, lyd_get_value(lyd_child(item->node)));
Michal Vasko9e685082021-01-29 14:49:09 +0100200 } else if (item->node->schema->nodetype == LYS_LEAFLIST) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200201 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (val: %s)", i + 1, item->pos,
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200202 item->node->schema->name, lyd_get_value(item->node));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200203 } else {
204 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s", i + 1, item->pos, item->node->schema->name);
205 }
206 break;
207 case LYXP_NODE_TEXT:
208 if (item->node->schema->nodetype & LYS_ANYDATA) {
209 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): TEXT <%s>", i + 1, item->pos,
Michal Vasko69730152020-10-09 16:30:07 +0200210 item->node->schema->nodetype == LYS_ANYXML ? "anyxml" : "anydata");
Michal Vasko03ff5a72019-09-11 13:49:33 +0200211 } else {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200212 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 +0200213 }
214 break;
Michal Vasko9f96a052020-03-10 09:41:45 +0100215 case LYXP_NODE_META:
216 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 +0200217 set->val.meta[i].meta->value);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200218 break;
219 }
220 }
221 break;
222
223 case LYXP_SET_SCNODE_SET:
224 LOGDBG(LY_LDGXPATH, "set SCNODE SET:");
225 for (i = 0; i < set->used; ++i) {
226 sitem = &set->val.scnodes[i];
227
228 switch (sitem->type) {
229 case LYXP_NODE_ROOT:
230 LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT", i + 1, sitem->in_ctx);
231 break;
232 case LYXP_NODE_ROOT_CONFIG:
233 LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT CONFIG", i + 1, sitem->in_ctx);
234 break;
235 case LYXP_NODE_ELEM:
236 LOGDBG(LY_LDGXPATH, "\t%d (%u): ELEM %s", i + 1, sitem->in_ctx, sitem->scnode->name);
237 break;
238 default:
239 LOGINT(NULL);
240 break;
241 }
242 }
243 break;
244
Michal Vasko03ff5a72019-09-11 13:49:33 +0200245 case LYXP_SET_BOOLEAN:
246 LOGDBG(LY_LDGXPATH, "set BOOLEAN");
Michal Vasko004d3152020-06-11 19:59:22 +0200247 LOGDBG(LY_LDGXPATH, "\t%s", (set->val.bln ? "true" : "false"));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200248 break;
249
250 case LYXP_SET_STRING:
251 LOGDBG(LY_LDGXPATH, "set STRING");
252 LOGDBG(LY_LDGXPATH, "\t%s", set->val.str);
253 break;
254
255 case LYXP_SET_NUMBER:
256 LOGDBG(LY_LDGXPATH, "set NUMBER");
257
258 if (isnan(set->val.num)) {
259 str = strdup("NaN");
260 } else if ((set->val.num == 0) || (set->val.num == -0.0f)) {
261 str = strdup("0");
262 } else if (isinf(set->val.num) && !signbit(set->val.num)) {
263 str = strdup("Infinity");
264 } else if (isinf(set->val.num) && signbit(set->val.num)) {
265 str = strdup("-Infinity");
266 } else if ((long long)set->val.num == set->val.num) {
267 if (asprintf(&str, "%lld", (long long)set->val.num) == -1) {
268 str = NULL;
269 }
270 } else {
271 if (asprintf(&str, "%03.1Lf", set->val.num) == -1) {
272 str = NULL;
273 }
274 }
275 LY_CHECK_ERR_RET(!str, LOGMEM(NULL), );
276
277 LOGDBG(LY_LDGXPATH, "\t%s", str);
278 free(str);
279 }
280}
281
282#endif
283
284/**
285 * @brief Realloc the string \p str.
286 *
287 * @param[in] ctx libyang context for logging.
288 * @param[in] needed How much free space is required.
289 * @param[in,out] str Pointer to the string to use.
290 * @param[in,out] used Used bytes in \p str.
291 * @param[in,out] size Allocated bytes in \p str.
292 * @return LY_ERR
293 */
294static LY_ERR
Michal Vasko52927e22020-03-16 17:26:14 +0100295cast_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 +0200296{
297 if (*size - *used < needed) {
298 do {
299 if ((UINT16_MAX - *size) < LYXP_STRING_CAST_SIZE_STEP) {
300 LOGERR(ctx, LY_EINVAL, "XPath string length limit (%u) reached.", UINT16_MAX);
301 return LY_EINVAL;
302 }
303 *size += LYXP_STRING_CAST_SIZE_STEP;
304 } while (*size - *used < needed);
305 *str = ly_realloc(*str, *size * sizeof(char));
306 LY_CHECK_ERR_RET(!(*str), LOGMEM(ctx), LY_EMEM);
307 }
308
309 return LY_SUCCESS;
310}
311
312/**
313 * @brief Cast nodes recursively to one string @p str.
314 *
315 * @param[in] node Node to cast.
316 * @param[in] fake_cont Whether to put the data into a "fake" container.
317 * @param[in] root_type Type of the XPath root.
318 * @param[in] indent Current indent.
319 * @param[in,out] str Resulting string.
320 * @param[in,out] used Used bytes in @p str.
321 * @param[in,out] size Allocated bytes in @p str.
322 * @return LY_ERR
323 */
324static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200325cast_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 +0200326 uint16_t *used, uint16_t *size)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200327{
Radek Krejci7f769d72020-07-11 23:13:56 +0200328 char *buf, *line, *ptr = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200329 const char *value_str;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200330 const struct lyd_node *child;
Michal Vasko60ea6352020-06-29 13:39:39 +0200331 struct lyd_node *tree;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200332 struct lyd_node_any *any;
333 LY_ERR rc;
334
335 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) {
336 return LY_SUCCESS;
337 }
338
339 if (fake_cont) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200340 rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200341 LY_CHECK_RET(rc);
342 strcpy(*str + (*used - 1), "\n");
343 ++(*used);
344
345 ++indent;
346 }
347
348 switch (node->schema->nodetype) {
349 case LYS_CONTAINER:
350 case LYS_LIST:
351 case LYS_RPC:
352 case LYS_NOTIF:
Michal Vaskob7be7a82020-08-20 09:09:04 +0200353 rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200354 LY_CHECK_RET(rc);
355 strcpy(*str + (*used - 1), "\n");
356 ++(*used);
357
Radek Krejcia1c1e542020-09-29 16:06:52 +0200358 for (child = lyd_child(node); child; child = child->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200359 rc = cast_string_recursive(child, 0, root_type, indent + 1, str, used, size);
360 LY_CHECK_RET(rc);
361 }
362
363 break;
364
365 case LYS_LEAF:
366 case LYS_LEAFLIST:
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200367 value_str = lyd_get_value(node);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200368
369 /* print indent */
Michal Vaskob7be7a82020-08-20 09:09:04 +0200370 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 +0200371 memset(*str + (*used - 1), ' ', indent * 2);
372 *used += indent * 2;
373
374 /* print value */
375 if (*used == 1) {
376 sprintf(*str + (*used - 1), "%s", value_str);
377 *used += strlen(value_str);
378 } else {
379 sprintf(*str + (*used - 1), "%s\n", value_str);
380 *used += strlen(value_str) + 1;
381 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200382
383 break;
384
385 case LYS_ANYXML:
386 case LYS_ANYDATA:
387 any = (struct lyd_node_any *)node;
388 if (!(void *)any->value.tree) {
389 /* no content */
390 buf = strdup("");
Michal Vaskob7be7a82020-08-20 09:09:04 +0200391 LY_CHECK_ERR_RET(!buf, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200392 } else {
Radek Krejci241f6b52020-05-21 18:13:49 +0200393 struct ly_out *out;
Radek Krejcia5bba312020-01-09 15:41:20 +0100394
Michal Vasko60ea6352020-06-29 13:39:39 +0200395 if (any->value_type == LYD_ANYDATA_LYB) {
396 /* try to parse it into a data tree */
Michal Vaskob7be7a82020-08-20 09:09:04 +0200397 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 +0200398 /* successfully parsed */
399 free(any->value.mem);
400 any->value.tree = tree;
401 any->value_type = LYD_ANYDATA_DATATREE;
402 }
Radek Krejci7931b192020-06-25 17:05:03 +0200403 /* error is covered by the following switch where LYD_ANYDATA_LYB causes failure */
Michal Vasko60ea6352020-06-29 13:39:39 +0200404 }
405
Michal Vasko03ff5a72019-09-11 13:49:33 +0200406 switch (any->value_type) {
407 case LYD_ANYDATA_STRING:
408 case LYD_ANYDATA_XML:
409 case LYD_ANYDATA_JSON:
410 buf = strdup(any->value.json);
Michal Vaskob7be7a82020-08-20 09:09:04 +0200411 LY_CHECK_ERR_RET(!buf, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200412 break;
413 case LYD_ANYDATA_DATATREE:
Radek Krejci84ce7b12020-06-11 17:28:25 +0200414 LY_CHECK_RET(ly_out_new_memory(&buf, 0, &out));
Michal Vasko3a41dff2020-07-15 14:30:28 +0200415 rc = lyd_print_all(out, any->value.tree, LYD_XML, 0);
Radek Krejci241f6b52020-05-21 18:13:49 +0200416 ly_out_free(out, NULL, 0);
Radek Krejcia5bba312020-01-09 15:41:20 +0100417 LY_CHECK_RET(rc < 0, -rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200418 break;
Michal Vasko60ea6352020-06-29 13:39:39 +0200419 case LYD_ANYDATA_LYB:
Michal Vaskob7be7a82020-08-20 09:09:04 +0200420 LOGERR(LYD_CTX(node), LY_EINVAL, "Cannot convert LYB anydata into string.");
Michal Vasko60ea6352020-06-29 13:39:39 +0200421 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200422 }
423 }
424
425 line = strtok_r(buf, "\n", &ptr);
426 do {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200427 rc = cast_string_realloc(LYD_CTX(node), indent * 2 + strlen(line) + 1, str, used, size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200428 if (rc != LY_SUCCESS) {
429 free(buf);
430 return rc;
431 }
432 memset(*str + (*used - 1), ' ', indent * 2);
433 *used += indent * 2;
434
435 strcpy(*str + (*used - 1), line);
436 *used += strlen(line);
437
438 strcpy(*str + (*used - 1), "\n");
439 *used += 1;
440 } while ((line = strtok_r(NULL, "\n", &ptr)));
441
442 free(buf);
443 break;
444
445 default:
Michal Vaskob7be7a82020-08-20 09:09:04 +0200446 LOGINT_RET(LYD_CTX(node));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200447 }
448
449 if (fake_cont) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200450 rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200451 LY_CHECK_RET(rc);
452 strcpy(*str + (*used - 1), "\n");
453 ++(*used);
454
455 --indent;
456 }
457
458 return LY_SUCCESS;
459}
460
461/**
462 * @brief Cast an element into a string.
463 *
464 * @param[in] node Node to cast.
465 * @param[in] fake_cont Whether to put the data into a "fake" container.
466 * @param[in] root_type Type of the XPath root.
467 * @param[out] str Element cast to dynamically-allocated string.
468 * @return LY_ERR
469 */
470static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200471cast_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 +0200472{
473 uint16_t used, size;
474 LY_ERR rc;
475
476 *str = malloc(LYXP_STRING_CAST_SIZE_START * sizeof(char));
Michal Vaskob7be7a82020-08-20 09:09:04 +0200477 LY_CHECK_ERR_RET(!*str, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200478 (*str)[0] = '\0';
479 used = 1;
480 size = LYXP_STRING_CAST_SIZE_START;
481
482 rc = cast_string_recursive(node, fake_cont, root_type, 0, str, &used, &size);
483 if (rc != LY_SUCCESS) {
484 free(*str);
485 return rc;
486 }
487
488 if (size > used) {
489 *str = ly_realloc(*str, used * sizeof(char));
Michal Vaskob7be7a82020-08-20 09:09:04 +0200490 LY_CHECK_ERR_RET(!*str, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200491 }
492 return LY_SUCCESS;
493}
494
495/**
496 * @brief Cast a LYXP_SET_NODE_SET set into a string.
497 * Context position aware.
498 *
499 * @param[in] set Set to cast.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200500 * @param[out] str Cast dynamically-allocated string.
501 * @return LY_ERR
502 */
503static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100504cast_node_set_to_string(struct lyxp_set *set, char **str)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200505{
Michal Vaskoaa677062021-03-09 13:52:53 +0100506 if (!set->used) {
507 *str = strdup("");
508 if (!*str) {
509 LOGMEM_RET(set->ctx);
510 }
511 return LY_SUCCESS;
512 }
513
Michal Vasko03ff5a72019-09-11 13:49:33 +0200514 switch (set->val.nodes[0].type) {
Michal Vasko2caefc12019-11-14 16:07:56 +0100515 case LYXP_NODE_NONE:
516 /* invalid */
517 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200518 case LYXP_NODE_ROOT:
519 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100520 return cast_string_elem(set->val.nodes[0].node, 1, set->root_type, str);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200521 case LYXP_NODE_ELEM:
522 case LYXP_NODE_TEXT:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100523 return cast_string_elem(set->val.nodes[0].node, 0, set->root_type, str);
Michal Vasko9f96a052020-03-10 09:41:45 +0100524 case LYXP_NODE_META:
Radek Krejci6d5ba0c2021-04-26 07:49:59 +0200525 *str = strdup(lyd_get_meta_value(set->val.meta[0].meta));
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200526 if (!*str) {
527 LOGMEM_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200528 }
529 return LY_SUCCESS;
530 }
531
532 LOGINT_RET(set->ctx);
533}
534
535/**
536 * @brief Cast a string into an XPath number.
537 *
538 * @param[in] str String to use.
539 * @return Cast number.
540 */
541static long double
542cast_string_to_number(const char *str)
543{
544 long double num;
545 char *ptr;
546
547 errno = 0;
548 num = strtold(str, &ptr);
549 if (errno || *ptr) {
550 num = NAN;
551 }
552 return num;
553}
554
555/**
556 * @brief Callback for checking value equality.
557 *
Michal Vasko62524a92021-02-26 10:08:50 +0100558 * Implementation of ::lyht_value_equal_cb.
Radek Krejci857189e2020-09-01 13:26:36 +0200559 *
Michal Vasko03ff5a72019-09-11 13:49:33 +0200560 * @param[in] val1_p First value.
561 * @param[in] val2_p Second value.
562 * @param[in] mod Whether hash table is being modified.
563 * @param[in] cb_data Callback data.
Radek Krejci857189e2020-09-01 13:26:36 +0200564 * @return Boolean value whether values are equal or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200565 */
Radek Krejci857189e2020-09-01 13:26:36 +0200566static ly_bool
567set_values_equal_cb(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void *UNUSED(cb_data))
Michal Vasko03ff5a72019-09-11 13:49:33 +0200568{
569 struct lyxp_set_hash_node *val1, *val2;
570
571 val1 = (struct lyxp_set_hash_node *)val1_p;
572 val2 = (struct lyxp_set_hash_node *)val2_p;
573
574 if ((val1->node == val2->node) && (val1->type == val2->type)) {
575 return 1;
576 }
577
578 return 0;
579}
580
581/**
582 * @brief Insert node and its hash into set.
583 *
584 * @param[in] set et to insert to.
585 * @param[in] node Node with hash.
586 * @param[in] type Node type.
587 */
588static void
589set_insert_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type)
590{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200591 LY_ERR r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200592 uint32_t i, hash;
593 struct lyxp_set_hash_node hnode;
594
595 if (!set->ht && (set->used >= LYD_HT_MIN_ITEMS)) {
596 /* create hash table and add all the nodes */
597 set->ht = lyht_new(1, sizeof(struct lyxp_set_hash_node), set_values_equal_cb, NULL, 1);
598 for (i = 0; i < set->used; ++i) {
599 hnode.node = set->val.nodes[i].node;
600 hnode.type = set->val.nodes[i].type;
601
602 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
603 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
604 hash = dict_hash_multi(hash, NULL, 0);
605
606 r = lyht_insert(set->ht, &hnode, hash, NULL);
607 assert(!r);
608 (void)r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200609
Michal Vasko9d6befd2019-12-11 14:56:56 +0100610 if (hnode.node == node) {
611 /* it was just added, do not add it twice */
612 node = NULL;
613 }
614 }
615 }
616
617 if (set->ht && node) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200618 /* add the new node into hash table */
619 hnode.node = node;
620 hnode.type = type;
621
622 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
623 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
624 hash = dict_hash_multi(hash, NULL, 0);
625
626 r = lyht_insert(set->ht, &hnode, hash, NULL);
627 assert(!r);
628 (void)r;
629 }
630}
631
632/**
633 * @brief Remove node and its hash from set.
634 *
635 * @param[in] set Set to remove from.
636 * @param[in] node Node to remove.
637 * @param[in] type Node type.
638 */
639static void
640set_remove_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type)
641{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200642 LY_ERR r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200643 struct lyxp_set_hash_node hnode;
644 uint32_t hash;
645
646 if (set->ht) {
647 hnode.node = node;
648 hnode.type = type;
649
650 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
651 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
652 hash = dict_hash_multi(hash, NULL, 0);
653
654 r = lyht_remove(set->ht, &hnode, hash);
655 assert(!r);
656 (void)r;
657
658 if (!set->ht->used) {
659 lyht_free(set->ht);
660 set->ht = NULL;
661 }
662 }
663}
664
665/**
666 * @brief Check whether node is in set based on its hash.
667 *
668 * @param[in] set Set to search in.
669 * @param[in] node Node to search for.
670 * @param[in] type Node type.
671 * @param[in] skip_idx Index in @p set to skip.
672 * @return LY_ERR
673 */
674static LY_ERR
675set_dup_node_hash_check(const struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type, int skip_idx)
676{
677 struct lyxp_set_hash_node hnode, *match_p;
678 uint32_t hash;
679
680 hnode.node = node;
681 hnode.type = type;
682
683 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
684 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
685 hash = dict_hash_multi(hash, NULL, 0);
686
687 if (!lyht_find(set->ht, &hnode, hash, (void **)&match_p)) {
688 if ((skip_idx > -1) && (set->val.nodes[skip_idx].node == match_p->node) && (set->val.nodes[skip_idx].type == match_p->type)) {
689 /* we found it on the index that should be skipped, find another */
690 hnode = *match_p;
691 if (lyht_find_next(set->ht, &hnode, hash, (void **)&match_p)) {
692 /* none other found */
693 return LY_SUCCESS;
694 }
695 }
696
697 return LY_EEXIST;
698 }
699
700 /* not found */
701 return LY_SUCCESS;
702}
703
Michal Vaskod3678892020-05-21 10:06:58 +0200704void
705lyxp_set_free_content(struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200706{
707 if (!set) {
708 return;
709 }
710
711 if (set->type == LYXP_SET_NODE_SET) {
712 free(set->val.nodes);
713 lyht_free(set->ht);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200714 } else if (set->type == LYXP_SET_SCNODE_SET) {
715 free(set->val.scnodes);
Michal Vaskod3678892020-05-21 10:06:58 +0200716 lyht_free(set->ht);
717 } else {
718 if (set->type == LYXP_SET_STRING) {
719 free(set->val.str);
720 }
721 set->type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200722 }
Michal Vaskod3678892020-05-21 10:06:58 +0200723
724 set->val.nodes = NULL;
725 set->used = 0;
726 set->size = 0;
727 set->ht = NULL;
728 set->ctx_pos = 0;
729 set->ctx_pos = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200730}
731
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100732/**
733 * @brief Free dynamically-allocated set.
734 *
735 * @param[in] set Set to free.
736 */
737static void
Michal Vasko03ff5a72019-09-11 13:49:33 +0200738lyxp_set_free(struct lyxp_set *set)
739{
740 if (!set) {
741 return;
742 }
743
Michal Vaskod3678892020-05-21 10:06:58 +0200744 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200745 free(set);
746}
747
748/**
749 * @brief Initialize set context.
750 *
751 * @param[in] new Set to initialize.
752 * @param[in] set Arbitrary initialized set.
753 */
754static void
Michal Vasko4c7763f2020-07-27 17:40:37 +0200755set_init(struct lyxp_set *new, const struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200756{
757 memset(new, 0, sizeof *new);
Michal Vasko02a77382019-09-12 11:47:35 +0200758 if (set) {
759 new->ctx = set->ctx;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200760 new->cur_node = set->cur_node;
Michal Vasko588112f2019-12-10 14:51:53 +0100761 new->root_type = set->root_type;
Michal Vasko6b26e742020-07-17 15:02:10 +0200762 new->context_op = set->context_op;
Michal Vaskof03ed032020-03-04 13:31:44 +0100763 new->tree = set->tree;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200764 new->cur_mod = set->cur_mod;
Michal Vasko02a77382019-09-12 11:47:35 +0200765 new->format = set->format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200766 new->prefix_data = set->prefix_data;
Michal Vasko02a77382019-09-12 11:47:35 +0200767 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200768}
769
770/**
771 * @brief Create a deep copy of a set.
772 *
773 * @param[in] set Set to copy.
774 * @return Copy of @p set.
775 */
776static struct lyxp_set *
777set_copy(struct lyxp_set *set)
778{
779 struct lyxp_set *ret;
Michal Vasko1fdd8fa2021-01-08 09:21:45 +0100780 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200781
782 if (!set) {
783 return NULL;
784 }
785
786 ret = malloc(sizeof *ret);
787 LY_CHECK_ERR_RET(!ret, LOGMEM(set->ctx), NULL);
788 set_init(ret, set);
789
790 if (set->type == LYXP_SET_SCNODE_SET) {
791 ret->type = set->type;
792
793 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100794 if ((set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) ||
795 (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +0200796 uint32_t idx;
797 LY_CHECK_ERR_RET(lyxp_set_scnode_insert_node(ret, set->val.scnodes[i].scnode, set->val.scnodes[i].type, &idx),
798 lyxp_set_free(ret), NULL);
Michal Vasko3f27c522020-01-06 08:37:49 +0100799 /* coverity seems to think scnodes can be NULL */
Radek Krejciaa6b53f2020-08-27 15:19:03 +0200800 if (!ret->val.scnodes) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200801 lyxp_set_free(ret);
802 return NULL;
803 }
Michal Vaskoba716542019-12-16 10:01:58 +0100804 ret->val.scnodes[idx].in_ctx = set->val.scnodes[i].in_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200805 }
806 }
807 } else if (set->type == LYXP_SET_NODE_SET) {
808 ret->type = set->type;
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
813 ret->used = ret->size = set->used;
814 ret->ctx_pos = set->ctx_pos;
815 ret->ctx_size = set->ctx_size;
Michal Vasko4a04e542021-02-01 08:58:30 +0100816 if (set->ht) {
817 ret->ht = lyht_dup(set->ht);
818 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200819 } else {
Radek Krejci0f969882020-08-21 16:56:47 +0200820 memcpy(ret, set, sizeof *ret);
821 if (set->type == LYXP_SET_STRING) {
822 ret->val.str = strdup(set->val.str);
823 LY_CHECK_ERR_RET(!ret->val.str, LOGMEM(set->ctx); free(ret), NULL);
824 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200825 }
826
827 return ret;
828}
829
830/**
831 * @brief Fill XPath set with a string. Any current data are disposed of.
832 *
833 * @param[in] set Set to fill.
834 * @param[in] string String to fill into \p set.
835 * @param[in] str_len Length of \p string. 0 is a valid value!
836 */
837static void
838set_fill_string(struct lyxp_set *set, const char *string, uint16_t str_len)
839{
Michal Vaskod3678892020-05-21 10:06:58 +0200840 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200841
842 set->type = LYXP_SET_STRING;
843 if ((str_len == 0) && (string[0] != '\0')) {
844 string = "";
845 }
846 set->val.str = strndup(string, str_len);
847}
848
849/**
850 * @brief Fill XPath set with a number. Any current data are disposed of.
851 *
852 * @param[in] set Set to fill.
853 * @param[in] number Number to fill into \p set.
854 */
855static void
856set_fill_number(struct lyxp_set *set, long double number)
857{
Michal Vaskod3678892020-05-21 10:06:58 +0200858 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200859
860 set->type = LYXP_SET_NUMBER;
861 set->val.num = number;
862}
863
864/**
865 * @brief Fill XPath set with a boolean. Any current data are disposed of.
866 *
867 * @param[in] set Set to fill.
868 * @param[in] boolean Boolean to fill into \p set.
869 */
870static void
Radek Krejci857189e2020-09-01 13:26:36 +0200871set_fill_boolean(struct lyxp_set *set, ly_bool boolean)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200872{
Michal Vaskod3678892020-05-21 10:06:58 +0200873 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200874
875 set->type = LYXP_SET_BOOLEAN;
Michal Vasko004d3152020-06-11 19:59:22 +0200876 set->val.bln = boolean;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200877}
878
879/**
880 * @brief Fill XPath set with the value from another set (deep assign).
881 * Any current data are disposed of.
882 *
883 * @param[in] trg Set to fill.
884 * @param[in] src Source set to copy into \p trg.
885 */
886static void
Michal Vasko4c7763f2020-07-27 17:40:37 +0200887set_fill_set(struct lyxp_set *trg, const struct lyxp_set *src)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200888{
889 if (!trg || !src) {
890 return;
891 }
892
893 if (trg->type == LYXP_SET_NODE_SET) {
894 free(trg->val.nodes);
895 } else if (trg->type == LYXP_SET_STRING) {
896 free(trg->val.str);
897 }
898 set_init(trg, src);
899
900 if (src->type == LYXP_SET_SCNODE_SET) {
901 trg->type = LYXP_SET_SCNODE_SET;
902 trg->used = src->used;
903 trg->size = src->used;
904
905 trg->val.scnodes = ly_realloc(trg->val.scnodes, trg->size * sizeof *trg->val.scnodes);
906 LY_CHECK_ERR_RET(!trg->val.scnodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), );
907 memcpy(trg->val.scnodes, src->val.scnodes, src->used * sizeof *src->val.scnodes);
908 } else if (src->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +0200909 set_fill_boolean(trg, src->val.bln);
Michal Vasko44f3d2c2020-08-24 09:49:38 +0200910 } else if (src->type == LYXP_SET_NUMBER) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200911 set_fill_number(trg, src->val.num);
912 } else if (src->type == LYXP_SET_STRING) {
913 set_fill_string(trg, src->val.str, strlen(src->val.str));
914 } else {
915 if (trg->type == LYXP_SET_NODE_SET) {
916 free(trg->val.nodes);
917 } else if (trg->type == LYXP_SET_STRING) {
918 free(trg->val.str);
919 }
920
Michal Vaskod3678892020-05-21 10:06:58 +0200921 assert(src->type == LYXP_SET_NODE_SET);
922
923 trg->type = LYXP_SET_NODE_SET;
924 trg->used = src->used;
925 trg->size = src->used;
926 trg->ctx_pos = src->ctx_pos;
927 trg->ctx_size = src->ctx_size;
928
929 trg->val.nodes = malloc(trg->used * sizeof *trg->val.nodes);
930 LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), );
931 memcpy(trg->val.nodes, src->val.nodes, src->used * sizeof *src->val.nodes);
932 if (src->ht) {
933 trg->ht = lyht_dup(src->ht);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200934 } else {
Michal Vaskod3678892020-05-21 10:06:58 +0200935 trg->ht = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200936 }
937 }
938}
939
940/**
941 * @brief Clear context of all schema nodes.
942 *
943 * @param[in] set Set to clear.
Michal Vasko1a09b212021-05-06 13:00:10 +0200944 * @param[in] new_ctx New context state for all the nodes currently in the context.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200945 */
946static void
Michal Vasko1a09b212021-05-06 13:00:10 +0200947set_scnode_clear_ctx(struct lyxp_set *set, int32_t new_ctx)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200948{
949 uint32_t i;
950
951 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100952 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko1a09b212021-05-06 13:00:10 +0200953 set->val.scnodes[i].in_ctx = new_ctx;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100954 } else if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START) {
955 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200956 }
957 }
958}
959
960/**
961 * @brief Remove a node from a set. Removing last node changes
962 * set into LYXP_SET_EMPTY. Context position aware.
963 *
964 * @param[in] set Set to use.
965 * @param[in] idx Index from @p set of the node to be removed.
966 */
967static void
968set_remove_node(struct lyxp_set *set, uint32_t idx)
969{
970 assert(set && (set->type == LYXP_SET_NODE_SET));
971 assert(idx < set->used);
972
973 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
974
975 --set->used;
976 if (set->used) {
977 memmove(&set->val.nodes[idx], &set->val.nodes[idx + 1],
978 (set->used - idx) * sizeof *set->val.nodes);
979 } else {
Michal Vaskod3678892020-05-21 10:06:58 +0200980 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200981 }
982}
983
984/**
Michal Vasko2caefc12019-11-14 16:07:56 +0100985 * @brief Remove a node from a set by setting its type to LYXP_NODE_NONE.
Michal Vasko57eab132019-09-24 11:46:26 +0200986 *
987 * @param[in] set Set to use.
988 * @param[in] idx Index from @p set of the node to be removed.
989 */
990static void
Michal Vasko2caefc12019-11-14 16:07:56 +0100991set_remove_node_none(struct lyxp_set *set, uint32_t idx)
Michal Vasko57eab132019-09-24 11:46:26 +0200992{
993 assert(set && (set->type == LYXP_SET_NODE_SET));
994 assert(idx < set->used);
995
Michal Vasko2caefc12019-11-14 16:07:56 +0100996 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
997 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
998 }
999 set->val.nodes[idx].type = LYXP_NODE_NONE;
Michal Vasko57eab132019-09-24 11:46:26 +02001000}
1001
1002/**
Michal Vasko2caefc12019-11-14 16:07:56 +01001003 * @brief Remove all LYXP_NODE_NONE nodes from a set. Removing last node changes
Michal Vasko57eab132019-09-24 11:46:26 +02001004 * set into LYXP_SET_EMPTY. Context position aware.
1005 *
1006 * @param[in] set Set to consolidate.
1007 */
1008static void
Michal Vasko2caefc12019-11-14 16:07:56 +01001009set_remove_nodes_none(struct lyxp_set *set)
Michal Vasko57eab132019-09-24 11:46:26 +02001010{
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01001011 uint32_t i, orig_used, end = 0;
1012 int64_t start;
Michal Vasko57eab132019-09-24 11:46:26 +02001013
Michal Vaskod3678892020-05-21 10:06:58 +02001014 assert(set);
Michal Vasko57eab132019-09-24 11:46:26 +02001015
1016 orig_used = set->used;
1017 set->used = 0;
Michal Vaskod989ba02020-08-24 10:59:24 +02001018 for (i = 0; i < orig_used; ) {
Michal Vasko57eab132019-09-24 11:46:26 +02001019 start = -1;
1020 do {
Michal Vasko2caefc12019-11-14 16:07:56 +01001021 if ((set->val.nodes[i].type != LYXP_NODE_NONE) && (start == -1)) {
Michal Vasko57eab132019-09-24 11:46:26 +02001022 start = i;
Michal Vasko2caefc12019-11-14 16:07:56 +01001023 } else if ((start > -1) && (set->val.nodes[i].type == LYXP_NODE_NONE)) {
Michal Vasko57eab132019-09-24 11:46:26 +02001024 end = i;
1025 ++i;
1026 break;
1027 }
1028
1029 ++i;
1030 if (i == orig_used) {
1031 end = i;
1032 }
1033 } while (i < orig_used);
1034
1035 if (start > -1) {
1036 /* move the whole chunk of valid nodes together */
1037 if (set->used != (unsigned)start) {
1038 memmove(&set->val.nodes[set->used], &set->val.nodes[start], (end - start) * sizeof *set->val.nodes);
1039 }
1040 set->used += end - start;
1041 }
1042 }
Michal Vasko57eab132019-09-24 11:46:26 +02001043}
1044
1045/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02001046 * @brief Check for duplicates in a node set.
1047 *
1048 * @param[in] set Set to check.
1049 * @param[in] node Node to look for in @p set.
1050 * @param[in] node_type Type of @p node.
1051 * @param[in] skip_idx Index from @p set to skip.
1052 * @return LY_ERR
1053 */
1054static LY_ERR
1055set_dup_node_check(const struct lyxp_set *set, const struct lyd_node *node, enum lyxp_node_type node_type, int skip_idx)
1056{
1057 uint32_t i;
1058
Michal Vasko2caefc12019-11-14 16:07:56 +01001059 if (set->ht && node) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001060 return set_dup_node_hash_check(set, (struct lyd_node *)node, node_type, skip_idx);
1061 }
1062
1063 for (i = 0; i < set->used; ++i) {
1064 if ((skip_idx > -1) && (i == (unsigned)skip_idx)) {
1065 continue;
1066 }
1067
1068 if ((set->val.nodes[i].node == node) && (set->val.nodes[i].type == node_type)) {
1069 return LY_EEXIST;
1070 }
1071 }
1072
1073 return LY_SUCCESS;
1074}
1075
Radek Krejci857189e2020-09-01 13:26:36 +02001076ly_bool
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001077lyxp_set_scnode_contains(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type, int skip_idx,
1078 uint32_t *index_p)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001079{
1080 uint32_t i;
1081
1082 for (i = 0; i < set->used; ++i) {
1083 if ((skip_idx > -1) && (i == (unsigned)skip_idx)) {
1084 continue;
1085 }
1086
1087 if ((set->val.scnodes[i].scnode == node) && (set->val.scnodes[i].type == node_type)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001088 if (index_p) {
1089 *index_p = i;
1090 }
1091 return 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001092 }
1093 }
1094
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001095 return 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001096}
1097
Michal Vaskoecd62de2019-11-13 12:35:11 +01001098void
1099lyxp_set_scnode_merge(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001100{
1101 uint32_t orig_used, i, j;
1102
Michal Vaskod3678892020-05-21 10:06:58 +02001103 assert((set1->type == LYXP_SET_SCNODE_SET) && (set2->type == LYXP_SET_SCNODE_SET));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001104
Michal Vaskod3678892020-05-21 10:06:58 +02001105 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001106 return;
1107 }
1108
Michal Vaskod3678892020-05-21 10:06:58 +02001109 if (!set1->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001110 memcpy(set1, set2, sizeof *set1);
1111 return;
1112 }
1113
1114 if (set1->used + set2->used > set1->size) {
1115 set1->size = set1->used + set2->used;
1116 set1->val.scnodes = ly_realloc(set1->val.scnodes, set1->size * sizeof *set1->val.scnodes);
1117 LY_CHECK_ERR_RET(!set1->val.scnodes, LOGMEM(set1->ctx), );
1118 }
1119
1120 orig_used = set1->used;
1121
1122 for (i = 0; i < set2->used; ++i) {
1123 for (j = 0; j < orig_used; ++j) {
1124 /* detect duplicities */
1125 if (set1->val.scnodes[j].scnode == set2->val.scnodes[i].scnode) {
1126 break;
1127 }
1128 }
1129
Michal Vasko0587bce2020-12-10 12:19:21 +01001130 if (j < orig_used) {
1131 /* node is there, but update its status if needed */
1132 if (set1->val.scnodes[j].in_ctx == LYXP_SET_SCNODE_START_USED) {
1133 set1->val.scnodes[j].in_ctx = set2->val.scnodes[i].in_ctx;
Michal Vasko1a09b212021-05-06 13:00:10 +02001134 } else if ((set1->val.scnodes[j].in_ctx == LYXP_SET_SCNODE_ATOM_NODE) &&
1135 (set2->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_VAL)) {
1136 set1->val.scnodes[j].in_ctx = set2->val.scnodes[i].in_ctx;
Michal Vasko0587bce2020-12-10 12:19:21 +01001137 }
1138 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001139 memcpy(&set1->val.scnodes[set1->used], &set2->val.scnodes[i], sizeof *set2->val.scnodes);
1140 ++set1->used;
1141 }
1142 }
1143
Michal Vaskod3678892020-05-21 10:06:58 +02001144 lyxp_set_free_content(set2);
1145 set2->type = LYXP_SET_SCNODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001146}
1147
1148/**
1149 * @brief Insert a node into a set. Context position aware.
1150 *
1151 * @param[in] set Set to use.
1152 * @param[in] node Node to insert to @p set.
1153 * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting.
1154 * @param[in] node_type Node type of @p node.
1155 * @param[in] idx Index in @p set to insert into.
1156 */
1157static void
1158set_insert_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx)
1159{
Michal Vaskod3678892020-05-21 10:06:58 +02001160 assert(set && (set->type == LYXP_SET_NODE_SET));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001161
Michal Vaskod3678892020-05-21 10:06:58 +02001162 if (!set->size) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001163 /* first item */
1164 if (idx) {
1165 /* no real harm done, but it is a bug */
1166 LOGINT(set->ctx);
1167 idx = 0;
1168 }
1169 set->val.nodes = malloc(LYXP_SET_SIZE_START * sizeof *set->val.nodes);
1170 LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), );
1171 set->type = LYXP_SET_NODE_SET;
1172 set->used = 0;
1173 set->size = LYXP_SET_SIZE_START;
1174 set->ctx_pos = 1;
1175 set->ctx_size = 1;
1176 set->ht = NULL;
1177 } else {
1178 /* not an empty set */
1179 if (set->used == set->size) {
1180
1181 /* set is full */
1182 set->val.nodes = ly_realloc(set->val.nodes, (set->size + LYXP_SET_SIZE_STEP) * sizeof *set->val.nodes);
1183 LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), );
1184 set->size += LYXP_SET_SIZE_STEP;
1185 }
1186
1187 if (idx > set->used) {
1188 LOGINT(set->ctx);
1189 idx = set->used;
1190 }
1191
1192 /* make space for the new node */
1193 if (idx < set->used) {
1194 memmove(&set->val.nodes[idx + 1], &set->val.nodes[idx], (set->used - idx) * sizeof *set->val.nodes);
1195 }
1196 }
1197
1198 /* finally assign the value */
1199 set->val.nodes[idx].node = (struct lyd_node *)node;
1200 set->val.nodes[idx].type = node_type;
1201 set->val.nodes[idx].pos = pos;
1202 ++set->used;
1203
Michal Vasko2caefc12019-11-14 16:07:56 +01001204 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1205 set_insert_node_hash(set, (struct lyd_node *)node, node_type);
1206 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001207}
1208
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001209LY_ERR
1210lyxp_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 +02001211{
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001212 uint32_t index;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001213
1214 assert(set->type == LYXP_SET_SCNODE_SET);
1215
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001216 if (lyxp_set_scnode_contains(set, node, node_type, -1, &index)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001217 set->val.scnodes[index].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001218 } else {
1219 if (set->used == set->size) {
1220 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 +02001221 LY_CHECK_ERR_RET(!set->val.scnodes, LOGMEM(set->ctx), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001222 set->size += LYXP_SET_SIZE_STEP;
1223 }
1224
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001225 index = set->used;
1226 set->val.scnodes[index].scnode = (struct lysc_node *)node;
1227 set->val.scnodes[index].type = node_type;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001228 set->val.scnodes[index].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001229 ++set->used;
1230 }
1231
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001232 if (index_p) {
1233 *index_p = index;
1234 }
1235
1236 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001237}
1238
1239/**
1240 * @brief Replace a node in a set with another. Context position aware.
1241 *
1242 * @param[in] set Set to use.
1243 * @param[in] node Node to insert to @p set.
1244 * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting.
1245 * @param[in] node_type Node type of @p node.
1246 * @param[in] idx Index in @p set of the node to replace.
1247 */
1248static void
1249set_replace_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx)
1250{
1251 assert(set && (idx < set->used));
1252
Michal Vasko2caefc12019-11-14 16:07:56 +01001253 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1254 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1255 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001256 set->val.nodes[idx].node = (struct lyd_node *)node;
1257 set->val.nodes[idx].type = node_type;
1258 set->val.nodes[idx].pos = pos;
Michal Vasko2caefc12019-11-14 16:07:56 +01001259 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1260 set_insert_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1261 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001262}
1263
1264/**
1265 * @brief Set all nodes with ctx 1 to a new unique context value.
1266 *
1267 * @param[in] set Set to modify.
1268 * @return New context value.
1269 */
Michal Vasko5c4e5892019-11-14 12:31:38 +01001270static int32_t
Michal Vasko03ff5a72019-09-11 13:49:33 +02001271set_scnode_new_in_ctx(struct lyxp_set *set)
1272{
Michal Vasko5c4e5892019-11-14 12:31:38 +01001273 uint32_t i;
1274 int32_t ret_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001275
1276 assert(set->type == LYXP_SET_SCNODE_SET);
1277
Radek Krejcif13b87b2020-12-01 22:02:17 +01001278 ret_ctx = LYXP_SET_SCNODE_ATOM_PRED_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001279retry:
1280 for (i = 0; i < set->used; ++i) {
1281 if (set->val.scnodes[i].in_ctx >= ret_ctx) {
1282 ret_ctx = set->val.scnodes[i].in_ctx + 1;
1283 goto retry;
1284 }
1285 }
1286 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001287 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001288 set->val.scnodes[i].in_ctx = ret_ctx;
1289 }
1290 }
1291
1292 return ret_ctx;
1293}
1294
1295/**
1296 * @brief Get unique @p node position in the data.
1297 *
1298 * @param[in] node Node to find.
1299 * @param[in] node_type Node type of @p node.
1300 * @param[in] root Root node.
1301 * @param[in] root_type Type of the XPath @p root node.
1302 * @param[in] prev Node that we think is before @p node in DFS from @p root. Can optionally
1303 * be used to increase efficiency and start the DFS from this node.
1304 * @param[in] prev_pos Node @p prev position. Optional, but must be set if @p prev is set.
1305 * @return Node position.
1306 */
1307static uint32_t
1308get_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 +02001309 enum lyxp_node_type root_type, const struct lyd_node **prev, uint32_t *prev_pos)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001310{
Michal Vasko56daf732020-08-10 10:57:18 +02001311 const struct lyd_node *elem = NULL, *top_sibling;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001312 uint32_t pos = 1;
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001313 ly_bool found = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001314
1315 assert(prev && prev_pos && !root->prev->next);
1316
1317 if ((node_type == LYXP_NODE_ROOT) || (node_type == LYXP_NODE_ROOT_CONFIG)) {
1318 return 0;
1319 }
1320
1321 if (*prev) {
1322 /* start from the previous element instead from the root */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001323 pos = *prev_pos;
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001324 for (top_sibling = *prev; top_sibling->parent; top_sibling = lyd_parent(top_sibling)) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001325 goto dfs_search;
1326 }
1327
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001328 LY_LIST_FOR(root, top_sibling) {
Michal Vasko56daf732020-08-10 10:57:18 +02001329 LYD_TREE_DFS_BEGIN(top_sibling, elem) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001330dfs_search:
Michal Vasko56daf732020-08-10 10:57:18 +02001331 if (*prev && !elem) {
1332 /* resume previous DFS */
1333 elem = LYD_TREE_DFS_next = (struct lyd_node *)*prev;
1334 LYD_TREE_DFS_continue = 0;
1335 }
1336
Michal Vasko03ff5a72019-09-11 13:49:33 +02001337 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (elem->schema->flags & LYS_CONFIG_R)) {
Michal Vasko56daf732020-08-10 10:57:18 +02001338 /* skip */
1339 LYD_TREE_DFS_continue = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001340 } else {
Michal Vasko56daf732020-08-10 10:57:18 +02001341 if (elem == node) {
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001342 found = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001343 break;
1344 }
Michal Vasko56daf732020-08-10 10:57:18 +02001345 ++pos;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001346 }
Michal Vasko56daf732020-08-10 10:57:18 +02001347
1348 LYD_TREE_DFS_END(top_sibling, elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001349 }
1350
1351 /* node found */
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001352 if (found) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001353 break;
1354 }
1355 }
1356
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001357 if (!found) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001358 if (!(*prev)) {
1359 /* we went from root and failed to find it, cannot be */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001360 LOGINT(LYD_CTX(node));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001361 return 0;
1362 } else {
Michal Vasko56daf732020-08-10 10:57:18 +02001363 /* start the search again from the beginning */
1364 *prev = root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001365
Michal Vasko56daf732020-08-10 10:57:18 +02001366 top_sibling = root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001367 pos = 1;
1368 goto dfs_search;
1369 }
1370 }
1371
1372 /* remember the last found node for next time */
1373 *prev = node;
1374 *prev_pos = pos;
1375
1376 return pos;
1377}
1378
1379/**
1380 * @brief Assign (fill) missing node positions.
1381 *
1382 * @param[in] set Set to fill positions in.
1383 * @param[in] root Context root node.
1384 * @param[in] root_type Context root type.
1385 * @return LY_ERR
1386 */
1387static LY_ERR
1388set_assign_pos(struct lyxp_set *set, const struct lyd_node *root, enum lyxp_node_type root_type)
1389{
1390 const struct lyd_node *prev = NULL, *tmp_node;
1391 uint32_t i, tmp_pos = 0;
1392
1393 for (i = 0; i < set->used; ++i) {
1394 if (!set->val.nodes[i].pos) {
1395 tmp_node = NULL;
1396 switch (set->val.nodes[i].type) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001397 case LYXP_NODE_META:
1398 tmp_node = set->val.meta[i].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001399 if (!tmp_node) {
1400 LOGINT_RET(root->schema->module->ctx);
1401 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01001402 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001403 case LYXP_NODE_ELEM:
1404 case LYXP_NODE_TEXT:
1405 if (!tmp_node) {
1406 tmp_node = set->val.nodes[i].node;
1407 }
1408 set->val.nodes[i].pos = get_node_pos(tmp_node, set->val.nodes[i].type, root, root_type, &prev, &tmp_pos);
1409 break;
1410 default:
1411 /* all roots have position 0 */
1412 break;
1413 }
1414 }
1415 }
1416
1417 return LY_SUCCESS;
1418}
1419
1420/**
Michal Vasko9f96a052020-03-10 09:41:45 +01001421 * @brief Get unique @p meta position in the parent metadata.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001422 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001423 * @param[in] meta Metadata to use.
1424 * @return Metadata position.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001425 */
1426static uint16_t
Michal Vasko9f96a052020-03-10 09:41:45 +01001427get_meta_pos(struct lyd_meta *meta)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001428{
1429 uint16_t pos = 0;
Michal Vasko9f96a052020-03-10 09:41:45 +01001430 struct lyd_meta *meta2;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001431
Michal Vasko9f96a052020-03-10 09:41:45 +01001432 for (meta2 = meta->parent->meta; meta2 && (meta2 != meta); meta2 = meta2->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001433 ++pos;
1434 }
1435
Michal Vasko9f96a052020-03-10 09:41:45 +01001436 assert(meta2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001437 return pos;
1438}
1439
1440/**
1441 * @brief Compare 2 nodes in respect to XPath document order.
1442 *
1443 * @param[in] item1 1st node.
1444 * @param[in] item2 2nd node.
1445 * @return If 1st > 2nd returns 1, 1st == 2nd returns 0, and 1st < 2nd returns -1.
1446 */
1447static int
1448set_sort_compare(struct lyxp_set_node *item1, struct lyxp_set_node *item2)
1449{
Michal Vasko9f96a052020-03-10 09:41:45 +01001450 uint32_t meta_pos1 = 0, meta_pos2 = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001451
1452 if (item1->pos < item2->pos) {
1453 return -1;
1454 }
1455
1456 if (item1->pos > item2->pos) {
1457 return 1;
1458 }
1459
1460 /* node positions are equal, the fun case */
1461
1462 /* 1st ELEM - == - 2nd TEXT, 1st TEXT - == - 2nd ELEM */
1463 /* special case since text nodes are actually saved as their parents */
1464 if ((item1->node == item2->node) && (item1->type != item2->type)) {
1465 if (item1->type == LYXP_NODE_ELEM) {
1466 assert(item2->type == LYXP_NODE_TEXT);
1467 return -1;
1468 } else {
1469 assert((item1->type == LYXP_NODE_TEXT) && (item2->type == LYXP_NODE_ELEM));
1470 return 1;
1471 }
1472 }
1473
Michal Vasko9f96a052020-03-10 09:41:45 +01001474 /* we need meta positions now */
1475 if (item1->type == LYXP_NODE_META) {
1476 meta_pos1 = get_meta_pos((struct lyd_meta *)item1->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001477 }
Michal Vasko9f96a052020-03-10 09:41:45 +01001478 if (item2->type == LYXP_NODE_META) {
1479 meta_pos2 = get_meta_pos((struct lyd_meta *)item2->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001480 }
1481
Michal Vasko9f96a052020-03-10 09:41:45 +01001482 /* 1st ROOT - 2nd ROOT, 1st ELEM - 2nd ELEM, 1st TEXT - 2nd TEXT, 1st META - =pos= - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001483 /* check for duplicates */
1484 if (item1->node == item2->node) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001485 assert((item1->type == item2->type) && ((item1->type != LYXP_NODE_META) || (meta_pos1 == meta_pos2)));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001486 return 0;
1487 }
1488
Michal Vasko9f96a052020-03-10 09:41:45 +01001489 /* 1st ELEM - 2nd TEXT, 1st ELEM - any pos - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001490 /* elem is always first, 2nd node is after it */
1491 if (item1->type == LYXP_NODE_ELEM) {
1492 assert(item2->type != LYXP_NODE_ELEM);
1493 return -1;
1494 }
1495
Michal Vasko9f96a052020-03-10 09:41:45 +01001496 /* 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 +02001497 /* 2nd is before 1st */
Michal Vasko69730152020-10-09 16:30:07 +02001498 if (((item1->type == LYXP_NODE_TEXT) &&
1499 ((item2->type == LYXP_NODE_ELEM) || (item2->type == LYXP_NODE_META))) ||
1500 ((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_ELEM)) ||
1501 (((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_META)) &&
1502 (meta_pos1 > meta_pos2))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001503 return 1;
1504 }
1505
Michal Vasko9f96a052020-03-10 09:41:45 +01001506 /* 1st META - any pos - 2nd TEXT, 1st META <pos< - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001507 /* 2nd is after 1st */
1508 return -1;
1509}
1510
1511/**
1512 * @brief Set cast for comparisons.
1513 *
1514 * @param[in] trg Target set to cast source into.
1515 * @param[in] src Source set.
1516 * @param[in] type Target set type.
1517 * @param[in] src_idx Source set node index.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001518 * @return LY_ERR
1519 */
1520static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001521set_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 +02001522{
1523 assert(src->type == LYXP_SET_NODE_SET);
1524
1525 set_init(trg, src);
1526
1527 /* insert node into target set */
1528 set_insert_node(trg, src->val.nodes[src_idx].node, src->val.nodes[src_idx].pos, src->val.nodes[src_idx].type, 0);
1529
1530 /* cast target set appropriately */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001531 return lyxp_set_cast(trg, type);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001532}
1533
Michal Vasko4c7763f2020-07-27 17:40:37 +02001534/**
1535 * @brief Set content canonization for comparisons.
1536 *
1537 * @param[in] trg Target set to put the canononized source into.
1538 * @param[in] src Source set.
1539 * @param[in] xp_node Source XPath node/meta to use for canonization.
1540 * @return LY_ERR
1541 */
1542static LY_ERR
1543set_comp_canonize(struct lyxp_set *trg, const struct lyxp_set *src, const struct lyxp_set_node *xp_node)
1544{
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001545 const struct lysc_type *type = NULL;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001546 struct lyd_value val;
1547 struct ly_err_item *err = NULL;
1548 char *str, *ptr;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001549 LY_ERR rc;
1550
1551 /* is there anything to canonize even? */
1552 if ((src->type == LYXP_SET_NUMBER) || (src->type == LYXP_SET_STRING)) {
1553 /* do we have a type to use for canonization? */
1554 if ((xp_node->type == LYXP_NODE_ELEM) && (xp_node->node->schema->nodetype & LYD_NODE_TERM)) {
1555 type = ((struct lyd_node_term *)xp_node->node)->value.realtype;
1556 } else if (xp_node->type == LYXP_NODE_META) {
1557 type = ((struct lyd_meta *)xp_node->node)->value.realtype;
1558 }
1559 }
1560 if (!type) {
1561 goto fill;
1562 }
1563
1564 if (src->type == LYXP_SET_NUMBER) {
1565 /* canonize number */
1566 if (asprintf(&str, "%Lf", src->val.num) == -1) {
1567 LOGMEM(src->ctx);
1568 return LY_EMEM;
1569 }
1570 } else {
1571 /* canonize string */
1572 str = strdup(src->val.str);
1573 }
1574
1575 /* ignore errors, the value may not satisfy schema constraints */
Radek Krejci0b013302021-03-29 15:22:32 +02001576 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 +01001577 LYD_HINT_DATA, xp_node->node->schema, &val, NULL, &err);
Michal Vasko4c7763f2020-07-27 17:40:37 +02001578 ly_err_free(err);
1579 if (rc) {
1580 /* invalid value */
Radek IÅ¡a48460222021-03-02 15:18:32 +01001581 /* function store automaticaly dealloc value when fail */
Michal Vasko4c7763f2020-07-27 17:40:37 +02001582 goto fill;
1583 }
1584
Michal Vasko4c7763f2020-07-27 17:40:37 +02001585 /* use the canonized value */
1586 set_init(trg, src);
1587 trg->type = src->type;
1588 if (src->type == LYXP_SET_NUMBER) {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001589 trg->val.num = strtold(type->plugin->print(src->ctx, &val, LY_VALUE_CANON, NULL, NULL, NULL), &ptr);
Michal Vasko4c7763f2020-07-27 17:40:37 +02001590 LY_CHECK_ERR_RET(ptr[0], LOGINT(src->ctx), LY_EINT);
1591 } else {
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02001592 trg->val.str = strdup(type->plugin->print(src->ctx, &val, LY_VALUE_CANON, NULL, NULL, NULL));
Michal Vasko4c7763f2020-07-27 17:40:37 +02001593 }
1594 type->plugin->free(src->ctx, &val);
1595 return LY_SUCCESS;
1596
1597fill:
1598 /* no canonization needed/possible */
1599 set_fill_set(trg, src);
1600 return LY_SUCCESS;
1601}
1602
Michal Vasko03ff5a72019-09-11 13:49:33 +02001603#ifndef NDEBUG
1604
1605/**
1606 * @brief Bubble sort @p set into XPath document order.
1607 * Context position aware. Unused in the 'Release' build target.
1608 *
1609 * @param[in] set Set to sort.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001610 * @return How many times the whole set was traversed - 1 (if set was sorted, returns 0).
1611 */
1612static int
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001613set_sort(struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001614{
1615 uint32_t i, j;
Radek Krejci1deb5be2020-08-26 16:43:36 +02001616 int ret = 0, cmp;
Radek Krejci857189e2020-09-01 13:26:36 +02001617 ly_bool inverted, change;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001618 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001619 struct lyxp_set_node item;
1620 struct lyxp_set_hash_node hnode;
1621 uint64_t hash;
1622
Michal Vasko3cf8fbf2020-05-27 15:21:21 +02001623 if ((set->type != LYXP_SET_NODE_SET) || (set->used < 2)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001624 return 0;
1625 }
1626
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001627 /* find first top-level node to be used as anchor for positions */
Michal Vasko9e685082021-01-29 14:49:09 +01001628 for (root = set->cur_node; root->parent; root = lyd_parent(root)) {}
Michal Vaskod989ba02020-08-24 10:59:24 +02001629 for ( ; root->prev->next; root = root->prev) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001630
1631 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001632 if (set_assign_pos(set, root, set->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001633 return -1;
1634 }
1635
1636 LOGDBG(LY_LDGXPATH, "SORT BEGIN");
1637 print_set_debug(set);
1638
1639 for (i = 0; i < set->used; ++i) {
1640 inverted = 0;
1641 change = 0;
1642
1643 for (j = 1; j < set->used - i; ++j) {
1644 /* compare node positions */
1645 if (inverted) {
1646 cmp = set_sort_compare(&set->val.nodes[j], &set->val.nodes[j - 1]);
1647 } else {
1648 cmp = set_sort_compare(&set->val.nodes[j - 1], &set->val.nodes[j]);
1649 }
1650
1651 /* swap if needed */
1652 if ((inverted && (cmp < 0)) || (!inverted && (cmp > 0))) {
1653 change = 1;
1654
1655 item = set->val.nodes[j - 1];
1656 set->val.nodes[j - 1] = set->val.nodes[j];
1657 set->val.nodes[j] = item;
1658 } else {
1659 /* whether node_pos1 should be smaller than node_pos2 or the other way around */
1660 inverted = !inverted;
1661 }
1662 }
1663
1664 ++ret;
1665
1666 if (!change) {
1667 break;
1668 }
1669 }
1670
1671 LOGDBG(LY_LDGXPATH, "SORT END %d", ret);
1672 print_set_debug(set);
1673
1674 /* check node hashes */
1675 if (set->used >= LYD_HT_MIN_ITEMS) {
1676 assert(set->ht);
1677 for (i = 0; i < set->used; ++i) {
1678 hnode.node = set->val.nodes[i].node;
1679 hnode.type = set->val.nodes[i].type;
1680
1681 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
1682 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
1683 hash = dict_hash_multi(hash, NULL, 0);
1684
1685 assert(!lyht_find(set->ht, &hnode, hash, NULL));
1686 }
1687 }
1688
1689 return ret - 1;
1690}
1691
1692/**
1693 * @brief Remove duplicate entries in a sorted node set.
1694 *
1695 * @param[in] set Sorted set to check.
1696 * @return LY_ERR (LY_EEXIST if some duplicates are found)
1697 */
1698static LY_ERR
1699set_sorted_dup_node_clean(struct lyxp_set *set)
1700{
1701 uint32_t i = 0;
1702 LY_ERR ret = LY_SUCCESS;
1703
1704 if (set->used > 1) {
1705 while (i < set->used - 1) {
Michal Vasko69730152020-10-09 16:30:07 +02001706 if ((set->val.nodes[i].node == set->val.nodes[i + 1].node) &&
1707 (set->val.nodes[i].type == set->val.nodes[i + 1].type)) {
Michal Vasko2caefc12019-11-14 16:07:56 +01001708 set_remove_node_none(set, i + 1);
Michal Vasko57eab132019-09-24 11:46:26 +02001709 ret = LY_EEXIST;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001710 }
Michal Vasko57eab132019-09-24 11:46:26 +02001711 ++i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001712 }
1713 }
1714
Michal Vasko2caefc12019-11-14 16:07:56 +01001715 set_remove_nodes_none(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001716 return ret;
1717}
1718
1719#endif
1720
1721/**
1722 * @brief Merge 2 sorted sets into one.
1723 *
1724 * @param[in,out] trg Set to merge into. Duplicates are removed.
1725 * @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 +02001726 * @return LY_ERR
1727 */
1728static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001729set_sorted_merge(struct lyxp_set *trg, struct lyxp_set *src)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001730{
1731 uint32_t i, j, k, count, dup_count;
1732 int cmp;
1733 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001734
Michal Vaskod3678892020-05-21 10:06:58 +02001735 if ((trg->type != LYXP_SET_NODE_SET) || (src->type != LYXP_SET_NODE_SET)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001736 return LY_EINVAL;
1737 }
1738
Michal Vaskod3678892020-05-21 10:06:58 +02001739 if (!src->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001740 return LY_SUCCESS;
Michal Vaskod3678892020-05-21 10:06:58 +02001741 } else if (!trg->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001742 set_fill_set(trg, src);
Michal Vaskod3678892020-05-21 10:06:58 +02001743 lyxp_set_free_content(src);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001744 return LY_SUCCESS;
1745 }
1746
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001747 /* find first top-level node to be used as anchor for positions */
Michal Vasko9e685082021-01-29 14:49:09 +01001748 for (root = trg->cur_node; root->parent; root = lyd_parent(root)) {}
Michal Vaskod989ba02020-08-24 10:59:24 +02001749 for ( ; root->prev->next; root = root->prev) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001750
1751 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001752 if (set_assign_pos(trg, root, trg->root_type) || set_assign_pos(src, root, src->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001753 return LY_EINT;
1754 }
1755
1756#ifndef NDEBUG
1757 LOGDBG(LY_LDGXPATH, "MERGE target");
1758 print_set_debug(trg);
1759 LOGDBG(LY_LDGXPATH, "MERGE source");
1760 print_set_debug(src);
1761#endif
1762
1763 /* make memory for the merge (duplicates are not detected yet, so space
1764 * will likely be wasted on them, too bad) */
1765 if (trg->size - trg->used < src->used) {
1766 trg->size = trg->used + src->used;
1767
1768 trg->val.nodes = ly_realloc(trg->val.nodes, trg->size * sizeof *trg->val.nodes);
1769 LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx), LY_EMEM);
1770 }
1771
1772 i = 0;
1773 j = 0;
1774 count = 0;
1775 dup_count = 0;
1776 do {
1777 cmp = set_sort_compare(&src->val.nodes[i], &trg->val.nodes[j]);
1778 if (!cmp) {
1779 if (!count) {
1780 /* duplicate, just skip it */
1781 ++i;
1782 ++j;
1783 } else {
1784 /* we are copying something already, so let's copy the duplicate too,
1785 * we are hoping that afterwards there are some more nodes to
1786 * copy and this way we can copy them all together */
1787 ++count;
1788 ++dup_count;
1789 ++i;
1790 ++j;
1791 }
1792 } else if (cmp < 0) {
1793 /* inserting src node into trg, just remember it for now */
1794 ++count;
1795 ++i;
1796
1797 /* insert the hash now */
1798 set_insert_node_hash(trg, src->val.nodes[i - 1].node, src->val.nodes[i - 1].type);
1799 } else if (count) {
1800copy_nodes:
1801 /* time to actually copy the nodes, we have found the largest block of nodes */
1802 memmove(&trg->val.nodes[j + (count - dup_count)],
1803 &trg->val.nodes[j],
1804 (trg->used - j) * sizeof *trg->val.nodes);
1805 memcpy(&trg->val.nodes[j - dup_count], &src->val.nodes[i - count], count * sizeof *src->val.nodes);
1806
1807 trg->used += count - dup_count;
1808 /* do not change i, except the copying above, we are basically doing exactly what is in the else branch below */
1809 j += count - dup_count;
1810
1811 count = 0;
1812 dup_count = 0;
1813 } else {
1814 ++j;
1815 }
1816 } while ((i < src->used) && (j < trg->used));
1817
1818 if ((i < src->used) || count) {
1819 /* insert all the hashes first */
1820 for (k = i; k < src->used; ++k) {
1821 set_insert_node_hash(trg, src->val.nodes[k].node, src->val.nodes[k].type);
1822 }
1823
1824 /* loop ended, but we need to copy something at trg end */
1825 count += src->used - i;
1826 i = src->used;
1827 goto copy_nodes;
1828 }
1829
1830 /* we are inserting hashes before the actual node insert, which causes
1831 * situations when there were initially not enough items for a hash table,
1832 * but even after some were inserted, hash table was not created (during
1833 * insertion the number of items is not updated yet) */
1834 if (!trg->ht && (trg->used >= LYD_HT_MIN_ITEMS)) {
1835 set_insert_node_hash(trg, NULL, 0);
1836 }
1837
1838#ifndef NDEBUG
1839 LOGDBG(LY_LDGXPATH, "MERGE result");
1840 print_set_debug(trg);
1841#endif
1842
Michal Vaskod3678892020-05-21 10:06:58 +02001843 lyxp_set_free_content(src);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001844 return LY_SUCCESS;
1845}
1846
1847/*
1848 * (re)parse functions
1849 *
1850 * Parse functions parse the expression into
1851 * tokens (syntactic analysis).
1852 *
1853 * Reparse functions perform semantic analysis
1854 * (do not save the result, just a check) of
1855 * the expression and fill repeat indices.
1856 */
1857
Michal Vasko14676352020-05-29 11:35:55 +02001858LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02001859lyxp_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 +02001860{
Michal Vasko004d3152020-06-11 19:59:22 +02001861 if (exp->used == tok_idx) {
Michal Vasko14676352020-05-29 11:35:55 +02001862 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001863 LOGVAL(ctx, LY_VCODE_XP_EOF);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001864 }
Michal Vasko14676352020-05-29 11:35:55 +02001865 return LY_EINCOMPLETE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001866 }
1867
Michal Vasko004d3152020-06-11 19:59:22 +02001868 if (want_tok && (exp->tokens[tok_idx] != want_tok)) {
Michal Vasko14676352020-05-29 11:35:55 +02001869 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001870 LOGVAL(ctx, LY_VCODE_XP_INTOK2, lyxp_print_token(exp->tokens[tok_idx]),
Michal Vasko0b468e62020-10-19 09:33:04 +02001871 &exp->expr[exp->tok_pos[tok_idx]], lyxp_print_token(want_tok));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001872 }
Michal Vasko14676352020-05-29 11:35:55 +02001873 return LY_ENOT;
1874 }
1875
1876 return LY_SUCCESS;
1877}
1878
Michal Vasko004d3152020-06-11 19:59:22 +02001879LY_ERR
1880lyxp_next_token(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_token want_tok)
1881{
1882 LY_CHECK_RET(lyxp_check_token(ctx, exp, *tok_idx, want_tok));
1883
1884 /* skip the token */
1885 ++(*tok_idx);
1886
1887 return LY_SUCCESS;
1888}
1889
Michal Vasko14676352020-05-29 11:35:55 +02001890/* just like lyxp_check_token() but tests for 2 tokens */
1891static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02001892exp_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 +02001893 enum lyxp_token want_tok2)
Michal Vasko14676352020-05-29 11:35:55 +02001894{
Michal Vasko004d3152020-06-11 19:59:22 +02001895 if (exp->used == tok_idx) {
Michal Vasko14676352020-05-29 11:35:55 +02001896 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001897 LOGVAL(ctx, LY_VCODE_XP_EOF);
Michal Vasko14676352020-05-29 11:35:55 +02001898 }
1899 return LY_EINCOMPLETE;
1900 }
1901
Michal Vasko004d3152020-06-11 19:59:22 +02001902 if ((exp->tokens[tok_idx] != want_tok1) && (exp->tokens[tok_idx] != want_tok2)) {
Michal Vasko14676352020-05-29 11:35:55 +02001903 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001904 LOGVAL(ctx, LY_VCODE_XP_INTOK, lyxp_print_token(exp->tokens[tok_idx]),
Michal Vasko0b468e62020-10-19 09:33:04 +02001905 &exp->expr[exp->tok_pos[tok_idx]]);
Michal Vasko14676352020-05-29 11:35:55 +02001906 }
1907 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001908 }
1909
1910 return LY_SUCCESS;
1911}
1912
1913/**
1914 * @brief Stack operation push on the repeat array.
1915 *
1916 * @param[in] exp Expression to use.
Michal Vasko004d3152020-06-11 19:59:22 +02001917 * @param[in] tok_idx Position in the expresion \p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001918 * @param[in] repeat_op_idx Index from \p exp of the operator token. This value is pushed.
1919 */
1920static void
Michal Vasko004d3152020-06-11 19:59:22 +02001921exp_repeat_push(struct lyxp_expr *exp, uint16_t tok_idx, uint16_t repeat_op_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001922{
1923 uint16_t i;
1924
Michal Vasko004d3152020-06-11 19:59:22 +02001925 if (exp->repeat[tok_idx]) {
Radek Krejci1e008d22020-08-17 11:37:37 +02001926 for (i = 0; exp->repeat[tok_idx][i]; ++i) {}
Michal Vasko004d3152020-06-11 19:59:22 +02001927 exp->repeat[tok_idx] = realloc(exp->repeat[tok_idx], (i + 2) * sizeof *exp->repeat[tok_idx]);
1928 LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), );
1929 exp->repeat[tok_idx][i] = repeat_op_idx;
1930 exp->repeat[tok_idx][i + 1] = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001931 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02001932 exp->repeat[tok_idx] = calloc(2, sizeof *exp->repeat[tok_idx]);
1933 LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), );
1934 exp->repeat[tok_idx][0] = repeat_op_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001935 }
1936}
1937
1938/**
1939 * @brief Reparse Predicate. Logs directly on error.
1940 *
1941 * [7] Predicate ::= '[' Expr ']'
1942 *
1943 * @param[in] ctx Context for logging.
1944 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02001945 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02001946 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001947 * @return LY_ERR
1948 */
1949static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02001950reparse_predicate(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t depth)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001951{
1952 LY_ERR rc;
1953
Michal Vasko004d3152020-06-11 19:59:22 +02001954 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001955 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02001956 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001957
aPiecekbf968d92021-05-27 14:35:05 +02001958 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001959 LY_CHECK_RET(rc);
1960
Michal Vasko004d3152020-06-11 19:59:22 +02001961 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001962 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02001963 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001964
1965 return LY_SUCCESS;
1966}
1967
1968/**
1969 * @brief Reparse RelativeLocationPath. Logs directly on error.
1970 *
1971 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
1972 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
1973 * [6] NodeTest ::= NameTest | NodeType '(' ')'
1974 *
1975 * @param[in] ctx Context for logging.
1976 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02001977 * @param[in] tok_idx Position in the expression \p exp.
aPiecekbf968d92021-05-27 14:35:05 +02001978 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001979 * @return LY_ERR (LY_EINCOMPLETE on forward reference)
1980 */
1981static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02001982reparse_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 +02001983{
1984 LY_ERR rc;
1985
Michal Vasko004d3152020-06-11 19:59:22 +02001986 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001987 LY_CHECK_RET(rc);
1988
1989 goto step;
1990 do {
1991 /* '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02001992 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001993
Michal Vasko004d3152020-06-11 19:59:22 +02001994 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001995 LY_CHECK_RET(rc);
1996step:
1997 /* Step */
Michal Vasko004d3152020-06-11 19:59:22 +02001998 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001999 case LYXP_TOKEN_DOT:
Michal Vasko004d3152020-06-11 19:59:22 +02002000 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002001 break;
2002
2003 case LYXP_TOKEN_DDOT:
Michal Vasko004d3152020-06-11 19:59:22 +02002004 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002005 break;
2006
2007 case LYXP_TOKEN_AT:
Michal Vasko004d3152020-06-11 19:59:22 +02002008 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002009
Michal Vasko004d3152020-06-11 19:59:22 +02002010 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002011 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002012 if ((exp->tokens[*tok_idx] != LYXP_TOKEN_NAMETEST) && (exp->tokens[*tok_idx] != LYXP_TOKEN_NODETYPE)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002013 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 +02002014 return LY_EVALID;
2015 }
Radek Krejci0f969882020-08-21 16:56:47 +02002016 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02002017 case LYXP_TOKEN_NAMETEST:
Michal Vasko004d3152020-06-11 19:59:22 +02002018 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002019 goto reparse_predicate;
2020 break;
2021
2022 case LYXP_TOKEN_NODETYPE:
Michal Vasko004d3152020-06-11 19:59:22 +02002023 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002024
2025 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02002026 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002027 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002028 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002029
2030 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02002031 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002032 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002033 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002034
2035reparse_predicate:
2036 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002037 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
aPiecekbf968d92021-05-27 14:35:05 +02002038 rc = reparse_predicate(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002039 LY_CHECK_RET(rc);
2040 }
2041 break;
2042 default:
Radek Krejci2efc45b2020-12-22 16:25:44 +01002043 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 +02002044 return LY_EVALID;
2045 }
Michal Vasko004d3152020-06-11 19:59:22 +02002046 } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02002047
2048 return LY_SUCCESS;
2049}
2050
2051/**
2052 * @brief Reparse AbsoluteLocationPath. Logs directly on error.
2053 *
2054 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
2055 *
2056 * @param[in] ctx Context for logging.
2057 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002058 * @param[in] tok_idx Position in the expression \p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002059 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002060 * @return LY_ERR
2061 */
2062static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002063reparse_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 +02002064{
2065 LY_ERR rc;
2066
Michal Vasko004d3152020-06-11 19:59:22 +02002067 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 +02002068
2069 /* '/' RelativeLocationPath? */
Michal Vasko004d3152020-06-11 19:59:22 +02002070 if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002071 /* '/' */
Michal Vasko004d3152020-06-11 19:59:22 +02002072 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002073
Michal Vasko004d3152020-06-11 19:59:22 +02002074 if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002075 return LY_SUCCESS;
2076 }
Michal Vasko004d3152020-06-11 19:59:22 +02002077 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002078 case LYXP_TOKEN_DOT:
2079 case LYXP_TOKEN_DDOT:
2080 case LYXP_TOKEN_AT:
2081 case LYXP_TOKEN_NAMETEST:
2082 case LYXP_TOKEN_NODETYPE:
aPiecekbf968d92021-05-27 14:35:05 +02002083 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002084 LY_CHECK_RET(rc);
Radek Krejci0f969882020-08-21 16:56:47 +02002085 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02002086 default:
2087 break;
2088 }
2089
Michal Vasko03ff5a72019-09-11 13:49:33 +02002090 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02002091 /* '//' RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002092 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002093
aPiecekbf968d92021-05-27 14:35:05 +02002094 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002095 LY_CHECK_RET(rc);
2096 }
2097
2098 return LY_SUCCESS;
2099}
2100
2101/**
2102 * @brief Reparse FunctionCall. Logs directly on error.
2103 *
2104 * [9] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
2105 *
2106 * @param[in] ctx Context for logging.
2107 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002108 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002109 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002110 * @return LY_ERR
2111 */
2112static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002113reparse_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 +02002114{
Radek Krejci1deb5be2020-08-26 16:43:36 +02002115 int8_t min_arg_count = -1;
2116 uint32_t arg_count, max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002117 uint16_t func_tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002118 LY_ERR rc;
2119
Michal Vasko004d3152020-06-11 19:59:22 +02002120 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_FUNCNAME);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002121 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002122 func_tok_idx = *tok_idx;
2123 switch (exp->tok_len[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002124 case 3:
Michal Vasko004d3152020-06-11 19:59:22 +02002125 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002126 min_arg_count = 1;
2127 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002128 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002129 min_arg_count = 1;
2130 max_arg_count = 1;
2131 }
2132 break;
2133 case 4:
Michal Vasko004d3152020-06-11 19:59:22 +02002134 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002135 min_arg_count = 1;
2136 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002137 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002138 min_arg_count = 0;
2139 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002140 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002141 min_arg_count = 0;
2142 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002143 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002144 min_arg_count = 0;
2145 max_arg_count = 0;
2146 }
2147 break;
2148 case 5:
Michal Vasko004d3152020-06-11 19:59:22 +02002149 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002150 min_arg_count = 1;
2151 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002152 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002153 min_arg_count = 0;
2154 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002155 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002156 min_arg_count = 1;
2157 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002158 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002159 min_arg_count = 1;
2160 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002161 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002162 min_arg_count = 1;
2163 max_arg_count = 1;
2164 }
2165 break;
2166 case 6:
Michal Vasko004d3152020-06-11 19:59:22 +02002167 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002168 min_arg_count = 2;
Radek Krejci1deb5be2020-08-26 16:43:36 +02002169 max_arg_count = UINT32_MAX;
Michal Vasko004d3152020-06-11 19:59:22 +02002170 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002171 min_arg_count = 0;
2172 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002173 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002174 min_arg_count = 0;
2175 max_arg_count = 1;
2176 }
2177 break;
2178 case 7:
Michal Vasko004d3152020-06-11 19:59:22 +02002179 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002180 min_arg_count = 1;
2181 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002182 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002183 min_arg_count = 1;
2184 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002185 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002186 min_arg_count = 0;
2187 max_arg_count = 0;
2188 }
2189 break;
2190 case 8:
Michal Vasko004d3152020-06-11 19:59:22 +02002191 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002192 min_arg_count = 2;
2193 max_arg_count = 2;
Michal Vasko004d3152020-06-11 19:59:22 +02002194 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002195 min_arg_count = 0;
2196 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002197 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002198 min_arg_count = 2;
2199 max_arg_count = 2;
2200 }
2201 break;
2202 case 9:
Michal Vasko004d3152020-06-11 19:59:22 +02002203 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002204 min_arg_count = 2;
2205 max_arg_count = 3;
Michal Vasko004d3152020-06-11 19:59:22 +02002206 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002207 min_arg_count = 3;
2208 max_arg_count = 3;
2209 }
2210 break;
2211 case 10:
Michal Vasko004d3152020-06-11 19:59:22 +02002212 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002213 min_arg_count = 0;
2214 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002215 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002216 min_arg_count = 1;
2217 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002218 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002219 min_arg_count = 2;
2220 max_arg_count = 2;
2221 }
2222 break;
2223 case 11:
Michal Vasko004d3152020-06-11 19:59:22 +02002224 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002225 min_arg_count = 2;
2226 max_arg_count = 2;
2227 }
2228 break;
2229 case 12:
Michal Vasko004d3152020-06-11 19:59:22 +02002230 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002231 min_arg_count = 2;
2232 max_arg_count = 2;
2233 }
2234 break;
2235 case 13:
Michal Vasko004d3152020-06-11 19:59:22 +02002236 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002237 min_arg_count = 0;
2238 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002239 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002240 min_arg_count = 0;
2241 max_arg_count = 1;
2242 }
2243 break;
2244 case 15:
Michal Vasko004d3152020-06-11 19:59:22 +02002245 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002246 min_arg_count = 0;
2247 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002248 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002249 min_arg_count = 2;
2250 max_arg_count = 2;
2251 }
2252 break;
2253 case 16:
Michal Vasko004d3152020-06-11 19:59:22 +02002254 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002255 min_arg_count = 2;
2256 max_arg_count = 2;
2257 }
2258 break;
2259 case 20:
Michal Vasko004d3152020-06-11 19:59:22 +02002260 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002261 min_arg_count = 2;
2262 max_arg_count = 2;
2263 }
2264 break;
2265 }
2266 if (min_arg_count == -1) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002267 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 +02002268 return LY_EINVAL;
2269 }
Michal Vasko004d3152020-06-11 19:59:22 +02002270 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002271
2272 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02002273 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002274 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002275 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002276
2277 /* ( Expr ( ',' Expr )* )? */
2278 arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002279 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002280 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002281 if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002282 ++arg_count;
aPiecekbf968d92021-05-27 14:35:05 +02002283 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002284 LY_CHECK_RET(rc);
2285 }
Michal Vasko004d3152020-06-11 19:59:22 +02002286 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) {
2287 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002288
2289 ++arg_count;
aPiecekbf968d92021-05-27 14:35:05 +02002290 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002291 LY_CHECK_RET(rc);
2292 }
2293
2294 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02002295 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002296 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002297 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002298
Radek Krejci857189e2020-09-01 13:26:36 +02002299 if ((arg_count < (uint32_t)min_arg_count) || (arg_count > max_arg_count)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002300 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 +02002301 return LY_EVALID;
2302 }
2303
2304 return LY_SUCCESS;
2305}
2306
2307/**
2308 * @brief Reparse PathExpr. Logs directly on error.
2309 *
2310 * [10] PathExpr ::= LocationPath | PrimaryExpr Predicate*
2311 * | PrimaryExpr Predicate* '/' RelativeLocationPath
2312 * | PrimaryExpr Predicate* '//' RelativeLocationPath
2313 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
2314 * [8] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall
2315 *
2316 * @param[in] ctx Context for logging.
2317 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002318 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002319 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002320 * @return LY_ERR
2321 */
2322static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002323reparse_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 +02002324{
2325 LY_ERR rc;
2326
Michal Vasko004d3152020-06-11 19:59:22 +02002327 if (lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko14676352020-05-29 11:35:55 +02002328 return LY_EVALID;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002329 }
2330
Michal Vasko004d3152020-06-11 19:59:22 +02002331 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002332 case LYXP_TOKEN_PAR1:
2333 /* '(' Expr ')' Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002334 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002335
aPiecekbf968d92021-05-27 14:35:05 +02002336 rc = reparse_or_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002337 LY_CHECK_RET(rc);
2338
Michal Vasko004d3152020-06-11 19:59:22 +02002339 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002340 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002341 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002342 goto predicate;
2343 break;
2344 case LYXP_TOKEN_DOT:
2345 case LYXP_TOKEN_DDOT:
2346 case LYXP_TOKEN_AT:
2347 case LYXP_TOKEN_NAMETEST:
2348 case LYXP_TOKEN_NODETYPE:
2349 /* RelativeLocationPath */
aPiecekbf968d92021-05-27 14:35:05 +02002350 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002351 LY_CHECK_RET(rc);
2352 break;
2353 case LYXP_TOKEN_FUNCNAME:
2354 /* FunctionCall */
aPiecekbf968d92021-05-27 14:35:05 +02002355 rc = reparse_function_call(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002356 LY_CHECK_RET(rc);
2357 goto predicate;
2358 break;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002359 case LYXP_TOKEN_OPER_PATH:
2360 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02002361 /* AbsoluteLocationPath */
aPiecekbf968d92021-05-27 14:35:05 +02002362 rc = reparse_absolute_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002363 LY_CHECK_RET(rc);
2364 break;
2365 case LYXP_TOKEN_LITERAL:
2366 /* Literal */
Michal Vasko004d3152020-06-11 19:59:22 +02002367 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002368 goto predicate;
2369 break;
2370 case LYXP_TOKEN_NUMBER:
2371 /* Number */
Michal Vasko004d3152020-06-11 19:59:22 +02002372 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002373 goto predicate;
2374 break;
2375 default:
Radek Krejci2efc45b2020-12-22 16:25:44 +01002376 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 +02002377 return LY_EVALID;
2378 }
2379
2380 return LY_SUCCESS;
2381
2382predicate:
2383 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002384 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
aPiecekbf968d92021-05-27 14:35:05 +02002385 rc = reparse_predicate(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002386 LY_CHECK_RET(rc);
2387 }
2388
2389 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002390 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002391
2392 /* '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02002393 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002394
aPiecekbf968d92021-05-27 14:35:05 +02002395 rc = reparse_relative_location_path(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002396 LY_CHECK_RET(rc);
2397 }
2398
2399 return LY_SUCCESS;
2400}
2401
2402/**
2403 * @brief Reparse UnaryExpr. Logs directly on error.
2404 *
2405 * [17] UnaryExpr ::= UnionExpr | '-' UnaryExpr
2406 * [18] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
2407 *
2408 * @param[in] ctx Context for logging.
2409 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002410 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002411 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002412 * @return LY_ERR
2413 */
2414static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002415reparse_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 +02002416{
2417 uint16_t prev_exp;
2418 LY_ERR rc;
2419
2420 /* ('-')* */
Michal Vasko004d3152020-06-11 19:59:22 +02002421 prev_exp = *tok_idx;
Michal Vasko69730152020-10-09 16:30:07 +02002422 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2423 (exp->expr[exp->tok_pos[*tok_idx]] == '-')) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002424 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNARY);
Michal Vasko004d3152020-06-11 19:59:22 +02002425 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002426 }
2427
2428 /* PathExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002429 prev_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002430 rc = reparse_path_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002431 LY_CHECK_RET(rc);
2432
2433 /* ('|' PathExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002434 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_UNI)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002435 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNION);
Michal Vasko004d3152020-06-11 19:59:22 +02002436 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002437
aPiecekbf968d92021-05-27 14:35:05 +02002438 rc = reparse_path_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002439 LY_CHECK_RET(rc);
2440 }
2441
2442 return LY_SUCCESS;
2443}
2444
2445/**
2446 * @brief Reparse AdditiveExpr. Logs directly on error.
2447 *
2448 * [15] AdditiveExpr ::= MultiplicativeExpr
2449 * | AdditiveExpr '+' MultiplicativeExpr
2450 * | AdditiveExpr '-' MultiplicativeExpr
2451 * [16] MultiplicativeExpr ::= UnaryExpr
2452 * | MultiplicativeExpr '*' UnaryExpr
2453 * | MultiplicativeExpr 'div' UnaryExpr
2454 * | MultiplicativeExpr 'mod' UnaryExpr
2455 *
2456 * @param[in] ctx Context for logging.
2457 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002458 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002459 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002460 * @return LY_ERR
2461 */
2462static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002463reparse_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 +02002464{
2465 uint16_t prev_add_exp, prev_mul_exp;
2466 LY_ERR rc;
2467
Michal Vasko004d3152020-06-11 19:59:22 +02002468 prev_add_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002469 goto reparse_multiplicative_expr;
2470
2471 /* ('+' / '-' MultiplicativeExpr)* */
Michal Vasko69730152020-10-09 16:30:07 +02002472 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2473 ((exp->expr[exp->tok_pos[*tok_idx]] == '+') || (exp->expr[exp->tok_pos[*tok_idx]] == '-'))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002474 exp_repeat_push(exp, prev_add_exp, LYXP_EXPR_ADDITIVE);
Michal Vasko004d3152020-06-11 19:59:22 +02002475 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002476
2477reparse_multiplicative_expr:
2478 /* UnaryExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002479 prev_mul_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002480 rc = reparse_unary_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002481 LY_CHECK_RET(rc);
2482
2483 /* ('*' / 'div' / 'mod' UnaryExpr)* */
Michal Vasko69730152020-10-09 16:30:07 +02002484 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2485 ((exp->expr[exp->tok_pos[*tok_idx]] == '*') || (exp->tok_len[*tok_idx] == 3))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002486 exp_repeat_push(exp, prev_mul_exp, LYXP_EXPR_MULTIPLICATIVE);
Michal Vasko004d3152020-06-11 19:59:22 +02002487 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002488
aPiecekbf968d92021-05-27 14:35:05 +02002489 rc = reparse_unary_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002490 LY_CHECK_RET(rc);
2491 }
2492 }
2493
2494 return LY_SUCCESS;
2495}
2496
2497/**
2498 * @brief Reparse EqualityExpr. Logs directly on error.
2499 *
2500 * [13] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
2501 * | EqualityExpr '!=' RelationalExpr
2502 * [14] RelationalExpr ::= AdditiveExpr
2503 * | RelationalExpr '<' AdditiveExpr
2504 * | RelationalExpr '>' AdditiveExpr
2505 * | RelationalExpr '<=' AdditiveExpr
2506 * | RelationalExpr '>=' AdditiveExpr
2507 *
2508 * @param[in] ctx Context for logging.
2509 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002510 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002511 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002512 * @return LY_ERR
2513 */
2514static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002515reparse_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 +02002516{
2517 uint16_t prev_eq_exp, prev_rel_exp;
2518 LY_ERR rc;
2519
Michal Vasko004d3152020-06-11 19:59:22 +02002520 prev_eq_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002521 goto reparse_additive_expr;
2522
2523 /* ('=' / '!=' RelationalExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002524 while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_EQUAL, LYXP_TOKEN_OPER_NEQUAL)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002525 exp_repeat_push(exp, prev_eq_exp, LYXP_EXPR_EQUALITY);
Michal Vasko004d3152020-06-11 19:59:22 +02002526 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002527
2528reparse_additive_expr:
2529 /* AdditiveExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002530 prev_rel_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002531 rc = reparse_additive_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002532 LY_CHECK_RET(rc);
2533
2534 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002535 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_COMP)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002536 exp_repeat_push(exp, prev_rel_exp, LYXP_EXPR_RELATIONAL);
Michal Vasko004d3152020-06-11 19:59:22 +02002537 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002538
aPiecekbf968d92021-05-27 14:35:05 +02002539 rc = reparse_additive_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002540 LY_CHECK_RET(rc);
2541 }
2542 }
2543
2544 return LY_SUCCESS;
2545}
2546
2547/**
2548 * @brief Reparse OrExpr. Logs directly on error.
2549 *
2550 * [11] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
2551 * [12] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
2552 *
2553 * @param[in] ctx Context for logging.
2554 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002555 * @param[in] tok_idx Position in the expression @p exp.
aPiecekbf968d92021-05-27 14:35:05 +02002556 * @param[in] depth Current number of nested expressions.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002557 * @return LY_ERR
2558 */
2559static LY_ERR
aPiecekbf968d92021-05-27 14:35:05 +02002560reparse_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 +02002561{
2562 uint16_t prev_or_exp, prev_and_exp;
2563 LY_ERR rc;
2564
aPiecekbf968d92021-05-27 14:35:05 +02002565 ++depth;
2566 LY_CHECK_ERR_RET(depth > LYXP_MAX_BLOCK_DEPTH, LOGVAL(ctx, LY_VCODE_XP_DEPTH), LY_EINVAL);
2567
Michal Vasko004d3152020-06-11 19:59:22 +02002568 prev_or_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002569 goto reparse_equality_expr;
2570
2571 /* ('or' AndExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002572 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 +02002573 exp_repeat_push(exp, prev_or_exp, LYXP_EXPR_OR);
Michal Vasko004d3152020-06-11 19:59:22 +02002574 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002575
2576reparse_equality_expr:
2577 /* EqualityExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002578 prev_and_exp = *tok_idx;
aPiecekbf968d92021-05-27 14:35:05 +02002579 rc = reparse_equality_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002580 LY_CHECK_RET(rc);
2581
2582 /* ('and' EqualityExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002583 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 +02002584 exp_repeat_push(exp, prev_and_exp, LYXP_EXPR_AND);
Michal Vasko004d3152020-06-11 19:59:22 +02002585 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002586
aPiecekbf968d92021-05-27 14:35:05 +02002587 rc = reparse_equality_expr(ctx, exp, tok_idx, depth);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002588 LY_CHECK_RET(rc);
2589 }
2590 }
2591
2592 return LY_SUCCESS;
2593}
Radek Krejcib1646a92018-11-02 16:08:26 +01002594
2595/**
2596 * @brief Parse NCName.
2597 *
2598 * @param[in] ncname Name to parse.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002599 * @return Length of @p ncname valid bytes.
Radek Krejcib1646a92018-11-02 16:08:26 +01002600 */
Radek Krejcid4270262019-01-07 15:07:25 +01002601static long int
Radek Krejcib1646a92018-11-02 16:08:26 +01002602parse_ncname(const char *ncname)
2603{
Radek Krejci1deb5be2020-08-26 16:43:36 +02002604 uint32_t uc;
Radek Krejcid4270262019-01-07 15:07:25 +01002605 size_t size;
2606 long int len = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002607
2608 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), 0);
2609 if (!is_xmlqnamestartchar(uc) || (uc == ':')) {
2610 return len;
2611 }
2612
2613 do {
2614 len += size;
Radek Krejci9a564c92019-01-07 14:53:57 +01002615 if (!*ncname) {
2616 break;
2617 }
Radek Krejcid4270262019-01-07 15:07:25 +01002618 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), -len);
Radek Krejcib1646a92018-11-02 16:08:26 +01002619 } while (is_xmlqnamechar(uc) && (uc != ':'));
2620
2621 return len;
2622}
2623
2624/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02002625 * @brief Add @p token into the expression @p exp.
Radek Krejcib1646a92018-11-02 16:08:26 +01002626 *
Michal Vasko03ff5a72019-09-11 13:49:33 +02002627 * @param[in] ctx Context for logging.
Radek Krejcib1646a92018-11-02 16:08:26 +01002628 * @param[in] exp Expression to use.
2629 * @param[in] token Token to add.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002630 * @param[in] tok_pos Token position in the XPath expression.
Radek Krejcib1646a92018-11-02 16:08:26 +01002631 * @param[in] tok_len Token length in the XPath expression.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002632 * @return LY_ERR
Radek Krejcib1646a92018-11-02 16:08:26 +01002633 */
2634static LY_ERR
Michal Vasko14676352020-05-29 11:35:55 +02002635exp_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 +01002636{
2637 uint32_t prev;
2638
2639 if (exp->used == exp->size) {
2640 prev = exp->size;
2641 exp->size += LYXP_EXPR_SIZE_STEP;
2642 if (prev > exp->size) {
2643 LOGINT(ctx);
2644 return LY_EINT;
2645 }
2646
2647 exp->tokens = ly_realloc(exp->tokens, exp->size * sizeof *exp->tokens);
2648 LY_CHECK_ERR_RET(!exp->tokens, LOGMEM(ctx), LY_EMEM);
2649 exp->tok_pos = ly_realloc(exp->tok_pos, exp->size * sizeof *exp->tok_pos);
2650 LY_CHECK_ERR_RET(!exp->tok_pos, LOGMEM(ctx), LY_EMEM);
2651 exp->tok_len = ly_realloc(exp->tok_len, exp->size * sizeof *exp->tok_len);
2652 LY_CHECK_ERR_RET(!exp->tok_len, LOGMEM(ctx), LY_EMEM);
2653 }
2654
2655 exp->tokens[exp->used] = token;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002656 exp->tok_pos[exp->used] = tok_pos;
Radek Krejcib1646a92018-11-02 16:08:26 +01002657 exp->tok_len[exp->used] = tok_len;
2658 ++exp->used;
2659 return LY_SUCCESS;
2660}
2661
2662void
Michal Vasko14676352020-05-29 11:35:55 +02002663lyxp_expr_free(const struct ly_ctx *ctx, struct lyxp_expr *expr)
Radek Krejcib1646a92018-11-02 16:08:26 +01002664{
2665 uint16_t i;
2666
2667 if (!expr) {
2668 return;
2669 }
2670
2671 lydict_remove(ctx, expr->expr);
2672 free(expr->tokens);
2673 free(expr->tok_pos);
2674 free(expr->tok_len);
2675 if (expr->repeat) {
2676 for (i = 0; i < expr->used; ++i) {
2677 free(expr->repeat[i]);
2678 }
2679 }
2680 free(expr->repeat);
2681 free(expr);
2682}
2683
Radek Krejcif03a9e22020-09-18 20:09:31 +02002684LY_ERR
2685lyxp_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 +01002686{
Radek Krejcif03a9e22020-09-18 20:09:31 +02002687 LY_ERR ret = LY_SUCCESS;
2688 struct lyxp_expr *expr;
Radek Krejcid4270262019-01-07 15:07:25 +01002689 size_t parsed = 0, tok_len;
Radek Krejcib1646a92018-11-02 16:08:26 +01002690 enum lyxp_token tok_type;
Radek Krejci857189e2020-09-01 13:26:36 +02002691 ly_bool prev_function_check = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002692 uint16_t tok_idx = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002693
Radek Krejcif03a9e22020-09-18 20:09:31 +02002694 assert(expr_p);
2695
2696 if (!expr_str[0]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002697 LOGVAL(ctx, LY_VCODE_XP_EOF);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002698 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02002699 }
2700
2701 if (!expr_len) {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002702 expr_len = strlen(expr_str);
Michal Vasko004d3152020-06-11 19:59:22 +02002703 }
2704 if (expr_len > UINT16_MAX) {
Michal Vasko623ac7b2021-04-09 12:44:23 +02002705 LOGVAL(ctx, LYVE_XPATH, "XPath expression cannot be longer than %u characters.", UINT16_MAX);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002706 return LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01002707 }
2708
2709 /* init lyxp_expr structure */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002710 expr = calloc(1, sizeof *expr);
2711 LY_CHECK_ERR_GOTO(!expr, LOGMEM(ctx); ret = LY_EMEM, error);
2712 LY_CHECK_GOTO(ret = lydict_insert(ctx, expr_str, expr_len, &expr->expr), error);
2713 expr->used = 0;
2714 expr->size = LYXP_EXPR_SIZE_START;
2715 expr->tokens = malloc(expr->size * sizeof *expr->tokens);
2716 LY_CHECK_ERR_GOTO(!expr->tokens, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002717
Radek Krejcif03a9e22020-09-18 20:09:31 +02002718 expr->tok_pos = malloc(expr->size * sizeof *expr->tok_pos);
2719 LY_CHECK_ERR_GOTO(!expr->tok_pos, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002720
Radek Krejcif03a9e22020-09-18 20:09:31 +02002721 expr->tok_len = malloc(expr->size * sizeof *expr->tok_len);
2722 LY_CHECK_ERR_GOTO(!expr->tok_len, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002723
Michal Vasko004d3152020-06-11 19:59:22 +02002724 /* make expr 0-terminated */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002725 expr_str = expr->expr;
Michal Vasko004d3152020-06-11 19:59:22 +02002726
Radek Krejcif03a9e22020-09-18 20:09:31 +02002727 while (is_xmlws(expr_str[parsed])) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002728 ++parsed;
2729 }
2730
2731 do {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002732 if (expr_str[parsed] == '(') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002733
2734 /* '(' */
2735 tok_len = 1;
2736 tok_type = LYXP_TOKEN_PAR1;
2737
Radek Krejcif03a9e22020-09-18 20:09:31 +02002738 if (prev_function_check && expr->used && (expr->tokens[expr->used - 1] == LYXP_TOKEN_NAMETEST)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002739 /* it is a NodeType/FunctionName after all */
Michal Vasko69730152020-10-09 16:30:07 +02002740 if (((expr->tok_len[expr->used - 1] == 4) &&
2741 (!strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "node", 4) ||
2742 !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "text", 4))) ||
2743 ((expr->tok_len[expr->used - 1] == 7) &&
2744 !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "comment", 7))) {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002745 expr->tokens[expr->used - 1] = LYXP_TOKEN_NODETYPE;
Radek Krejcib1646a92018-11-02 16:08:26 +01002746 } else {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002747 expr->tokens[expr->used - 1] = LYXP_TOKEN_FUNCNAME;
Radek Krejcib1646a92018-11-02 16:08:26 +01002748 }
2749 prev_function_check = 0;
2750 }
2751
Radek Krejcif03a9e22020-09-18 20:09:31 +02002752 } else if (expr_str[parsed] == ')') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002753
2754 /* ')' */
2755 tok_len = 1;
2756 tok_type = LYXP_TOKEN_PAR2;
2757
Radek Krejcif03a9e22020-09-18 20:09:31 +02002758 } else if (expr_str[parsed] == '[') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002759
2760 /* '[' */
2761 tok_len = 1;
2762 tok_type = LYXP_TOKEN_BRACK1;
2763
Radek Krejcif03a9e22020-09-18 20:09:31 +02002764 } else if (expr_str[parsed] == ']') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002765
2766 /* ']' */
2767 tok_len = 1;
2768 tok_type = LYXP_TOKEN_BRACK2;
2769
Radek Krejcif03a9e22020-09-18 20:09:31 +02002770 } else if (!strncmp(&expr_str[parsed], "..", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002771
2772 /* '..' */
2773 tok_len = 2;
2774 tok_type = LYXP_TOKEN_DDOT;
2775
Radek Krejcif03a9e22020-09-18 20:09:31 +02002776 } else if ((expr_str[parsed] == '.') && (!isdigit(expr_str[parsed + 1]))) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002777
2778 /* '.' */
2779 tok_len = 1;
2780 tok_type = LYXP_TOKEN_DOT;
2781
Radek Krejcif03a9e22020-09-18 20:09:31 +02002782 } else if (expr_str[parsed] == '@') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002783
2784 /* '@' */
2785 tok_len = 1;
2786 tok_type = LYXP_TOKEN_AT;
2787
Radek Krejcif03a9e22020-09-18 20:09:31 +02002788 } else if (expr_str[parsed] == ',') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002789
2790 /* ',' */
2791 tok_len = 1;
2792 tok_type = LYXP_TOKEN_COMMA;
2793
Radek Krejcif03a9e22020-09-18 20:09:31 +02002794 } else if (expr_str[parsed] == '\'') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002795
2796 /* Literal with ' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002797 for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\''); ++tok_len) {}
2798 LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0',
Radek Krejci2efc45b2020-12-22 16:25:44 +01002799 LOGVAL(ctx, LY_VCODE_XP_EOE, expr_str[parsed], &expr_str[parsed]); ret = LY_EVALID,
Michal Vasko69730152020-10-09 16:30:07 +02002800 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002801 ++tok_len;
2802 tok_type = LYXP_TOKEN_LITERAL;
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] == '.') || (isdigit(expr_str[parsed]))) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002815
2816 /* Number */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002817 for (tok_len = 0; isdigit(expr_str[parsed + tok_len]); ++tok_len) {}
2818 if (expr_str[parsed + tok_len] == '.') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002819 ++tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02002820 for ( ; isdigit(expr_str[parsed + tok_len]); ++tok_len) {}
Radek Krejcib1646a92018-11-02 16:08:26 +01002821 }
2822 tok_type = LYXP_TOKEN_NUMBER;
2823
Radek Krejcif03a9e22020-09-18 20:09:31 +02002824 } else if (expr_str[parsed] == '/') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002825
2826 /* Operator '/', '//' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002827 if (!strncmp(&expr_str[parsed], "//", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002828 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002829 tok_type = LYXP_TOKEN_OPER_RPATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002830 } else {
2831 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002832 tok_type = LYXP_TOKEN_OPER_PATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002833 }
Radek Krejcib1646a92018-11-02 16:08:26 +01002834
Radek Krejcif03a9e22020-09-18 20:09:31 +02002835 } else if (!strncmp(&expr_str[parsed], "!=", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002836
Michal Vasko3e48bf32020-06-01 08:39:07 +02002837 /* Operator '!=' */
Radek Krejcib1646a92018-11-02 16:08:26 +01002838 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002839 tok_type = LYXP_TOKEN_OPER_NEQUAL;
2840
Radek Krejcif03a9e22020-09-18 20:09:31 +02002841 } else if (!strncmp(&expr_str[parsed], "<=", 2) || !strncmp(&expr_str[parsed], ">=", 2)) {
Michal Vasko3e48bf32020-06-01 08:39:07 +02002842
2843 /* Operator '<=', '>=' */
2844 tok_len = 2;
2845 tok_type = LYXP_TOKEN_OPER_COMP;
Radek Krejcib1646a92018-11-02 16:08:26 +01002846
Radek Krejcif03a9e22020-09-18 20:09:31 +02002847 } else if (expr_str[parsed] == '|') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002848
2849 /* Operator '|' */
2850 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002851 tok_type = LYXP_TOKEN_OPER_UNI;
Radek Krejcib1646a92018-11-02 16:08:26 +01002852
Radek Krejcif03a9e22020-09-18 20:09:31 +02002853 } else if ((expr_str[parsed] == '+') || (expr_str[parsed] == '-')) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002854
2855 /* Operator '+', '-' */
2856 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002857 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002858
Radek Krejcif03a9e22020-09-18 20:09:31 +02002859 } else if (expr_str[parsed] == '=') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002860
Michal Vasko3e48bf32020-06-01 08:39:07 +02002861 /* Operator '=' */
Radek Krejcib1646a92018-11-02 16:08:26 +01002862 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002863 tok_type = LYXP_TOKEN_OPER_EQUAL;
2864
Radek Krejcif03a9e22020-09-18 20:09:31 +02002865 } else if ((expr_str[parsed] == '<') || (expr_str[parsed] == '>')) {
Michal Vasko3e48bf32020-06-01 08:39:07 +02002866
2867 /* Operator '<', '>' */
2868 tok_len = 1;
2869 tok_type = LYXP_TOKEN_OPER_COMP;
Radek Krejcib1646a92018-11-02 16:08:26 +01002870
Michal Vasko69730152020-10-09 16:30:07 +02002871 } else if (expr->used && (expr->tokens[expr->used - 1] != LYXP_TOKEN_AT) &&
2872 (expr->tokens[expr->used - 1] != LYXP_TOKEN_PAR1) &&
2873 (expr->tokens[expr->used - 1] != LYXP_TOKEN_BRACK1) &&
2874 (expr->tokens[expr->used - 1] != LYXP_TOKEN_COMMA) &&
2875 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_LOG) &&
2876 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_EQUAL) &&
2877 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_NEQUAL) &&
2878 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_COMP) &&
2879 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_MATH) &&
2880 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_UNI) &&
2881 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_PATH) &&
2882 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_RPATH)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002883
2884 /* Operator '*', 'or', 'and', 'mod', or 'div' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002885 if (expr_str[parsed] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002886 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002887 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002888
Radek Krejcif03a9e22020-09-18 20:09:31 +02002889 } else if (!strncmp(&expr_str[parsed], "or", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002890 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002891 tok_type = LYXP_TOKEN_OPER_LOG;
Radek Krejcib1646a92018-11-02 16:08:26 +01002892
Radek Krejcif03a9e22020-09-18 20:09:31 +02002893 } else if (!strncmp(&expr_str[parsed], "and", 3)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002894 tok_len = 3;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002895 tok_type = LYXP_TOKEN_OPER_LOG;
Radek Krejcib1646a92018-11-02 16:08:26 +01002896
Radek Krejcif03a9e22020-09-18 20:09:31 +02002897 } else if (!strncmp(&expr_str[parsed], "mod", 3) || !strncmp(&expr_str[parsed], "div", 3)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002898 tok_len = 3;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002899 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002900
2901 } else if (prev_function_check) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002902 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 +02002903 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 +02002904 ret = LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01002905 goto error;
2906 } else {
Michal Vasko774ce402021-04-14 15:35:06 +02002907 LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed], parsed + 1, expr_str);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002908 ret = LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01002909 goto error;
2910 }
Radek Krejcif03a9e22020-09-18 20:09:31 +02002911 } else if (expr_str[parsed] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002912
2913 /* NameTest '*' */
2914 tok_len = 1;
2915 tok_type = LYXP_TOKEN_NAMETEST;
2916
2917 } else {
2918
2919 /* NameTest (NCName ':' '*' | QName) or NodeType/FunctionName */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002920 long int ncname_len = parse_ncname(&expr_str[parsed]);
Michal Vasko774ce402021-04-14 15:35:06 +02002921 LY_CHECK_ERR_GOTO(ncname_len < 0, LOGVAL(ctx, LY_VCODE_XP_INEXPR, expr_str[parsed - ncname_len],
2922 parsed - ncname_len + 1, expr_str); ret = LY_EVALID, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002923 tok_len = ncname_len;
2924
Radek Krejcif03a9e22020-09-18 20:09:31 +02002925 if (expr_str[parsed + tok_len] == ':') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002926 ++tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02002927 if (expr_str[parsed + tok_len] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002928 ++tok_len;
2929 } else {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002930 ncname_len = parse_ncname(&expr_str[parsed + tok_len]);
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 }
2935 /* remove old flag to prevent ambiguities */
2936 prev_function_check = 0;
2937 tok_type = LYXP_TOKEN_NAMETEST;
2938 } else {
2939 /* there is no prefix so it can still be NodeType/FunctionName, we can't finally decide now */
2940 prev_function_check = 1;
2941 tok_type = LYXP_TOKEN_NAMETEST;
2942 }
2943 }
2944
2945 /* store the token, move on to the next one */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002946 LY_CHECK_GOTO(ret = exp_add_token(ctx, expr, tok_type, parsed, tok_len), error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002947 parsed += tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02002948 while (is_xmlws(expr_str[parsed])) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002949 ++parsed;
2950 }
2951
Radek Krejcif03a9e22020-09-18 20:09:31 +02002952 } while (expr_str[parsed]);
Radek Krejcib1646a92018-11-02 16:08:26 +01002953
Michal Vasko004d3152020-06-11 19:59:22 +02002954 if (reparse) {
2955 /* prealloc repeat */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002956 expr->repeat = calloc(expr->size, sizeof *expr->repeat);
2957 LY_CHECK_ERR_GOTO(!expr->repeat, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002958
Michal Vasko004d3152020-06-11 19:59:22 +02002959 /* fill repeat */
aPiecekbf968d92021-05-27 14:35:05 +02002960 LY_CHECK_ERR_GOTO(reparse_or_expr(ctx, expr, &tok_idx, 0), ret = LY_EVALID, error);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002961 if (expr->used > tok_idx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002962 LOGVAL(ctx, LYVE_XPATH, "Unparsed characters \"%s\" left at the end of an XPath expression.",
Michal Vasko69730152020-10-09 16:30:07 +02002963 &expr->expr[expr->tok_pos[tok_idx]]);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002964 ret = LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02002965 goto error;
2966 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02002967 }
2968
Radek Krejcif03a9e22020-09-18 20:09:31 +02002969 print_expr_struct_debug(expr);
2970 *expr_p = expr;
2971 return LY_SUCCESS;
Radek Krejcib1646a92018-11-02 16:08:26 +01002972
2973error:
Radek Krejcif03a9e22020-09-18 20:09:31 +02002974 lyxp_expr_free(ctx, expr);
2975 return ret;
Radek Krejcib1646a92018-11-02 16:08:26 +01002976}
2977
Michal Vasko1734be92020-09-22 08:55:10 +02002978LY_ERR
2979lyxp_expr_dup(const struct ly_ctx *ctx, const struct lyxp_expr *exp, struct lyxp_expr **dup_p)
Michal Vasko004d3152020-06-11 19:59:22 +02002980{
Michal Vasko1734be92020-09-22 08:55:10 +02002981 LY_ERR ret = LY_SUCCESS;
2982 struct lyxp_expr *dup = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02002983 uint32_t i, j;
2984
Michal Vasko7f45cf22020-10-01 12:49:44 +02002985 if (!exp) {
2986 goto cleanup;
2987 }
2988
Michal Vasko004d3152020-06-11 19:59:22 +02002989 dup = calloc(1, sizeof *dup);
Michal Vasko1734be92020-09-22 08:55:10 +02002990 LY_CHECK_ERR_GOTO(!dup, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02002991
2992 dup->tokens = malloc(exp->used * sizeof *dup->tokens);
Michal Vasko1734be92020-09-22 08:55:10 +02002993 LY_CHECK_ERR_GOTO(!dup->tokens, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02002994 memcpy(dup->tokens, exp->tokens, exp->used * sizeof *dup->tokens);
2995
2996 dup->tok_pos = malloc(exp->used * sizeof *dup->tok_pos);
Michal Vasko1734be92020-09-22 08:55:10 +02002997 LY_CHECK_ERR_GOTO(!dup->tok_pos, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02002998 memcpy(dup->tok_pos, exp->tok_pos, exp->used * sizeof *dup->tok_pos);
2999
3000 dup->tok_len = malloc(exp->used * sizeof *dup->tok_len);
Michal Vasko1734be92020-09-22 08:55:10 +02003001 LY_CHECK_ERR_GOTO(!dup->tok_len, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02003002 memcpy(dup->tok_len, exp->tok_len, exp->used * sizeof *dup->tok_len);
3003
3004 dup->repeat = malloc(exp->used * sizeof *dup->repeat);
Michal Vasko1734be92020-09-22 08:55:10 +02003005 LY_CHECK_ERR_GOTO(!dup->repeat, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02003006 for (i = 0; i < exp->used; ++i) {
3007 if (!exp->repeat[i]) {
3008 dup->repeat[i] = NULL;
3009 } else {
Radek Krejci1e008d22020-08-17 11:37:37 +02003010 for (j = 0; exp->repeat[i][j]; ++j) {}
Michal Vasko004d3152020-06-11 19:59:22 +02003011 /* the ending 0 as well */
3012 ++j;
3013
Michal Vasko99c71642020-07-03 13:33:36 +02003014 dup->repeat[i] = malloc(j * sizeof **dup->repeat);
Michal Vasko1734be92020-09-22 08:55:10 +02003015 LY_CHECK_ERR_GOTO(!dup->repeat[i], LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02003016 memcpy(dup->repeat[i], exp->repeat[i], j * sizeof **dup->repeat);
3017 dup->repeat[i][j - 1] = 0;
3018 }
3019 }
3020
3021 dup->used = exp->used;
3022 dup->size = exp->used;
Michal Vasko1734be92020-09-22 08:55:10 +02003023 LY_CHECK_GOTO(ret = lydict_insert(ctx, exp->expr, 0, &dup->expr), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02003024
Michal Vasko1734be92020-09-22 08:55:10 +02003025cleanup:
3026 if (ret) {
3027 lyxp_expr_free(ctx, dup);
3028 } else {
3029 *dup_p = dup;
3030 }
3031 return ret;
Michal Vasko004d3152020-06-11 19:59:22 +02003032}
3033
Michal Vasko03ff5a72019-09-11 13:49:33 +02003034/*
3035 * warn functions
3036 *
3037 * Warn functions check specific reasonable conditions for schema XPath
3038 * and print a warning if they are not satisfied.
3039 */
3040
3041/**
3042 * @brief Get the last-added schema node that is currently in the context.
3043 *
3044 * @param[in] set Set to search in.
3045 * @return Last-added schema context node, NULL if no node is in context.
3046 */
3047static struct lysc_node *
3048warn_get_scnode_in_ctx(struct lyxp_set *set)
3049{
3050 uint32_t i;
3051
3052 if (!set || (set->type != LYXP_SET_SCNODE_SET)) {
3053 return NULL;
3054 }
3055
3056 i = set->used;
3057 do {
3058 --i;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003059 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003060 /* if there are more, simply return the first found (last added) */
3061 return set->val.scnodes[i].scnode;
3062 }
3063 } while (i);
3064
3065 return NULL;
3066}
3067
3068/**
3069 * @brief Test whether a type is numeric - integer type or decimal64.
3070 *
3071 * @param[in] type Type to test.
Radek Krejci857189e2020-09-01 13:26:36 +02003072 * @return Boolean value whether @p type is numeric type or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003073 */
Radek Krejci857189e2020-09-01 13:26:36 +02003074static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003075warn_is_numeric_type(struct lysc_type *type)
3076{
3077 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003078 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003079 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003080
3081 switch (type->basetype) {
3082 case LY_TYPE_DEC64:
3083 case LY_TYPE_INT8:
3084 case LY_TYPE_UINT8:
3085 case LY_TYPE_INT16:
3086 case LY_TYPE_UINT16:
3087 case LY_TYPE_INT32:
3088 case LY_TYPE_UINT32:
3089 case LY_TYPE_INT64:
3090 case LY_TYPE_UINT64:
3091 return 1;
3092 case LY_TYPE_UNION:
3093 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003094 LY_ARRAY_FOR(uni->types, u) {
3095 ret = warn_is_numeric_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003096 if (ret) {
3097 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003098 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003099 }
3100 }
3101 /* did not find any suitable type */
3102 return 0;
3103 case LY_TYPE_LEAFREF:
3104 return warn_is_numeric_type(((struct lysc_type_leafref *)type)->realtype);
3105 default:
3106 return 0;
3107 }
3108}
3109
3110/**
3111 * @brief Test whether a type is string-like - no integers, decimal64 or binary.
3112 *
3113 * @param[in] type Type to test.
Radek Krejci857189e2020-09-01 13:26:36 +02003114 * @return Boolean value whether @p type's basetype is string type or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003115 */
Radek Krejci857189e2020-09-01 13:26:36 +02003116static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003117warn_is_string_type(struct lysc_type *type)
3118{
3119 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003120 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003121 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003122
3123 switch (type->basetype) {
3124 case LY_TYPE_BITS:
3125 case LY_TYPE_ENUM:
3126 case LY_TYPE_IDENT:
3127 case LY_TYPE_INST:
3128 case LY_TYPE_STRING:
3129 return 1;
3130 case LY_TYPE_UNION:
3131 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003132 LY_ARRAY_FOR(uni->types, u) {
3133 ret = warn_is_string_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003134 if (ret) {
3135 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003136 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003137 }
3138 }
3139 /* did not find any suitable type */
3140 return 0;
3141 case LY_TYPE_LEAFREF:
3142 return warn_is_string_type(((struct lysc_type_leafref *)type)->realtype);
3143 default:
3144 return 0;
3145 }
3146}
3147
3148/**
3149 * @brief Test whether a type is one specific type.
3150 *
3151 * @param[in] type Type to test.
3152 * @param[in] base Expected type.
Radek Krejci857189e2020-09-01 13:26:36 +02003153 * @return Boolean value whether the given @p type is of the specific basetype @p base.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003154 */
Radek Krejci857189e2020-09-01 13:26:36 +02003155static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003156warn_is_specific_type(struct lysc_type *type, LY_DATA_TYPE base)
3157{
3158 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003159 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003160 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003161
3162 if (type->basetype == base) {
3163 return 1;
3164 } else if (type->basetype == LY_TYPE_UNION) {
3165 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003166 LY_ARRAY_FOR(uni->types, u) {
3167 ret = warn_is_specific_type(uni->types[u], base);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003168 if (ret) {
3169 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003170 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003171 }
3172 }
3173 /* did not find any suitable type */
3174 return 0;
3175 } else if (type->basetype == LY_TYPE_LEAFREF) {
3176 return warn_is_specific_type(((struct lysc_type_leafref *)type)->realtype, base);
3177 }
3178
3179 return 0;
3180}
3181
3182/**
3183 * @brief Get next type of a (union) type.
3184 *
3185 * @param[in] type Base type.
3186 * @param[in] prev_type Previously returned type.
3187 * @return Next type or NULL.
3188 */
3189static struct lysc_type *
3190warn_is_equal_type_next_type(struct lysc_type *type, struct lysc_type *prev_type)
3191{
3192 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003193 ly_bool found = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003194 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003195
3196 switch (type->basetype) {
3197 case LY_TYPE_UNION:
3198 uni = (struct lysc_type_union *)type;
3199 if (!prev_type) {
3200 return uni->types[0];
3201 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003202 LY_ARRAY_FOR(uni->types, u) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003203 if (found) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003204 return uni->types[u];
Michal Vasko03ff5a72019-09-11 13:49:33 +02003205 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003206 if (prev_type == uni->types[u]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003207 found = 1;
3208 }
3209 }
3210 return NULL;
3211 default:
3212 if (prev_type) {
3213 assert(type == prev_type);
3214 return NULL;
3215 } else {
3216 return type;
3217 }
3218 }
3219}
3220
3221/**
3222 * @brief Test whether 2 types have a common type.
3223 *
3224 * @param[in] type1 First type.
3225 * @param[in] type2 Second type.
3226 * @return 1 if they do, 0 otherwise.
3227 */
3228static int
3229warn_is_equal_type(struct lysc_type *type1, struct lysc_type *type2)
3230{
3231 struct lysc_type *t1, *rt1, *t2, *rt2;
3232
3233 t1 = NULL;
3234 while ((t1 = warn_is_equal_type_next_type(type1, t1))) {
3235 if (t1->basetype == LY_TYPE_LEAFREF) {
3236 rt1 = ((struct lysc_type_leafref *)t1)->realtype;
3237 } else {
3238 rt1 = t1;
3239 }
3240
3241 t2 = NULL;
3242 while ((t2 = warn_is_equal_type_next_type(type2, t2))) {
3243 if (t2->basetype == LY_TYPE_LEAFREF) {
3244 rt2 = ((struct lysc_type_leafref *)t2)->realtype;
3245 } else {
3246 rt2 = t2;
3247 }
3248
3249 if (rt2->basetype == rt1->basetype) {
3250 /* match found */
3251 return 1;
3252 }
3253 }
3254 }
3255
3256 return 0;
3257}
3258
3259/**
3260 * @brief Check both operands of comparison operators.
3261 *
3262 * @param[in] ctx Context for errors.
3263 * @param[in] set1 First operand set.
3264 * @param[in] set2 Second operand set.
3265 * @param[in] numbers_only Whether accept only numbers or other types are fine too (for '=' and '!=').
3266 * @param[in] expr Start of the expression to print with the warning.
3267 * @param[in] tok_pos Token position.
3268 */
3269static void
Radek Krejci857189e2020-09-01 13:26:36 +02003270warn_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 +02003271{
3272 struct lysc_node_leaf *node1, *node2;
Radek Krejci857189e2020-09-01 13:26:36 +02003273 ly_bool leaves = 1, warning = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003274
3275 node1 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set1);
3276 node2 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set2);
3277
3278 if (!node1 && !node2) {
3279 /* no node-sets involved, nothing to do */
3280 return;
3281 }
3282
3283 if (node1) {
3284 if (!(node1->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3285 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node1->nodetype), node1->name);
3286 warning = 1;
3287 leaves = 0;
3288 } else if (numbers_only && !warn_is_numeric_type(node1->type)) {
3289 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node1->name);
3290 warning = 1;
3291 }
3292 }
3293
3294 if (node2) {
3295 if (!(node2->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3296 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node2->nodetype), node2->name);
3297 warning = 1;
3298 leaves = 0;
3299 } else if (numbers_only && !warn_is_numeric_type(node2->type)) {
3300 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node2->name);
3301 warning = 1;
3302 }
3303 }
3304
3305 if (node1 && node2 && leaves && !numbers_only) {
Michal Vasko69730152020-10-09 16:30:07 +02003306 if ((warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type)) ||
3307 (!warn_is_numeric_type(node1->type) && warn_is_numeric_type(node2->type)) ||
3308 (!warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type) &&
3309 !warn_is_equal_type(node1->type, node2->type))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003310 LOGWRN(ctx, "Incompatible types of operands \"%s\" and \"%s\" for comparison.", node1->name, node2->name);
3311 warning = 1;
3312 }
3313 }
3314
3315 if (warning) {
3316 LOGWRN(ctx, "Previous warning generated by XPath subexpression[%u] \"%.20s\".", tok_pos, expr + tok_pos);
3317 }
3318}
3319
3320/**
3321 * @brief Check that a value is valid for a leaf. If not applicable, does nothing.
3322 *
3323 * @param[in] exp Parsed XPath expression.
3324 * @param[in] set Set with the leaf/leaf-list.
3325 * @param[in] val_exp Index of the value (literal/number) in @p exp.
3326 * @param[in] equal_exp Index of the start of the equality expression in @p exp.
3327 * @param[in] last_equal_exp Index of the end of the equality expression in @p exp.
3328 */
3329static void
Michal Vasko40308e72020-10-20 16:38:40 +02003330warn_equality_value(const struct lyxp_expr *exp, struct lyxp_set *set, uint16_t val_exp, uint16_t equal_exp,
3331 uint16_t last_equal_exp)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003332{
3333 struct lysc_node *scnode;
3334 struct lysc_type *type;
3335 char *value;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003336 struct lyd_value storage;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003337 LY_ERR rc;
3338 struct ly_err_item *err = NULL;
3339
Michal Vasko69730152020-10-09 16:30:07 +02003340 if ((scnode = warn_get_scnode_in_ctx(set)) && (scnode->nodetype & (LYS_LEAF | LYS_LEAFLIST)) &&
3341 ((exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) || (exp->tokens[val_exp] == LYXP_TOKEN_NUMBER))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003342 /* check that the node can have the specified value */
3343 if (exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) {
3344 value = strndup(exp->expr + exp->tok_pos[val_exp] + 1, exp->tok_len[val_exp] - 2);
3345 } else {
3346 value = strndup(exp->expr + exp->tok_pos[val_exp], exp->tok_len[val_exp]);
3347 }
3348 if (!value) {
3349 LOGMEM(set->ctx);
3350 return;
3351 }
3352
3353 if ((((struct lysc_node_leaf *)scnode)->type->basetype == LY_TYPE_IDENT) && !strchr(value, ':')) {
3354 LOGWRN(set->ctx, "Identityref \"%s\" comparison with identity \"%s\" without prefix, consider adding"
Michal Vasko69730152020-10-09 16:30:07 +02003355 " a prefix or best using \"derived-from(-or-self)()\" functions.", scnode->name, value);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003356 LOGWRN(set->ctx, "Previous warning generated by XPath subexpression[%u] \"%.*s\".", exp->tok_pos[equal_exp],
Radek Krejci0f969882020-08-21 16:56:47 +02003357 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
Michal Vasko69730152020-10-09 16:30:07 +02003358 exp->expr + exp->tok_pos[equal_exp]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003359 }
3360
3361 type = ((struct lysc_node_leaf *)scnode)->type;
3362 if (type->basetype != LY_TYPE_IDENT) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003363 rc = type->plugin->store(set->ctx, type, value, strlen(value), 0, set->format, set->prefix_data,
Michal Vasko405cc9e2020-12-01 12:01:27 +01003364 LYD_HINT_DATA, scnode, &storage, NULL, &err);
Michal Vaskobf42e832020-11-23 16:59:42 +01003365 if (rc == LY_EINCOMPLETE) {
3366 rc = LY_SUCCESS;
3367 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003368
3369 if (err) {
3370 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type (%s).", value, err->msg);
3371 ly_err_free(err);
3372 } else if (rc != LY_SUCCESS) {
3373 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type.", value);
3374 }
3375 if (rc != LY_SUCCESS) {
3376 LOGWRN(set->ctx, "Previous warning generated by XPath subexpression[%u] \"%.*s\".", exp->tok_pos[equal_exp],
Radek Krejci0f969882020-08-21 16:56:47 +02003377 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
Michal Vasko69730152020-10-09 16:30:07 +02003378 exp->expr + exp->tok_pos[equal_exp]);
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003379 } else {
3380 type->plugin->free(set->ctx, &storage);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003381 }
3382 }
3383 free(value);
3384 }
3385}
3386
3387/*
3388 * XPath functions
3389 */
3390
3391/**
3392 * @brief Execute the YANG 1.1 bit-is-set(node-set, string) function. Returns LYXP_SET_BOOLEAN
3393 * depending on whether the first node bit value from the second argument is set.
3394 *
3395 * @param[in] args Array of arguments.
3396 * @param[in] arg_count Count of elements in @p args.
3397 * @param[in,out] set Context and result set at the same time.
3398 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003399 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003400 */
3401static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003402xpath_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 +02003403{
3404 struct lyd_node_term *leaf;
3405 struct lysc_node_leaf *sleaf;
3406 struct lysc_type_bits *bits;
3407 LY_ERR rc = LY_SUCCESS;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003408 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003409
3410 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003411 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003412 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003413 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3414 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3415 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3416 sleaf->name);
3417 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_BITS)) {
3418 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"bits\".", __func__, sleaf->name);
3419 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003420 }
3421
3422 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3423 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003424 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3425 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003426 } else if (!warn_is_string_type(sleaf->type)) {
3427 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003428 }
3429 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003430 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003431 return rc;
3432 }
3433
Michal Vaskod3678892020-05-21 10:06:58 +02003434 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003435 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 +02003436 return LY_EVALID;
3437 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003438 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003439 LY_CHECK_RET(rc);
3440
3441 set_fill_boolean(set, 0);
Michal Vaskod3678892020-05-21 10:06:58 +02003442 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003443 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
Michal Vasko69730152020-10-09 16:30:07 +02003444 if ((leaf->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) &&
3445 (((struct lysc_node_leaf *)leaf->schema)->type->basetype == LY_TYPE_BITS)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003446 bits = (struct lysc_type_bits *)((struct lysc_node_leaf *)leaf->schema)->type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003447 LY_ARRAY_FOR(bits->bits, u) {
3448 if (!strcmp(bits->bits[u].name, args[1]->val.str)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003449 set_fill_boolean(set, 1);
3450 break;
3451 }
3452 }
3453 }
3454 }
3455
3456 return LY_SUCCESS;
3457}
3458
3459/**
3460 * @brief Execute the XPath boolean(object) function. Returns LYXP_SET_BOOLEAN
3461 * with the argument converted to boolean.
3462 *
3463 * @param[in] args Array of arguments.
3464 * @param[in] arg_count Count of elements in @p args.
3465 * @param[in,out] set Context and result set at the same time.
3466 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003467 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003468 */
3469static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003470xpath_boolean(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003471{
3472 LY_ERR rc;
3473
3474 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02003475 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003476 return LY_SUCCESS;
3477 }
3478
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003479 rc = lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003480 LY_CHECK_RET(rc);
3481 set_fill_set(set, args[0]);
3482
3483 return LY_SUCCESS;
3484}
3485
3486/**
3487 * @brief Execute the XPath ceiling(number) function. Returns LYXP_SET_NUMBER
3488 * with the first argument rounded up to the nearest integer.
3489 *
3490 * @param[in] args Array of arguments.
3491 * @param[in] arg_count Count of elements in @p args.
3492 * @param[in,out] set Context and result set at the same time.
3493 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003494 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003495 */
3496static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003497xpath_ceiling(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003498{
3499 struct lysc_node_leaf *sleaf;
3500 LY_ERR rc = LY_SUCCESS;
3501
3502 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003503 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003504 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003505 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3506 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3507 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3508 sleaf->name);
3509 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
3510 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
3511 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003512 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003513 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003514 return rc;
3515 }
3516
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003517 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003518 LY_CHECK_RET(rc);
3519 if ((long long)args[0]->val.num != args[0]->val.num) {
3520 set_fill_number(set, ((long long)args[0]->val.num) + 1);
3521 } else {
3522 set_fill_number(set, args[0]->val.num);
3523 }
3524
3525 return LY_SUCCESS;
3526}
3527
3528/**
3529 * @brief Execute the XPath concat(string, string, string*) function.
3530 * Returns LYXP_SET_STRING with the concatenation of all the arguments.
3531 *
3532 * @param[in] args Array of arguments.
3533 * @param[in] arg_count Count of elements in @p args.
3534 * @param[in,out] set Context and result set at the same time.
3535 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003536 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003537 */
3538static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003539xpath_concat(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003540{
3541 uint16_t i;
3542 char *str = NULL;
3543 size_t used = 1;
3544 LY_ERR rc = LY_SUCCESS;
3545 struct lysc_node_leaf *sleaf;
3546
3547 if (options & LYXP_SCNODE_ALL) {
3548 for (i = 0; i < arg_count; ++i) {
3549 if ((args[i]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[i]))) {
3550 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3551 LOGWRN(set->ctx, "Argument #%u of %s is a %s node \"%s\".",
Michal Vasko69730152020-10-09 16:30:07 +02003552 i + 1, __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003553 } else if (!warn_is_string_type(sleaf->type)) {
Radek Krejci70124c82020-08-14 22:17:03 +02003554 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 +02003555 }
3556 }
3557 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003558 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003559 return rc;
3560 }
3561
3562 for (i = 0; i < arg_count; ++i) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003563 rc = lyxp_set_cast(args[i], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003564 if (rc != LY_SUCCESS) {
3565 free(str);
3566 return rc;
3567 }
3568
3569 str = ly_realloc(str, (used + strlen(args[i]->val.str)) * sizeof(char));
3570 LY_CHECK_ERR_RET(!str, LOGMEM(set->ctx), LY_EMEM);
3571 strcpy(str + used - 1, args[i]->val.str);
3572 used += strlen(args[i]->val.str);
3573 }
3574
3575 /* free, kind of */
Michal Vaskod3678892020-05-21 10:06:58 +02003576 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003577 set->type = LYXP_SET_STRING;
3578 set->val.str = str;
3579
3580 return LY_SUCCESS;
3581}
3582
3583/**
3584 * @brief Execute the XPath contains(string, string) function.
3585 * Returns LYXP_SET_BOOLEAN whether the second argument can
3586 * be found in the first or not.
3587 *
3588 * @param[in] args Array of arguments.
3589 * @param[in] arg_count Count of elements in @p args.
3590 * @param[in,out] set Context and result set at the same time.
3591 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003592 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003593 */
3594static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003595xpath_contains(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003596{
3597 struct lysc_node_leaf *sleaf;
3598 LY_ERR rc = LY_SUCCESS;
3599
3600 if (options & LYXP_SCNODE_ALL) {
3601 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3602 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003603 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3604 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003605 } else if (!warn_is_string_type(sleaf->type)) {
3606 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003607 }
3608 }
3609
3610 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3611 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003612 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3613 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003614 } else if (!warn_is_string_type(sleaf->type)) {
3615 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003616 }
3617 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003618 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003619 return rc;
3620 }
3621
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003622 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003623 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003624 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003625 LY_CHECK_RET(rc);
3626
3627 if (strstr(args[0]->val.str, args[1]->val.str)) {
3628 set_fill_boolean(set, 1);
3629 } else {
3630 set_fill_boolean(set, 0);
3631 }
3632
3633 return LY_SUCCESS;
3634}
3635
3636/**
3637 * @brief Execute the XPath count(node-set) function. Returns LYXP_SET_NUMBER
3638 * with the size of the node-set from the argument.
3639 *
3640 * @param[in] args Array of arguments.
3641 * @param[in] arg_count Count of elements in @p args.
3642 * @param[in,out] set Context and result set at the same time.
3643 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003644 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003645 */
3646static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003647xpath_count(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003648{
Michal Vasko03ff5a72019-09-11 13:49:33 +02003649 LY_ERR rc = LY_SUCCESS;
3650
3651 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003652 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003653 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003654 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003655 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003656 return rc;
3657 }
3658
Michal Vasko03ff5a72019-09-11 13:49:33 +02003659 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003660 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "count(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003661 return LY_EVALID;
3662 }
3663
3664 set_fill_number(set, args[0]->used);
3665 return LY_SUCCESS;
3666}
3667
3668/**
3669 * @brief Execute the XPath current() function. Returns LYXP_SET_NODE_SET
3670 * with the context with the intial node.
3671 *
3672 * @param[in] args Array of arguments.
3673 * @param[in] arg_count Count of elements in @p args.
3674 * @param[in,out] set Context and result set at the same time.
3675 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003676 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003677 */
3678static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003679xpath_current(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003680{
3681 if (arg_count || args) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003682 LOGVAL(set->ctx, LY_VCODE_XP_INARGCOUNT, arg_count, "current()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003683 return LY_EVALID;
3684 }
3685
3686 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02003687 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003688
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003689 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, set->cur_scnode, LYXP_NODE_ELEM, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02003690 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02003691 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003692
3693 /* position is filled later */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003694 set_insert_node(set, set->cur_node, 0, LYXP_NODE_ELEM, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003695 }
3696
3697 return LY_SUCCESS;
3698}
3699
3700/**
3701 * @brief Execute the YANG 1.1 deref(node-set) function. Returns LYXP_SET_NODE_SET with either
3702 * leafref or instance-identifier target node(s).
3703 *
3704 * @param[in] args Array of arguments.
3705 * @param[in] arg_count Count of elements in @p args.
3706 * @param[in,out] set Context and result set at the same time.
3707 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003708 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003709 */
3710static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003711xpath_deref(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003712{
3713 struct lyd_node_term *leaf;
Michal Vasko42e497c2020-01-06 08:38:25 +01003714 struct lysc_node_leaf *sleaf = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02003715 struct lysc_type_leafref *lref;
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003716 const struct lysc_node *target;
Michal Vasko004d3152020-06-11 19:59:22 +02003717 struct ly_path *p;
3718 struct lyd_node *node;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003719 char *errmsg = NULL;
Michal Vasko00cbf532020-06-15 13:58:47 +02003720 uint8_t oper;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003721 LY_ERR rc = LY_SUCCESS;
3722
3723 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003724 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003725 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003726 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3727 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3728 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3729 sleaf->name);
3730 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_LEAFREF) && !warn_is_specific_type(sleaf->type, LY_TYPE_INST)) {
3731 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"leafref\" nor \"instance-identifier\".",
3732 __func__, sleaf->name);
3733 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003734 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003735 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko42e497c2020-01-06 08:38:25 +01003736 if (sleaf && (sleaf->type->basetype == LY_TYPE_LEAFREF)) {
Michal Vasko004d3152020-06-11 19:59:22 +02003737 lref = (struct lysc_type_leafref *)sleaf->type;
Michal Vaskod1e53b92021-01-28 13:11:06 +01003738 oper = (sleaf->flags & LYS_IS_OUTPUT) ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT;
Michal Vasko004d3152020-06-11 19:59:22 +02003739
3740 /* it was already evaluated on schema, it must succeed */
Radek Krejcid5d37432021-03-12 13:46:40 +01003741 rc = ly_path_compile(set->ctx, lref->cur_mod, &sleaf->node, NULL, lref->path, LY_PATH_LREF_TRUE, oper,
Radek Krejci8df109d2021-04-23 12:19:08 +02003742 LY_PATH_TARGET_MANY, LY_VALUE_SCHEMA_RESOLVED, lref->prefixes, NULL, &p);
Michal Vasko004d3152020-06-11 19:59:22 +02003743 assert(!rc);
3744
3745 /* get the target node */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003746 target = p[LY_ARRAY_COUNT(p) - 1].node;
Michal Vasko004d3152020-06-11 19:59:22 +02003747 ly_path_free(set->ctx, p);
3748
Radek Krejciaa6b53f2020-08-27 15:19:03 +02003749 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, target, LYXP_NODE_ELEM, NULL));
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003750 }
3751
Michal Vasko03ff5a72019-09-11 13:49:33 +02003752 return rc;
3753 }
3754
Michal Vaskod3678892020-05-21 10:06:58 +02003755 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003756 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "deref(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003757 return LY_EVALID;
3758 }
3759
Michal Vaskod3678892020-05-21 10:06:58 +02003760 lyxp_set_free_content(set);
3761 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003762 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
3763 sleaf = (struct lysc_node_leaf *)leaf->schema;
3764 if (sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
3765 if (sleaf->type->basetype == LY_TYPE_LEAFREF) {
3766 /* find leafref target */
Radek Krejci0b013302021-03-29 15:22:32 +02003767 if (lyplg_type_resolve_leafref((struct lysc_type_leafref *)sleaf->type, &leaf->node, &leaf->value, set->tree,
Michal Vasko9e685082021-01-29 14:49:09 +01003768 &node, &errmsg)) {
Michal Vasko004d3152020-06-11 19:59:22 +02003769 LOGERR(set->ctx, LY_EVALID, errmsg);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003770 free(errmsg);
Michal Vasko004d3152020-06-11 19:59:22 +02003771 return LY_EVALID;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003772 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003773 } else {
3774 assert(sleaf->type->basetype == LY_TYPE_INST);
Michal Vasko004d3152020-06-11 19:59:22 +02003775 if (ly_path_eval(leaf->value.target, set->tree, &node)) {
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003776 LOGERR(set->ctx, LY_EVALID, "Invalid instance-identifier \"%s\" value - required instance not found.",
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02003777 lyd_get_value(&leaf->node));
Michal Vasko03ff5a72019-09-11 13:49:33 +02003778 return LY_EVALID;
3779 }
3780 }
Michal Vasko004d3152020-06-11 19:59:22 +02003781
3782 /* insert it */
3783 set_insert_node(set, node, 0, LYXP_NODE_ELEM, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003784 }
3785 }
3786
3787 return LY_SUCCESS;
3788}
3789
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003790static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02003791xpath_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 +02003792{
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01003793 uint32_t i;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003794 LY_ARRAY_COUNT_TYPE u;
3795 struct lyd_node_term *leaf;
3796 struct lysc_node_leaf *sleaf;
3797 struct lyd_meta *meta;
Michal Vasko93923692021-05-07 15:28:02 +02003798 struct lyd_value *val;
3799 const struct lys_module *mod;
3800 const char *id_name;
3801 uint16_t id_len;
3802 struct lysc_ident *id;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003803 LY_ERR rc = LY_SUCCESS;
Radek Krejci857189e2020-09-01 13:26:36 +02003804 ly_bool found;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003805
3806 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003807 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003808 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", func);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003809 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3810 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3811 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype),
3812 sleaf->name);
3813 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_IDENT)) {
3814 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"identityref\".", func, sleaf->name);
3815 }
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003816 }
3817
3818 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3819 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3820 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype),
Michal Vasko69730152020-10-09 16:30:07 +02003821 sleaf->name);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003822 } else if (!warn_is_string_type(sleaf->type)) {
3823 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", func, sleaf->name);
3824 }
3825 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003826 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003827 return rc;
3828 }
3829
3830 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003831 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 +02003832 return LY_EVALID;
3833 }
3834 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
3835 LY_CHECK_RET(rc);
3836
Michal Vasko93923692021-05-07 15:28:02 +02003837 /* parse the identity */
3838 id_name = args[1]->val.str;
3839 id_len = strlen(id_name);
3840 rc = moveto_resolve_model(&id_name, &id_len, set, set->cur_node ? set->cur_node->schema : NULL, &mod);
3841 LY_CHECK_RET(rc);
3842 if (!mod) {
3843 LOGVAL(set->ctx, LYVE_XPATH, "Identity \"%.*s\" without a prefix.", (int)id_len, id_name);
3844 return LY_EVALID;
3845 }
3846
3847 /* find the identity */
3848 found = 0;
3849 LY_ARRAY_FOR(mod->identities, u) {
3850 if (!ly_strncmp(mod->identities[u].name, id_name, id_len)) {
3851 /* we have match */
3852 found = 1;
3853 break;
3854 }
3855 }
3856 if (!found) {
3857 LOGVAL(set->ctx, LYVE_XPATH, "Identity \"%.*s\" not found in module \"%s\".", (int)id_len, id_name, mod->name);
3858 return LY_EVALID;
3859 }
3860 id = &mod->identities[u];
3861
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003862 set_fill_boolean(set, 0);
3863 found = 0;
3864 for (i = 0; i < args[0]->used; ++i) {
3865 if ((args[0]->val.nodes[i].type != LYXP_NODE_ELEM) && (args[0]->val.nodes[i].type != LYXP_NODE_META)) {
3866 continue;
3867 }
3868
3869 if (args[0]->val.nodes[i].type == LYXP_NODE_ELEM) {
3870 leaf = (struct lyd_node_term *)args[0]->val.nodes[i].node;
3871 sleaf = (struct lysc_node_leaf *)leaf->schema;
3872 val = &leaf->value;
3873 if (!(sleaf->nodetype & LYD_NODE_TERM) || (leaf->value.realtype->basetype != LY_TYPE_IDENT)) {
3874 /* uninteresting */
3875 continue;
3876 }
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003877 } else {
3878 meta = args[0]->val.meta[i].meta;
3879 val = &meta->value;
3880 if (val->realtype->basetype != LY_TYPE_IDENT) {
3881 /* uninteresting */
3882 continue;
3883 }
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003884 }
3885
Michal Vasko93923692021-05-07 15:28:02 +02003886 /* check the identity itself */
3887 if (self_match && (id == val->ident)) {
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003888 set_fill_boolean(set, 1);
3889 found = 1;
3890 }
Michal Vasko93923692021-05-07 15:28:02 +02003891 if (!found && !lyplg_type_identity_isderived(id, val->ident)) {
3892 set_fill_boolean(set, 1);
3893 found = 1;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003894 }
3895
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003896 if (found) {
3897 break;
3898 }
3899 }
3900
3901 return LY_SUCCESS;
3902}
3903
Michal Vasko03ff5a72019-09-11 13:49:33 +02003904/**
3905 * @brief Execute the YANG 1.1 derived-from(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
3906 * on whether the first argument nodes contain a node of an identity derived from the second
3907 * argument identity.
3908 *
3909 * @param[in] args Array of arguments.
3910 * @param[in] arg_count Count of elements in @p args.
3911 * @param[in,out] set Context and result set at the same time.
3912 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003913 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003914 */
3915static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003916xpath_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 +02003917{
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003918 return xpath_derived_(args, set, options, 0, __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003919}
3920
3921/**
3922 * @brief Execute the YANG 1.1 derived-from-or-self(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
3923 * on whether the first argument nodes contain a node of an identity that either is or is derived from
3924 * the second argument identity.
3925 *
3926 * @param[in] args Array of arguments.
3927 * @param[in] arg_count Count of elements in @p args.
3928 * @param[in,out] set Context and result set at the same time.
3929 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003930 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003931 */
3932static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003933xpath_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 +02003934{
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003935 return xpath_derived_(args, set, options, 1, __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003936}
3937
3938/**
3939 * @brief Execute the YANG 1.1 enum-value(node-set) function. Returns LYXP_SET_NUMBER
3940 * with the integer value of the first node's enum value, otherwise NaN.
3941 *
3942 * @param[in] args Array of arguments.
3943 * @param[in] arg_count Count of elements in @p args.
3944 * @param[in,out] set Context and result set at the same time.
3945 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003946 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003947 */
3948static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003949xpath_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 +02003950{
3951 struct lyd_node_term *leaf;
3952 struct lysc_node_leaf *sleaf;
3953 LY_ERR rc = LY_SUCCESS;
3954
3955 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003956 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003957 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02003958 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3959 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3960 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
3961 sleaf->name);
3962 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_ENUM)) {
3963 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"enumeration\".", __func__, sleaf->name);
3964 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003965 }
Michal Vasko1a09b212021-05-06 13:00:10 +02003966 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003967 return rc;
3968 }
3969
Michal Vaskod3678892020-05-21 10:06:58 +02003970 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003971 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "enum-value(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003972 return LY_EVALID;
3973 }
3974
3975 set_fill_number(set, NAN);
Michal Vaskod3678892020-05-21 10:06:58 +02003976 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003977 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
3978 sleaf = (struct lysc_node_leaf *)leaf->schema;
3979 if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_ENUM)) {
3980 set_fill_number(set, leaf->value.enum_item->value);
3981 }
3982 }
3983
3984 return LY_SUCCESS;
3985}
3986
3987/**
3988 * @brief Execute the XPath false() function. Returns LYXP_SET_BOOLEAN
3989 * with false value.
3990 *
3991 * @param[in] args Array of arguments.
3992 * @param[in] arg_count Count of elements in @p args.
3993 * @param[in,out] set Context and result set at the same time.
3994 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003995 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003996 */
3997static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003998xpath_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 +02003999{
4000 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004001 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004002 return LY_SUCCESS;
4003 }
4004
4005 set_fill_boolean(set, 0);
4006 return LY_SUCCESS;
4007}
4008
4009/**
4010 * @brief Execute the XPath floor(number) function. Returns LYXP_SET_NUMBER
4011 * with the first argument floored (truncated).
4012 *
4013 * @param[in] args Array of arguments.
4014 * @param[in] arg_count Count of elements in @p args.
4015 * @param[in,out] set Context and result set at the same time.
4016 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004017 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004018 */
4019static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004020xpath_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 +02004021{
4022 LY_ERR rc;
4023
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004024 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004025 LY_CHECK_RET(rc);
4026 if (isfinite(args[0]->val.num)) {
4027 set_fill_number(set, (long long)args[0]->val.num);
4028 }
4029
4030 return LY_SUCCESS;
4031}
4032
4033/**
4034 * @brief Execute the XPath lang(string) function. Returns LYXP_SET_BOOLEAN
4035 * whether the language of the text matches the one from the argument.
4036 *
4037 * @param[in] args Array of arguments.
4038 * @param[in] arg_count Count of elements in @p args.
4039 * @param[in,out] set Context and result set at the same time.
4040 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004041 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004042 */
4043static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004044xpath_lang(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004045{
4046 const struct lyd_node *node;
4047 struct lysc_node_leaf *sleaf;
Michal Vasko9f96a052020-03-10 09:41:45 +01004048 struct lyd_meta *meta = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004049 const char *val;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004050 LY_ERR rc = LY_SUCCESS;
4051
4052 if (options & LYXP_SCNODE_ALL) {
4053 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4054 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004055 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4056 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004057 } else if (!warn_is_string_type(sleaf->type)) {
4058 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004059 }
4060 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004061 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004062 return rc;
4063 }
4064
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004065 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004066 LY_CHECK_RET(rc);
4067
Michal Vasko03ff5a72019-09-11 13:49:33 +02004068 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004069 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "lang(string)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004070 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004071 } else if (!set->used) {
4072 set_fill_boolean(set, 0);
4073 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004074 }
4075
4076 switch (set->val.nodes[0].type) {
4077 case LYXP_NODE_ELEM:
4078 case LYXP_NODE_TEXT:
4079 node = set->val.nodes[0].node;
4080 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004081 case LYXP_NODE_META:
4082 node = set->val.meta[0].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004083 break;
4084 default:
4085 /* nothing to do with roots */
4086 set_fill_boolean(set, 0);
4087 return LY_SUCCESS;
4088 }
4089
Michal Vasko9f96a052020-03-10 09:41:45 +01004090 /* find lang metadata */
Michal Vasko9e685082021-01-29 14:49:09 +01004091 for ( ; node; node = lyd_parent(node)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01004092 for (meta = node->meta; meta; meta = meta->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004093 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004094 if (meta->name && !strcmp(meta->name, "lang") && !strcmp(meta->annotation->module->name, "xml")) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004095 break;
4096 }
4097 }
4098
Michal Vasko9f96a052020-03-10 09:41:45 +01004099 if (meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004100 break;
4101 }
4102 }
4103
4104 /* compare languages */
Michal Vasko9f96a052020-03-10 09:41:45 +01004105 if (!meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004106 set_fill_boolean(set, 0);
4107 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004108 uint64_t i;
4109
Radek Krejci6d5ba0c2021-04-26 07:49:59 +02004110 val = lyd_get_meta_value(meta);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004111 for (i = 0; args[0]->val.str[i]; ++i) {
4112 if (tolower(args[0]->val.str[i]) != tolower(val[i])) {
4113 set_fill_boolean(set, 0);
4114 break;
4115 }
4116 }
4117 if (!args[0]->val.str[i]) {
4118 if (!val[i] || (val[i] == '-')) {
4119 set_fill_boolean(set, 1);
4120 } else {
4121 set_fill_boolean(set, 0);
4122 }
4123 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004124 }
4125
4126 return LY_SUCCESS;
4127}
4128
4129/**
4130 * @brief Execute the XPath last() function. Returns LYXP_SET_NUMBER
4131 * with the context size.
4132 *
4133 * @param[in] args Array of arguments.
4134 * @param[in] arg_count Count of elements in @p args.
4135 * @param[in,out] set Context and result set at the same time.
4136 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004137 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004138 */
4139static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004140xpath_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 +02004141{
4142 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004143 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004144 return LY_SUCCESS;
4145 }
4146
Michal Vasko03ff5a72019-09-11 13:49:33 +02004147 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004148 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "last()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004149 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004150 } else if (!set->used) {
4151 set_fill_number(set, 0);
4152 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004153 }
4154
4155 set_fill_number(set, set->ctx_size);
4156 return LY_SUCCESS;
4157}
4158
4159/**
4160 * @brief Execute the XPath local-name(node-set?) function. Returns LYXP_SET_STRING
4161 * with the node name without namespace from the argument or the context.
4162 *
4163 * @param[in] args Array of arguments.
4164 * @param[in] arg_count Count of elements in @p args.
4165 * @param[in,out] set Context and result set at the same time.
4166 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004167 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004168 */
4169static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004170xpath_local_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004171{
4172 struct lyxp_set_node *item;
Michal Vasko69730152020-10-09 16:30:07 +02004173
Michal Vasko03ff5a72019-09-11 13:49:33 +02004174 /* suppress unused variable warning */
4175 (void)options;
4176
4177 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004178 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004179 return LY_SUCCESS;
4180 }
4181
4182 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004183 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004184 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004185 "local-name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004186 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004187 } else if (!args[0]->used) {
4188 set_fill_string(set, "", 0);
4189 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004190 }
4191
4192 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004193 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004194
4195 item = &args[0]->val.nodes[0];
4196 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004197 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004198 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "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 (!set->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(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004207
4208 item = &set->val.nodes[0];
4209 }
4210
4211 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004212 case LYXP_NODE_NONE:
4213 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004214 case LYXP_NODE_ROOT:
4215 case LYXP_NODE_ROOT_CONFIG:
4216 case LYXP_NODE_TEXT:
4217 set_fill_string(set, "", 0);
4218 break;
4219 case LYXP_NODE_ELEM:
4220 set_fill_string(set, item->node->schema->name, strlen(item->node->schema->name));
4221 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004222 case LYXP_NODE_META:
4223 set_fill_string(set, ((struct lyd_meta *)item->node)->name, strlen(((struct lyd_meta *)item->node)->name));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004224 break;
4225 }
4226
4227 return LY_SUCCESS;
4228}
4229
4230/**
4231 * @brief Execute the XPath name(node-set?) function. Returns LYXP_SET_STRING
4232 * with the node name fully qualified (with namespace) from the argument or the context.
4233 *
4234 * @param[in] args Array of arguments.
4235 * @param[in] arg_count Count of elements in @p args.
4236 * @param[in,out] set Context and result set at the same time.
4237 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004238 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004239 */
4240static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004241xpath_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004242{
4243 struct lyxp_set_node *item;
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004244 struct lys_module *mod = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004245 char *str;
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004246 const char *name = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004247
4248 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004249 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004250 return LY_SUCCESS;
4251 }
4252
4253 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004254 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004255 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004256 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004257 } else if (!args[0]->used) {
4258 set_fill_string(set, "", 0);
4259 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004260 }
4261
4262 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004263 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004264
4265 item = &args[0]->val.nodes[0];
4266 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004267 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004268 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004269 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004270 } else if (!set->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(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004277
4278 item = &set->val.nodes[0];
4279 }
4280
4281 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004282 case LYXP_NODE_NONE:
4283 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004284 case LYXP_NODE_ROOT:
4285 case LYXP_NODE_ROOT_CONFIG:
4286 case LYXP_NODE_TEXT:
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004287 /* keep NULL */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004288 break;
4289 case LYXP_NODE_ELEM:
4290 mod = item->node->schema->module;
4291 name = item->node->schema->name;
4292 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004293 case LYXP_NODE_META:
4294 mod = ((struct lyd_meta *)item->node)->annotation->module;
4295 name = ((struct lyd_meta *)item->node)->name;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004296 break;
4297 }
4298
4299 if (mod && name) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004300 int rc = asprintf(&str, "%s:%s", ly_get_prefix(mod, set->format, set->prefix_data), name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004301 LY_CHECK_ERR_RET(rc == -1, LOGMEM(set->ctx), LY_EMEM);
4302 set_fill_string(set, str, strlen(str));
4303 free(str);
4304 } else {
4305 set_fill_string(set, "", 0);
4306 }
4307
4308 return LY_SUCCESS;
4309}
4310
4311/**
4312 * @brief Execute the XPath namespace-uri(node-set?) function. Returns LYXP_SET_STRING
4313 * with the namespace of the node from the argument or the context.
4314 *
4315 * @param[in] args Array of arguments.
4316 * @param[in] arg_count Count of elements in @p args.
4317 * @param[in,out] set Context and result set at the same time.
4318 * @param[in] options XPath options.
4319 * @return LY_ERR (LY_EINVAL for wrong arguments on schema)
4320 */
4321static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004322xpath_namespace_uri(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004323{
4324 struct lyxp_set_node *item;
4325 struct lys_module *mod;
Michal Vasko69730152020-10-09 16:30:07 +02004326
Michal Vasko03ff5a72019-09-11 13:49:33 +02004327 /* suppress unused variable warning */
4328 (void)options;
4329
4330 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004331 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004332 return LY_SUCCESS;
4333 }
4334
4335 if (arg_count) {
Michal Vaskod3678892020-05-21 10:06:58 +02004336 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004337 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
Michal Vasko69730152020-10-09 16:30:07 +02004338 "namespace-uri(node-set?)");
Michal Vaskod3678892020-05-21 10:06:58 +02004339 return LY_EVALID;
4340 } else if (!args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004341 set_fill_string(set, "", 0);
4342 return LY_SUCCESS;
4343 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004344
4345 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004346 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004347
4348 item = &args[0]->val.nodes[0];
4349 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004350 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004351 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "namespace-uri(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004352 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004353 } else if (!set->used) {
4354 set_fill_string(set, "", 0);
4355 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004356 }
4357
4358 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004359 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004360
4361 item = &set->val.nodes[0];
4362 }
4363
4364 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004365 case LYXP_NODE_NONE:
4366 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004367 case LYXP_NODE_ROOT:
4368 case LYXP_NODE_ROOT_CONFIG:
4369 case LYXP_NODE_TEXT:
4370 set_fill_string(set, "", 0);
4371 break;
4372 case LYXP_NODE_ELEM:
Michal Vasko9f96a052020-03-10 09:41:45 +01004373 case LYXP_NODE_META:
Michal Vasko03ff5a72019-09-11 13:49:33 +02004374 if (item->type == LYXP_NODE_ELEM) {
4375 mod = item->node->schema->module;
Michal Vasko9f96a052020-03-10 09:41:45 +01004376 } else { /* LYXP_NODE_META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004377 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004378 mod = ((struct lyd_meta *)item->node)->annotation->module;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004379 }
4380
4381 set_fill_string(set, mod->ns, strlen(mod->ns));
4382 break;
4383 }
4384
4385 return LY_SUCCESS;
4386}
4387
4388/**
4389 * @brief Execute the XPath node() function (node type). Returns LYXP_SET_NODE_SET
4390 * with only nodes from the context. In practice it either leaves the context
4391 * as it is or returns an empty node set.
4392 *
4393 * @param[in] args Array of arguments.
4394 * @param[in] arg_count Count of elements in @p args.
4395 * @param[in,out] set Context and result set at the same time.
4396 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004397 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004398 */
4399static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004400xpath_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 +02004401{
4402 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004403 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004404 return LY_SUCCESS;
4405 }
4406
4407 if (set->type != LYXP_SET_NODE_SET) {
Michal Vaskod3678892020-05-21 10:06:58 +02004408 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004409 }
4410 return LY_SUCCESS;
4411}
4412
4413/**
4414 * @brief Execute the XPath normalize-space(string?) function. Returns LYXP_SET_STRING
4415 * with normalized value (no leading, trailing, double white spaces) of the node
4416 * from the argument or the context.
4417 *
4418 * @param[in] args Array of arguments.
4419 * @param[in] arg_count Count of elements in @p args.
4420 * @param[in,out] set Context and result set at the same time.
4421 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004422 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004423 */
4424static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004425xpath_normalize_space(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004426{
4427 uint16_t i, new_used;
4428 char *new;
Radek Krejci857189e2020-09-01 13:26:36 +02004429 ly_bool have_spaces = 0, space_before = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004430 struct lysc_node_leaf *sleaf;
4431 LY_ERR rc = LY_SUCCESS;
4432
4433 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004434 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) &&
4435 (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004436 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004437 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4438 sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004439 } else if (!warn_is_string_type(sleaf->type)) {
4440 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004441 }
4442 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004443 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004444 return rc;
4445 }
4446
4447 if (arg_count) {
4448 set_fill_set(set, args[0]);
4449 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004450 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004451 LY_CHECK_RET(rc);
4452
4453 /* is there any normalization necessary? */
4454 for (i = 0; set->val.str[i]; ++i) {
4455 if (is_xmlws(set->val.str[i])) {
4456 if ((i == 0) || space_before || (!set->val.str[i + 1])) {
4457 have_spaces = 1;
4458 break;
4459 }
4460 space_before = 1;
4461 } else {
4462 space_before = 0;
4463 }
4464 }
4465
4466 /* yep, there is */
4467 if (have_spaces) {
4468 /* it's enough, at least one character will go, makes space for ending '\0' */
4469 new = malloc(strlen(set->val.str) * sizeof(char));
4470 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4471 new_used = 0;
4472
4473 space_before = 0;
4474 for (i = 0; set->val.str[i]; ++i) {
4475 if (is_xmlws(set->val.str[i])) {
4476 if ((i == 0) || space_before) {
4477 space_before = 1;
4478 continue;
4479 } else {
4480 space_before = 1;
4481 }
4482 } else {
4483 space_before = 0;
4484 }
4485
4486 new[new_used] = (space_before ? ' ' : set->val.str[i]);
4487 ++new_used;
4488 }
4489
4490 /* at worst there is one trailing space now */
4491 if (new_used && is_xmlws(new[new_used - 1])) {
4492 --new_used;
4493 }
4494
4495 new = ly_realloc(new, (new_used + 1) * sizeof(char));
4496 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4497 new[new_used] = '\0';
4498
4499 free(set->val.str);
4500 set->val.str = new;
4501 }
4502
4503 return LY_SUCCESS;
4504}
4505
4506/**
4507 * @brief Execute the XPath not(boolean) function. Returns LYXP_SET_BOOLEAN
4508 * with the argument converted to boolean and logically inverted.
4509 *
4510 * @param[in] args Array of arguments.
4511 * @param[in] arg_count Count of elements in @p args.
4512 * @param[in,out] set Context and result set at the same time.
4513 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004514 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004515 */
4516static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004517xpath_not(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004518{
4519 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004520 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004521 return LY_SUCCESS;
4522 }
4523
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004524 lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02004525 if (args[0]->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004526 set_fill_boolean(set, 0);
4527 } else {
4528 set_fill_boolean(set, 1);
4529 }
4530
4531 return LY_SUCCESS;
4532}
4533
4534/**
4535 * @brief Execute the XPath number(object?) function. Returns LYXP_SET_NUMBER
4536 * with the number representation of either the argument or the context.
4537 *
4538 * @param[in] args Array of arguments.
4539 * @param[in] arg_count Count of elements in @p args.
4540 * @param[in,out] set Context and result set at the same time.
4541 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004542 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004543 */
4544static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004545xpath_number(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004546{
4547 LY_ERR rc;
4548
4549 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004550 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004551 return LY_SUCCESS;
4552 }
4553
4554 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004555 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004556 LY_CHECK_RET(rc);
4557 set_fill_set(set, args[0]);
4558 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004559 rc = lyxp_set_cast(set, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004560 LY_CHECK_RET(rc);
4561 }
4562
4563 return LY_SUCCESS;
4564}
4565
4566/**
4567 * @brief Execute the XPath position() function. Returns LYXP_SET_NUMBER
4568 * with the context position.
4569 *
4570 * @param[in] args Array of arguments.
4571 * @param[in] arg_count Count of elements in @p args.
4572 * @param[in,out] set Context and result set at the same time.
4573 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004574 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004575 */
4576static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004577xpath_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 +02004578{
4579 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004580 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004581 return LY_SUCCESS;
4582 }
4583
Michal Vasko03ff5a72019-09-11 13:49:33 +02004584 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004585 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "position()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004586 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004587 } else if (!set->used) {
4588 set_fill_number(set, 0);
4589 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004590 }
4591
4592 set_fill_number(set, set->ctx_pos);
4593
4594 /* UNUSED in 'Release' build type */
4595 (void)options;
4596 return LY_SUCCESS;
4597}
4598
4599/**
4600 * @brief Execute the YANG 1.1 re-match(string, string) function. Returns LYXP_SET_BOOLEAN
4601 * depending on whether the second argument regex matches the first argument string. For details refer to
4602 * YANG 1.1 RFC section 10.2.1.
4603 *
4604 * @param[in] args Array of arguments.
4605 * @param[in] arg_count Count of elements in @p args.
4606 * @param[in,out] set Context and result set at the same time.
4607 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004608 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004609 */
4610static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004611xpath_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 +02004612{
4613 struct lysc_pattern **patterns = NULL, **pattern;
4614 struct lysc_node_leaf *sleaf;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004615 LY_ERR rc = LY_SUCCESS;
4616 struct ly_err_item *err;
4617
4618 if (options & LYXP_SCNODE_ALL) {
4619 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4620 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4621 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 +02004622 } else if (!warn_is_string_type(sleaf->type)) {
4623 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004624 }
4625 }
4626
4627 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4628 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4629 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 +02004630 } else if (!warn_is_string_type(sleaf->type)) {
4631 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004632 }
4633 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004634 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004635 return rc;
4636 }
4637
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004638 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004639 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004640 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004641 LY_CHECK_RET(rc);
4642
4643 LY_ARRAY_NEW_RET(set->ctx, patterns, pattern, LY_EMEM);
Radek IÅ¡a45802b52021-02-09 09:21:58 +01004644 *pattern = calloc(1, sizeof **pattern);
Radek Krejciddace2c2021-01-08 11:30:56 +01004645 LOG_LOCSET(NULL, set->cur_node, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01004646 rc = lys_compile_type_pattern_check(set->ctx, args[1]->val.str, &(*pattern)->code);
Radek Krejciddace2c2021-01-08 11:30:56 +01004647 LOG_LOCBACK(0, 1, 0, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004648 if (rc != LY_SUCCESS) {
4649 LY_ARRAY_FREE(patterns);
4650 return rc;
4651 }
4652
Radek Krejci0b013302021-03-29 15:22:32 +02004653 rc = lyplg_type_validate_patterns(patterns, args[0]->val.str, strlen(args[0]->val.str), &err);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004654 pcre2_code_free((*pattern)->code);
4655 free(*pattern);
4656 LY_ARRAY_FREE(patterns);
4657 if (rc && (rc != LY_EVALID)) {
Michal Vasko177d0ed2020-11-23 16:43:03 +01004658 ly_err_print(set->ctx, err);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004659 ly_err_free(err);
4660 return rc;
4661 }
4662
4663 if (rc == LY_EVALID) {
4664 ly_err_free(err);
4665 set_fill_boolean(set, 0);
4666 } else {
4667 set_fill_boolean(set, 1);
4668 }
4669
4670 return LY_SUCCESS;
4671}
4672
4673/**
4674 * @brief Execute the XPath round(number) function. Returns LYXP_SET_NUMBER
4675 * with the rounded first argument. For details refer to
4676 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-round.
4677 *
4678 * @param[in] args Array of arguments.
4679 * @param[in] arg_count Count of elements in @p args.
4680 * @param[in,out] set Context and result set at the same time.
4681 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004682 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004683 */
4684static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004685xpath_round(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004686{
4687 struct lysc_node_leaf *sleaf;
4688 LY_ERR rc = LY_SUCCESS;
4689
4690 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02004691 if (args[0]->type != LYXP_SET_SCNODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004692 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko5676f4e2021-04-06 17:14:45 +02004693 } else if ((sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4694 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4695 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype),
4696 sleaf->name);
4697 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
4698 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
4699 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004700 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004701 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004702 return rc;
4703 }
4704
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004705 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004706 LY_CHECK_RET(rc);
4707
4708 /* cover only the cases where floor can't be used */
4709 if ((args[0]->val.num == -0.0f) || ((args[0]->val.num < 0) && (args[0]->val.num >= -0.5))) {
4710 set_fill_number(set, -0.0f);
4711 } else {
4712 args[0]->val.num += 0.5;
4713 rc = xpath_floor(args, 1, args[0], options);
4714 LY_CHECK_RET(rc);
4715 set_fill_number(set, args[0]->val.num);
4716 }
4717
4718 return LY_SUCCESS;
4719}
4720
4721/**
4722 * @brief Execute the XPath starts-with(string, string) function.
4723 * Returns LYXP_SET_BOOLEAN whether the second argument is
4724 * the prefix of the first or not.
4725 *
4726 * @param[in] args Array of arguments.
4727 * @param[in] arg_count Count of elements in @p args.
4728 * @param[in,out] set Context and result set at the same time.
4729 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004730 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004731 */
4732static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004733xpath_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 +02004734{
4735 struct lysc_node_leaf *sleaf;
4736 LY_ERR rc = LY_SUCCESS;
4737
4738 if (options & LYXP_SCNODE_ALL) {
4739 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4740 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4741 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 +02004742 } else if (!warn_is_string_type(sleaf->type)) {
4743 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004744 }
4745 }
4746
4747 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4748 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4749 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 +02004750 } else if (!warn_is_string_type(sleaf->type)) {
4751 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004752 }
4753 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004754 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004755 return rc;
4756 }
4757
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004758 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004759 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004760 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004761 LY_CHECK_RET(rc);
4762
4763 if (strncmp(args[0]->val.str, args[1]->val.str, strlen(args[1]->val.str))) {
4764 set_fill_boolean(set, 0);
4765 } else {
4766 set_fill_boolean(set, 1);
4767 }
4768
4769 return LY_SUCCESS;
4770}
4771
4772/**
4773 * @brief Execute the XPath string(object?) function. Returns LYXP_SET_STRING
4774 * with the string representation of either the argument or the context.
4775 *
4776 * @param[in] args Array of arguments.
4777 * @param[in] arg_count Count of elements in @p args.
4778 * @param[in,out] set Context and result set at the same time.
4779 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004780 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004781 */
4782static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004783xpath_string(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004784{
4785 LY_ERR rc;
4786
4787 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02004788 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004789 return LY_SUCCESS;
4790 }
4791
4792 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004793 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004794 LY_CHECK_RET(rc);
4795 set_fill_set(set, args[0]);
4796 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004797 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004798 LY_CHECK_RET(rc);
4799 }
4800
4801 return LY_SUCCESS;
4802}
4803
4804/**
4805 * @brief Execute the XPath string-length(string?) function. Returns LYXP_SET_NUMBER
4806 * with the length of the string in either the argument or the context.
4807 *
4808 * @param[in] args Array of arguments.
4809 * @param[in] arg_count Count of elements in @p args.
4810 * @param[in,out] set Context and result set at the same time.
4811 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004812 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004813 */
4814static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004815xpath_string_length(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004816{
4817 struct lysc_node_leaf *sleaf;
4818 LY_ERR rc = LY_SUCCESS;
4819
4820 if (options & LYXP_SCNODE_ALL) {
4821 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4822 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4823 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 +02004824 } else if (!warn_is_string_type(sleaf->type)) {
4825 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004826 }
4827 }
4828 if (!arg_count && (set->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set))) {
4829 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4830 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 +02004831 } else if (!warn_is_string_type(sleaf->type)) {
4832 LOGWRN(set->ctx, "Argument #0 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004833 }
4834 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004835 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004836 return rc;
4837 }
4838
4839 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004840 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004841 LY_CHECK_RET(rc);
4842 set_fill_number(set, strlen(args[0]->val.str));
4843 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004844 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004845 LY_CHECK_RET(rc);
4846 set_fill_number(set, strlen(set->val.str));
4847 }
4848
4849 return LY_SUCCESS;
4850}
4851
4852/**
4853 * @brief Execute the XPath substring(string, number, number?) function.
4854 * Returns LYXP_SET_STRING substring of the first argument starting
4855 * on the second argument index ending on the third argument index,
4856 * indexed from 1. For exact definition refer to
4857 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-substring.
4858 *
4859 * @param[in] args Array of arguments.
4860 * @param[in] arg_count Count of elements in @p args.
4861 * @param[in,out] set Context and result set at the same time.
4862 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004863 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004864 */
4865static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004866xpath_substring(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004867{
Radek Krejci1deb5be2020-08-26 16:43:36 +02004868 int32_t start, len;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004869 uint16_t str_start, str_len, pos;
4870 struct lysc_node_leaf *sleaf;
4871 LY_ERR rc = LY_SUCCESS;
4872
4873 if (options & LYXP_SCNODE_ALL) {
4874 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4875 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4876 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 +02004877 } else if (!warn_is_string_type(sleaf->type)) {
4878 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004879 }
4880 }
4881
4882 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4883 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4884 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 +02004885 } else if (!warn_is_numeric_type(sleaf->type)) {
4886 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004887 }
4888 }
4889
Michal Vasko69730152020-10-09 16:30:07 +02004890 if ((arg_count == 3) && (args[2]->type == LYXP_SET_SCNODE_SET) &&
4891 (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004892 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4893 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 +02004894 } else if (!warn_is_numeric_type(sleaf->type)) {
4895 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004896 }
4897 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004898 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004899 return rc;
4900 }
4901
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004902 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004903 LY_CHECK_RET(rc);
4904
4905 /* start */
4906 if (xpath_round(&args[1], 1, args[1], options)) {
4907 return -1;
4908 }
4909 if (isfinite(args[1]->val.num)) {
4910 start = args[1]->val.num - 1;
4911 } else if (isinf(args[1]->val.num) && signbit(args[1]->val.num)) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004912 start = INT32_MIN;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004913 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004914 start = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004915 }
4916
4917 /* len */
4918 if (arg_count == 3) {
4919 rc = xpath_round(&args[2], 1, args[2], options);
4920 LY_CHECK_RET(rc);
Radek Krejci1deb5be2020-08-26 16:43:36 +02004921 if (isnan(args[2]->val.num) || signbit(args[2]->val.num)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004922 len = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02004923 } else if (isfinite(args[2]->val.num)) {
4924 len = args[2]->val.num;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004925 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004926 len = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004927 }
4928 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004929 len = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004930 }
4931
4932 /* find matching character positions */
4933 str_start = 0;
4934 str_len = 0;
4935 for (pos = 0; args[0]->val.str[pos]; ++pos) {
4936 if (pos < start) {
4937 ++str_start;
4938 } else if (pos < start + len) {
4939 ++str_len;
4940 } else {
4941 break;
4942 }
4943 }
4944
4945 set_fill_string(set, args[0]->val.str + str_start, str_len);
4946 return LY_SUCCESS;
4947}
4948
4949/**
4950 * @brief Execute the XPath substring-after(string, string) function.
4951 * Returns LYXP_SET_STRING with the string succeeding the occurance
4952 * of the second argument in the first or an empty string.
4953 *
4954 * @param[in] args Array of arguments.
4955 * @param[in] arg_count Count of elements in @p args.
4956 * @param[in,out] set Context and result set at the same time.
4957 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004958 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004959 */
4960static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004961xpath_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 +02004962{
4963 char *ptr;
4964 struct lysc_node_leaf *sleaf;
4965 LY_ERR rc = LY_SUCCESS;
4966
4967 if (options & LYXP_SCNODE_ALL) {
4968 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4969 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4970 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 +02004971 } else if (!warn_is_string_type(sleaf->type)) {
4972 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004973 }
4974 }
4975
4976 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4977 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4978 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 +02004979 } else if (!warn_is_string_type(sleaf->type)) {
4980 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004981 }
4982 }
Michal Vasko1a09b212021-05-06 13:00:10 +02004983 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004984 return rc;
4985 }
4986
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004987 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004988 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004989 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004990 LY_CHECK_RET(rc);
4991
4992 ptr = strstr(args[0]->val.str, args[1]->val.str);
4993 if (ptr) {
4994 set_fill_string(set, ptr + strlen(args[1]->val.str), strlen(ptr + strlen(args[1]->val.str)));
4995 } else {
4996 set_fill_string(set, "", 0);
4997 }
4998
4999 return LY_SUCCESS;
5000}
5001
5002/**
5003 * @brief Execute the XPath substring-before(string, string) function.
5004 * Returns LYXP_SET_STRING with the string preceding the occurance
5005 * of the second argument in the first or an empty string.
5006 *
5007 * @param[in] args Array of arguments.
5008 * @param[in] arg_count Count of elements in @p args.
5009 * @param[in,out] set Context and result set at the same time.
5010 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005011 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005012 */
5013static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005014xpath_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 +02005015{
5016 char *ptr;
5017 struct lysc_node_leaf *sleaf;
5018 LY_ERR rc = LY_SUCCESS;
5019
5020 if (options & LYXP_SCNODE_ALL) {
5021 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5022 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5023 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 +02005024 } else if (!warn_is_string_type(sleaf->type)) {
5025 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005026 }
5027 }
5028
5029 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5030 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5031 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 +02005032 } else if (!warn_is_string_type(sleaf->type)) {
5033 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005034 }
5035 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005036 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005037 return rc;
5038 }
5039
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005040 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005041 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005042 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005043 LY_CHECK_RET(rc);
5044
5045 ptr = strstr(args[0]->val.str, args[1]->val.str);
5046 if (ptr) {
5047 set_fill_string(set, args[0]->val.str, ptr - args[0]->val.str);
5048 } else {
5049 set_fill_string(set, "", 0);
5050 }
5051
5052 return LY_SUCCESS;
5053}
5054
5055/**
5056 * @brief Execute the XPath sum(node-set) function. Returns LYXP_SET_NUMBER
5057 * with the sum of all the nodes in the context.
5058 *
5059 * @param[in] args Array of arguments.
5060 * @param[in] arg_count Count of elements in @p args.
5061 * @param[in,out] set Context and result set at the same time.
5062 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005063 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005064 */
5065static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005066xpath_sum(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005067{
5068 long double num;
5069 char *str;
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01005070 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005071 struct lyxp_set set_item;
5072 struct lysc_node_leaf *sleaf;
5073 LY_ERR rc = LY_SUCCESS;
5074
5075 if (options & LYXP_SCNODE_ALL) {
5076 if (args[0]->type == LYXP_SET_SCNODE_SET) {
5077 for (i = 0; i < args[0]->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005078 if (args[0]->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005079 sleaf = (struct lysc_node_leaf *)args[0]->val.scnodes[i].scnode;
5080 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5081 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__,
Michal Vasko69730152020-10-09 16:30:07 +02005082 lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005083 } else if (!warn_is_numeric_type(sleaf->type)) {
5084 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005085 }
5086 }
5087 }
5088 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005089 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005090 return rc;
5091 }
5092
5093 set_fill_number(set, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005094
5095 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005096 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "sum(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02005097 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02005098 } else if (!args[0]->used) {
5099 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005100 }
5101
Michal Vasko5c4e5892019-11-14 12:31:38 +01005102 set_init(&set_item, set);
5103
Michal Vasko03ff5a72019-09-11 13:49:33 +02005104 set_item.type = LYXP_SET_NODE_SET;
5105 set_item.val.nodes = malloc(sizeof *set_item.val.nodes);
5106 LY_CHECK_ERR_RET(!set_item.val.nodes, LOGMEM(set->ctx), LY_EMEM);
5107
5108 set_item.used = 1;
5109 set_item.size = 1;
5110
5111 for (i = 0; i < args[0]->used; ++i) {
5112 set_item.val.nodes[0] = args[0]->val.nodes[i];
5113
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005114 rc = cast_node_set_to_string(&set_item, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005115 LY_CHECK_RET(rc);
5116 num = cast_string_to_number(str);
5117 free(str);
5118 set->val.num += num;
5119 }
5120
5121 free(set_item.val.nodes);
5122
5123 return LY_SUCCESS;
5124}
5125
5126/**
5127 * @brief Execute the XPath text() function (node type). Returns LYXP_SET_NODE_SET
5128 * with the text content of the nodes in the context.
5129 *
5130 * @param[in] args Array of arguments.
5131 * @param[in] arg_count Count of elements in @p args.
5132 * @param[in,out] set Context and result set at the same time.
5133 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005134 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005135 */
5136static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005137xpath_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 +02005138{
5139 uint32_t i;
5140
5141 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02005142 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005143 return LY_SUCCESS;
5144 }
5145
Michal Vasko03ff5a72019-09-11 13:49:33 +02005146 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005147 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "text()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02005148 return LY_EVALID;
5149 }
5150
Michal Vaskod989ba02020-08-24 10:59:24 +02005151 for (i = 0; i < set->used; ) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005152 switch (set->val.nodes[i].type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01005153 case LYXP_NODE_NONE:
5154 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005155 case LYXP_NODE_ELEM:
Michal Vasko03ff5a72019-09-11 13:49:33 +02005156 if (set->val.nodes[i].node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
5157 set->val.nodes[i].type = LYXP_NODE_TEXT;
5158 ++i;
5159 break;
5160 }
Radek Krejci0f969882020-08-21 16:56:47 +02005161 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005162 case LYXP_NODE_ROOT:
5163 case LYXP_NODE_ROOT_CONFIG:
5164 case LYXP_NODE_TEXT:
Michal Vasko9f96a052020-03-10 09:41:45 +01005165 case LYXP_NODE_META:
Michal Vasko03ff5a72019-09-11 13:49:33 +02005166 set_remove_node(set, i);
5167 break;
5168 }
5169 }
5170
5171 return LY_SUCCESS;
5172}
5173
5174/**
5175 * @brief Execute the XPath translate(string, string, string) function.
5176 * Returns LYXP_SET_STRING with the first argument with the characters
5177 * from the second argument replaced by those on the corresponding
5178 * positions in the third argument.
5179 *
5180 * @param[in] args Array of arguments.
5181 * @param[in] arg_count Count of elements in @p args.
5182 * @param[in,out] set Context and result set at the same time.
5183 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005184 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005185 */
5186static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005187xpath_translate(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005188{
5189 uint16_t i, j, new_used;
5190 char *new;
Radek Krejci857189e2020-09-01 13:26:36 +02005191 ly_bool have_removed;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005192 struct lysc_node_leaf *sleaf;
5193 LY_ERR rc = LY_SUCCESS;
5194
5195 if (options & LYXP_SCNODE_ALL) {
5196 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5197 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5198 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 +02005199 } else if (!warn_is_string_type(sleaf->type)) {
5200 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005201 }
5202 }
5203
5204 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5205 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5206 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 +02005207 } else if (!warn_is_string_type(sleaf->type)) {
5208 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005209 }
5210 }
5211
5212 if ((args[2]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
5213 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5214 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 +02005215 } else if (!warn_is_string_type(sleaf->type)) {
5216 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005217 }
5218 }
Michal Vasko1a09b212021-05-06 13:00:10 +02005219 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005220 return rc;
5221 }
5222
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005223 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005224 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005225 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005226 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005227 rc = lyxp_set_cast(args[2], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005228 LY_CHECK_RET(rc);
5229
5230 new = malloc((strlen(args[0]->val.str) + 1) * sizeof(char));
5231 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5232 new_used = 0;
5233
5234 have_removed = 0;
5235 for (i = 0; args[0]->val.str[i]; ++i) {
Radek Krejci857189e2020-09-01 13:26:36 +02005236 ly_bool found = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005237
5238 for (j = 0; args[1]->val.str[j]; ++j) {
5239 if (args[0]->val.str[i] == args[1]->val.str[j]) {
5240 /* removing this char */
5241 if (j >= strlen(args[2]->val.str)) {
5242 have_removed = 1;
5243 found = 1;
5244 break;
5245 }
5246 /* replacing this char */
5247 new[new_used] = args[2]->val.str[j];
5248 ++new_used;
5249 found = 1;
5250 break;
5251 }
5252 }
5253
5254 /* copying this char */
5255 if (!found) {
5256 new[new_used] = args[0]->val.str[i];
5257 ++new_used;
5258 }
5259 }
5260
5261 if (have_removed) {
5262 new = ly_realloc(new, (new_used + 1) * sizeof(char));
5263 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5264 }
5265 new[new_used] = '\0';
5266
Michal Vaskod3678892020-05-21 10:06:58 +02005267 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005268 set->type = LYXP_SET_STRING;
5269 set->val.str = new;
5270
5271 return LY_SUCCESS;
5272}
5273
5274/**
5275 * @brief Execute the XPath true() function. Returns LYXP_SET_BOOLEAN
5276 * with true value.
5277 *
5278 * @param[in] args Array of arguments.
5279 * @param[in] arg_count Count of elements in @p args.
5280 * @param[in,out] set Context and result set at the same time.
5281 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005282 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005283 */
5284static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005285xpath_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 +02005286{
5287 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02005288 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005289 return LY_SUCCESS;
5290 }
5291
5292 set_fill_boolean(set, 1);
5293 return LY_SUCCESS;
5294}
5295
5296/*
5297 * moveto functions
5298 *
5299 * They and only they actually change the context (set).
5300 */
5301
5302/**
Michal Vasko6346ece2019-09-24 13:12:53 +02005303 * @brief Skip prefix and return corresponding model if there is a prefix. Logs directly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005304 *
Michal Vasko2104e9f2020-03-06 08:23:25 +01005305 * XPath @p set is expected to be a (sc)node set!
5306 *
Michal Vasko6346ece2019-09-24 13:12:53 +02005307 * @param[in,out] qname Qualified node name. If includes prefix, it is skipped.
5308 * @param[in,out] qname_len Length of @p qname, is updated accordingly.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005309 * @param[in] set Set with general XPath context.
5310 * @param[in] ctx_scnode Context node to inherit module for unprefixed node for ::LY_PREF_JSON.
Michal Vasko6346ece2019-09-24 13:12:53 +02005311 * @param[out] moveto_mod Expected module of a matching node.
5312 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005313 */
Michal Vasko6346ece2019-09-24 13:12:53 +02005314static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005315moveto_resolve_model(const char **qname, uint16_t *qname_len, const struct lyxp_set *set,
5316 const struct lysc_node *ctx_scnode, const struct lys_module **moveto_mod)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005317{
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02005318 const struct lys_module *mod = NULL;
Michal Vasko6346ece2019-09-24 13:12:53 +02005319 const char *ptr;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005320 size_t pref_len;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005321
Michal Vasko2104e9f2020-03-06 08:23:25 +01005322 assert((set->type == LYXP_SET_NODE_SET) || (set->type == LYXP_SET_SCNODE_SET));
5323
Michal Vasko6346ece2019-09-24 13:12:53 +02005324 if ((ptr = ly_strnchr(*qname, ':', *qname_len))) {
5325 /* specific module */
5326 pref_len = ptr - *qname;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005327 mod = ly_resolve_prefix(set->ctx, *qname, pref_len, set->format, set->prefix_data);
Michal Vasko6346ece2019-09-24 13:12:53 +02005328
Michal Vasko004d3152020-06-11 19:59:22 +02005329 /* check for errors and non-implemented modules, as they are not valid */
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005330 if (!mod || !mod->implemented) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005331 LOGVAL(set->ctx, LY_VCODE_XP_INMOD, pref_len, *qname);
Michal Vasko6346ece2019-09-24 13:12:53 +02005332 return LY_EVALID;
5333 }
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005334
Michal Vasko6346ece2019-09-24 13:12:53 +02005335 *qname += pref_len + 1;
5336 *qname_len -= pref_len + 1;
5337 } else if (((*qname)[0] == '*') && (*qname_len == 1)) {
5338 /* all modules - special case */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005339 mod = NULL;
Michal Vasko6346ece2019-09-24 13:12:53 +02005340 } else {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005341 switch (set->format) {
Radek Krejci8df109d2021-04-23 12:19:08 +02005342 case LY_VALUE_SCHEMA:
5343 case LY_VALUE_SCHEMA_RESOLVED:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005344 /* current module */
5345 mod = set->cur_mod;
5346 break;
Radek Krejci224d4b42021-04-23 13:54:59 +02005347 case LY_VALUE_CANON:
Radek Krejci8df109d2021-04-23 12:19:08 +02005348 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +02005349 case LY_VALUE_LYB:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005350 /* inherit parent (context node) module */
5351 if (ctx_scnode) {
5352 mod = ctx_scnode->module;
5353 } else {
5354 mod = NULL;
5355 }
5356 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02005357 case LY_VALUE_XML:
Michal Vasko52143d12021-04-14 15:36:39 +02005358 /* all nodes need to be prefixed */
5359 LOGVAL(set->ctx, LYVE_DATA, "Non-prefixed node \"%.*s\" in XML xpath found.", *qname_len, *qname);
5360 return LY_EVALID;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005361 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005362 }
5363
Michal Vasko6346ece2019-09-24 13:12:53 +02005364 *moveto_mod = mod;
5365 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005366}
5367
5368/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02005369 * @brief Move context @p set to the root. Handles absolute path.
5370 * Result is LYXP_SET_NODE_SET.
5371 *
5372 * @param[in,out] set Set to use.
5373 * @param[in] options Xpath options.
Michal Vaskob0099a92020-08-31 14:55:23 +02005374 * @return LY_ERR value.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005375 */
Michal Vaskob0099a92020-08-31 14:55:23 +02005376static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005377moveto_root(struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005378{
Michal Vasko03ff5a72019-09-11 13:49:33 +02005379 if (!set) {
Michal Vaskob0099a92020-08-31 14:55:23 +02005380 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005381 }
5382
5383 if (options & LYXP_SCNODE_ALL) {
Michal Vasko1a09b212021-05-06 13:00:10 +02005384 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskob0099a92020-08-31 14:55:23 +02005385 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, NULL, set->root_type, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005386 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005387 set->type = LYXP_SET_NODE_SET;
5388 set->used = 0;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005389 set_insert_node(set, NULL, 0, set->root_type, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005390 }
Michal Vaskob0099a92020-08-31 14:55:23 +02005391
5392 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005393}
5394
5395/**
5396 * @brief Check @p node as a part of NameTest processing.
5397 *
5398 * @param[in] node Node to check.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005399 * @param[in] ctx_node Context node.
5400 * @param[in] set Set to read general context from.
Michal Vaskod3678892020-05-21 10:06:58 +02005401 * @param[in] node_name Node name in the dictionary to move to, NULL for any node.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005402 * @param[in] moveto_mod Expected module of the node, NULL for no prefix.
Michal Vaskocdad7122020-11-09 21:04:44 +01005403 * @param[in] options XPath options.
Michal Vasko6346ece2019-09-24 13:12:53 +02005404 * @return LY_ERR (LY_ENOT if node does not match, LY_EINCOMPLETE on unresolved when,
5405 * LY_EINVAL if netither node nor any children match)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005406 */
5407static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005408moveto_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 +01005409 const char *node_name, const struct lys_module *moveto_mod, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005410{
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005411 if (!moveto_mod && (!node_name || strcmp(node_name, "*"))) {
5412 switch (set->format) {
Radek Krejci8df109d2021-04-23 12:19:08 +02005413 case LY_VALUE_SCHEMA:
5414 case LY_VALUE_SCHEMA_RESOLVED:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005415 /* use current module */
5416 moveto_mod = set->cur_mod;
5417 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02005418 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +02005419 case LY_VALUE_LYB:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005420 /* inherit module of the context node, if any */
5421 if (ctx_node) {
5422 moveto_mod = ctx_node->schema->module;
5423 }
5424 break;
Radek Krejci224d4b42021-04-23 13:54:59 +02005425 case LY_VALUE_CANON:
Radek Krejci8df109d2021-04-23 12:19:08 +02005426 case LY_VALUE_XML:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005427 /* not defined */
5428 LOGINT(set->ctx);
5429 return LY_EINVAL;
5430 }
5431 }
5432
Michal Vasko03ff5a72019-09-11 13:49:33 +02005433 /* module check */
5434 if (moveto_mod && (node->schema->module != moveto_mod)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005435 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005436 }
5437
Michal Vasko5c4e5892019-11-14 12:31:38 +01005438 /* context check */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005439 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005440 return LY_EINVAL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005441 } else if (set->context_op && (node->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) &&
5442 (node->schema != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005443 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005444 }
5445
5446 /* name check */
Michal Vasko61ac2f62020-05-25 12:39:51 +02005447 if (node_name && strcmp(node_name, "*") && (node->schema->name != node_name)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005448 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005449 }
5450
Michal Vaskoa1424542019-11-14 16:08:52 +01005451 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01005452 if (!(options & LYXP_IGNORE_WHEN) && lysc_has_when(node->schema) && !(node->flags & LYD_WHEN_TRUE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005453 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01005454 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005455
5456 /* match */
5457 return LY_SUCCESS;
5458}
5459
5460/**
5461 * @brief Check @p node as a part of schema NameTest processing.
5462 *
5463 * @param[in] node Schema node to check.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005464 * @param[in] ctx_scnode Context node.
5465 * @param[in] set Set to read general context from.
Michal Vaskod3678892020-05-21 10:06:58 +02005466 * @param[in] node_name Node name in the dictionary to move to, NULL for any nodes.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005467 * @param[in] moveto_mod Expected module of the node, NULL for no prefix.
Michal Vasko6346ece2019-09-24 13:12:53 +02005468 * @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 +02005469 */
5470static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005471moveto_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 +02005472 const char *node_name, const struct lys_module *moveto_mod)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005473{
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005474 if (!moveto_mod && (!node_name || strcmp(node_name, "*"))) {
5475 switch (set->format) {
Radek Krejci8df109d2021-04-23 12:19:08 +02005476 case LY_VALUE_SCHEMA:
5477 case LY_VALUE_SCHEMA_RESOLVED:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005478 /* use current module */
5479 moveto_mod = set->cur_mod;
5480 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02005481 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +02005482 case LY_VALUE_LYB:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005483 /* inherit module of the context node, if any */
5484 if (ctx_scnode) {
5485 moveto_mod = ctx_scnode->module;
5486 }
5487 break;
Radek Krejci224d4b42021-04-23 13:54:59 +02005488 case LY_VALUE_CANON:
Radek Krejci8df109d2021-04-23 12:19:08 +02005489 case LY_VALUE_XML:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005490 /* not defined */
5491 LOGINT(set->ctx);
5492 return LY_EINVAL;
5493 }
5494 }
5495
Michal Vasko03ff5a72019-09-11 13:49:33 +02005496 /* module check */
Michal Vaskod3678892020-05-21 10:06:58 +02005497 if (moveto_mod && (node->module != moveto_mod)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005498 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005499 }
5500
5501 /* context check */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005502 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (node->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005503 return LY_EINVAL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005504 } else if (set->context_op && (node->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && (node != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005505 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005506 }
5507
5508 /* name check */
Michal Vasko61ac2f62020-05-25 12:39:51 +02005509 if (node_name && strcmp(node_name, "*") && (node->name != node_name)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005510 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005511 }
5512
5513 /* match */
5514 return LY_SUCCESS;
5515}
5516
5517/**
Michal Vaskod3678892020-05-21 10:06:58 +02005518 * @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 +02005519 *
5520 * @param[in,out] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005521 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005522 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01005523 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005524 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5525 */
5526static LY_ERR
Michal Vaskocdad7122020-11-09 21:04:44 +01005527moveto_node(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005528{
Michal Vaskof03ed032020-03-04 13:31:44 +01005529 uint32_t i;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005530 const struct lyd_node *siblings, *sub, *ctx_node;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005531 LY_ERR rc;
5532
Michal Vaskod3678892020-05-21 10:06:58 +02005533 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005534 return LY_SUCCESS;
5535 }
5536
5537 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005538 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005539 return LY_EVALID;
5540 }
5541
Michal Vasko03ff5a72019-09-11 13:49:33 +02005542 for (i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02005543 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005544
5545 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 +01005546 assert(!set->val.nodes[i].node);
Michal Vaskod3678892020-05-21 10:06:58 +02005547
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005548 /* search in all the trees */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005549 ctx_node = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02005550 siblings = set->tree;
Michal Vasko5bfd4be2020-06-23 13:26:19 +02005551 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005552 /* search in children */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005553 ctx_node = set->val.nodes[i].node;
5554 siblings = lyd_child(ctx_node);
Michal Vaskod3678892020-05-21 10:06:58 +02005555 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005556
Michal Vaskod3678892020-05-21 10:06:58 +02005557 for (sub = siblings; sub; sub = sub->next) {
Michal Vaskocdad7122020-11-09 21:04:44 +01005558 rc = moveto_node_check(sub, ctx_node, set, ncname, moveto_mod, options);
Michal Vaskod3678892020-05-21 10:06:58 +02005559 if (rc == LY_SUCCESS) {
5560 if (!replaced) {
5561 set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i);
5562 replaced = 1;
5563 } else {
5564 set_insert_node(set, sub, 0, LYXP_NODE_ELEM, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005565 }
Michal Vaskod3678892020-05-21 10:06:58 +02005566 ++i;
5567 } else if (rc == LY_EINCOMPLETE) {
5568 return rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005569 }
5570 }
5571
5572 if (!replaced) {
5573 /* no match */
5574 set_remove_node(set, i);
5575 }
5576 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005577
5578 return LY_SUCCESS;
5579}
5580
5581/**
Michal Vaskod3678892020-05-21 10:06:58 +02005582 * @brief Move context @p set to a node using hashes. Result is LYXP_SET_NODE_SET (or LYXP_SET_EMPTY).
5583 * Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005584 *
5585 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005586 * @param[in] scnode Matching node schema.
Michal Vasko004d3152020-06-11 19:59:22 +02005587 * @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 +01005588 * @param[in] options XPath options.
Michal Vaskod3678892020-05-21 10:06:58 +02005589 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5590 */
5591static LY_ERR
Michal Vaskocdad7122020-11-09 21:04:44 +01005592moveto_node_hash(struct lyxp_set *set, const struct lysc_node *scnode, const struct ly_path_predicate *predicates,
5593 uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02005594{
Michal Vasko004d3152020-06-11 19:59:22 +02005595 LY_ERR ret = LY_SUCCESS;
Michal Vaskod3678892020-05-21 10:06:58 +02005596 uint32_t i;
Michal Vaskod3678892020-05-21 10:06:58 +02005597 const struct lyd_node *siblings;
Michal Vasko004d3152020-06-11 19:59:22 +02005598 struct lyd_node *sub, *inst = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02005599
Michal Vasko004d3152020-06-11 19:59:22 +02005600 assert(scnode && (!(scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)) || predicates));
Michal Vaskod3678892020-05-21 10:06:58 +02005601
5602 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02005603 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005604 }
5605
5606 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005607 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko004d3152020-06-11 19:59:22 +02005608 ret = LY_EVALID;
5609 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005610 }
5611
5612 /* context check for all the nodes since we have the schema node */
5613 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) {
5614 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02005615 goto cleanup;
Michal Vasko69730152020-10-09 16:30:07 +02005616 } else if (set->context_op && (scnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) &&
5617 (scnode != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005618 lyxp_set_free_content(set);
5619 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +02005620 }
5621
5622 /* create specific data instance if needed */
5623 if (scnode->nodetype == LYS_LIST) {
5624 LY_CHECK_GOTO(ret = lyd_create_list(scnode, predicates, &inst), cleanup);
5625 } else if (scnode->nodetype == LYS_LEAFLIST) {
5626 LY_CHECK_GOTO(ret = lyd_create_term2(scnode, &predicates[0].value, &inst), cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02005627 }
5628
5629 for (i = 0; i < set->used; ) {
Michal Vaskod3678892020-05-21 10:06:58 +02005630 siblings = NULL;
5631
5632 if ((set->val.nodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.nodes[i].type == LYXP_NODE_ROOT)) {
5633 assert(!set->val.nodes[i].node);
5634
5635 /* search in all the trees */
5636 siblings = set->tree;
5637 } else if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
5638 /* search in children */
Radek Krejcia1c1e542020-09-29 16:06:52 +02005639 siblings = lyd_child(set->val.nodes[i].node);
Michal Vaskod3678892020-05-21 10:06:58 +02005640 }
5641
5642 /* find the node using hashes */
Michal Vasko004d3152020-06-11 19:59:22 +02005643 if (inst) {
5644 lyd_find_sibling_first(siblings, inst, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02005645 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02005646 lyd_find_sibling_val(siblings, scnode, NULL, 0, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02005647 }
5648
5649 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01005650 if (!(options & LYXP_IGNORE_WHEN) && sub && lysc_has_when(sub->schema) && !(sub->flags & LYD_WHEN_TRUE)) {
Michal Vasko004d3152020-06-11 19:59:22 +02005651 ret = LY_EINCOMPLETE;
5652 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005653 }
5654
5655 if (sub) {
5656 /* pos filled later */
Michal Vaskocb1b7c02020-07-03 13:38:12 +02005657 set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i);
Michal Vaskod3678892020-05-21 10:06:58 +02005658 ++i;
Michal Vaskocb1b7c02020-07-03 13:38:12 +02005659 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005660 /* no match */
5661 set_remove_node(set, i);
5662 }
5663 }
5664
Michal Vasko004d3152020-06-11 19:59:22 +02005665cleanup:
5666 lyd_free_tree(inst);
5667 return ret;
Michal Vaskod3678892020-05-21 10:06:58 +02005668}
5669
5670/**
5671 * @brief Move context @p set to a schema node. Result is LYXP_SET_SCNODE_SET (or LYXP_SET_EMPTY).
5672 *
5673 * @param[in,out] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005674 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005675 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005676 * @param[in] options XPath options.
5677 * @return LY_ERR
5678 */
5679static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005680moveto_scnode(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005681{
Radek Krejci857189e2020-09-01 13:26:36 +02005682 ly_bool temp_ctx = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005683 uint32_t getnext_opts;
5684 uint32_t orig_used, i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005685 uint32_t mod_idx;
Michal Vasko519fd602020-05-26 12:17:39 +02005686 const struct lysc_node *iter, *start_parent;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005687 const struct lys_module *mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005688
Michal Vaskod3678892020-05-21 10:06:58 +02005689 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005690 return LY_SUCCESS;
5691 }
5692
5693 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005694 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005695 return LY_EVALID;
5696 }
5697
Michal Vaskocafad9d2019-11-07 15:20:03 +01005698 /* getnext opts */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01005699 getnext_opts = 0;
Michal Vaskocafad9d2019-11-07 15:20:03 +01005700 if (options & LYXP_SCNODE_OUTPUT) {
5701 getnext_opts |= LYS_GETNEXT_OUTPUT;
5702 }
5703
Michal Vasko03ff5a72019-09-11 13:49:33 +02005704 orig_used = set->used;
5705 for (i = 0; i < orig_used; ++i) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005706 uint32_t idx;
5707
Radek Krejcif13b87b2020-12-01 22:02:17 +01005708 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
5709 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01005710 continue;
5711 }
5712
5713 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01005714 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vaskoec4df482019-12-16 10:02:18 +01005715 } else {
Michal Vasko1a09b212021-05-06 13:00:10 +02005716 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005717 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005718
5719 start_parent = set->val.scnodes[i].scnode;
5720
5721 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 +02005722 /* 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 +02005723 * it can be in a top-level augment (the root node itself is useless in this case) */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005724 mod_idx = 0;
Michal Vasko9e9f26d2020-10-12 16:31:33 +02005725 while ((mod = (struct lys_module *)ly_ctx_get_module_iter(set->ctx, &mod_idx))) {
Michal Vasko519fd602020-05-26 12:17:39 +02005726 iter = NULL;
Michal Vasko509de4d2019-12-10 14:51:30 +01005727 /* module may not be implemented */
Michal Vasko519fd602020-05-26 12:17:39 +02005728 while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005729 if (!moveto_scnode_check(iter, NULL, set, ncname, moveto_mod)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005730 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, &idx));
5731
Michal Vasko03ff5a72019-09-11 13:49:33 +02005732 /* we need to prevent these nodes from being considered in this moveto */
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005733 if ((idx < orig_used) && (idx > i)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005734 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005735 temp_ctx = 1;
5736 }
5737 }
5738 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005739 }
5740
Michal Vasko519fd602020-05-26 12:17:39 +02005741 } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
5742 iter = NULL;
5743 while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005744 if (!moveto_scnode_check(iter, start_parent, set, ncname, moveto_mod)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005745 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, &idx));
5746
5747 if ((idx < orig_used) && (idx > i)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005748 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005749 temp_ctx = 1;
5750 }
5751 }
5752 }
5753 }
5754 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005755
5756 /* correct temporary in_ctx values */
5757 if (temp_ctx) {
5758 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005759 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_NEW_CTX) {
5760 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005761 }
5762 }
5763 }
5764
5765 return LY_SUCCESS;
5766}
5767
5768/**
Michal Vaskod3678892020-05-21 10:06:58 +02005769 * @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 +02005770 * Context position aware.
5771 *
5772 * @param[in] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005773 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005774 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01005775 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005776 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5777 */
5778static LY_ERR
Michal Vaskocdad7122020-11-09 21:04:44 +01005779moveto_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 +02005780{
5781 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005782 const struct lyd_node *next, *elem, *start;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005783 struct lyxp_set ret_set;
5784 LY_ERR rc;
5785
Michal Vaskod3678892020-05-21 10:06:58 +02005786 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005787 return LY_SUCCESS;
5788 }
5789
5790 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005791 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005792 return LY_EVALID;
5793 }
5794
Michal Vasko9f96a052020-03-10 09:41:45 +01005795 /* 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 +01005796 rc = moveto_node(set, NULL, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005797 LY_CHECK_RET(rc);
5798
Michal Vasko6346ece2019-09-24 13:12:53 +02005799 /* this loop traverses all the nodes in the set and adds/keeps only those that match qname */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005800 set_init(&ret_set, set);
5801 for (i = 0; i < set->used; ++i) {
5802
5803 /* TREE DFS */
5804 start = set->val.nodes[i].node;
5805 for (elem = next = start; elem; elem = next) {
Michal Vaskocdad7122020-11-09 21:04:44 +01005806 rc = moveto_node_check(elem, start, set, ncname, moveto_mod, options);
Michal Vasko6346ece2019-09-24 13:12:53 +02005807 if (!rc) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005808 /* add matching node into result set */
5809 set_insert_node(&ret_set, elem, 0, LYXP_NODE_ELEM, ret_set.used);
5810 if (set_dup_node_check(set, elem, LYXP_NODE_ELEM, i)) {
5811 /* the node is a duplicate, we'll process it later in the set */
5812 goto skip_children;
5813 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005814 } else if (rc == LY_EINCOMPLETE) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005815 return rc;
5816 } else if (rc == LY_EINVAL) {
5817 goto skip_children;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005818 }
5819
5820 /* TREE DFS NEXT ELEM */
5821 /* select element for the next run - children first */
Radek Krejcia1c1e542020-09-29 16:06:52 +02005822 next = lyd_child(elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005823 if (!next) {
5824skip_children:
5825 /* no children, so try siblings, but only if it's not the start,
5826 * that is considered to be the root and it's siblings are not traversed */
5827 if (elem != start) {
5828 next = elem->next;
5829 } else {
5830 break;
5831 }
5832 }
5833 while (!next) {
5834 /* no siblings, go back through the parents */
Michal Vasko9e685082021-01-29 14:49:09 +01005835 if (lyd_parent(elem) == start) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005836 /* we are done, no next element to process */
5837 break;
5838 }
5839 /* parent is already processed, go to its sibling */
Michal Vasko9e685082021-01-29 14:49:09 +01005840 elem = lyd_parent(elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005841 next = elem->next;
5842 }
5843 }
5844 }
5845
5846 /* make the temporary set the current one */
5847 ret_set.ctx_pos = set->ctx_pos;
5848 ret_set.ctx_size = set->ctx_size;
Michal Vaskod3678892020-05-21 10:06:58 +02005849 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005850 memcpy(set, &ret_set, sizeof *set);
5851
5852 return LY_SUCCESS;
5853}
5854
5855/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005856 * @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 +02005857 *
5858 * @param[in] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005859 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005860 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005861 * @param[in] options XPath options.
5862 * @return LY_ERR
5863 */
5864static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005865moveto_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 +02005866{
Radek Krejci1deb5be2020-08-26 16:43:36 +02005867 uint32_t i, orig_used;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005868 const struct lysc_node *next, *elem, *start;
Michal Vasko6346ece2019-09-24 13:12:53 +02005869 LY_ERR rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005870
Michal Vaskod3678892020-05-21 10:06:58 +02005871 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005872 return LY_SUCCESS;
5873 }
5874
5875 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005876 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005877 return LY_EVALID;
5878 }
5879
Michal Vasko03ff5a72019-09-11 13:49:33 +02005880 orig_used = set->used;
5881 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005882 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
5883 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01005884 continue;
5885 }
5886
5887 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01005888 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vaskoec4df482019-12-16 10:02:18 +01005889 } else {
Michal Vasko1a09b212021-05-06 13:00:10 +02005890 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005891 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005892
5893 /* TREE DFS */
5894 start = set->val.scnodes[i].scnode;
5895 for (elem = next = start; elem; elem = next) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005896 if ((elem == start) || (elem->nodetype & (LYS_CHOICE | LYS_CASE))) {
5897 /* schema-only nodes, skip root */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005898 goto next_iter;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005899 }
5900
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005901 rc = moveto_scnode_check(elem, start, set, ncname, moveto_mod);
Michal Vasko6346ece2019-09-24 13:12:53 +02005902 if (!rc) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005903 uint32_t idx;
5904
5905 if (lyxp_set_scnode_contains(set, elem, LYXP_NODE_ELEM, i, &idx)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005906 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005907 if ((uint32_t)idx > i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005908 /* we will process it later in the set */
5909 goto skip_children;
5910 }
5911 } else {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005912 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, elem, LYXP_NODE_ELEM, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005913 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005914 } else if (rc == LY_EINVAL) {
5915 goto skip_children;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005916 }
5917
5918next_iter:
5919 /* TREE DFS NEXT ELEM */
5920 /* select element for the next run - children first */
Michal Vasko544e58a2021-01-28 14:33:41 +01005921 next = lysc_node_child(elem);
5922 if (next && (next->nodetype == LYS_INPUT) && (options & LYXP_SCNODE_OUTPUT)) {
5923 next = next->next;
5924 } else if (next && (next->nodetype == LYS_OUTPUT) && !(options & LYXP_SCNODE_OUTPUT)) {
5925 next = next->next;
5926 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005927 if (!next) {
5928skip_children:
5929 /* no children, so try siblings, but only if it's not the start,
5930 * that is considered to be the root and it's siblings are not traversed */
5931 if (elem != start) {
5932 next = elem->next;
5933 } else {
5934 break;
5935 }
5936 }
5937 while (!next) {
5938 /* no siblings, go back through the parents */
5939 if (elem->parent == start) {
5940 /* we are done, no next element to process */
5941 break;
5942 }
5943 /* parent is already processed, go to its sibling */
5944 elem = elem->parent;
5945 next = elem->next;
5946 }
5947 }
5948 }
5949
5950 return LY_SUCCESS;
5951}
5952
5953/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005954 * @brief Move context @p set to an attribute. Result is LYXP_SET_NODE_SET.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005955 * Indirectly context position aware.
5956 *
5957 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005958 * @param[in] mod Matching metadata module, NULL for any.
5959 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005960 * @return LY_ERR
5961 */
5962static LY_ERR
Michal Vaskod3678892020-05-21 10:06:58 +02005963moveto_attr(struct lyxp_set *set, const struct lys_module *mod, const char *ncname)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005964{
Michal Vasko9f96a052020-03-10 09:41:45 +01005965 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005966
Michal Vaskod3678892020-05-21 10:06:58 +02005967 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005968 return LY_SUCCESS;
5969 }
5970
5971 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005972 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005973 return LY_EVALID;
5974 }
5975
Radek Krejci1deb5be2020-08-26 16:43:36 +02005976 for (uint32_t i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02005977 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005978
5979 /* only attributes of an elem (not dummy) can be in the result, skip all the rest;
5980 * our attributes are always qualified */
Michal Vasko5c4e5892019-11-14 12:31:38 +01005981 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01005982 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005983
5984 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02005985 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005986 continue;
5987 }
5988
Michal Vaskod3678892020-05-21 10:06:58 +02005989 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005990 /* match */
5991 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01005992 set->val.meta[i].meta = sub;
5993 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005994 /* pos does not change */
5995 replaced = 1;
5996 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01005997 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 +02005998 }
5999 ++i;
6000 }
6001 }
6002 }
6003
6004 if (!replaced) {
6005 /* no match */
6006 set_remove_node(set, i);
6007 }
6008 }
6009
6010 return LY_SUCCESS;
6011}
6012
6013/**
6014 * @brief Move context @p set1 to union with @p set2. @p set2 is emptied afterwards.
Michal Vasko61ac2f62020-05-25 12:39:51 +02006015 * Result is LYXP_SET_NODE_SET. Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006016 *
6017 * @param[in,out] set1 Set to use for the result.
6018 * @param[in] set2 Set that is copied to @p set1.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006019 * @return LY_ERR
6020 */
6021static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006022moveto_union(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006023{
6024 LY_ERR rc;
6025
Michal Vaskod3678892020-05-21 10:06:58 +02006026 if ((set1->type != LYXP_SET_NODE_SET) || (set2->type != LYXP_SET_NODE_SET)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006027 LOGVAL(set1->ctx, LY_VCODE_XP_INOP_2, "union", print_set_type(set1), print_set_type(set2));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006028 return LY_EVALID;
6029 }
6030
6031 /* set2 is empty or both set1 and set2 */
Michal Vaskod3678892020-05-21 10:06:58 +02006032 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006033 return LY_SUCCESS;
6034 }
6035
Michal Vaskod3678892020-05-21 10:06:58 +02006036 if (!set1->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006037 memcpy(set1, set2, sizeof *set1);
6038 /* dynamic memory belongs to set1 now, do not free */
Michal Vaskod3678892020-05-21 10:06:58 +02006039 memset(set2, 0, sizeof *set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006040 return LY_SUCCESS;
6041 }
6042
6043 /* we assume sets are sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006044 assert(!set_sort(set1) && !set_sort(set2));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006045
6046 /* sort, remove duplicates */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006047 rc = set_sorted_merge(set1, set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006048 LY_CHECK_RET(rc);
6049
6050 /* final set must be sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006051 assert(!set_sort(set1));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006052
6053 return LY_SUCCESS;
6054}
6055
6056/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02006057 * @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 +02006058 * Context position aware.
6059 *
6060 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02006061 * @param[in] mod Matching metadata module, NULL for any.
6062 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01006063 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006064 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6065 */
6066static int
Michal Vaskocdad7122020-11-09 21:04:44 +01006067moveto_attr_alldesc(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006068{
Michal Vasko9f96a052020-03-10 09:41:45 +01006069 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006070 struct lyxp_set *set_all_desc = NULL;
6071 LY_ERR rc;
6072
Michal Vaskod3678892020-05-21 10:06:58 +02006073 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006074 return LY_SUCCESS;
6075 }
6076
6077 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006078 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006079 return LY_EVALID;
6080 }
6081
Michal Vasko03ff5a72019-09-11 13:49:33 +02006082 /* can be optimized similarly to moveto_node_alldesc() and save considerable amount of memory,
6083 * but it likely won't be used much, so it's a waste of time */
6084 /* copy the context */
6085 set_all_desc = set_copy(set);
6086 /* get all descendant nodes (the original context nodes are removed) */
Michal Vaskocdad7122020-11-09 21:04:44 +01006087 rc = moveto_node_alldesc(set_all_desc, NULL, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006088 if (rc != LY_SUCCESS) {
6089 lyxp_set_free(set_all_desc);
6090 return rc;
6091 }
6092 /* prepend the original context nodes */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006093 rc = moveto_union(set, set_all_desc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006094 if (rc != LY_SUCCESS) {
6095 lyxp_set_free(set_all_desc);
6096 return rc;
6097 }
6098 lyxp_set_free(set_all_desc);
6099
Radek Krejci1deb5be2020-08-26 16:43:36 +02006100 for (uint32_t i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02006101 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006102
6103 /* only attributes of an elem can be in the result, skip all the rest,
6104 * we have all attributes qualified in lyd tree */
6105 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01006106 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006107 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02006108 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006109 continue;
6110 }
6111
Michal Vaskod3678892020-05-21 10:06:58 +02006112 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006113 /* match */
6114 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01006115 set->val.meta[i].meta = sub;
6116 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006117 /* pos does not change */
6118 replaced = 1;
6119 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01006120 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 +02006121 }
6122 ++i;
6123 }
6124 }
6125 }
6126
6127 if (!replaced) {
6128 /* no match */
6129 set_remove_node(set, i);
6130 }
6131 }
6132
6133 return LY_SUCCESS;
6134}
6135
6136/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02006137 * @brief Move context @p set to self and al chilren, recursively. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET.
6138 * Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006139 *
6140 * @param[in] parent Current parent.
6141 * @param[in] parent_pos Position of @p parent.
6142 * @param[in] parent_type Node type of @p parent.
6143 * @param[in,out] to_set Set to use.
6144 * @param[in] dup_check_set Set for checking duplicities.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006145 * @param[in] options XPath options.
6146 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6147 */
6148static LY_ERR
6149moveto_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 +02006150 struct lyxp_set *to_set, const struct lyxp_set *dup_check_set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006151{
Michal Vasko61ac2f62020-05-25 12:39:51 +02006152 const struct lyd_node *iter, *first;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006153 LY_ERR rc;
6154
6155 switch (parent_type) {
6156 case LYXP_NODE_ROOT:
6157 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko61ac2f62020-05-25 12:39:51 +02006158 assert(!parent);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006159
Michal Vasko61ac2f62020-05-25 12:39:51 +02006160 /* add all top-level nodes as elements */
6161 first = to_set->tree;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006162 break;
6163 case LYXP_NODE_ELEM:
Michal Vasko61ac2f62020-05-25 12:39:51 +02006164 /* add just the text node of this term element node */
6165 if (parent->schema->nodetype & (LYD_NODE_TERM | LYD_NODE_ANY)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006166 if (!set_dup_node_check(dup_check_set, parent, LYXP_NODE_TEXT, -1)) {
6167 set_insert_node(to_set, parent, parent_pos, LYXP_NODE_TEXT, to_set->used);
6168 }
Michal Vasko61ac2f62020-05-25 12:39:51 +02006169 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006170 }
Michal Vasko61ac2f62020-05-25 12:39:51 +02006171
6172 /* add all the children of this node */
Radek Krejcia1c1e542020-09-29 16:06:52 +02006173 first = lyd_child(parent);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006174 break;
6175 default:
6176 LOGINT_RET(parent->schema->module->ctx);
6177 }
6178
Michal Vasko61ac2f62020-05-25 12:39:51 +02006179 /* add all top-level nodes as elements */
6180 LY_LIST_FOR(first, iter) {
6181 /* context check */
6182 if ((parent_type == LYXP_NODE_ROOT_CONFIG) && (iter->schema->flags & LYS_CONFIG_R)) {
6183 continue;
6184 }
6185
6186 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01006187 if (!(options & LYXP_IGNORE_WHEN) && lysc_has_when(iter->schema) && !(iter->flags & LYD_WHEN_TRUE)) {
Michal Vasko61ac2f62020-05-25 12:39:51 +02006188 return LY_EINCOMPLETE;
6189 }
6190
6191 if (!set_dup_node_check(dup_check_set, iter, LYXP_NODE_ELEM, -1)) {
6192 set_insert_node(to_set, iter, 0, LYXP_NODE_ELEM, to_set->used);
6193
6194 /* also add all the children of this node, recursively */
6195 rc = moveto_self_add_children_r(iter, 0, LYXP_NODE_ELEM, to_set, dup_check_set, options);
6196 LY_CHECK_RET(rc);
6197 }
6198 }
6199
Michal Vasko03ff5a72019-09-11 13:49:33 +02006200 return LY_SUCCESS;
6201}
6202
6203/**
6204 * @brief Move context @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET
6205 * (or LYXP_SET_EMPTY). Context position aware.
6206 *
6207 * @param[in,out] set Set to use.
6208 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6209 * @param[in] options XPath options.
6210 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6211 */
6212static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006213moveto_self(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006214{
Michal Vasko03ff5a72019-09-11 13:49:33 +02006215 struct lyxp_set ret_set;
6216 LY_ERR rc;
6217
Michal Vaskod3678892020-05-21 10:06:58 +02006218 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006219 return LY_SUCCESS;
6220 }
6221
6222 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006223 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006224 return LY_EVALID;
6225 }
6226
6227 /* nothing to do */
6228 if (!all_desc) {
6229 return LY_SUCCESS;
6230 }
6231
Michal Vasko03ff5a72019-09-11 13:49:33 +02006232 /* add all the children, they get added recursively */
6233 set_init(&ret_set, set);
Radek Krejci1deb5be2020-08-26 16:43:36 +02006234 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006235 /* copy the current node to tmp */
6236 set_insert_node(&ret_set, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, ret_set.used);
6237
6238 /* do not touch attributes and text nodes */
Michal Vasko9f96a052020-03-10 09:41:45 +01006239 if ((set->val.nodes[i].type == LYXP_NODE_TEXT) || (set->val.nodes[i].type == LYXP_NODE_META)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006240 continue;
6241 }
6242
Michal Vasko03ff5a72019-09-11 13:49:33 +02006243 /* add all the children */
6244 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 +02006245 set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006246 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006247 lyxp_set_free_content(&ret_set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006248 return rc;
6249 }
6250 }
6251
6252 /* use the temporary set as the current one */
6253 ret_set.ctx_pos = set->ctx_pos;
6254 ret_set.ctx_size = set->ctx_size;
Michal Vaskod3678892020-05-21 10:06:58 +02006255 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006256 memcpy(set, &ret_set, sizeof *set);
6257
6258 return LY_SUCCESS;
6259}
6260
6261/**
6262 * @brief Move context schema @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_SCNODE_SET
6263 * (or LYXP_SET_EMPTY).
6264 *
6265 * @param[in,out] set Set to use.
6266 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6267 * @param[in] options XPath options.
6268 * @return LY_ERR
6269 */
6270static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006271moveto_scnode_self(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006272{
Radek Krejci1deb5be2020-08-26 16:43:36 +02006273 uint32_t getnext_opts;
6274 uint32_t mod_idx;
Michal Vasko519fd602020-05-26 12:17:39 +02006275 const struct lysc_node *iter, *start_parent;
6276 const struct lys_module *mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006277
Michal Vaskod3678892020-05-21 10:06:58 +02006278 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006279 return LY_SUCCESS;
6280 }
6281
6282 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006283 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006284 return LY_EVALID;
6285 }
6286
Michal Vasko519fd602020-05-26 12:17:39 +02006287 /* getnext opts */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01006288 getnext_opts = 0;
Michal Vasko519fd602020-05-26 12:17:39 +02006289 if (options & LYXP_SCNODE_OUTPUT) {
6290 getnext_opts |= LYS_GETNEXT_OUTPUT;
6291 }
6292
6293 /* add all the children, recursively as they are being added into the same set */
Radek Krejci1deb5be2020-08-26 16:43:36 +02006294 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vaskoe657f962020-12-10 12:19:48 +01006295 if (!all_desc) {
6296 /* traverse the start node */
6297 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START) {
6298 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
6299 }
6300 continue;
6301 }
6302
Radek Krejcif13b87b2020-12-01 22:02:17 +01006303 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
6304 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006305 continue;
6306 }
6307
Michal Vasko519fd602020-05-26 12:17:39 +02006308 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01006309 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vasko519fd602020-05-26 12:17:39 +02006310 } else {
Michal Vasko1a09b212021-05-06 13:00:10 +02006311 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006312 }
6313
Michal Vasko519fd602020-05-26 12:17:39 +02006314 start_parent = set->val.scnodes[i].scnode;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006315
Michal Vasko519fd602020-05-26 12:17:39 +02006316 if ((set->val.scnodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.scnodes[i].type == LYXP_NODE_ROOT)) {
6317 /* it can actually be in any module, it's all <running> */
6318 mod_idx = 0;
6319 while ((mod = (struct lys_module *)ly_ctx_get_module_iter(set->ctx, &mod_idx))) {
6320 iter = NULL;
6321 /* module may not be implemented */
6322 while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) {
6323 /* context check */
6324 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) {
6325 continue;
6326 }
6327
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006328 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, NULL));
Michal Vasko519fd602020-05-26 12:17:39 +02006329 /* throw away the insert index, we want to consider that node again, recursively */
6330 }
6331 }
6332
6333 } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
6334 iter = NULL;
6335 while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006336 /* context check */
Michal Vasko519fd602020-05-26 12:17:39 +02006337 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006338 continue;
6339 }
6340
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006341 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006342 }
6343 }
6344 }
6345
6346 return LY_SUCCESS;
6347}
6348
6349/**
6350 * @brief Move context @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_NODE_SET
6351 * (or LYXP_SET_EMPTY). Context position aware.
6352 *
6353 * @param[in] set Set to use.
6354 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6355 * @param[in] options XPath options.
6356 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6357 */
6358static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006359moveto_parent(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006360{
6361 LY_ERR rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006362 struct lyd_node *node, *new_node;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006363 enum lyxp_node_type new_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006364
Michal Vaskod3678892020-05-21 10:06:58 +02006365 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006366 return LY_SUCCESS;
6367 }
6368
6369 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006370 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006371 return LY_EVALID;
6372 }
6373
6374 if (all_desc) {
6375 /* <path>//.. == <path>//./.. */
6376 rc = moveto_self(set, 1, options);
6377 LY_CHECK_RET(rc);
6378 }
6379
Radek Krejci1deb5be2020-08-26 16:43:36 +02006380 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006381 node = set->val.nodes[i].node;
6382
6383 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9e685082021-01-29 14:49:09 +01006384 new_node = lyd_parent(node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006385 } else if (set->val.nodes[i].type == LYXP_NODE_TEXT) {
6386 new_node = node;
Michal Vasko9f96a052020-03-10 09:41:45 +01006387 } else if (set->val.nodes[i].type == LYXP_NODE_META) {
6388 new_node = set->val.meta[i].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006389 if (!new_node) {
6390 LOGINT_RET(set->ctx);
6391 }
6392 } else {
6393 /* root does not have a parent */
Michal Vasko2caefc12019-11-14 16:07:56 +01006394 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006395 continue;
6396 }
6397
Michal Vaskoa1424542019-11-14 16:08:52 +01006398 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01006399 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 +02006400 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01006401 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006402
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006403 if (!new_node) {
Radek Krejcif6a11002020-08-21 13:29:07 +02006404 /* node already there can also be the root */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006405 new_type = set->root_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006406
Michal Vasko03ff5a72019-09-11 13:49:33 +02006407 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02006408 /* 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 +02006409 new_type = LYXP_NODE_ELEM;
6410 }
6411
Michal Vasko03ff5a72019-09-11 13:49:33 +02006412 if (set_dup_node_check(set, new_node, new_type, -1)) {
Michal Vasko2caefc12019-11-14 16:07:56 +01006413 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006414 } else {
6415 set_replace_node(set, new_node, 0, new_type, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006416 }
6417 }
6418
Michal Vasko2caefc12019-11-14 16:07:56 +01006419 set_remove_nodes_none(set);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006420 assert(!set_sort(set) && !set_sorted_dup_node_clean(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006421
6422 return LY_SUCCESS;
6423}
6424
6425/**
6426 * @brief Move context schema @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_SCNODE_SET
6427 * (or LYXP_SET_EMPTY).
6428 *
6429 * @param[in] set Set to use.
6430 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6431 * @param[in] options XPath options.
6432 * @return LY_ERR
6433 */
6434static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006435moveto_scnode_parent(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006436{
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006437 uint32_t i, orig_used, idx;
Radek Krejci857189e2020-09-01 13:26:36 +02006438 ly_bool temp_ctx = 0;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006439 const struct lysc_node *node, *new_node;
6440 enum lyxp_node_type new_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006441
Michal Vaskod3678892020-05-21 10:06:58 +02006442 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006443 return LY_SUCCESS;
6444 }
6445
6446 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006447 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006448 return LY_EVALID;
6449 }
6450
6451 if (all_desc) {
6452 /* <path>//.. == <path>//./.. */
Radek Krejci1deb5be2020-08-26 16:43:36 +02006453 LY_CHECK_RET(moveto_scnode_self(set, 1, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006454 }
6455
Michal Vasko03ff5a72019-09-11 13:49:33 +02006456 orig_used = set->used;
6457 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006458 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
6459 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006460 continue;
6461 }
6462
6463 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01006464 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vaskoec4df482019-12-16 10:02:18 +01006465 } else {
Michal Vasko1a09b212021-05-06 13:00:10 +02006466 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006467 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006468
6469 node = set->val.scnodes[i].scnode;
6470
6471 if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
Michal Vaskod3678892020-05-21 10:06:58 +02006472 new_node = lysc_data_parent(node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006473 } else {
6474 /* root does not have a parent */
6475 continue;
6476 }
6477
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006478 if (!new_node) {
Radek Krejcif6a11002020-08-21 13:29:07 +02006479 /* node has no parent */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006480 new_type = set->root_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006481
Michal Vasko03ff5a72019-09-11 13:49:33 +02006482 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02006483 /* 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 +02006484 new_type = LYXP_NODE_ELEM;
6485 }
6486
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006487 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, new_node, new_type, &idx));
6488 if ((idx < orig_used) && (idx > i)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006489 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006490 temp_ctx = 1;
6491 }
6492 }
6493
6494 if (temp_ctx) {
6495 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006496 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_NEW_CTX) {
6497 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006498 }
6499 }
6500 }
6501
6502 return LY_SUCCESS;
6503}
6504
6505/**
6506 * @brief Move context @p set to the result of a comparison. Handles '=', '!=', '<=', '<', '>=', or '>'.
6507 * Result is LYXP_SET_BOOLEAN. Indirectly context position aware.
6508 *
6509 * @param[in,out] set1 Set to use for the result.
6510 * @param[in] set2 Set acting as the second operand for @p op.
6511 * @param[in] op Comparison operator to process.
6512 * @param[in] options XPath options.
6513 * @return LY_ERR
6514 */
6515static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02006516moveto_op_comp(struct lyxp_set *set1, struct lyxp_set *set2, const char *op, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006517{
6518 /*
6519 * NODE SET + NODE SET = NODE SET + STRING /(1 NODE SET) 2 STRING
6520 * NODE SET + STRING = STRING + STRING /1 STRING (2 STRING)
6521 * NODE SET + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6522 * NODE SET + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6523 * STRING + NODE SET = STRING + STRING /(1 STRING) 2 STRING
6524 * NUMBER + NODE SET = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6525 * BOOLEAN + NODE SET = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6526 *
6527 * '=' or '!='
6528 * BOOLEAN + BOOLEAN
6529 * BOOLEAN + STRING = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6530 * BOOLEAN + NUMBER = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6531 * STRING + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6532 * NUMBER + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6533 * NUMBER + NUMBER
6534 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6535 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6536 * STRING + STRING
6537 *
6538 * '<=', '<', '>=', '>'
6539 * NUMBER + NUMBER
6540 * BOOLEAN + BOOLEAN = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6541 * BOOLEAN + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6542 * BOOLEAN + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6543 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6544 * STRING + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6545 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6546 * NUMBER + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6547 * STRING + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6548 */
6549 struct lyxp_set iter1, iter2;
6550 int result;
6551 int64_t i;
6552 LY_ERR rc;
6553
Michal Vasko004d3152020-06-11 19:59:22 +02006554 memset(&iter1, 0, sizeof iter1);
6555 memset(&iter2, 0, sizeof iter2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006556
6557 /* iterative evaluation with node-sets */
6558 if ((set1->type == LYXP_SET_NODE_SET) || (set2->type == LYXP_SET_NODE_SET)) {
6559 if (set1->type == LYXP_SET_NODE_SET) {
6560 for (i = 0; i < set1->used; ++i) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006561 /* cast set1 */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006562 switch (set2->type) {
6563 case LYXP_SET_NUMBER:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006564 rc = set_comp_cast(&iter1, set1, LYXP_SET_NUMBER, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006565 break;
6566 case LYXP_SET_BOOLEAN:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006567 rc = set_comp_cast(&iter1, set1, LYXP_SET_BOOLEAN, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006568 break;
6569 default:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006570 rc = set_comp_cast(&iter1, set1, LYXP_SET_STRING, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006571 break;
6572 }
6573 LY_CHECK_RET(rc);
6574
Michal Vasko4c7763f2020-07-27 17:40:37 +02006575 /* canonize set2 */
6576 LY_CHECK_ERR_RET(rc = set_comp_canonize(&iter2, set2, &set1->val.nodes[i]), lyxp_set_free_content(&iter1), rc);
6577
6578 /* compare recursively */
6579 rc = moveto_op_comp(&iter1, &iter2, op, options);
6580 lyxp_set_free_content(&iter2);
6581 LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006582
6583 /* lazy evaluation until true */
Michal Vasko004d3152020-06-11 19:59:22 +02006584 if (iter1.val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006585 set_fill_boolean(set1, 1);
6586 return LY_SUCCESS;
6587 }
6588 }
6589 } else {
6590 for (i = 0; i < set2->used; ++i) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006591 /* set set2 */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006592 switch (set1->type) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006593 case LYXP_SET_NUMBER:
6594 rc = set_comp_cast(&iter2, set2, LYXP_SET_NUMBER, i);
6595 break;
6596 case LYXP_SET_BOOLEAN:
6597 rc = set_comp_cast(&iter2, set2, LYXP_SET_BOOLEAN, i);
6598 break;
6599 default:
6600 rc = set_comp_cast(&iter2, set2, LYXP_SET_STRING, i);
6601 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006602 }
6603 LY_CHECK_RET(rc);
6604
Michal Vasko4c7763f2020-07-27 17:40:37 +02006605 /* canonize set1 */
6606 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 +02006607
Michal Vasko4c7763f2020-07-27 17:40:37 +02006608 /* compare recursively */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006609 rc = moveto_op_comp(&iter1, &iter2, op, options);
Michal Vaskod3678892020-05-21 10:06:58 +02006610 lyxp_set_free_content(&iter2);
Michal Vasko4c7763f2020-07-27 17:40:37 +02006611 LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006612
6613 /* lazy evaluation until true */
Michal Vasko004d3152020-06-11 19:59:22 +02006614 if (iter1.val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006615 set_fill_boolean(set1, 1);
6616 return LY_SUCCESS;
6617 }
6618 }
6619 }
6620
6621 /* false for all nodes */
6622 set_fill_boolean(set1, 0);
6623 return LY_SUCCESS;
6624 }
6625
6626 /* first convert properly */
6627 if ((op[0] == '=') || (op[0] == '!')) {
6628 if ((set1->type == LYXP_SET_BOOLEAN) || (set2->type == LYXP_SET_BOOLEAN)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006629 lyxp_set_cast(set1, LYXP_SET_BOOLEAN);
6630 lyxp_set_cast(set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006631 } else if ((set1->type == LYXP_SET_NUMBER) || (set2->type == LYXP_SET_NUMBER)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006632 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006633 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006634 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006635 LY_CHECK_RET(rc);
6636 } /* else we have 2 strings */
6637 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006638 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006639 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006640 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006641 LY_CHECK_RET(rc);
6642 }
6643
6644 assert(set1->type == set2->type);
6645
6646 /* compute result */
6647 if (op[0] == '=') {
6648 if (set1->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02006649 result = (set1->val.bln == set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006650 } else if (set1->type == LYXP_SET_NUMBER) {
6651 result = (set1->val.num == set2->val.num);
6652 } else {
6653 assert(set1->type == LYXP_SET_STRING);
Michal Vaskoac6c72f2019-11-14 16:09:34 +01006654 result = !strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006655 }
6656 } else if (op[0] == '!') {
6657 if (set1->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02006658 result = (set1->val.bln != set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006659 } else if (set1->type == LYXP_SET_NUMBER) {
6660 result = (set1->val.num != set2->val.num);
6661 } else {
6662 assert(set1->type == LYXP_SET_STRING);
Michal Vaskoc2058432020-11-06 17:26:21 +01006663 result = strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006664 }
6665 } else {
6666 assert(set1->type == LYXP_SET_NUMBER);
6667 if (op[0] == '<') {
6668 if (op[1] == '=') {
6669 result = (set1->val.num <= set2->val.num);
6670 } else {
6671 result = (set1->val.num < set2->val.num);
6672 }
6673 } else {
6674 if (op[1] == '=') {
6675 result = (set1->val.num >= set2->val.num);
6676 } else {
6677 result = (set1->val.num > set2->val.num);
6678 }
6679 }
6680 }
6681
6682 /* assign result */
6683 if (result) {
6684 set_fill_boolean(set1, 1);
6685 } else {
6686 set_fill_boolean(set1, 0);
6687 }
6688
6689 return LY_SUCCESS;
6690}
6691
6692/**
6693 * @brief Move context @p set to the result of a basic operation. Handles '+', '-', unary '-', '*', 'div',
6694 * or 'mod'. Result is LYXP_SET_NUMBER. Indirectly context position aware.
6695 *
6696 * @param[in,out] set1 Set to use for the result.
6697 * @param[in] set2 Set acting as the second operand for @p op.
6698 * @param[in] op Operator to process.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006699 * @return LY_ERR
6700 */
6701static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006702moveto_op_math(struct lyxp_set *set1, struct lyxp_set *set2, const char *op)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006703{
6704 LY_ERR rc;
6705
6706 /* unary '-' */
6707 if (!set2 && (op[0] == '-')) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006708 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006709 LY_CHECK_RET(rc);
6710 set1->val.num *= -1;
6711 lyxp_set_free(set2);
6712 return LY_SUCCESS;
6713 }
6714
6715 assert(set1 && set2);
6716
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006717 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006718 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006719 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006720 LY_CHECK_RET(rc);
6721
6722 switch (op[0]) {
6723 /* '+' */
6724 case '+':
6725 set1->val.num += set2->val.num;
6726 break;
6727
6728 /* '-' */
6729 case '-':
6730 set1->val.num -= set2->val.num;
6731 break;
6732
6733 /* '*' */
6734 case '*':
6735 set1->val.num *= set2->val.num;
6736 break;
6737
6738 /* 'div' */
6739 case 'd':
6740 set1->val.num /= set2->val.num;
6741 break;
6742
6743 /* 'mod' */
6744 case 'm':
6745 set1->val.num = ((long long)set1->val.num) % ((long long)set2->val.num);
6746 break;
6747
6748 default:
6749 LOGINT_RET(set1->ctx);
6750 }
6751
6752 return LY_SUCCESS;
6753}
6754
6755/*
6756 * eval functions
6757 *
6758 * They execute a parsed XPath expression on some data subtree.
6759 */
6760
6761/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02006762 * @brief Evaluate Predicate. Logs directly on error.
6763 *
Michal Vaskod3678892020-05-21 10:06:58 +02006764 * [9] Predicate ::= '[' Expr ']'
Michal Vasko03ff5a72019-09-11 13:49:33 +02006765 *
6766 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02006767 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006768 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
6769 * @param[in] options XPath options.
6770 * @param[in] parent_pos_pred Whether parent predicate was a positional one.
6771 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6772 */
6773static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02006774eval_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 +02006775{
6776 LY_ERR rc;
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01006777 uint16_t orig_exp;
6778 uint32_t i, orig_pos, orig_size;
Michal Vasko5c4e5892019-11-14 12:31:38 +01006779 int32_t pred_in_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006780 struct lyxp_set set2;
6781 struct lyd_node *orig_parent;
6782
6783 /* '[' */
6784 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02006785 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02006786 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006787
6788 if (!set) {
6789only_parse:
Michal Vasko004d3152020-06-11 19:59:22 +02006790 rc = eval_expr_select(exp, tok_idx, 0, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006791 LY_CHECK_RET(rc);
6792 } else if (set->type == LYXP_SET_NODE_SET) {
6793 /* 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 +01006794 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006795
6796 /* empty set, nothing to evaluate */
6797 if (!set->used) {
6798 goto only_parse;
6799 }
6800
Michal Vasko004d3152020-06-11 19:59:22 +02006801 orig_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006802 orig_pos = 0;
6803 orig_size = set->used;
6804 orig_parent = NULL;
Michal Vasko39dbf352020-05-21 10:08:59 +02006805 for (i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006806 set_init(&set2, set);
6807 set_insert_node(&set2, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, 0);
6808 /* remember the node context position for position() and context size for last(),
6809 * predicates should always be evaluated with respect to the child axis (since we do
6810 * not support explicit axes) so we assign positions based on their parents */
Michal Vasko9e685082021-01-29 14:49:09 +01006811 if (parent_pos_pred && (lyd_parent(set->val.nodes[i].node) != orig_parent)) {
6812 orig_parent = lyd_parent(set->val.nodes[i].node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006813 orig_pos = 1;
6814 } else {
6815 ++orig_pos;
6816 }
6817
6818 set2.ctx_pos = orig_pos;
6819 set2.ctx_size = orig_size;
Michal Vasko004d3152020-06-11 19:59:22 +02006820 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006821
Michal Vasko004d3152020-06-11 19:59:22 +02006822 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006823 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006824 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006825 return rc;
6826 }
6827
6828 /* number is a position */
6829 if (set2.type == LYXP_SET_NUMBER) {
6830 if ((long long)set2.val.num == orig_pos) {
6831 set2.val.num = 1;
6832 } else {
6833 set2.val.num = 0;
6834 }
6835 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006836 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006837
6838 /* predicate satisfied or not? */
Michal Vasko004d3152020-06-11 19:59:22 +02006839 if (!set2.val.bln) {
Michal Vasko2caefc12019-11-14 16:07:56 +01006840 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006841 }
6842 }
Michal Vasko2caefc12019-11-14 16:07:56 +01006843 set_remove_nodes_none(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006844
6845 } else if (set->type == LYXP_SET_SCNODE_SET) {
6846 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006847 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006848 /* there is a currently-valid node */
6849 break;
6850 }
6851 }
6852 /* empty set, nothing to evaluate */
6853 if (i == set->used) {
6854 goto only_parse;
6855 }
6856
Michal Vasko004d3152020-06-11 19:59:22 +02006857 orig_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006858
Michal Vasko03ff5a72019-09-11 13:49:33 +02006859 /* set special in_ctx to all the valid snodes */
6860 pred_in_ctx = set_scnode_new_in_ctx(set);
6861
6862 /* use the valid snodes one-by-one */
6863 for (i = 0; i < set->used; ++i) {
6864 if (set->val.scnodes[i].in_ctx != pred_in_ctx) {
6865 continue;
6866 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01006867 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006868
Michal Vasko004d3152020-06-11 19:59:22 +02006869 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006870
Michal Vasko004d3152020-06-11 19:59:22 +02006871 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006872 LY_CHECK_RET(rc);
6873
6874 set->val.scnodes[i].in_ctx = pred_in_ctx;
6875 }
6876
6877 /* restore the state as it was before the predicate */
6878 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006879 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko1a09b212021-05-06 13:00:10 +02006880 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_NODE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006881 } else if (set->val.scnodes[i].in_ctx == pred_in_ctx) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006882 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006883 }
6884 }
6885
6886 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02006887 set2.type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006888 set_fill_set(&set2, set);
6889
Michal Vasko004d3152020-06-11 19:59:22 +02006890 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006891 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006892 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006893 return rc;
6894 }
6895
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006896 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02006897 if (!set2.val.bln) {
Michal Vaskod3678892020-05-21 10:06:58 +02006898 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006899 }
Michal Vaskod3678892020-05-21 10:06:58 +02006900 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006901 }
6902
6903 /* ']' */
Michal Vasko004d3152020-06-11 19:59:22 +02006904 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_BRACK2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006905 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02006906 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02006907 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006908
6909 return LY_SUCCESS;
6910}
6911
6912/**
Michal Vaskod3678892020-05-21 10:06:58 +02006913 * @brief Evaluate Literal. Logs directly on error.
6914 *
6915 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02006916 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02006917 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
6918 */
6919static void
Michal Vasko40308e72020-10-20 16:38:40 +02006920eval_literal(const struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set)
Michal Vaskod3678892020-05-21 10:06:58 +02006921{
6922 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02006923 if (exp->tok_len[*tok_idx] == 2) {
Michal Vaskod3678892020-05-21 10:06:58 +02006924 set_fill_string(set, "", 0);
6925 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02006926 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 +02006927 }
6928 }
6929 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02006930 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02006931 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02006932}
6933
6934/**
Michal Vasko004d3152020-06-11 19:59:22 +02006935 * @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 +02006936 *
Michal Vasko004d3152020-06-11 19:59:22 +02006937 * @param[in] exp Full parsed XPath expression.
6938 * @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 +02006939 * @param[in] ctx_node Found schema node as the context for the predicate.
6940 * @param[in] cur_mod Current module for the expression.
6941 * @param[in] cur_node Current (original context) node.
Michal Vasko004d3152020-06-11 19:59:22 +02006942 * @param[in] format Format of any prefixes in key names/values.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006943 * @param[in] prefix_data Format-specific prefix data (see ::ly_resolve_prefix).
Michal Vasko004d3152020-06-11 19:59:22 +02006944 * @param[out] predicates Parsed predicates.
6945 * @param[out] pred_type Type of @p predicates.
6946 * @return LY_SUCCESS on success,
6947 * @return LY_ERR on any error.
Michal Vaskod3678892020-05-21 10:06:58 +02006948 */
6949static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02006950eval_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 +02006951 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 +02006952 struct ly_path_predicate **predicates, enum ly_path_pred_type *pred_type)
Michal Vaskod3678892020-05-21 10:06:58 +02006953{
Michal Vasko004d3152020-06-11 19:59:22 +02006954 LY_ERR ret = LY_SUCCESS;
6955 uint16_t key_count, e_idx, pred_idx = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02006956 const struct lysc_node *key;
Michal Vasko004d3152020-06-11 19:59:22 +02006957 size_t pred_len;
Radek Krejci1deb5be2020-08-26 16:43:36 +02006958 uint32_t prev_lo;
Michal Vasko004d3152020-06-11 19:59:22 +02006959 struct lyxp_expr *exp2 = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02006960
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006961 assert(ctx_node->nodetype & (LYS_LIST | LYS_LEAFLIST));
Michal Vaskod3678892020-05-21 10:06:58 +02006962
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006963 if (ctx_node->nodetype == LYS_LIST) {
Michal Vasko004d3152020-06-11 19:59:22 +02006964 /* get key count */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006965 if (ctx_node->flags & LYS_KEYLESS) {
Michal Vasko004d3152020-06-11 19:59:22 +02006966 return LY_EINVAL;
6967 }
Michal Vasko544e58a2021-01-28 14:33:41 +01006968 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 +02006969 assert(key_count);
Michal Vaskod3678892020-05-21 10:06:58 +02006970
Michal Vasko004d3152020-06-11 19:59:22 +02006971 /* learn where the predicates end */
6972 e_idx = *tok_idx;
6973 while (key_count) {
6974 /* '[' */
6975 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
6976 return LY_EINVAL;
6977 }
6978 ++e_idx;
6979
Michal Vasko3354d272021-04-06 09:40:06 +02006980 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_NAMETEST)) {
6981 /* definitely not a key */
6982 return LY_EINVAL;
6983 }
6984
Michal Vasko004d3152020-06-11 19:59:22 +02006985 /* ']' */
6986 while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
6987 ++e_idx;
6988 }
6989 ++e_idx;
6990
6991 /* another presumably key predicate parsed */
6992 --key_count;
6993 }
Michal Vasko004d3152020-06-11 19:59:22 +02006994 } else {
6995 /* learn just where this single predicate ends */
6996 e_idx = *tok_idx;
6997
Michal Vaskod3678892020-05-21 10:06:58 +02006998 /* '[' */
Michal Vasko004d3152020-06-11 19:59:22 +02006999 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
7000 return LY_EINVAL;
7001 }
7002 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02007003
Michal Vasko3354d272021-04-06 09:40:06 +02007004 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_DOT)) {
7005 /* definitely not the value */
7006 return LY_EINVAL;
7007 }
7008
Michal Vaskod3678892020-05-21 10:06:58 +02007009 /* ']' */
Michal Vasko004d3152020-06-11 19:59:22 +02007010 while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
7011 ++e_idx;
7012 }
7013 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02007014 }
7015
Michal Vasko004d3152020-06-11 19:59:22 +02007016 /* get the joined length of all the keys predicates/of the single leaf-list predicate */
7017 pred_len = (exp->tok_pos[e_idx - 1] + exp->tok_len[e_idx - 1]) - exp->tok_pos[*tok_idx];
7018
7019 /* turn logging off */
7020 prev_lo = ly_log_options(0);
7021
7022 /* parse the predicate(s) */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007023 LY_CHECK_GOTO(ret = ly_path_parse_predicate(ctx_node->module->ctx, ctx_node, exp->expr + exp->tok_pos[*tok_idx],
7024 pred_len, LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &exp2), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02007025
7026 /* compile */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007027 ret = ly_path_compile_predicate(ctx_node->module->ctx, cur_node, cur_mod, ctx_node, exp2, &pred_idx, format,
7028 prefix_data, predicates, pred_type);
Michal Vasko004d3152020-06-11 19:59:22 +02007029 LY_CHECK_GOTO(ret, cleanup);
7030
7031 /* success, the predicate must include all the needed information for hash-based search */
7032 *tok_idx = e_idx;
7033
7034cleanup:
7035 ly_log_options(prev_lo);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007036 lyxp_expr_free(ctx_node->module->ctx, exp2);
Michal Vasko004d3152020-06-11 19:59:22 +02007037 return ret;
Michal Vaskod3678892020-05-21 10:06:58 +02007038}
7039
7040/**
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007041 * @brief Search for/check the next schema node that could be the only matching schema node meaning the
7042 * data node(s) could be found using a single hash-based search.
7043 *
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007044 * @param[in] ctx libyang context.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007045 * @param[in] node Next context node to check.
7046 * @param[in] name Expected node name.
7047 * @param[in] name_len Length of @p name.
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007048 * @param[in] moveto_mod Expected node module, can be NULL for JSON format with no prefix.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007049 * @param[in] root_type XPath root type.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007050 * @param[in] format Prefix format.
7051 * @param[in,out] found Previously found node, is updated.
7052 * @return LY_SUCCESS on success,
7053 * @return LY_ENOT if the whole check failed and hashes cannot be used.
7054 */
7055static LY_ERR
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007056eval_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 +02007057 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 +02007058 const struct lysc_node **found)
7059{
7060 const struct lysc_node *scnode;
7061 const struct lys_module *mod;
7062 uint32_t idx = 0;
7063
Radek Krejci8df109d2021-04-23 12:19:08 +02007064 assert((format == LY_VALUE_JSON) || moveto_mod);
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007065
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007066continue_search:
Michal Vasko7d1d0912020-10-16 08:38:30 +02007067 scnode = NULL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007068 if (!node) {
Radek Krejci8df109d2021-04-23 12:19:08 +02007069 if ((format == LY_VALUE_JSON) && !moveto_mod) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007070 /* search all modules for a single match */
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007071 while ((mod = ly_ctx_get_module_iter(ctx, &idx))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007072 scnode = lys_find_child(NULL, mod, name, name_len, 0, 0);
7073 if (scnode) {
7074 /* we have found a match */
7075 break;
7076 }
7077 }
7078
Michal Vasko7d1d0912020-10-16 08:38:30 +02007079 if (!scnode) {
7080 /* all modules searched */
7081 idx = 0;
7082 }
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007083 } else {
7084 /* search in top-level */
7085 scnode = lys_find_child(NULL, moveto_mod, name, name_len, 0, 0);
7086 }
7087 } else if (!*found || (lysc_data_parent(*found) != node->schema)) {
Radek Krejci8df109d2021-04-23 12:19:08 +02007088 if ((format == LY_VALUE_JSON) && !moveto_mod) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007089 /* we must adjust the module to inherit the one from the context node */
7090 moveto_mod = node->schema->module;
7091 }
7092
7093 /* search in children, do not repeat the same search */
7094 scnode = lys_find_child(node->schema, moveto_mod, name, name_len, 0, 0);
Michal Vasko7d1d0912020-10-16 08:38:30 +02007095 } /* else skip redundant search */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007096
7097 /* additional context check */
7098 if (scnode && (root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) {
7099 scnode = NULL;
7100 }
7101
7102 if (scnode) {
7103 if (*found) {
7104 /* we found a schema node with the same name but at different level, give up, too complicated
7105 * (more hash-based searches would be required, not supported) */
7106 return LY_ENOT;
7107 } else {
7108 /* remember the found schema node and continue to make sure it can be used */
7109 *found = scnode;
7110 }
7111 }
7112
7113 if (idx) {
7114 /* continue searching all the following models */
7115 goto continue_search;
7116 }
7117
7118 return LY_SUCCESS;
7119}
7120
7121/**
Michal Vaskod3678892020-05-21 10:06:58 +02007122 * @brief Evaluate NameTest and any following Predicates. Logs directly on error.
7123 *
7124 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
7125 * [6] NodeTest ::= NameTest | NodeType '(' ')'
7126 * [7] NameTest ::= '*' | NCName ':' '*' | QName
7127 *
7128 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007129 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02007130 * @param[in] attr_axis Whether to search attributes or standard nodes.
7131 * @param[in] all_desc Whether to search all the descendants or children only.
7132 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7133 * @param[in] options XPath options.
7134 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7135 */
7136static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007137eval_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 +02007138 struct lyxp_set *set, uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02007139{
Michal Vaskod3678892020-05-21 10:06:58 +02007140 char *path;
Michal Vasko004d3152020-06-11 19:59:22 +02007141 const char *ncname, *ncname_dict = NULL;
7142 uint16_t ncname_len;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007143 const struct lys_module *moveto_mod = NULL;
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007144 const struct lysc_node *scnode = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02007145 struct ly_path_predicate *predicates = NULL;
7146 enum ly_path_pred_type pred_type = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02007147 LY_ERR rc = LY_SUCCESS;
7148
7149 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007150 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007151 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007152
7153 if (!set) {
7154 goto moveto;
7155 }
7156
Michal Vasko004d3152020-06-11 19:59:22 +02007157 ncname = &exp->expr[exp->tok_pos[*tok_idx - 1]];
7158 ncname_len = exp->tok_len[*tok_idx - 1];
Michal Vaskod3678892020-05-21 10:06:58 +02007159
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007160 if ((ncname[0] == '*') && (ncname_len == 1)) {
7161 /* all nodes will match */
7162 goto moveto;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007163 }
7164
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007165 /* parse (and skip) module name */
7166 rc = moveto_resolve_model(&ncname, &ncname_len, set, NULL, &moveto_mod);
Michal Vaskod3678892020-05-21 10:06:58 +02007167 LY_CHECK_GOTO(rc, cleanup);
7168
Radek Krejci8df109d2021-04-23 12:19:08 +02007169 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 +02007170 /* find the matching schema node in some parent in the context */
Radek Krejci1deb5be2020-08-26 16:43:36 +02007171 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007172 if (eval_name_test_with_predicate_get_scnode(set->ctx, set->val.nodes[i].node, ncname, ncname_len,
7173 moveto_mod, set->root_type, set->format, &scnode)) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007174 /* check failed */
7175 scnode = NULL;
7176 break;
Michal Vaskod3678892020-05-21 10:06:58 +02007177 }
7178 }
7179
Michal Vasko004d3152020-06-11 19:59:22 +02007180 if (scnode && (scnode->nodetype & (LYS_LIST | LYS_LEAFLIST))) {
7181 /* try to create the predicates */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007182 if (eval_name_test_try_compile_predicates(exp, tok_idx, scnode, set->cur_mod, set->cur_node ?
7183 set->cur_node->schema : NULL, set->format, set->prefix_data, &predicates, &pred_type)) {
Michal Vasko004d3152020-06-11 19:59:22 +02007184 /* hashes cannot be used */
Michal Vaskod3678892020-05-21 10:06:58 +02007185 scnode = NULL;
7186 }
7187 }
7188 }
7189
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007190 if (!scnode) {
7191 /* we are not able to match based on a schema node and not all the modules match ("*"),
Michal Vasko004d3152020-06-11 19:59:22 +02007192 * use dictionary for efficient comparison */
Radek Krejci011e4aa2020-09-04 15:22:31 +02007193 LY_CHECK_GOTO(rc = lydict_insert(set->ctx, ncname, ncname_len, &ncname_dict), cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02007194 }
7195
7196moveto:
7197 /* move to the attribute(s), data node(s), or schema node(s) */
7198 if (attr_axis) {
7199 if (set && (options & LYXP_SCNODE_ALL)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007200 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskod3678892020-05-21 10:06:58 +02007201 } else {
7202 if (all_desc) {
Michal Vaskocdad7122020-11-09 21:04:44 +01007203 rc = moveto_attr_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007204 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007205 rc = moveto_attr(set, moveto_mod, ncname_dict);
Michal Vaskod3678892020-05-21 10:06:58 +02007206 }
7207 LY_CHECK_GOTO(rc, cleanup);
7208 }
7209 } else {
7210 if (set && (options & LYXP_SCNODE_ALL)) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02007211 int64_t i;
7212
Michal Vaskod3678892020-05-21 10:06:58 +02007213 if (all_desc) {
Michal Vasko004d3152020-06-11 19:59:22 +02007214 rc = moveto_scnode_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007215 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007216 rc = moveto_scnode(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007217 }
7218 LY_CHECK_GOTO(rc, cleanup);
7219
7220 for (i = set->used - 1; i > -1; --i) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007221 if (set->val.scnodes[i].in_ctx > LYXP_SET_SCNODE_ATOM_NODE) {
Michal Vaskod3678892020-05-21 10:06:58 +02007222 break;
7223 }
7224 }
7225 if (i == -1) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007226 path = lysc_path(set->cur_scnode, LYSC_PATH_LOG, NULL, 0);
Michal Vasko90f12dc2020-12-03 14:20:42 +01007227 LOGWRN(set->ctx, "Schema node \"%.*s\" not found (\"%.*s\") with context node \"%s\".",
7228 ncname_len, ncname, (ncname - exp->expr) + ncname_len, exp->expr, path);
Michal Vaskod3678892020-05-21 10:06:58 +02007229 free(path);
7230 }
7231 } else {
7232 if (all_desc) {
Michal Vaskocdad7122020-11-09 21:04:44 +01007233 rc = moveto_node_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007234 } else {
7235 if (scnode) {
7236 /* we can find the nodes using hashes */
Michal Vaskocdad7122020-11-09 21:04:44 +01007237 rc = moveto_node_hash(set, scnode, predicates, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007238 } else {
Michal Vaskocdad7122020-11-09 21:04:44 +01007239 rc = moveto_node(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007240 }
7241 }
7242 LY_CHECK_GOTO(rc, cleanup);
7243 }
7244 }
7245
7246 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007247 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7248 rc = eval_predicate(exp, tok_idx, set, options, 1);
Michal Vaskod3678892020-05-21 10:06:58 +02007249 LY_CHECK_RET(rc);
7250 }
7251
7252cleanup:
Michal Vaskodb51a8d2020-05-27 15:22:29 +02007253 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007254 lydict_remove(set->ctx, ncname_dict);
Michal Vaskof7e16e22020-10-21 09:24:39 +02007255 ly_path_predicates_free(set->ctx, pred_type, predicates);
Michal Vaskodb51a8d2020-05-27 15:22:29 +02007256 }
Michal Vaskod3678892020-05-21 10:06:58 +02007257 return rc;
7258}
7259
7260/**
7261 * @brief Evaluate NodeType and any following Predicates. Logs directly on error.
7262 *
7263 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
7264 * [6] NodeTest ::= NameTest | NodeType '(' ')'
7265 * [8] NodeType ::= 'text' | 'node'
7266 *
7267 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007268 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02007269 * @param[in] attr_axis Whether to search attributes or standard nodes.
7270 * @param[in] all_desc Whether to search all the descendants or children only.
7271 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7272 * @param[in] options XPath options.
7273 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7274 */
7275static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007276eval_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 +02007277 struct lyxp_set *set, uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02007278{
7279 LY_ERR rc;
7280
7281 /* TODO */
7282 (void)attr_axis;
7283 (void)all_desc;
7284
7285 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007286 assert(exp->tok_len[*tok_idx] == 4);
Michal Vaskod3678892020-05-21 10:06:58 +02007287 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007288 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskod3678892020-05-21 10:06:58 +02007289 /* just for the debug message below */
7290 set = NULL;
7291 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007292 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "node", 4)) {
Michal Vaskod3678892020-05-21 10:06:58 +02007293 rc = xpath_node(NULL, 0, set, options);
7294 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007295 assert(!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "text", 4));
Michal Vaskod3678892020-05-21 10:06:58 +02007296 rc = xpath_text(NULL, 0, set, options);
7297 }
7298 LY_CHECK_RET(rc);
7299 }
7300 }
7301 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007302 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007303 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007304
7305 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02007306 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
Michal Vaskod3678892020-05-21 10:06:58 +02007307 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007308 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007309 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007310
7311 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007312 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
Michal Vaskod3678892020-05-21 10:06:58 +02007313 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007314 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007315 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007316
7317 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007318 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7319 rc = eval_predicate(exp, tok_idx, set, options, 1);
Michal Vaskod3678892020-05-21 10:06:58 +02007320 LY_CHECK_RET(rc);
7321 }
7322
7323 return LY_SUCCESS;
7324}
7325
7326/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02007327 * @brief Evaluate RelativeLocationPath. Logs directly on error.
7328 *
7329 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
7330 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
Michal Vaskod3678892020-05-21 10:06:58 +02007331 * [6] NodeTest ::= NameTest | NodeType '(' ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007332 *
7333 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007334 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007335 * @param[in] all_desc Whether to search all the descendants or children only.
7336 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7337 * @param[in] options XPath options.
7338 * @return LY_ERR (YL_EINCOMPLETE on unresolved when)
7339 */
7340static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007341eval_relative_location_path(const struct lyxp_expr *exp, uint16_t *tok_idx, ly_bool all_desc, struct lyxp_set *set,
7342 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007343{
Radek Krejci857189e2020-09-01 13:26:36 +02007344 ly_bool attr_axis;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007345 LY_ERR rc;
7346
7347 goto step;
7348 do {
7349 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02007350 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007351 all_desc = 0;
7352 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007353 assert(exp->tok_len[*tok_idx] == 2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007354 all_desc = 1;
7355 }
7356 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007357 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007358 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007359
7360step:
Michal Vaskod3678892020-05-21 10:06:58 +02007361 /* evaluate abbreviated axis '@'? if any */
Michal Vasko004d3152020-06-11 19:59:22 +02007362 if (exp->tokens[*tok_idx] == LYXP_TOKEN_AT) {
Michal Vaskod3678892020-05-21 10:06:58 +02007363 attr_axis = 1;
7364
7365 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007366 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007367 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007368 } else {
7369 attr_axis = 0;
7370 }
7371
Michal Vasko03ff5a72019-09-11 13:49:33 +02007372 /* Step */
Michal Vasko004d3152020-06-11 19:59:22 +02007373 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007374 case LYXP_TOKEN_DOT:
7375 /* evaluate '.' */
7376 if (set && (options & LYXP_SCNODE_ALL)) {
7377 rc = moveto_scnode_self(set, all_desc, options);
7378 } else {
7379 rc = moveto_self(set, all_desc, options);
7380 }
7381 LY_CHECK_RET(rc);
7382 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007383 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007384 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007385 break;
7386
7387 case LYXP_TOKEN_DDOT:
7388 /* evaluate '..' */
7389 if (set && (options & LYXP_SCNODE_ALL)) {
7390 rc = moveto_scnode_parent(set, all_desc, options);
7391 } else {
7392 rc = moveto_parent(set, all_desc, options);
7393 }
7394 LY_CHECK_RET(rc);
7395 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007396 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007397 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007398 break;
7399
Michal Vasko03ff5a72019-09-11 13:49:33 +02007400 case LYXP_TOKEN_NAMETEST:
Michal Vaskod3678892020-05-21 10:06:58 +02007401 /* evaluate NameTest Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007402 rc = eval_name_test_with_predicate(exp, tok_idx, attr_axis, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007403 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02007404 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007405
Michal Vaskod3678892020-05-21 10:06:58 +02007406 case LYXP_TOKEN_NODETYPE:
7407 /* evaluate NodeType Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007408 rc = eval_node_type_with_predicate(exp, tok_idx, attr_axis, all_desc, set, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007409 LY_CHECK_RET(rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007410 break;
7411
7412 default:
Michal Vasko02a77382019-09-12 11:47:35 +02007413 LOGINT_RET(set ? set->ctx : NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007414 }
Michal Vasko004d3152020-06-11 19:59:22 +02007415 } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007416
7417 return LY_SUCCESS;
7418}
7419
7420/**
7421 * @brief Evaluate AbsoluteLocationPath. Logs directly on error.
7422 *
7423 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
7424 *
7425 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007426 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007427 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7428 * @param[in] options XPath options.
7429 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7430 */
7431static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007432eval_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 +02007433{
Radek Krejci857189e2020-09-01 13:26:36 +02007434 ly_bool all_desc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007435
7436 if (set) {
7437 /* no matter what tokens follow, we need to be at the root */
Michal Vaskob0099a92020-08-31 14:55:23 +02007438 LY_CHECK_RET(moveto_root(set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007439 }
7440
7441 /* '/' RelativeLocationPath? */
Michal Vasko004d3152020-06-11 19:59:22 +02007442 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007443 /* evaluate '/' - deferred */
7444 all_desc = 0;
7445 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007446 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007447 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007448
Michal Vasko004d3152020-06-11 19:59:22 +02007449 if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007450 return LY_SUCCESS;
7451 }
Michal Vasko004d3152020-06-11 19:59:22 +02007452 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007453 case LYXP_TOKEN_DOT:
7454 case LYXP_TOKEN_DDOT:
7455 case LYXP_TOKEN_AT:
7456 case LYXP_TOKEN_NAMETEST:
7457 case LYXP_TOKEN_NODETYPE:
Michal Vaskob0099a92020-08-31 14:55:23 +02007458 LY_CHECK_RET(eval_relative_location_path(exp, tok_idx, all_desc, set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007459 break;
7460 default:
7461 break;
7462 }
7463
Michal Vasko03ff5a72019-09-11 13:49:33 +02007464 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02007465 /* '//' RelativeLocationPath */
Michal Vasko03ff5a72019-09-11 13:49:33 +02007466 /* evaluate '//' - deferred so as not to waste memory by remembering all the nodes */
7467 all_desc = 1;
7468 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007469 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007470 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007471
Michal Vaskob0099a92020-08-31 14:55:23 +02007472 LY_CHECK_RET(eval_relative_location_path(exp, tok_idx, all_desc, set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007473 }
7474
7475 return LY_SUCCESS;
7476}
7477
7478/**
7479 * @brief Evaluate FunctionCall. Logs directly on error.
7480 *
Michal Vaskod3678892020-05-21 10:06:58 +02007481 * [11] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007482 *
7483 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007484 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007485 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7486 * @param[in] options XPath options.
7487 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7488 */
7489static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007490eval_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 +02007491{
7492 LY_ERR rc;
Michal Vasko69730152020-10-09 16:30:07 +02007493
Radek Krejci1deb5be2020-08-26 16:43:36 +02007494 LY_ERR (*xpath_func)(struct lyxp_set **, uint16_t, struct lyxp_set *, uint32_t) = NULL;
Michal Vasko0cbf54f2019-12-16 10:01:06 +01007495 uint16_t arg_count = 0, i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007496 struct lyxp_set **args = NULL, **args_aux;
7497
7498 if (set) {
7499 /* FunctionName */
Michal Vasko004d3152020-06-11 19:59:22 +02007500 switch (exp->tok_len[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007501 case 3:
Michal Vasko004d3152020-06-11 19:59:22 +02007502 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007503 xpath_func = &xpath_not;
Michal Vasko004d3152020-06-11 19:59:22 +02007504 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007505 xpath_func = &xpath_sum;
7506 }
7507 break;
7508 case 4:
Michal Vasko004d3152020-06-11 19:59:22 +02007509 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007510 xpath_func = &xpath_lang;
Michal Vasko004d3152020-06-11 19:59:22 +02007511 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007512 xpath_func = &xpath_last;
Michal Vasko004d3152020-06-11 19:59:22 +02007513 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007514 xpath_func = &xpath_name;
Michal Vasko004d3152020-06-11 19:59:22 +02007515 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007516 xpath_func = &xpath_true;
7517 }
7518 break;
7519 case 5:
Michal Vasko004d3152020-06-11 19:59:22 +02007520 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007521 xpath_func = &xpath_count;
Michal Vasko004d3152020-06-11 19:59:22 +02007522 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007523 xpath_func = &xpath_false;
Michal Vasko004d3152020-06-11 19:59:22 +02007524 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007525 xpath_func = &xpath_floor;
Michal Vasko004d3152020-06-11 19:59:22 +02007526 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007527 xpath_func = &xpath_round;
Michal Vasko004d3152020-06-11 19:59:22 +02007528 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007529 xpath_func = &xpath_deref;
7530 }
7531 break;
7532 case 6:
Michal Vasko004d3152020-06-11 19:59:22 +02007533 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007534 xpath_func = &xpath_concat;
Michal Vasko004d3152020-06-11 19:59:22 +02007535 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007536 xpath_func = &xpath_number;
Michal Vasko004d3152020-06-11 19:59:22 +02007537 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007538 xpath_func = &xpath_string;
7539 }
7540 break;
7541 case 7:
Michal Vasko004d3152020-06-11 19:59:22 +02007542 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007543 xpath_func = &xpath_boolean;
Michal Vasko004d3152020-06-11 19:59:22 +02007544 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007545 xpath_func = &xpath_ceiling;
Michal Vasko004d3152020-06-11 19:59:22 +02007546 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007547 xpath_func = &xpath_current;
7548 }
7549 break;
7550 case 8:
Michal Vasko004d3152020-06-11 19:59:22 +02007551 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007552 xpath_func = &xpath_contains;
Michal Vasko004d3152020-06-11 19:59:22 +02007553 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007554 xpath_func = &xpath_position;
Michal Vasko004d3152020-06-11 19:59:22 +02007555 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007556 xpath_func = &xpath_re_match;
7557 }
7558 break;
7559 case 9:
Michal Vasko004d3152020-06-11 19:59:22 +02007560 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007561 xpath_func = &xpath_substring;
Michal Vasko004d3152020-06-11 19:59:22 +02007562 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007563 xpath_func = &xpath_translate;
7564 }
7565 break;
7566 case 10:
Michal Vasko004d3152020-06-11 19:59:22 +02007567 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007568 xpath_func = &xpath_local_name;
Michal Vasko004d3152020-06-11 19:59:22 +02007569 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007570 xpath_func = &xpath_enum_value;
Michal Vasko004d3152020-06-11 19:59:22 +02007571 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007572 xpath_func = &xpath_bit_is_set;
7573 }
7574 break;
7575 case 11:
Michal Vasko004d3152020-06-11 19:59:22 +02007576 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007577 xpath_func = &xpath_starts_with;
7578 }
7579 break;
7580 case 12:
Michal Vasko004d3152020-06-11 19:59:22 +02007581 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007582 xpath_func = &xpath_derived_from;
7583 }
7584 break;
7585 case 13:
Michal Vasko004d3152020-06-11 19:59:22 +02007586 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007587 xpath_func = &xpath_namespace_uri;
Michal Vasko004d3152020-06-11 19:59:22 +02007588 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007589 xpath_func = &xpath_string_length;
7590 }
7591 break;
7592 case 15:
Michal Vasko004d3152020-06-11 19:59:22 +02007593 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007594 xpath_func = &xpath_normalize_space;
Michal Vasko004d3152020-06-11 19:59:22 +02007595 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007596 xpath_func = &xpath_substring_after;
7597 }
7598 break;
7599 case 16:
Michal Vasko004d3152020-06-11 19:59:22 +02007600 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007601 xpath_func = &xpath_substring_before;
7602 }
7603 break;
7604 case 20:
Michal Vasko004d3152020-06-11 19:59:22 +02007605 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007606 xpath_func = &xpath_derived_from_or_self;
7607 }
7608 break;
7609 }
7610
7611 if (!xpath_func) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01007612 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 +02007613 return LY_EVALID;
7614 }
7615 }
7616
7617 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007618 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007619 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007620
7621 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02007622 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007623 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007624 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007625 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007626
7627 /* ( Expr ( ',' Expr )* )? */
Michal Vasko004d3152020-06-11 19:59:22 +02007628 if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007629 if (set) {
7630 args = malloc(sizeof *args);
7631 LY_CHECK_ERR_GOTO(!args, LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7632 arg_count = 1;
7633 args[0] = set_copy(set);
7634 if (!args[0]) {
7635 rc = LY_EMEM;
7636 goto cleanup;
7637 }
7638
Michal Vasko004d3152020-06-11 19:59:22 +02007639 rc = eval_expr_select(exp, tok_idx, 0, args[0], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007640 LY_CHECK_GOTO(rc, cleanup);
7641 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007642 rc = eval_expr_select(exp, tok_idx, 0, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007643 LY_CHECK_GOTO(rc, cleanup);
7644 }
7645 }
Michal Vasko004d3152020-06-11 19:59:22 +02007646 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007647 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007648 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007649 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007650
7651 if (set) {
7652 ++arg_count;
7653 args_aux = realloc(args, arg_count * sizeof *args);
7654 LY_CHECK_ERR_GOTO(!args_aux, arg_count--; LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7655 args = args_aux;
7656 args[arg_count - 1] = set_copy(set);
7657 if (!args[arg_count - 1]) {
7658 rc = LY_EMEM;
7659 goto cleanup;
7660 }
7661
Michal Vasko004d3152020-06-11 19:59:22 +02007662 rc = eval_expr_select(exp, tok_idx, 0, args[arg_count - 1], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007663 LY_CHECK_GOTO(rc, cleanup);
7664 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007665 rc = eval_expr_select(exp, tok_idx, 0, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007666 LY_CHECK_GOTO(rc, cleanup);
7667 }
7668 }
7669
7670 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007671 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007672 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007673 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007674 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007675
7676 if (set) {
7677 /* evaluate function */
7678 rc = xpath_func(args, arg_count, set, options);
7679
7680 if (options & LYXP_SCNODE_ALL) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007681 /* merge all nodes from arg evaluations */
7682 for (i = 0; i < arg_count; ++i) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007683 set_scnode_clear_ctx(args[i], LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007684 lyxp_set_scnode_merge(set, args[i]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007685 }
7686 }
7687 } else {
7688 rc = LY_SUCCESS;
7689 }
7690
7691cleanup:
7692 for (i = 0; i < arg_count; ++i) {
7693 lyxp_set_free(args[i]);
7694 }
7695 free(args);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007696 return rc;
7697}
7698
7699/**
7700 * @brief Evaluate Number. Logs directly on error.
7701 *
7702 * @param[in] ctx Context for errors.
7703 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007704 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007705 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7706 * @return LY_ERR
7707 */
7708static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007709eval_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 +02007710{
7711 long double num;
7712 char *endptr;
7713
7714 if (set) {
7715 errno = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02007716 num = strtold(&exp->expr[exp->tok_pos[*tok_idx]], &endptr);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007717 if (errno) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01007718 LOGVAL(ctx, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]);
7719 LOGVAL(ctx, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double (%s).",
Michal Vasko69730152020-10-09 16:30:07 +02007720 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]], strerror(errno));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007721 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02007722 } else if (endptr - &exp->expr[exp->tok_pos[*tok_idx]] != exp->tok_len[*tok_idx]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01007723 LOGVAL(ctx, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]);
7724 LOGVAL(ctx, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double.",
Michal Vasko69730152020-10-09 16:30:07 +02007725 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007726 return LY_EVALID;
7727 }
7728
7729 set_fill_number(set, num);
7730 }
7731
7732 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007733 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007734 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007735 return LY_SUCCESS;
7736}
7737
7738/**
7739 * @brief Evaluate PathExpr. Logs directly on error.
7740 *
Michal Vaskod3678892020-05-21 10:06:58 +02007741 * [12] PathExpr ::= LocationPath | PrimaryExpr Predicate*
Michal Vasko03ff5a72019-09-11 13:49:33 +02007742 * | PrimaryExpr Predicate* '/' RelativeLocationPath
7743 * | PrimaryExpr Predicate* '//' RelativeLocationPath
7744 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
Michal Vaskod3678892020-05-21 10:06:58 +02007745 * [10] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall
Michal Vasko03ff5a72019-09-11 13:49:33 +02007746 *
7747 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007748 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007749 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7750 * @param[in] options XPath options.
7751 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7752 */
7753static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007754eval_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 +02007755{
Radek Krejci857189e2020-09-01 13:26:36 +02007756 ly_bool all_desc, parent_pos_pred;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007757 LY_ERR rc;
7758
Michal Vasko004d3152020-06-11 19:59:22 +02007759 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007760 case LYXP_TOKEN_PAR1:
7761 /* '(' Expr ')' */
7762
7763 /* '(' */
7764 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007765 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007766 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007767
7768 /* Expr */
Michal Vasko004d3152020-06-11 19:59:22 +02007769 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007770 LY_CHECK_RET(rc);
7771
7772 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007773 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007774 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007775 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007776 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007777
7778 parent_pos_pred = 0;
7779 goto predicate;
7780
7781 case LYXP_TOKEN_DOT:
7782 case LYXP_TOKEN_DDOT:
7783 case LYXP_TOKEN_AT:
7784 case LYXP_TOKEN_NAMETEST:
7785 case LYXP_TOKEN_NODETYPE:
7786 /* RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007787 rc = eval_relative_location_path(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007788 LY_CHECK_RET(rc);
7789 break;
7790
7791 case LYXP_TOKEN_FUNCNAME:
7792 /* FunctionCall */
7793 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007794 rc = eval_function_call(exp, tok_idx, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007795 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007796 rc = eval_function_call(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007797 }
7798 LY_CHECK_RET(rc);
7799
7800 parent_pos_pred = 1;
7801 goto predicate;
7802
Michal Vasko3e48bf32020-06-01 08:39:07 +02007803 case LYXP_TOKEN_OPER_PATH:
7804 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02007805 /* AbsoluteLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007806 rc = eval_absolute_location_path(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007807 LY_CHECK_RET(rc);
7808 break;
7809
7810 case LYXP_TOKEN_LITERAL:
7811 /* Literal */
7812 if (!set || (options & LYXP_SCNODE_ALL)) {
7813 if (set) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007814 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007815 }
Michal Vasko004d3152020-06-11 19:59:22 +02007816 eval_literal(exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007817 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007818 eval_literal(exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007819 }
7820
7821 parent_pos_pred = 1;
7822 goto predicate;
7823
7824 case LYXP_TOKEN_NUMBER:
7825 /* Number */
7826 if (!set || (options & LYXP_SCNODE_ALL)) {
7827 if (set) {
Michal Vasko1a09b212021-05-06 13:00:10 +02007828 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007829 }
Michal Vasko004d3152020-06-11 19:59:22 +02007830 rc = eval_number(NULL, exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007831 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007832 rc = eval_number(set->ctx, exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007833 }
7834 LY_CHECK_RET(rc);
7835
7836 parent_pos_pred = 1;
7837 goto predicate;
7838
7839 default:
Radek Krejci2efc45b2020-12-22 16:25:44 +01007840 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 +02007841 return LY_EVALID;
7842 }
7843
7844 return LY_SUCCESS;
7845
7846predicate:
7847 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007848 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7849 rc = eval_predicate(exp, tok_idx, set, options, parent_pos_pred);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007850 LY_CHECK_RET(rc);
7851 }
7852
7853 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007854 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007855
7856 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02007857 if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007858 all_desc = 0;
7859 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007860 all_desc = 1;
7861 }
7862
7863 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007864 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007865 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007866
Michal Vasko004d3152020-06-11 19:59:22 +02007867 rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007868 LY_CHECK_RET(rc);
7869 }
7870
7871 return LY_SUCCESS;
7872}
7873
7874/**
7875 * @brief Evaluate UnionExpr. Logs directly on error.
7876 *
Michal Vaskod3678892020-05-21 10:06:58 +02007877 * [20] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007878 *
7879 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007880 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007881 * @param[in] repeat How many times this expression is repeated.
7882 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7883 * @param[in] options XPath options.
7884 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7885 */
7886static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007887eval_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 +02007888{
7889 LY_ERR rc = LY_SUCCESS;
7890 struct lyxp_set orig_set, set2;
7891 uint16_t i;
7892
7893 assert(repeat);
7894
7895 set_init(&orig_set, set);
7896 set_init(&set2, set);
7897
7898 set_fill_set(&orig_set, set);
7899
Michal Vasko004d3152020-06-11 19:59:22 +02007900 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007901 LY_CHECK_GOTO(rc, cleanup);
7902
7903 /* ('|' PathExpr)* */
7904 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007905 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_UNI);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007906 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007907 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007908 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007909
7910 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007911 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007912 LY_CHECK_GOTO(rc, cleanup);
7913 continue;
7914 }
7915
7916 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02007917 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007918 LY_CHECK_GOTO(rc, cleanup);
7919
7920 /* eval */
7921 if (options & LYXP_SCNODE_ALL) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01007922 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007923 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007924 rc = moveto_union(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007925 LY_CHECK_GOTO(rc, cleanup);
7926 }
7927 }
7928
7929cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007930 lyxp_set_free_content(&orig_set);
7931 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007932 return rc;
7933}
7934
7935/**
7936 * @brief Evaluate UnaryExpr. Logs directly on error.
7937 *
Michal Vaskod3678892020-05-21 10:06:58 +02007938 * [19] UnaryExpr ::= UnionExpr | '-' UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007939 *
7940 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007941 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007942 * @param[in] repeat How many times this expression is repeated.
7943 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7944 * @param[in] options XPath options.
7945 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7946 */
7947static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007948eval_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 +02007949{
7950 LY_ERR rc;
7951 uint16_t this_op, i;
7952
7953 assert(repeat);
7954
7955 /* ('-')+ */
Michal Vasko004d3152020-06-11 19:59:22 +02007956 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007957 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007958 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 +02007959
7960 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007961 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007962 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007963 }
7964
Michal Vasko004d3152020-06-11 19:59:22 +02007965 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNARY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007966 LY_CHECK_RET(rc);
7967
7968 if (set && (repeat % 2)) {
7969 if (options & LYXP_SCNODE_ALL) {
7970 warn_operands(set->ctx, set, NULL, 1, exp->expr, exp->tok_pos[this_op]);
7971 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007972 rc = moveto_op_math(set, NULL, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007973 LY_CHECK_RET(rc);
7974 }
7975 }
7976
7977 return LY_SUCCESS;
7978}
7979
7980/**
7981 * @brief Evaluate MultiplicativeExpr. Logs directly on error.
7982 *
Michal Vaskod3678892020-05-21 10:06:58 +02007983 * [18] MultiplicativeExpr ::= UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007984 * | MultiplicativeExpr '*' UnaryExpr
7985 * | MultiplicativeExpr 'div' UnaryExpr
7986 * | MultiplicativeExpr 'mod' UnaryExpr
7987 *
7988 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007989 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007990 * @param[in] repeat How many times this expression is repeated.
7991 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7992 * @param[in] options XPath options.
7993 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7994 */
7995static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007996eval_multiplicative_expr(const struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set,
7997 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007998{
7999 LY_ERR rc;
8000 uint16_t this_op;
8001 struct lyxp_set orig_set, set2;
8002 uint16_t i;
8003
8004 assert(repeat);
8005
8006 set_init(&orig_set, set);
8007 set_init(&set2, set);
8008
8009 set_fill_set(&orig_set, set);
8010
Michal Vasko004d3152020-06-11 19:59:22 +02008011 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008012 LY_CHECK_GOTO(rc, cleanup);
8013
8014 /* ('*' / 'div' / 'mod' UnaryExpr)* */
8015 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008016 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008017
Michal Vasko004d3152020-06-11 19:59:22 +02008018 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008019 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02008020 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008021 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008022
8023 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02008024 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008025 LY_CHECK_GOTO(rc, cleanup);
8026 continue;
8027 }
8028
8029 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008030 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008031 LY_CHECK_GOTO(rc, cleanup);
8032
8033 /* eval */
8034 if (options & LYXP_SCNODE_ALL) {
8035 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008036 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02008037 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008038 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008039 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008040 LY_CHECK_GOTO(rc, cleanup);
8041 }
8042 }
8043
8044cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008045 lyxp_set_free_content(&orig_set);
8046 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008047 return rc;
8048}
8049
8050/**
8051 * @brief Evaluate AdditiveExpr. Logs directly on error.
8052 *
Michal Vaskod3678892020-05-21 10:06:58 +02008053 * [17] AdditiveExpr ::= MultiplicativeExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008054 * | AdditiveExpr '+' MultiplicativeExpr
8055 * | AdditiveExpr '-' MultiplicativeExpr
8056 *
8057 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008058 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008059 * @param[in] repeat How many times this expression is repeated.
8060 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8061 * @param[in] options XPath options.
8062 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8063 */
8064static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008065eval_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 +02008066{
8067 LY_ERR rc;
8068 uint16_t this_op;
8069 struct lyxp_set orig_set, set2;
8070 uint16_t i;
8071
8072 assert(repeat);
8073
8074 set_init(&orig_set, set);
8075 set_init(&set2, set);
8076
8077 set_fill_set(&orig_set, set);
8078
Michal Vasko004d3152020-06-11 19:59:22 +02008079 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008080 LY_CHECK_GOTO(rc, cleanup);
8081
8082 /* ('+' / '-' MultiplicativeExpr)* */
8083 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008084 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008085
Michal Vasko004d3152020-06-11 19:59:22 +02008086 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008087 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02008088 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008089 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008090
8091 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02008092 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008093 LY_CHECK_GOTO(rc, cleanup);
8094 continue;
8095 }
8096
8097 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008098 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008099 LY_CHECK_GOTO(rc, cleanup);
8100
8101 /* eval */
8102 if (options & LYXP_SCNODE_ALL) {
8103 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008104 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02008105 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008106 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008107 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008108 LY_CHECK_GOTO(rc, cleanup);
8109 }
8110 }
8111
8112cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008113 lyxp_set_free_content(&orig_set);
8114 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008115 return rc;
8116}
8117
8118/**
8119 * @brief Evaluate RelationalExpr. Logs directly on error.
8120 *
Michal Vaskod3678892020-05-21 10:06:58 +02008121 * [16] RelationalExpr ::= AdditiveExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008122 * | RelationalExpr '<' AdditiveExpr
8123 * | RelationalExpr '>' AdditiveExpr
8124 * | RelationalExpr '<=' AdditiveExpr
8125 * | RelationalExpr '>=' AdditiveExpr
8126 *
8127 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008128 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008129 * @param[in] repeat How many times this expression is repeated.
8130 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8131 * @param[in] options XPath options.
8132 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8133 */
8134static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008135eval_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 +02008136{
8137 LY_ERR rc;
8138 uint16_t this_op;
8139 struct lyxp_set orig_set, set2;
8140 uint16_t i;
8141
8142 assert(repeat);
8143
8144 set_init(&orig_set, set);
8145 set_init(&set2, set);
8146
8147 set_fill_set(&orig_set, set);
8148
Michal Vasko004d3152020-06-11 19:59:22 +02008149 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008150 LY_CHECK_GOTO(rc, cleanup);
8151
8152 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
8153 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008154 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008155
Michal Vasko004d3152020-06-11 19:59:22 +02008156 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_COMP);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008157 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02008158 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008159 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008160
8161 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02008162 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008163 LY_CHECK_GOTO(rc, cleanup);
8164 continue;
8165 }
8166
8167 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008168 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008169 LY_CHECK_GOTO(rc, cleanup);
8170
8171 /* eval */
8172 if (options & LYXP_SCNODE_ALL) {
8173 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008174 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02008175 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008176 } else {
8177 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options);
8178 LY_CHECK_GOTO(rc, cleanup);
8179 }
8180 }
8181
8182cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008183 lyxp_set_free_content(&orig_set);
8184 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008185 return rc;
8186}
8187
8188/**
8189 * @brief Evaluate EqualityExpr. Logs directly on error.
8190 *
Michal Vaskod3678892020-05-21 10:06:58 +02008191 * [15] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008192 * | EqualityExpr '!=' RelationalExpr
8193 *
8194 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008195 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008196 * @param[in] repeat How many times this expression is repeated.
8197 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8198 * @param[in] options XPath options.
8199 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8200 */
8201static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008202eval_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 +02008203{
8204 LY_ERR rc;
8205 uint16_t this_op;
8206 struct lyxp_set orig_set, set2;
8207 uint16_t i;
8208
8209 assert(repeat);
8210
8211 set_init(&orig_set, set);
8212 set_init(&set2, set);
8213
8214 set_fill_set(&orig_set, set);
8215
Michal Vasko004d3152020-06-11 19:59:22 +02008216 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008217 LY_CHECK_GOTO(rc, cleanup);
8218
8219 /* ('=' / '!=' RelationalExpr)* */
8220 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008221 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008222
Michal Vasko004d3152020-06-11 19:59:22 +02008223 assert((exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_EQUAL) || (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_NEQUAL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008224 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02008225 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008226 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008227
8228 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02008229 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008230 LY_CHECK_GOTO(rc, cleanup);
8231 continue;
8232 }
8233
8234 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008235 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008236 LY_CHECK_GOTO(rc, cleanup);
8237
8238 /* eval */
8239 if (options & LYXP_SCNODE_ALL) {
8240 warn_operands(set->ctx, set, &set2, 0, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vasko004d3152020-06-11 19:59:22 +02008241 warn_equality_value(exp, set, *tok_idx - 1, this_op - 1, *tok_idx - 1);
8242 warn_equality_value(exp, &set2, this_op - 1, this_op - 1, *tok_idx - 1);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008243 lyxp_set_scnode_merge(set, &set2);
Michal Vasko1a09b212021-05-06 13:00:10 +02008244 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_VAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008245 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008246 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options);
8247 LY_CHECK_GOTO(rc, cleanup);
8248 }
8249 }
8250
8251cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008252 lyxp_set_free_content(&orig_set);
8253 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008254 return rc;
8255}
8256
8257/**
8258 * @brief Evaluate AndExpr. Logs directly on error.
8259 *
Michal Vaskod3678892020-05-21 10:06:58 +02008260 * [14] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008261 *
8262 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008263 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008264 * @param[in] repeat How many times this expression is repeated.
8265 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8266 * @param[in] options XPath options.
8267 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8268 */
8269static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008270eval_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 +02008271{
8272 LY_ERR rc;
8273 struct lyxp_set orig_set, set2;
8274 uint16_t i;
8275
8276 assert(repeat);
8277
8278 set_init(&orig_set, set);
8279 set_init(&set2, set);
8280
8281 set_fill_set(&orig_set, set);
8282
Michal Vasko004d3152020-06-11 19:59:22 +02008283 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008284 LY_CHECK_GOTO(rc, cleanup);
8285
8286 /* cast to boolean, we know that will be the final result */
8287 if (set && (options & LYXP_SCNODE_ALL)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008288 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008289 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008290 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008291 }
8292
8293 /* ('and' EqualityExpr)* */
8294 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008295 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
8296 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (!set || !set->val.bln ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008297 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008298 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008299
8300 /* lazy evaluation */
Michal Vasko004d3152020-06-11 19:59:22 +02008301 if (!set || ((set->type == LYXP_SET_BOOLEAN) && !set->val.bln)) {
8302 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008303 LY_CHECK_GOTO(rc, cleanup);
8304 continue;
8305 }
8306
8307 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008308 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008309 LY_CHECK_GOTO(rc, cleanup);
8310
8311 /* eval - just get boolean value actually */
8312 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008313 set_scnode_clear_ctx(&set2, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008314 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008315 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008316 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008317 set_fill_set(set, &set2);
8318 }
8319 }
8320
8321cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008322 lyxp_set_free_content(&orig_set);
8323 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008324 return rc;
8325}
8326
8327/**
8328 * @brief Evaluate OrExpr. Logs directly on error.
8329 *
Michal Vaskod3678892020-05-21 10:06:58 +02008330 * [13] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008331 *
8332 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008333 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008334 * @param[in] repeat How many times this expression is repeated.
8335 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8336 * @param[in] options XPath options.
8337 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8338 */
8339static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008340eval_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 +02008341{
8342 LY_ERR rc;
8343 struct lyxp_set orig_set, set2;
8344 uint16_t i;
8345
8346 assert(repeat);
8347
8348 set_init(&orig_set, set);
8349 set_init(&set2, set);
8350
8351 set_fill_set(&orig_set, set);
8352
Michal Vasko004d3152020-06-11 19:59:22 +02008353 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008354 LY_CHECK_GOTO(rc, cleanup);
8355
8356 /* cast to boolean, we know that will be the final result */
8357 if (set && (options & LYXP_SCNODE_ALL)) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008358 set_scnode_clear_ctx(set, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008359 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008360 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008361 }
8362
8363 /* ('or' AndExpr)* */
8364 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008365 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
8366 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (!set || set->val.bln ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008367 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008368 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008369
8370 /* lazy evaluation */
Michal Vasko004d3152020-06-11 19:59:22 +02008371 if (!set || ((set->type == LYXP_SET_BOOLEAN) && set->val.bln)) {
8372 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008373 LY_CHECK_GOTO(rc, cleanup);
8374 continue;
8375 }
8376
8377 set_fill_set(&set2, &orig_set);
8378 /* expr_type cound have been LYXP_EXPR_NONE in all these later calls (except for the first one),
8379 * but it does not matter */
Michal Vasko004d3152020-06-11 19:59:22 +02008380 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008381 LY_CHECK_GOTO(rc, cleanup);
8382
8383 /* eval - just get boolean value actually */
8384 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vasko1a09b212021-05-06 13:00:10 +02008385 set_scnode_clear_ctx(&set2, LYXP_SET_SCNODE_ATOM_NODE);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008386 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008387 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008388 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008389 set_fill_set(set, &set2);
8390 }
8391 }
8392
8393cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008394 lyxp_set_free_content(&orig_set);
8395 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008396 return rc;
8397}
8398
8399/**
Michal Vasko004d3152020-06-11 19:59:22 +02008400 * @brief Decide what expression is at the pointer @p tok_idx and evaluate it accordingly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008401 *
8402 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008403 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008404 * @param[in] etype Expression type to evaluate.
8405 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8406 * @param[in] options XPath options.
8407 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8408 */
8409static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008410eval_expr_select(const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_expr_type etype, struct lyxp_set *set,
8411 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008412{
8413 uint16_t i, count;
8414 enum lyxp_expr_type next_etype;
8415 LY_ERR rc;
8416
8417 /* process operator repeats */
Michal Vasko004d3152020-06-11 19:59:22 +02008418 if (!exp->repeat[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008419 next_etype = LYXP_EXPR_NONE;
8420 } else {
8421 /* find etype repeat */
Radek Krejci1e008d22020-08-17 11:37:37 +02008422 for (i = 0; exp->repeat[*tok_idx][i] > etype; ++i) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02008423
8424 /* select one-priority lower because etype expression called us */
8425 if (i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008426 next_etype = exp->repeat[*tok_idx][i - 1];
Michal Vasko03ff5a72019-09-11 13:49:33 +02008427 /* count repeats for that expression */
Radek Krejci1e008d22020-08-17 11:37:37 +02008428 for (count = 0; i && exp->repeat[*tok_idx][i - 1] == next_etype; ++count, --i) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02008429 } else {
8430 next_etype = LYXP_EXPR_NONE;
8431 }
8432 }
8433
8434 /* decide what expression are we parsing based on the repeat */
8435 switch (next_etype) {
8436 case LYXP_EXPR_OR:
Michal Vasko004d3152020-06-11 19:59:22 +02008437 rc = eval_or_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008438 break;
8439 case LYXP_EXPR_AND:
Michal Vasko004d3152020-06-11 19:59:22 +02008440 rc = eval_and_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008441 break;
8442 case LYXP_EXPR_EQUALITY:
Michal Vasko004d3152020-06-11 19:59:22 +02008443 rc = eval_equality_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008444 break;
8445 case LYXP_EXPR_RELATIONAL:
Michal Vasko004d3152020-06-11 19:59:22 +02008446 rc = eval_relational_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008447 break;
8448 case LYXP_EXPR_ADDITIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02008449 rc = eval_additive_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008450 break;
8451 case LYXP_EXPR_MULTIPLICATIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02008452 rc = eval_multiplicative_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008453 break;
8454 case LYXP_EXPR_UNARY:
Michal Vasko004d3152020-06-11 19:59:22 +02008455 rc = eval_unary_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008456 break;
8457 case LYXP_EXPR_UNION:
Michal Vasko004d3152020-06-11 19:59:22 +02008458 rc = eval_union_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008459 break;
8460 case LYXP_EXPR_NONE:
Michal Vasko004d3152020-06-11 19:59:22 +02008461 rc = eval_path_expr(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008462 break;
8463 default:
8464 LOGINT_RET(set->ctx);
8465 }
8466
8467 return rc;
8468}
8469
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008470/**
8471 * @brief Get root type.
8472 *
8473 * @param[in] ctx_node Context node.
8474 * @param[in] ctx_scnode Schema context node.
8475 * @param[in] options XPath options.
8476 * @return Root type.
8477 */
8478static enum lyxp_node_type
Radek Krejci1deb5be2020-08-26 16:43:36 +02008479lyxp_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 +01008480{
Michal Vasko6b26e742020-07-17 15:02:10 +02008481 const struct lysc_node *op;
8482
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008483 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008484 /* schema */
Radek Krejci1e008d22020-08-17 11:37:37 +02008485 for (op = ctx_scnode; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02008486
8487 if (op || (options & LYXP_SCNODE)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008488 /* general root that can access everything */
8489 return LYXP_NODE_ROOT;
8490 } else if (!ctx_scnode || (ctx_scnode->flags & LYS_CONFIG_W)) {
8491 /* root context node can access only config data (because we said so, it is unspecified) */
8492 return LYXP_NODE_ROOT_CONFIG;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008493 }
Michal Vasko6b26e742020-07-17 15:02:10 +02008494 return LYXP_NODE_ROOT;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008495 }
8496
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008497 /* data */
Michal Vasko6b26e742020-07-17 15:02:10 +02008498 op = ctx_node ? ctx_node->schema : NULL;
Michal Vaskod989ba02020-08-24 10:59:24 +02008499 for ( ; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02008500
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008501 if (op || !(options & LYXP_SCHEMA)) {
8502 /* general root that can access everything */
8503 return LYXP_NODE_ROOT;
Christian Hoppsb6ecaea2021-02-06 09:45:38 -05008504 } else if (!ctx_node || !ctx_node->schema || (ctx_node->schema->flags & LYS_CONFIG_W)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008505 /* root context node can access only config data (because we said so, it is unspecified) */
8506 return LYXP_NODE_ROOT_CONFIG;
8507 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008508 return LYXP_NODE_ROOT;
8509}
8510
Michal Vasko03ff5a72019-09-11 13:49:33 +02008511LY_ERR
Michal Vasko400e9672021-01-11 13:39:17 +01008512lyxp_eval(const struct ly_ctx *ctx, const struct lyxp_expr *exp, const struct lys_module *cur_mod,
Radek Krejci8df109d2021-04-23 12:19:08 +02008513 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 +01008514 struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008515{
Michal Vasko004d3152020-06-11 19:59:22 +02008516 uint16_t tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008517 LY_ERR rc;
8518
Michal Vasko400e9672021-01-11 13:39:17 +01008519 LY_CHECK_ARG_RET(ctx, ctx, exp, set, LY_EINVAL);
Radek Krejci8df109d2021-04-23 12:19:08 +02008520 if (!cur_mod && ((format == LY_VALUE_SCHEMA) || (format == LY_VALUE_SCHEMA_RESOLVED))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008521 LOGARG(NULL, "Current module must be set if schema format is used.");
8522 return LY_EINVAL;
Michal Vasko004d3152020-06-11 19:59:22 +02008523 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008524
Michal Vaskod3bb12f2020-12-04 14:33:09 +01008525 if (tree) {
8526 /* adjust the pointer to be the first top-level sibling */
8527 while (tree->parent) {
8528 tree = lyd_parent(tree);
8529 }
8530 tree = lyd_first_sibling(tree);
8531 }
8532
Michal Vasko03ff5a72019-09-11 13:49:33 +02008533 /* prepare set for evaluation */
Michal Vasko03ff5a72019-09-11 13:49:33 +02008534 memset(set, 0, sizeof *set);
Michal Vaskod3678892020-05-21 10:06:58 +02008535 set->type = LYXP_SET_NODE_SET;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008536 set->root_type = lyxp_get_root_type(ctx_node, NULL, options);
8537 set_insert_node(set, (struct lyd_node *)ctx_node, 0, ctx_node ? LYXP_NODE_ELEM : set->root_type, 0);
8538
Michal Vasko400e9672021-01-11 13:39:17 +01008539 set->ctx = (struct ly_ctx *)ctx;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008540 set->cur_node = ctx_node;
8541 for (set->context_op = ctx_node ? ctx_node->schema : NULL;
Radek Krejci0f969882020-08-21 16:56:47 +02008542 set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF));
8543 set->context_op = set->context_op->parent) {}
Michal Vaskof03ed032020-03-04 13:31:44 +01008544 set->tree = tree;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008545 set->cur_mod = cur_mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008546 set->format = format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008547 set->prefix_data = prefix_data;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008548
Radek Krejciddace2c2021-01-08 11:30:56 +01008549 LOG_LOCSET(NULL, set->cur_node, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01008550
Michal Vasko03ff5a72019-09-11 13:49:33 +02008551 /* evaluate */
Michal Vasko004d3152020-06-11 19:59:22 +02008552 rc = eval_expr_select(exp, &tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008553 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02008554 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008555 }
8556
Radek Krejciddace2c2021-01-08 11:30:56 +01008557 LOG_LOCBACK(0, 1, 0, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008558 return rc;
8559}
8560
8561#if 0
8562
8563/* full xml printing of set elements, not used currently */
8564
8565void
8566lyxp_set_print_xml(FILE *f, struct lyxp_set *set)
8567{
8568 uint32_t i;
8569 char *str_num;
8570 struct lyout out;
8571
8572 memset(&out, 0, sizeof out);
8573
8574 out.type = LYOUT_STREAM;
8575 out.method.f = f;
8576
8577 switch (set->type) {
8578 case LYXP_SET_EMPTY:
Michal Vasko5233e962020-08-14 14:26:20 +02008579 ly_print_(&out, "Empty XPath set\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008580 break;
8581 case LYXP_SET_BOOLEAN:
Michal Vasko5233e962020-08-14 14:26:20 +02008582 ly_print_(&out, "Boolean XPath set:\n");
8583 ly_print_(&out, "%s\n\n", set->value.bool ? "true" : "false");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008584 break;
8585 case LYXP_SET_STRING:
Michal Vasko5233e962020-08-14 14:26:20 +02008586 ly_print_(&out, "String XPath set:\n");
8587 ly_print_(&out, "\"%s\"\n\n", set->value.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008588 break;
8589 case LYXP_SET_NUMBER:
Michal Vasko5233e962020-08-14 14:26:20 +02008590 ly_print_(&out, "Number XPath set:\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008591
8592 if (isnan(set->value.num)) {
8593 str_num = strdup("NaN");
8594 } else if ((set->value.num == 0) || (set->value.num == -0.0f)) {
8595 str_num = strdup("0");
8596 } else if (isinf(set->value.num) && !signbit(set->value.num)) {
8597 str_num = strdup("Infinity");
8598 } else if (isinf(set->value.num) && signbit(set->value.num)) {
8599 str_num = strdup("-Infinity");
8600 } else if ((long long)set->value.num == set->value.num) {
8601 if (asprintf(&str_num, "%lld", (long long)set->value.num) == -1) {
8602 str_num = NULL;
8603 }
8604 } else {
8605 if (asprintf(&str_num, "%03.1Lf", set->value.num) == -1) {
8606 str_num = NULL;
8607 }
8608 }
8609 if (!str_num) {
8610 LOGMEM;
8611 return;
8612 }
Michal Vasko5233e962020-08-14 14:26:20 +02008613 ly_print_(&out, "%s\n\n", str_num);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008614 free(str_num);
8615 break;
8616 case LYXP_SET_NODE_SET:
Michal Vasko5233e962020-08-14 14:26:20 +02008617 ly_print_(&out, "Node XPath set:\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008618
8619 for (i = 0; i < set->used; ++i) {
Michal Vasko5233e962020-08-14 14:26:20 +02008620 ly_print_(&out, "%d. ", i + 1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008621 switch (set->node_type[i]) {
8622 case LYXP_NODE_ROOT_ALL:
Michal Vasko5233e962020-08-14 14:26:20 +02008623 ly_print_(&out, "ROOT all\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008624 break;
8625 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko5233e962020-08-14 14:26:20 +02008626 ly_print_(&out, "ROOT config\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008627 break;
8628 case LYXP_NODE_ROOT_STATE:
Michal Vasko5233e962020-08-14 14:26:20 +02008629 ly_print_(&out, "ROOT state\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008630 break;
8631 case LYXP_NODE_ROOT_NOTIF:
Michal Vasko5233e962020-08-14 14:26:20 +02008632 ly_print_(&out, "ROOT notification \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008633 break;
8634 case LYXP_NODE_ROOT_RPC:
Michal Vasko5233e962020-08-14 14:26:20 +02008635 ly_print_(&out, "ROOT rpc \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008636 break;
8637 case LYXP_NODE_ROOT_OUTPUT:
Michal Vasko5233e962020-08-14 14:26:20 +02008638 ly_print_(&out, "ROOT output \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008639 break;
8640 case LYXP_NODE_ELEM:
Michal Vasko5233e962020-08-14 14:26:20 +02008641 ly_print_(&out, "ELEM \"%s\"\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008642 xml_print_node(&out, 1, set->value.nodes[i], 1, LYP_FORMAT);
Michal Vasko5233e962020-08-14 14:26:20 +02008643 ly_print_(&out, "\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008644 break;
8645 case LYXP_NODE_TEXT:
Michal Vasko5233e962020-08-14 14:26:20 +02008646 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 +02008647 break;
8648 case LYXP_NODE_ATTR:
Michal Vasko5233e962020-08-14 14:26:20 +02008649 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 +02008650 break;
8651 }
8652 }
8653 break;
8654 }
8655}
8656
8657#endif
8658
8659LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008660lyxp_set_cast(struct lyxp_set *set, enum lyxp_set_type target)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008661{
8662 long double num;
8663 char *str;
8664 LY_ERR rc;
8665
8666 if (!set || (set->type == target)) {
8667 return LY_SUCCESS;
8668 }
8669
8670 /* it's not possible to convert anything into a node set */
Michal Vaskod3678892020-05-21 10:06:58 +02008671 assert(target != LYXP_SET_NODE_SET);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008672
8673 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vaskod3678892020-05-21 10:06:58 +02008674 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008675 return LY_EINVAL;
8676 }
8677
8678 /* to STRING */
Michal Vaskod3678892020-05-21 10:06:58 +02008679 if ((target == LYXP_SET_STRING) || ((target == LYXP_SET_NUMBER) && (set->type == LYXP_SET_NODE_SET))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008680 switch (set->type) {
8681 case LYXP_SET_NUMBER:
8682 if (isnan(set->val.num)) {
8683 set->val.str = strdup("NaN");
8684 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8685 } else if ((set->val.num == 0) || (set->val.num == -0.0f)) {
8686 set->val.str = strdup("0");
8687 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8688 } else if (isinf(set->val.num) && !signbit(set->val.num)) {
8689 set->val.str = strdup("Infinity");
8690 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8691 } else if (isinf(set->val.num) && signbit(set->val.num)) {
8692 set->val.str = strdup("-Infinity");
8693 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8694 } else if ((long long)set->val.num == set->val.num) {
8695 if (asprintf(&str, "%lld", (long long)set->val.num) == -1) {
8696 LOGMEM_RET(set->ctx);
8697 }
8698 set->val.str = str;
8699 } else {
8700 if (asprintf(&str, "%03.1Lf", set->val.num) == -1) {
8701 LOGMEM_RET(set->ctx);
8702 }
8703 set->val.str = str;
8704 }
8705 break;
8706 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02008707 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008708 set->val.str = strdup("true");
8709 } else {
8710 set->val.str = strdup("false");
8711 }
8712 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM);
8713 break;
8714 case LYXP_SET_NODE_SET:
Michal Vasko03ff5a72019-09-11 13:49:33 +02008715 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008716 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008717
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008718 rc = cast_node_set_to_string(set, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008719 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02008720 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008721 set->val.str = str;
8722 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008723 default:
8724 LOGINT_RET(set->ctx);
8725 }
8726 set->type = LYXP_SET_STRING;
8727 }
8728
8729 /* to NUMBER */
8730 if (target == LYXP_SET_NUMBER) {
8731 switch (set->type) {
8732 case LYXP_SET_STRING:
8733 num = cast_string_to_number(set->val.str);
Michal Vaskod3678892020-05-21 10:06:58 +02008734 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008735 set->val.num = num;
8736 break;
8737 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02008738 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008739 set->val.num = 1;
8740 } else {
8741 set->val.num = 0;
8742 }
8743 break;
8744 default:
8745 LOGINT_RET(set->ctx);
8746 }
8747 set->type = LYXP_SET_NUMBER;
8748 }
8749
8750 /* to BOOLEAN */
8751 if (target == LYXP_SET_BOOLEAN) {
8752 switch (set->type) {
8753 case LYXP_SET_NUMBER:
8754 if ((set->val.num == 0) || (set->val.num == -0.0f) || isnan(set->val.num)) {
Michal Vasko004d3152020-06-11 19:59:22 +02008755 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008756 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02008757 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008758 }
8759 break;
8760 case LYXP_SET_STRING:
8761 if (set->val.str[0]) {
Michal Vaskod3678892020-05-21 10:06:58 +02008762 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008763 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008764 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02008765 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008766 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008767 }
8768 break;
8769 case LYXP_SET_NODE_SET:
Michal Vaskod3678892020-05-21 10:06:58 +02008770 if (set->used) {
8771 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008772 set->val.bln = 1;
Michal Vaskod3678892020-05-21 10:06:58 +02008773 } else {
8774 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008775 set->val.bln = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02008776 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008777 break;
8778 default:
8779 LOGINT_RET(set->ctx);
8780 }
8781 set->type = LYXP_SET_BOOLEAN;
8782 }
8783
Michal Vasko03ff5a72019-09-11 13:49:33 +02008784 return LY_SUCCESS;
8785}
8786
8787LY_ERR
Michal Vasko400e9672021-01-11 13:39:17 +01008788lyxp_atomize(const struct ly_ctx *ctx, const struct lyxp_expr *exp, const struct lys_module *cur_mod,
Radek Krejci8df109d2021-04-23 12:19:08 +02008789 LY_VALUE_FORMAT format, void *prefix_data, const struct lysc_node *ctx_scnode, struct lyxp_set *set,
Michal Vasko400e9672021-01-11 13:39:17 +01008790 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008791{
Radek Krejci2efc45b2020-12-22 16:25:44 +01008792 LY_ERR ret;
Michal Vasko004d3152020-06-11 19:59:22 +02008793 uint16_t tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008794
Michal Vasko400e9672021-01-11 13:39:17 +01008795 LY_CHECK_ARG_RET(ctx, ctx, exp, set, LY_EINVAL);
Radek Krejci8df109d2021-04-23 12:19:08 +02008796 if (!cur_mod && ((format == LY_VALUE_SCHEMA) || (format == LY_VALUE_SCHEMA_RESOLVED))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008797 LOGARG(NULL, "Current module must be set if schema format is used.");
8798 return LY_EINVAL;
Michal Vasko004d3152020-06-11 19:59:22 +02008799 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008800
8801 /* prepare set for evaluation */
Michal Vasko03ff5a72019-09-11 13:49:33 +02008802 memset(set, 0, sizeof *set);
8803 set->type = LYXP_SET_SCNODE_SET;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008804 set->root_type = lyxp_get_root_type(NULL, ctx_scnode, options);
8805 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 +01008806 set->val.scnodes[0].in_ctx = LYXP_SET_SCNODE_START;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008807
Michal Vasko400e9672021-01-11 13:39:17 +01008808 set->ctx = (struct ly_ctx *)ctx;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008809 set->cur_scnode = ctx_scnode;
Michal Vasko6b26e742020-07-17 15:02:10 +02008810 for (set->context_op = ctx_scnode;
Radek Krejci0f969882020-08-21 16:56:47 +02008811 set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF));
8812 set->context_op = set->context_op->parent) {}
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008813 set->cur_mod = cur_mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008814 set->format = format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008815 set->prefix_data = prefix_data;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008816
Radek Krejciddace2c2021-01-08 11:30:56 +01008817 LOG_LOCSET(set->cur_scnode, NULL, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01008818
Michal Vasko03ff5a72019-09-11 13:49:33 +02008819 /* evaluate */
Radek Krejci2efc45b2020-12-22 16:25:44 +01008820 ret = eval_expr_select(exp, &tok_idx, 0, set, options);
8821
Radek Krejciddace2c2021-01-08 11:30:56 +01008822 LOG_LOCBACK(1, 0, 0, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +01008823 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008824}
Michal Vaskod43d71a2020-08-07 14:54:58 +02008825
8826API const char *
8827lyxp_get_expr(const struct lyxp_expr *path)
8828{
8829 if (!path) {
8830 return NULL;
8831 }
8832
8833 return path->expr;
8834}