blob: 4bf4ca86ec61512615dea807f3e213048933f1c0 [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 */
Michal Vasko03ff5a72019-09-11 13:49:33 +020014#define _GNU_SOURCE
15
16/* needed by libmath functions isfinite(), isinf(), isnan(), signbit(), ... */
17#define _ISOC99_SOURCE
Radek Krejcib1646a92018-11-02 16:08:26 +010018
Radek Krejci535ea9f2020-05-29 16:01:05 +020019#include "xpath.h"
Radek Krejcib1646a92018-11-02 16:08:26 +010020
Radek Krejci535ea9f2020-05-29 16:01:05 +020021#include <assert.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010022#include <ctype.h>
Radek Krejci535ea9f2020-05-29 16:01:05 +020023#include <errno.h>
Radek Krejci535ea9f2020-05-29 16:01:05 +020024#include <math.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020025#include <stdint.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010026#include <stdio.h>
27#include <stdlib.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010028#include <string.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010029
Radek Krejci535ea9f2020-05-29 16:01:05 +020030#include "common.h"
Michal Vasko5aa44c02020-06-29 11:47:02 +020031#include "compat.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020032#include "context.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020033#include "dict.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020034#include "hash_table.h"
Radek Krejci47fab892020-11-05 17:02:41 +010035#include "out.h"
Radek Krejci7931b192020-06-25 17:05:03 +020036#include "parser_data.h"
Michal Vasko004d3152020-06-11 19:59:22 +020037#include "path.h"
Michal Vasko03ff5a72019-09-11 13:49:33 +020038#include "plugins_types.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020039#include "printer_data.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020040#include "schema_compile_node.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020041#include "tree.h"
42#include "tree_data_internal.h"
43#include "tree_schema_internal.h"
44#include "xml.h"
Michal Vasko03ff5a72019-09-11 13:49:33 +020045
Michal Vasko004d3152020-06-11 19:59:22 +020046static LY_ERR reparse_or_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx);
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 Vasko03ff5a72019-09-11 13:49:33 +020049
50/**
51 * @brief Print the type of an XPath \p set.
52 *
53 * @param[in] set Set to use.
54 * @return Set type string.
55 */
56static const char *
57print_set_type(struct lyxp_set *set)
58{
59 switch (set->type) {
Michal Vasko03ff5a72019-09-11 13:49:33 +020060 case LYXP_SET_NODE_SET:
61 return "node set";
62 case LYXP_SET_SCNODE_SET:
63 return "schema node set";
64 case LYXP_SET_BOOLEAN:
65 return "boolean";
66 case LYXP_SET_NUMBER:
67 return "number";
68 case LYXP_SET_STRING:
69 return "string";
70 }
71
72 return NULL;
73}
74
Michal Vasko24cddf82020-06-01 08:17:01 +020075const char *
76lyxp_print_token(enum lyxp_token tok)
Michal Vasko03ff5a72019-09-11 13:49:33 +020077{
78 switch (tok) {
79 case LYXP_TOKEN_PAR1:
80 return "(";
81 case LYXP_TOKEN_PAR2:
82 return ")";
83 case LYXP_TOKEN_BRACK1:
84 return "[";
85 case LYXP_TOKEN_BRACK2:
86 return "]";
87 case LYXP_TOKEN_DOT:
88 return ".";
89 case LYXP_TOKEN_DDOT:
90 return "..";
91 case LYXP_TOKEN_AT:
92 return "@";
93 case LYXP_TOKEN_COMMA:
94 return ",";
95 case LYXP_TOKEN_NAMETEST:
96 return "NameTest";
97 case LYXP_TOKEN_NODETYPE:
98 return "NodeType";
99 case LYXP_TOKEN_FUNCNAME:
100 return "FunctionName";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200101 case LYXP_TOKEN_OPER_LOG:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200102 return "Operator(Logic)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200103 case LYXP_TOKEN_OPER_EQUAL:
104 return "Operator(Equal)";
105 case LYXP_TOKEN_OPER_NEQUAL:
106 return "Operator(Non-equal)";
107 case LYXP_TOKEN_OPER_COMP:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200108 return "Operator(Comparison)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200109 case LYXP_TOKEN_OPER_MATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200110 return "Operator(Math)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200111 case LYXP_TOKEN_OPER_UNI:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200112 return "Operator(Union)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200113 case LYXP_TOKEN_OPER_PATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200114 return "Operator(Path)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200115 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko14676352020-05-29 11:35:55 +0200116 return "Operator(Recursive Path)";
Michal Vasko03ff5a72019-09-11 13:49:33 +0200117 case LYXP_TOKEN_LITERAL:
118 return "Literal";
119 case LYXP_TOKEN_NUMBER:
120 return "Number";
121 default:
122 LOGINT(NULL);
123 return "";
124 }
125}
126
127/**
128 * @brief Print the whole expression \p exp to debug output.
129 *
130 * @param[in] exp Expression to use.
131 */
132static void
Michal Vasko40308e72020-10-20 16:38:40 +0200133print_expr_struct_debug(const struct lyxp_expr *exp)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200134{
135 uint16_t i, j;
136 char tmp[128];
137
Radek Krejci52b6d512020-10-12 12:33:17 +0200138 if (!exp || (ly_ll < LY_LLDBG)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200139 return;
140 }
141
142 LOGDBG(LY_LDGXPATH, "expression \"%s\":", exp->expr);
143 for (i = 0; i < exp->used; ++i) {
Michal Vasko24cddf82020-06-01 08:17:01 +0200144 sprintf(tmp, "\ttoken %s, in expression \"%.*s\"", lyxp_print_token(exp->tokens[i]), exp->tok_len[i],
Michal Vasko69730152020-10-09 16:30:07 +0200145 &exp->expr[exp->tok_pos[i]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200146 if (exp->repeat[i]) {
147 sprintf(tmp + strlen(tmp), " (repeat %d", exp->repeat[i][0]);
148 for (j = 1; exp->repeat[i][j]; ++j) {
149 sprintf(tmp + strlen(tmp), ", %d", exp->repeat[i][j]);
150 }
151 strcat(tmp, ")");
152 }
153 LOGDBG(LY_LDGXPATH, tmp);
154 }
155}
156
157#ifndef NDEBUG
158
159/**
160 * @brief Print XPath set content to debug output.
161 *
162 * @param[in] set Set to print.
163 */
164static void
165print_set_debug(struct lyxp_set *set)
166{
167 uint32_t i;
168 char *str;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200169 struct lyxp_set_node *item;
170 struct lyxp_set_scnode *sitem;
171
Radek Krejci52b6d512020-10-12 12:33:17 +0200172 if (ly_ll < LY_LLDBG) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200173 return;
174 }
175
176 switch (set->type) {
177 case LYXP_SET_NODE_SET:
178 LOGDBG(LY_LDGXPATH, "set NODE SET:");
179 for (i = 0; i < set->used; ++i) {
180 item = &set->val.nodes[i];
181
182 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +0100183 case LYXP_NODE_NONE:
184 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): NONE", i + 1, item->pos);
185 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200186 case LYXP_NODE_ROOT:
187 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT", i + 1, item->pos);
188 break;
189 case LYXP_NODE_ROOT_CONFIG:
190 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT CONFIG", i + 1, item->pos);
191 break;
192 case LYXP_NODE_ELEM:
Michal Vasko69730152020-10-09 16:30:07 +0200193 if ((item->node->schema->nodetype == LYS_LIST) &&
194 (((struct lyd_node_inner *)item->node)->child->schema->nodetype == LYS_LEAF)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200195 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (1st child val: %s)", i + 1, item->pos,
Michal Vasko69730152020-10-09 16:30:07 +0200196 item->node->schema->name, LYD_CANON_VALUE(lyd_child(item->node)));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200197 } else if (((struct lyd_node_inner *)item->node)->schema->nodetype == LYS_LEAFLIST) {
198 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (val: %s)", i + 1, item->pos,
Michal Vasko69730152020-10-09 16:30:07 +0200199 item->node->schema->name, LYD_CANON_VALUE(item->node));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200200 } else {
201 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s", i + 1, item->pos, item->node->schema->name);
202 }
203 break;
204 case LYXP_NODE_TEXT:
205 if (item->node->schema->nodetype & LYS_ANYDATA) {
206 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): TEXT <%s>", i + 1, item->pos,
Michal Vasko69730152020-10-09 16:30:07 +0200207 item->node->schema->nodetype == LYS_ANYXML ? "anyxml" : "anydata");
Michal Vasko03ff5a72019-09-11 13:49:33 +0200208 } else {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200209 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): TEXT %s", i + 1, item->pos, LYD_CANON_VALUE(item->node));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200210 }
211 break;
Michal Vasko9f96a052020-03-10 09:41:45 +0100212 case LYXP_NODE_META:
213 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 +0200214 set->val.meta[i].meta->value);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200215 break;
216 }
217 }
218 break;
219
220 case LYXP_SET_SCNODE_SET:
221 LOGDBG(LY_LDGXPATH, "set SCNODE SET:");
222 for (i = 0; i < set->used; ++i) {
223 sitem = &set->val.scnodes[i];
224
225 switch (sitem->type) {
226 case LYXP_NODE_ROOT:
227 LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT", i + 1, sitem->in_ctx);
228 break;
229 case LYXP_NODE_ROOT_CONFIG:
230 LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT CONFIG", i + 1, sitem->in_ctx);
231 break;
232 case LYXP_NODE_ELEM:
233 LOGDBG(LY_LDGXPATH, "\t%d (%u): ELEM %s", i + 1, sitem->in_ctx, sitem->scnode->name);
234 break;
235 default:
236 LOGINT(NULL);
237 break;
238 }
239 }
240 break;
241
Michal Vasko03ff5a72019-09-11 13:49:33 +0200242 case LYXP_SET_BOOLEAN:
243 LOGDBG(LY_LDGXPATH, "set BOOLEAN");
Michal Vasko004d3152020-06-11 19:59:22 +0200244 LOGDBG(LY_LDGXPATH, "\t%s", (set->val.bln ? "true" : "false"));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200245 break;
246
247 case LYXP_SET_STRING:
248 LOGDBG(LY_LDGXPATH, "set STRING");
249 LOGDBG(LY_LDGXPATH, "\t%s", set->val.str);
250 break;
251
252 case LYXP_SET_NUMBER:
253 LOGDBG(LY_LDGXPATH, "set NUMBER");
254
255 if (isnan(set->val.num)) {
256 str = strdup("NaN");
257 } else if ((set->val.num == 0) || (set->val.num == -0.0f)) {
258 str = strdup("0");
259 } else if (isinf(set->val.num) && !signbit(set->val.num)) {
260 str = strdup("Infinity");
261 } else if (isinf(set->val.num) && signbit(set->val.num)) {
262 str = strdup("-Infinity");
263 } else if ((long long)set->val.num == set->val.num) {
264 if (asprintf(&str, "%lld", (long long)set->val.num) == -1) {
265 str = NULL;
266 }
267 } else {
268 if (asprintf(&str, "%03.1Lf", set->val.num) == -1) {
269 str = NULL;
270 }
271 }
272 LY_CHECK_ERR_RET(!str, LOGMEM(NULL), );
273
274 LOGDBG(LY_LDGXPATH, "\t%s", str);
275 free(str);
276 }
277}
278
279#endif
280
281/**
282 * @brief Realloc the string \p str.
283 *
284 * @param[in] ctx libyang context for logging.
285 * @param[in] needed How much free space is required.
286 * @param[in,out] str Pointer to the string to use.
287 * @param[in,out] used Used bytes in \p str.
288 * @param[in,out] size Allocated bytes in \p str.
289 * @return LY_ERR
290 */
291static LY_ERR
Michal Vasko52927e22020-03-16 17:26:14 +0100292cast_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 +0200293{
294 if (*size - *used < needed) {
295 do {
296 if ((UINT16_MAX - *size) < LYXP_STRING_CAST_SIZE_STEP) {
297 LOGERR(ctx, LY_EINVAL, "XPath string length limit (%u) reached.", UINT16_MAX);
298 return LY_EINVAL;
299 }
300 *size += LYXP_STRING_CAST_SIZE_STEP;
301 } while (*size - *used < needed);
302 *str = ly_realloc(*str, *size * sizeof(char));
303 LY_CHECK_ERR_RET(!(*str), LOGMEM(ctx), LY_EMEM);
304 }
305
306 return LY_SUCCESS;
307}
308
309/**
310 * @brief Cast nodes recursively to one string @p str.
311 *
312 * @param[in] node Node to cast.
313 * @param[in] fake_cont Whether to put the data into a "fake" container.
314 * @param[in] root_type Type of the XPath root.
315 * @param[in] indent Current indent.
316 * @param[in,out] str Resulting string.
317 * @param[in,out] used Used bytes in @p str.
318 * @param[in,out] size Allocated bytes in @p str.
319 * @return LY_ERR
320 */
321static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200322cast_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 +0200323 uint16_t *used, uint16_t *size)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200324{
Radek Krejci7f769d72020-07-11 23:13:56 +0200325 char *buf, *line, *ptr = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200326 const char *value_str;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200327 const struct lyd_node *child;
Michal Vasko60ea6352020-06-29 13:39:39 +0200328 struct lyd_node *tree;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200329 struct lyd_node_any *any;
330 LY_ERR rc;
331
332 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) {
333 return LY_SUCCESS;
334 }
335
336 if (fake_cont) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200337 rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200338 LY_CHECK_RET(rc);
339 strcpy(*str + (*used - 1), "\n");
340 ++(*used);
341
342 ++indent;
343 }
344
345 switch (node->schema->nodetype) {
346 case LYS_CONTAINER:
347 case LYS_LIST:
348 case LYS_RPC:
349 case LYS_NOTIF:
Michal Vaskob7be7a82020-08-20 09:09:04 +0200350 rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200351 LY_CHECK_RET(rc);
352 strcpy(*str + (*used - 1), "\n");
353 ++(*used);
354
Radek Krejcia1c1e542020-09-29 16:06:52 +0200355 for (child = lyd_child(node); child; child = child->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200356 rc = cast_string_recursive(child, 0, root_type, indent + 1, str, used, size);
357 LY_CHECK_RET(rc);
358 }
359
360 break;
361
362 case LYS_LEAF:
363 case LYS_LEAFLIST:
Michal Vaskob7be7a82020-08-20 09:09:04 +0200364 value_str = LYD_CANON_VALUE(node);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200365
366 /* print indent */
Michal Vaskob7be7a82020-08-20 09:09:04 +0200367 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 +0200368 memset(*str + (*used - 1), ' ', indent * 2);
369 *used += indent * 2;
370
371 /* print value */
372 if (*used == 1) {
373 sprintf(*str + (*used - 1), "%s", value_str);
374 *used += strlen(value_str);
375 } else {
376 sprintf(*str + (*used - 1), "%s\n", value_str);
377 *used += strlen(value_str) + 1;
378 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200379
380 break;
381
382 case LYS_ANYXML:
383 case LYS_ANYDATA:
384 any = (struct lyd_node_any *)node;
385 if (!(void *)any->value.tree) {
386 /* no content */
387 buf = strdup("");
Michal Vaskob7be7a82020-08-20 09:09:04 +0200388 LY_CHECK_ERR_RET(!buf, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200389 } else {
Radek Krejci241f6b52020-05-21 18:13:49 +0200390 struct ly_out *out;
Radek Krejcia5bba312020-01-09 15:41:20 +0100391
Michal Vasko60ea6352020-06-29 13:39:39 +0200392 if (any->value_type == LYD_ANYDATA_LYB) {
393 /* try to parse it into a data tree */
Michal Vaskob7be7a82020-08-20 09:09:04 +0200394 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 +0200395 /* successfully parsed */
396 free(any->value.mem);
397 any->value.tree = tree;
398 any->value_type = LYD_ANYDATA_DATATREE;
399 }
Radek Krejci7931b192020-06-25 17:05:03 +0200400 /* error is covered by the following switch where LYD_ANYDATA_LYB causes failure */
Michal Vasko60ea6352020-06-29 13:39:39 +0200401 }
402
Michal Vasko03ff5a72019-09-11 13:49:33 +0200403 switch (any->value_type) {
404 case LYD_ANYDATA_STRING:
405 case LYD_ANYDATA_XML:
406 case LYD_ANYDATA_JSON:
407 buf = strdup(any->value.json);
Michal Vaskob7be7a82020-08-20 09:09:04 +0200408 LY_CHECK_ERR_RET(!buf, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200409 break;
410 case LYD_ANYDATA_DATATREE:
Radek Krejci84ce7b12020-06-11 17:28:25 +0200411 LY_CHECK_RET(ly_out_new_memory(&buf, 0, &out));
Michal Vasko3a41dff2020-07-15 14:30:28 +0200412 rc = lyd_print_all(out, any->value.tree, LYD_XML, 0);
Radek Krejci241f6b52020-05-21 18:13:49 +0200413 ly_out_free(out, NULL, 0);
Radek Krejcia5bba312020-01-09 15:41:20 +0100414 LY_CHECK_RET(rc < 0, -rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200415 break;
Michal Vasko60ea6352020-06-29 13:39:39 +0200416 case LYD_ANYDATA_LYB:
Michal Vaskob7be7a82020-08-20 09:09:04 +0200417 LOGERR(LYD_CTX(node), LY_EINVAL, "Cannot convert LYB anydata into string.");
Michal Vasko60ea6352020-06-29 13:39:39 +0200418 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200419 }
420 }
421
422 line = strtok_r(buf, "\n", &ptr);
423 do {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200424 rc = cast_string_realloc(LYD_CTX(node), indent * 2 + strlen(line) + 1, str, used, size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200425 if (rc != LY_SUCCESS) {
426 free(buf);
427 return rc;
428 }
429 memset(*str + (*used - 1), ' ', indent * 2);
430 *used += indent * 2;
431
432 strcpy(*str + (*used - 1), line);
433 *used += strlen(line);
434
435 strcpy(*str + (*used - 1), "\n");
436 *used += 1;
437 } while ((line = strtok_r(NULL, "\n", &ptr)));
438
439 free(buf);
440 break;
441
442 default:
Michal Vaskob7be7a82020-08-20 09:09:04 +0200443 LOGINT_RET(LYD_CTX(node));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200444 }
445
446 if (fake_cont) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200447 rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200448 LY_CHECK_RET(rc);
449 strcpy(*str + (*used - 1), "\n");
450 ++(*used);
451
452 --indent;
453 }
454
455 return LY_SUCCESS;
456}
457
458/**
459 * @brief Cast an element into a string.
460 *
461 * @param[in] node Node to cast.
462 * @param[in] fake_cont Whether to put the data into a "fake" container.
463 * @param[in] root_type Type of the XPath root.
464 * @param[out] str Element cast to dynamically-allocated string.
465 * @return LY_ERR
466 */
467static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200468cast_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 +0200469{
470 uint16_t used, size;
471 LY_ERR rc;
472
473 *str = malloc(LYXP_STRING_CAST_SIZE_START * sizeof(char));
Michal Vaskob7be7a82020-08-20 09:09:04 +0200474 LY_CHECK_ERR_RET(!*str, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200475 (*str)[0] = '\0';
476 used = 1;
477 size = LYXP_STRING_CAST_SIZE_START;
478
479 rc = cast_string_recursive(node, fake_cont, root_type, 0, str, &used, &size);
480 if (rc != LY_SUCCESS) {
481 free(*str);
482 return rc;
483 }
484
485 if (size > used) {
486 *str = ly_realloc(*str, used * sizeof(char));
Michal Vaskob7be7a82020-08-20 09:09:04 +0200487 LY_CHECK_ERR_RET(!*str, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200488 }
489 return LY_SUCCESS;
490}
491
492/**
493 * @brief Cast a LYXP_SET_NODE_SET set into a string.
494 * Context position aware.
495 *
496 * @param[in] set Set to cast.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200497 * @param[out] str Cast dynamically-allocated string.
498 * @return LY_ERR
499 */
500static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100501cast_node_set_to_string(struct lyxp_set *set, char **str)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200502{
Michal Vasko03ff5a72019-09-11 13:49:33 +0200503 switch (set->val.nodes[0].type) {
Michal Vasko2caefc12019-11-14 16:07:56 +0100504 case LYXP_NODE_NONE:
505 /* invalid */
506 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200507 case LYXP_NODE_ROOT:
508 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100509 return cast_string_elem(set->val.nodes[0].node, 1, set->root_type, str);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200510 case LYXP_NODE_ELEM:
511 case LYXP_NODE_TEXT:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100512 return cast_string_elem(set->val.nodes[0].node, 0, set->root_type, str);
Michal Vasko9f96a052020-03-10 09:41:45 +0100513 case LYXP_NODE_META:
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200514 *str = strdup(set->val.meta[0].meta->value.canonical);
515 if (!*str) {
516 LOGMEM_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200517 }
518 return LY_SUCCESS;
519 }
520
521 LOGINT_RET(set->ctx);
522}
523
524/**
525 * @brief Cast a string into an XPath number.
526 *
527 * @param[in] str String to use.
528 * @return Cast number.
529 */
530static long double
531cast_string_to_number(const char *str)
532{
533 long double num;
534 char *ptr;
535
536 errno = 0;
537 num = strtold(str, &ptr);
538 if (errno || *ptr) {
539 num = NAN;
540 }
541 return num;
542}
543
544/**
545 * @brief Callback for checking value equality.
546 *
Radek Krejci857189e2020-09-01 13:26:36 +0200547 * Implementation of ::values_equal_cb.
548 *
Michal Vasko03ff5a72019-09-11 13:49:33 +0200549 * @param[in] val1_p First value.
550 * @param[in] val2_p Second value.
551 * @param[in] mod Whether hash table is being modified.
552 * @param[in] cb_data Callback data.
Radek Krejci857189e2020-09-01 13:26:36 +0200553 * @return Boolean value whether values are equal or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200554 */
Radek Krejci857189e2020-09-01 13:26:36 +0200555static ly_bool
556set_values_equal_cb(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void *UNUSED(cb_data))
Michal Vasko03ff5a72019-09-11 13:49:33 +0200557{
558 struct lyxp_set_hash_node *val1, *val2;
559
560 val1 = (struct lyxp_set_hash_node *)val1_p;
561 val2 = (struct lyxp_set_hash_node *)val2_p;
562
563 if ((val1->node == val2->node) && (val1->type == val2->type)) {
564 return 1;
565 }
566
567 return 0;
568}
569
570/**
571 * @brief Insert node and its hash into set.
572 *
573 * @param[in] set et to insert to.
574 * @param[in] node Node with hash.
575 * @param[in] type Node type.
576 */
577static void
578set_insert_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type)
579{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200580 LY_ERR r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200581 uint32_t i, hash;
582 struct lyxp_set_hash_node hnode;
583
584 if (!set->ht && (set->used >= LYD_HT_MIN_ITEMS)) {
585 /* create hash table and add all the nodes */
586 set->ht = lyht_new(1, sizeof(struct lyxp_set_hash_node), set_values_equal_cb, NULL, 1);
587 for (i = 0; i < set->used; ++i) {
588 hnode.node = set->val.nodes[i].node;
589 hnode.type = set->val.nodes[i].type;
590
591 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
592 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
593 hash = dict_hash_multi(hash, NULL, 0);
594
595 r = lyht_insert(set->ht, &hnode, hash, NULL);
596 assert(!r);
597 (void)r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200598
Michal Vasko9d6befd2019-12-11 14:56:56 +0100599 if (hnode.node == node) {
600 /* it was just added, do not add it twice */
601 node = NULL;
602 }
603 }
604 }
605
606 if (set->ht && node) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200607 /* add the new node into hash table */
608 hnode.node = node;
609 hnode.type = type;
610
611 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
612 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
613 hash = dict_hash_multi(hash, NULL, 0);
614
615 r = lyht_insert(set->ht, &hnode, hash, NULL);
616 assert(!r);
617 (void)r;
618 }
619}
620
621/**
622 * @brief Remove node and its hash from set.
623 *
624 * @param[in] set Set to remove from.
625 * @param[in] node Node to remove.
626 * @param[in] type Node type.
627 */
628static void
629set_remove_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type)
630{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200631 LY_ERR r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200632 struct lyxp_set_hash_node hnode;
633 uint32_t hash;
634
635 if (set->ht) {
636 hnode.node = node;
637 hnode.type = type;
638
639 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
640 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
641 hash = dict_hash_multi(hash, NULL, 0);
642
643 r = lyht_remove(set->ht, &hnode, hash);
644 assert(!r);
645 (void)r;
646
647 if (!set->ht->used) {
648 lyht_free(set->ht);
649 set->ht = NULL;
650 }
651 }
652}
653
654/**
655 * @brief Check whether node is in set based on its hash.
656 *
657 * @param[in] set Set to search in.
658 * @param[in] node Node to search for.
659 * @param[in] type Node type.
660 * @param[in] skip_idx Index in @p set to skip.
661 * @return LY_ERR
662 */
663static LY_ERR
664set_dup_node_hash_check(const struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type, int skip_idx)
665{
666 struct lyxp_set_hash_node hnode, *match_p;
667 uint32_t hash;
668
669 hnode.node = node;
670 hnode.type = type;
671
672 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
673 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
674 hash = dict_hash_multi(hash, NULL, 0);
675
676 if (!lyht_find(set->ht, &hnode, hash, (void **)&match_p)) {
677 if ((skip_idx > -1) && (set->val.nodes[skip_idx].node == match_p->node) && (set->val.nodes[skip_idx].type == match_p->type)) {
678 /* we found it on the index that should be skipped, find another */
679 hnode = *match_p;
680 if (lyht_find_next(set->ht, &hnode, hash, (void **)&match_p)) {
681 /* none other found */
682 return LY_SUCCESS;
683 }
684 }
685
686 return LY_EEXIST;
687 }
688
689 /* not found */
690 return LY_SUCCESS;
691}
692
Michal Vaskod3678892020-05-21 10:06:58 +0200693void
694lyxp_set_free_content(struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200695{
696 if (!set) {
697 return;
698 }
699
700 if (set->type == LYXP_SET_NODE_SET) {
701 free(set->val.nodes);
702 lyht_free(set->ht);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200703 } else if (set->type == LYXP_SET_SCNODE_SET) {
704 free(set->val.scnodes);
Michal Vaskod3678892020-05-21 10:06:58 +0200705 lyht_free(set->ht);
706 } else {
707 if (set->type == LYXP_SET_STRING) {
708 free(set->val.str);
709 }
710 set->type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200711 }
Michal Vaskod3678892020-05-21 10:06:58 +0200712
713 set->val.nodes = NULL;
714 set->used = 0;
715 set->size = 0;
716 set->ht = NULL;
717 set->ctx_pos = 0;
718 set->ctx_pos = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200719}
720
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100721/**
722 * @brief Free dynamically-allocated set.
723 *
724 * @param[in] set Set to free.
725 */
726static void
Michal Vasko03ff5a72019-09-11 13:49:33 +0200727lyxp_set_free(struct lyxp_set *set)
728{
729 if (!set) {
730 return;
731 }
732
Michal Vaskod3678892020-05-21 10:06:58 +0200733 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200734 free(set);
735}
736
737/**
738 * @brief Initialize set context.
739 *
740 * @param[in] new Set to initialize.
741 * @param[in] set Arbitrary initialized set.
742 */
743static void
Michal Vasko4c7763f2020-07-27 17:40:37 +0200744set_init(struct lyxp_set *new, const struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200745{
746 memset(new, 0, sizeof *new);
Michal Vasko02a77382019-09-12 11:47:35 +0200747 if (set) {
748 new->ctx = set->ctx;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200749 new->cur_node = set->cur_node;
Michal Vasko588112f2019-12-10 14:51:53 +0100750 new->root_type = set->root_type;
Michal Vasko6b26e742020-07-17 15:02:10 +0200751 new->context_op = set->context_op;
Michal Vaskof03ed032020-03-04 13:31:44 +0100752 new->tree = set->tree;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200753 new->cur_mod = set->cur_mod;
Michal Vasko02a77382019-09-12 11:47:35 +0200754 new->format = set->format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200755 new->prefix_data = set->prefix_data;
Michal Vasko02a77382019-09-12 11:47:35 +0200756 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200757}
758
759/**
760 * @brief Create a deep copy of a set.
761 *
762 * @param[in] set Set to copy.
763 * @return Copy of @p set.
764 */
765static struct lyxp_set *
766set_copy(struct lyxp_set *set)
767{
768 struct lyxp_set *ret;
769 uint16_t i;
770
771 if (!set) {
772 return NULL;
773 }
774
775 ret = malloc(sizeof *ret);
776 LY_CHECK_ERR_RET(!ret, LOGMEM(set->ctx), NULL);
777 set_init(ret, set);
778
779 if (set->type == LYXP_SET_SCNODE_SET) {
780 ret->type = set->type;
781
782 for (i = 0; i < set->used; ++i) {
Michal Vaskoba716542019-12-16 10:01:58 +0100783 if ((set->val.scnodes[i].in_ctx == 1) || (set->val.scnodes[i].in_ctx == -2)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +0200784 uint32_t idx;
785 LY_CHECK_ERR_RET(lyxp_set_scnode_insert_node(ret, set->val.scnodes[i].scnode, set->val.scnodes[i].type, &idx),
786 lyxp_set_free(ret), NULL);
Michal Vasko3f27c522020-01-06 08:37:49 +0100787 /* coverity seems to think scnodes can be NULL */
Radek Krejciaa6b53f2020-08-27 15:19:03 +0200788 if (!ret->val.scnodes) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200789 lyxp_set_free(ret);
790 return NULL;
791 }
Michal Vaskoba716542019-12-16 10:01:58 +0100792 ret->val.scnodes[idx].in_ctx = set->val.scnodes[i].in_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200793 }
794 }
795 } else if (set->type == LYXP_SET_NODE_SET) {
796 ret->type = set->type;
797 ret->val.nodes = malloc(set->used * sizeof *ret->val.nodes);
798 LY_CHECK_ERR_RET(!ret->val.nodes, LOGMEM(set->ctx); free(ret), NULL);
799 memcpy(ret->val.nodes, set->val.nodes, set->used * sizeof *ret->val.nodes);
800
801 ret->used = ret->size = set->used;
802 ret->ctx_pos = set->ctx_pos;
803 ret->ctx_size = set->ctx_size;
804 ret->ht = lyht_dup(set->ht);
805 } else {
Radek Krejci0f969882020-08-21 16:56:47 +0200806 memcpy(ret, set, sizeof *ret);
807 if (set->type == LYXP_SET_STRING) {
808 ret->val.str = strdup(set->val.str);
809 LY_CHECK_ERR_RET(!ret->val.str, LOGMEM(set->ctx); free(ret), NULL);
810 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200811 }
812
813 return ret;
814}
815
816/**
817 * @brief Fill XPath set with a string. Any current data are disposed of.
818 *
819 * @param[in] set Set to fill.
820 * @param[in] string String to fill into \p set.
821 * @param[in] str_len Length of \p string. 0 is a valid value!
822 */
823static void
824set_fill_string(struct lyxp_set *set, const char *string, uint16_t str_len)
825{
Michal Vaskod3678892020-05-21 10:06:58 +0200826 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200827
828 set->type = LYXP_SET_STRING;
829 if ((str_len == 0) && (string[0] != '\0')) {
830 string = "";
831 }
832 set->val.str = strndup(string, str_len);
833}
834
835/**
836 * @brief Fill XPath set with a number. Any current data are disposed of.
837 *
838 * @param[in] set Set to fill.
839 * @param[in] number Number to fill into \p set.
840 */
841static void
842set_fill_number(struct lyxp_set *set, long double number)
843{
Michal Vaskod3678892020-05-21 10:06:58 +0200844 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200845
846 set->type = LYXP_SET_NUMBER;
847 set->val.num = number;
848}
849
850/**
851 * @brief Fill XPath set with a boolean. Any current data are disposed of.
852 *
853 * @param[in] set Set to fill.
854 * @param[in] boolean Boolean to fill into \p set.
855 */
856static void
Radek Krejci857189e2020-09-01 13:26:36 +0200857set_fill_boolean(struct lyxp_set *set, ly_bool boolean)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200858{
Michal Vaskod3678892020-05-21 10:06:58 +0200859 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200860
861 set->type = LYXP_SET_BOOLEAN;
Michal Vasko004d3152020-06-11 19:59:22 +0200862 set->val.bln = boolean;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200863}
864
865/**
866 * @brief Fill XPath set with the value from another set (deep assign).
867 * Any current data are disposed of.
868 *
869 * @param[in] trg Set to fill.
870 * @param[in] src Source set to copy into \p trg.
871 */
872static void
Michal Vasko4c7763f2020-07-27 17:40:37 +0200873set_fill_set(struct lyxp_set *trg, const struct lyxp_set *src)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200874{
875 if (!trg || !src) {
876 return;
877 }
878
879 if (trg->type == LYXP_SET_NODE_SET) {
880 free(trg->val.nodes);
881 } else if (trg->type == LYXP_SET_STRING) {
882 free(trg->val.str);
883 }
884 set_init(trg, src);
885
886 if (src->type == LYXP_SET_SCNODE_SET) {
887 trg->type = LYXP_SET_SCNODE_SET;
888 trg->used = src->used;
889 trg->size = src->used;
890
891 trg->val.scnodes = ly_realloc(trg->val.scnodes, trg->size * sizeof *trg->val.scnodes);
892 LY_CHECK_ERR_RET(!trg->val.scnodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), );
893 memcpy(trg->val.scnodes, src->val.scnodes, src->used * sizeof *src->val.scnodes);
894 } else if (src->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +0200895 set_fill_boolean(trg, src->val.bln);
Michal Vasko44f3d2c2020-08-24 09:49:38 +0200896 } else if (src->type == LYXP_SET_NUMBER) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200897 set_fill_number(trg, src->val.num);
898 } else if (src->type == LYXP_SET_STRING) {
899 set_fill_string(trg, src->val.str, strlen(src->val.str));
900 } else {
901 if (trg->type == LYXP_SET_NODE_SET) {
902 free(trg->val.nodes);
903 } else if (trg->type == LYXP_SET_STRING) {
904 free(trg->val.str);
905 }
906
Michal Vaskod3678892020-05-21 10:06:58 +0200907 assert(src->type == LYXP_SET_NODE_SET);
908
909 trg->type = LYXP_SET_NODE_SET;
910 trg->used = src->used;
911 trg->size = src->used;
912 trg->ctx_pos = src->ctx_pos;
913 trg->ctx_size = src->ctx_size;
914
915 trg->val.nodes = malloc(trg->used * sizeof *trg->val.nodes);
916 LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), );
917 memcpy(trg->val.nodes, src->val.nodes, src->used * sizeof *src->val.nodes);
918 if (src->ht) {
919 trg->ht = lyht_dup(src->ht);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200920 } else {
Michal Vaskod3678892020-05-21 10:06:58 +0200921 trg->ht = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200922 }
923 }
924}
925
926/**
927 * @brief Clear context of all schema nodes.
928 *
929 * @param[in] set Set to clear.
930 */
931static void
932set_scnode_clear_ctx(struct lyxp_set *set)
933{
934 uint32_t i;
935
936 for (i = 0; i < set->used; ++i) {
937 if (set->val.scnodes[i].in_ctx == 1) {
938 set->val.scnodes[i].in_ctx = 0;
Michal Vasko5c4e5892019-11-14 12:31:38 +0100939 } else if (set->val.scnodes[i].in_ctx == -2) {
940 set->val.scnodes[i].in_ctx = -1;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200941 }
942 }
943}
944
945/**
946 * @brief Remove a node from a set. Removing last node changes
947 * set into LYXP_SET_EMPTY. Context position aware.
948 *
949 * @param[in] set Set to use.
950 * @param[in] idx Index from @p set of the node to be removed.
951 */
952static void
953set_remove_node(struct lyxp_set *set, uint32_t idx)
954{
955 assert(set && (set->type == LYXP_SET_NODE_SET));
956 assert(idx < set->used);
957
958 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
959
960 --set->used;
961 if (set->used) {
962 memmove(&set->val.nodes[idx], &set->val.nodes[idx + 1],
963 (set->used - idx) * sizeof *set->val.nodes);
964 } else {
Michal Vaskod3678892020-05-21 10:06:58 +0200965 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200966 }
967}
968
969/**
Michal Vasko2caefc12019-11-14 16:07:56 +0100970 * @brief Remove a node from a set by setting its type to LYXP_NODE_NONE.
Michal Vasko57eab132019-09-24 11:46:26 +0200971 *
972 * @param[in] set Set to use.
973 * @param[in] idx Index from @p set of the node to be removed.
974 */
975static void
Michal Vasko2caefc12019-11-14 16:07:56 +0100976set_remove_node_none(struct lyxp_set *set, uint32_t idx)
Michal Vasko57eab132019-09-24 11:46:26 +0200977{
978 assert(set && (set->type == LYXP_SET_NODE_SET));
979 assert(idx < set->used);
980
Michal Vasko2caefc12019-11-14 16:07:56 +0100981 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
982 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
983 }
984 set->val.nodes[idx].type = LYXP_NODE_NONE;
Michal Vasko57eab132019-09-24 11:46:26 +0200985}
986
987/**
Michal Vasko2caefc12019-11-14 16:07:56 +0100988 * @brief Remove all LYXP_NODE_NONE nodes from a set. Removing last node changes
Michal Vasko57eab132019-09-24 11:46:26 +0200989 * set into LYXP_SET_EMPTY. Context position aware.
990 *
991 * @param[in] set Set to consolidate.
992 */
993static void
Michal Vasko2caefc12019-11-14 16:07:56 +0100994set_remove_nodes_none(struct lyxp_set *set)
Michal Vasko57eab132019-09-24 11:46:26 +0200995{
Michal Vaskoed4fcfe2020-07-08 10:38:56 +0200996 uint16_t i, orig_used, end = 0;
Michal Vasko57eab132019-09-24 11:46:26 +0200997 int32_t start;
998
Michal Vaskod3678892020-05-21 10:06:58 +0200999 assert(set);
Michal Vasko57eab132019-09-24 11:46:26 +02001000
1001 orig_used = set->used;
1002 set->used = 0;
Michal Vaskod989ba02020-08-24 10:59:24 +02001003 for (i = 0; i < orig_used; ) {
Michal Vasko57eab132019-09-24 11:46:26 +02001004 start = -1;
1005 do {
Michal Vasko2caefc12019-11-14 16:07:56 +01001006 if ((set->val.nodes[i].type != LYXP_NODE_NONE) && (start == -1)) {
Michal Vasko57eab132019-09-24 11:46:26 +02001007 start = i;
Michal Vasko2caefc12019-11-14 16:07:56 +01001008 } else if ((start > -1) && (set->val.nodes[i].type == LYXP_NODE_NONE)) {
Michal Vasko57eab132019-09-24 11:46:26 +02001009 end = i;
1010 ++i;
1011 break;
1012 }
1013
1014 ++i;
1015 if (i == orig_used) {
1016 end = i;
1017 }
1018 } while (i < orig_used);
1019
1020 if (start > -1) {
1021 /* move the whole chunk of valid nodes together */
1022 if (set->used != (unsigned)start) {
1023 memmove(&set->val.nodes[set->used], &set->val.nodes[start], (end - start) * sizeof *set->val.nodes);
1024 }
1025 set->used += end - start;
1026 }
1027 }
Michal Vasko57eab132019-09-24 11:46:26 +02001028}
1029
1030/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02001031 * @brief Check for duplicates in a node set.
1032 *
1033 * @param[in] set Set to check.
1034 * @param[in] node Node to look for in @p set.
1035 * @param[in] node_type Type of @p node.
1036 * @param[in] skip_idx Index from @p set to skip.
1037 * @return LY_ERR
1038 */
1039static LY_ERR
1040set_dup_node_check(const struct lyxp_set *set, const struct lyd_node *node, enum lyxp_node_type node_type, int skip_idx)
1041{
1042 uint32_t i;
1043
Michal Vasko2caefc12019-11-14 16:07:56 +01001044 if (set->ht && node) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001045 return set_dup_node_hash_check(set, (struct lyd_node *)node, node_type, skip_idx);
1046 }
1047
1048 for (i = 0; i < set->used; ++i) {
1049 if ((skip_idx > -1) && (i == (unsigned)skip_idx)) {
1050 continue;
1051 }
1052
1053 if ((set->val.nodes[i].node == node) && (set->val.nodes[i].type == node_type)) {
1054 return LY_EEXIST;
1055 }
1056 }
1057
1058 return LY_SUCCESS;
1059}
1060
Radek Krejci857189e2020-09-01 13:26:36 +02001061ly_bool
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001062lyxp_set_scnode_contains(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type, int skip_idx,
1063 uint32_t *index_p)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001064{
1065 uint32_t i;
1066
1067 for (i = 0; i < set->used; ++i) {
1068 if ((skip_idx > -1) && (i == (unsigned)skip_idx)) {
1069 continue;
1070 }
1071
1072 if ((set->val.scnodes[i].scnode == node) && (set->val.scnodes[i].type == node_type)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001073 if (index_p) {
1074 *index_p = i;
1075 }
1076 return 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001077 }
1078 }
1079
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001080 return 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001081}
1082
Michal Vaskoecd62de2019-11-13 12:35:11 +01001083void
1084lyxp_set_scnode_merge(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001085{
1086 uint32_t orig_used, i, j;
1087
Michal Vaskod3678892020-05-21 10:06:58 +02001088 assert((set1->type == LYXP_SET_SCNODE_SET) && (set2->type == LYXP_SET_SCNODE_SET));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001089
Michal Vaskod3678892020-05-21 10:06:58 +02001090 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001091 return;
1092 }
1093
Michal Vaskod3678892020-05-21 10:06:58 +02001094 if (!set1->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001095 memcpy(set1, set2, sizeof *set1);
1096 return;
1097 }
1098
1099 if (set1->used + set2->used > set1->size) {
1100 set1->size = set1->used + set2->used;
1101 set1->val.scnodes = ly_realloc(set1->val.scnodes, set1->size * sizeof *set1->val.scnodes);
1102 LY_CHECK_ERR_RET(!set1->val.scnodes, LOGMEM(set1->ctx), );
1103 }
1104
1105 orig_used = set1->used;
1106
1107 for (i = 0; i < set2->used; ++i) {
1108 for (j = 0; j < orig_used; ++j) {
1109 /* detect duplicities */
1110 if (set1->val.scnodes[j].scnode == set2->val.scnodes[i].scnode) {
1111 break;
1112 }
1113 }
1114
1115 if (j == orig_used) {
1116 memcpy(&set1->val.scnodes[set1->used], &set2->val.scnodes[i], sizeof *set2->val.scnodes);
1117 ++set1->used;
1118 }
1119 }
1120
Michal Vaskod3678892020-05-21 10:06:58 +02001121 lyxp_set_free_content(set2);
1122 set2->type = LYXP_SET_SCNODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001123}
1124
1125/**
1126 * @brief Insert a node into a set. Context position aware.
1127 *
1128 * @param[in] set Set to use.
1129 * @param[in] node Node to insert to @p set.
1130 * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting.
1131 * @param[in] node_type Node type of @p node.
1132 * @param[in] idx Index in @p set to insert into.
1133 */
1134static void
1135set_insert_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx)
1136{
Michal Vaskod3678892020-05-21 10:06:58 +02001137 assert(set && (set->type == LYXP_SET_NODE_SET));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001138
Michal Vaskod3678892020-05-21 10:06:58 +02001139 if (!set->size) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001140 /* first item */
1141 if (idx) {
1142 /* no real harm done, but it is a bug */
1143 LOGINT(set->ctx);
1144 idx = 0;
1145 }
1146 set->val.nodes = malloc(LYXP_SET_SIZE_START * sizeof *set->val.nodes);
1147 LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), );
1148 set->type = LYXP_SET_NODE_SET;
1149 set->used = 0;
1150 set->size = LYXP_SET_SIZE_START;
1151 set->ctx_pos = 1;
1152 set->ctx_size = 1;
1153 set->ht = NULL;
1154 } else {
1155 /* not an empty set */
1156 if (set->used == set->size) {
1157
1158 /* set is full */
1159 set->val.nodes = ly_realloc(set->val.nodes, (set->size + LYXP_SET_SIZE_STEP) * sizeof *set->val.nodes);
1160 LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), );
1161 set->size += LYXP_SET_SIZE_STEP;
1162 }
1163
1164 if (idx > set->used) {
1165 LOGINT(set->ctx);
1166 idx = set->used;
1167 }
1168
1169 /* make space for the new node */
1170 if (idx < set->used) {
1171 memmove(&set->val.nodes[idx + 1], &set->val.nodes[idx], (set->used - idx) * sizeof *set->val.nodes);
1172 }
1173 }
1174
1175 /* finally assign the value */
1176 set->val.nodes[idx].node = (struct lyd_node *)node;
1177 set->val.nodes[idx].type = node_type;
1178 set->val.nodes[idx].pos = pos;
1179 ++set->used;
1180
Michal Vasko2caefc12019-11-14 16:07:56 +01001181 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1182 set_insert_node_hash(set, (struct lyd_node *)node, node_type);
1183 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001184}
1185
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001186LY_ERR
1187lyxp_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 +02001188{
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001189 uint32_t index;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001190
1191 assert(set->type == LYXP_SET_SCNODE_SET);
1192
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001193 if (lyxp_set_scnode_contains(set, node, node_type, -1, &index)) {
1194 set->val.scnodes[index].in_ctx = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001195 } else {
1196 if (set->used == set->size) {
1197 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 +02001198 LY_CHECK_ERR_RET(!set->val.scnodes, LOGMEM(set->ctx), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001199 set->size += LYXP_SET_SIZE_STEP;
1200 }
1201
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001202 index = set->used;
1203 set->val.scnodes[index].scnode = (struct lysc_node *)node;
1204 set->val.scnodes[index].type = node_type;
1205 set->val.scnodes[index].in_ctx = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001206 ++set->used;
1207 }
1208
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001209 if (index_p) {
1210 *index_p = index;
1211 }
1212
1213 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001214}
1215
1216/**
1217 * @brief Replace a node in a set with another. Context position aware.
1218 *
1219 * @param[in] set Set to use.
1220 * @param[in] node Node to insert to @p set.
1221 * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting.
1222 * @param[in] node_type Node type of @p node.
1223 * @param[in] idx Index in @p set of the node to replace.
1224 */
1225static void
1226set_replace_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx)
1227{
1228 assert(set && (idx < set->used));
1229
Michal Vasko2caefc12019-11-14 16:07:56 +01001230 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1231 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1232 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001233 set->val.nodes[idx].node = (struct lyd_node *)node;
1234 set->val.nodes[idx].type = node_type;
1235 set->val.nodes[idx].pos = pos;
Michal Vasko2caefc12019-11-14 16:07:56 +01001236 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1237 set_insert_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1238 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001239}
1240
1241/**
1242 * @brief Set all nodes with ctx 1 to a new unique context value.
1243 *
1244 * @param[in] set Set to modify.
1245 * @return New context value.
1246 */
Michal Vasko5c4e5892019-11-14 12:31:38 +01001247static int32_t
Michal Vasko03ff5a72019-09-11 13:49:33 +02001248set_scnode_new_in_ctx(struct lyxp_set *set)
1249{
Michal Vasko5c4e5892019-11-14 12:31:38 +01001250 uint32_t i;
1251 int32_t ret_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001252
1253 assert(set->type == LYXP_SET_SCNODE_SET);
1254
1255 ret_ctx = 3;
1256retry:
1257 for (i = 0; i < set->used; ++i) {
1258 if (set->val.scnodes[i].in_ctx >= ret_ctx) {
1259 ret_ctx = set->val.scnodes[i].in_ctx + 1;
1260 goto retry;
1261 }
1262 }
1263 for (i = 0; i < set->used; ++i) {
1264 if (set->val.scnodes[i].in_ctx == 1) {
1265 set->val.scnodes[i].in_ctx = ret_ctx;
1266 }
1267 }
1268
1269 return ret_ctx;
1270}
1271
1272/**
1273 * @brief Get unique @p node position in the data.
1274 *
1275 * @param[in] node Node to find.
1276 * @param[in] node_type Node type of @p node.
1277 * @param[in] root Root node.
1278 * @param[in] root_type Type of the XPath @p root node.
1279 * @param[in] prev Node that we think is before @p node in DFS from @p root. Can optionally
1280 * be used to increase efficiency and start the DFS from this node.
1281 * @param[in] prev_pos Node @p prev position. Optional, but must be set if @p prev is set.
1282 * @return Node position.
1283 */
1284static uint32_t
1285get_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 +02001286 enum lyxp_node_type root_type, const struct lyd_node **prev, uint32_t *prev_pos)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001287{
Michal Vasko56daf732020-08-10 10:57:18 +02001288 const struct lyd_node *elem = NULL, *top_sibling;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001289 uint32_t pos = 1;
1290
1291 assert(prev && prev_pos && !root->prev->next);
1292
1293 if ((node_type == LYXP_NODE_ROOT) || (node_type == LYXP_NODE_ROOT_CONFIG)) {
1294 return 0;
1295 }
1296
1297 if (*prev) {
1298 /* start from the previous element instead from the root */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001299 pos = *prev_pos;
Radek Krejci1e008d22020-08-17 11:37:37 +02001300 for (top_sibling = *prev; top_sibling->parent; top_sibling = (struct lyd_node *)top_sibling->parent) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001301 goto dfs_search;
1302 }
1303
1304 for (top_sibling = root; top_sibling; top_sibling = top_sibling->next) {
Michal Vasko56daf732020-08-10 10:57:18 +02001305 LYD_TREE_DFS_BEGIN(top_sibling, elem) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001306dfs_search:
Michal Vasko56daf732020-08-10 10:57:18 +02001307 if (*prev && !elem) {
1308 /* resume previous DFS */
1309 elem = LYD_TREE_DFS_next = (struct lyd_node *)*prev;
1310 LYD_TREE_DFS_continue = 0;
1311 }
1312
Michal Vasko03ff5a72019-09-11 13:49:33 +02001313 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (elem->schema->flags & LYS_CONFIG_R)) {
Michal Vasko56daf732020-08-10 10:57:18 +02001314 /* skip */
1315 LYD_TREE_DFS_continue = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001316 } else {
Michal Vasko56daf732020-08-10 10:57:18 +02001317 if (elem == node) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001318 break;
1319 }
Michal Vasko56daf732020-08-10 10:57:18 +02001320 ++pos;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001321 }
Michal Vasko56daf732020-08-10 10:57:18 +02001322
1323 LYD_TREE_DFS_END(top_sibling, elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001324 }
1325
1326 /* node found */
1327 if (elem) {
1328 break;
1329 }
1330 }
1331
1332 if (!elem) {
1333 if (!(*prev)) {
1334 /* we went from root and failed to find it, cannot be */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001335 LOGINT(LYD_CTX(node));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001336 return 0;
1337 } else {
Michal Vasko56daf732020-08-10 10:57:18 +02001338 /* start the search again from the beginning */
1339 *prev = root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001340
Michal Vasko56daf732020-08-10 10:57:18 +02001341 top_sibling = root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001342 pos = 1;
1343 goto dfs_search;
1344 }
1345 }
1346
1347 /* remember the last found node for next time */
1348 *prev = node;
1349 *prev_pos = pos;
1350
1351 return pos;
1352}
1353
1354/**
1355 * @brief Assign (fill) missing node positions.
1356 *
1357 * @param[in] set Set to fill positions in.
1358 * @param[in] root Context root node.
1359 * @param[in] root_type Context root type.
1360 * @return LY_ERR
1361 */
1362static LY_ERR
1363set_assign_pos(struct lyxp_set *set, const struct lyd_node *root, enum lyxp_node_type root_type)
1364{
1365 const struct lyd_node *prev = NULL, *tmp_node;
1366 uint32_t i, tmp_pos = 0;
1367
1368 for (i = 0; i < set->used; ++i) {
1369 if (!set->val.nodes[i].pos) {
1370 tmp_node = NULL;
1371 switch (set->val.nodes[i].type) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001372 case LYXP_NODE_META:
1373 tmp_node = set->val.meta[i].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001374 if (!tmp_node) {
1375 LOGINT_RET(root->schema->module->ctx);
1376 }
Radek Krejci0f969882020-08-21 16:56:47 +02001377 /* fallthrough */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001378 case LYXP_NODE_ELEM:
1379 case LYXP_NODE_TEXT:
1380 if (!tmp_node) {
1381 tmp_node = set->val.nodes[i].node;
1382 }
1383 set->val.nodes[i].pos = get_node_pos(tmp_node, set->val.nodes[i].type, root, root_type, &prev, &tmp_pos);
1384 break;
1385 default:
1386 /* all roots have position 0 */
1387 break;
1388 }
1389 }
1390 }
1391
1392 return LY_SUCCESS;
1393}
1394
1395/**
Michal Vasko9f96a052020-03-10 09:41:45 +01001396 * @brief Get unique @p meta position in the parent metadata.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001397 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001398 * @param[in] meta Metadata to use.
1399 * @return Metadata position.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001400 */
1401static uint16_t
Michal Vasko9f96a052020-03-10 09:41:45 +01001402get_meta_pos(struct lyd_meta *meta)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001403{
1404 uint16_t pos = 0;
Michal Vasko9f96a052020-03-10 09:41:45 +01001405 struct lyd_meta *meta2;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001406
Michal Vasko9f96a052020-03-10 09:41:45 +01001407 for (meta2 = meta->parent->meta; meta2 && (meta2 != meta); meta2 = meta2->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001408 ++pos;
1409 }
1410
Michal Vasko9f96a052020-03-10 09:41:45 +01001411 assert(meta2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001412 return pos;
1413}
1414
1415/**
1416 * @brief Compare 2 nodes in respect to XPath document order.
1417 *
1418 * @param[in] item1 1st node.
1419 * @param[in] item2 2nd node.
1420 * @return If 1st > 2nd returns 1, 1st == 2nd returns 0, and 1st < 2nd returns -1.
1421 */
1422static int
1423set_sort_compare(struct lyxp_set_node *item1, struct lyxp_set_node *item2)
1424{
Michal Vasko9f96a052020-03-10 09:41:45 +01001425 uint32_t meta_pos1 = 0, meta_pos2 = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001426
1427 if (item1->pos < item2->pos) {
1428 return -1;
1429 }
1430
1431 if (item1->pos > item2->pos) {
1432 return 1;
1433 }
1434
1435 /* node positions are equal, the fun case */
1436
1437 /* 1st ELEM - == - 2nd TEXT, 1st TEXT - == - 2nd ELEM */
1438 /* special case since text nodes are actually saved as their parents */
1439 if ((item1->node == item2->node) && (item1->type != item2->type)) {
1440 if (item1->type == LYXP_NODE_ELEM) {
1441 assert(item2->type == LYXP_NODE_TEXT);
1442 return -1;
1443 } else {
1444 assert((item1->type == LYXP_NODE_TEXT) && (item2->type == LYXP_NODE_ELEM));
1445 return 1;
1446 }
1447 }
1448
Michal Vasko9f96a052020-03-10 09:41:45 +01001449 /* we need meta positions now */
1450 if (item1->type == LYXP_NODE_META) {
1451 meta_pos1 = get_meta_pos((struct lyd_meta *)item1->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001452 }
Michal Vasko9f96a052020-03-10 09:41:45 +01001453 if (item2->type == LYXP_NODE_META) {
1454 meta_pos2 = get_meta_pos((struct lyd_meta *)item2->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001455 }
1456
Michal Vasko9f96a052020-03-10 09:41:45 +01001457 /* 1st ROOT - 2nd ROOT, 1st ELEM - 2nd ELEM, 1st TEXT - 2nd TEXT, 1st META - =pos= - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001458 /* check for duplicates */
1459 if (item1->node == item2->node) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001460 assert((item1->type == item2->type) && ((item1->type != LYXP_NODE_META) || (meta_pos1 == meta_pos2)));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001461 return 0;
1462 }
1463
Michal Vasko9f96a052020-03-10 09:41:45 +01001464 /* 1st ELEM - 2nd TEXT, 1st ELEM - any pos - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001465 /* elem is always first, 2nd node is after it */
1466 if (item1->type == LYXP_NODE_ELEM) {
1467 assert(item2->type != LYXP_NODE_ELEM);
1468 return -1;
1469 }
1470
Michal Vasko9f96a052020-03-10 09:41:45 +01001471 /* 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 +02001472 /* 2nd is before 1st */
Michal Vasko69730152020-10-09 16:30:07 +02001473 if (((item1->type == LYXP_NODE_TEXT) &&
1474 ((item2->type == LYXP_NODE_ELEM) || (item2->type == LYXP_NODE_META))) ||
1475 ((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_ELEM)) ||
1476 (((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_META)) &&
1477 (meta_pos1 > meta_pos2))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001478 return 1;
1479 }
1480
Michal Vasko9f96a052020-03-10 09:41:45 +01001481 /* 1st META - any pos - 2nd TEXT, 1st META <pos< - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001482 /* 2nd is after 1st */
1483 return -1;
1484}
1485
1486/**
1487 * @brief Set cast for comparisons.
1488 *
1489 * @param[in] trg Target set to cast source into.
1490 * @param[in] src Source set.
1491 * @param[in] type Target set type.
1492 * @param[in] src_idx Source set node index.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001493 * @return LY_ERR
1494 */
1495static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001496set_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 +02001497{
1498 assert(src->type == LYXP_SET_NODE_SET);
1499
1500 set_init(trg, src);
1501
1502 /* insert node into target set */
1503 set_insert_node(trg, src->val.nodes[src_idx].node, src->val.nodes[src_idx].pos, src->val.nodes[src_idx].type, 0);
1504
1505 /* cast target set appropriately */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001506 return lyxp_set_cast(trg, type);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001507}
1508
Michal Vasko4c7763f2020-07-27 17:40:37 +02001509/**
1510 * @brief Set content canonization for comparisons.
1511 *
1512 * @param[in] trg Target set to put the canononized source into.
1513 * @param[in] src Source set.
1514 * @param[in] xp_node Source XPath node/meta to use for canonization.
1515 * @return LY_ERR
1516 */
1517static LY_ERR
1518set_comp_canonize(struct lyxp_set *trg, const struct lyxp_set *src, const struct lyxp_set_node *xp_node)
1519{
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001520 const struct lysc_type *type = NULL;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001521 struct lyd_value val;
1522 struct ly_err_item *err = NULL;
1523 char *str, *ptr;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001524 LY_ERR rc;
1525
1526 /* is there anything to canonize even? */
1527 if ((src->type == LYXP_SET_NUMBER) || (src->type == LYXP_SET_STRING)) {
1528 /* do we have a type to use for canonization? */
1529 if ((xp_node->type == LYXP_NODE_ELEM) && (xp_node->node->schema->nodetype & LYD_NODE_TERM)) {
1530 type = ((struct lyd_node_term *)xp_node->node)->value.realtype;
1531 } else if (xp_node->type == LYXP_NODE_META) {
1532 type = ((struct lyd_meta *)xp_node->node)->value.realtype;
1533 }
1534 }
1535 if (!type) {
1536 goto fill;
1537 }
1538
1539 if (src->type == LYXP_SET_NUMBER) {
1540 /* canonize number */
1541 if (asprintf(&str, "%Lf", src->val.num) == -1) {
1542 LOGMEM(src->ctx);
1543 return LY_EMEM;
1544 }
1545 } else {
1546 /* canonize string */
1547 str = strdup(src->val.str);
1548 }
1549
1550 /* ignore errors, the value may not satisfy schema constraints */
Michal Vasko0fdb0d92020-11-06 17:25:50 +01001551 rc = type->plugin->store(src->ctx, type, str, strlen(str), LY_TYPE_STORE_DYNAMIC, src->format, src->prefix_data,
1552 LYD_HINT_DATA, xp_node->node->schema, &val, &err);
Michal Vasko4c7763f2020-07-27 17:40:37 +02001553 ly_err_free(err);
1554 if (rc) {
1555 /* invalid value */
1556 free(str);
1557 goto fill;
1558 }
1559
Michal Vasko4c7763f2020-07-27 17:40:37 +02001560 /* use the canonized value */
1561 set_init(trg, src);
1562 trg->type = src->type;
1563 if (src->type == LYXP_SET_NUMBER) {
Michal Vasko0fdb0d92020-11-06 17:25:50 +01001564 trg->val.num = strtold(val.canonical, &ptr);
Michal Vasko4c7763f2020-07-27 17:40:37 +02001565 LY_CHECK_ERR_RET(ptr[0], LOGINT(src->ctx), LY_EINT);
1566 } else {
Michal Vasko0fdb0d92020-11-06 17:25:50 +01001567 trg->val.str = strdup(val.canonical);
Michal Vasko4c7763f2020-07-27 17:40:37 +02001568 }
1569 type->plugin->free(src->ctx, &val);
1570 return LY_SUCCESS;
1571
1572fill:
1573 /* no canonization needed/possible */
1574 set_fill_set(trg, src);
1575 return LY_SUCCESS;
1576}
1577
Michal Vasko03ff5a72019-09-11 13:49:33 +02001578#ifndef NDEBUG
1579
1580/**
1581 * @brief Bubble sort @p set into XPath document order.
1582 * Context position aware. Unused in the 'Release' build target.
1583 *
1584 * @param[in] set Set to sort.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001585 * @return How many times the whole set was traversed - 1 (if set was sorted, returns 0).
1586 */
1587static int
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001588set_sort(struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001589{
1590 uint32_t i, j;
Radek Krejci1deb5be2020-08-26 16:43:36 +02001591 int ret = 0, cmp;
Radek Krejci857189e2020-09-01 13:26:36 +02001592 ly_bool inverted, change;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001593 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001594 struct lyxp_set_node item;
1595 struct lyxp_set_hash_node hnode;
1596 uint64_t hash;
1597
Michal Vasko3cf8fbf2020-05-27 15:21:21 +02001598 if ((set->type != LYXP_SET_NODE_SET) || (set->used < 2)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001599 return 0;
1600 }
1601
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001602 /* find first top-level node to be used as anchor for positions */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001603 for (root = set->cur_node; root->parent; root = (const struct lyd_node *)root->parent) {}
Michal Vaskod989ba02020-08-24 10:59:24 +02001604 for ( ; root->prev->next; root = root->prev) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001605
1606 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001607 if (set_assign_pos(set, root, set->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001608 return -1;
1609 }
1610
1611 LOGDBG(LY_LDGXPATH, "SORT BEGIN");
1612 print_set_debug(set);
1613
1614 for (i = 0; i < set->used; ++i) {
1615 inverted = 0;
1616 change = 0;
1617
1618 for (j = 1; j < set->used - i; ++j) {
1619 /* compare node positions */
1620 if (inverted) {
1621 cmp = set_sort_compare(&set->val.nodes[j], &set->val.nodes[j - 1]);
1622 } else {
1623 cmp = set_sort_compare(&set->val.nodes[j - 1], &set->val.nodes[j]);
1624 }
1625
1626 /* swap if needed */
1627 if ((inverted && (cmp < 0)) || (!inverted && (cmp > 0))) {
1628 change = 1;
1629
1630 item = set->val.nodes[j - 1];
1631 set->val.nodes[j - 1] = set->val.nodes[j];
1632 set->val.nodes[j] = item;
1633 } else {
1634 /* whether node_pos1 should be smaller than node_pos2 or the other way around */
1635 inverted = !inverted;
1636 }
1637 }
1638
1639 ++ret;
1640
1641 if (!change) {
1642 break;
1643 }
1644 }
1645
1646 LOGDBG(LY_LDGXPATH, "SORT END %d", ret);
1647 print_set_debug(set);
1648
1649 /* check node hashes */
1650 if (set->used >= LYD_HT_MIN_ITEMS) {
1651 assert(set->ht);
1652 for (i = 0; i < set->used; ++i) {
1653 hnode.node = set->val.nodes[i].node;
1654 hnode.type = set->val.nodes[i].type;
1655
1656 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
1657 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
1658 hash = dict_hash_multi(hash, NULL, 0);
1659
1660 assert(!lyht_find(set->ht, &hnode, hash, NULL));
1661 }
1662 }
1663
1664 return ret - 1;
1665}
1666
1667/**
1668 * @brief Remove duplicate entries in a sorted node set.
1669 *
1670 * @param[in] set Sorted set to check.
1671 * @return LY_ERR (LY_EEXIST if some duplicates are found)
1672 */
1673static LY_ERR
1674set_sorted_dup_node_clean(struct lyxp_set *set)
1675{
1676 uint32_t i = 0;
1677 LY_ERR ret = LY_SUCCESS;
1678
1679 if (set->used > 1) {
1680 while (i < set->used - 1) {
Michal Vasko69730152020-10-09 16:30:07 +02001681 if ((set->val.nodes[i].node == set->val.nodes[i + 1].node) &&
1682 (set->val.nodes[i].type == set->val.nodes[i + 1].type)) {
Michal Vasko2caefc12019-11-14 16:07:56 +01001683 set_remove_node_none(set, i + 1);
Michal Vasko57eab132019-09-24 11:46:26 +02001684 ret = LY_EEXIST;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001685 }
Michal Vasko57eab132019-09-24 11:46:26 +02001686 ++i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001687 }
1688 }
1689
Michal Vasko2caefc12019-11-14 16:07:56 +01001690 set_remove_nodes_none(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001691 return ret;
1692}
1693
1694#endif
1695
1696/**
1697 * @brief Merge 2 sorted sets into one.
1698 *
1699 * @param[in,out] trg Set to merge into. Duplicates are removed.
1700 * @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 +02001701 * @return LY_ERR
1702 */
1703static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001704set_sorted_merge(struct lyxp_set *trg, struct lyxp_set *src)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001705{
1706 uint32_t i, j, k, count, dup_count;
1707 int cmp;
1708 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001709
Michal Vaskod3678892020-05-21 10:06:58 +02001710 if ((trg->type != LYXP_SET_NODE_SET) || (src->type != LYXP_SET_NODE_SET)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001711 return LY_EINVAL;
1712 }
1713
Michal Vaskod3678892020-05-21 10:06:58 +02001714 if (!src->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001715 return LY_SUCCESS;
Michal Vaskod3678892020-05-21 10:06:58 +02001716 } else if (!trg->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001717 set_fill_set(trg, src);
Michal Vaskod3678892020-05-21 10:06:58 +02001718 lyxp_set_free_content(src);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001719 return LY_SUCCESS;
1720 }
1721
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001722 /* find first top-level node to be used as anchor for positions */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02001723 for (root = trg->cur_node; root->parent; root = (const struct lyd_node *)root->parent) {}
Michal Vaskod989ba02020-08-24 10:59:24 +02001724 for ( ; root->prev->next; root = root->prev) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001725
1726 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001727 if (set_assign_pos(trg, root, trg->root_type) || set_assign_pos(src, root, src->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001728 return LY_EINT;
1729 }
1730
1731#ifndef NDEBUG
1732 LOGDBG(LY_LDGXPATH, "MERGE target");
1733 print_set_debug(trg);
1734 LOGDBG(LY_LDGXPATH, "MERGE source");
1735 print_set_debug(src);
1736#endif
1737
1738 /* make memory for the merge (duplicates are not detected yet, so space
1739 * will likely be wasted on them, too bad) */
1740 if (trg->size - trg->used < src->used) {
1741 trg->size = trg->used + src->used;
1742
1743 trg->val.nodes = ly_realloc(trg->val.nodes, trg->size * sizeof *trg->val.nodes);
1744 LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx), LY_EMEM);
1745 }
1746
1747 i = 0;
1748 j = 0;
1749 count = 0;
1750 dup_count = 0;
1751 do {
1752 cmp = set_sort_compare(&src->val.nodes[i], &trg->val.nodes[j]);
1753 if (!cmp) {
1754 if (!count) {
1755 /* duplicate, just skip it */
1756 ++i;
1757 ++j;
1758 } else {
1759 /* we are copying something already, so let's copy the duplicate too,
1760 * we are hoping that afterwards there are some more nodes to
1761 * copy and this way we can copy them all together */
1762 ++count;
1763 ++dup_count;
1764 ++i;
1765 ++j;
1766 }
1767 } else if (cmp < 0) {
1768 /* inserting src node into trg, just remember it for now */
1769 ++count;
1770 ++i;
1771
1772 /* insert the hash now */
1773 set_insert_node_hash(trg, src->val.nodes[i - 1].node, src->val.nodes[i - 1].type);
1774 } else if (count) {
1775copy_nodes:
1776 /* time to actually copy the nodes, we have found the largest block of nodes */
1777 memmove(&trg->val.nodes[j + (count - dup_count)],
1778 &trg->val.nodes[j],
1779 (trg->used - j) * sizeof *trg->val.nodes);
1780 memcpy(&trg->val.nodes[j - dup_count], &src->val.nodes[i - count], count * sizeof *src->val.nodes);
1781
1782 trg->used += count - dup_count;
1783 /* do not change i, except the copying above, we are basically doing exactly what is in the else branch below */
1784 j += count - dup_count;
1785
1786 count = 0;
1787 dup_count = 0;
1788 } else {
1789 ++j;
1790 }
1791 } while ((i < src->used) && (j < trg->used));
1792
1793 if ((i < src->used) || count) {
1794 /* insert all the hashes first */
1795 for (k = i; k < src->used; ++k) {
1796 set_insert_node_hash(trg, src->val.nodes[k].node, src->val.nodes[k].type);
1797 }
1798
1799 /* loop ended, but we need to copy something at trg end */
1800 count += src->used - i;
1801 i = src->used;
1802 goto copy_nodes;
1803 }
1804
1805 /* we are inserting hashes before the actual node insert, which causes
1806 * situations when there were initially not enough items for a hash table,
1807 * but even after some were inserted, hash table was not created (during
1808 * insertion the number of items is not updated yet) */
1809 if (!trg->ht && (trg->used >= LYD_HT_MIN_ITEMS)) {
1810 set_insert_node_hash(trg, NULL, 0);
1811 }
1812
1813#ifndef NDEBUG
1814 LOGDBG(LY_LDGXPATH, "MERGE result");
1815 print_set_debug(trg);
1816#endif
1817
Michal Vaskod3678892020-05-21 10:06:58 +02001818 lyxp_set_free_content(src);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001819 return LY_SUCCESS;
1820}
1821
1822/*
1823 * (re)parse functions
1824 *
1825 * Parse functions parse the expression into
1826 * tokens (syntactic analysis).
1827 *
1828 * Reparse functions perform semantic analysis
1829 * (do not save the result, just a check) of
1830 * the expression and fill repeat indices.
1831 */
1832
Michal Vasko14676352020-05-29 11:35:55 +02001833LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02001834lyxp_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 +02001835{
Michal Vasko004d3152020-06-11 19:59:22 +02001836 if (exp->used == tok_idx) {
Michal Vasko14676352020-05-29 11:35:55 +02001837 if (ctx) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001838 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOF);
1839 }
Michal Vasko14676352020-05-29 11:35:55 +02001840 return LY_EINCOMPLETE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001841 }
1842
Michal Vasko004d3152020-06-11 19:59:22 +02001843 if (want_tok && (exp->tokens[tok_idx] != want_tok)) {
Michal Vasko14676352020-05-29 11:35:55 +02001844 if (ctx) {
Michal Vasko0b468e62020-10-19 09:33:04 +02001845 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK2, lyxp_print_token(exp->tokens[tok_idx]),
1846 &exp->expr[exp->tok_pos[tok_idx]], lyxp_print_token(want_tok));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001847 }
Michal Vasko14676352020-05-29 11:35:55 +02001848 return LY_ENOT;
1849 }
1850
1851 return LY_SUCCESS;
1852}
1853
Michal Vasko004d3152020-06-11 19:59:22 +02001854LY_ERR
1855lyxp_next_token(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_token want_tok)
1856{
1857 LY_CHECK_RET(lyxp_check_token(ctx, exp, *tok_idx, want_tok));
1858
1859 /* skip the token */
1860 ++(*tok_idx);
1861
1862 return LY_SUCCESS;
1863}
1864
Michal Vasko14676352020-05-29 11:35:55 +02001865/* just like lyxp_check_token() but tests for 2 tokens */
1866static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02001867exp_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 +02001868 enum lyxp_token want_tok2)
Michal Vasko14676352020-05-29 11:35:55 +02001869{
Michal Vasko004d3152020-06-11 19:59:22 +02001870 if (exp->used == tok_idx) {
Michal Vasko14676352020-05-29 11:35:55 +02001871 if (ctx) {
1872 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOF);
1873 }
1874 return LY_EINCOMPLETE;
1875 }
1876
Michal Vasko004d3152020-06-11 19:59:22 +02001877 if ((exp->tokens[tok_idx] != want_tok1) && (exp->tokens[tok_idx] != want_tok2)) {
Michal Vasko14676352020-05-29 11:35:55 +02001878 if (ctx) {
Michal Vasko0b468e62020-10-19 09:33:04 +02001879 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK, lyxp_print_token(exp->tokens[tok_idx]),
1880 &exp->expr[exp->tok_pos[tok_idx]]);
Michal Vasko14676352020-05-29 11:35:55 +02001881 }
1882 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001883 }
1884
1885 return LY_SUCCESS;
1886}
1887
1888/**
1889 * @brief Stack operation push on the repeat array.
1890 *
1891 * @param[in] exp Expression to use.
Michal Vasko004d3152020-06-11 19:59:22 +02001892 * @param[in] tok_idx Position in the expresion \p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001893 * @param[in] repeat_op_idx Index from \p exp of the operator token. This value is pushed.
1894 */
1895static void
Michal Vasko004d3152020-06-11 19:59:22 +02001896exp_repeat_push(struct lyxp_expr *exp, uint16_t tok_idx, uint16_t repeat_op_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001897{
1898 uint16_t i;
1899
Michal Vasko004d3152020-06-11 19:59:22 +02001900 if (exp->repeat[tok_idx]) {
Radek Krejci1e008d22020-08-17 11:37:37 +02001901 for (i = 0; exp->repeat[tok_idx][i]; ++i) {}
Michal Vasko004d3152020-06-11 19:59:22 +02001902 exp->repeat[tok_idx] = realloc(exp->repeat[tok_idx], (i + 2) * sizeof *exp->repeat[tok_idx]);
1903 LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), );
1904 exp->repeat[tok_idx][i] = repeat_op_idx;
1905 exp->repeat[tok_idx][i + 1] = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001906 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02001907 exp->repeat[tok_idx] = calloc(2, sizeof *exp->repeat[tok_idx]);
1908 LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), );
1909 exp->repeat[tok_idx][0] = repeat_op_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001910 }
1911}
1912
1913/**
1914 * @brief Reparse Predicate. Logs directly on error.
1915 *
1916 * [7] Predicate ::= '[' Expr ']'
1917 *
1918 * @param[in] ctx Context for logging.
1919 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02001920 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001921 * @return LY_ERR
1922 */
1923static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02001924reparse_predicate(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001925{
1926 LY_ERR rc;
1927
Michal Vasko004d3152020-06-11 19:59:22 +02001928 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001929 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02001930 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001931
Michal Vasko004d3152020-06-11 19:59:22 +02001932 rc = reparse_or_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001933 LY_CHECK_RET(rc);
1934
Michal Vasko004d3152020-06-11 19:59:22 +02001935 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001936 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02001937 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001938
1939 return LY_SUCCESS;
1940}
1941
1942/**
1943 * @brief Reparse RelativeLocationPath. Logs directly on error.
1944 *
1945 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
1946 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
1947 * [6] NodeTest ::= NameTest | NodeType '(' ')'
1948 *
1949 * @param[in] ctx Context for logging.
1950 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02001951 * @param[in] tok_idx Position in the expression \p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001952 * @return LY_ERR (LY_EINCOMPLETE on forward reference)
1953 */
1954static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02001955reparse_relative_location_path(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001956{
1957 LY_ERR rc;
1958
Michal Vasko004d3152020-06-11 19:59:22 +02001959 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001960 LY_CHECK_RET(rc);
1961
1962 goto step;
1963 do {
1964 /* '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02001965 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001966
Michal Vasko004d3152020-06-11 19:59:22 +02001967 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001968 LY_CHECK_RET(rc);
1969step:
1970 /* Step */
Michal Vasko004d3152020-06-11 19:59:22 +02001971 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001972 case LYXP_TOKEN_DOT:
Michal Vasko004d3152020-06-11 19:59:22 +02001973 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001974 break;
1975
1976 case LYXP_TOKEN_DDOT:
Michal Vasko004d3152020-06-11 19:59:22 +02001977 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001978 break;
1979
1980 case LYXP_TOKEN_AT:
Michal Vasko004d3152020-06-11 19:59:22 +02001981 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001982
Michal Vasko004d3152020-06-11 19:59:22 +02001983 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001984 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02001985 if ((exp->tokens[*tok_idx] != LYXP_TOKEN_NAMETEST) && (exp->tokens[*tok_idx] != LYXP_TOKEN_NODETYPE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001986 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK,
Michal Vasko69730152020-10-09 16:30:07 +02001987 lyxp_print_token(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001988 return LY_EVALID;
1989 }
Radek Krejci0f969882020-08-21 16:56:47 +02001990 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001991 case LYXP_TOKEN_NAMETEST:
Michal Vasko004d3152020-06-11 19:59:22 +02001992 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001993 goto reparse_predicate;
1994 break;
1995
1996 case LYXP_TOKEN_NODETYPE:
Michal Vasko004d3152020-06-11 19:59:22 +02001997 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001998
1999 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02002000 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002001 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002002 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002003
2004 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02002005 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002006 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002007 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002008
2009reparse_predicate:
2010 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002011 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
2012 rc = reparse_predicate(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002013 LY_CHECK_RET(rc);
2014 }
2015 break;
2016 default:
2017 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK,
Michal Vasko69730152020-10-09 16:30:07 +02002018 lyxp_print_token(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002019 return LY_EVALID;
2020 }
Michal Vasko004d3152020-06-11 19:59:22 +02002021 } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02002022
2023 return LY_SUCCESS;
2024}
2025
2026/**
2027 * @brief Reparse AbsoluteLocationPath. Logs directly on error.
2028 *
2029 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
2030 *
2031 * @param[in] ctx Context for logging.
2032 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002033 * @param[in] tok_idx Position in the expression \p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002034 * @return LY_ERR
2035 */
2036static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002037reparse_absolute_location_path(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002038{
2039 LY_ERR rc;
2040
Michal Vasko004d3152020-06-11 19:59:22 +02002041 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 +02002042
2043 /* '/' RelativeLocationPath? */
Michal Vasko004d3152020-06-11 19:59:22 +02002044 if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002045 /* '/' */
Michal Vasko004d3152020-06-11 19:59:22 +02002046 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002047
Michal Vasko004d3152020-06-11 19:59:22 +02002048 if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002049 return LY_SUCCESS;
2050 }
Michal Vasko004d3152020-06-11 19:59:22 +02002051 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002052 case LYXP_TOKEN_DOT:
2053 case LYXP_TOKEN_DDOT:
2054 case LYXP_TOKEN_AT:
2055 case LYXP_TOKEN_NAMETEST:
2056 case LYXP_TOKEN_NODETYPE:
Michal Vasko004d3152020-06-11 19:59:22 +02002057 rc = reparse_relative_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002058 LY_CHECK_RET(rc);
Radek Krejci0f969882020-08-21 16:56:47 +02002059 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02002060 default:
2061 break;
2062 }
2063
Michal Vasko03ff5a72019-09-11 13:49:33 +02002064 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02002065 /* '//' RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002066 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002067
Michal Vasko004d3152020-06-11 19:59:22 +02002068 rc = reparse_relative_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002069 LY_CHECK_RET(rc);
2070 }
2071
2072 return LY_SUCCESS;
2073}
2074
2075/**
2076 * @brief Reparse FunctionCall. Logs directly on error.
2077 *
2078 * [9] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
2079 *
2080 * @param[in] ctx Context for logging.
2081 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002082 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002083 * @return LY_ERR
2084 */
2085static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002086reparse_function_call(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002087{
Radek Krejci1deb5be2020-08-26 16:43:36 +02002088 int8_t min_arg_count = -1;
2089 uint32_t arg_count, max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002090 uint16_t func_tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002091 LY_ERR rc;
2092
Michal Vasko004d3152020-06-11 19:59:22 +02002093 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_FUNCNAME);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002094 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002095 func_tok_idx = *tok_idx;
2096 switch (exp->tok_len[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002097 case 3:
Michal Vasko004d3152020-06-11 19:59:22 +02002098 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002099 min_arg_count = 1;
2100 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002101 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002102 min_arg_count = 1;
2103 max_arg_count = 1;
2104 }
2105 break;
2106 case 4:
Michal Vasko004d3152020-06-11 19:59:22 +02002107 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002108 min_arg_count = 1;
2109 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002110 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002111 min_arg_count = 0;
2112 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002113 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002114 min_arg_count = 0;
2115 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002116 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002117 min_arg_count = 0;
2118 max_arg_count = 0;
2119 }
2120 break;
2121 case 5:
Michal Vasko004d3152020-06-11 19:59:22 +02002122 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002123 min_arg_count = 1;
2124 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002125 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002126 min_arg_count = 0;
2127 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002128 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002129 min_arg_count = 1;
2130 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002131 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002132 min_arg_count = 1;
2133 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002134 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002135 min_arg_count = 1;
2136 max_arg_count = 1;
2137 }
2138 break;
2139 case 6:
Michal Vasko004d3152020-06-11 19:59:22 +02002140 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002141 min_arg_count = 2;
Radek Krejci1deb5be2020-08-26 16:43:36 +02002142 max_arg_count = UINT32_MAX;
Michal Vasko004d3152020-06-11 19:59:22 +02002143 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002144 min_arg_count = 0;
2145 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002146 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002147 min_arg_count = 0;
2148 max_arg_count = 1;
2149 }
2150 break;
2151 case 7:
Michal Vasko004d3152020-06-11 19:59:22 +02002152 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002153 min_arg_count = 1;
2154 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002155 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) {
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]], "current", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002159 min_arg_count = 0;
2160 max_arg_count = 0;
2161 }
2162 break;
2163 case 8:
Michal Vasko004d3152020-06-11 19:59:22 +02002164 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002165 min_arg_count = 2;
2166 max_arg_count = 2;
Michal Vasko004d3152020-06-11 19:59:22 +02002167 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002168 min_arg_count = 0;
2169 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002170 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002171 min_arg_count = 2;
2172 max_arg_count = 2;
2173 }
2174 break;
2175 case 9:
Michal Vasko004d3152020-06-11 19:59:22 +02002176 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002177 min_arg_count = 2;
2178 max_arg_count = 3;
Michal Vasko004d3152020-06-11 19:59:22 +02002179 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002180 min_arg_count = 3;
2181 max_arg_count = 3;
2182 }
2183 break;
2184 case 10:
Michal Vasko004d3152020-06-11 19:59:22 +02002185 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002186 min_arg_count = 0;
2187 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002188 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002189 min_arg_count = 1;
2190 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002191 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002192 min_arg_count = 2;
2193 max_arg_count = 2;
2194 }
2195 break;
2196 case 11:
Michal Vasko004d3152020-06-11 19:59:22 +02002197 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002198 min_arg_count = 2;
2199 max_arg_count = 2;
2200 }
2201 break;
2202 case 12:
Michal Vasko004d3152020-06-11 19:59:22 +02002203 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002204 min_arg_count = 2;
2205 max_arg_count = 2;
2206 }
2207 break;
2208 case 13:
Michal Vasko004d3152020-06-11 19:59:22 +02002209 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002210 min_arg_count = 0;
2211 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002212 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002213 min_arg_count = 0;
2214 max_arg_count = 1;
2215 }
2216 break;
2217 case 15:
Michal Vasko004d3152020-06-11 19:59:22 +02002218 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002219 min_arg_count = 0;
2220 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002221 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002222 min_arg_count = 2;
2223 max_arg_count = 2;
2224 }
2225 break;
2226 case 16:
Michal Vasko004d3152020-06-11 19:59:22 +02002227 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002228 min_arg_count = 2;
2229 max_arg_count = 2;
2230 }
2231 break;
2232 case 20:
Michal Vasko004d3152020-06-11 19:59:22 +02002233 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002234 min_arg_count = 2;
2235 max_arg_count = 2;
2236 }
2237 break;
2238 }
2239 if (min_arg_count == -1) {
Michal Vasko004d3152020-06-11 19:59:22 +02002240 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INFUNC, exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002241 return LY_EINVAL;
2242 }
Michal Vasko004d3152020-06-11 19:59:22 +02002243 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002244
2245 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02002246 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002247 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002248 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002249
2250 /* ( Expr ( ',' Expr )* )? */
2251 arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002252 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002253 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002254 if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002255 ++arg_count;
Michal Vasko004d3152020-06-11 19:59:22 +02002256 rc = reparse_or_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002257 LY_CHECK_RET(rc);
2258 }
Michal Vasko004d3152020-06-11 19:59:22 +02002259 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) {
2260 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002261
2262 ++arg_count;
Michal Vasko004d3152020-06-11 19:59:22 +02002263 rc = reparse_or_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002264 LY_CHECK_RET(rc);
2265 }
2266
2267 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02002268 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002269 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002270 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002271
Radek Krejci857189e2020-09-01 13:26:36 +02002272 if ((arg_count < (uint32_t)min_arg_count) || (arg_count > max_arg_count)) {
Michal Vasko004d3152020-06-11 19:59:22 +02002273 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INARGCOUNT, arg_count, exp->tok_len[func_tok_idx],
Michal Vasko69730152020-10-09 16:30:07 +02002274 &exp->expr[exp->tok_pos[func_tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002275 return LY_EVALID;
2276 }
2277
2278 return LY_SUCCESS;
2279}
2280
2281/**
2282 * @brief Reparse PathExpr. Logs directly on error.
2283 *
2284 * [10] PathExpr ::= LocationPath | PrimaryExpr Predicate*
2285 * | PrimaryExpr Predicate* '/' RelativeLocationPath
2286 * | PrimaryExpr Predicate* '//' RelativeLocationPath
2287 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
2288 * [8] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall
2289 *
2290 * @param[in] ctx Context for logging.
2291 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002292 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002293 * @return LY_ERR
2294 */
2295static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002296reparse_path_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002297{
2298 LY_ERR rc;
2299
Michal Vasko004d3152020-06-11 19:59:22 +02002300 if (lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko14676352020-05-29 11:35:55 +02002301 return LY_EVALID;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002302 }
2303
Michal Vasko004d3152020-06-11 19:59:22 +02002304 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002305 case LYXP_TOKEN_PAR1:
2306 /* '(' Expr ')' Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002307 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002308
Michal Vasko004d3152020-06-11 19:59:22 +02002309 rc = reparse_or_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002310 LY_CHECK_RET(rc);
2311
Michal Vasko004d3152020-06-11 19:59:22 +02002312 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002313 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002314 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002315 goto predicate;
2316 break;
2317 case LYXP_TOKEN_DOT:
2318 case LYXP_TOKEN_DDOT:
2319 case LYXP_TOKEN_AT:
2320 case LYXP_TOKEN_NAMETEST:
2321 case LYXP_TOKEN_NODETYPE:
2322 /* RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002323 rc = reparse_relative_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002324 LY_CHECK_RET(rc);
2325 break;
2326 case LYXP_TOKEN_FUNCNAME:
2327 /* FunctionCall */
Michal Vasko004d3152020-06-11 19:59:22 +02002328 rc = reparse_function_call(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002329 LY_CHECK_RET(rc);
2330 goto predicate;
2331 break;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002332 case LYXP_TOKEN_OPER_PATH:
2333 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02002334 /* AbsoluteLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002335 rc = reparse_absolute_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002336 LY_CHECK_RET(rc);
2337 break;
2338 case LYXP_TOKEN_LITERAL:
2339 /* Literal */
Michal Vasko004d3152020-06-11 19:59:22 +02002340 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002341 goto predicate;
2342 break;
2343 case LYXP_TOKEN_NUMBER:
2344 /* Number */
Michal Vasko004d3152020-06-11 19:59:22 +02002345 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002346 goto predicate;
2347 break;
2348 default:
2349 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK,
Michal Vasko69730152020-10-09 16:30:07 +02002350 lyxp_print_token(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002351 return LY_EVALID;
2352 }
2353
2354 return LY_SUCCESS;
2355
2356predicate:
2357 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002358 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
2359 rc = reparse_predicate(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002360 LY_CHECK_RET(rc);
2361 }
2362
2363 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002364 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002365
2366 /* '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02002367 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002368
Michal Vasko004d3152020-06-11 19:59:22 +02002369 rc = reparse_relative_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002370 LY_CHECK_RET(rc);
2371 }
2372
2373 return LY_SUCCESS;
2374}
2375
2376/**
2377 * @brief Reparse UnaryExpr. Logs directly on error.
2378 *
2379 * [17] UnaryExpr ::= UnionExpr | '-' UnaryExpr
2380 * [18] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
2381 *
2382 * @param[in] ctx Context for logging.
2383 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002384 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002385 * @return LY_ERR
2386 */
2387static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002388reparse_unary_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002389{
2390 uint16_t prev_exp;
2391 LY_ERR rc;
2392
2393 /* ('-')* */
Michal Vasko004d3152020-06-11 19:59:22 +02002394 prev_exp = *tok_idx;
Michal Vasko69730152020-10-09 16:30:07 +02002395 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2396 (exp->expr[exp->tok_pos[*tok_idx]] == '-')) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002397 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNARY);
Michal Vasko004d3152020-06-11 19:59:22 +02002398 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002399 }
2400
2401 /* PathExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002402 prev_exp = *tok_idx;
2403 rc = reparse_path_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002404 LY_CHECK_RET(rc);
2405
2406 /* ('|' PathExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002407 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_UNI)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002408 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNION);
Michal Vasko004d3152020-06-11 19:59:22 +02002409 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002410
Michal Vasko004d3152020-06-11 19:59:22 +02002411 rc = reparse_path_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002412 LY_CHECK_RET(rc);
2413 }
2414
2415 return LY_SUCCESS;
2416}
2417
2418/**
2419 * @brief Reparse AdditiveExpr. Logs directly on error.
2420 *
2421 * [15] AdditiveExpr ::= MultiplicativeExpr
2422 * | AdditiveExpr '+' MultiplicativeExpr
2423 * | AdditiveExpr '-' MultiplicativeExpr
2424 * [16] MultiplicativeExpr ::= UnaryExpr
2425 * | MultiplicativeExpr '*' UnaryExpr
2426 * | MultiplicativeExpr 'div' UnaryExpr
2427 * | MultiplicativeExpr 'mod' UnaryExpr
2428 *
2429 * @param[in] ctx Context for logging.
2430 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002431 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002432 * @return LY_ERR
2433 */
2434static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002435reparse_additive_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002436{
2437 uint16_t prev_add_exp, prev_mul_exp;
2438 LY_ERR rc;
2439
Michal Vasko004d3152020-06-11 19:59:22 +02002440 prev_add_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002441 goto reparse_multiplicative_expr;
2442
2443 /* ('+' / '-' MultiplicativeExpr)* */
Michal Vasko69730152020-10-09 16:30:07 +02002444 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2445 ((exp->expr[exp->tok_pos[*tok_idx]] == '+') || (exp->expr[exp->tok_pos[*tok_idx]] == '-'))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002446 exp_repeat_push(exp, prev_add_exp, LYXP_EXPR_ADDITIVE);
Michal Vasko004d3152020-06-11 19:59:22 +02002447 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002448
2449reparse_multiplicative_expr:
2450 /* UnaryExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002451 prev_mul_exp = *tok_idx;
2452 rc = reparse_unary_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002453 LY_CHECK_RET(rc);
2454
2455 /* ('*' / 'div' / 'mod' UnaryExpr)* */
Michal Vasko69730152020-10-09 16:30:07 +02002456 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2457 ((exp->expr[exp->tok_pos[*tok_idx]] == '*') || (exp->tok_len[*tok_idx] == 3))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002458 exp_repeat_push(exp, prev_mul_exp, LYXP_EXPR_MULTIPLICATIVE);
Michal Vasko004d3152020-06-11 19:59:22 +02002459 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002460
Michal Vasko004d3152020-06-11 19:59:22 +02002461 rc = reparse_unary_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002462 LY_CHECK_RET(rc);
2463 }
2464 }
2465
2466 return LY_SUCCESS;
2467}
2468
2469/**
2470 * @brief Reparse EqualityExpr. Logs directly on error.
2471 *
2472 * [13] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
2473 * | EqualityExpr '!=' RelationalExpr
2474 * [14] RelationalExpr ::= AdditiveExpr
2475 * | RelationalExpr '<' AdditiveExpr
2476 * | RelationalExpr '>' AdditiveExpr
2477 * | RelationalExpr '<=' AdditiveExpr
2478 * | RelationalExpr '>=' AdditiveExpr
2479 *
2480 * @param[in] ctx Context for logging.
2481 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002482 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002483 * @return LY_ERR
2484 */
2485static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002486reparse_equality_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002487{
2488 uint16_t prev_eq_exp, prev_rel_exp;
2489 LY_ERR rc;
2490
Michal Vasko004d3152020-06-11 19:59:22 +02002491 prev_eq_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002492 goto reparse_additive_expr;
2493
2494 /* ('=' / '!=' RelationalExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002495 while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_EQUAL, LYXP_TOKEN_OPER_NEQUAL)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002496 exp_repeat_push(exp, prev_eq_exp, LYXP_EXPR_EQUALITY);
Michal Vasko004d3152020-06-11 19:59:22 +02002497 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002498
2499reparse_additive_expr:
2500 /* AdditiveExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002501 prev_rel_exp = *tok_idx;
2502 rc = reparse_additive_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002503 LY_CHECK_RET(rc);
2504
2505 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002506 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_COMP)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002507 exp_repeat_push(exp, prev_rel_exp, LYXP_EXPR_RELATIONAL);
Michal Vasko004d3152020-06-11 19:59:22 +02002508 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002509
Michal Vasko004d3152020-06-11 19:59:22 +02002510 rc = reparse_additive_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002511 LY_CHECK_RET(rc);
2512 }
2513 }
2514
2515 return LY_SUCCESS;
2516}
2517
2518/**
2519 * @brief Reparse OrExpr. Logs directly on error.
2520 *
2521 * [11] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
2522 * [12] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
2523 *
2524 * @param[in] ctx Context for logging.
2525 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002526 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002527 * @return LY_ERR
2528 */
2529static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002530reparse_or_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002531{
2532 uint16_t prev_or_exp, prev_and_exp;
2533 LY_ERR rc;
2534
Michal Vasko004d3152020-06-11 19:59:22 +02002535 prev_or_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002536 goto reparse_equality_expr;
2537
2538 /* ('or' AndExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002539 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 +02002540 exp_repeat_push(exp, prev_or_exp, LYXP_EXPR_OR);
Michal Vasko004d3152020-06-11 19:59:22 +02002541 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002542
2543reparse_equality_expr:
2544 /* EqualityExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002545 prev_and_exp = *tok_idx;
2546 rc = reparse_equality_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002547 LY_CHECK_RET(rc);
2548
2549 /* ('and' EqualityExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002550 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 +02002551 exp_repeat_push(exp, prev_and_exp, LYXP_EXPR_AND);
Michal Vasko004d3152020-06-11 19:59:22 +02002552 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002553
Michal Vasko004d3152020-06-11 19:59:22 +02002554 rc = reparse_equality_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002555 LY_CHECK_RET(rc);
2556 }
2557 }
2558
2559 return LY_SUCCESS;
2560}
Radek Krejcib1646a92018-11-02 16:08:26 +01002561
2562/**
2563 * @brief Parse NCName.
2564 *
2565 * @param[in] ncname Name to parse.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002566 * @return Length of @p ncname valid bytes.
Radek Krejcib1646a92018-11-02 16:08:26 +01002567 */
Radek Krejcid4270262019-01-07 15:07:25 +01002568static long int
Radek Krejcib1646a92018-11-02 16:08:26 +01002569parse_ncname(const char *ncname)
2570{
Radek Krejci1deb5be2020-08-26 16:43:36 +02002571 uint32_t uc;
Radek Krejcid4270262019-01-07 15:07:25 +01002572 size_t size;
2573 long int len = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002574
2575 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), 0);
2576 if (!is_xmlqnamestartchar(uc) || (uc == ':')) {
2577 return len;
2578 }
2579
2580 do {
2581 len += size;
Radek Krejci9a564c92019-01-07 14:53:57 +01002582 if (!*ncname) {
2583 break;
2584 }
Radek Krejcid4270262019-01-07 15:07:25 +01002585 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), -len);
Radek Krejcib1646a92018-11-02 16:08:26 +01002586 } while (is_xmlqnamechar(uc) && (uc != ':'));
2587
2588 return len;
2589}
2590
2591/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02002592 * @brief Add @p token into the expression @p exp.
Radek Krejcib1646a92018-11-02 16:08:26 +01002593 *
Michal Vasko03ff5a72019-09-11 13:49:33 +02002594 * @param[in] ctx Context for logging.
Radek Krejcib1646a92018-11-02 16:08:26 +01002595 * @param[in] exp Expression to use.
2596 * @param[in] token Token to add.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002597 * @param[in] tok_pos Token position in the XPath expression.
Radek Krejcib1646a92018-11-02 16:08:26 +01002598 * @param[in] tok_len Token length in the XPath expression.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002599 * @return LY_ERR
Radek Krejcib1646a92018-11-02 16:08:26 +01002600 */
2601static LY_ERR
Michal Vasko14676352020-05-29 11:35:55 +02002602exp_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 +01002603{
2604 uint32_t prev;
2605
2606 if (exp->used == exp->size) {
2607 prev = exp->size;
2608 exp->size += LYXP_EXPR_SIZE_STEP;
2609 if (prev > exp->size) {
2610 LOGINT(ctx);
2611 return LY_EINT;
2612 }
2613
2614 exp->tokens = ly_realloc(exp->tokens, exp->size * sizeof *exp->tokens);
2615 LY_CHECK_ERR_RET(!exp->tokens, LOGMEM(ctx), LY_EMEM);
2616 exp->tok_pos = ly_realloc(exp->tok_pos, exp->size * sizeof *exp->tok_pos);
2617 LY_CHECK_ERR_RET(!exp->tok_pos, LOGMEM(ctx), LY_EMEM);
2618 exp->tok_len = ly_realloc(exp->tok_len, exp->size * sizeof *exp->tok_len);
2619 LY_CHECK_ERR_RET(!exp->tok_len, LOGMEM(ctx), LY_EMEM);
2620 }
2621
2622 exp->tokens[exp->used] = token;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002623 exp->tok_pos[exp->used] = tok_pos;
Radek Krejcib1646a92018-11-02 16:08:26 +01002624 exp->tok_len[exp->used] = tok_len;
2625 ++exp->used;
2626 return LY_SUCCESS;
2627}
2628
2629void
Michal Vasko14676352020-05-29 11:35:55 +02002630lyxp_expr_free(const struct ly_ctx *ctx, struct lyxp_expr *expr)
Radek Krejcib1646a92018-11-02 16:08:26 +01002631{
2632 uint16_t i;
2633
2634 if (!expr) {
2635 return;
2636 }
2637
2638 lydict_remove(ctx, expr->expr);
2639 free(expr->tokens);
2640 free(expr->tok_pos);
2641 free(expr->tok_len);
2642 if (expr->repeat) {
2643 for (i = 0; i < expr->used; ++i) {
2644 free(expr->repeat[i]);
2645 }
2646 }
2647 free(expr->repeat);
2648 free(expr);
2649}
2650
Radek Krejcif03a9e22020-09-18 20:09:31 +02002651LY_ERR
2652lyxp_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 +01002653{
Radek Krejcif03a9e22020-09-18 20:09:31 +02002654 LY_ERR ret = LY_SUCCESS;
2655 struct lyxp_expr *expr;
Radek Krejcid4270262019-01-07 15:07:25 +01002656 size_t parsed = 0, tok_len;
Radek Krejcib1646a92018-11-02 16:08:26 +01002657 enum lyxp_token tok_type;
Radek Krejci857189e2020-09-01 13:26:36 +02002658 ly_bool prev_function_check = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002659 uint16_t tok_idx = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002660
Radek Krejcif03a9e22020-09-18 20:09:31 +02002661 assert(expr_p);
2662
2663 if (!expr_str[0]) {
Michal Vasko004d3152020-06-11 19:59:22 +02002664 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOF);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002665 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02002666 }
2667
2668 if (!expr_len) {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002669 expr_len = strlen(expr_str);
Michal Vasko004d3152020-06-11 19:59:22 +02002670 }
2671 if (expr_len > UINT16_MAX) {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002672 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "XPath expression cannot be longer than %ud characters.", UINT16_MAX);
2673 return LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01002674 }
2675
2676 /* init lyxp_expr structure */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002677 expr = calloc(1, sizeof *expr);
2678 LY_CHECK_ERR_GOTO(!expr, LOGMEM(ctx); ret = LY_EMEM, error);
2679 LY_CHECK_GOTO(ret = lydict_insert(ctx, expr_str, expr_len, &expr->expr), error);
2680 expr->used = 0;
2681 expr->size = LYXP_EXPR_SIZE_START;
2682 expr->tokens = malloc(expr->size * sizeof *expr->tokens);
2683 LY_CHECK_ERR_GOTO(!expr->tokens, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002684
Radek Krejcif03a9e22020-09-18 20:09:31 +02002685 expr->tok_pos = malloc(expr->size * sizeof *expr->tok_pos);
2686 LY_CHECK_ERR_GOTO(!expr->tok_pos, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002687
Radek Krejcif03a9e22020-09-18 20:09:31 +02002688 expr->tok_len = malloc(expr->size * sizeof *expr->tok_len);
2689 LY_CHECK_ERR_GOTO(!expr->tok_len, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002690
Michal Vasko004d3152020-06-11 19:59:22 +02002691 /* make expr 0-terminated */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002692 expr_str = expr->expr;
Michal Vasko004d3152020-06-11 19:59:22 +02002693
Radek Krejcif03a9e22020-09-18 20:09:31 +02002694 while (is_xmlws(expr_str[parsed])) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002695 ++parsed;
2696 }
2697
2698 do {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002699 if (expr_str[parsed] == '(') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002700
2701 /* '(' */
2702 tok_len = 1;
2703 tok_type = LYXP_TOKEN_PAR1;
2704
Radek Krejcif03a9e22020-09-18 20:09:31 +02002705 if (prev_function_check && expr->used && (expr->tokens[expr->used - 1] == LYXP_TOKEN_NAMETEST)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002706 /* it is a NodeType/FunctionName after all */
Michal Vasko69730152020-10-09 16:30:07 +02002707 if (((expr->tok_len[expr->used - 1] == 4) &&
2708 (!strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "node", 4) ||
2709 !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "text", 4))) ||
2710 ((expr->tok_len[expr->used - 1] == 7) &&
2711 !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "comment", 7))) {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002712 expr->tokens[expr->used - 1] = LYXP_TOKEN_NODETYPE;
Radek Krejcib1646a92018-11-02 16:08:26 +01002713 } else {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002714 expr->tokens[expr->used - 1] = LYXP_TOKEN_FUNCNAME;
Radek Krejcib1646a92018-11-02 16:08:26 +01002715 }
2716 prev_function_check = 0;
2717 }
2718
Radek Krejcif03a9e22020-09-18 20:09:31 +02002719 } else if (expr_str[parsed] == ')') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002720
2721 /* ')' */
2722 tok_len = 1;
2723 tok_type = LYXP_TOKEN_PAR2;
2724
Radek Krejcif03a9e22020-09-18 20:09:31 +02002725 } else if (expr_str[parsed] == '[') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002726
2727 /* '[' */
2728 tok_len = 1;
2729 tok_type = LYXP_TOKEN_BRACK1;
2730
Radek Krejcif03a9e22020-09-18 20:09:31 +02002731 } else if (expr_str[parsed] == ']') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002732
2733 /* ']' */
2734 tok_len = 1;
2735 tok_type = LYXP_TOKEN_BRACK2;
2736
Radek Krejcif03a9e22020-09-18 20:09:31 +02002737 } else if (!strncmp(&expr_str[parsed], "..", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002738
2739 /* '..' */
2740 tok_len = 2;
2741 tok_type = LYXP_TOKEN_DDOT;
2742
Radek Krejcif03a9e22020-09-18 20:09:31 +02002743 } else if ((expr_str[parsed] == '.') && (!isdigit(expr_str[parsed + 1]))) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002744
2745 /* '.' */
2746 tok_len = 1;
2747 tok_type = LYXP_TOKEN_DOT;
2748
Radek Krejcif03a9e22020-09-18 20:09:31 +02002749 } else if (expr_str[parsed] == '@') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002750
2751 /* '@' */
2752 tok_len = 1;
2753 tok_type = LYXP_TOKEN_AT;
2754
Radek Krejcif03a9e22020-09-18 20:09:31 +02002755 } else if (expr_str[parsed] == ',') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002756
2757 /* ',' */
2758 tok_len = 1;
2759 tok_type = LYXP_TOKEN_COMMA;
2760
Radek Krejcif03a9e22020-09-18 20:09:31 +02002761 } else if (expr_str[parsed] == '\'') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002762
2763 /* Literal with ' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002764 for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\''); ++tok_len) {}
2765 LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0',
Michal Vasko69730152020-10-09 16:30:07 +02002766 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOE, expr_str[parsed], &expr_str[parsed]); ret = LY_EVALID,
2767 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002768 ++tok_len;
2769 tok_type = LYXP_TOKEN_LITERAL;
2770
Radek Krejcif03a9e22020-09-18 20:09:31 +02002771 } else if (expr_str[parsed] == '\"') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002772
2773 /* Literal with " */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002774 for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\"'); ++tok_len) {}
2775 LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0',
Michal Vasko69730152020-10-09 16:30:07 +02002776 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOE, expr_str[parsed], &expr_str[parsed]); ret = LY_EVALID,
2777 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002778 ++tok_len;
2779 tok_type = LYXP_TOKEN_LITERAL;
2780
Radek Krejcif03a9e22020-09-18 20:09:31 +02002781 } else if ((expr_str[parsed] == '.') || (isdigit(expr_str[parsed]))) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002782
2783 /* Number */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002784 for (tok_len = 0; isdigit(expr_str[parsed + tok_len]); ++tok_len) {}
2785 if (expr_str[parsed + tok_len] == '.') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002786 ++tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02002787 for ( ; isdigit(expr_str[parsed + tok_len]); ++tok_len) {}
Radek Krejcib1646a92018-11-02 16:08:26 +01002788 }
2789 tok_type = LYXP_TOKEN_NUMBER;
2790
Radek Krejcif03a9e22020-09-18 20:09:31 +02002791 } else if (expr_str[parsed] == '/') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002792
2793 /* Operator '/', '//' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002794 if (!strncmp(&expr_str[parsed], "//", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002795 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002796 tok_type = LYXP_TOKEN_OPER_RPATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002797 } else {
2798 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002799 tok_type = LYXP_TOKEN_OPER_PATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002800 }
Radek Krejcib1646a92018-11-02 16:08:26 +01002801
Radek Krejcif03a9e22020-09-18 20:09:31 +02002802 } else if (!strncmp(&expr_str[parsed], "!=", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002803
Michal Vasko3e48bf32020-06-01 08:39:07 +02002804 /* Operator '!=' */
Radek Krejcib1646a92018-11-02 16:08:26 +01002805 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002806 tok_type = LYXP_TOKEN_OPER_NEQUAL;
2807
Radek Krejcif03a9e22020-09-18 20:09:31 +02002808 } else if (!strncmp(&expr_str[parsed], "<=", 2) || !strncmp(&expr_str[parsed], ">=", 2)) {
Michal Vasko3e48bf32020-06-01 08:39:07 +02002809
2810 /* Operator '<=', '>=' */
2811 tok_len = 2;
2812 tok_type = LYXP_TOKEN_OPER_COMP;
Radek Krejcib1646a92018-11-02 16:08:26 +01002813
Radek Krejcif03a9e22020-09-18 20:09:31 +02002814 } else if (expr_str[parsed] == '|') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002815
2816 /* Operator '|' */
2817 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002818 tok_type = LYXP_TOKEN_OPER_UNI;
Radek Krejcib1646a92018-11-02 16:08:26 +01002819
Radek Krejcif03a9e22020-09-18 20:09:31 +02002820 } else if ((expr_str[parsed] == '+') || (expr_str[parsed] == '-')) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002821
2822 /* Operator '+', '-' */
2823 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002824 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002825
Radek Krejcif03a9e22020-09-18 20:09:31 +02002826 } else if (expr_str[parsed] == '=') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002827
Michal Vasko3e48bf32020-06-01 08:39:07 +02002828 /* Operator '=' */
Radek Krejcib1646a92018-11-02 16:08:26 +01002829 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002830 tok_type = LYXP_TOKEN_OPER_EQUAL;
2831
Radek Krejcif03a9e22020-09-18 20:09:31 +02002832 } else if ((expr_str[parsed] == '<') || (expr_str[parsed] == '>')) {
Michal Vasko3e48bf32020-06-01 08:39:07 +02002833
2834 /* Operator '<', '>' */
2835 tok_len = 1;
2836 tok_type = LYXP_TOKEN_OPER_COMP;
Radek Krejcib1646a92018-11-02 16:08:26 +01002837
Michal Vasko69730152020-10-09 16:30:07 +02002838 } else if (expr->used && (expr->tokens[expr->used - 1] != LYXP_TOKEN_AT) &&
2839 (expr->tokens[expr->used - 1] != LYXP_TOKEN_PAR1) &&
2840 (expr->tokens[expr->used - 1] != LYXP_TOKEN_BRACK1) &&
2841 (expr->tokens[expr->used - 1] != LYXP_TOKEN_COMMA) &&
2842 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_LOG) &&
2843 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_EQUAL) &&
2844 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_NEQUAL) &&
2845 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_COMP) &&
2846 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_MATH) &&
2847 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_UNI) &&
2848 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_PATH) &&
2849 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_RPATH)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002850
2851 /* Operator '*', 'or', 'and', 'mod', or 'div' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002852 if (expr_str[parsed] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002853 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002854 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002855
Radek Krejcif03a9e22020-09-18 20:09:31 +02002856 } else if (!strncmp(&expr_str[parsed], "or", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002857 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002858 tok_type = LYXP_TOKEN_OPER_LOG;
Radek Krejcib1646a92018-11-02 16:08:26 +01002859
Radek Krejcif03a9e22020-09-18 20:09:31 +02002860 } else if (!strncmp(&expr_str[parsed], "and", 3)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002861 tok_len = 3;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002862 tok_type = LYXP_TOKEN_OPER_LOG;
Radek Krejcib1646a92018-11-02 16:08:26 +01002863
Radek Krejcif03a9e22020-09-18 20:09:31 +02002864 } else if (!strncmp(&expr_str[parsed], "mod", 3) || !strncmp(&expr_str[parsed], "div", 3)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002865 tok_len = 3;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002866 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002867
2868 } else if (prev_function_check) {
Michal Vasko53078572019-05-24 08:50:15 +02002869 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "Invalid character 0x%x ('%c'), perhaps \"%.*s\" is supposed to be a function call.",
Michal Vasko69730152020-10-09 16:30:07 +02002870 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 +02002871 ret = LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01002872 goto error;
2873 } else {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002874 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INEXPR, parsed + 1, expr_str);
2875 ret = LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01002876 goto error;
2877 }
Radek Krejcif03a9e22020-09-18 20:09:31 +02002878 } else if (expr_str[parsed] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002879
2880 /* NameTest '*' */
2881 tok_len = 1;
2882 tok_type = LYXP_TOKEN_NAMETEST;
2883
2884 } else {
2885
2886 /* NameTest (NCName ':' '*' | QName) or NodeType/FunctionName */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002887 long int ncname_len = parse_ncname(&expr_str[parsed]);
2888 LY_CHECK_ERR_GOTO(ncname_len < 0,
Michal Vasko69730152020-10-09 16:30:07 +02002889 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INEXPR, parsed - ncname_len + 1, expr_str); ret = LY_EVALID,
2890 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002891 tok_len = ncname_len;
2892
Radek Krejcif03a9e22020-09-18 20:09:31 +02002893 if (expr_str[parsed + tok_len] == ':') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002894 ++tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02002895 if (expr_str[parsed + tok_len] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002896 ++tok_len;
2897 } else {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002898 ncname_len = parse_ncname(&expr_str[parsed + tok_len]);
2899 LY_CHECK_ERR_GOTO(ncname_len < 0,
Michal Vasko69730152020-10-09 16:30:07 +02002900 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INEXPR, parsed - ncname_len + 1, expr_str); ret = LY_EVALID,
2901 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002902 tok_len += ncname_len;
2903 }
2904 /* remove old flag to prevent ambiguities */
2905 prev_function_check = 0;
2906 tok_type = LYXP_TOKEN_NAMETEST;
2907 } else {
2908 /* there is no prefix so it can still be NodeType/FunctionName, we can't finally decide now */
2909 prev_function_check = 1;
2910 tok_type = LYXP_TOKEN_NAMETEST;
2911 }
2912 }
2913
2914 /* store the token, move on to the next one */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002915 LY_CHECK_GOTO(ret = exp_add_token(ctx, expr, tok_type, parsed, tok_len), error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002916 parsed += tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02002917 while (is_xmlws(expr_str[parsed])) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002918 ++parsed;
2919 }
2920
Radek Krejcif03a9e22020-09-18 20:09:31 +02002921 } while (expr_str[parsed]);
Radek Krejcib1646a92018-11-02 16:08:26 +01002922
Michal Vasko004d3152020-06-11 19:59:22 +02002923 if (reparse) {
2924 /* prealloc repeat */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002925 expr->repeat = calloc(expr->size, sizeof *expr->repeat);
2926 LY_CHECK_ERR_GOTO(!expr->repeat, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002927
Michal Vasko004d3152020-06-11 19:59:22 +02002928 /* fill repeat */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002929 LY_CHECK_ERR_GOTO(reparse_or_expr(ctx, expr, &tok_idx), ret = LY_EVALID, error);
2930 if (expr->used > tok_idx) {
Michal Vasko004d3152020-06-11 19:59:22 +02002931 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "Unparsed characters \"%s\" left at the end of an XPath expression.",
Michal Vasko69730152020-10-09 16:30:07 +02002932 &expr->expr[expr->tok_pos[tok_idx]]);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002933 ret = LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02002934 goto error;
2935 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02002936 }
2937
Radek Krejcif03a9e22020-09-18 20:09:31 +02002938 print_expr_struct_debug(expr);
2939 *expr_p = expr;
2940 return LY_SUCCESS;
Radek Krejcib1646a92018-11-02 16:08:26 +01002941
2942error:
Radek Krejcif03a9e22020-09-18 20:09:31 +02002943 lyxp_expr_free(ctx, expr);
2944 return ret;
Radek Krejcib1646a92018-11-02 16:08:26 +01002945}
2946
Michal Vasko1734be92020-09-22 08:55:10 +02002947LY_ERR
2948lyxp_expr_dup(const struct ly_ctx *ctx, const struct lyxp_expr *exp, struct lyxp_expr **dup_p)
Michal Vasko004d3152020-06-11 19:59:22 +02002949{
Michal Vasko1734be92020-09-22 08:55:10 +02002950 LY_ERR ret = LY_SUCCESS;
2951 struct lyxp_expr *dup = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02002952 uint32_t i, j;
2953
Michal Vasko7f45cf22020-10-01 12:49:44 +02002954 if (!exp) {
2955 goto cleanup;
2956 }
2957
Michal Vasko004d3152020-06-11 19:59:22 +02002958 dup = calloc(1, sizeof *dup);
Michal Vasko1734be92020-09-22 08:55:10 +02002959 LY_CHECK_ERR_GOTO(!dup, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02002960
2961 dup->tokens = malloc(exp->used * sizeof *dup->tokens);
Michal Vasko1734be92020-09-22 08:55:10 +02002962 LY_CHECK_ERR_GOTO(!dup->tokens, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02002963 memcpy(dup->tokens, exp->tokens, exp->used * sizeof *dup->tokens);
2964
2965 dup->tok_pos = malloc(exp->used * sizeof *dup->tok_pos);
Michal Vasko1734be92020-09-22 08:55:10 +02002966 LY_CHECK_ERR_GOTO(!dup->tok_pos, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02002967 memcpy(dup->tok_pos, exp->tok_pos, exp->used * sizeof *dup->tok_pos);
2968
2969 dup->tok_len = malloc(exp->used * sizeof *dup->tok_len);
Michal Vasko1734be92020-09-22 08:55:10 +02002970 LY_CHECK_ERR_GOTO(!dup->tok_len, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02002971 memcpy(dup->tok_len, exp->tok_len, exp->used * sizeof *dup->tok_len);
2972
2973 dup->repeat = malloc(exp->used * sizeof *dup->repeat);
Michal Vasko1734be92020-09-22 08:55:10 +02002974 LY_CHECK_ERR_GOTO(!dup->repeat, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02002975 for (i = 0; i < exp->used; ++i) {
2976 if (!exp->repeat[i]) {
2977 dup->repeat[i] = NULL;
2978 } else {
Radek Krejci1e008d22020-08-17 11:37:37 +02002979 for (j = 0; exp->repeat[i][j]; ++j) {}
Michal Vasko004d3152020-06-11 19:59:22 +02002980 /* the ending 0 as well */
2981 ++j;
2982
Michal Vasko99c71642020-07-03 13:33:36 +02002983 dup->repeat[i] = malloc(j * sizeof **dup->repeat);
Michal Vasko1734be92020-09-22 08:55:10 +02002984 LY_CHECK_ERR_GOTO(!dup->repeat[i], LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02002985 memcpy(dup->repeat[i], exp->repeat[i], j * sizeof **dup->repeat);
2986 dup->repeat[i][j - 1] = 0;
2987 }
2988 }
2989
2990 dup->used = exp->used;
2991 dup->size = exp->used;
Michal Vasko1734be92020-09-22 08:55:10 +02002992 LY_CHECK_GOTO(ret = lydict_insert(ctx, exp->expr, 0, &dup->expr), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02002993
Michal Vasko1734be92020-09-22 08:55:10 +02002994cleanup:
2995 if (ret) {
2996 lyxp_expr_free(ctx, dup);
2997 } else {
2998 *dup_p = dup;
2999 }
3000 return ret;
Michal Vasko004d3152020-06-11 19:59:22 +02003001}
3002
Michal Vasko03ff5a72019-09-11 13:49:33 +02003003/*
3004 * warn functions
3005 *
3006 * Warn functions check specific reasonable conditions for schema XPath
3007 * and print a warning if they are not satisfied.
3008 */
3009
3010/**
3011 * @brief Get the last-added schema node that is currently in the context.
3012 *
3013 * @param[in] set Set to search in.
3014 * @return Last-added schema context node, NULL if no node is in context.
3015 */
3016static struct lysc_node *
3017warn_get_scnode_in_ctx(struct lyxp_set *set)
3018{
3019 uint32_t i;
3020
3021 if (!set || (set->type != LYXP_SET_SCNODE_SET)) {
3022 return NULL;
3023 }
3024
3025 i = set->used;
3026 do {
3027 --i;
3028 if (set->val.scnodes[i].in_ctx == 1) {
3029 /* if there are more, simply return the first found (last added) */
3030 return set->val.scnodes[i].scnode;
3031 }
3032 } while (i);
3033
3034 return NULL;
3035}
3036
3037/**
3038 * @brief Test whether a type is numeric - integer type or decimal64.
3039 *
3040 * @param[in] type Type to test.
Radek Krejci857189e2020-09-01 13:26:36 +02003041 * @return Boolean value whether @p type is numeric type or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003042 */
Radek Krejci857189e2020-09-01 13:26:36 +02003043static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003044warn_is_numeric_type(struct lysc_type *type)
3045{
3046 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003047 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003048 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003049
3050 switch (type->basetype) {
3051 case LY_TYPE_DEC64:
3052 case LY_TYPE_INT8:
3053 case LY_TYPE_UINT8:
3054 case LY_TYPE_INT16:
3055 case LY_TYPE_UINT16:
3056 case LY_TYPE_INT32:
3057 case LY_TYPE_UINT32:
3058 case LY_TYPE_INT64:
3059 case LY_TYPE_UINT64:
3060 return 1;
3061 case LY_TYPE_UNION:
3062 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003063 LY_ARRAY_FOR(uni->types, u) {
3064 ret = warn_is_numeric_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003065 if (ret) {
3066 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003067 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003068 }
3069 }
3070 /* did not find any suitable type */
3071 return 0;
3072 case LY_TYPE_LEAFREF:
3073 return warn_is_numeric_type(((struct lysc_type_leafref *)type)->realtype);
3074 default:
3075 return 0;
3076 }
3077}
3078
3079/**
3080 * @brief Test whether a type is string-like - no integers, decimal64 or binary.
3081 *
3082 * @param[in] type Type to test.
Radek Krejci857189e2020-09-01 13:26:36 +02003083 * @return Boolean value whether @p type's basetype is string type or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003084 */
Radek Krejci857189e2020-09-01 13:26:36 +02003085static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003086warn_is_string_type(struct lysc_type *type)
3087{
3088 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003089 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003090 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003091
3092 switch (type->basetype) {
3093 case LY_TYPE_BITS:
3094 case LY_TYPE_ENUM:
3095 case LY_TYPE_IDENT:
3096 case LY_TYPE_INST:
3097 case LY_TYPE_STRING:
3098 return 1;
3099 case LY_TYPE_UNION:
3100 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003101 LY_ARRAY_FOR(uni->types, u) {
3102 ret = warn_is_string_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003103 if (ret) {
3104 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003105 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003106 }
3107 }
3108 /* did not find any suitable type */
3109 return 0;
3110 case LY_TYPE_LEAFREF:
3111 return warn_is_string_type(((struct lysc_type_leafref *)type)->realtype);
3112 default:
3113 return 0;
3114 }
3115}
3116
3117/**
3118 * @brief Test whether a type is one specific type.
3119 *
3120 * @param[in] type Type to test.
3121 * @param[in] base Expected type.
Radek Krejci857189e2020-09-01 13:26:36 +02003122 * @return Boolean value whether the given @p type is of the specific basetype @p base.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003123 */
Radek Krejci857189e2020-09-01 13:26:36 +02003124static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003125warn_is_specific_type(struct lysc_type *type, LY_DATA_TYPE base)
3126{
3127 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003128 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003129 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003130
3131 if (type->basetype == base) {
3132 return 1;
3133 } else if (type->basetype == LY_TYPE_UNION) {
3134 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003135 LY_ARRAY_FOR(uni->types, u) {
3136 ret = warn_is_specific_type(uni->types[u], base);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003137 if (ret) {
3138 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003139 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003140 }
3141 }
3142 /* did not find any suitable type */
3143 return 0;
3144 } else if (type->basetype == LY_TYPE_LEAFREF) {
3145 return warn_is_specific_type(((struct lysc_type_leafref *)type)->realtype, base);
3146 }
3147
3148 return 0;
3149}
3150
3151/**
3152 * @brief Get next type of a (union) type.
3153 *
3154 * @param[in] type Base type.
3155 * @param[in] prev_type Previously returned type.
3156 * @return Next type or NULL.
3157 */
3158static struct lysc_type *
3159warn_is_equal_type_next_type(struct lysc_type *type, struct lysc_type *prev_type)
3160{
3161 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003162 ly_bool found = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003163 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003164
3165 switch (type->basetype) {
3166 case LY_TYPE_UNION:
3167 uni = (struct lysc_type_union *)type;
3168 if (!prev_type) {
3169 return uni->types[0];
3170 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003171 LY_ARRAY_FOR(uni->types, u) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003172 if (found) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003173 return uni->types[u];
Michal Vasko03ff5a72019-09-11 13:49:33 +02003174 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003175 if (prev_type == uni->types[u]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003176 found = 1;
3177 }
3178 }
3179 return NULL;
3180 default:
3181 if (prev_type) {
3182 assert(type == prev_type);
3183 return NULL;
3184 } else {
3185 return type;
3186 }
3187 }
3188}
3189
3190/**
3191 * @brief Test whether 2 types have a common type.
3192 *
3193 * @param[in] type1 First type.
3194 * @param[in] type2 Second type.
3195 * @return 1 if they do, 0 otherwise.
3196 */
3197static int
3198warn_is_equal_type(struct lysc_type *type1, struct lysc_type *type2)
3199{
3200 struct lysc_type *t1, *rt1, *t2, *rt2;
3201
3202 t1 = NULL;
3203 while ((t1 = warn_is_equal_type_next_type(type1, t1))) {
3204 if (t1->basetype == LY_TYPE_LEAFREF) {
3205 rt1 = ((struct lysc_type_leafref *)t1)->realtype;
3206 } else {
3207 rt1 = t1;
3208 }
3209
3210 t2 = NULL;
3211 while ((t2 = warn_is_equal_type_next_type(type2, t2))) {
3212 if (t2->basetype == LY_TYPE_LEAFREF) {
3213 rt2 = ((struct lysc_type_leafref *)t2)->realtype;
3214 } else {
3215 rt2 = t2;
3216 }
3217
3218 if (rt2->basetype == rt1->basetype) {
3219 /* match found */
3220 return 1;
3221 }
3222 }
3223 }
3224
3225 return 0;
3226}
3227
3228/**
3229 * @brief Check both operands of comparison operators.
3230 *
3231 * @param[in] ctx Context for errors.
3232 * @param[in] set1 First operand set.
3233 * @param[in] set2 Second operand set.
3234 * @param[in] numbers_only Whether accept only numbers or other types are fine too (for '=' and '!=').
3235 * @param[in] expr Start of the expression to print with the warning.
3236 * @param[in] tok_pos Token position.
3237 */
3238static void
Radek Krejci857189e2020-09-01 13:26:36 +02003239warn_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 +02003240{
3241 struct lysc_node_leaf *node1, *node2;
Radek Krejci857189e2020-09-01 13:26:36 +02003242 ly_bool leaves = 1, warning = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003243
3244 node1 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set1);
3245 node2 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set2);
3246
3247 if (!node1 && !node2) {
3248 /* no node-sets involved, nothing to do */
3249 return;
3250 }
3251
3252 if (node1) {
3253 if (!(node1->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3254 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node1->nodetype), node1->name);
3255 warning = 1;
3256 leaves = 0;
3257 } else if (numbers_only && !warn_is_numeric_type(node1->type)) {
3258 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node1->name);
3259 warning = 1;
3260 }
3261 }
3262
3263 if (node2) {
3264 if (!(node2->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3265 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node2->nodetype), node2->name);
3266 warning = 1;
3267 leaves = 0;
3268 } else if (numbers_only && !warn_is_numeric_type(node2->type)) {
3269 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node2->name);
3270 warning = 1;
3271 }
3272 }
3273
3274 if (node1 && node2 && leaves && !numbers_only) {
Michal Vasko69730152020-10-09 16:30:07 +02003275 if ((warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type)) ||
3276 (!warn_is_numeric_type(node1->type) && warn_is_numeric_type(node2->type)) ||
3277 (!warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type) &&
3278 !warn_is_equal_type(node1->type, node2->type))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003279 LOGWRN(ctx, "Incompatible types of operands \"%s\" and \"%s\" for comparison.", node1->name, node2->name);
3280 warning = 1;
3281 }
3282 }
3283
3284 if (warning) {
3285 LOGWRN(ctx, "Previous warning generated by XPath subexpression[%u] \"%.20s\".", tok_pos, expr + tok_pos);
3286 }
3287}
3288
3289/**
3290 * @brief Check that a value is valid for a leaf. If not applicable, does nothing.
3291 *
3292 * @param[in] exp Parsed XPath expression.
3293 * @param[in] set Set with the leaf/leaf-list.
3294 * @param[in] val_exp Index of the value (literal/number) in @p exp.
3295 * @param[in] equal_exp Index of the start of the equality expression in @p exp.
3296 * @param[in] last_equal_exp Index of the end of the equality expression in @p exp.
3297 */
3298static void
Michal Vasko40308e72020-10-20 16:38:40 +02003299warn_equality_value(const struct lyxp_expr *exp, struct lyxp_set *set, uint16_t val_exp, uint16_t equal_exp,
3300 uint16_t last_equal_exp)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003301{
3302 struct lysc_node *scnode;
3303 struct lysc_type *type;
3304 char *value;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003305 struct lyd_value storage;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003306 LY_ERR rc;
3307 struct ly_err_item *err = NULL;
3308
Michal Vasko69730152020-10-09 16:30:07 +02003309 if ((scnode = warn_get_scnode_in_ctx(set)) && (scnode->nodetype & (LYS_LEAF | LYS_LEAFLIST)) &&
3310 ((exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) || (exp->tokens[val_exp] == LYXP_TOKEN_NUMBER))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003311 /* check that the node can have the specified value */
3312 if (exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) {
3313 value = strndup(exp->expr + exp->tok_pos[val_exp] + 1, exp->tok_len[val_exp] - 2);
3314 } else {
3315 value = strndup(exp->expr + exp->tok_pos[val_exp], exp->tok_len[val_exp]);
3316 }
3317 if (!value) {
3318 LOGMEM(set->ctx);
3319 return;
3320 }
3321
3322 if ((((struct lysc_node_leaf *)scnode)->type->basetype == LY_TYPE_IDENT) && !strchr(value, ':')) {
3323 LOGWRN(set->ctx, "Identityref \"%s\" comparison with identity \"%s\" without prefix, consider adding"
Michal Vasko69730152020-10-09 16:30:07 +02003324 " a prefix or best using \"derived-from(-or-self)()\" functions.", scnode->name, value);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003325 LOGWRN(set->ctx, "Previous warning generated by XPath subexpression[%u] \"%.*s\".", exp->tok_pos[equal_exp],
Radek Krejci0f969882020-08-21 16:56:47 +02003326 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
Michal Vasko69730152020-10-09 16:30:07 +02003327 exp->expr + exp->tok_pos[equal_exp]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003328 }
3329
3330 type = ((struct lysc_node_leaf *)scnode)->type;
3331 if (type->basetype != LY_TYPE_IDENT) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003332 rc = type->plugin->store(set->ctx, type, value, strlen(value), 0, set->format, set->prefix_data,
Michal Vaskofeca4fb2020-10-05 08:58:40 +02003333 LYD_HINT_DATA, scnode, &storage, &err);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003334
3335 if (err) {
3336 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type (%s).", value, err->msg);
3337 ly_err_free(err);
3338 } else if (rc != LY_SUCCESS) {
3339 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type.", value);
3340 }
3341 if (rc != LY_SUCCESS) {
3342 LOGWRN(set->ctx, "Previous warning generated by XPath subexpression[%u] \"%.*s\".", exp->tok_pos[equal_exp],
Radek Krejci0f969882020-08-21 16:56:47 +02003343 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
Michal Vasko69730152020-10-09 16:30:07 +02003344 exp->expr + exp->tok_pos[equal_exp]);
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003345 } else {
3346 type->plugin->free(set->ctx, &storage);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003347 }
3348 }
3349 free(value);
3350 }
3351}
3352
3353/*
3354 * XPath functions
3355 */
3356
3357/**
3358 * @brief Execute the YANG 1.1 bit-is-set(node-set, string) function. Returns LYXP_SET_BOOLEAN
3359 * depending on whether the first node bit value from the second argument is set.
3360 *
3361 * @param[in] args Array of arguments.
3362 * @param[in] arg_count Count of elements in @p args.
3363 * @param[in,out] set Context and result set at the same time.
3364 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003365 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003366 */
3367static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003368xpath_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 +02003369{
3370 struct lyd_node_term *leaf;
3371 struct lysc_node_leaf *sleaf;
3372 struct lysc_type_bits *bits;
3373 LY_ERR rc = LY_SUCCESS;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003374 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003375
3376 if (options & LYXP_SCNODE_ALL) {
3377 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3378 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003379 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3380 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 +02003381 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_BITS)) {
3382 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"bits\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003383 }
3384
3385 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3386 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3387 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 +02003388 } else if (!warn_is_string_type(sleaf->type)) {
3389 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003390 }
3391 }
3392 set_scnode_clear_ctx(set);
3393 return rc;
3394 }
3395
Michal Vaskod3678892020-05-21 10:06:58 +02003396 if (args[0]->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003397 LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
3398 "bit-is-set(node-set, string)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003399 return LY_EVALID;
3400 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003401 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003402 LY_CHECK_RET(rc);
3403
3404 set_fill_boolean(set, 0);
Michal Vaskod3678892020-05-21 10:06:58 +02003405 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003406 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
Michal Vasko69730152020-10-09 16:30:07 +02003407 if ((leaf->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) &&
3408 (((struct lysc_node_leaf *)leaf->schema)->type->basetype == LY_TYPE_BITS)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003409 bits = (struct lysc_type_bits *)((struct lysc_node_leaf *)leaf->schema)->type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003410 LY_ARRAY_FOR(bits->bits, u) {
3411 if (!strcmp(bits->bits[u].name, args[1]->val.str)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003412 set_fill_boolean(set, 1);
3413 break;
3414 }
3415 }
3416 }
3417 }
3418
3419 return LY_SUCCESS;
3420}
3421
3422/**
3423 * @brief Execute the XPath boolean(object) function. Returns LYXP_SET_BOOLEAN
3424 * with the argument converted to boolean.
3425 *
3426 * @param[in] args Array of arguments.
3427 * @param[in] arg_count Count of elements in @p args.
3428 * @param[in,out] set Context and result set at the same time.
3429 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003430 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003431 */
3432static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003433xpath_boolean(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003434{
3435 LY_ERR rc;
3436
3437 if (options & LYXP_SCNODE_ALL) {
3438 set_scnode_clear_ctx(set);
3439 return LY_SUCCESS;
3440 }
3441
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003442 rc = lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003443 LY_CHECK_RET(rc);
3444 set_fill_set(set, args[0]);
3445
3446 return LY_SUCCESS;
3447}
3448
3449/**
3450 * @brief Execute the XPath ceiling(number) function. Returns LYXP_SET_NUMBER
3451 * with the first argument rounded up to the nearest integer.
3452 *
3453 * @param[in] args Array of arguments.
3454 * @param[in] arg_count Count of elements in @p args.
3455 * @param[in,out] set Context and result set at the same time.
3456 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003457 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003458 */
3459static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003460xpath_ceiling(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003461{
3462 struct lysc_node_leaf *sleaf;
3463 LY_ERR rc = LY_SUCCESS;
3464
3465 if (options & LYXP_SCNODE_ALL) {
3466 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3467 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003468 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3469 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 +02003470 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
3471 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003472 }
3473 set_scnode_clear_ctx(set);
3474 return rc;
3475 }
3476
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003477 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003478 LY_CHECK_RET(rc);
3479 if ((long long)args[0]->val.num != args[0]->val.num) {
3480 set_fill_number(set, ((long long)args[0]->val.num) + 1);
3481 } else {
3482 set_fill_number(set, args[0]->val.num);
3483 }
3484
3485 return LY_SUCCESS;
3486}
3487
3488/**
3489 * @brief Execute the XPath concat(string, string, string*) function.
3490 * Returns LYXP_SET_STRING with the concatenation of all the arguments.
3491 *
3492 * @param[in] args Array of arguments.
3493 * @param[in] arg_count Count of elements in @p args.
3494 * @param[in,out] set Context and result set at the same time.
3495 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003496 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003497 */
3498static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003499xpath_concat(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003500{
3501 uint16_t i;
3502 char *str = NULL;
3503 size_t used = 1;
3504 LY_ERR rc = LY_SUCCESS;
3505 struct lysc_node_leaf *sleaf;
3506
3507 if (options & LYXP_SCNODE_ALL) {
3508 for (i = 0; i < arg_count; ++i) {
3509 if ((args[i]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[i]))) {
3510 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3511 LOGWRN(set->ctx, "Argument #%u of %s is a %s node \"%s\".",
Michal Vasko69730152020-10-09 16:30:07 +02003512 i + 1, __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003513 } else if (!warn_is_string_type(sleaf->type)) {
Radek Krejci70124c82020-08-14 22:17:03 +02003514 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 +02003515 }
3516 }
3517 }
3518 set_scnode_clear_ctx(set);
3519 return rc;
3520 }
3521
3522 for (i = 0; i < arg_count; ++i) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003523 rc = lyxp_set_cast(args[i], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003524 if (rc != LY_SUCCESS) {
3525 free(str);
3526 return rc;
3527 }
3528
3529 str = ly_realloc(str, (used + strlen(args[i]->val.str)) * sizeof(char));
3530 LY_CHECK_ERR_RET(!str, LOGMEM(set->ctx), LY_EMEM);
3531 strcpy(str + used - 1, args[i]->val.str);
3532 used += strlen(args[i]->val.str);
3533 }
3534
3535 /* free, kind of */
Michal Vaskod3678892020-05-21 10:06:58 +02003536 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003537 set->type = LYXP_SET_STRING;
3538 set->val.str = str;
3539
3540 return LY_SUCCESS;
3541}
3542
3543/**
3544 * @brief Execute the XPath contains(string, string) function.
3545 * Returns LYXP_SET_BOOLEAN whether the second argument can
3546 * be found in the first or not.
3547 *
3548 * @param[in] args Array of arguments.
3549 * @param[in] arg_count Count of elements in @p args.
3550 * @param[in,out] set Context and result set at the same time.
3551 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003552 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003553 */
3554static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003555xpath_contains(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003556{
3557 struct lysc_node_leaf *sleaf;
3558 LY_ERR rc = LY_SUCCESS;
3559
3560 if (options & LYXP_SCNODE_ALL) {
3561 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3562 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3563 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 +02003564 } else if (!warn_is_string_type(sleaf->type)) {
3565 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003566 }
3567 }
3568
3569 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3570 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3571 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 +02003572 } else if (!warn_is_string_type(sleaf->type)) {
3573 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003574 }
3575 }
3576 set_scnode_clear_ctx(set);
3577 return rc;
3578 }
3579
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003580 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003581 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003582 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003583 LY_CHECK_RET(rc);
3584
3585 if (strstr(args[0]->val.str, args[1]->val.str)) {
3586 set_fill_boolean(set, 1);
3587 } else {
3588 set_fill_boolean(set, 0);
3589 }
3590
3591 return LY_SUCCESS;
3592}
3593
3594/**
3595 * @brief Execute the XPath count(node-set) function. Returns LYXP_SET_NUMBER
3596 * with the size of the node-set from the argument.
3597 *
3598 * @param[in] args Array of arguments.
3599 * @param[in] arg_count Count of elements in @p args.
3600 * @param[in,out] set Context and result set at the same time.
3601 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003602 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003603 */
3604static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003605xpath_count(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003606{
3607 struct lysc_node *scnode = NULL;
3608 LY_ERR rc = LY_SUCCESS;
3609
3610 if (options & LYXP_SCNODE_ALL) {
3611 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(scnode = warn_get_scnode_in_ctx(args[0]))) {
3612 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003613 }
3614 set_scnode_clear_ctx(set);
3615 return rc;
3616 }
3617
Michal Vasko03ff5a72019-09-11 13:49:33 +02003618 if (args[0]->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003619 LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "count(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003620 return LY_EVALID;
3621 }
3622
3623 set_fill_number(set, args[0]->used);
3624 return LY_SUCCESS;
3625}
3626
3627/**
3628 * @brief Execute the XPath current() function. Returns LYXP_SET_NODE_SET
3629 * with the context with the intial node.
3630 *
3631 * @param[in] args Array of arguments.
3632 * @param[in] arg_count Count of elements in @p args.
3633 * @param[in,out] set Context and result set at the same time.
3634 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003635 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003636 */
3637static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003638xpath_current(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003639{
3640 if (arg_count || args) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003641 LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INARGCOUNT, arg_count, "current()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003642 return LY_EVALID;
3643 }
3644
3645 if (options & LYXP_SCNODE_ALL) {
3646 set_scnode_clear_ctx(set);
3647
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003648 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, set->cur_scnode, LYXP_NODE_ELEM, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02003649 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02003650 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003651
3652 /* position is filled later */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003653 set_insert_node(set, set->cur_node, 0, LYXP_NODE_ELEM, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003654 }
3655
3656 return LY_SUCCESS;
3657}
3658
3659/**
3660 * @brief Execute the YANG 1.1 deref(node-set) function. Returns LYXP_SET_NODE_SET with either
3661 * leafref or instance-identifier target node(s).
3662 *
3663 * @param[in] args Array of arguments.
3664 * @param[in] arg_count Count of elements in @p args.
3665 * @param[in,out] set Context and result set at the same time.
3666 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003667 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003668 */
3669static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003670xpath_deref(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003671{
3672 struct lyd_node_term *leaf;
Michal Vasko42e497c2020-01-06 08:38:25 +01003673 struct lysc_node_leaf *sleaf = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02003674 struct lysc_type_leafref *lref;
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003675 const struct lysc_node *target;
Michal Vasko004d3152020-06-11 19:59:22 +02003676 struct ly_path *p;
3677 struct lyd_node *node;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003678 char *errmsg = NULL;
Michal Vasko00cbf532020-06-15 13:58:47 +02003679 uint8_t oper;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003680 LY_ERR rc = LY_SUCCESS;
3681
3682 if (options & LYXP_SCNODE_ALL) {
3683 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3684 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003685 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3686 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 +02003687 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_LEAFREF) && !warn_is_specific_type(sleaf->type, LY_TYPE_INST)) {
3688 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"leafref\" nor \"instance-identifier\".",
Michal Vasko69730152020-10-09 16:30:07 +02003689 __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003690 }
3691 set_scnode_clear_ctx(set);
Michal Vasko42e497c2020-01-06 08:38:25 +01003692 if (sleaf && (sleaf->type->basetype == LY_TYPE_LEAFREF)) {
Michal Vasko004d3152020-06-11 19:59:22 +02003693 lref = (struct lysc_type_leafref *)sleaf->type;
Michal Vasko00cbf532020-06-15 13:58:47 +02003694 oper = lysc_is_output((struct lysc_node *)sleaf) ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT;
Michal Vasko004d3152020-06-11 19:59:22 +02003695
3696 /* it was already evaluated on schema, it must succeed */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003697 rc = ly_path_compile(set->ctx, sleaf->module, (struct lysc_node *)sleaf, lref->path,
3698 LY_PATH_LREF_TRUE, oper, LY_PATH_TARGET_MANY, LY_PREF_SCHEMA_RESOLVED, lref->prefixes, &p);
Michal Vasko004d3152020-06-11 19:59:22 +02003699 assert(!rc);
3700
3701 /* get the target node */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003702 target = p[LY_ARRAY_COUNT(p) - 1].node;
Michal Vasko004d3152020-06-11 19:59:22 +02003703 ly_path_free(set->ctx, p);
3704
Radek Krejciaa6b53f2020-08-27 15:19:03 +02003705 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, target, LYXP_NODE_ELEM, NULL));
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003706 }
3707
Michal Vasko03ff5a72019-09-11 13:49:33 +02003708 return rc;
3709 }
3710
Michal Vaskod3678892020-05-21 10:06:58 +02003711 if (args[0]->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003712 LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "deref(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003713 return LY_EVALID;
3714 }
3715
Michal Vaskod3678892020-05-21 10:06:58 +02003716 lyxp_set_free_content(set);
3717 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003718 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
3719 sleaf = (struct lysc_node_leaf *)leaf->schema;
3720 if (sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
3721 if (sleaf->type->basetype == LY_TYPE_LEAFREF) {
3722 /* find leafref target */
Michal Vasko004d3152020-06-11 19:59:22 +02003723 if (ly_type_find_leafref((struct lysc_type_leafref *)sleaf->type, (struct lyd_node *)leaf,
Michal Vasko69730152020-10-09 16:30:07 +02003724 &leaf->value, set->tree, &node, &errmsg)) {
Michal Vasko004d3152020-06-11 19:59:22 +02003725 LOGERR(set->ctx, LY_EVALID, errmsg);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003726 free(errmsg);
Michal Vasko004d3152020-06-11 19:59:22 +02003727 return LY_EVALID;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003728 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003729 } else {
3730 assert(sleaf->type->basetype == LY_TYPE_INST);
Michal Vasko004d3152020-06-11 19:59:22 +02003731 if (ly_path_eval(leaf->value.target, set->tree, &node)) {
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003732 LOGERR(set->ctx, LY_EVALID, "Invalid instance-identifier \"%s\" value - required instance not found.",
Michal Vasko69730152020-10-09 16:30:07 +02003733 LYD_CANON_VALUE(leaf));
Michal Vasko03ff5a72019-09-11 13:49:33 +02003734 return LY_EVALID;
3735 }
3736 }
Michal Vasko004d3152020-06-11 19:59:22 +02003737
3738 /* insert it */
3739 set_insert_node(set, node, 0, LYXP_NODE_ELEM, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003740 }
3741 }
3742
3743 return LY_SUCCESS;
3744}
3745
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003746static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02003747xpath_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 +02003748{
3749 uint16_t i;
3750 LY_ARRAY_COUNT_TYPE u;
3751 struct lyd_node_term *leaf;
3752 struct lysc_node_leaf *sleaf;
3753 struct lyd_meta *meta;
3754 struct lyd_value data = {0}, *val;
3755 struct ly_err_item *err = NULL;
3756 LY_ERR rc = LY_SUCCESS;
Radek Krejci857189e2020-09-01 13:26:36 +02003757 ly_bool found;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003758
3759 if (options & LYXP_SCNODE_ALL) {
3760 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3761 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", func);
3762 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3763 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype),
Michal Vasko69730152020-10-09 16:30:07 +02003764 sleaf->name);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003765 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_IDENT)) {
3766 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"identityref\".", func, sleaf->name);
3767 }
3768
3769 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3770 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3771 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype),
Michal Vasko69730152020-10-09 16:30:07 +02003772 sleaf->name);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003773 } else if (!warn_is_string_type(sleaf->type)) {
3774 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", func, sleaf->name);
3775 }
3776 }
3777 set_scnode_clear_ctx(set);
3778 return rc;
3779 }
3780
3781 if (args[0]->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003782 LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
Michal Vasko69730152020-10-09 16:30:07 +02003783 "derived-from(-or-self)(node-set, string)");
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003784 return LY_EVALID;
3785 }
3786 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
3787 LY_CHECK_RET(rc);
3788
3789 set_fill_boolean(set, 0);
3790 found = 0;
3791 for (i = 0; i < args[0]->used; ++i) {
3792 if ((args[0]->val.nodes[i].type != LYXP_NODE_ELEM) && (args[0]->val.nodes[i].type != LYXP_NODE_META)) {
3793 continue;
3794 }
3795
3796 if (args[0]->val.nodes[i].type == LYXP_NODE_ELEM) {
3797 leaf = (struct lyd_node_term *)args[0]->val.nodes[i].node;
3798 sleaf = (struct lysc_node_leaf *)leaf->schema;
3799 val = &leaf->value;
3800 if (!(sleaf->nodetype & LYD_NODE_TERM) || (leaf->value.realtype->basetype != LY_TYPE_IDENT)) {
3801 /* uninteresting */
3802 continue;
3803 }
3804
3805 /* store args[1] as ident */
3806 rc = val->realtype->plugin->store(set->ctx, val->realtype, args[1]->val.str, strlen(args[1]->val.str),
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003807 0, set->format, set->prefix_data, LYD_HINT_DATA, leaf->schema, &data, &err);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003808 } else {
3809 meta = args[0]->val.meta[i].meta;
3810 val = &meta->value;
3811 if (val->realtype->basetype != LY_TYPE_IDENT) {
3812 /* uninteresting */
3813 continue;
3814 }
3815
3816 /* store args[1] as ident */
Michal Vaskofeca4fb2020-10-05 08:58:40 +02003817 rc = val->realtype->plugin->store(set->ctx, val->realtype, args[1]->val.str, strlen(args[1]->val.str), 0,
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003818 set->format, set->prefix_data, LYD_HINT_DATA, meta->parent->schema, &data, &err);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003819 }
3820
3821 if (err) {
3822 ly_err_print(err);
3823 ly_err_free(err);
3824 }
3825 LY_CHECK_RET(rc);
3826
3827 /* finally check the identity itself */
3828 if (self_match && (data.ident == val->ident)) {
3829 set_fill_boolean(set, 1);
3830 found = 1;
3831 }
3832 if (!found) {
3833 LY_ARRAY_FOR(data.ident->derived, u) {
3834 if (data.ident->derived[u] == val->ident) {
3835 set_fill_boolean(set, 1);
3836 found = 1;
3837 break;
3838 }
3839 }
3840 }
3841
3842 /* free temporary value */
3843 val->realtype->plugin->free(set->ctx, &data);
3844 if (found) {
3845 break;
3846 }
3847 }
3848
3849 return LY_SUCCESS;
3850}
3851
Michal Vasko03ff5a72019-09-11 13:49:33 +02003852/**
3853 * @brief Execute the YANG 1.1 derived-from(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
3854 * on whether the first argument nodes contain a node of an identity derived from the second
3855 * argument identity.
3856 *
3857 * @param[in] args Array of arguments.
3858 * @param[in] arg_count Count of elements in @p args.
3859 * @param[in,out] set Context and result set at the same time.
3860 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003861 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003862 */
3863static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003864xpath_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 +02003865{
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003866 return xpath_derived_(args, set, options, 0, __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003867}
3868
3869/**
3870 * @brief Execute the YANG 1.1 derived-from-or-self(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
3871 * on whether the first argument nodes contain a node of an identity that either is or is derived from
3872 * the second argument identity.
3873 *
3874 * @param[in] args Array of arguments.
3875 * @param[in] arg_count Count of elements in @p args.
3876 * @param[in,out] set Context and result set at the same time.
3877 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003878 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003879 */
3880static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003881xpath_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 +02003882{
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003883 return xpath_derived_(args, set, options, 1, __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003884}
3885
3886/**
3887 * @brief Execute the YANG 1.1 enum-value(node-set) function. Returns LYXP_SET_NUMBER
3888 * with the integer value of the first node's enum value, otherwise NaN.
3889 *
3890 * @param[in] args Array of arguments.
3891 * @param[in] arg_count Count of elements in @p args.
3892 * @param[in,out] set Context and result set at the same time.
3893 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003894 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003895 */
3896static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003897xpath_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 +02003898{
3899 struct lyd_node_term *leaf;
3900 struct lysc_node_leaf *sleaf;
3901 LY_ERR rc = LY_SUCCESS;
3902
3903 if (options & LYXP_SCNODE_ALL) {
3904 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3905 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003906 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3907 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 +02003908 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_ENUM)) {
3909 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"enumeration\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003910 }
3911 set_scnode_clear_ctx(set);
3912 return rc;
3913 }
3914
Michal Vaskod3678892020-05-21 10:06:58 +02003915 if (args[0]->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003916 LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "enum-value(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003917 return LY_EVALID;
3918 }
3919
3920 set_fill_number(set, NAN);
Michal Vaskod3678892020-05-21 10:06:58 +02003921 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003922 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
3923 sleaf = (struct lysc_node_leaf *)leaf->schema;
3924 if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_ENUM)) {
3925 set_fill_number(set, leaf->value.enum_item->value);
3926 }
3927 }
3928
3929 return LY_SUCCESS;
3930}
3931
3932/**
3933 * @brief Execute the XPath false() function. Returns LYXP_SET_BOOLEAN
3934 * with false value.
3935 *
3936 * @param[in] args Array of arguments.
3937 * @param[in] arg_count Count of elements in @p args.
3938 * @param[in,out] set Context and result set at the same time.
3939 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003940 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003941 */
3942static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003943xpath_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 +02003944{
3945 if (options & LYXP_SCNODE_ALL) {
3946 set_scnode_clear_ctx(set);
3947 return LY_SUCCESS;
3948 }
3949
3950 set_fill_boolean(set, 0);
3951 return LY_SUCCESS;
3952}
3953
3954/**
3955 * @brief Execute the XPath floor(number) function. Returns LYXP_SET_NUMBER
3956 * with the first argument floored (truncated).
3957 *
3958 * @param[in] args Array of arguments.
3959 * @param[in] arg_count Count of elements in @p args.
3960 * @param[in,out] set Context and result set at the same time.
3961 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003962 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003963 */
3964static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003965xpath_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 +02003966{
3967 LY_ERR rc;
3968
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003969 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003970 LY_CHECK_RET(rc);
3971 if (isfinite(args[0]->val.num)) {
3972 set_fill_number(set, (long long)args[0]->val.num);
3973 }
3974
3975 return LY_SUCCESS;
3976}
3977
3978/**
3979 * @brief Execute the XPath lang(string) function. Returns LYXP_SET_BOOLEAN
3980 * whether the language of the text matches the one from the argument.
3981 *
3982 * @param[in] args Array of arguments.
3983 * @param[in] arg_count Count of elements in @p args.
3984 * @param[in,out] set Context and result set at the same time.
3985 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003986 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003987 */
3988static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003989xpath_lang(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003990{
3991 const struct lyd_node *node;
3992 struct lysc_node_leaf *sleaf;
Michal Vasko9f96a052020-03-10 09:41:45 +01003993 struct lyd_meta *meta = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003994 const char *val;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003995 LY_ERR rc = LY_SUCCESS;
3996
3997 if (options & LYXP_SCNODE_ALL) {
3998 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3999 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4000 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 +02004001 } else if (!warn_is_string_type(sleaf->type)) {
4002 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004003 }
4004 }
4005 set_scnode_clear_ctx(set);
4006 return rc;
4007 }
4008
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004009 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004010 LY_CHECK_RET(rc);
4011
Michal Vasko03ff5a72019-09-11 13:49:33 +02004012 if (set->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004013 LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INCTX, print_set_type(set), "lang(string)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004014 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004015 } else if (!set->used) {
4016 set_fill_boolean(set, 0);
4017 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004018 }
4019
4020 switch (set->val.nodes[0].type) {
4021 case LYXP_NODE_ELEM:
4022 case LYXP_NODE_TEXT:
4023 node = set->val.nodes[0].node;
4024 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004025 case LYXP_NODE_META:
4026 node = set->val.meta[0].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004027 break;
4028 default:
4029 /* nothing to do with roots */
4030 set_fill_boolean(set, 0);
4031 return LY_SUCCESS;
4032 }
4033
Michal Vasko9f96a052020-03-10 09:41:45 +01004034 /* find lang metadata */
Michal Vaskod989ba02020-08-24 10:59:24 +02004035 for ( ; node; node = (struct lyd_node *)node->parent) {
Michal Vasko9f96a052020-03-10 09:41:45 +01004036 for (meta = node->meta; meta; meta = meta->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004037 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004038 if (meta->name && !strcmp(meta->name, "lang") && !strcmp(meta->annotation->module->name, "xml")) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004039 break;
4040 }
4041 }
4042
Michal Vasko9f96a052020-03-10 09:41:45 +01004043 if (meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004044 break;
4045 }
4046 }
4047
4048 /* compare languages */
Michal Vasko9f96a052020-03-10 09:41:45 +01004049 if (!meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004050 set_fill_boolean(set, 0);
4051 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004052 uint64_t i;
4053
Michal Vaskoba99a3e2020-08-18 15:50:05 +02004054 val = meta->value.canonical;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004055 for (i = 0; args[0]->val.str[i]; ++i) {
4056 if (tolower(args[0]->val.str[i]) != tolower(val[i])) {
4057 set_fill_boolean(set, 0);
4058 break;
4059 }
4060 }
4061 if (!args[0]->val.str[i]) {
4062 if (!val[i] || (val[i] == '-')) {
4063 set_fill_boolean(set, 1);
4064 } else {
4065 set_fill_boolean(set, 0);
4066 }
4067 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004068 }
4069
4070 return LY_SUCCESS;
4071}
4072
4073/**
4074 * @brief Execute the XPath last() function. Returns LYXP_SET_NUMBER
4075 * with the context size.
4076 *
4077 * @param[in] args Array of arguments.
4078 * @param[in] arg_count Count of elements in @p args.
4079 * @param[in,out] set Context and result set at the same time.
4080 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004081 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004082 */
4083static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004084xpath_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 +02004085{
4086 if (options & LYXP_SCNODE_ALL) {
4087 set_scnode_clear_ctx(set);
4088 return LY_SUCCESS;
4089 }
4090
Michal Vasko03ff5a72019-09-11 13:49:33 +02004091 if (set->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004092 LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INCTX, print_set_type(set), "last()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004093 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004094 } else if (!set->used) {
4095 set_fill_number(set, 0);
4096 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004097 }
4098
4099 set_fill_number(set, set->ctx_size);
4100 return LY_SUCCESS;
4101}
4102
4103/**
4104 * @brief Execute the XPath local-name(node-set?) function. Returns LYXP_SET_STRING
4105 * with the node name without namespace from the argument or the context.
4106 *
4107 * @param[in] args Array of arguments.
4108 * @param[in] arg_count Count of elements in @p args.
4109 * @param[in,out] set Context and result set at the same time.
4110 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004111 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004112 */
4113static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004114xpath_local_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004115{
4116 struct lyxp_set_node *item;
Michal Vasko69730152020-10-09 16:30:07 +02004117
Michal Vasko03ff5a72019-09-11 13:49:33 +02004118 /* suppress unused variable warning */
4119 (void)options;
4120
4121 if (options & LYXP_SCNODE_ALL) {
4122 set_scnode_clear_ctx(set);
4123 return LY_SUCCESS;
4124 }
4125
4126 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004127 if (args[0]->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004128 LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
4129 "local-name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004130 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004131 } else if (!args[0]->used) {
4132 set_fill_string(set, "", 0);
4133 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004134 }
4135
4136 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004137 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004138
4139 item = &args[0]->val.nodes[0];
4140 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004141 if (set->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004142 LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INCTX, print_set_type(set), "local-name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004143 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004144 } else if (!set->used) {
4145 set_fill_string(set, "", 0);
4146 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004147 }
4148
4149 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004150 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004151
4152 item = &set->val.nodes[0];
4153 }
4154
4155 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004156 case LYXP_NODE_NONE:
4157 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004158 case LYXP_NODE_ROOT:
4159 case LYXP_NODE_ROOT_CONFIG:
4160 case LYXP_NODE_TEXT:
4161 set_fill_string(set, "", 0);
4162 break;
4163 case LYXP_NODE_ELEM:
4164 set_fill_string(set, item->node->schema->name, strlen(item->node->schema->name));
4165 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004166 case LYXP_NODE_META:
4167 set_fill_string(set, ((struct lyd_meta *)item->node)->name, strlen(((struct lyd_meta *)item->node)->name));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004168 break;
4169 }
4170
4171 return LY_SUCCESS;
4172}
4173
4174/**
4175 * @brief Execute the XPath name(node-set?) function. Returns LYXP_SET_STRING
4176 * with the node name fully qualified (with namespace) from the argument or the context.
4177 *
4178 * @param[in] args Array of arguments.
4179 * @param[in] arg_count Count of elements in @p args.
4180 * @param[in,out] set Context and result set at the same time.
4181 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004182 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004183 */
4184static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004185xpath_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004186{
4187 struct lyxp_set_node *item;
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004188 struct lys_module *mod = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004189 char *str;
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004190 const char *name = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004191
4192 if (options & LYXP_SCNODE_ALL) {
4193 set_scnode_clear_ctx(set);
4194 return LY_SUCCESS;
4195 }
4196
4197 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004198 if (args[0]->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004199 LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004200 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004201 } else if (!args[0]->used) {
4202 set_fill_string(set, "", 0);
4203 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004204 }
4205
4206 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004207 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004208
4209 item = &args[0]->val.nodes[0];
4210 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004211 if (set->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004212 LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INCTX, print_set_type(set), "name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004213 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004214 } else if (!set->used) {
4215 set_fill_string(set, "", 0);
4216 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004217 }
4218
4219 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004220 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004221
4222 item = &set->val.nodes[0];
4223 }
4224
4225 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004226 case LYXP_NODE_NONE:
4227 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004228 case LYXP_NODE_ROOT:
4229 case LYXP_NODE_ROOT_CONFIG:
4230 case LYXP_NODE_TEXT:
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004231 /* keep NULL */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004232 break;
4233 case LYXP_NODE_ELEM:
4234 mod = item->node->schema->module;
4235 name = item->node->schema->name;
4236 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004237 case LYXP_NODE_META:
4238 mod = ((struct lyd_meta *)item->node)->annotation->module;
4239 name = ((struct lyd_meta *)item->node)->name;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004240 break;
4241 }
4242
4243 if (mod && name) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004244 int rc = asprintf(&str, "%s:%s", ly_get_prefix(mod, set->format, set->prefix_data), name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004245 LY_CHECK_ERR_RET(rc == -1, LOGMEM(set->ctx), LY_EMEM);
4246 set_fill_string(set, str, strlen(str));
4247 free(str);
4248 } else {
4249 set_fill_string(set, "", 0);
4250 }
4251
4252 return LY_SUCCESS;
4253}
4254
4255/**
4256 * @brief Execute the XPath namespace-uri(node-set?) function. Returns LYXP_SET_STRING
4257 * with the namespace of the node from the argument or the context.
4258 *
4259 * @param[in] args Array of arguments.
4260 * @param[in] arg_count Count of elements in @p args.
4261 * @param[in,out] set Context and result set at the same time.
4262 * @param[in] options XPath options.
4263 * @return LY_ERR (LY_EINVAL for wrong arguments on schema)
4264 */
4265static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004266xpath_namespace_uri(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004267{
4268 struct lyxp_set_node *item;
4269 struct lys_module *mod;
Michal Vasko69730152020-10-09 16:30:07 +02004270
Michal Vasko03ff5a72019-09-11 13:49:33 +02004271 /* suppress unused variable warning */
4272 (void)options;
4273
4274 if (options & LYXP_SCNODE_ALL) {
4275 set_scnode_clear_ctx(set);
4276 return LY_SUCCESS;
4277 }
4278
4279 if (arg_count) {
Michal Vaskod3678892020-05-21 10:06:58 +02004280 if (args[0]->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004281 LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
Michal Vasko69730152020-10-09 16:30:07 +02004282 "namespace-uri(node-set?)");
Michal Vaskod3678892020-05-21 10:06:58 +02004283 return LY_EVALID;
4284 } else if (!args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004285 set_fill_string(set, "", 0);
4286 return LY_SUCCESS;
4287 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004288
4289 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004290 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004291
4292 item = &args[0]->val.nodes[0];
4293 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004294 if (set->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004295 LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INCTX, print_set_type(set), "namespace-uri(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004296 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004297 } else if (!set->used) {
4298 set_fill_string(set, "", 0);
4299 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004300 }
4301
4302 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004303 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004304
4305 item = &set->val.nodes[0];
4306 }
4307
4308 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004309 case LYXP_NODE_NONE:
4310 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004311 case LYXP_NODE_ROOT:
4312 case LYXP_NODE_ROOT_CONFIG:
4313 case LYXP_NODE_TEXT:
4314 set_fill_string(set, "", 0);
4315 break;
4316 case LYXP_NODE_ELEM:
Michal Vasko9f96a052020-03-10 09:41:45 +01004317 case LYXP_NODE_META:
Michal Vasko03ff5a72019-09-11 13:49:33 +02004318 if (item->type == LYXP_NODE_ELEM) {
4319 mod = item->node->schema->module;
Michal Vasko9f96a052020-03-10 09:41:45 +01004320 } else { /* LYXP_NODE_META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004321 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004322 mod = ((struct lyd_meta *)item->node)->annotation->module;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004323 }
4324
4325 set_fill_string(set, mod->ns, strlen(mod->ns));
4326 break;
4327 }
4328
4329 return LY_SUCCESS;
4330}
4331
4332/**
4333 * @brief Execute the XPath node() function (node type). Returns LYXP_SET_NODE_SET
4334 * with only nodes from the context. In practice it either leaves the context
4335 * as it is or returns an empty node set.
4336 *
4337 * @param[in] args Array of arguments.
4338 * @param[in] arg_count Count of elements in @p args.
4339 * @param[in,out] set Context and result set at the same time.
4340 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004341 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004342 */
4343static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004344xpath_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 +02004345{
4346 if (options & LYXP_SCNODE_ALL) {
4347 set_scnode_clear_ctx(set);
4348 return LY_SUCCESS;
4349 }
4350
4351 if (set->type != LYXP_SET_NODE_SET) {
Michal Vaskod3678892020-05-21 10:06:58 +02004352 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004353 }
4354 return LY_SUCCESS;
4355}
4356
4357/**
4358 * @brief Execute the XPath normalize-space(string?) function. Returns LYXP_SET_STRING
4359 * with normalized value (no leading, trailing, double white spaces) of the node
4360 * from the argument or the context.
4361 *
4362 * @param[in] args Array of arguments.
4363 * @param[in] arg_count Count of elements in @p args.
4364 * @param[in,out] set Context and result set at the same time.
4365 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004366 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004367 */
4368static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004369xpath_normalize_space(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004370{
4371 uint16_t i, new_used;
4372 char *new;
Radek Krejci857189e2020-09-01 13:26:36 +02004373 ly_bool have_spaces = 0, space_before = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004374 struct lysc_node_leaf *sleaf;
4375 LY_ERR rc = LY_SUCCESS;
4376
4377 if (options & LYXP_SCNODE_ALL) {
4378 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4379 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4380 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 +02004381 } else if (!warn_is_string_type(sleaf->type)) {
4382 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004383 }
4384 }
4385 set_scnode_clear_ctx(set);
4386 return rc;
4387 }
4388
4389 if (arg_count) {
4390 set_fill_set(set, args[0]);
4391 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004392 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004393 LY_CHECK_RET(rc);
4394
4395 /* is there any normalization necessary? */
4396 for (i = 0; set->val.str[i]; ++i) {
4397 if (is_xmlws(set->val.str[i])) {
4398 if ((i == 0) || space_before || (!set->val.str[i + 1])) {
4399 have_spaces = 1;
4400 break;
4401 }
4402 space_before = 1;
4403 } else {
4404 space_before = 0;
4405 }
4406 }
4407
4408 /* yep, there is */
4409 if (have_spaces) {
4410 /* it's enough, at least one character will go, makes space for ending '\0' */
4411 new = malloc(strlen(set->val.str) * sizeof(char));
4412 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4413 new_used = 0;
4414
4415 space_before = 0;
4416 for (i = 0; set->val.str[i]; ++i) {
4417 if (is_xmlws(set->val.str[i])) {
4418 if ((i == 0) || space_before) {
4419 space_before = 1;
4420 continue;
4421 } else {
4422 space_before = 1;
4423 }
4424 } else {
4425 space_before = 0;
4426 }
4427
4428 new[new_used] = (space_before ? ' ' : set->val.str[i]);
4429 ++new_used;
4430 }
4431
4432 /* at worst there is one trailing space now */
4433 if (new_used && is_xmlws(new[new_used - 1])) {
4434 --new_used;
4435 }
4436
4437 new = ly_realloc(new, (new_used + 1) * sizeof(char));
4438 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4439 new[new_used] = '\0';
4440
4441 free(set->val.str);
4442 set->val.str = new;
4443 }
4444
4445 return LY_SUCCESS;
4446}
4447
4448/**
4449 * @brief Execute the XPath not(boolean) function. Returns LYXP_SET_BOOLEAN
4450 * with the argument converted to boolean and logically inverted.
4451 *
4452 * @param[in] args Array of arguments.
4453 * @param[in] arg_count Count of elements in @p args.
4454 * @param[in,out] set Context and result set at the same time.
4455 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004456 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004457 */
4458static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004459xpath_not(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004460{
4461 if (options & LYXP_SCNODE_ALL) {
4462 set_scnode_clear_ctx(set);
4463 return LY_SUCCESS;
4464 }
4465
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004466 lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02004467 if (args[0]->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004468 set_fill_boolean(set, 0);
4469 } else {
4470 set_fill_boolean(set, 1);
4471 }
4472
4473 return LY_SUCCESS;
4474}
4475
4476/**
4477 * @brief Execute the XPath number(object?) function. Returns LYXP_SET_NUMBER
4478 * with the number representation of either the argument or the context.
4479 *
4480 * @param[in] args Array of arguments.
4481 * @param[in] arg_count Count of elements in @p args.
4482 * @param[in,out] set Context and result set at the same time.
4483 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004484 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004485 */
4486static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004487xpath_number(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004488{
4489 LY_ERR rc;
4490
4491 if (options & LYXP_SCNODE_ALL) {
4492 set_scnode_clear_ctx(set);
4493 return LY_SUCCESS;
4494 }
4495
4496 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004497 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004498 LY_CHECK_RET(rc);
4499 set_fill_set(set, args[0]);
4500 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004501 rc = lyxp_set_cast(set, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004502 LY_CHECK_RET(rc);
4503 }
4504
4505 return LY_SUCCESS;
4506}
4507
4508/**
4509 * @brief Execute the XPath position() function. Returns LYXP_SET_NUMBER
4510 * with the context position.
4511 *
4512 * @param[in] args Array of arguments.
4513 * @param[in] arg_count Count of elements in @p args.
4514 * @param[in,out] set Context and result set at the same time.
4515 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004516 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004517 */
4518static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004519xpath_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 +02004520{
4521 if (options & LYXP_SCNODE_ALL) {
4522 set_scnode_clear_ctx(set);
4523 return LY_SUCCESS;
4524 }
4525
Michal Vasko03ff5a72019-09-11 13:49:33 +02004526 if (set->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004527 LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INCTX, print_set_type(set), "position()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004528 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004529 } else if (!set->used) {
4530 set_fill_number(set, 0);
4531 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004532 }
4533
4534 set_fill_number(set, set->ctx_pos);
4535
4536 /* UNUSED in 'Release' build type */
4537 (void)options;
4538 return LY_SUCCESS;
4539}
4540
4541/**
4542 * @brief Execute the YANG 1.1 re-match(string, string) function. Returns LYXP_SET_BOOLEAN
4543 * depending on whether the second argument regex matches the first argument string. For details refer to
4544 * YANG 1.1 RFC section 10.2.1.
4545 *
4546 * @param[in] args Array of arguments.
4547 * @param[in] arg_count Count of elements in @p args.
4548 * @param[in,out] set Context and result set at the same time.
4549 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004550 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004551 */
4552static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004553xpath_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 +02004554{
4555 struct lysc_pattern **patterns = NULL, **pattern;
4556 struct lysc_node_leaf *sleaf;
4557 char *path;
4558 LY_ERR rc = LY_SUCCESS;
4559 struct ly_err_item *err;
4560
4561 if (options & LYXP_SCNODE_ALL) {
4562 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4563 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4564 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 +02004565 } else if (!warn_is_string_type(sleaf->type)) {
4566 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004567 }
4568 }
4569
4570 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4571 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4572 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 +02004573 } else if (!warn_is_string_type(sleaf->type)) {
4574 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004575 }
4576 }
4577 set_scnode_clear_ctx(set);
4578 return rc;
4579 }
4580
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004581 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004582 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004583 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004584 LY_CHECK_RET(rc);
4585
4586 LY_ARRAY_NEW_RET(set->ctx, patterns, pattern, LY_EMEM);
4587 *pattern = malloc(sizeof **pattern);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004588 path = lyd_path(set->cur_node, LYD_PATH_LOG, NULL, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004589 rc = lys_compile_type_pattern_check(set->ctx, path, args[1]->val.str, &(*pattern)->code);
4590 free(path);
4591 if (rc != LY_SUCCESS) {
4592 LY_ARRAY_FREE(patterns);
4593 return rc;
4594 }
4595
4596 rc = ly_type_validate_patterns(patterns, args[0]->val.str, strlen(args[0]->val.str), &err);
4597 pcre2_code_free((*pattern)->code);
4598 free(*pattern);
4599 LY_ARRAY_FREE(patterns);
4600 if (rc && (rc != LY_EVALID)) {
4601 ly_err_print(err);
4602 ly_err_free(err);
4603 return rc;
4604 }
4605
4606 if (rc == LY_EVALID) {
4607 ly_err_free(err);
4608 set_fill_boolean(set, 0);
4609 } else {
4610 set_fill_boolean(set, 1);
4611 }
4612
4613 return LY_SUCCESS;
4614}
4615
4616/**
4617 * @brief Execute the XPath round(number) function. Returns LYXP_SET_NUMBER
4618 * with the rounded first argument. For details refer to
4619 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-round.
4620 *
4621 * @param[in] args Array of arguments.
4622 * @param[in] arg_count Count of elements in @p args.
4623 * @param[in,out] set Context and result set at the same time.
4624 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004625 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004626 */
4627static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004628xpath_round(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004629{
4630 struct lysc_node_leaf *sleaf;
4631 LY_ERR rc = LY_SUCCESS;
4632
4633 if (options & LYXP_SCNODE_ALL) {
4634 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4635 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004636 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4637 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 +02004638 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
4639 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004640 }
4641 set_scnode_clear_ctx(set);
4642 return rc;
4643 }
4644
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004645 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004646 LY_CHECK_RET(rc);
4647
4648 /* cover only the cases where floor can't be used */
4649 if ((args[0]->val.num == -0.0f) || ((args[0]->val.num < 0) && (args[0]->val.num >= -0.5))) {
4650 set_fill_number(set, -0.0f);
4651 } else {
4652 args[0]->val.num += 0.5;
4653 rc = xpath_floor(args, 1, args[0], options);
4654 LY_CHECK_RET(rc);
4655 set_fill_number(set, args[0]->val.num);
4656 }
4657
4658 return LY_SUCCESS;
4659}
4660
4661/**
4662 * @brief Execute the XPath starts-with(string, string) function.
4663 * Returns LYXP_SET_BOOLEAN whether the second argument is
4664 * the prefix of the first or not.
4665 *
4666 * @param[in] args Array of arguments.
4667 * @param[in] arg_count Count of elements in @p args.
4668 * @param[in,out] set Context and result set at the same time.
4669 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004670 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004671 */
4672static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004673xpath_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 +02004674{
4675 struct lysc_node_leaf *sleaf;
4676 LY_ERR rc = LY_SUCCESS;
4677
4678 if (options & LYXP_SCNODE_ALL) {
4679 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4680 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4681 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 +02004682 } else if (!warn_is_string_type(sleaf->type)) {
4683 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004684 }
4685 }
4686
4687 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4688 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4689 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 +02004690 } else if (!warn_is_string_type(sleaf->type)) {
4691 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004692 }
4693 }
4694 set_scnode_clear_ctx(set);
4695 return rc;
4696 }
4697
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004698 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004699 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004700 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004701 LY_CHECK_RET(rc);
4702
4703 if (strncmp(args[0]->val.str, args[1]->val.str, strlen(args[1]->val.str))) {
4704 set_fill_boolean(set, 0);
4705 } else {
4706 set_fill_boolean(set, 1);
4707 }
4708
4709 return LY_SUCCESS;
4710}
4711
4712/**
4713 * @brief Execute the XPath string(object?) function. Returns LYXP_SET_STRING
4714 * with the string representation of either the argument or the context.
4715 *
4716 * @param[in] args Array of arguments.
4717 * @param[in] arg_count Count of elements in @p args.
4718 * @param[in,out] set Context and result set at the same time.
4719 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004720 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004721 */
4722static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004723xpath_string(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004724{
4725 LY_ERR rc;
4726
4727 if (options & LYXP_SCNODE_ALL) {
4728 set_scnode_clear_ctx(set);
4729 return LY_SUCCESS;
4730 }
4731
4732 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004733 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004734 LY_CHECK_RET(rc);
4735 set_fill_set(set, args[0]);
4736 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004737 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004738 LY_CHECK_RET(rc);
4739 }
4740
4741 return LY_SUCCESS;
4742}
4743
4744/**
4745 * @brief Execute the XPath string-length(string?) function. Returns LYXP_SET_NUMBER
4746 * with the length of the string in either the argument or the context.
4747 *
4748 * @param[in] args Array of arguments.
4749 * @param[in] arg_count Count of elements in @p args.
4750 * @param[in,out] set Context and result set at the same time.
4751 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004752 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004753 */
4754static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004755xpath_string_length(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004756{
4757 struct lysc_node_leaf *sleaf;
4758 LY_ERR rc = LY_SUCCESS;
4759
4760 if (options & LYXP_SCNODE_ALL) {
4761 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4762 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4763 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 +02004764 } else if (!warn_is_string_type(sleaf->type)) {
4765 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004766 }
4767 }
4768 if (!arg_count && (set->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set))) {
4769 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4770 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 +02004771 } else if (!warn_is_string_type(sleaf->type)) {
4772 LOGWRN(set->ctx, "Argument #0 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004773 }
4774 }
4775 set_scnode_clear_ctx(set);
4776 return rc;
4777 }
4778
4779 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004780 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004781 LY_CHECK_RET(rc);
4782 set_fill_number(set, strlen(args[0]->val.str));
4783 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004784 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004785 LY_CHECK_RET(rc);
4786 set_fill_number(set, strlen(set->val.str));
4787 }
4788
4789 return LY_SUCCESS;
4790}
4791
4792/**
4793 * @brief Execute the XPath substring(string, number, number?) function.
4794 * Returns LYXP_SET_STRING substring of the first argument starting
4795 * on the second argument index ending on the third argument index,
4796 * indexed from 1. For exact definition refer to
4797 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-substring.
4798 *
4799 * @param[in] args Array of arguments.
4800 * @param[in] arg_count Count of elements in @p args.
4801 * @param[in,out] set Context and result set at the same time.
4802 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004803 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004804 */
4805static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004806xpath_substring(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004807{
Radek Krejci1deb5be2020-08-26 16:43:36 +02004808 int32_t start, len;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004809 uint16_t str_start, str_len, pos;
4810 struct lysc_node_leaf *sleaf;
4811 LY_ERR rc = LY_SUCCESS;
4812
4813 if (options & LYXP_SCNODE_ALL) {
4814 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4815 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4816 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 +02004817 } else if (!warn_is_string_type(sleaf->type)) {
4818 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004819 }
4820 }
4821
4822 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4823 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4824 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 +02004825 } else if (!warn_is_numeric_type(sleaf->type)) {
4826 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004827 }
4828 }
4829
Michal Vasko69730152020-10-09 16:30:07 +02004830 if ((arg_count == 3) && (args[2]->type == LYXP_SET_SCNODE_SET) &&
4831 (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004832 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4833 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 +02004834 } else if (!warn_is_numeric_type(sleaf->type)) {
4835 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004836 }
4837 }
4838 set_scnode_clear_ctx(set);
4839 return rc;
4840 }
4841
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004842 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004843 LY_CHECK_RET(rc);
4844
4845 /* start */
4846 if (xpath_round(&args[1], 1, args[1], options)) {
4847 return -1;
4848 }
4849 if (isfinite(args[1]->val.num)) {
4850 start = args[1]->val.num - 1;
4851 } else if (isinf(args[1]->val.num) && signbit(args[1]->val.num)) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004852 start = INT32_MIN;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004853 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004854 start = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004855 }
4856
4857 /* len */
4858 if (arg_count == 3) {
4859 rc = xpath_round(&args[2], 1, args[2], options);
4860 LY_CHECK_RET(rc);
Radek Krejci1deb5be2020-08-26 16:43:36 +02004861 if (isnan(args[2]->val.num) || signbit(args[2]->val.num)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004862 len = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02004863 } else if (isfinite(args[2]->val.num)) {
4864 len = args[2]->val.num;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004865 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004866 len = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004867 }
4868 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004869 len = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004870 }
4871
4872 /* find matching character positions */
4873 str_start = 0;
4874 str_len = 0;
4875 for (pos = 0; args[0]->val.str[pos]; ++pos) {
4876 if (pos < start) {
4877 ++str_start;
4878 } else if (pos < start + len) {
4879 ++str_len;
4880 } else {
4881 break;
4882 }
4883 }
4884
4885 set_fill_string(set, args[0]->val.str + str_start, str_len);
4886 return LY_SUCCESS;
4887}
4888
4889/**
4890 * @brief Execute the XPath substring-after(string, string) function.
4891 * Returns LYXP_SET_STRING with the string succeeding the occurance
4892 * of the second argument in the first or an empty string.
4893 *
4894 * @param[in] args Array of arguments.
4895 * @param[in] arg_count Count of elements in @p args.
4896 * @param[in,out] set Context and result set at the same time.
4897 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004898 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004899 */
4900static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004901xpath_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 +02004902{
4903 char *ptr;
4904 struct lysc_node_leaf *sleaf;
4905 LY_ERR rc = LY_SUCCESS;
4906
4907 if (options & LYXP_SCNODE_ALL) {
4908 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4909 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4910 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 +02004911 } else if (!warn_is_string_type(sleaf->type)) {
4912 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004913 }
4914 }
4915
4916 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4917 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4918 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 +02004919 } else if (!warn_is_string_type(sleaf->type)) {
4920 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004921 }
4922 }
4923 set_scnode_clear_ctx(set);
4924 return rc;
4925 }
4926
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004927 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004928 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004929 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004930 LY_CHECK_RET(rc);
4931
4932 ptr = strstr(args[0]->val.str, args[1]->val.str);
4933 if (ptr) {
4934 set_fill_string(set, ptr + strlen(args[1]->val.str), strlen(ptr + strlen(args[1]->val.str)));
4935 } else {
4936 set_fill_string(set, "", 0);
4937 }
4938
4939 return LY_SUCCESS;
4940}
4941
4942/**
4943 * @brief Execute the XPath substring-before(string, string) function.
4944 * Returns LYXP_SET_STRING with the string preceding the occurance
4945 * of the second argument in the first or an empty string.
4946 *
4947 * @param[in] args Array of arguments.
4948 * @param[in] arg_count Count of elements in @p args.
4949 * @param[in,out] set Context and result set at the same time.
4950 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004951 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004952 */
4953static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004954xpath_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 +02004955{
4956 char *ptr;
4957 struct lysc_node_leaf *sleaf;
4958 LY_ERR rc = LY_SUCCESS;
4959
4960 if (options & LYXP_SCNODE_ALL) {
4961 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4962 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4963 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 +02004964 } else if (!warn_is_string_type(sleaf->type)) {
4965 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004966 }
4967 }
4968
4969 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4970 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4971 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 +02004972 } else if (!warn_is_string_type(sleaf->type)) {
4973 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004974 }
4975 }
4976 set_scnode_clear_ctx(set);
4977 return rc;
4978 }
4979
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004980 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004981 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004982 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004983 LY_CHECK_RET(rc);
4984
4985 ptr = strstr(args[0]->val.str, args[1]->val.str);
4986 if (ptr) {
4987 set_fill_string(set, args[0]->val.str, ptr - args[0]->val.str);
4988 } else {
4989 set_fill_string(set, "", 0);
4990 }
4991
4992 return LY_SUCCESS;
4993}
4994
4995/**
4996 * @brief Execute the XPath sum(node-set) function. Returns LYXP_SET_NUMBER
4997 * with the sum of all the nodes in the context.
4998 *
4999 * @param[in] args Array of arguments.
5000 * @param[in] arg_count Count of elements in @p args.
5001 * @param[in,out] set Context and result set at the same time.
5002 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005003 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005004 */
5005static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005006xpath_sum(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005007{
5008 long double num;
5009 char *str;
5010 uint16_t i;
5011 struct lyxp_set set_item;
5012 struct lysc_node_leaf *sleaf;
5013 LY_ERR rc = LY_SUCCESS;
5014
5015 if (options & LYXP_SCNODE_ALL) {
5016 if (args[0]->type == LYXP_SET_SCNODE_SET) {
5017 for (i = 0; i < args[0]->used; ++i) {
5018 if (args[0]->val.scnodes[i].in_ctx == 1) {
5019 sleaf = (struct lysc_node_leaf *)args[0]->val.scnodes[i].scnode;
5020 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5021 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__,
Michal Vasko69730152020-10-09 16:30:07 +02005022 lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005023 } else if (!warn_is_numeric_type(sleaf->type)) {
5024 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005025 }
5026 }
5027 }
5028 }
5029 set_scnode_clear_ctx(set);
5030 return rc;
5031 }
5032
5033 set_fill_number(set, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005034
5035 if (args[0]->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005036 LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "sum(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02005037 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02005038 } else if (!args[0]->used) {
5039 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005040 }
5041
Michal Vasko5c4e5892019-11-14 12:31:38 +01005042 set_init(&set_item, set);
5043
Michal Vasko03ff5a72019-09-11 13:49:33 +02005044 set_item.type = LYXP_SET_NODE_SET;
5045 set_item.val.nodes = malloc(sizeof *set_item.val.nodes);
5046 LY_CHECK_ERR_RET(!set_item.val.nodes, LOGMEM(set->ctx), LY_EMEM);
5047
5048 set_item.used = 1;
5049 set_item.size = 1;
5050
5051 for (i = 0; i < args[0]->used; ++i) {
5052 set_item.val.nodes[0] = args[0]->val.nodes[i];
5053
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005054 rc = cast_node_set_to_string(&set_item, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005055 LY_CHECK_RET(rc);
5056 num = cast_string_to_number(str);
5057 free(str);
5058 set->val.num += num;
5059 }
5060
5061 free(set_item.val.nodes);
5062
5063 return LY_SUCCESS;
5064}
5065
5066/**
5067 * @brief Execute the XPath text() function (node type). Returns LYXP_SET_NODE_SET
5068 * with the text content of the nodes in the context.
5069 *
5070 * @param[in] args Array of arguments.
5071 * @param[in] arg_count Count of elements in @p args.
5072 * @param[in,out] set Context and result set at the same time.
5073 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005074 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005075 */
5076static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005077xpath_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 +02005078{
5079 uint32_t i;
5080
5081 if (options & LYXP_SCNODE_ALL) {
5082 set_scnode_clear_ctx(set);
5083 return LY_SUCCESS;
5084 }
5085
Michal Vasko03ff5a72019-09-11 13:49:33 +02005086 if (set->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005087 LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INCTX, print_set_type(set), "text()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02005088 return LY_EVALID;
5089 }
5090
Michal Vaskod989ba02020-08-24 10:59:24 +02005091 for (i = 0; i < set->used; ) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005092 switch (set->val.nodes[i].type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01005093 case LYXP_NODE_NONE:
5094 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005095 case LYXP_NODE_ELEM:
Michal Vasko03ff5a72019-09-11 13:49:33 +02005096 if (set->val.nodes[i].node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
5097 set->val.nodes[i].type = LYXP_NODE_TEXT;
5098 ++i;
5099 break;
5100 }
Radek Krejci0f969882020-08-21 16:56:47 +02005101 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005102 case LYXP_NODE_ROOT:
5103 case LYXP_NODE_ROOT_CONFIG:
5104 case LYXP_NODE_TEXT:
Michal Vasko9f96a052020-03-10 09:41:45 +01005105 case LYXP_NODE_META:
Michal Vasko03ff5a72019-09-11 13:49:33 +02005106 set_remove_node(set, i);
5107 break;
5108 }
5109 }
5110
5111 return LY_SUCCESS;
5112}
5113
5114/**
5115 * @brief Execute the XPath translate(string, string, string) function.
5116 * Returns LYXP_SET_STRING with the first argument with the characters
5117 * from the second argument replaced by those on the corresponding
5118 * positions in the third argument.
5119 *
5120 * @param[in] args Array of arguments.
5121 * @param[in] arg_count Count of elements in @p args.
5122 * @param[in,out] set Context and result set at the same time.
5123 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005124 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005125 */
5126static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005127xpath_translate(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005128{
5129 uint16_t i, j, new_used;
5130 char *new;
Radek Krejci857189e2020-09-01 13:26:36 +02005131 ly_bool have_removed;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005132 struct lysc_node_leaf *sleaf;
5133 LY_ERR rc = LY_SUCCESS;
5134
5135 if (options & LYXP_SCNODE_ALL) {
5136 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5137 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5138 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 +02005139 } else if (!warn_is_string_type(sleaf->type)) {
5140 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005141 }
5142 }
5143
5144 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5145 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5146 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 +02005147 } else if (!warn_is_string_type(sleaf->type)) {
5148 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005149 }
5150 }
5151
5152 if ((args[2]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
5153 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5154 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 +02005155 } else if (!warn_is_string_type(sleaf->type)) {
5156 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005157 }
5158 }
5159 set_scnode_clear_ctx(set);
5160 return rc;
5161 }
5162
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005163 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005164 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005165 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005166 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005167 rc = lyxp_set_cast(args[2], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005168 LY_CHECK_RET(rc);
5169
5170 new = malloc((strlen(args[0]->val.str) + 1) * sizeof(char));
5171 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5172 new_used = 0;
5173
5174 have_removed = 0;
5175 for (i = 0; args[0]->val.str[i]; ++i) {
Radek Krejci857189e2020-09-01 13:26:36 +02005176 ly_bool found = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005177
5178 for (j = 0; args[1]->val.str[j]; ++j) {
5179 if (args[0]->val.str[i] == args[1]->val.str[j]) {
5180 /* removing this char */
5181 if (j >= strlen(args[2]->val.str)) {
5182 have_removed = 1;
5183 found = 1;
5184 break;
5185 }
5186 /* replacing this char */
5187 new[new_used] = args[2]->val.str[j];
5188 ++new_used;
5189 found = 1;
5190 break;
5191 }
5192 }
5193
5194 /* copying this char */
5195 if (!found) {
5196 new[new_used] = args[0]->val.str[i];
5197 ++new_used;
5198 }
5199 }
5200
5201 if (have_removed) {
5202 new = ly_realloc(new, (new_used + 1) * sizeof(char));
5203 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5204 }
5205 new[new_used] = '\0';
5206
Michal Vaskod3678892020-05-21 10:06:58 +02005207 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005208 set->type = LYXP_SET_STRING;
5209 set->val.str = new;
5210
5211 return LY_SUCCESS;
5212}
5213
5214/**
5215 * @brief Execute the XPath true() function. Returns LYXP_SET_BOOLEAN
5216 * with true value.
5217 *
5218 * @param[in] args Array of arguments.
5219 * @param[in] arg_count Count of elements in @p args.
5220 * @param[in,out] set Context and result set at the same time.
5221 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005222 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005223 */
5224static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005225xpath_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 +02005226{
5227 if (options & LYXP_SCNODE_ALL) {
5228 set_scnode_clear_ctx(set);
5229 return LY_SUCCESS;
5230 }
5231
5232 set_fill_boolean(set, 1);
5233 return LY_SUCCESS;
5234}
5235
5236/*
5237 * moveto functions
5238 *
5239 * They and only they actually change the context (set).
5240 */
5241
5242/**
Michal Vasko6346ece2019-09-24 13:12:53 +02005243 * @brief Skip prefix and return corresponding model if there is a prefix. Logs directly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005244 *
Michal Vasko2104e9f2020-03-06 08:23:25 +01005245 * XPath @p set is expected to be a (sc)node set!
5246 *
Michal Vasko6346ece2019-09-24 13:12:53 +02005247 * @param[in,out] qname Qualified node name. If includes prefix, it is skipped.
5248 * @param[in,out] qname_len Length of @p qname, is updated accordingly.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005249 * @param[in] set Set with general XPath context.
5250 * @param[in] ctx_scnode Context node to inherit module for unprefixed node for ::LY_PREF_JSON.
Michal Vasko6346ece2019-09-24 13:12:53 +02005251 * @param[out] moveto_mod Expected module of a matching node.
5252 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005253 */
Michal Vasko6346ece2019-09-24 13:12:53 +02005254static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005255moveto_resolve_model(const char **qname, uint16_t *qname_len, const struct lyxp_set *set,
5256 const struct lysc_node *ctx_scnode, const struct lys_module **moveto_mod)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005257{
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02005258 const struct lys_module *mod = NULL;
Michal Vasko6346ece2019-09-24 13:12:53 +02005259 const char *ptr;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005260 size_t pref_len;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005261
Michal Vasko2104e9f2020-03-06 08:23:25 +01005262 assert((set->type == LYXP_SET_NODE_SET) || (set->type == LYXP_SET_SCNODE_SET));
5263
Michal Vasko6346ece2019-09-24 13:12:53 +02005264 if ((ptr = ly_strnchr(*qname, ':', *qname_len))) {
5265 /* specific module */
5266 pref_len = ptr - *qname;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005267 mod = ly_resolve_prefix(set->ctx, *qname, pref_len, set->format, set->prefix_data);
Michal Vasko6346ece2019-09-24 13:12:53 +02005268
Michal Vasko004d3152020-06-11 19:59:22 +02005269 /* check for errors and non-implemented modules, as they are not valid */
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005270 if (!mod || !mod->implemented) {
Michal Vasko2104e9f2020-03-06 08:23:25 +01005271 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005272 LOGVAL(set->ctx, LY_VLOG_LYSC, set->cur_scnode, LY_VCODE_XP_INMOD, pref_len, *qname);
Michal Vasko2104e9f2020-03-06 08:23:25 +01005273 } else {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005274 LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INMOD, pref_len, *qname);
Michal Vasko2104e9f2020-03-06 08:23:25 +01005275 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005276 return LY_EVALID;
5277 }
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005278
Michal Vasko6346ece2019-09-24 13:12:53 +02005279 *qname += pref_len + 1;
5280 *qname_len -= pref_len + 1;
5281 } else if (((*qname)[0] == '*') && (*qname_len == 1)) {
5282 /* all modules - special case */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005283 mod = NULL;
Michal Vasko6346ece2019-09-24 13:12:53 +02005284 } else {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005285 switch (set->format) {
5286 case LY_PREF_SCHEMA:
5287 case LY_PREF_SCHEMA_RESOLVED:
5288 /* current module */
5289 mod = set->cur_mod;
5290 break;
5291 case LY_PREF_JSON:
5292 /* inherit parent (context node) module */
5293 if (ctx_scnode) {
5294 mod = ctx_scnode->module;
5295 } else {
5296 mod = NULL;
5297 }
5298 break;
5299 case LY_PREF_XML:
5300 /* not defined */
5301 LOGINT_RET(set->ctx);
5302 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005303 }
5304
Michal Vasko6346ece2019-09-24 13:12:53 +02005305 *moveto_mod = mod;
5306 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005307}
5308
5309/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02005310 * @brief Move context @p set to the root. Handles absolute path.
5311 * Result is LYXP_SET_NODE_SET.
5312 *
5313 * @param[in,out] set Set to use.
5314 * @param[in] options Xpath options.
Michal Vaskob0099a92020-08-31 14:55:23 +02005315 * @return LY_ERR value.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005316 */
Michal Vaskob0099a92020-08-31 14:55:23 +02005317static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005318moveto_root(struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005319{
Michal Vasko03ff5a72019-09-11 13:49:33 +02005320 if (!set) {
Michal Vaskob0099a92020-08-31 14:55:23 +02005321 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005322 }
5323
5324 if (options & LYXP_SCNODE_ALL) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005325 set_scnode_clear_ctx(set);
Michal Vaskob0099a92020-08-31 14:55:23 +02005326 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, NULL, set->root_type, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005327 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005328 set->type = LYXP_SET_NODE_SET;
5329 set->used = 0;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005330 set_insert_node(set, NULL, 0, set->root_type, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005331 }
Michal Vaskob0099a92020-08-31 14:55:23 +02005332
5333 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005334}
5335
5336/**
Michal Vaskoa1424542019-11-14 16:08:52 +01005337 * @brief Check whether a node has some unresolved "when".
5338 *
5339 * @param[in] node Node to check.
5340 * @return LY_ERR value (LY_EINCOMPLETE if there are some unresolved "when")
5341 */
5342static LY_ERR
5343moveto_when_check(const struct lyd_node *node)
5344{
5345 const struct lysc_node *schema;
5346
5347 if (!node) {
5348 return LY_SUCCESS;
5349 }
5350
5351 schema = node->schema;
5352 do {
5353 if (schema->when && !(node->flags & LYD_WHEN_TRUE)) {
5354 return LY_EINCOMPLETE;
5355 }
5356 schema = schema->parent;
5357 } while (schema && (schema->nodetype & (LYS_CASE | LYS_CHOICE)));
5358
5359 return LY_SUCCESS;
5360}
5361
5362/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02005363 * @brief Check @p node as a part of NameTest processing.
5364 *
5365 * @param[in] node Node to check.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005366 * @param[in] ctx_node Context node.
5367 * @param[in] set Set to read general context from.
Michal Vaskod3678892020-05-21 10:06:58 +02005368 * @param[in] node_name Node name in the dictionary to move to, NULL for any node.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005369 * @param[in] moveto_mod Expected module of the node, NULL for no prefix.
Michal Vasko6346ece2019-09-24 13:12:53 +02005370 * @return LY_ERR (LY_ENOT if node does not match, LY_EINCOMPLETE on unresolved when,
5371 * LY_EINVAL if netither node nor any children match)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005372 */
5373static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005374moveto_node_check(const struct lyd_node *node, const struct lyd_node *ctx_node, const struct lyxp_set *set,
Radek Krejci0f969882020-08-21 16:56:47 +02005375 const char *node_name, const struct lys_module *moveto_mod)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005376{
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005377 if (!moveto_mod && (!node_name || strcmp(node_name, "*"))) {
5378 switch (set->format) {
5379 case LY_PREF_SCHEMA:
5380 case LY_PREF_SCHEMA_RESOLVED:
5381 /* use current module */
5382 moveto_mod = set->cur_mod;
5383 break;
5384 case LY_PREF_JSON:
5385 /* inherit module of the context node, if any */
5386 if (ctx_node) {
5387 moveto_mod = ctx_node->schema->module;
5388 }
5389 break;
5390 case LY_PREF_XML:
5391 /* not defined */
5392 LOGINT(set->ctx);
5393 return LY_EINVAL;
5394 }
5395 }
5396
Michal Vasko03ff5a72019-09-11 13:49:33 +02005397 /* module check */
5398 if (moveto_mod && (node->schema->module != moveto_mod)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005399 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005400 }
5401
Michal Vasko5c4e5892019-11-14 12:31:38 +01005402 /* context check */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005403 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005404 return LY_EINVAL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005405 } else if (set->context_op && (node->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) &&
5406 (node->schema != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005407 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005408 }
5409
5410 /* name check */
Michal Vasko61ac2f62020-05-25 12:39:51 +02005411 if (node_name && strcmp(node_name, "*") && (node->schema->name != node_name)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005412 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005413 }
5414
Michal Vaskoa1424542019-11-14 16:08:52 +01005415 /* when check */
5416 if (moveto_when_check(node)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005417 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01005418 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005419
5420 /* match */
5421 return LY_SUCCESS;
5422}
5423
5424/**
5425 * @brief Check @p node as a part of schema NameTest processing.
5426 *
5427 * @param[in] node Schema node to check.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005428 * @param[in] ctx_scnode Context node.
5429 * @param[in] set Set to read general context from.
Michal Vaskod3678892020-05-21 10:06:58 +02005430 * @param[in] node_name Node name in the dictionary to move to, NULL for any nodes.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005431 * @param[in] moveto_mod Expected module of the node, NULL for no prefix.
Michal Vasko6346ece2019-09-24 13:12:53 +02005432 * @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 +02005433 */
5434static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005435moveto_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 +02005436 const char *node_name, const struct lys_module *moveto_mod)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005437{
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005438 if (!moveto_mod && (!node_name || strcmp(node_name, "*"))) {
5439 switch (set->format) {
5440 case LY_PREF_SCHEMA:
5441 case LY_PREF_SCHEMA_RESOLVED:
5442 /* use current module */
5443 moveto_mod = set->cur_mod;
5444 break;
5445 case LY_PREF_JSON:
5446 /* inherit module of the context node, if any */
5447 if (ctx_scnode) {
5448 moveto_mod = ctx_scnode->module;
5449 }
5450 break;
5451 case LY_PREF_XML:
5452 /* not defined */
5453 LOGINT(set->ctx);
5454 return LY_EINVAL;
5455 }
5456 }
5457
Michal Vasko03ff5a72019-09-11 13:49:33 +02005458 /* module check */
Michal Vaskod3678892020-05-21 10:06:58 +02005459 if (moveto_mod && (node->module != moveto_mod)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005460 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005461 }
5462
5463 /* context check */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005464 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (node->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005465 return LY_EINVAL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005466 } else if (set->context_op && (node->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && (node != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005467 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005468 }
5469
5470 /* name check */
Michal Vasko61ac2f62020-05-25 12:39:51 +02005471 if (node_name && strcmp(node_name, "*") && (node->name != node_name)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005472 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005473 }
5474
5475 /* match */
5476 return LY_SUCCESS;
5477}
5478
5479/**
Michal Vaskod3678892020-05-21 10:06:58 +02005480 * @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 +02005481 *
5482 * @param[in,out] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005483 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005484 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005485 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5486 */
5487static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005488moveto_node(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005489{
Michal Vaskof03ed032020-03-04 13:31:44 +01005490 uint32_t i;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005491 const struct lyd_node *siblings, *sub, *ctx_node;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005492 LY_ERR rc;
5493
Michal Vaskod3678892020-05-21 10:06:58 +02005494 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005495 return LY_SUCCESS;
5496 }
5497
5498 if (set->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005499 LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005500 return LY_EVALID;
5501 }
5502
Michal Vasko03ff5a72019-09-11 13:49:33 +02005503 for (i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02005504 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005505
5506 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 +01005507 assert(!set->val.nodes[i].node);
Michal Vaskod3678892020-05-21 10:06:58 +02005508
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005509 /* search in all the trees */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005510 ctx_node = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02005511 siblings = set->tree;
Michal Vasko5bfd4be2020-06-23 13:26:19 +02005512 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005513 /* search in children */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005514 ctx_node = set->val.nodes[i].node;
5515 siblings = lyd_child(ctx_node);
Michal Vaskod3678892020-05-21 10:06:58 +02005516 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005517
Michal Vaskod3678892020-05-21 10:06:58 +02005518 for (sub = siblings; sub; sub = sub->next) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005519 rc = moveto_node_check(sub, ctx_node, set, ncname, moveto_mod);
Michal Vaskod3678892020-05-21 10:06:58 +02005520 if (rc == LY_SUCCESS) {
5521 if (!replaced) {
5522 set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i);
5523 replaced = 1;
5524 } else {
5525 set_insert_node(set, sub, 0, LYXP_NODE_ELEM, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005526 }
Michal Vaskod3678892020-05-21 10:06:58 +02005527 ++i;
5528 } else if (rc == LY_EINCOMPLETE) {
5529 return rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005530 }
5531 }
5532
5533 if (!replaced) {
5534 /* no match */
5535 set_remove_node(set, i);
5536 }
5537 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005538
5539 return LY_SUCCESS;
5540}
5541
5542/**
Michal Vaskod3678892020-05-21 10:06:58 +02005543 * @brief Move context @p set to a node using hashes. Result is LYXP_SET_NODE_SET (or LYXP_SET_EMPTY).
5544 * Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005545 *
5546 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005547 * @param[in] scnode Matching node schema.
Michal Vasko004d3152020-06-11 19:59:22 +02005548 * @param[in] predicates If @p scnode is ::LYS_LIST or ::LYS_LEAFLIST, the predicates specifying a single instance.
Michal Vaskod3678892020-05-21 10:06:58 +02005549 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5550 */
5551static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02005552moveto_node_hash(struct lyxp_set *set, const struct lysc_node *scnode, const struct ly_path_predicate *predicates)
Michal Vaskod3678892020-05-21 10:06:58 +02005553{
Michal Vasko004d3152020-06-11 19:59:22 +02005554 LY_ERR ret = LY_SUCCESS;
Michal Vaskod3678892020-05-21 10:06:58 +02005555 uint32_t i;
Michal Vaskod3678892020-05-21 10:06:58 +02005556 const struct lyd_node *siblings;
Michal Vasko004d3152020-06-11 19:59:22 +02005557 struct lyd_node *sub, *inst = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02005558
Michal Vasko004d3152020-06-11 19:59:22 +02005559 assert(scnode && (!(scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)) || predicates));
Michal Vaskod3678892020-05-21 10:06:58 +02005560
5561 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02005562 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005563 }
5564
5565 if (set->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005566 LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko004d3152020-06-11 19:59:22 +02005567 ret = LY_EVALID;
5568 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005569 }
5570
5571 /* context check for all the nodes since we have the schema node */
5572 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) {
5573 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02005574 goto cleanup;
Michal Vasko69730152020-10-09 16:30:07 +02005575 } else if (set->context_op && (scnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) &&
5576 (scnode != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005577 lyxp_set_free_content(set);
5578 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +02005579 }
5580
5581 /* create specific data instance if needed */
5582 if (scnode->nodetype == LYS_LIST) {
5583 LY_CHECK_GOTO(ret = lyd_create_list(scnode, predicates, &inst), cleanup);
5584 } else if (scnode->nodetype == LYS_LEAFLIST) {
5585 LY_CHECK_GOTO(ret = lyd_create_term2(scnode, &predicates[0].value, &inst), cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02005586 }
5587
5588 for (i = 0; i < set->used; ) {
Michal Vaskod3678892020-05-21 10:06:58 +02005589 siblings = NULL;
5590
5591 if ((set->val.nodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.nodes[i].type == LYXP_NODE_ROOT)) {
5592 assert(!set->val.nodes[i].node);
5593
5594 /* search in all the trees */
5595 siblings = set->tree;
5596 } else if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
5597 /* search in children */
Radek Krejcia1c1e542020-09-29 16:06:52 +02005598 siblings = lyd_child(set->val.nodes[i].node);
Michal Vaskod3678892020-05-21 10:06:58 +02005599 }
5600
5601 /* find the node using hashes */
Michal Vasko004d3152020-06-11 19:59:22 +02005602 if (inst) {
5603 lyd_find_sibling_first(siblings, inst, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02005604 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02005605 lyd_find_sibling_val(siblings, scnode, NULL, 0, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02005606 }
5607
5608 /* when check */
5609 if (sub && moveto_when_check(sub)) {
Michal Vasko004d3152020-06-11 19:59:22 +02005610 ret = LY_EINCOMPLETE;
5611 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005612 }
5613
5614 if (sub) {
5615 /* pos filled later */
Michal Vaskocb1b7c02020-07-03 13:38:12 +02005616 set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i);
Michal Vaskod3678892020-05-21 10:06:58 +02005617 ++i;
Michal Vaskocb1b7c02020-07-03 13:38:12 +02005618 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005619 /* no match */
5620 set_remove_node(set, i);
5621 }
5622 }
5623
Michal Vasko004d3152020-06-11 19:59:22 +02005624cleanup:
5625 lyd_free_tree(inst);
5626 return ret;
Michal Vaskod3678892020-05-21 10:06:58 +02005627}
5628
5629/**
5630 * @brief Move context @p set to a schema node. Result is LYXP_SET_SCNODE_SET (or LYXP_SET_EMPTY).
5631 *
5632 * @param[in,out] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005633 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005634 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005635 * @param[in] options XPath options.
5636 * @return LY_ERR
5637 */
5638static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005639moveto_scnode(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005640{
Radek Krejci857189e2020-09-01 13:26:36 +02005641 ly_bool temp_ctx = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005642 uint32_t getnext_opts;
5643 uint32_t orig_used, i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005644 uint32_t mod_idx;
Michal Vasko519fd602020-05-26 12:17:39 +02005645 const struct lysc_node *iter, *start_parent;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005646 const struct lys_module *mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005647
Michal Vaskod3678892020-05-21 10:06:58 +02005648 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005649 return LY_SUCCESS;
5650 }
5651
5652 if (set->type != LYXP_SET_SCNODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005653 LOGVAL(set->ctx, LY_VLOG_LYSC, set->cur_scnode, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005654 return LY_EVALID;
5655 }
5656
Michal Vaskocafad9d2019-11-07 15:20:03 +01005657 /* getnext opts */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01005658 getnext_opts = 0;
Michal Vaskocafad9d2019-11-07 15:20:03 +01005659 if (options & LYXP_SCNODE_OUTPUT) {
5660 getnext_opts |= LYS_GETNEXT_OUTPUT;
5661 }
5662
Michal Vasko03ff5a72019-09-11 13:49:33 +02005663 orig_used = set->used;
5664 for (i = 0; i < orig_used; ++i) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005665 uint32_t idx;
5666
Michal Vasko03ff5a72019-09-11 13:49:33 +02005667 if (set->val.scnodes[i].in_ctx != 1) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01005668 if (set->val.scnodes[i].in_ctx != -2) {
5669 continue;
5670 }
5671
5672 /* remember context node */
5673 set->val.scnodes[i].in_ctx = -1;
Michal Vaskoec4df482019-12-16 10:02:18 +01005674 } else {
5675 set->val.scnodes[i].in_ctx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005676 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005677
5678 start_parent = set->val.scnodes[i].scnode;
5679
5680 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 +02005681 /* 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 +02005682 * it can be in a top-level augment (the root node itself is useless in this case) */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005683 mod_idx = 0;
Michal Vasko9e9f26d2020-10-12 16:31:33 +02005684 while ((mod = (struct lys_module *)ly_ctx_get_module_iter(set->ctx, &mod_idx))) {
Michal Vasko519fd602020-05-26 12:17:39 +02005685 iter = NULL;
Michal Vasko509de4d2019-12-10 14:51:30 +01005686 /* module may not be implemented */
Michal Vasko519fd602020-05-26 12:17:39 +02005687 while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005688 if (!moveto_scnode_check(iter, NULL, set, ncname, moveto_mod)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005689 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, &idx));
5690
Michal Vasko03ff5a72019-09-11 13:49:33 +02005691 /* we need to prevent these nodes from being considered in this moveto */
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005692 if ((idx < orig_used) && (idx > i)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005693 set->val.scnodes[idx].in_ctx = 2;
5694 temp_ctx = 1;
5695 }
5696 }
5697 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005698 }
5699
Michal Vasko519fd602020-05-26 12:17:39 +02005700 } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
5701 iter = NULL;
5702 while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005703 if (!moveto_scnode_check(iter, start_parent, set, ncname, moveto_mod)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005704 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, &idx));
5705
5706 if ((idx < orig_used) && (idx > i)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005707 set->val.scnodes[idx].in_ctx = 2;
5708 temp_ctx = 1;
5709 }
5710 }
5711 }
5712 }
5713 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005714
5715 /* correct temporary in_ctx values */
5716 if (temp_ctx) {
5717 for (i = 0; i < orig_used; ++i) {
5718 if (set->val.scnodes[i].in_ctx == 2) {
5719 set->val.scnodes[i].in_ctx = 1;
5720 }
5721 }
5722 }
5723
5724 return LY_SUCCESS;
5725}
5726
5727/**
Michal Vaskod3678892020-05-21 10:06:58 +02005728 * @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 +02005729 * Context position aware.
5730 *
5731 * @param[in] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005732 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005733 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005734 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5735 */
5736static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005737moveto_node_alldesc(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005738{
5739 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005740 const struct lyd_node *next, *elem, *start;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005741 struct lyxp_set ret_set;
5742 LY_ERR rc;
5743
Michal Vaskod3678892020-05-21 10:06:58 +02005744 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005745 return LY_SUCCESS;
5746 }
5747
5748 if (set->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005749 LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005750 return LY_EVALID;
5751 }
5752
Michal Vasko9f96a052020-03-10 09:41:45 +01005753 /* replace the original nodes (and throws away all text and meta nodes, root is replaced by a child) */
Michal Vaskod3678892020-05-21 10:06:58 +02005754 rc = moveto_node(set, NULL, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005755 LY_CHECK_RET(rc);
5756
Michal Vasko6346ece2019-09-24 13:12:53 +02005757 /* this loop traverses all the nodes in the set and adds/keeps only those that match qname */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005758 set_init(&ret_set, set);
5759 for (i = 0; i < set->used; ++i) {
5760
5761 /* TREE DFS */
5762 start = set->val.nodes[i].node;
5763 for (elem = next = start; elem; elem = next) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005764 rc = moveto_node_check(elem, start, set, ncname, moveto_mod);
Michal Vasko6346ece2019-09-24 13:12:53 +02005765 if (!rc) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005766 /* add matching node into result set */
5767 set_insert_node(&ret_set, elem, 0, LYXP_NODE_ELEM, ret_set.used);
5768 if (set_dup_node_check(set, elem, LYXP_NODE_ELEM, i)) {
5769 /* the node is a duplicate, we'll process it later in the set */
5770 goto skip_children;
5771 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005772 } else if (rc == LY_EINCOMPLETE) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005773 return rc;
5774 } else if (rc == LY_EINVAL) {
5775 goto skip_children;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005776 }
5777
5778 /* TREE DFS NEXT ELEM */
5779 /* select element for the next run - children first */
Radek Krejcia1c1e542020-09-29 16:06:52 +02005780 next = lyd_child(elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005781 if (!next) {
5782skip_children:
5783 /* no children, so try siblings, but only if it's not the start,
5784 * that is considered to be the root and it's siblings are not traversed */
5785 if (elem != start) {
5786 next = elem->next;
5787 } else {
5788 break;
5789 }
5790 }
5791 while (!next) {
5792 /* no siblings, go back through the parents */
5793 if ((struct lyd_node *)elem->parent == start) {
5794 /* we are done, no next element to process */
5795 break;
5796 }
5797 /* parent is already processed, go to its sibling */
5798 elem = (struct lyd_node *)elem->parent;
5799 next = elem->next;
5800 }
5801 }
5802 }
5803
5804 /* make the temporary set the current one */
5805 ret_set.ctx_pos = set->ctx_pos;
5806 ret_set.ctx_size = set->ctx_size;
Michal Vaskod3678892020-05-21 10:06:58 +02005807 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005808 memcpy(set, &ret_set, sizeof *set);
5809
5810 return LY_SUCCESS;
5811}
5812
5813/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005814 * @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 +02005815 *
5816 * @param[in] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005817 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005818 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005819 * @param[in] options XPath options.
5820 * @return LY_ERR
5821 */
5822static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005823moveto_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 +02005824{
Radek Krejci1deb5be2020-08-26 16:43:36 +02005825 uint32_t i, orig_used;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005826 const struct lysc_node *next, *elem, *start;
Michal Vasko6346ece2019-09-24 13:12:53 +02005827 LY_ERR rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005828
Michal Vaskod3678892020-05-21 10:06:58 +02005829 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005830 return LY_SUCCESS;
5831 }
5832
5833 if (set->type != LYXP_SET_SCNODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005834 LOGVAL(set->ctx, LY_VLOG_LYSC, set->cur_scnode, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005835 return LY_EVALID;
5836 }
5837
Michal Vasko03ff5a72019-09-11 13:49:33 +02005838 orig_used = set->used;
5839 for (i = 0; i < orig_used; ++i) {
5840 if (set->val.scnodes[i].in_ctx != 1) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01005841 if (set->val.scnodes[i].in_ctx != -2) {
5842 continue;
5843 }
5844
5845 /* remember context node */
5846 set->val.scnodes[i].in_ctx = -1;
Michal Vaskoec4df482019-12-16 10:02:18 +01005847 } else {
5848 set->val.scnodes[i].in_ctx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005849 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005850
5851 /* TREE DFS */
5852 start = set->val.scnodes[i].scnode;
5853 for (elem = next = start; elem; elem = next) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005854 if ((elem == start) || (elem->nodetype & (LYS_CHOICE | LYS_CASE))) {
5855 /* schema-only nodes, skip root */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005856 goto next_iter;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005857 }
5858
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005859 rc = moveto_scnode_check(elem, start, set, ncname, moveto_mod);
Michal Vasko6346ece2019-09-24 13:12:53 +02005860 if (!rc) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005861 uint32_t idx;
5862
5863 if (lyxp_set_scnode_contains(set, elem, LYXP_NODE_ELEM, i, &idx)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005864 set->val.scnodes[idx].in_ctx = 1;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005865 if ((uint32_t)idx > i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005866 /* we will process it later in the set */
5867 goto skip_children;
5868 }
5869 } else {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005870 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, elem, LYXP_NODE_ELEM, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005871 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005872 } else if (rc == LY_EINVAL) {
5873 goto skip_children;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005874 }
5875
5876next_iter:
5877 /* TREE DFS NEXT ELEM */
5878 /* select element for the next run - children first */
5879 next = lysc_node_children(elem, options & LYXP_SCNODE_OUTPUT ? LYS_CONFIG_R : LYS_CONFIG_W);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005880 if (!next) {
5881skip_children:
5882 /* no children, so try siblings, but only if it's not the start,
5883 * that is considered to be the root and it's siblings are not traversed */
5884 if (elem != start) {
5885 next = elem->next;
5886 } else {
5887 break;
5888 }
5889 }
5890 while (!next) {
5891 /* no siblings, go back through the parents */
5892 if (elem->parent == start) {
5893 /* we are done, no next element to process */
5894 break;
5895 }
5896 /* parent is already processed, go to its sibling */
5897 elem = elem->parent;
5898 next = elem->next;
5899 }
5900 }
5901 }
5902
5903 return LY_SUCCESS;
5904}
5905
5906/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005907 * @brief Move context @p set to an attribute. Result is LYXP_SET_NODE_SET.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005908 * Indirectly context position aware.
5909 *
5910 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005911 * @param[in] mod Matching metadata module, NULL for any.
5912 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005913 * @return LY_ERR
5914 */
5915static LY_ERR
Michal Vaskod3678892020-05-21 10:06:58 +02005916moveto_attr(struct lyxp_set *set, const struct lys_module *mod, const char *ncname)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005917{
Michal Vasko9f96a052020-03-10 09:41:45 +01005918 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005919
Michal Vaskod3678892020-05-21 10:06:58 +02005920 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005921 return LY_SUCCESS;
5922 }
5923
5924 if (set->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005925 LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005926 return LY_EVALID;
5927 }
5928
Radek Krejci1deb5be2020-08-26 16:43:36 +02005929 for (uint32_t i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02005930 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005931
5932 /* only attributes of an elem (not dummy) can be in the result, skip all the rest;
5933 * our attributes are always qualified */
Michal Vasko5c4e5892019-11-14 12:31:38 +01005934 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01005935 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005936
5937 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02005938 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005939 continue;
5940 }
5941
Michal Vaskod3678892020-05-21 10:06:58 +02005942 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005943 /* match */
5944 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01005945 set->val.meta[i].meta = sub;
5946 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005947 /* pos does not change */
5948 replaced = 1;
5949 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01005950 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 +02005951 }
5952 ++i;
5953 }
5954 }
5955 }
5956
5957 if (!replaced) {
5958 /* no match */
5959 set_remove_node(set, i);
5960 }
5961 }
5962
5963 return LY_SUCCESS;
5964}
5965
5966/**
5967 * @brief Move context @p set1 to union with @p set2. @p set2 is emptied afterwards.
Michal Vasko61ac2f62020-05-25 12:39:51 +02005968 * Result is LYXP_SET_NODE_SET. Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005969 *
5970 * @param[in,out] set1 Set to use for the result.
5971 * @param[in] set2 Set that is copied to @p set1.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005972 * @return LY_ERR
5973 */
5974static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005975moveto_union(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005976{
5977 LY_ERR rc;
5978
Michal Vaskod3678892020-05-21 10:06:58 +02005979 if ((set1->type != LYXP_SET_NODE_SET) || (set2->type != LYXP_SET_NODE_SET)) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005980 LOGVAL(set1->ctx, LY_VLOG_LYD, set1->cur_node, LY_VCODE_XP_INOP_2, "union", print_set_type(set1), print_set_type(set2));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005981 return LY_EVALID;
5982 }
5983
5984 /* set2 is empty or both set1 and set2 */
Michal Vaskod3678892020-05-21 10:06:58 +02005985 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005986 return LY_SUCCESS;
5987 }
5988
Michal Vaskod3678892020-05-21 10:06:58 +02005989 if (!set1->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005990 memcpy(set1, set2, sizeof *set1);
5991 /* dynamic memory belongs to set1 now, do not free */
Michal Vaskod3678892020-05-21 10:06:58 +02005992 memset(set2, 0, sizeof *set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005993 return LY_SUCCESS;
5994 }
5995
5996 /* we assume sets are sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005997 assert(!set_sort(set1) && !set_sort(set2));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005998
5999 /* sort, remove duplicates */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006000 rc = set_sorted_merge(set1, set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006001 LY_CHECK_RET(rc);
6002
6003 /* final set must be sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006004 assert(!set_sort(set1));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006005
6006 return LY_SUCCESS;
6007}
6008
6009/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02006010 * @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 +02006011 * Context position aware.
6012 *
6013 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02006014 * @param[in] mod Matching metadata module, NULL for any.
6015 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006016 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6017 */
6018static int
Michal Vaskod3678892020-05-21 10:06:58 +02006019moveto_attr_alldesc(struct lyxp_set *set, const struct lys_module *mod, const char *ncname)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006020{
Michal Vasko9f96a052020-03-10 09:41:45 +01006021 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006022 struct lyxp_set *set_all_desc = NULL;
6023 LY_ERR rc;
6024
Michal Vaskod3678892020-05-21 10:06:58 +02006025 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006026 return LY_SUCCESS;
6027 }
6028
6029 if (set->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006030 LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006031 return LY_EVALID;
6032 }
6033
Michal Vasko03ff5a72019-09-11 13:49:33 +02006034 /* can be optimized similarly to moveto_node_alldesc() and save considerable amount of memory,
6035 * but it likely won't be used much, so it's a waste of time */
6036 /* copy the context */
6037 set_all_desc = set_copy(set);
6038 /* get all descendant nodes (the original context nodes are removed) */
Michal Vaskod3678892020-05-21 10:06:58 +02006039 rc = moveto_node_alldesc(set_all_desc, NULL, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006040 if (rc != LY_SUCCESS) {
6041 lyxp_set_free(set_all_desc);
6042 return rc;
6043 }
6044 /* prepend the original context nodes */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006045 rc = moveto_union(set, set_all_desc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006046 if (rc != LY_SUCCESS) {
6047 lyxp_set_free(set_all_desc);
6048 return rc;
6049 }
6050 lyxp_set_free(set_all_desc);
6051
Radek Krejci1deb5be2020-08-26 16:43:36 +02006052 for (uint32_t i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02006053 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006054
6055 /* only attributes of an elem can be in the result, skip all the rest,
6056 * we have all attributes qualified in lyd tree */
6057 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01006058 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006059 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02006060 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006061 continue;
6062 }
6063
Michal Vaskod3678892020-05-21 10:06:58 +02006064 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006065 /* match */
6066 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01006067 set->val.meta[i].meta = sub;
6068 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006069 /* pos does not change */
6070 replaced = 1;
6071 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01006072 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 +02006073 }
6074 ++i;
6075 }
6076 }
6077 }
6078
6079 if (!replaced) {
6080 /* no match */
6081 set_remove_node(set, i);
6082 }
6083 }
6084
6085 return LY_SUCCESS;
6086}
6087
6088/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02006089 * @brief Move context @p set to self and al chilren, recursively. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET.
6090 * Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006091 *
6092 * @param[in] parent Current parent.
6093 * @param[in] parent_pos Position of @p parent.
6094 * @param[in] parent_type Node type of @p parent.
6095 * @param[in,out] to_set Set to use.
6096 * @param[in] dup_check_set Set for checking duplicities.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006097 * @param[in] options XPath options.
6098 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6099 */
6100static LY_ERR
6101moveto_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 +02006102 struct lyxp_set *to_set, const struct lyxp_set *dup_check_set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006103{
Michal Vasko61ac2f62020-05-25 12:39:51 +02006104 const struct lyd_node *iter, *first;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006105 LY_ERR rc;
6106
6107 switch (parent_type) {
6108 case LYXP_NODE_ROOT:
6109 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko61ac2f62020-05-25 12:39:51 +02006110 assert(!parent);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006111
Michal Vasko61ac2f62020-05-25 12:39:51 +02006112 /* add all top-level nodes as elements */
6113 first = to_set->tree;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006114 break;
6115 case LYXP_NODE_ELEM:
Michal Vasko61ac2f62020-05-25 12:39:51 +02006116 /* add just the text node of this term element node */
6117 if (parent->schema->nodetype & (LYD_NODE_TERM | LYD_NODE_ANY)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006118 if (!set_dup_node_check(dup_check_set, parent, LYXP_NODE_TEXT, -1)) {
6119 set_insert_node(to_set, parent, parent_pos, LYXP_NODE_TEXT, to_set->used);
6120 }
Michal Vasko61ac2f62020-05-25 12:39:51 +02006121 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006122 }
Michal Vasko61ac2f62020-05-25 12:39:51 +02006123
6124 /* add all the children of this node */
Radek Krejcia1c1e542020-09-29 16:06:52 +02006125 first = lyd_child(parent);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006126 break;
6127 default:
6128 LOGINT_RET(parent->schema->module->ctx);
6129 }
6130
Michal Vasko61ac2f62020-05-25 12:39:51 +02006131 /* add all top-level nodes as elements */
6132 LY_LIST_FOR(first, iter) {
6133 /* context check */
6134 if ((parent_type == LYXP_NODE_ROOT_CONFIG) && (iter->schema->flags & LYS_CONFIG_R)) {
6135 continue;
6136 }
6137
6138 /* when check */
6139 if (moveto_when_check(iter)) {
6140 return LY_EINCOMPLETE;
6141 }
6142
6143 if (!set_dup_node_check(dup_check_set, iter, LYXP_NODE_ELEM, -1)) {
6144 set_insert_node(to_set, iter, 0, LYXP_NODE_ELEM, to_set->used);
6145
6146 /* also add all the children of this node, recursively */
6147 rc = moveto_self_add_children_r(iter, 0, LYXP_NODE_ELEM, to_set, dup_check_set, options);
6148 LY_CHECK_RET(rc);
6149 }
6150 }
6151
Michal Vasko03ff5a72019-09-11 13:49:33 +02006152 return LY_SUCCESS;
6153}
6154
6155/**
6156 * @brief Move context @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET
6157 * (or LYXP_SET_EMPTY). Context position aware.
6158 *
6159 * @param[in,out] set Set to use.
6160 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6161 * @param[in] options XPath options.
6162 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6163 */
6164static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006165moveto_self(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006166{
Michal Vasko03ff5a72019-09-11 13:49:33 +02006167 struct lyxp_set ret_set;
6168 LY_ERR rc;
6169
Michal Vaskod3678892020-05-21 10:06:58 +02006170 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006171 return LY_SUCCESS;
6172 }
6173
6174 if (set->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006175 LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006176 return LY_EVALID;
6177 }
6178
6179 /* nothing to do */
6180 if (!all_desc) {
6181 return LY_SUCCESS;
6182 }
6183
Michal Vasko03ff5a72019-09-11 13:49:33 +02006184 /* add all the children, they get added recursively */
6185 set_init(&ret_set, set);
Radek Krejci1deb5be2020-08-26 16:43:36 +02006186 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006187 /* copy the current node to tmp */
6188 set_insert_node(&ret_set, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, ret_set.used);
6189
6190 /* do not touch attributes and text nodes */
Michal Vasko9f96a052020-03-10 09:41:45 +01006191 if ((set->val.nodes[i].type == LYXP_NODE_TEXT) || (set->val.nodes[i].type == LYXP_NODE_META)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006192 continue;
6193 }
6194
Michal Vasko03ff5a72019-09-11 13:49:33 +02006195 /* add all the children */
6196 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 +02006197 set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006198 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006199 lyxp_set_free_content(&ret_set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006200 return rc;
6201 }
6202 }
6203
6204 /* use the temporary set as the current one */
6205 ret_set.ctx_pos = set->ctx_pos;
6206 ret_set.ctx_size = set->ctx_size;
Michal Vaskod3678892020-05-21 10:06:58 +02006207 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006208 memcpy(set, &ret_set, sizeof *set);
6209
6210 return LY_SUCCESS;
6211}
6212
6213/**
6214 * @brief Move context schema @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_SCNODE_SET
6215 * (or LYXP_SET_EMPTY).
6216 *
6217 * @param[in,out] set Set to use.
6218 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6219 * @param[in] options XPath options.
6220 * @return LY_ERR
6221 */
6222static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006223moveto_scnode_self(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006224{
Radek Krejci1deb5be2020-08-26 16:43:36 +02006225 uint32_t getnext_opts;
6226 uint32_t mod_idx;
Michal Vasko519fd602020-05-26 12:17:39 +02006227 const struct lysc_node *iter, *start_parent;
6228 const struct lys_module *mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006229
Michal Vaskod3678892020-05-21 10:06:58 +02006230 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006231 return LY_SUCCESS;
6232 }
6233
6234 if (set->type != LYXP_SET_SCNODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006235 LOGVAL(set->ctx, LY_VLOG_LYSC, set->cur_scnode, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006236 return LY_EVALID;
6237 }
6238
6239 /* nothing to do */
6240 if (!all_desc) {
6241 return LY_SUCCESS;
6242 }
6243
Michal Vasko519fd602020-05-26 12:17:39 +02006244 /* getnext opts */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01006245 getnext_opts = 0;
Michal Vasko519fd602020-05-26 12:17:39 +02006246 if (options & LYXP_SCNODE_OUTPUT) {
6247 getnext_opts |= LYS_GETNEXT_OUTPUT;
6248 }
6249
6250 /* add all the children, recursively as they are being added into the same set */
Radek Krejci1deb5be2020-08-26 16:43:36 +02006251 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006252 if (set->val.scnodes[i].in_ctx != 1) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006253 if (set->val.scnodes[i].in_ctx != -2) {
6254 continue;
6255 }
6256
Michal Vasko519fd602020-05-26 12:17:39 +02006257 /* remember context node */
6258 set->val.scnodes[i].in_ctx = -1;
6259 } else {
6260 set->val.scnodes[i].in_ctx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006261 }
6262
Michal Vasko519fd602020-05-26 12:17:39 +02006263 start_parent = set->val.scnodes[i].scnode;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006264
Michal Vasko519fd602020-05-26 12:17:39 +02006265 if ((set->val.scnodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.scnodes[i].type == LYXP_NODE_ROOT)) {
6266 /* it can actually be in any module, it's all <running> */
6267 mod_idx = 0;
6268 while ((mod = (struct lys_module *)ly_ctx_get_module_iter(set->ctx, &mod_idx))) {
6269 iter = NULL;
6270 /* module may not be implemented */
6271 while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) {
6272 /* context check */
6273 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) {
6274 continue;
6275 }
6276
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006277 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, NULL));
Michal Vasko519fd602020-05-26 12:17:39 +02006278 /* throw away the insert index, we want to consider that node again, recursively */
6279 }
6280 }
6281
6282 } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
6283 iter = NULL;
6284 while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006285 /* context check */
Michal Vasko519fd602020-05-26 12:17:39 +02006286 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006287 continue;
6288 }
6289
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006290 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006291 }
6292 }
6293 }
6294
6295 return LY_SUCCESS;
6296}
6297
6298/**
6299 * @brief Move context @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_NODE_SET
6300 * (or LYXP_SET_EMPTY). Context position aware.
6301 *
6302 * @param[in] set Set to use.
6303 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6304 * @param[in] options XPath options.
6305 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6306 */
6307static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006308moveto_parent(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006309{
6310 LY_ERR rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006311 struct lyd_node *node, *new_node;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006312 enum lyxp_node_type new_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006313
Michal Vaskod3678892020-05-21 10:06:58 +02006314 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006315 return LY_SUCCESS;
6316 }
6317
6318 if (set->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006319 LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006320 return LY_EVALID;
6321 }
6322
6323 if (all_desc) {
6324 /* <path>//.. == <path>//./.. */
6325 rc = moveto_self(set, 1, options);
6326 LY_CHECK_RET(rc);
6327 }
6328
Radek Krejci1deb5be2020-08-26 16:43:36 +02006329 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006330 node = set->val.nodes[i].node;
6331
6332 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
6333 new_node = (struct lyd_node *)node->parent;
6334 } else if (set->val.nodes[i].type == LYXP_NODE_TEXT) {
6335 new_node = node;
Michal Vasko9f96a052020-03-10 09:41:45 +01006336 } else if (set->val.nodes[i].type == LYXP_NODE_META) {
6337 new_node = set->val.meta[i].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006338 if (!new_node) {
6339 LOGINT_RET(set->ctx);
6340 }
6341 } else {
6342 /* root does not have a parent */
Michal Vasko2caefc12019-11-14 16:07:56 +01006343 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006344 continue;
6345 }
6346
Michal Vaskoa1424542019-11-14 16:08:52 +01006347 /* when check */
6348 if (moveto_when_check(new_node)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006349 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01006350 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006351
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006352 if (!new_node) {
Radek Krejcif6a11002020-08-21 13:29:07 +02006353 /* node already there can also be the root */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006354 new_type = set->root_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006355
Michal Vasko03ff5a72019-09-11 13:49:33 +02006356 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02006357 /* 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 +02006358 new_type = LYXP_NODE_ELEM;
6359 }
6360
Michal Vasko03ff5a72019-09-11 13:49:33 +02006361 if (set_dup_node_check(set, new_node, new_type, -1)) {
Michal Vasko2caefc12019-11-14 16:07:56 +01006362 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006363 } else {
6364 set_replace_node(set, new_node, 0, new_type, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006365 }
6366 }
6367
Michal Vasko2caefc12019-11-14 16:07:56 +01006368 set_remove_nodes_none(set);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006369 assert(!set_sort(set) && !set_sorted_dup_node_clean(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006370
6371 return LY_SUCCESS;
6372}
6373
6374/**
6375 * @brief Move context schema @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_SCNODE_SET
6376 * (or LYXP_SET_EMPTY).
6377 *
6378 * @param[in] set Set to use.
6379 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6380 * @param[in] options XPath options.
6381 * @return LY_ERR
6382 */
6383static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006384moveto_scnode_parent(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006385{
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006386 uint32_t i, orig_used, idx;
Radek Krejci857189e2020-09-01 13:26:36 +02006387 ly_bool temp_ctx = 0;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006388 const struct lysc_node *node, *new_node;
6389 enum lyxp_node_type new_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006390
Michal Vaskod3678892020-05-21 10:06:58 +02006391 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006392 return LY_SUCCESS;
6393 }
6394
6395 if (set->type != LYXP_SET_SCNODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006396 LOGVAL(set->ctx, LY_VLOG_LYSC, set->cur_scnode, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006397 return LY_EVALID;
6398 }
6399
6400 if (all_desc) {
6401 /* <path>//.. == <path>//./.. */
Radek Krejci1deb5be2020-08-26 16:43:36 +02006402 LY_CHECK_RET(moveto_scnode_self(set, 1, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006403 }
6404
Michal Vasko03ff5a72019-09-11 13:49:33 +02006405 orig_used = set->used;
6406 for (i = 0; i < orig_used; ++i) {
6407 if (set->val.scnodes[i].in_ctx != 1) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006408 if (set->val.scnodes[i].in_ctx != -2) {
6409 continue;
6410 }
6411
6412 /* remember context node */
6413 set->val.scnodes[i].in_ctx = -1;
Michal Vaskoec4df482019-12-16 10:02:18 +01006414 } else {
6415 set->val.scnodes[i].in_ctx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006416 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006417
6418 node = set->val.scnodes[i].scnode;
6419
6420 if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
Michal Vaskod3678892020-05-21 10:06:58 +02006421 new_node = lysc_data_parent(node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006422 } else {
6423 /* root does not have a parent */
6424 continue;
6425 }
6426
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006427 if (!new_node) {
Radek Krejcif6a11002020-08-21 13:29:07 +02006428 /* node has no parent */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006429 new_type = set->root_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006430
Michal Vasko03ff5a72019-09-11 13:49:33 +02006431 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02006432 /* 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 +02006433 new_type = LYXP_NODE_ELEM;
6434 }
6435
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006436 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, new_node, new_type, &idx));
6437 if ((idx < orig_used) && (idx > i)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006438 set->val.scnodes[idx].in_ctx = 2;
6439 temp_ctx = 1;
6440 }
6441 }
6442
6443 if (temp_ctx) {
6444 for (i = 0; i < orig_used; ++i) {
6445 if (set->val.scnodes[i].in_ctx == 2) {
6446 set->val.scnodes[i].in_ctx = 1;
6447 }
6448 }
6449 }
6450
6451 return LY_SUCCESS;
6452}
6453
6454/**
6455 * @brief Move context @p set to the result of a comparison. Handles '=', '!=', '<=', '<', '>=', or '>'.
6456 * Result is LYXP_SET_BOOLEAN. Indirectly context position aware.
6457 *
6458 * @param[in,out] set1 Set to use for the result.
6459 * @param[in] set2 Set acting as the second operand for @p op.
6460 * @param[in] op Comparison operator to process.
6461 * @param[in] options XPath options.
6462 * @return LY_ERR
6463 */
6464static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02006465moveto_op_comp(struct lyxp_set *set1, struct lyxp_set *set2, const char *op, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006466{
6467 /*
6468 * NODE SET + NODE SET = NODE SET + STRING /(1 NODE SET) 2 STRING
6469 * NODE SET + STRING = STRING + STRING /1 STRING (2 STRING)
6470 * NODE SET + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6471 * NODE SET + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6472 * STRING + NODE SET = STRING + STRING /(1 STRING) 2 STRING
6473 * NUMBER + NODE SET = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6474 * BOOLEAN + NODE SET = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6475 *
6476 * '=' or '!='
6477 * BOOLEAN + BOOLEAN
6478 * BOOLEAN + STRING = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6479 * BOOLEAN + NUMBER = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6480 * STRING + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6481 * NUMBER + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6482 * NUMBER + NUMBER
6483 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6484 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6485 * STRING + STRING
6486 *
6487 * '<=', '<', '>=', '>'
6488 * NUMBER + NUMBER
6489 * BOOLEAN + BOOLEAN = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6490 * BOOLEAN + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6491 * BOOLEAN + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6492 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6493 * STRING + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6494 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6495 * NUMBER + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6496 * STRING + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6497 */
6498 struct lyxp_set iter1, iter2;
6499 int result;
6500 int64_t i;
6501 LY_ERR rc;
6502
Michal Vasko004d3152020-06-11 19:59:22 +02006503 memset(&iter1, 0, sizeof iter1);
6504 memset(&iter2, 0, sizeof iter2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006505
6506 /* iterative evaluation with node-sets */
6507 if ((set1->type == LYXP_SET_NODE_SET) || (set2->type == LYXP_SET_NODE_SET)) {
6508 if (set1->type == LYXP_SET_NODE_SET) {
6509 for (i = 0; i < set1->used; ++i) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006510 /* cast set1 */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006511 switch (set2->type) {
6512 case LYXP_SET_NUMBER:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006513 rc = set_comp_cast(&iter1, set1, LYXP_SET_NUMBER, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006514 break;
6515 case LYXP_SET_BOOLEAN:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006516 rc = set_comp_cast(&iter1, set1, LYXP_SET_BOOLEAN, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006517 break;
6518 default:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006519 rc = set_comp_cast(&iter1, set1, LYXP_SET_STRING, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006520 break;
6521 }
6522 LY_CHECK_RET(rc);
6523
Michal Vasko4c7763f2020-07-27 17:40:37 +02006524 /* canonize set2 */
6525 LY_CHECK_ERR_RET(rc = set_comp_canonize(&iter2, set2, &set1->val.nodes[i]), lyxp_set_free_content(&iter1), rc);
6526
6527 /* compare recursively */
6528 rc = moveto_op_comp(&iter1, &iter2, op, options);
6529 lyxp_set_free_content(&iter2);
6530 LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006531
6532 /* lazy evaluation until true */
Michal Vasko004d3152020-06-11 19:59:22 +02006533 if (iter1.val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006534 set_fill_boolean(set1, 1);
6535 return LY_SUCCESS;
6536 }
6537 }
6538 } else {
6539 for (i = 0; i < set2->used; ++i) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006540 /* set set2 */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006541 switch (set1->type) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006542 case LYXP_SET_NUMBER:
6543 rc = set_comp_cast(&iter2, set2, LYXP_SET_NUMBER, i);
6544 break;
6545 case LYXP_SET_BOOLEAN:
6546 rc = set_comp_cast(&iter2, set2, LYXP_SET_BOOLEAN, i);
6547 break;
6548 default:
6549 rc = set_comp_cast(&iter2, set2, LYXP_SET_STRING, i);
6550 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006551 }
6552 LY_CHECK_RET(rc);
6553
Michal Vasko4c7763f2020-07-27 17:40:37 +02006554 /* canonize set1 */
6555 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 +02006556
Michal Vasko4c7763f2020-07-27 17:40:37 +02006557 /* compare recursively */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006558 rc = moveto_op_comp(&iter1, &iter2, op, options);
Michal Vaskod3678892020-05-21 10:06:58 +02006559 lyxp_set_free_content(&iter2);
Michal Vasko4c7763f2020-07-27 17:40:37 +02006560 LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006561
6562 /* lazy evaluation until true */
Michal Vasko004d3152020-06-11 19:59:22 +02006563 if (iter1.val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006564 set_fill_boolean(set1, 1);
6565 return LY_SUCCESS;
6566 }
6567 }
6568 }
6569
6570 /* false for all nodes */
6571 set_fill_boolean(set1, 0);
6572 return LY_SUCCESS;
6573 }
6574
6575 /* first convert properly */
6576 if ((op[0] == '=') || (op[0] == '!')) {
6577 if ((set1->type == LYXP_SET_BOOLEAN) || (set2->type == LYXP_SET_BOOLEAN)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006578 lyxp_set_cast(set1, LYXP_SET_BOOLEAN);
6579 lyxp_set_cast(set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006580 } else if ((set1->type == LYXP_SET_NUMBER) || (set2->type == LYXP_SET_NUMBER)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006581 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006582 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006583 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006584 LY_CHECK_RET(rc);
6585 } /* else we have 2 strings */
6586 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006587 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006588 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006589 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006590 LY_CHECK_RET(rc);
6591 }
6592
6593 assert(set1->type == set2->type);
6594
6595 /* compute result */
6596 if (op[0] == '=') {
6597 if (set1->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02006598 result = (set1->val.bln == set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006599 } else if (set1->type == LYXP_SET_NUMBER) {
6600 result = (set1->val.num == set2->val.num);
6601 } else {
6602 assert(set1->type == LYXP_SET_STRING);
Michal Vaskoac6c72f2019-11-14 16:09:34 +01006603 result = !strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006604 }
6605 } else if (op[0] == '!') {
6606 if (set1->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02006607 result = (set1->val.bln != set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006608 } else if (set1->type == LYXP_SET_NUMBER) {
6609 result = (set1->val.num != set2->val.num);
6610 } else {
6611 assert(set1->type == LYXP_SET_STRING);
Michal Vaskoac6c72f2019-11-14 16:09:34 +01006612 result = !strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006613 }
6614 } else {
6615 assert(set1->type == LYXP_SET_NUMBER);
6616 if (op[0] == '<') {
6617 if (op[1] == '=') {
6618 result = (set1->val.num <= set2->val.num);
6619 } else {
6620 result = (set1->val.num < set2->val.num);
6621 }
6622 } else {
6623 if (op[1] == '=') {
6624 result = (set1->val.num >= set2->val.num);
6625 } else {
6626 result = (set1->val.num > set2->val.num);
6627 }
6628 }
6629 }
6630
6631 /* assign result */
6632 if (result) {
6633 set_fill_boolean(set1, 1);
6634 } else {
6635 set_fill_boolean(set1, 0);
6636 }
6637
6638 return LY_SUCCESS;
6639}
6640
6641/**
6642 * @brief Move context @p set to the result of a basic operation. Handles '+', '-', unary '-', '*', 'div',
6643 * or 'mod'. Result is LYXP_SET_NUMBER. Indirectly context position aware.
6644 *
6645 * @param[in,out] set1 Set to use for the result.
6646 * @param[in] set2 Set acting as the second operand for @p op.
6647 * @param[in] op Operator to process.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006648 * @return LY_ERR
6649 */
6650static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006651moveto_op_math(struct lyxp_set *set1, struct lyxp_set *set2, const char *op)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006652{
6653 LY_ERR rc;
6654
6655 /* unary '-' */
6656 if (!set2 && (op[0] == '-')) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006657 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006658 LY_CHECK_RET(rc);
6659 set1->val.num *= -1;
6660 lyxp_set_free(set2);
6661 return LY_SUCCESS;
6662 }
6663
6664 assert(set1 && set2);
6665
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006666 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006667 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006668 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006669 LY_CHECK_RET(rc);
6670
6671 switch (op[0]) {
6672 /* '+' */
6673 case '+':
6674 set1->val.num += set2->val.num;
6675 break;
6676
6677 /* '-' */
6678 case '-':
6679 set1->val.num -= set2->val.num;
6680 break;
6681
6682 /* '*' */
6683 case '*':
6684 set1->val.num *= set2->val.num;
6685 break;
6686
6687 /* 'div' */
6688 case 'd':
6689 set1->val.num /= set2->val.num;
6690 break;
6691
6692 /* 'mod' */
6693 case 'm':
6694 set1->val.num = ((long long)set1->val.num) % ((long long)set2->val.num);
6695 break;
6696
6697 default:
6698 LOGINT_RET(set1->ctx);
6699 }
6700
6701 return LY_SUCCESS;
6702}
6703
6704/*
6705 * eval functions
6706 *
6707 * They execute a parsed XPath expression on some data subtree.
6708 */
6709
6710/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02006711 * @brief Evaluate Predicate. Logs directly on error.
6712 *
Michal Vaskod3678892020-05-21 10:06:58 +02006713 * [9] Predicate ::= '[' Expr ']'
Michal Vasko03ff5a72019-09-11 13:49:33 +02006714 *
6715 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02006716 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006717 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
6718 * @param[in] options XPath options.
6719 * @param[in] parent_pos_pred Whether parent predicate was a positional one.
6720 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6721 */
6722static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02006723eval_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 +02006724{
6725 LY_ERR rc;
Michal Vasko57eab132019-09-24 11:46:26 +02006726 uint16_t i, orig_exp;
Michal Vasko5c4e5892019-11-14 12:31:38 +01006727 uint32_t orig_pos, orig_size;
6728 int32_t pred_in_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006729 struct lyxp_set set2;
6730 struct lyd_node *orig_parent;
6731
6732 /* '[' */
6733 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02006734 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02006735 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006736
6737 if (!set) {
6738only_parse:
Michal Vasko004d3152020-06-11 19:59:22 +02006739 rc = eval_expr_select(exp, tok_idx, 0, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006740 LY_CHECK_RET(rc);
6741 } else if (set->type == LYXP_SET_NODE_SET) {
6742 /* 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 +01006743 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006744
6745 /* empty set, nothing to evaluate */
6746 if (!set->used) {
6747 goto only_parse;
6748 }
6749
Michal Vasko004d3152020-06-11 19:59:22 +02006750 orig_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006751 orig_pos = 0;
6752 orig_size = set->used;
6753 orig_parent = NULL;
Michal Vasko39dbf352020-05-21 10:08:59 +02006754 for (i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006755 set_init(&set2, set);
6756 set_insert_node(&set2, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, 0);
6757 /* remember the node context position for position() and context size for last(),
6758 * predicates should always be evaluated with respect to the child axis (since we do
6759 * not support explicit axes) so we assign positions based on their parents */
6760 if (parent_pos_pred && ((struct lyd_node *)set->val.nodes[i].node->parent != orig_parent)) {
6761 orig_parent = (struct lyd_node *)set->val.nodes[i].node->parent;
6762 orig_pos = 1;
6763 } else {
6764 ++orig_pos;
6765 }
6766
6767 set2.ctx_pos = orig_pos;
6768 set2.ctx_size = orig_size;
Michal Vasko004d3152020-06-11 19:59:22 +02006769 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006770
Michal Vasko004d3152020-06-11 19:59:22 +02006771 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006772 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006773 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006774 return rc;
6775 }
6776
6777 /* number is a position */
6778 if (set2.type == LYXP_SET_NUMBER) {
6779 if ((long long)set2.val.num == orig_pos) {
6780 set2.val.num = 1;
6781 } else {
6782 set2.val.num = 0;
6783 }
6784 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006785 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006786
6787 /* predicate satisfied or not? */
Michal Vasko004d3152020-06-11 19:59:22 +02006788 if (!set2.val.bln) {
Michal Vasko2caefc12019-11-14 16:07:56 +01006789 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006790 }
6791 }
Michal Vasko2caefc12019-11-14 16:07:56 +01006792 set_remove_nodes_none(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006793
6794 } else if (set->type == LYXP_SET_SCNODE_SET) {
6795 for (i = 0; i < set->used; ++i) {
6796 if (set->val.scnodes[i].in_ctx == 1) {
6797 /* there is a currently-valid node */
6798 break;
6799 }
6800 }
6801 /* empty set, nothing to evaluate */
6802 if (i == set->used) {
6803 goto only_parse;
6804 }
6805
Michal Vasko004d3152020-06-11 19:59:22 +02006806 orig_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006807
Michal Vasko03ff5a72019-09-11 13:49:33 +02006808 /* set special in_ctx to all the valid snodes */
6809 pred_in_ctx = set_scnode_new_in_ctx(set);
6810
6811 /* use the valid snodes one-by-one */
6812 for (i = 0; i < set->used; ++i) {
6813 if (set->val.scnodes[i].in_ctx != pred_in_ctx) {
6814 continue;
6815 }
6816 set->val.scnodes[i].in_ctx = 1;
6817
Michal Vasko004d3152020-06-11 19:59:22 +02006818 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006819
Michal Vasko004d3152020-06-11 19:59:22 +02006820 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006821 LY_CHECK_RET(rc);
6822
6823 set->val.scnodes[i].in_ctx = pred_in_ctx;
6824 }
6825
6826 /* restore the state as it was before the predicate */
6827 for (i = 0; i < set->used; ++i) {
6828 if (set->val.scnodes[i].in_ctx == 1) {
6829 set->val.scnodes[i].in_ctx = 0;
6830 } else if (set->val.scnodes[i].in_ctx == pred_in_ctx) {
6831 set->val.scnodes[i].in_ctx = 1;
6832 }
6833 }
6834
6835 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02006836 set2.type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006837 set_fill_set(&set2, set);
6838
Michal Vasko004d3152020-06-11 19:59:22 +02006839 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006840 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006841 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006842 return rc;
6843 }
6844
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006845 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02006846 if (!set2.val.bln) {
Michal Vaskod3678892020-05-21 10:06:58 +02006847 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006848 }
Michal Vaskod3678892020-05-21 10:06:58 +02006849 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006850 }
6851
6852 /* ']' */
Michal Vasko004d3152020-06-11 19:59:22 +02006853 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_BRACK2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006854 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02006855 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02006856 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006857
6858 return LY_SUCCESS;
6859}
6860
6861/**
Michal Vaskod3678892020-05-21 10:06:58 +02006862 * @brief Evaluate Literal. Logs directly on error.
6863 *
6864 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02006865 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02006866 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
6867 */
6868static void
Michal Vasko40308e72020-10-20 16:38:40 +02006869eval_literal(const struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set)
Michal Vaskod3678892020-05-21 10:06:58 +02006870{
6871 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02006872 if (exp->tok_len[*tok_idx] == 2) {
Michal Vaskod3678892020-05-21 10:06:58 +02006873 set_fill_string(set, "", 0);
6874 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02006875 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 +02006876 }
6877 }
6878 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02006879 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02006880 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02006881}
6882
6883/**
Michal Vasko004d3152020-06-11 19:59:22 +02006884 * @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 +02006885 *
Michal Vasko004d3152020-06-11 19:59:22 +02006886 * @param[in] exp Full parsed XPath expression.
6887 * @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 +02006888 * @param[in] ctx_node Found schema node as the context for the predicate.
6889 * @param[in] cur_mod Current module for the expression.
6890 * @param[in] cur_node Current (original context) node.
Michal Vasko004d3152020-06-11 19:59:22 +02006891 * @param[in] format Format of any prefixes in key names/values.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006892 * @param[in] prefix_data Format-specific prefix data (see ::ly_resolve_prefix).
Michal Vasko004d3152020-06-11 19:59:22 +02006893 * @param[out] predicates Parsed predicates.
6894 * @param[out] pred_type Type of @p predicates.
6895 * @return LY_SUCCESS on success,
6896 * @return LY_ERR on any error.
Michal Vaskod3678892020-05-21 10:06:58 +02006897 */
6898static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02006899eval_name_test_try_compile_predicates(const struct lyxp_expr *exp, uint16_t *tok_idx, const struct lysc_node *ctx_node,
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006900 const struct lys_module *cur_mod, const struct lysc_node *cur_node, LY_PREFIX_FORMAT format, void *prefix_data,
6901 struct ly_path_predicate **predicates, enum ly_path_pred_type *pred_type)
Michal Vaskod3678892020-05-21 10:06:58 +02006902{
Michal Vasko004d3152020-06-11 19:59:22 +02006903 LY_ERR ret = LY_SUCCESS;
6904 uint16_t key_count, e_idx, pred_idx = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02006905 const struct lysc_node *key;
Michal Vasko004d3152020-06-11 19:59:22 +02006906 size_t pred_len;
Radek Krejci1deb5be2020-08-26 16:43:36 +02006907 uint32_t prev_lo;
Michal Vasko004d3152020-06-11 19:59:22 +02006908 struct lyxp_expr *exp2 = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02006909
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006910 assert(ctx_node->nodetype & (LYS_LIST | LYS_LEAFLIST));
Michal Vaskod3678892020-05-21 10:06:58 +02006911
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006912 if (ctx_node->nodetype == LYS_LIST) {
Michal Vasko004d3152020-06-11 19:59:22 +02006913 /* get key count */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006914 if (ctx_node->flags & LYS_KEYLESS) {
Michal Vasko004d3152020-06-11 19:59:22 +02006915 return LY_EINVAL;
6916 }
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006917 for (key_count = 0, key = lysc_node_children(ctx_node, 0); key && (key->flags & LYS_KEY); key = key->next, ++key_count) {}
Michal Vasko004d3152020-06-11 19:59:22 +02006918 assert(key_count);
Michal Vaskod3678892020-05-21 10:06:58 +02006919
Michal Vasko004d3152020-06-11 19:59:22 +02006920 /* learn where the predicates end */
6921 e_idx = *tok_idx;
6922 while (key_count) {
6923 /* '[' */
6924 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
6925 return LY_EINVAL;
6926 }
6927 ++e_idx;
6928
6929 /* ']' */
6930 while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
6931 ++e_idx;
6932 }
6933 ++e_idx;
6934
6935 /* another presumably key predicate parsed */
6936 --key_count;
6937 }
Michal Vasko004d3152020-06-11 19:59:22 +02006938 } else {
6939 /* learn just where this single predicate ends */
6940 e_idx = *tok_idx;
6941
Michal Vaskod3678892020-05-21 10:06:58 +02006942 /* '[' */
Michal Vasko004d3152020-06-11 19:59:22 +02006943 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
6944 return LY_EINVAL;
6945 }
6946 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02006947
6948 /* ']' */
Michal Vasko004d3152020-06-11 19:59:22 +02006949 while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
6950 ++e_idx;
6951 }
6952 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02006953 }
6954
Michal Vasko004d3152020-06-11 19:59:22 +02006955 /* get the joined length of all the keys predicates/of the single leaf-list predicate */
6956 pred_len = (exp->tok_pos[e_idx - 1] + exp->tok_len[e_idx - 1]) - exp->tok_pos[*tok_idx];
6957
6958 /* turn logging off */
6959 prev_lo = ly_log_options(0);
6960
6961 /* parse the predicate(s) */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006962 LY_CHECK_GOTO(ret = ly_path_parse_predicate(ctx_node->module->ctx, ctx_node, exp->expr + exp->tok_pos[*tok_idx],
6963 pred_len, LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &exp2), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02006964
6965 /* compile */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006966 ret = ly_path_compile_predicate(ctx_node->module->ctx, cur_node, cur_mod, ctx_node, exp2, &pred_idx, format,
6967 prefix_data, predicates, pred_type);
Michal Vasko004d3152020-06-11 19:59:22 +02006968 LY_CHECK_GOTO(ret, cleanup);
6969
6970 /* success, the predicate must include all the needed information for hash-based search */
6971 *tok_idx = e_idx;
6972
6973cleanup:
6974 ly_log_options(prev_lo);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006975 lyxp_expr_free(ctx_node->module->ctx, exp2);
Michal Vasko004d3152020-06-11 19:59:22 +02006976 return ret;
Michal Vaskod3678892020-05-21 10:06:58 +02006977}
6978
6979/**
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006980 * @brief Search for/check the next schema node that could be the only matching schema node meaning the
6981 * data node(s) could be found using a single hash-based search.
6982 *
6983 * @param[in] node Next context node to check.
6984 * @param[in] name Expected node name.
6985 * @param[in] name_len Length of @p name.
6986 * @param[in] moveto_mod Expected node module.
6987 * @param[in] root_type XPath root type.
6988 * @param[in] no_prefix Whether the node name was unprefixed.
6989 * @param[in] format Prefix format.
6990 * @param[in,out] found Previously found node, is updated.
6991 * @return LY_SUCCESS on success,
6992 * @return LY_ENOT if the whole check failed and hashes cannot be used.
6993 */
6994static LY_ERR
6995eval_name_test_with_predicate_get_scnode(const struct lyd_node *node, const char *name, uint16_t name_len,
6996 const struct lys_module *moveto_mod, enum lyxp_node_type root_type, ly_bool no_prefix, LY_PREFIX_FORMAT format,
6997 const struct lysc_node **found)
6998{
6999 const struct lysc_node *scnode;
7000 const struct lys_module *mod;
7001 uint32_t idx = 0;
7002
7003continue_search:
Michal Vasko7d1d0912020-10-16 08:38:30 +02007004 scnode = NULL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007005 if (!node) {
7006 if (no_prefix && (format == LY_PREF_JSON)) {
7007 /* search all modules for a single match */
7008 while ((mod = ly_ctx_get_module_iter(moveto_mod->ctx, &idx))) {
7009 scnode = lys_find_child(NULL, mod, name, name_len, 0, 0);
7010 if (scnode) {
7011 /* we have found a match */
7012 break;
7013 }
7014 }
7015
Michal Vasko7d1d0912020-10-16 08:38:30 +02007016 if (!scnode) {
7017 /* all modules searched */
7018 idx = 0;
7019 }
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007020 } else {
7021 /* search in top-level */
7022 scnode = lys_find_child(NULL, moveto_mod, name, name_len, 0, 0);
7023 }
7024 } else if (!*found || (lysc_data_parent(*found) != node->schema)) {
7025 if (no_prefix && (format == LY_PREF_JSON)) {
7026 /* we must adjust the module to inherit the one from the context node */
7027 moveto_mod = node->schema->module;
7028 }
7029
7030 /* search in children, do not repeat the same search */
7031 scnode = lys_find_child(node->schema, moveto_mod, name, name_len, 0, 0);
Michal Vasko7d1d0912020-10-16 08:38:30 +02007032 } /* else skip redundant search */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007033
7034 /* additional context check */
7035 if (scnode && (root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) {
7036 scnode = NULL;
7037 }
7038
7039 if (scnode) {
7040 if (*found) {
7041 /* we found a schema node with the same name but at different level, give up, too complicated
7042 * (more hash-based searches would be required, not supported) */
7043 return LY_ENOT;
7044 } else {
7045 /* remember the found schema node and continue to make sure it can be used */
7046 *found = scnode;
7047 }
7048 }
7049
7050 if (idx) {
7051 /* continue searching all the following models */
7052 goto continue_search;
7053 }
7054
7055 return LY_SUCCESS;
7056}
7057
7058/**
Michal Vaskod3678892020-05-21 10:06:58 +02007059 * @brief Evaluate NameTest and any following Predicates. Logs directly on error.
7060 *
7061 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
7062 * [6] NodeTest ::= NameTest | NodeType '(' ')'
7063 * [7] NameTest ::= '*' | NCName ':' '*' | QName
7064 *
7065 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007066 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02007067 * @param[in] attr_axis Whether to search attributes or standard nodes.
7068 * @param[in] all_desc Whether to search all the descendants or children only.
7069 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7070 * @param[in] options XPath options.
7071 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7072 */
7073static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007074eval_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 +02007075 struct lyxp_set *set, uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02007076{
Michal Vaskod3678892020-05-21 10:06:58 +02007077 char *path;
Michal Vasko004d3152020-06-11 19:59:22 +02007078 const char *ncname, *ncname_dict = NULL;
7079 uint16_t ncname_len;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007080 const struct lys_module *moveto_mod = NULL;
7081 const struct lysc_node *scnode = NULL, *ctx_scnode;
Michal Vasko004d3152020-06-11 19:59:22 +02007082 struct ly_path_predicate *predicates = NULL;
7083 enum ly_path_pred_type pred_type = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02007084 LY_ERR rc = LY_SUCCESS;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007085 ly_bool no_prefix;
Michal Vaskod3678892020-05-21 10:06:58 +02007086
7087 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007088 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007089 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007090
7091 if (!set) {
7092 goto moveto;
7093 }
7094
Michal Vasko004d3152020-06-11 19:59:22 +02007095 ncname = &exp->expr[exp->tok_pos[*tok_idx - 1]];
7096 ncname_len = exp->tok_len[*tok_idx - 1];
Michal Vaskod3678892020-05-21 10:06:58 +02007097
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007098 /* get the first schema context node */
7099 if (set->used) {
7100 if (set->type == LYXP_SET_NODE_SET) {
7101 ctx_scnode = set->val.nodes[0].node ? set->val.nodes[0].node->schema : NULL;
7102 } else {
7103 ctx_scnode = set->val.scnodes[0].scnode;
7104 }
7105 } else {
7106 ctx_scnode = NULL;
7107 }
7108
7109 /* parse (and skip) module name, use any context node (if available) just so we get some moveto_mod if there
7110 * is no prefix for ::LY_PREF_JSON */
7111 rc = moveto_resolve_model(&ncname, &ncname_len, set, ctx_scnode, &moveto_mod);
Michal Vaskod3678892020-05-21 10:06:58 +02007112 LY_CHECK_GOTO(rc, cleanup);
7113
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007114 if (ncname_len == exp->tok_len[*tok_idx - 1]) {
7115 /* remember that there is no prefix */
7116 no_prefix = 1;
7117 } else {
7118 no_prefix = 0;
7119 }
7120
Michal Vaskod3678892020-05-21 10:06:58 +02007121 if (moveto_mod && !attr_axis && !all_desc && (set->type == LYXP_SET_NODE_SET)) {
7122 /* find the matching schema node in some parent in the context */
Radek Krejci1deb5be2020-08-26 16:43:36 +02007123 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007124 if (eval_name_test_with_predicate_get_scnode(set->val.nodes[i].node, ncname, ncname_len, moveto_mod,
7125 set->root_type, no_prefix, set->format, &scnode)) {
7126 /* check failed */
7127 scnode = NULL;
7128 break;
Michal Vaskod3678892020-05-21 10:06:58 +02007129 }
7130 }
7131
Michal Vasko004d3152020-06-11 19:59:22 +02007132 if (scnode && (scnode->nodetype & (LYS_LIST | LYS_LEAFLIST))) {
7133 /* try to create the predicates */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007134 if (eval_name_test_try_compile_predicates(exp, tok_idx, scnode, set->cur_mod, set->cur_node ?
7135 set->cur_node->schema : NULL, set->format, set->prefix_data, &predicates, &pred_type)) {
Michal Vasko004d3152020-06-11 19:59:22 +02007136 /* hashes cannot be used */
Michal Vaskod3678892020-05-21 10:06:58 +02007137 scnode = NULL;
7138 }
7139 }
7140 }
7141
Michal Vasko004d3152020-06-11 19:59:22 +02007142 if (!scnode && moveto_mod) {
7143 /* we are not able to match based on a schema node and not all the modules match,
7144 * use dictionary for efficient comparison */
Radek Krejci011e4aa2020-09-04 15:22:31 +02007145 LY_CHECK_GOTO(rc = lydict_insert(set->ctx, ncname, ncname_len, &ncname_dict), cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02007146 }
7147
7148moveto:
7149 /* move to the attribute(s), data node(s), or schema node(s) */
7150 if (attr_axis) {
7151 if (set && (options & LYXP_SCNODE_ALL)) {
7152 set_scnode_clear_ctx(set);
7153 } else {
7154 if (all_desc) {
Michal Vasko004d3152020-06-11 19:59:22 +02007155 rc = moveto_attr_alldesc(set, moveto_mod, ncname_dict);
Michal Vaskod3678892020-05-21 10:06:58 +02007156 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007157 rc = moveto_attr(set, moveto_mod, ncname_dict);
Michal Vaskod3678892020-05-21 10:06:58 +02007158 }
7159 LY_CHECK_GOTO(rc, cleanup);
7160 }
7161 } else {
7162 if (set && (options & LYXP_SCNODE_ALL)) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02007163 int64_t i;
7164
Michal Vaskod3678892020-05-21 10:06:58 +02007165 if (all_desc) {
Michal Vasko004d3152020-06-11 19:59:22 +02007166 rc = moveto_scnode_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007167 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007168 rc = moveto_scnode(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007169 }
7170 LY_CHECK_GOTO(rc, cleanup);
7171
7172 for (i = set->used - 1; i > -1; --i) {
7173 if (set->val.scnodes[i].in_ctx > 0) {
7174 break;
7175 }
7176 }
7177 if (i == -1) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007178 path = lysc_path(set->cur_scnode, LYSC_PATH_LOG, NULL, 0);
Michal Vaskod3678892020-05-21 10:06:58 +02007179 LOGWRN(set->ctx, "Schema node \"%.*s\" not found (%.*s) with context node \"%s\".",
Michal Vasko69730152020-10-09 16:30:07 +02007180 ncname_len, ncname, ncname - exp->expr, exp->expr, path);
Michal Vaskod3678892020-05-21 10:06:58 +02007181 free(path);
7182 }
7183 } else {
7184 if (all_desc) {
Michal Vasko004d3152020-06-11 19:59:22 +02007185 rc = moveto_node_alldesc(set, moveto_mod, ncname_dict);
Michal Vaskod3678892020-05-21 10:06:58 +02007186 } else {
7187 if (scnode) {
7188 /* we can find the nodes using hashes */
Michal Vasko004d3152020-06-11 19:59:22 +02007189 rc = moveto_node_hash(set, scnode, predicates);
Michal Vaskod3678892020-05-21 10:06:58 +02007190 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007191 rc = moveto_node(set, moveto_mod, ncname_dict);
Michal Vaskod3678892020-05-21 10:06:58 +02007192 }
7193 }
7194 LY_CHECK_GOTO(rc, cleanup);
7195 }
7196 }
7197
7198 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007199 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7200 rc = eval_predicate(exp, tok_idx, set, options, 1);
Michal Vaskod3678892020-05-21 10:06:58 +02007201 LY_CHECK_RET(rc);
7202 }
7203
7204cleanup:
Michal Vaskodb51a8d2020-05-27 15:22:29 +02007205 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007206 lydict_remove(set->ctx, ncname_dict);
Michal Vaskof7e16e22020-10-21 09:24:39 +02007207 ly_path_predicates_free(set->ctx, pred_type, predicates);
Michal Vaskodb51a8d2020-05-27 15:22:29 +02007208 }
Michal Vaskod3678892020-05-21 10:06:58 +02007209 return rc;
7210}
7211
7212/**
7213 * @brief Evaluate NodeType and any following Predicates. Logs directly on error.
7214 *
7215 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
7216 * [6] NodeTest ::= NameTest | NodeType '(' ')'
7217 * [8] NodeType ::= 'text' | 'node'
7218 *
7219 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007220 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02007221 * @param[in] attr_axis Whether to search attributes or standard nodes.
7222 * @param[in] all_desc Whether to search all the descendants or children only.
7223 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7224 * @param[in] options XPath options.
7225 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7226 */
7227static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007228eval_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 +02007229 struct lyxp_set *set, uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02007230{
7231 LY_ERR rc;
7232
7233 /* TODO */
7234 (void)attr_axis;
7235 (void)all_desc;
7236
7237 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007238 assert(exp->tok_len[*tok_idx] == 4);
Michal Vaskod3678892020-05-21 10:06:58 +02007239 if (set->type == LYXP_SET_SCNODE_SET) {
7240 set_scnode_clear_ctx(set);
7241 /* just for the debug message below */
7242 set = NULL;
7243 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007244 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "node", 4)) {
Michal Vaskod3678892020-05-21 10:06:58 +02007245 rc = xpath_node(NULL, 0, set, options);
7246 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007247 assert(!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "text", 4));
Michal Vaskod3678892020-05-21 10:06:58 +02007248 rc = xpath_text(NULL, 0, set, options);
7249 }
7250 LY_CHECK_RET(rc);
7251 }
7252 }
7253 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007254 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007255 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007256
7257 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02007258 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
Michal Vaskod3678892020-05-21 10:06:58 +02007259 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007260 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007261 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007262
7263 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007264 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
Michal Vaskod3678892020-05-21 10:06:58 +02007265 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007266 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007267 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007268
7269 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007270 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7271 rc = eval_predicate(exp, tok_idx, set, options, 1);
Michal Vaskod3678892020-05-21 10:06:58 +02007272 LY_CHECK_RET(rc);
7273 }
7274
7275 return LY_SUCCESS;
7276}
7277
7278/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02007279 * @brief Evaluate RelativeLocationPath. Logs directly on error.
7280 *
7281 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
7282 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
Michal Vaskod3678892020-05-21 10:06:58 +02007283 * [6] NodeTest ::= NameTest | NodeType '(' ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007284 *
7285 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007286 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007287 * @param[in] all_desc Whether to search all the descendants or children only.
7288 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7289 * @param[in] options XPath options.
7290 * @return LY_ERR (YL_EINCOMPLETE on unresolved when)
7291 */
7292static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007293eval_relative_location_path(const struct lyxp_expr *exp, uint16_t *tok_idx, ly_bool all_desc, struct lyxp_set *set,
7294 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007295{
Radek Krejci857189e2020-09-01 13:26:36 +02007296 ly_bool attr_axis;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007297 LY_ERR rc;
7298
7299 goto step;
7300 do {
7301 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02007302 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007303 all_desc = 0;
7304 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007305 assert(exp->tok_len[*tok_idx] == 2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007306 all_desc = 1;
7307 }
7308 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007309 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007310 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007311
7312step:
Michal Vaskod3678892020-05-21 10:06:58 +02007313 /* evaluate abbreviated axis '@'? if any */
Michal Vasko004d3152020-06-11 19:59:22 +02007314 if (exp->tokens[*tok_idx] == LYXP_TOKEN_AT) {
Michal Vaskod3678892020-05-21 10:06:58 +02007315 attr_axis = 1;
7316
7317 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007318 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007319 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007320 } else {
7321 attr_axis = 0;
7322 }
7323
Michal Vasko03ff5a72019-09-11 13:49:33 +02007324 /* Step */
Michal Vasko004d3152020-06-11 19:59:22 +02007325 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007326 case LYXP_TOKEN_DOT:
7327 /* evaluate '.' */
7328 if (set && (options & LYXP_SCNODE_ALL)) {
7329 rc = moveto_scnode_self(set, all_desc, options);
7330 } else {
7331 rc = moveto_self(set, all_desc, options);
7332 }
7333 LY_CHECK_RET(rc);
7334 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007335 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007336 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007337 break;
7338
7339 case LYXP_TOKEN_DDOT:
7340 /* evaluate '..' */
7341 if (set && (options & LYXP_SCNODE_ALL)) {
7342 rc = moveto_scnode_parent(set, all_desc, options);
7343 } else {
7344 rc = moveto_parent(set, all_desc, options);
7345 }
7346 LY_CHECK_RET(rc);
7347 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007348 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007349 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007350 break;
7351
Michal Vasko03ff5a72019-09-11 13:49:33 +02007352 case LYXP_TOKEN_NAMETEST:
Michal Vaskod3678892020-05-21 10:06:58 +02007353 /* evaluate NameTest Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007354 rc = eval_name_test_with_predicate(exp, tok_idx, attr_axis, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007355 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02007356 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007357
Michal Vaskod3678892020-05-21 10:06:58 +02007358 case LYXP_TOKEN_NODETYPE:
7359 /* evaluate NodeType Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007360 rc = eval_node_type_with_predicate(exp, tok_idx, attr_axis, all_desc, set, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007361 LY_CHECK_RET(rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007362 break;
7363
7364 default:
Michal Vasko02a77382019-09-12 11:47:35 +02007365 LOGINT_RET(set ? set->ctx : NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007366 }
Michal Vasko004d3152020-06-11 19:59:22 +02007367 } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007368
7369 return LY_SUCCESS;
7370}
7371
7372/**
7373 * @brief Evaluate AbsoluteLocationPath. Logs directly on error.
7374 *
7375 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
7376 *
7377 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007378 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007379 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7380 * @param[in] options XPath options.
7381 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7382 */
7383static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007384eval_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 +02007385{
Radek Krejci857189e2020-09-01 13:26:36 +02007386 ly_bool all_desc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007387
7388 if (set) {
7389 /* no matter what tokens follow, we need to be at the root */
Michal Vaskob0099a92020-08-31 14:55:23 +02007390 LY_CHECK_RET(moveto_root(set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007391 }
7392
7393 /* '/' RelativeLocationPath? */
Michal Vasko004d3152020-06-11 19:59:22 +02007394 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007395 /* evaluate '/' - deferred */
7396 all_desc = 0;
7397 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007398 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007399 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007400
Michal Vasko004d3152020-06-11 19:59:22 +02007401 if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007402 return LY_SUCCESS;
7403 }
Michal Vasko004d3152020-06-11 19:59:22 +02007404 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007405 case LYXP_TOKEN_DOT:
7406 case LYXP_TOKEN_DDOT:
7407 case LYXP_TOKEN_AT:
7408 case LYXP_TOKEN_NAMETEST:
7409 case LYXP_TOKEN_NODETYPE:
Michal Vaskob0099a92020-08-31 14:55:23 +02007410 LY_CHECK_RET(eval_relative_location_path(exp, tok_idx, all_desc, set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007411 break;
7412 default:
7413 break;
7414 }
7415
Michal Vasko03ff5a72019-09-11 13:49:33 +02007416 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02007417 /* '//' RelativeLocationPath */
Michal Vasko03ff5a72019-09-11 13:49:33 +02007418 /* evaluate '//' - deferred so as not to waste memory by remembering all the nodes */
7419 all_desc = 1;
7420 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007421 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007422 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007423
Michal Vaskob0099a92020-08-31 14:55:23 +02007424 LY_CHECK_RET(eval_relative_location_path(exp, tok_idx, all_desc, set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007425 }
7426
7427 return LY_SUCCESS;
7428}
7429
7430/**
7431 * @brief Evaluate FunctionCall. Logs directly on error.
7432 *
Michal Vaskod3678892020-05-21 10:06:58 +02007433 * [11] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007434 *
7435 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007436 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007437 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7438 * @param[in] options XPath options.
7439 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7440 */
7441static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007442eval_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 +02007443{
7444 LY_ERR rc;
Michal Vasko69730152020-10-09 16:30:07 +02007445
Radek Krejci1deb5be2020-08-26 16:43:36 +02007446 LY_ERR (*xpath_func)(struct lyxp_set **, uint16_t, struct lyxp_set *, uint32_t) = NULL;
Michal Vasko0cbf54f2019-12-16 10:01:06 +01007447 uint16_t arg_count = 0, i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007448 struct lyxp_set **args = NULL, **args_aux;
7449
7450 if (set) {
7451 /* FunctionName */
Michal Vasko004d3152020-06-11 19:59:22 +02007452 switch (exp->tok_len[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007453 case 3:
Michal Vasko004d3152020-06-11 19:59:22 +02007454 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007455 xpath_func = &xpath_not;
Michal Vasko004d3152020-06-11 19:59:22 +02007456 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007457 xpath_func = &xpath_sum;
7458 }
7459 break;
7460 case 4:
Michal Vasko004d3152020-06-11 19:59:22 +02007461 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007462 xpath_func = &xpath_lang;
Michal Vasko004d3152020-06-11 19:59:22 +02007463 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007464 xpath_func = &xpath_last;
Michal Vasko004d3152020-06-11 19:59:22 +02007465 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007466 xpath_func = &xpath_name;
Michal Vasko004d3152020-06-11 19:59:22 +02007467 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007468 xpath_func = &xpath_true;
7469 }
7470 break;
7471 case 5:
Michal Vasko004d3152020-06-11 19:59:22 +02007472 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007473 xpath_func = &xpath_count;
Michal Vasko004d3152020-06-11 19:59:22 +02007474 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007475 xpath_func = &xpath_false;
Michal Vasko004d3152020-06-11 19:59:22 +02007476 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007477 xpath_func = &xpath_floor;
Michal Vasko004d3152020-06-11 19:59:22 +02007478 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007479 xpath_func = &xpath_round;
Michal Vasko004d3152020-06-11 19:59:22 +02007480 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007481 xpath_func = &xpath_deref;
7482 }
7483 break;
7484 case 6:
Michal Vasko004d3152020-06-11 19:59:22 +02007485 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007486 xpath_func = &xpath_concat;
Michal Vasko004d3152020-06-11 19:59:22 +02007487 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007488 xpath_func = &xpath_number;
Michal Vasko004d3152020-06-11 19:59:22 +02007489 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007490 xpath_func = &xpath_string;
7491 }
7492 break;
7493 case 7:
Michal Vasko004d3152020-06-11 19:59:22 +02007494 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007495 xpath_func = &xpath_boolean;
Michal Vasko004d3152020-06-11 19:59:22 +02007496 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007497 xpath_func = &xpath_ceiling;
Michal Vasko004d3152020-06-11 19:59:22 +02007498 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007499 xpath_func = &xpath_current;
7500 }
7501 break;
7502 case 8:
Michal Vasko004d3152020-06-11 19:59:22 +02007503 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007504 xpath_func = &xpath_contains;
Michal Vasko004d3152020-06-11 19:59:22 +02007505 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007506 xpath_func = &xpath_position;
Michal Vasko004d3152020-06-11 19:59:22 +02007507 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007508 xpath_func = &xpath_re_match;
7509 }
7510 break;
7511 case 9:
Michal Vasko004d3152020-06-11 19:59:22 +02007512 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007513 xpath_func = &xpath_substring;
Michal Vasko004d3152020-06-11 19:59:22 +02007514 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007515 xpath_func = &xpath_translate;
7516 }
7517 break;
7518 case 10:
Michal Vasko004d3152020-06-11 19:59:22 +02007519 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007520 xpath_func = &xpath_local_name;
Michal Vasko004d3152020-06-11 19:59:22 +02007521 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007522 xpath_func = &xpath_enum_value;
Michal Vasko004d3152020-06-11 19:59:22 +02007523 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007524 xpath_func = &xpath_bit_is_set;
7525 }
7526 break;
7527 case 11:
Michal Vasko004d3152020-06-11 19:59:22 +02007528 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007529 xpath_func = &xpath_starts_with;
7530 }
7531 break;
7532 case 12:
Michal Vasko004d3152020-06-11 19:59:22 +02007533 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007534 xpath_func = &xpath_derived_from;
7535 }
7536 break;
7537 case 13:
Michal Vasko004d3152020-06-11 19:59:22 +02007538 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007539 xpath_func = &xpath_namespace_uri;
Michal Vasko004d3152020-06-11 19:59:22 +02007540 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007541 xpath_func = &xpath_string_length;
7542 }
7543 break;
7544 case 15:
Michal Vasko004d3152020-06-11 19:59:22 +02007545 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007546 xpath_func = &xpath_normalize_space;
Michal Vasko004d3152020-06-11 19:59:22 +02007547 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007548 xpath_func = &xpath_substring_after;
7549 }
7550 break;
7551 case 16:
Michal Vasko004d3152020-06-11 19:59:22 +02007552 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007553 xpath_func = &xpath_substring_before;
7554 }
7555 break;
7556 case 20:
Michal Vasko004d3152020-06-11 19:59:22 +02007557 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007558 xpath_func = &xpath_derived_from_or_self;
7559 }
7560 break;
7561 }
7562
7563 if (!xpath_func) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007564 LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INFUNC, exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007565 return LY_EVALID;
7566 }
7567 }
7568
7569 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007570 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007571 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007572
7573 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02007574 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007575 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007576 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007577 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007578
7579 /* ( Expr ( ',' Expr )* )? */
Michal Vasko004d3152020-06-11 19:59:22 +02007580 if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007581 if (set) {
7582 args = malloc(sizeof *args);
7583 LY_CHECK_ERR_GOTO(!args, LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7584 arg_count = 1;
7585 args[0] = set_copy(set);
7586 if (!args[0]) {
7587 rc = LY_EMEM;
7588 goto cleanup;
7589 }
7590
Michal Vasko004d3152020-06-11 19:59:22 +02007591 rc = eval_expr_select(exp, tok_idx, 0, args[0], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007592 LY_CHECK_GOTO(rc, cleanup);
7593 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007594 rc = eval_expr_select(exp, tok_idx, 0, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007595 LY_CHECK_GOTO(rc, cleanup);
7596 }
7597 }
Michal Vasko004d3152020-06-11 19:59:22 +02007598 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007599 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007600 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007601 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007602
7603 if (set) {
7604 ++arg_count;
7605 args_aux = realloc(args, arg_count * sizeof *args);
7606 LY_CHECK_ERR_GOTO(!args_aux, arg_count--; LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7607 args = args_aux;
7608 args[arg_count - 1] = set_copy(set);
7609 if (!args[arg_count - 1]) {
7610 rc = LY_EMEM;
7611 goto cleanup;
7612 }
7613
Michal Vasko004d3152020-06-11 19:59:22 +02007614 rc = eval_expr_select(exp, tok_idx, 0, args[arg_count - 1], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007615 LY_CHECK_GOTO(rc, cleanup);
7616 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007617 rc = eval_expr_select(exp, tok_idx, 0, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007618 LY_CHECK_GOTO(rc, cleanup);
7619 }
7620 }
7621
7622 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007623 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007624 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007625 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007626 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007627
7628 if (set) {
7629 /* evaluate function */
7630 rc = xpath_func(args, arg_count, set, options);
7631
7632 if (options & LYXP_SCNODE_ALL) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007633 /* merge all nodes from arg evaluations */
7634 for (i = 0; i < arg_count; ++i) {
7635 set_scnode_clear_ctx(args[i]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007636 lyxp_set_scnode_merge(set, args[i]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007637 }
7638 }
7639 } else {
7640 rc = LY_SUCCESS;
7641 }
7642
7643cleanup:
7644 for (i = 0; i < arg_count; ++i) {
7645 lyxp_set_free(args[i]);
7646 }
7647 free(args);
7648
7649 return rc;
7650}
7651
7652/**
7653 * @brief Evaluate Number. Logs directly on error.
7654 *
7655 * @param[in] ctx Context for errors.
7656 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007657 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007658 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7659 * @return LY_ERR
7660 */
7661static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007662eval_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 +02007663{
7664 long double num;
7665 char *endptr;
7666
7667 if (set) {
7668 errno = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02007669 num = strtold(&exp->expr[exp->tok_pos[*tok_idx]], &endptr);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007670 if (errno) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007671 LOGVAL(ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]);
7672 LOGVAL(ctx, LY_VLOG_LYD, set->cur_node, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double (%s).",
Michal Vasko69730152020-10-09 16:30:07 +02007673 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]], strerror(errno));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007674 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02007675 } else if (endptr - &exp->expr[exp->tok_pos[*tok_idx]] != exp->tok_len[*tok_idx]) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007676 LOGVAL(ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]);
7677 LOGVAL(ctx, LY_VLOG_LYD, set->cur_node, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double.",
Michal Vasko69730152020-10-09 16:30:07 +02007678 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007679 return LY_EVALID;
7680 }
7681
7682 set_fill_number(set, num);
7683 }
7684
7685 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007686 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007687 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007688 return LY_SUCCESS;
7689}
7690
7691/**
7692 * @brief Evaluate PathExpr. Logs directly on error.
7693 *
Michal Vaskod3678892020-05-21 10:06:58 +02007694 * [12] PathExpr ::= LocationPath | PrimaryExpr Predicate*
Michal Vasko03ff5a72019-09-11 13:49:33 +02007695 * | PrimaryExpr Predicate* '/' RelativeLocationPath
7696 * | PrimaryExpr Predicate* '//' RelativeLocationPath
7697 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
Michal Vaskod3678892020-05-21 10:06:58 +02007698 * [10] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall
Michal Vasko03ff5a72019-09-11 13:49:33 +02007699 *
7700 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007701 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007702 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7703 * @param[in] options XPath options.
7704 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7705 */
7706static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007707eval_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 +02007708{
Radek Krejci857189e2020-09-01 13:26:36 +02007709 ly_bool all_desc, parent_pos_pred;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007710 LY_ERR rc;
7711
Michal Vasko004d3152020-06-11 19:59:22 +02007712 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007713 case LYXP_TOKEN_PAR1:
7714 /* '(' Expr ')' */
7715
7716 /* '(' */
7717 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007718 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007719 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007720
7721 /* Expr */
Michal Vasko004d3152020-06-11 19:59:22 +02007722 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007723 LY_CHECK_RET(rc);
7724
7725 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007726 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007727 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007728 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007729 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007730
7731 parent_pos_pred = 0;
7732 goto predicate;
7733
7734 case LYXP_TOKEN_DOT:
7735 case LYXP_TOKEN_DDOT:
7736 case LYXP_TOKEN_AT:
7737 case LYXP_TOKEN_NAMETEST:
7738 case LYXP_TOKEN_NODETYPE:
7739 /* RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007740 rc = eval_relative_location_path(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007741 LY_CHECK_RET(rc);
7742 break;
7743
7744 case LYXP_TOKEN_FUNCNAME:
7745 /* FunctionCall */
7746 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007747 rc = eval_function_call(exp, tok_idx, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007748 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007749 rc = eval_function_call(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007750 }
7751 LY_CHECK_RET(rc);
7752
7753 parent_pos_pred = 1;
7754 goto predicate;
7755
Michal Vasko3e48bf32020-06-01 08:39:07 +02007756 case LYXP_TOKEN_OPER_PATH:
7757 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02007758 /* AbsoluteLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007759 rc = eval_absolute_location_path(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007760 LY_CHECK_RET(rc);
7761 break;
7762
7763 case LYXP_TOKEN_LITERAL:
7764 /* Literal */
7765 if (!set || (options & LYXP_SCNODE_ALL)) {
7766 if (set) {
7767 set_scnode_clear_ctx(set);
7768 }
Michal Vasko004d3152020-06-11 19:59:22 +02007769 eval_literal(exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007770 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007771 eval_literal(exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007772 }
7773
7774 parent_pos_pred = 1;
7775 goto predicate;
7776
7777 case LYXP_TOKEN_NUMBER:
7778 /* Number */
7779 if (!set || (options & LYXP_SCNODE_ALL)) {
7780 if (set) {
7781 set_scnode_clear_ctx(set);
7782 }
Michal Vasko004d3152020-06-11 19:59:22 +02007783 rc = eval_number(NULL, exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007784 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007785 rc = eval_number(set->ctx, exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007786 }
7787 LY_CHECK_RET(rc);
7788
7789 parent_pos_pred = 1;
7790 goto predicate;
7791
7792 default:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007793 LOGVAL(set->ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INTOK, lyxp_print_token(exp->tokens[*tok_idx]),
Michal Vasko69730152020-10-09 16:30:07 +02007794 &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007795 return LY_EVALID;
7796 }
7797
7798 return LY_SUCCESS;
7799
7800predicate:
7801 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007802 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7803 rc = eval_predicate(exp, tok_idx, set, options, parent_pos_pred);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007804 LY_CHECK_RET(rc);
7805 }
7806
7807 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007808 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007809
7810 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02007811 if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007812 all_desc = 0;
7813 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007814 all_desc = 1;
7815 }
7816
7817 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007818 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007819 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007820
Michal Vasko004d3152020-06-11 19:59:22 +02007821 rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007822 LY_CHECK_RET(rc);
7823 }
7824
7825 return LY_SUCCESS;
7826}
7827
7828/**
7829 * @brief Evaluate UnionExpr. Logs directly on error.
7830 *
Michal Vaskod3678892020-05-21 10:06:58 +02007831 * [20] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007832 *
7833 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007834 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007835 * @param[in] repeat How many times this expression is repeated.
7836 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7837 * @param[in] options XPath options.
7838 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7839 */
7840static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007841eval_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 +02007842{
7843 LY_ERR rc = LY_SUCCESS;
7844 struct lyxp_set orig_set, set2;
7845 uint16_t i;
7846
7847 assert(repeat);
7848
7849 set_init(&orig_set, set);
7850 set_init(&set2, set);
7851
7852 set_fill_set(&orig_set, set);
7853
Michal Vasko004d3152020-06-11 19:59:22 +02007854 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007855 LY_CHECK_GOTO(rc, cleanup);
7856
7857 /* ('|' PathExpr)* */
7858 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007859 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_UNI);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007860 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007861 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007862 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007863
7864 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007865 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007866 LY_CHECK_GOTO(rc, cleanup);
7867 continue;
7868 }
7869
7870 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02007871 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007872 LY_CHECK_GOTO(rc, cleanup);
7873
7874 /* eval */
7875 if (options & LYXP_SCNODE_ALL) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01007876 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007877 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007878 rc = moveto_union(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007879 LY_CHECK_GOTO(rc, cleanup);
7880 }
7881 }
7882
7883cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007884 lyxp_set_free_content(&orig_set);
7885 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007886 return rc;
7887}
7888
7889/**
7890 * @brief Evaluate UnaryExpr. Logs directly on error.
7891 *
Michal Vaskod3678892020-05-21 10:06:58 +02007892 * [19] UnaryExpr ::= UnionExpr | '-' UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007893 *
7894 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007895 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007896 * @param[in] repeat How many times this expression is repeated.
7897 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7898 * @param[in] options XPath options.
7899 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7900 */
7901static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007902eval_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 +02007903{
7904 LY_ERR rc;
7905 uint16_t this_op, i;
7906
7907 assert(repeat);
7908
7909 /* ('-')+ */
Michal Vasko004d3152020-06-11 19:59:22 +02007910 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007911 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007912 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 +02007913
7914 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007915 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007916 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007917 }
7918
Michal Vasko004d3152020-06-11 19:59:22 +02007919 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNARY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007920 LY_CHECK_RET(rc);
7921
7922 if (set && (repeat % 2)) {
7923 if (options & LYXP_SCNODE_ALL) {
7924 warn_operands(set->ctx, set, NULL, 1, exp->expr, exp->tok_pos[this_op]);
7925 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007926 rc = moveto_op_math(set, NULL, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007927 LY_CHECK_RET(rc);
7928 }
7929 }
7930
7931 return LY_SUCCESS;
7932}
7933
7934/**
7935 * @brief Evaluate MultiplicativeExpr. Logs directly on error.
7936 *
Michal Vaskod3678892020-05-21 10:06:58 +02007937 * [18] MultiplicativeExpr ::= UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007938 * | MultiplicativeExpr '*' UnaryExpr
7939 * | MultiplicativeExpr 'div' UnaryExpr
7940 * | MultiplicativeExpr 'mod' UnaryExpr
7941 *
7942 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007943 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007944 * @param[in] repeat How many times this expression is repeated.
7945 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7946 * @param[in] options XPath options.
7947 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7948 */
7949static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007950eval_multiplicative_expr(const struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set,
7951 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007952{
7953 LY_ERR rc;
7954 uint16_t this_op;
7955 struct lyxp_set orig_set, set2;
7956 uint16_t i;
7957
7958 assert(repeat);
7959
7960 set_init(&orig_set, set);
7961 set_init(&set2, set);
7962
7963 set_fill_set(&orig_set, set);
7964
Michal Vasko004d3152020-06-11 19:59:22 +02007965 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007966 LY_CHECK_GOTO(rc, cleanup);
7967
7968 /* ('*' / 'div' / 'mod' UnaryExpr)* */
7969 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007970 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007971
Michal Vasko004d3152020-06-11 19:59:22 +02007972 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007973 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007974 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007975 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007976
7977 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007978 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007979 LY_CHECK_GOTO(rc, cleanup);
7980 continue;
7981 }
7982
7983 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02007984 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007985 LY_CHECK_GOTO(rc, cleanup);
7986
7987 /* eval */
7988 if (options & LYXP_SCNODE_ALL) {
7989 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007990 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007991 set_scnode_clear_ctx(set);
7992 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007993 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007994 LY_CHECK_GOTO(rc, cleanup);
7995 }
7996 }
7997
7998cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007999 lyxp_set_free_content(&orig_set);
8000 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008001 return rc;
8002}
8003
8004/**
8005 * @brief Evaluate AdditiveExpr. Logs directly on error.
8006 *
Michal Vaskod3678892020-05-21 10:06:58 +02008007 * [17] AdditiveExpr ::= MultiplicativeExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008008 * | AdditiveExpr '+' MultiplicativeExpr
8009 * | AdditiveExpr '-' MultiplicativeExpr
8010 *
8011 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008012 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008013 * @param[in] repeat How many times this expression is repeated.
8014 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8015 * @param[in] options XPath options.
8016 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8017 */
8018static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008019eval_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 +02008020{
8021 LY_ERR rc;
8022 uint16_t this_op;
8023 struct lyxp_set orig_set, set2;
8024 uint16_t i;
8025
8026 assert(repeat);
8027
8028 set_init(&orig_set, set);
8029 set_init(&set2, set);
8030
8031 set_fill_set(&orig_set, set);
8032
Michal Vasko004d3152020-06-11 19:59:22 +02008033 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008034 LY_CHECK_GOTO(rc, cleanup);
8035
8036 /* ('+' / '-' MultiplicativeExpr)* */
8037 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008038 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008039
Michal Vasko004d3152020-06-11 19:59:22 +02008040 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008041 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02008042 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008043 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008044
8045 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02008046 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008047 LY_CHECK_GOTO(rc, cleanup);
8048 continue;
8049 }
8050
8051 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008052 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008053 LY_CHECK_GOTO(rc, cleanup);
8054
8055 /* eval */
8056 if (options & LYXP_SCNODE_ALL) {
8057 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008058 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008059 set_scnode_clear_ctx(set);
8060 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008061 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008062 LY_CHECK_GOTO(rc, cleanup);
8063 }
8064 }
8065
8066cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008067 lyxp_set_free_content(&orig_set);
8068 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008069 return rc;
8070}
8071
8072/**
8073 * @brief Evaluate RelationalExpr. Logs directly on error.
8074 *
Michal Vaskod3678892020-05-21 10:06:58 +02008075 * [16] RelationalExpr ::= AdditiveExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008076 * | RelationalExpr '<' AdditiveExpr
8077 * | RelationalExpr '>' AdditiveExpr
8078 * | RelationalExpr '<=' AdditiveExpr
8079 * | RelationalExpr '>=' AdditiveExpr
8080 *
8081 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008082 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008083 * @param[in] repeat How many times this expression is repeated.
8084 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8085 * @param[in] options XPath options.
8086 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8087 */
8088static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008089eval_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 +02008090{
8091 LY_ERR rc;
8092 uint16_t this_op;
8093 struct lyxp_set orig_set, set2;
8094 uint16_t i;
8095
8096 assert(repeat);
8097
8098 set_init(&orig_set, set);
8099 set_init(&set2, set);
8100
8101 set_fill_set(&orig_set, set);
8102
Michal Vasko004d3152020-06-11 19:59:22 +02008103 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008104 LY_CHECK_GOTO(rc, cleanup);
8105
8106 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
8107 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008108 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008109
Michal Vasko004d3152020-06-11 19:59:22 +02008110 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_COMP);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008111 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02008112 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008113 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008114
8115 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02008116 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008117 LY_CHECK_GOTO(rc, cleanup);
8118 continue;
8119 }
8120
8121 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008122 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008123 LY_CHECK_GOTO(rc, cleanup);
8124
8125 /* eval */
8126 if (options & LYXP_SCNODE_ALL) {
8127 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008128 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008129 set_scnode_clear_ctx(set);
8130 } else {
8131 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options);
8132 LY_CHECK_GOTO(rc, cleanup);
8133 }
8134 }
8135
8136cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008137 lyxp_set_free_content(&orig_set);
8138 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008139 return rc;
8140}
8141
8142/**
8143 * @brief Evaluate EqualityExpr. Logs directly on error.
8144 *
Michal Vaskod3678892020-05-21 10:06:58 +02008145 * [15] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008146 * | EqualityExpr '!=' RelationalExpr
8147 *
8148 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008149 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008150 * @param[in] repeat How many times this expression is repeated.
8151 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8152 * @param[in] options XPath options.
8153 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8154 */
8155static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008156eval_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 +02008157{
8158 LY_ERR rc;
8159 uint16_t this_op;
8160 struct lyxp_set orig_set, set2;
8161 uint16_t i;
8162
8163 assert(repeat);
8164
8165 set_init(&orig_set, set);
8166 set_init(&set2, set);
8167
8168 set_fill_set(&orig_set, set);
8169
Michal Vasko004d3152020-06-11 19:59:22 +02008170 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008171 LY_CHECK_GOTO(rc, cleanup);
8172
8173 /* ('=' / '!=' RelationalExpr)* */
8174 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008175 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008176
Michal Vasko004d3152020-06-11 19:59:22 +02008177 assert((exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_EQUAL) || (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_NEQUAL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008178 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02008179 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008180 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008181
8182 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02008183 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008184 LY_CHECK_GOTO(rc, cleanup);
8185 continue;
8186 }
8187
8188 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008189 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008190 LY_CHECK_GOTO(rc, cleanup);
8191
8192 /* eval */
8193 if (options & LYXP_SCNODE_ALL) {
8194 warn_operands(set->ctx, set, &set2, 0, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vasko004d3152020-06-11 19:59:22 +02008195 warn_equality_value(exp, set, *tok_idx - 1, this_op - 1, *tok_idx - 1);
8196 warn_equality_value(exp, &set2, this_op - 1, this_op - 1, *tok_idx - 1);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008197 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008198 set_scnode_clear_ctx(set);
8199 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008200 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options);
8201 LY_CHECK_GOTO(rc, cleanup);
8202 }
8203 }
8204
8205cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008206 lyxp_set_free_content(&orig_set);
8207 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008208 return rc;
8209}
8210
8211/**
8212 * @brief Evaluate AndExpr. Logs directly on error.
8213 *
Michal Vaskod3678892020-05-21 10:06:58 +02008214 * [14] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008215 *
8216 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008217 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008218 * @param[in] repeat How many times this expression is repeated.
8219 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8220 * @param[in] options XPath options.
8221 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8222 */
8223static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008224eval_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 +02008225{
8226 LY_ERR rc;
8227 struct lyxp_set orig_set, set2;
8228 uint16_t i;
8229
8230 assert(repeat);
8231
8232 set_init(&orig_set, set);
8233 set_init(&set2, set);
8234
8235 set_fill_set(&orig_set, set);
8236
Michal Vasko004d3152020-06-11 19:59:22 +02008237 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008238 LY_CHECK_GOTO(rc, cleanup);
8239
8240 /* cast to boolean, we know that will be the final result */
8241 if (set && (options & LYXP_SCNODE_ALL)) {
8242 set_scnode_clear_ctx(set);
8243 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008244 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008245 }
8246
8247 /* ('and' EqualityExpr)* */
8248 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008249 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
8250 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (!set || !set->val.bln ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008251 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008252 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008253
8254 /* lazy evaluation */
Michal Vasko004d3152020-06-11 19:59:22 +02008255 if (!set || ((set->type == LYXP_SET_BOOLEAN) && !set->val.bln)) {
8256 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008257 LY_CHECK_GOTO(rc, cleanup);
8258 continue;
8259 }
8260
8261 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008262 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008263 LY_CHECK_GOTO(rc, cleanup);
8264
8265 /* eval - just get boolean value actually */
8266 if (set->type == LYXP_SET_SCNODE_SET) {
8267 set_scnode_clear_ctx(&set2);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008268 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008269 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008270 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008271 set_fill_set(set, &set2);
8272 }
8273 }
8274
8275cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008276 lyxp_set_free_content(&orig_set);
8277 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008278 return rc;
8279}
8280
8281/**
8282 * @brief Evaluate OrExpr. Logs directly on error.
8283 *
Michal Vaskod3678892020-05-21 10:06:58 +02008284 * [13] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008285 *
8286 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008287 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008288 * @param[in] repeat How many times this expression is repeated.
8289 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8290 * @param[in] options XPath options.
8291 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8292 */
8293static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008294eval_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 +02008295{
8296 LY_ERR rc;
8297 struct lyxp_set orig_set, set2;
8298 uint16_t i;
8299
8300 assert(repeat);
8301
8302 set_init(&orig_set, set);
8303 set_init(&set2, set);
8304
8305 set_fill_set(&orig_set, set);
8306
Michal Vasko004d3152020-06-11 19:59:22 +02008307 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008308 LY_CHECK_GOTO(rc, cleanup);
8309
8310 /* cast to boolean, we know that will be the final result */
8311 if (set && (options & LYXP_SCNODE_ALL)) {
8312 set_scnode_clear_ctx(set);
8313 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008314 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008315 }
8316
8317 /* ('or' AndExpr)* */
8318 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008319 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
8320 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (!set || set->val.bln ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008321 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008322 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008323
8324 /* lazy evaluation */
Michal Vasko004d3152020-06-11 19:59:22 +02008325 if (!set || ((set->type == LYXP_SET_BOOLEAN) && set->val.bln)) {
8326 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008327 LY_CHECK_GOTO(rc, cleanup);
8328 continue;
8329 }
8330
8331 set_fill_set(&set2, &orig_set);
8332 /* expr_type cound have been LYXP_EXPR_NONE in all these later calls (except for the first one),
8333 * but it does not matter */
Michal Vasko004d3152020-06-11 19:59:22 +02008334 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008335 LY_CHECK_GOTO(rc, cleanup);
8336
8337 /* eval - just get boolean value actually */
8338 if (set->type == LYXP_SET_SCNODE_SET) {
8339 set_scnode_clear_ctx(&set2);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008340 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008341 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008342 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008343 set_fill_set(set, &set2);
8344 }
8345 }
8346
8347cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008348 lyxp_set_free_content(&orig_set);
8349 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008350 return rc;
8351}
8352
8353/**
Michal Vasko004d3152020-06-11 19:59:22 +02008354 * @brief Decide what expression is at the pointer @p tok_idx and evaluate it accordingly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008355 *
8356 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008357 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008358 * @param[in] etype Expression type to evaluate.
8359 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8360 * @param[in] options XPath options.
8361 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8362 */
8363static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008364eval_expr_select(const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_expr_type etype, struct lyxp_set *set,
8365 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008366{
8367 uint16_t i, count;
8368 enum lyxp_expr_type next_etype;
8369 LY_ERR rc;
8370
8371 /* process operator repeats */
Michal Vasko004d3152020-06-11 19:59:22 +02008372 if (!exp->repeat[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008373 next_etype = LYXP_EXPR_NONE;
8374 } else {
8375 /* find etype repeat */
Radek Krejci1e008d22020-08-17 11:37:37 +02008376 for (i = 0; exp->repeat[*tok_idx][i] > etype; ++i) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02008377
8378 /* select one-priority lower because etype expression called us */
8379 if (i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008380 next_etype = exp->repeat[*tok_idx][i - 1];
Michal Vasko03ff5a72019-09-11 13:49:33 +02008381 /* count repeats for that expression */
Radek Krejci1e008d22020-08-17 11:37:37 +02008382 for (count = 0; i && exp->repeat[*tok_idx][i - 1] == next_etype; ++count, --i) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02008383 } else {
8384 next_etype = LYXP_EXPR_NONE;
8385 }
8386 }
8387
8388 /* decide what expression are we parsing based on the repeat */
8389 switch (next_etype) {
8390 case LYXP_EXPR_OR:
Michal Vasko004d3152020-06-11 19:59:22 +02008391 rc = eval_or_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008392 break;
8393 case LYXP_EXPR_AND:
Michal Vasko004d3152020-06-11 19:59:22 +02008394 rc = eval_and_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008395 break;
8396 case LYXP_EXPR_EQUALITY:
Michal Vasko004d3152020-06-11 19:59:22 +02008397 rc = eval_equality_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008398 break;
8399 case LYXP_EXPR_RELATIONAL:
Michal Vasko004d3152020-06-11 19:59:22 +02008400 rc = eval_relational_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008401 break;
8402 case LYXP_EXPR_ADDITIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02008403 rc = eval_additive_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008404 break;
8405 case LYXP_EXPR_MULTIPLICATIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02008406 rc = eval_multiplicative_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008407 break;
8408 case LYXP_EXPR_UNARY:
Michal Vasko004d3152020-06-11 19:59:22 +02008409 rc = eval_unary_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008410 break;
8411 case LYXP_EXPR_UNION:
Michal Vasko004d3152020-06-11 19:59:22 +02008412 rc = eval_union_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008413 break;
8414 case LYXP_EXPR_NONE:
Michal Vasko004d3152020-06-11 19:59:22 +02008415 rc = eval_path_expr(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008416 break;
8417 default:
8418 LOGINT_RET(set->ctx);
8419 }
8420
8421 return rc;
8422}
8423
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008424/**
8425 * @brief Get root type.
8426 *
8427 * @param[in] ctx_node Context node.
8428 * @param[in] ctx_scnode Schema context node.
8429 * @param[in] options XPath options.
8430 * @return Root type.
8431 */
8432static enum lyxp_node_type
Radek Krejci1deb5be2020-08-26 16:43:36 +02008433lyxp_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 +01008434{
Michal Vasko6b26e742020-07-17 15:02:10 +02008435 const struct lysc_node *op;
8436
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008437 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008438 /* schema */
Radek Krejci1e008d22020-08-17 11:37:37 +02008439 for (op = ctx_scnode; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02008440
8441 if (op || (options & LYXP_SCNODE)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008442 /* general root that can access everything */
8443 return LYXP_NODE_ROOT;
8444 } else if (!ctx_scnode || (ctx_scnode->flags & LYS_CONFIG_W)) {
8445 /* root context node can access only config data (because we said so, it is unspecified) */
8446 return LYXP_NODE_ROOT_CONFIG;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008447 }
Michal Vasko6b26e742020-07-17 15:02:10 +02008448 return LYXP_NODE_ROOT;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008449 }
8450
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008451 /* data */
Michal Vasko6b26e742020-07-17 15:02:10 +02008452 op = ctx_node ? ctx_node->schema : NULL;
Michal Vaskod989ba02020-08-24 10:59:24 +02008453 for ( ; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02008454
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008455 if (op || !(options & LYXP_SCHEMA)) {
8456 /* general root that can access everything */
8457 return LYXP_NODE_ROOT;
8458 } else if (!ctx_node || (ctx_node->schema->flags & LYS_CONFIG_W)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008459 /* root context node can access only config data (because we said so, it is unspecified) */
8460 return LYXP_NODE_ROOT_CONFIG;
8461 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008462 return LYXP_NODE_ROOT;
8463}
8464
Michal Vasko03ff5a72019-09-11 13:49:33 +02008465LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008466lyxp_eval(const struct lyxp_expr *exp, const struct lys_module *cur_mod, LY_PREFIX_FORMAT format, void *prefix_data,
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008467 const struct lyd_node *ctx_node, const struct lyd_node *tree, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008468{
Michal Vasko004d3152020-06-11 19:59:22 +02008469 uint16_t tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008470 LY_ERR rc;
8471
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008472 LY_CHECK_ARG_RET(NULL, exp, set, LY_EINVAL);
8473 if (!cur_mod && ((format == LY_PREF_SCHEMA) || (format == LY_PREF_SCHEMA_RESOLVED))) {
8474 LOGARG(NULL, "Current module must be set if schema format is used.");
8475 return LY_EINVAL;
Michal Vasko004d3152020-06-11 19:59:22 +02008476 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008477
8478 /* prepare set for evaluation */
Michal Vasko03ff5a72019-09-11 13:49:33 +02008479 memset(set, 0, sizeof *set);
Michal Vaskod3678892020-05-21 10:06:58 +02008480 set->type = LYXP_SET_NODE_SET;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008481 set->root_type = lyxp_get_root_type(ctx_node, NULL, options);
8482 set_insert_node(set, (struct lyd_node *)ctx_node, 0, ctx_node ? LYXP_NODE_ELEM : set->root_type, 0);
8483
8484 set->ctx = (struct ly_ctx *)(cur_mod ? cur_mod->ctx : (ctx_node ? LYD_CTX(ctx_node) : (tree ? LYD_CTX(tree) : NULL)));
8485 set->cur_node = ctx_node;
8486 for (set->context_op = ctx_node ? ctx_node->schema : NULL;
Radek Krejci0f969882020-08-21 16:56:47 +02008487 set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF));
8488 set->context_op = set->context_op->parent) {}
Michal Vaskof03ed032020-03-04 13:31:44 +01008489 set->tree = tree;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008490 set->cur_mod = cur_mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008491 set->format = format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008492 set->prefix_data = prefix_data;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008493
8494 /* evaluate */
Michal Vasko004d3152020-06-11 19:59:22 +02008495 rc = eval_expr_select(exp, &tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008496 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02008497 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008498 }
8499
Michal Vasko03ff5a72019-09-11 13:49:33 +02008500 return rc;
8501}
8502
8503#if 0
8504
8505/* full xml printing of set elements, not used currently */
8506
8507void
8508lyxp_set_print_xml(FILE *f, struct lyxp_set *set)
8509{
8510 uint32_t i;
8511 char *str_num;
8512 struct lyout out;
8513
8514 memset(&out, 0, sizeof out);
8515
8516 out.type = LYOUT_STREAM;
8517 out.method.f = f;
8518
8519 switch (set->type) {
8520 case LYXP_SET_EMPTY:
Michal Vasko5233e962020-08-14 14:26:20 +02008521 ly_print_(&out, "Empty XPath set\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008522 break;
8523 case LYXP_SET_BOOLEAN:
Michal Vasko5233e962020-08-14 14:26:20 +02008524 ly_print_(&out, "Boolean XPath set:\n");
8525 ly_print_(&out, "%s\n\n", set->value.bool ? "true" : "false");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008526 break;
8527 case LYXP_SET_STRING:
Michal Vasko5233e962020-08-14 14:26:20 +02008528 ly_print_(&out, "String XPath set:\n");
8529 ly_print_(&out, "\"%s\"\n\n", set->value.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008530 break;
8531 case LYXP_SET_NUMBER:
Michal Vasko5233e962020-08-14 14:26:20 +02008532 ly_print_(&out, "Number XPath set:\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008533
8534 if (isnan(set->value.num)) {
8535 str_num = strdup("NaN");
8536 } else if ((set->value.num == 0) || (set->value.num == -0.0f)) {
8537 str_num = strdup("0");
8538 } else if (isinf(set->value.num) && !signbit(set->value.num)) {
8539 str_num = strdup("Infinity");
8540 } else if (isinf(set->value.num) && signbit(set->value.num)) {
8541 str_num = strdup("-Infinity");
8542 } else if ((long long)set->value.num == set->value.num) {
8543 if (asprintf(&str_num, "%lld", (long long)set->value.num) == -1) {
8544 str_num = NULL;
8545 }
8546 } else {
8547 if (asprintf(&str_num, "%03.1Lf", set->value.num) == -1) {
8548 str_num = NULL;
8549 }
8550 }
8551 if (!str_num) {
8552 LOGMEM;
8553 return;
8554 }
Michal Vasko5233e962020-08-14 14:26:20 +02008555 ly_print_(&out, "%s\n\n", str_num);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008556 free(str_num);
8557 break;
8558 case LYXP_SET_NODE_SET:
Michal Vasko5233e962020-08-14 14:26:20 +02008559 ly_print_(&out, "Node XPath set:\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008560
8561 for (i = 0; i < set->used; ++i) {
Michal Vasko5233e962020-08-14 14:26:20 +02008562 ly_print_(&out, "%d. ", i + 1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008563 switch (set->node_type[i]) {
8564 case LYXP_NODE_ROOT_ALL:
Michal Vasko5233e962020-08-14 14:26:20 +02008565 ly_print_(&out, "ROOT all\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008566 break;
8567 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko5233e962020-08-14 14:26:20 +02008568 ly_print_(&out, "ROOT config\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008569 break;
8570 case LYXP_NODE_ROOT_STATE:
Michal Vasko5233e962020-08-14 14:26:20 +02008571 ly_print_(&out, "ROOT state\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008572 break;
8573 case LYXP_NODE_ROOT_NOTIF:
Michal Vasko5233e962020-08-14 14:26:20 +02008574 ly_print_(&out, "ROOT notification \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008575 break;
8576 case LYXP_NODE_ROOT_RPC:
Michal Vasko5233e962020-08-14 14:26:20 +02008577 ly_print_(&out, "ROOT rpc \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008578 break;
8579 case LYXP_NODE_ROOT_OUTPUT:
Michal Vasko5233e962020-08-14 14:26:20 +02008580 ly_print_(&out, "ROOT output \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008581 break;
8582 case LYXP_NODE_ELEM:
Michal Vasko5233e962020-08-14 14:26:20 +02008583 ly_print_(&out, "ELEM \"%s\"\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008584 xml_print_node(&out, 1, set->value.nodes[i], 1, LYP_FORMAT);
Michal Vasko5233e962020-08-14 14:26:20 +02008585 ly_print_(&out, "\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008586 break;
8587 case LYXP_NODE_TEXT:
Michal Vasko5233e962020-08-14 14:26:20 +02008588 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 +02008589 break;
8590 case LYXP_NODE_ATTR:
Michal Vasko5233e962020-08-14 14:26:20 +02008591 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 +02008592 break;
8593 }
8594 }
8595 break;
8596 }
8597}
8598
8599#endif
8600
8601LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008602lyxp_set_cast(struct lyxp_set *set, enum lyxp_set_type target)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008603{
8604 long double num;
8605 char *str;
8606 LY_ERR rc;
8607
8608 if (!set || (set->type == target)) {
8609 return LY_SUCCESS;
8610 }
8611
8612 /* it's not possible to convert anything into a node set */
Michal Vaskod3678892020-05-21 10:06:58 +02008613 assert(target != LYXP_SET_NODE_SET);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008614
8615 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vaskod3678892020-05-21 10:06:58 +02008616 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008617 return LY_EINVAL;
8618 }
8619
8620 /* to STRING */
Michal Vaskod3678892020-05-21 10:06:58 +02008621 if ((target == LYXP_SET_STRING) || ((target == LYXP_SET_NUMBER) && (set->type == LYXP_SET_NODE_SET))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008622 switch (set->type) {
8623 case LYXP_SET_NUMBER:
8624 if (isnan(set->val.num)) {
8625 set->val.str = strdup("NaN");
8626 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8627 } else if ((set->val.num == 0) || (set->val.num == -0.0f)) {
8628 set->val.str = strdup("0");
8629 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8630 } else if (isinf(set->val.num) && !signbit(set->val.num)) {
8631 set->val.str = strdup("Infinity");
8632 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8633 } else if (isinf(set->val.num) && signbit(set->val.num)) {
8634 set->val.str = strdup("-Infinity");
8635 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8636 } else if ((long long)set->val.num == set->val.num) {
8637 if (asprintf(&str, "%lld", (long long)set->val.num) == -1) {
8638 LOGMEM_RET(set->ctx);
8639 }
8640 set->val.str = str;
8641 } else {
8642 if (asprintf(&str, "%03.1Lf", set->val.num) == -1) {
8643 LOGMEM_RET(set->ctx);
8644 }
8645 set->val.str = str;
8646 }
8647 break;
8648 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02008649 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008650 set->val.str = strdup("true");
8651 } else {
8652 set->val.str = strdup("false");
8653 }
8654 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM);
8655 break;
8656 case LYXP_SET_NODE_SET:
8657 assert(set->used);
8658
8659 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008660 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008661
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008662 rc = cast_node_set_to_string(set, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008663 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02008664 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008665 set->val.str = str;
8666 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008667 default:
8668 LOGINT_RET(set->ctx);
8669 }
8670 set->type = LYXP_SET_STRING;
8671 }
8672
8673 /* to NUMBER */
8674 if (target == LYXP_SET_NUMBER) {
8675 switch (set->type) {
8676 case LYXP_SET_STRING:
8677 num = cast_string_to_number(set->val.str);
Michal Vaskod3678892020-05-21 10:06:58 +02008678 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008679 set->val.num = num;
8680 break;
8681 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02008682 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008683 set->val.num = 1;
8684 } else {
8685 set->val.num = 0;
8686 }
8687 break;
8688 default:
8689 LOGINT_RET(set->ctx);
8690 }
8691 set->type = LYXP_SET_NUMBER;
8692 }
8693
8694 /* to BOOLEAN */
8695 if (target == LYXP_SET_BOOLEAN) {
8696 switch (set->type) {
8697 case LYXP_SET_NUMBER:
8698 if ((set->val.num == 0) || (set->val.num == -0.0f) || isnan(set->val.num)) {
Michal Vasko004d3152020-06-11 19:59:22 +02008699 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008700 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02008701 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008702 }
8703 break;
8704 case LYXP_SET_STRING:
8705 if (set->val.str[0]) {
Michal Vaskod3678892020-05-21 10:06:58 +02008706 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008707 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008708 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02008709 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008710 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008711 }
8712 break;
8713 case LYXP_SET_NODE_SET:
Michal Vaskod3678892020-05-21 10:06:58 +02008714 if (set->used) {
8715 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008716 set->val.bln = 1;
Michal Vaskod3678892020-05-21 10:06:58 +02008717 } else {
8718 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008719 set->val.bln = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02008720 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008721 break;
8722 default:
8723 LOGINT_RET(set->ctx);
8724 }
8725 set->type = LYXP_SET_BOOLEAN;
8726 }
8727
Michal Vasko03ff5a72019-09-11 13:49:33 +02008728 return LY_SUCCESS;
8729}
8730
8731LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008732lyxp_atomize(const struct lyxp_expr *exp, const struct lys_module *cur_mod, LY_PREFIX_FORMAT format, void *prefix_data,
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008733 const struct lysc_node *ctx_scnode, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008734{
Michal Vasko004d3152020-06-11 19:59:22 +02008735 uint16_t tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008736
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008737 LY_CHECK_ARG_RET(NULL, exp, set, LY_EINVAL);
8738 if (!cur_mod && ((format == LY_PREF_SCHEMA) || (format == LY_PREF_SCHEMA_RESOLVED))) {
8739 LOGARG(NULL, "Current module must be set if schema format is used.");
8740 return LY_EINVAL;
Michal Vasko004d3152020-06-11 19:59:22 +02008741 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008742
8743 /* prepare set for evaluation */
Michal Vasko03ff5a72019-09-11 13:49:33 +02008744 memset(set, 0, sizeof *set);
8745 set->type = LYXP_SET_SCNODE_SET;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008746 set->root_type = lyxp_get_root_type(NULL, ctx_scnode, options);
8747 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, ctx_scnode, ctx_scnode ? LYXP_NODE_ELEM : set->root_type, NULL));
Michal Vasko5c4e5892019-11-14 12:31:38 +01008748 set->val.scnodes[0].in_ctx = -2;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008749
8750 set->ctx = cur_mod ? cur_mod->ctx : (ctx_scnode ? ctx_scnode->module->ctx : NULL);
8751 set->cur_scnode = ctx_scnode;
Michal Vasko6b26e742020-07-17 15:02:10 +02008752 for (set->context_op = ctx_scnode;
Radek Krejci0f969882020-08-21 16:56:47 +02008753 set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF));
8754 set->context_op = set->context_op->parent) {}
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008755 set->cur_mod = cur_mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008756 set->format = format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008757 set->prefix_data = prefix_data;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008758
8759 /* evaluate */
Michal Vasko004d3152020-06-11 19:59:22 +02008760 return eval_expr_select(exp, &tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008761}
Michal Vaskod43d71a2020-08-07 14:54:58 +02008762
8763API const char *
8764lyxp_get_expr(const struct lyxp_expr *path)
8765{
8766 if (!path) {
8767 return NULL;
8768 }
8769
8770 return path->expr;
8771}