blob: 874065c1ee8990fb6b81a7cdac33763c8e4e65a9 [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/**
5337 * @brief Check @p node as a part of NameTest processing.
5338 *
5339 * @param[in] node Node to check.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005340 * @param[in] ctx_node Context node.
5341 * @param[in] set Set to read general context from.
Michal Vaskod3678892020-05-21 10:06:58 +02005342 * @param[in] node_name Node name in the dictionary to move to, NULL for any node.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005343 * @param[in] moveto_mod Expected module of the node, NULL for no prefix.
Michal Vaskocdad7122020-11-09 21:04:44 +01005344 * @param[in] options XPath options.
Michal Vasko6346ece2019-09-24 13:12:53 +02005345 * @return LY_ERR (LY_ENOT if node does not match, LY_EINCOMPLETE on unresolved when,
5346 * LY_EINVAL if netither node nor any children match)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005347 */
5348static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005349moveto_node_check(const struct lyd_node *node, const struct lyd_node *ctx_node, const struct lyxp_set *set,
Michal Vaskocdad7122020-11-09 21:04:44 +01005350 const char *node_name, const struct lys_module *moveto_mod, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005351{
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005352 if (!moveto_mod && (!node_name || strcmp(node_name, "*"))) {
5353 switch (set->format) {
5354 case LY_PREF_SCHEMA:
5355 case LY_PREF_SCHEMA_RESOLVED:
5356 /* use current module */
5357 moveto_mod = set->cur_mod;
5358 break;
5359 case LY_PREF_JSON:
5360 /* inherit module of the context node, if any */
5361 if (ctx_node) {
5362 moveto_mod = ctx_node->schema->module;
5363 }
5364 break;
5365 case LY_PREF_XML:
5366 /* not defined */
5367 LOGINT(set->ctx);
5368 return LY_EINVAL;
5369 }
5370 }
5371
Michal Vasko03ff5a72019-09-11 13:49:33 +02005372 /* module check */
5373 if (moveto_mod && (node->schema->module != moveto_mod)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005374 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005375 }
5376
Michal Vasko5c4e5892019-11-14 12:31:38 +01005377 /* context check */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005378 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005379 return LY_EINVAL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005380 } else if (set->context_op && (node->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) &&
5381 (node->schema != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005382 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005383 }
5384
5385 /* name check */
Michal Vasko61ac2f62020-05-25 12:39:51 +02005386 if (node_name && strcmp(node_name, "*") && (node->schema->name != node_name)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005387 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005388 }
5389
Michal Vaskoa1424542019-11-14 16:08:52 +01005390 /* when check */
Michal Vaskocdad7122020-11-09 21:04:44 +01005391 if (!(options & LYXP_IGNORE_WHEN) && lyd_has_when(node) && !(node->flags & LYD_WHEN_TRUE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005392 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01005393 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005394
5395 /* match */
5396 return LY_SUCCESS;
5397}
5398
5399/**
5400 * @brief Check @p node as a part of schema NameTest processing.
5401 *
5402 * @param[in] node Schema node to check.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005403 * @param[in] ctx_scnode Context node.
5404 * @param[in] set Set to read general context from.
Michal Vaskod3678892020-05-21 10:06:58 +02005405 * @param[in] node_name Node name in the dictionary to move to, NULL for any nodes.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005406 * @param[in] moveto_mod Expected module of the node, NULL for no prefix.
Michal Vasko6346ece2019-09-24 13:12:53 +02005407 * @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 +02005408 */
5409static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005410moveto_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 +02005411 const char *node_name, const struct lys_module *moveto_mod)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005412{
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005413 if (!moveto_mod && (!node_name || strcmp(node_name, "*"))) {
5414 switch (set->format) {
5415 case LY_PREF_SCHEMA:
5416 case LY_PREF_SCHEMA_RESOLVED:
5417 /* use current module */
5418 moveto_mod = set->cur_mod;
5419 break;
5420 case LY_PREF_JSON:
5421 /* inherit module of the context node, if any */
5422 if (ctx_scnode) {
5423 moveto_mod = ctx_scnode->module;
5424 }
5425 break;
5426 case LY_PREF_XML:
5427 /* not defined */
5428 LOGINT(set->ctx);
5429 return LY_EINVAL;
5430 }
5431 }
5432
Michal Vasko03ff5a72019-09-11 13:49:33 +02005433 /* module check */
Michal Vaskod3678892020-05-21 10:06:58 +02005434 if (moveto_mod && (node->module != moveto_mod)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005435 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005436 }
5437
5438 /* context check */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005439 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (node->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005440 return LY_EINVAL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005441 } else if (set->context_op && (node->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && (node != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005442 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005443 }
5444
5445 /* name check */
Michal Vasko61ac2f62020-05-25 12:39:51 +02005446 if (node_name && strcmp(node_name, "*") && (node->name != node_name)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005447 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005448 }
5449
5450 /* match */
5451 return LY_SUCCESS;
5452}
5453
5454/**
Michal Vaskod3678892020-05-21 10:06:58 +02005455 * @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 +02005456 *
5457 * @param[in,out] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005458 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005459 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01005460 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005461 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5462 */
5463static LY_ERR
Michal Vaskocdad7122020-11-09 21:04:44 +01005464moveto_node(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005465{
Michal Vaskof03ed032020-03-04 13:31:44 +01005466 uint32_t i;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005467 const struct lyd_node *siblings, *sub, *ctx_node;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005468 LY_ERR rc;
5469
Michal Vaskod3678892020-05-21 10:06:58 +02005470 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005471 return LY_SUCCESS;
5472 }
5473
5474 if (set->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005475 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 +02005476 return LY_EVALID;
5477 }
5478
Michal Vasko03ff5a72019-09-11 13:49:33 +02005479 for (i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02005480 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005481
5482 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 +01005483 assert(!set->val.nodes[i].node);
Michal Vaskod3678892020-05-21 10:06:58 +02005484
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005485 /* search in all the trees */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005486 ctx_node = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02005487 siblings = set->tree;
Michal Vasko5bfd4be2020-06-23 13:26:19 +02005488 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005489 /* search in children */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005490 ctx_node = set->val.nodes[i].node;
5491 siblings = lyd_child(ctx_node);
Michal Vaskod3678892020-05-21 10:06:58 +02005492 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005493
Michal Vaskod3678892020-05-21 10:06:58 +02005494 for (sub = siblings; sub; sub = sub->next) {
Michal Vaskocdad7122020-11-09 21:04:44 +01005495 rc = moveto_node_check(sub, ctx_node, set, ncname, moveto_mod, options);
Michal Vaskod3678892020-05-21 10:06:58 +02005496 if (rc == LY_SUCCESS) {
5497 if (!replaced) {
5498 set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i);
5499 replaced = 1;
5500 } else {
5501 set_insert_node(set, sub, 0, LYXP_NODE_ELEM, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005502 }
Michal Vaskod3678892020-05-21 10:06:58 +02005503 ++i;
5504 } else if (rc == LY_EINCOMPLETE) {
5505 return rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005506 }
5507 }
5508
5509 if (!replaced) {
5510 /* no match */
5511 set_remove_node(set, i);
5512 }
5513 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005514
5515 return LY_SUCCESS;
5516}
5517
5518/**
Michal Vaskod3678892020-05-21 10:06:58 +02005519 * @brief Move context @p set to a node using hashes. Result is LYXP_SET_NODE_SET (or LYXP_SET_EMPTY).
5520 * Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005521 *
5522 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005523 * @param[in] scnode Matching node schema.
Michal Vasko004d3152020-06-11 19:59:22 +02005524 * @param[in] predicates If @p scnode is ::LYS_LIST or ::LYS_LEAFLIST, the predicates specifying a single instance.
Michal Vaskocdad7122020-11-09 21:04:44 +01005525 * @param[in] options XPath options.
Michal Vaskod3678892020-05-21 10:06:58 +02005526 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5527 */
5528static LY_ERR
Michal Vaskocdad7122020-11-09 21:04:44 +01005529moveto_node_hash(struct lyxp_set *set, const struct lysc_node *scnode, const struct ly_path_predicate *predicates,
5530 uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02005531{
Michal Vasko004d3152020-06-11 19:59:22 +02005532 LY_ERR ret = LY_SUCCESS;
Michal Vaskod3678892020-05-21 10:06:58 +02005533 uint32_t i;
Michal Vaskod3678892020-05-21 10:06:58 +02005534 const struct lyd_node *siblings;
Michal Vasko004d3152020-06-11 19:59:22 +02005535 struct lyd_node *sub, *inst = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02005536
Michal Vasko004d3152020-06-11 19:59:22 +02005537 assert(scnode && (!(scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)) || predicates));
Michal Vaskod3678892020-05-21 10:06:58 +02005538
5539 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02005540 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005541 }
5542
5543 if (set->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005544 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 +02005545 ret = LY_EVALID;
5546 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005547 }
5548
5549 /* context check for all the nodes since we have the schema node */
5550 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) {
5551 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02005552 goto cleanup;
Michal Vasko69730152020-10-09 16:30:07 +02005553 } else if (set->context_op && (scnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) &&
5554 (scnode != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005555 lyxp_set_free_content(set);
5556 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +02005557 }
5558
5559 /* create specific data instance if needed */
5560 if (scnode->nodetype == LYS_LIST) {
5561 LY_CHECK_GOTO(ret = lyd_create_list(scnode, predicates, &inst), cleanup);
5562 } else if (scnode->nodetype == LYS_LEAFLIST) {
5563 LY_CHECK_GOTO(ret = lyd_create_term2(scnode, &predicates[0].value, &inst), cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02005564 }
5565
5566 for (i = 0; i < set->used; ) {
Michal Vaskod3678892020-05-21 10:06:58 +02005567 siblings = NULL;
5568
5569 if ((set->val.nodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.nodes[i].type == LYXP_NODE_ROOT)) {
5570 assert(!set->val.nodes[i].node);
5571
5572 /* search in all the trees */
5573 siblings = set->tree;
5574 } else if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
5575 /* search in children */
Radek Krejcia1c1e542020-09-29 16:06:52 +02005576 siblings = lyd_child(set->val.nodes[i].node);
Michal Vaskod3678892020-05-21 10:06:58 +02005577 }
5578
5579 /* find the node using hashes */
Michal Vasko004d3152020-06-11 19:59:22 +02005580 if (inst) {
5581 lyd_find_sibling_first(siblings, inst, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02005582 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02005583 lyd_find_sibling_val(siblings, scnode, NULL, 0, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02005584 }
5585
5586 /* when check */
Michal Vaskocdad7122020-11-09 21:04:44 +01005587 if (!(options & LYXP_IGNORE_WHEN) && sub && lyd_has_when(sub) && !(sub->flags & LYD_WHEN_TRUE)) {
Michal Vasko004d3152020-06-11 19:59:22 +02005588 ret = LY_EINCOMPLETE;
5589 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005590 }
5591
5592 if (sub) {
5593 /* pos filled later */
Michal Vaskocb1b7c02020-07-03 13:38:12 +02005594 set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i);
Michal Vaskod3678892020-05-21 10:06:58 +02005595 ++i;
Michal Vaskocb1b7c02020-07-03 13:38:12 +02005596 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005597 /* no match */
5598 set_remove_node(set, i);
5599 }
5600 }
5601
Michal Vasko004d3152020-06-11 19:59:22 +02005602cleanup:
5603 lyd_free_tree(inst);
5604 return ret;
Michal Vaskod3678892020-05-21 10:06:58 +02005605}
5606
5607/**
5608 * @brief Move context @p set to a schema node. Result is LYXP_SET_SCNODE_SET (or LYXP_SET_EMPTY).
5609 *
5610 * @param[in,out] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005611 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005612 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005613 * @param[in] options XPath options.
5614 * @return LY_ERR
5615 */
5616static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005617moveto_scnode(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005618{
Radek Krejci857189e2020-09-01 13:26:36 +02005619 ly_bool temp_ctx = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005620 uint32_t getnext_opts;
5621 uint32_t orig_used, i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005622 uint32_t mod_idx;
Michal Vasko519fd602020-05-26 12:17:39 +02005623 const struct lysc_node *iter, *start_parent;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005624 const struct lys_module *mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005625
Michal Vaskod3678892020-05-21 10:06:58 +02005626 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005627 return LY_SUCCESS;
5628 }
5629
5630 if (set->type != LYXP_SET_SCNODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005631 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 +02005632 return LY_EVALID;
5633 }
5634
Michal Vaskocafad9d2019-11-07 15:20:03 +01005635 /* getnext opts */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01005636 getnext_opts = 0;
Michal Vaskocafad9d2019-11-07 15:20:03 +01005637 if (options & LYXP_SCNODE_OUTPUT) {
5638 getnext_opts |= LYS_GETNEXT_OUTPUT;
5639 }
5640
Michal Vasko03ff5a72019-09-11 13:49:33 +02005641 orig_used = set->used;
5642 for (i = 0; i < orig_used; ++i) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005643 uint32_t idx;
5644
Michal Vasko03ff5a72019-09-11 13:49:33 +02005645 if (set->val.scnodes[i].in_ctx != 1) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01005646 if (set->val.scnodes[i].in_ctx != -2) {
5647 continue;
5648 }
5649
5650 /* remember context node */
5651 set->val.scnodes[i].in_ctx = -1;
Michal Vaskoec4df482019-12-16 10:02:18 +01005652 } else {
5653 set->val.scnodes[i].in_ctx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005654 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005655
5656 start_parent = set->val.scnodes[i].scnode;
5657
5658 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 +02005659 /* 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 +02005660 * it can be in a top-level augment (the root node itself is useless in this case) */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005661 mod_idx = 0;
Michal Vasko9e9f26d2020-10-12 16:31:33 +02005662 while ((mod = (struct lys_module *)ly_ctx_get_module_iter(set->ctx, &mod_idx))) {
Michal Vasko519fd602020-05-26 12:17:39 +02005663 iter = NULL;
Michal Vasko509de4d2019-12-10 14:51:30 +01005664 /* module may not be implemented */
Michal Vasko519fd602020-05-26 12:17:39 +02005665 while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005666 if (!moveto_scnode_check(iter, NULL, set, ncname, moveto_mod)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005667 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, &idx));
5668
Michal Vasko03ff5a72019-09-11 13:49:33 +02005669 /* we need to prevent these nodes from being considered in this moveto */
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005670 if ((idx < orig_used) && (idx > i)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005671 set->val.scnodes[idx].in_ctx = 2;
5672 temp_ctx = 1;
5673 }
5674 }
5675 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005676 }
5677
Michal Vasko519fd602020-05-26 12:17:39 +02005678 } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
5679 iter = NULL;
5680 while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005681 if (!moveto_scnode_check(iter, start_parent, set, ncname, moveto_mod)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005682 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, &idx));
5683
5684 if ((idx < orig_used) && (idx > i)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005685 set->val.scnodes[idx].in_ctx = 2;
5686 temp_ctx = 1;
5687 }
5688 }
5689 }
5690 }
5691 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005692
5693 /* correct temporary in_ctx values */
5694 if (temp_ctx) {
5695 for (i = 0; i < orig_used; ++i) {
5696 if (set->val.scnodes[i].in_ctx == 2) {
5697 set->val.scnodes[i].in_ctx = 1;
5698 }
5699 }
5700 }
5701
5702 return LY_SUCCESS;
5703}
5704
5705/**
Michal Vaskod3678892020-05-21 10:06:58 +02005706 * @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 +02005707 * Context position aware.
5708 *
5709 * @param[in] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005710 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005711 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01005712 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005713 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5714 */
5715static LY_ERR
Michal Vaskocdad7122020-11-09 21:04:44 +01005716moveto_node_alldesc(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005717{
5718 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005719 const struct lyd_node *next, *elem, *start;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005720 struct lyxp_set ret_set;
5721 LY_ERR rc;
5722
Michal Vaskod3678892020-05-21 10:06:58 +02005723 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005724 return LY_SUCCESS;
5725 }
5726
5727 if (set->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005728 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 +02005729 return LY_EVALID;
5730 }
5731
Michal Vasko9f96a052020-03-10 09:41:45 +01005732 /* replace the original nodes (and throws away all text and meta nodes, root is replaced by a child) */
Michal Vaskocdad7122020-11-09 21:04:44 +01005733 rc = moveto_node(set, NULL, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005734 LY_CHECK_RET(rc);
5735
Michal Vasko6346ece2019-09-24 13:12:53 +02005736 /* this loop traverses all the nodes in the set and adds/keeps only those that match qname */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005737 set_init(&ret_set, set);
5738 for (i = 0; i < set->used; ++i) {
5739
5740 /* TREE DFS */
5741 start = set->val.nodes[i].node;
5742 for (elem = next = start; elem; elem = next) {
Michal Vaskocdad7122020-11-09 21:04:44 +01005743 rc = moveto_node_check(elem, start, set, ncname, moveto_mod, options);
Michal Vasko6346ece2019-09-24 13:12:53 +02005744 if (!rc) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005745 /* add matching node into result set */
5746 set_insert_node(&ret_set, elem, 0, LYXP_NODE_ELEM, ret_set.used);
5747 if (set_dup_node_check(set, elem, LYXP_NODE_ELEM, i)) {
5748 /* the node is a duplicate, we'll process it later in the set */
5749 goto skip_children;
5750 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005751 } else if (rc == LY_EINCOMPLETE) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005752 return rc;
5753 } else if (rc == LY_EINVAL) {
5754 goto skip_children;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005755 }
5756
5757 /* TREE DFS NEXT ELEM */
5758 /* select element for the next run - children first */
Radek Krejcia1c1e542020-09-29 16:06:52 +02005759 next = lyd_child(elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005760 if (!next) {
5761skip_children:
5762 /* no children, so try siblings, but only if it's not the start,
5763 * that is considered to be the root and it's siblings are not traversed */
5764 if (elem != start) {
5765 next = elem->next;
5766 } else {
5767 break;
5768 }
5769 }
5770 while (!next) {
5771 /* no siblings, go back through the parents */
5772 if ((struct lyd_node *)elem->parent == start) {
5773 /* we are done, no next element to process */
5774 break;
5775 }
5776 /* parent is already processed, go to its sibling */
5777 elem = (struct lyd_node *)elem->parent;
5778 next = elem->next;
5779 }
5780 }
5781 }
5782
5783 /* make the temporary set the current one */
5784 ret_set.ctx_pos = set->ctx_pos;
5785 ret_set.ctx_size = set->ctx_size;
Michal Vaskod3678892020-05-21 10:06:58 +02005786 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005787 memcpy(set, &ret_set, sizeof *set);
5788
5789 return LY_SUCCESS;
5790}
5791
5792/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005793 * @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 +02005794 *
5795 * @param[in] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005796 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005797 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005798 * @param[in] options XPath options.
5799 * @return LY_ERR
5800 */
5801static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005802moveto_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 +02005803{
Radek Krejci1deb5be2020-08-26 16:43:36 +02005804 uint32_t i, orig_used;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005805 const struct lysc_node *next, *elem, *start;
Michal Vasko6346ece2019-09-24 13:12:53 +02005806 LY_ERR rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005807
Michal Vaskod3678892020-05-21 10:06:58 +02005808 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005809 return LY_SUCCESS;
5810 }
5811
5812 if (set->type != LYXP_SET_SCNODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005813 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 +02005814 return LY_EVALID;
5815 }
5816
Michal Vasko03ff5a72019-09-11 13:49:33 +02005817 orig_used = set->used;
5818 for (i = 0; i < orig_used; ++i) {
5819 if (set->val.scnodes[i].in_ctx != 1) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01005820 if (set->val.scnodes[i].in_ctx != -2) {
5821 continue;
5822 }
5823
5824 /* remember context node */
5825 set->val.scnodes[i].in_ctx = -1;
Michal Vaskoec4df482019-12-16 10:02:18 +01005826 } else {
5827 set->val.scnodes[i].in_ctx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005828 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005829
5830 /* TREE DFS */
5831 start = set->val.scnodes[i].scnode;
5832 for (elem = next = start; elem; elem = next) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005833 if ((elem == start) || (elem->nodetype & (LYS_CHOICE | LYS_CASE))) {
5834 /* schema-only nodes, skip root */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005835 goto next_iter;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005836 }
5837
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005838 rc = moveto_scnode_check(elem, start, set, ncname, moveto_mod);
Michal Vasko6346ece2019-09-24 13:12:53 +02005839 if (!rc) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005840 uint32_t idx;
5841
5842 if (lyxp_set_scnode_contains(set, elem, LYXP_NODE_ELEM, i, &idx)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005843 set->val.scnodes[idx].in_ctx = 1;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005844 if ((uint32_t)idx > i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005845 /* we will process it later in the set */
5846 goto skip_children;
5847 }
5848 } else {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005849 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, elem, LYXP_NODE_ELEM, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005850 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005851 } else if (rc == LY_EINVAL) {
5852 goto skip_children;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005853 }
5854
5855next_iter:
5856 /* TREE DFS NEXT ELEM */
5857 /* select element for the next run - children first */
5858 next = lysc_node_children(elem, options & LYXP_SCNODE_OUTPUT ? LYS_CONFIG_R : LYS_CONFIG_W);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005859 if (!next) {
5860skip_children:
5861 /* no children, so try siblings, but only if it's not the start,
5862 * that is considered to be the root and it's siblings are not traversed */
5863 if (elem != start) {
5864 next = elem->next;
5865 } else {
5866 break;
5867 }
5868 }
5869 while (!next) {
5870 /* no siblings, go back through the parents */
5871 if (elem->parent == start) {
5872 /* we are done, no next element to process */
5873 break;
5874 }
5875 /* parent is already processed, go to its sibling */
5876 elem = elem->parent;
5877 next = elem->next;
5878 }
5879 }
5880 }
5881
5882 return LY_SUCCESS;
5883}
5884
5885/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005886 * @brief Move context @p set to an attribute. Result is LYXP_SET_NODE_SET.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005887 * Indirectly context position aware.
5888 *
5889 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005890 * @param[in] mod Matching metadata module, NULL for any.
5891 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005892 * @return LY_ERR
5893 */
5894static LY_ERR
Michal Vaskod3678892020-05-21 10:06:58 +02005895moveto_attr(struct lyxp_set *set, const struct lys_module *mod, const char *ncname)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005896{
Michal Vasko9f96a052020-03-10 09:41:45 +01005897 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005898
Michal Vaskod3678892020-05-21 10:06:58 +02005899 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005900 return LY_SUCCESS;
5901 }
5902
5903 if (set->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005904 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 +02005905 return LY_EVALID;
5906 }
5907
Radek Krejci1deb5be2020-08-26 16:43:36 +02005908 for (uint32_t i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02005909 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005910
5911 /* only attributes of an elem (not dummy) can be in the result, skip all the rest;
5912 * our attributes are always qualified */
Michal Vasko5c4e5892019-11-14 12:31:38 +01005913 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01005914 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005915
5916 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02005917 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005918 continue;
5919 }
5920
Michal Vaskod3678892020-05-21 10:06:58 +02005921 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005922 /* match */
5923 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01005924 set->val.meta[i].meta = sub;
5925 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005926 /* pos does not change */
5927 replaced = 1;
5928 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01005929 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 +02005930 }
5931 ++i;
5932 }
5933 }
5934 }
5935
5936 if (!replaced) {
5937 /* no match */
5938 set_remove_node(set, i);
5939 }
5940 }
5941
5942 return LY_SUCCESS;
5943}
5944
5945/**
5946 * @brief Move context @p set1 to union with @p set2. @p set2 is emptied afterwards.
Michal Vasko61ac2f62020-05-25 12:39:51 +02005947 * Result is LYXP_SET_NODE_SET. Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005948 *
5949 * @param[in,out] set1 Set to use for the result.
5950 * @param[in] set2 Set that is copied to @p set1.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005951 * @return LY_ERR
5952 */
5953static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005954moveto_union(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005955{
5956 LY_ERR rc;
5957
Michal Vaskod3678892020-05-21 10:06:58 +02005958 if ((set1->type != LYXP_SET_NODE_SET) || (set2->type != LYXP_SET_NODE_SET)) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005959 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 +02005960 return LY_EVALID;
5961 }
5962
5963 /* set2 is empty or both set1 and set2 */
Michal Vaskod3678892020-05-21 10:06:58 +02005964 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005965 return LY_SUCCESS;
5966 }
5967
Michal Vaskod3678892020-05-21 10:06:58 +02005968 if (!set1->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005969 memcpy(set1, set2, sizeof *set1);
5970 /* dynamic memory belongs to set1 now, do not free */
Michal Vaskod3678892020-05-21 10:06:58 +02005971 memset(set2, 0, sizeof *set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005972 return LY_SUCCESS;
5973 }
5974
5975 /* we assume sets are sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005976 assert(!set_sort(set1) && !set_sort(set2));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005977
5978 /* sort, remove duplicates */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005979 rc = set_sorted_merge(set1, set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005980 LY_CHECK_RET(rc);
5981
5982 /* final set must be sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005983 assert(!set_sort(set1));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005984
5985 return LY_SUCCESS;
5986}
5987
5988/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005989 * @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 +02005990 * Context position aware.
5991 *
5992 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005993 * @param[in] mod Matching metadata module, NULL for any.
5994 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01005995 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005996 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5997 */
5998static int
Michal Vaskocdad7122020-11-09 21:04:44 +01005999moveto_attr_alldesc(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006000{
Michal Vasko9f96a052020-03-10 09:41:45 +01006001 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006002 struct lyxp_set *set_all_desc = NULL;
6003 LY_ERR rc;
6004
Michal Vaskod3678892020-05-21 10:06:58 +02006005 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006006 return LY_SUCCESS;
6007 }
6008
6009 if (set->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006010 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 +02006011 return LY_EVALID;
6012 }
6013
Michal Vasko03ff5a72019-09-11 13:49:33 +02006014 /* can be optimized similarly to moveto_node_alldesc() and save considerable amount of memory,
6015 * but it likely won't be used much, so it's a waste of time */
6016 /* copy the context */
6017 set_all_desc = set_copy(set);
6018 /* get all descendant nodes (the original context nodes are removed) */
Michal Vaskocdad7122020-11-09 21:04:44 +01006019 rc = moveto_node_alldesc(set_all_desc, NULL, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006020 if (rc != LY_SUCCESS) {
6021 lyxp_set_free(set_all_desc);
6022 return rc;
6023 }
6024 /* prepend the original context nodes */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006025 rc = moveto_union(set, set_all_desc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006026 if (rc != LY_SUCCESS) {
6027 lyxp_set_free(set_all_desc);
6028 return rc;
6029 }
6030 lyxp_set_free(set_all_desc);
6031
Radek Krejci1deb5be2020-08-26 16:43:36 +02006032 for (uint32_t i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02006033 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006034
6035 /* only attributes of an elem can be in the result, skip all the rest,
6036 * we have all attributes qualified in lyd tree */
6037 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01006038 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006039 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02006040 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006041 continue;
6042 }
6043
Michal Vaskod3678892020-05-21 10:06:58 +02006044 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006045 /* match */
6046 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01006047 set->val.meta[i].meta = sub;
6048 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006049 /* pos does not change */
6050 replaced = 1;
6051 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01006052 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 +02006053 }
6054 ++i;
6055 }
6056 }
6057 }
6058
6059 if (!replaced) {
6060 /* no match */
6061 set_remove_node(set, i);
6062 }
6063 }
6064
6065 return LY_SUCCESS;
6066}
6067
6068/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02006069 * @brief Move context @p set to self and al chilren, recursively. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET.
6070 * Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006071 *
6072 * @param[in] parent Current parent.
6073 * @param[in] parent_pos Position of @p parent.
6074 * @param[in] parent_type Node type of @p parent.
6075 * @param[in,out] to_set Set to use.
6076 * @param[in] dup_check_set Set for checking duplicities.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006077 * @param[in] options XPath options.
6078 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6079 */
6080static LY_ERR
6081moveto_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 +02006082 struct lyxp_set *to_set, const struct lyxp_set *dup_check_set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006083{
Michal Vasko61ac2f62020-05-25 12:39:51 +02006084 const struct lyd_node *iter, *first;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006085 LY_ERR rc;
6086
6087 switch (parent_type) {
6088 case LYXP_NODE_ROOT:
6089 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko61ac2f62020-05-25 12:39:51 +02006090 assert(!parent);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006091
Michal Vasko61ac2f62020-05-25 12:39:51 +02006092 /* add all top-level nodes as elements */
6093 first = to_set->tree;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006094 break;
6095 case LYXP_NODE_ELEM:
Michal Vasko61ac2f62020-05-25 12:39:51 +02006096 /* add just the text node of this term element node */
6097 if (parent->schema->nodetype & (LYD_NODE_TERM | LYD_NODE_ANY)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006098 if (!set_dup_node_check(dup_check_set, parent, LYXP_NODE_TEXT, -1)) {
6099 set_insert_node(to_set, parent, parent_pos, LYXP_NODE_TEXT, to_set->used);
6100 }
Michal Vasko61ac2f62020-05-25 12:39:51 +02006101 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006102 }
Michal Vasko61ac2f62020-05-25 12:39:51 +02006103
6104 /* add all the children of this node */
Radek Krejcia1c1e542020-09-29 16:06:52 +02006105 first = lyd_child(parent);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006106 break;
6107 default:
6108 LOGINT_RET(parent->schema->module->ctx);
6109 }
6110
Michal Vasko61ac2f62020-05-25 12:39:51 +02006111 /* add all top-level nodes as elements */
6112 LY_LIST_FOR(first, iter) {
6113 /* context check */
6114 if ((parent_type == LYXP_NODE_ROOT_CONFIG) && (iter->schema->flags & LYS_CONFIG_R)) {
6115 continue;
6116 }
6117
6118 /* when check */
Michal Vaskocdad7122020-11-09 21:04:44 +01006119 if (!(options & LYXP_IGNORE_WHEN) && lyd_has_when(iter) && !(iter->flags & LYD_WHEN_TRUE)) {
Michal Vasko61ac2f62020-05-25 12:39:51 +02006120 return LY_EINCOMPLETE;
6121 }
6122
6123 if (!set_dup_node_check(dup_check_set, iter, LYXP_NODE_ELEM, -1)) {
6124 set_insert_node(to_set, iter, 0, LYXP_NODE_ELEM, to_set->used);
6125
6126 /* also add all the children of this node, recursively */
6127 rc = moveto_self_add_children_r(iter, 0, LYXP_NODE_ELEM, to_set, dup_check_set, options);
6128 LY_CHECK_RET(rc);
6129 }
6130 }
6131
Michal Vasko03ff5a72019-09-11 13:49:33 +02006132 return LY_SUCCESS;
6133}
6134
6135/**
6136 * @brief Move context @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET
6137 * (or LYXP_SET_EMPTY). Context position aware.
6138 *
6139 * @param[in,out] set Set to use.
6140 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6141 * @param[in] options XPath options.
6142 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6143 */
6144static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006145moveto_self(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006146{
Michal Vasko03ff5a72019-09-11 13:49:33 +02006147 struct lyxp_set ret_set;
6148 LY_ERR rc;
6149
Michal Vaskod3678892020-05-21 10:06:58 +02006150 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006151 return LY_SUCCESS;
6152 }
6153
6154 if (set->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006155 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 +02006156 return LY_EVALID;
6157 }
6158
6159 /* nothing to do */
6160 if (!all_desc) {
6161 return LY_SUCCESS;
6162 }
6163
Michal Vasko03ff5a72019-09-11 13:49:33 +02006164 /* add all the children, they get added recursively */
6165 set_init(&ret_set, set);
Radek Krejci1deb5be2020-08-26 16:43:36 +02006166 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006167 /* copy the current node to tmp */
6168 set_insert_node(&ret_set, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, ret_set.used);
6169
6170 /* do not touch attributes and text nodes */
Michal Vasko9f96a052020-03-10 09:41:45 +01006171 if ((set->val.nodes[i].type == LYXP_NODE_TEXT) || (set->val.nodes[i].type == LYXP_NODE_META)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006172 continue;
6173 }
6174
Michal Vasko03ff5a72019-09-11 13:49:33 +02006175 /* add all the children */
6176 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 +02006177 set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006178 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006179 lyxp_set_free_content(&ret_set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006180 return rc;
6181 }
6182 }
6183
6184 /* use the temporary set as the current one */
6185 ret_set.ctx_pos = set->ctx_pos;
6186 ret_set.ctx_size = set->ctx_size;
Michal Vaskod3678892020-05-21 10:06:58 +02006187 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006188 memcpy(set, &ret_set, sizeof *set);
6189
6190 return LY_SUCCESS;
6191}
6192
6193/**
6194 * @brief Move context schema @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_SCNODE_SET
6195 * (or LYXP_SET_EMPTY).
6196 *
6197 * @param[in,out] set Set to use.
6198 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6199 * @param[in] options XPath options.
6200 * @return LY_ERR
6201 */
6202static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006203moveto_scnode_self(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006204{
Radek Krejci1deb5be2020-08-26 16:43:36 +02006205 uint32_t getnext_opts;
6206 uint32_t mod_idx;
Michal Vasko519fd602020-05-26 12:17:39 +02006207 const struct lysc_node *iter, *start_parent;
6208 const struct lys_module *mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006209
Michal Vaskod3678892020-05-21 10:06:58 +02006210 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006211 return LY_SUCCESS;
6212 }
6213
6214 if (set->type != LYXP_SET_SCNODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006215 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 +02006216 return LY_EVALID;
6217 }
6218
6219 /* nothing to do */
6220 if (!all_desc) {
6221 return LY_SUCCESS;
6222 }
6223
Michal Vasko519fd602020-05-26 12:17:39 +02006224 /* getnext opts */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01006225 getnext_opts = 0;
Michal Vasko519fd602020-05-26 12:17:39 +02006226 if (options & LYXP_SCNODE_OUTPUT) {
6227 getnext_opts |= LYS_GETNEXT_OUTPUT;
6228 }
6229
6230 /* add all the children, recursively as they are being added into the same set */
Radek Krejci1deb5be2020-08-26 16:43:36 +02006231 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006232 if (set->val.scnodes[i].in_ctx != 1) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006233 if (set->val.scnodes[i].in_ctx != -2) {
6234 continue;
6235 }
6236
Michal Vasko519fd602020-05-26 12:17:39 +02006237 /* remember context node */
6238 set->val.scnodes[i].in_ctx = -1;
6239 } else {
6240 set->val.scnodes[i].in_ctx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006241 }
6242
Michal Vasko519fd602020-05-26 12:17:39 +02006243 start_parent = set->val.scnodes[i].scnode;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006244
Michal Vasko519fd602020-05-26 12:17:39 +02006245 if ((set->val.scnodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.scnodes[i].type == LYXP_NODE_ROOT)) {
6246 /* it can actually be in any module, it's all <running> */
6247 mod_idx = 0;
6248 while ((mod = (struct lys_module *)ly_ctx_get_module_iter(set->ctx, &mod_idx))) {
6249 iter = NULL;
6250 /* module may not be implemented */
6251 while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) {
6252 /* context check */
6253 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) {
6254 continue;
6255 }
6256
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006257 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, NULL));
Michal Vasko519fd602020-05-26 12:17:39 +02006258 /* throw away the insert index, we want to consider that node again, recursively */
6259 }
6260 }
6261
6262 } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
6263 iter = NULL;
6264 while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006265 /* context check */
Michal Vasko519fd602020-05-26 12:17:39 +02006266 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006267 continue;
6268 }
6269
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006270 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006271 }
6272 }
6273 }
6274
6275 return LY_SUCCESS;
6276}
6277
6278/**
6279 * @brief Move context @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_NODE_SET
6280 * (or LYXP_SET_EMPTY). Context position aware.
6281 *
6282 * @param[in] set Set to use.
6283 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6284 * @param[in] options XPath options.
6285 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6286 */
6287static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006288moveto_parent(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006289{
6290 LY_ERR rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006291 struct lyd_node *node, *new_node;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006292 enum lyxp_node_type new_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006293
Michal Vaskod3678892020-05-21 10:06:58 +02006294 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006295 return LY_SUCCESS;
6296 }
6297
6298 if (set->type != LYXP_SET_NODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006299 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 +02006300 return LY_EVALID;
6301 }
6302
6303 if (all_desc) {
6304 /* <path>//.. == <path>//./.. */
6305 rc = moveto_self(set, 1, options);
6306 LY_CHECK_RET(rc);
6307 }
6308
Radek Krejci1deb5be2020-08-26 16:43:36 +02006309 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006310 node = set->val.nodes[i].node;
6311
6312 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
6313 new_node = (struct lyd_node *)node->parent;
6314 } else if (set->val.nodes[i].type == LYXP_NODE_TEXT) {
6315 new_node = node;
Michal Vasko9f96a052020-03-10 09:41:45 +01006316 } else if (set->val.nodes[i].type == LYXP_NODE_META) {
6317 new_node = set->val.meta[i].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006318 if (!new_node) {
6319 LOGINT_RET(set->ctx);
6320 }
6321 } else {
6322 /* root does not have a parent */
Michal Vasko2caefc12019-11-14 16:07:56 +01006323 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006324 continue;
6325 }
6326
Michal Vaskoa1424542019-11-14 16:08:52 +01006327 /* when check */
Michal Vaskocdad7122020-11-09 21:04:44 +01006328 if (!(options & LYXP_IGNORE_WHEN) && lyd_has_when(new_node) && !(new_node->flags & LYD_WHEN_TRUE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006329 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01006330 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006331
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006332 if (!new_node) {
Radek Krejcif6a11002020-08-21 13:29:07 +02006333 /* node already there can also be the root */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006334 new_type = set->root_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006335
Michal Vasko03ff5a72019-09-11 13:49:33 +02006336 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02006337 /* 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 +02006338 new_type = LYXP_NODE_ELEM;
6339 }
6340
Michal Vasko03ff5a72019-09-11 13:49:33 +02006341 if (set_dup_node_check(set, new_node, new_type, -1)) {
Michal Vasko2caefc12019-11-14 16:07:56 +01006342 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006343 } else {
6344 set_replace_node(set, new_node, 0, new_type, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006345 }
6346 }
6347
Michal Vasko2caefc12019-11-14 16:07:56 +01006348 set_remove_nodes_none(set);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006349 assert(!set_sort(set) && !set_sorted_dup_node_clean(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006350
6351 return LY_SUCCESS;
6352}
6353
6354/**
6355 * @brief Move context schema @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_SCNODE_SET
6356 * (or LYXP_SET_EMPTY).
6357 *
6358 * @param[in] set Set to use.
6359 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6360 * @param[in] options XPath options.
6361 * @return LY_ERR
6362 */
6363static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006364moveto_scnode_parent(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006365{
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006366 uint32_t i, orig_used, idx;
Radek Krejci857189e2020-09-01 13:26:36 +02006367 ly_bool temp_ctx = 0;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006368 const struct lysc_node *node, *new_node;
6369 enum lyxp_node_type new_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006370
Michal Vaskod3678892020-05-21 10:06:58 +02006371 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006372 return LY_SUCCESS;
6373 }
6374
6375 if (set->type != LYXP_SET_SCNODE_SET) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006376 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 +02006377 return LY_EVALID;
6378 }
6379
6380 if (all_desc) {
6381 /* <path>//.. == <path>//./.. */
Radek Krejci1deb5be2020-08-26 16:43:36 +02006382 LY_CHECK_RET(moveto_scnode_self(set, 1, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006383 }
6384
Michal Vasko03ff5a72019-09-11 13:49:33 +02006385 orig_used = set->used;
6386 for (i = 0; i < orig_used; ++i) {
6387 if (set->val.scnodes[i].in_ctx != 1) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006388 if (set->val.scnodes[i].in_ctx != -2) {
6389 continue;
6390 }
6391
6392 /* remember context node */
6393 set->val.scnodes[i].in_ctx = -1;
Michal Vaskoec4df482019-12-16 10:02:18 +01006394 } else {
6395 set->val.scnodes[i].in_ctx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006396 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006397
6398 node = set->val.scnodes[i].scnode;
6399
6400 if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
Michal Vaskod3678892020-05-21 10:06:58 +02006401 new_node = lysc_data_parent(node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006402 } else {
6403 /* root does not have a parent */
6404 continue;
6405 }
6406
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006407 if (!new_node) {
Radek Krejcif6a11002020-08-21 13:29:07 +02006408 /* node has no parent */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006409 new_type = set->root_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006410
Michal Vasko03ff5a72019-09-11 13:49:33 +02006411 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02006412 /* 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 +02006413 new_type = LYXP_NODE_ELEM;
6414 }
6415
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006416 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, new_node, new_type, &idx));
6417 if ((idx < orig_used) && (idx > i)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006418 set->val.scnodes[idx].in_ctx = 2;
6419 temp_ctx = 1;
6420 }
6421 }
6422
6423 if (temp_ctx) {
6424 for (i = 0; i < orig_used; ++i) {
6425 if (set->val.scnodes[i].in_ctx == 2) {
6426 set->val.scnodes[i].in_ctx = 1;
6427 }
6428 }
6429 }
6430
6431 return LY_SUCCESS;
6432}
6433
6434/**
6435 * @brief Move context @p set to the result of a comparison. Handles '=', '!=', '<=', '<', '>=', or '>'.
6436 * Result is LYXP_SET_BOOLEAN. Indirectly context position aware.
6437 *
6438 * @param[in,out] set1 Set to use for the result.
6439 * @param[in] set2 Set acting as the second operand for @p op.
6440 * @param[in] op Comparison operator to process.
6441 * @param[in] options XPath options.
6442 * @return LY_ERR
6443 */
6444static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02006445moveto_op_comp(struct lyxp_set *set1, struct lyxp_set *set2, const char *op, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006446{
6447 /*
6448 * NODE SET + NODE SET = NODE SET + STRING /(1 NODE SET) 2 STRING
6449 * NODE SET + STRING = STRING + STRING /1 STRING (2 STRING)
6450 * NODE SET + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6451 * NODE SET + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6452 * STRING + NODE SET = STRING + STRING /(1 STRING) 2 STRING
6453 * NUMBER + NODE SET = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6454 * BOOLEAN + NODE SET = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6455 *
6456 * '=' or '!='
6457 * BOOLEAN + BOOLEAN
6458 * BOOLEAN + STRING = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6459 * BOOLEAN + NUMBER = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6460 * STRING + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6461 * NUMBER + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6462 * NUMBER + NUMBER
6463 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6464 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6465 * STRING + STRING
6466 *
6467 * '<=', '<', '>=', '>'
6468 * NUMBER + NUMBER
6469 * BOOLEAN + BOOLEAN = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6470 * BOOLEAN + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6471 * BOOLEAN + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6472 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6473 * STRING + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6474 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6475 * NUMBER + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6476 * STRING + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6477 */
6478 struct lyxp_set iter1, iter2;
6479 int result;
6480 int64_t i;
6481 LY_ERR rc;
6482
Michal Vasko004d3152020-06-11 19:59:22 +02006483 memset(&iter1, 0, sizeof iter1);
6484 memset(&iter2, 0, sizeof iter2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006485
6486 /* iterative evaluation with node-sets */
6487 if ((set1->type == LYXP_SET_NODE_SET) || (set2->type == LYXP_SET_NODE_SET)) {
6488 if (set1->type == LYXP_SET_NODE_SET) {
6489 for (i = 0; i < set1->used; ++i) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006490 /* cast set1 */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006491 switch (set2->type) {
6492 case LYXP_SET_NUMBER:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006493 rc = set_comp_cast(&iter1, set1, LYXP_SET_NUMBER, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006494 break;
6495 case LYXP_SET_BOOLEAN:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006496 rc = set_comp_cast(&iter1, set1, LYXP_SET_BOOLEAN, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006497 break;
6498 default:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006499 rc = set_comp_cast(&iter1, set1, LYXP_SET_STRING, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006500 break;
6501 }
6502 LY_CHECK_RET(rc);
6503
Michal Vasko4c7763f2020-07-27 17:40:37 +02006504 /* canonize set2 */
6505 LY_CHECK_ERR_RET(rc = set_comp_canonize(&iter2, set2, &set1->val.nodes[i]), lyxp_set_free_content(&iter1), rc);
6506
6507 /* compare recursively */
6508 rc = moveto_op_comp(&iter1, &iter2, op, options);
6509 lyxp_set_free_content(&iter2);
6510 LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006511
6512 /* lazy evaluation until true */
Michal Vasko004d3152020-06-11 19:59:22 +02006513 if (iter1.val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006514 set_fill_boolean(set1, 1);
6515 return LY_SUCCESS;
6516 }
6517 }
6518 } else {
6519 for (i = 0; i < set2->used; ++i) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006520 /* set set2 */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006521 switch (set1->type) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006522 case LYXP_SET_NUMBER:
6523 rc = set_comp_cast(&iter2, set2, LYXP_SET_NUMBER, i);
6524 break;
6525 case LYXP_SET_BOOLEAN:
6526 rc = set_comp_cast(&iter2, set2, LYXP_SET_BOOLEAN, i);
6527 break;
6528 default:
6529 rc = set_comp_cast(&iter2, set2, LYXP_SET_STRING, i);
6530 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006531 }
6532 LY_CHECK_RET(rc);
6533
Michal Vasko4c7763f2020-07-27 17:40:37 +02006534 /* canonize set1 */
6535 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 +02006536
Michal Vasko4c7763f2020-07-27 17:40:37 +02006537 /* compare recursively */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006538 rc = moveto_op_comp(&iter1, &iter2, op, options);
Michal Vaskod3678892020-05-21 10:06:58 +02006539 lyxp_set_free_content(&iter2);
Michal Vasko4c7763f2020-07-27 17:40:37 +02006540 LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006541
6542 /* lazy evaluation until true */
Michal Vasko004d3152020-06-11 19:59:22 +02006543 if (iter1.val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006544 set_fill_boolean(set1, 1);
6545 return LY_SUCCESS;
6546 }
6547 }
6548 }
6549
6550 /* false for all nodes */
6551 set_fill_boolean(set1, 0);
6552 return LY_SUCCESS;
6553 }
6554
6555 /* first convert properly */
6556 if ((op[0] == '=') || (op[0] == '!')) {
6557 if ((set1->type == LYXP_SET_BOOLEAN) || (set2->type == LYXP_SET_BOOLEAN)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006558 lyxp_set_cast(set1, LYXP_SET_BOOLEAN);
6559 lyxp_set_cast(set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006560 } else if ((set1->type == LYXP_SET_NUMBER) || (set2->type == LYXP_SET_NUMBER)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006561 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006562 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006563 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006564 LY_CHECK_RET(rc);
6565 } /* else we have 2 strings */
6566 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006567 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006568 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006569 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006570 LY_CHECK_RET(rc);
6571 }
6572
6573 assert(set1->type == set2->type);
6574
6575 /* compute result */
6576 if (op[0] == '=') {
6577 if (set1->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02006578 result = (set1->val.bln == set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006579 } else if (set1->type == LYXP_SET_NUMBER) {
6580 result = (set1->val.num == set2->val.num);
6581 } else {
6582 assert(set1->type == LYXP_SET_STRING);
Michal Vaskoac6c72f2019-11-14 16:09:34 +01006583 result = !strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006584 }
6585 } else if (op[0] == '!') {
6586 if (set1->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02006587 result = (set1->val.bln != set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006588 } else if (set1->type == LYXP_SET_NUMBER) {
6589 result = (set1->val.num != set2->val.num);
6590 } else {
6591 assert(set1->type == LYXP_SET_STRING);
Michal Vaskoc2058432020-11-06 17:26:21 +01006592 result = strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006593 }
6594 } else {
6595 assert(set1->type == LYXP_SET_NUMBER);
6596 if (op[0] == '<') {
6597 if (op[1] == '=') {
6598 result = (set1->val.num <= set2->val.num);
6599 } else {
6600 result = (set1->val.num < set2->val.num);
6601 }
6602 } else {
6603 if (op[1] == '=') {
6604 result = (set1->val.num >= set2->val.num);
6605 } else {
6606 result = (set1->val.num > set2->val.num);
6607 }
6608 }
6609 }
6610
6611 /* assign result */
6612 if (result) {
6613 set_fill_boolean(set1, 1);
6614 } else {
6615 set_fill_boolean(set1, 0);
6616 }
6617
6618 return LY_SUCCESS;
6619}
6620
6621/**
6622 * @brief Move context @p set to the result of a basic operation. Handles '+', '-', unary '-', '*', 'div',
6623 * or 'mod'. Result is LYXP_SET_NUMBER. Indirectly context position aware.
6624 *
6625 * @param[in,out] set1 Set to use for the result.
6626 * @param[in] set2 Set acting as the second operand for @p op.
6627 * @param[in] op Operator to process.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006628 * @return LY_ERR
6629 */
6630static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006631moveto_op_math(struct lyxp_set *set1, struct lyxp_set *set2, const char *op)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006632{
6633 LY_ERR rc;
6634
6635 /* unary '-' */
6636 if (!set2 && (op[0] == '-')) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006637 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006638 LY_CHECK_RET(rc);
6639 set1->val.num *= -1;
6640 lyxp_set_free(set2);
6641 return LY_SUCCESS;
6642 }
6643
6644 assert(set1 && set2);
6645
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006646 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006647 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006648 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006649 LY_CHECK_RET(rc);
6650
6651 switch (op[0]) {
6652 /* '+' */
6653 case '+':
6654 set1->val.num += set2->val.num;
6655 break;
6656
6657 /* '-' */
6658 case '-':
6659 set1->val.num -= set2->val.num;
6660 break;
6661
6662 /* '*' */
6663 case '*':
6664 set1->val.num *= set2->val.num;
6665 break;
6666
6667 /* 'div' */
6668 case 'd':
6669 set1->val.num /= set2->val.num;
6670 break;
6671
6672 /* 'mod' */
6673 case 'm':
6674 set1->val.num = ((long long)set1->val.num) % ((long long)set2->val.num);
6675 break;
6676
6677 default:
6678 LOGINT_RET(set1->ctx);
6679 }
6680
6681 return LY_SUCCESS;
6682}
6683
6684/*
6685 * eval functions
6686 *
6687 * They execute a parsed XPath expression on some data subtree.
6688 */
6689
6690/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02006691 * @brief Evaluate Predicate. Logs directly on error.
6692 *
Michal Vaskod3678892020-05-21 10:06:58 +02006693 * [9] Predicate ::= '[' Expr ']'
Michal Vasko03ff5a72019-09-11 13:49:33 +02006694 *
6695 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02006696 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006697 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
6698 * @param[in] options XPath options.
6699 * @param[in] parent_pos_pred Whether parent predicate was a positional one.
6700 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6701 */
6702static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02006703eval_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 +02006704{
6705 LY_ERR rc;
Michal Vasko57eab132019-09-24 11:46:26 +02006706 uint16_t i, orig_exp;
Michal Vasko5c4e5892019-11-14 12:31:38 +01006707 uint32_t orig_pos, orig_size;
6708 int32_t pred_in_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006709 struct lyxp_set set2;
6710 struct lyd_node *orig_parent;
6711
6712 /* '[' */
6713 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02006714 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02006715 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006716
6717 if (!set) {
6718only_parse:
Michal Vasko004d3152020-06-11 19:59:22 +02006719 rc = eval_expr_select(exp, tok_idx, 0, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006720 LY_CHECK_RET(rc);
6721 } else if (set->type == LYXP_SET_NODE_SET) {
6722 /* 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 +01006723 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006724
6725 /* empty set, nothing to evaluate */
6726 if (!set->used) {
6727 goto only_parse;
6728 }
6729
Michal Vasko004d3152020-06-11 19:59:22 +02006730 orig_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006731 orig_pos = 0;
6732 orig_size = set->used;
6733 orig_parent = NULL;
Michal Vasko39dbf352020-05-21 10:08:59 +02006734 for (i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006735 set_init(&set2, set);
6736 set_insert_node(&set2, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, 0);
6737 /* remember the node context position for position() and context size for last(),
6738 * predicates should always be evaluated with respect to the child axis (since we do
6739 * not support explicit axes) so we assign positions based on their parents */
6740 if (parent_pos_pred && ((struct lyd_node *)set->val.nodes[i].node->parent != orig_parent)) {
6741 orig_parent = (struct lyd_node *)set->val.nodes[i].node->parent;
6742 orig_pos = 1;
6743 } else {
6744 ++orig_pos;
6745 }
6746
6747 set2.ctx_pos = orig_pos;
6748 set2.ctx_size = orig_size;
Michal Vasko004d3152020-06-11 19:59:22 +02006749 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006750
Michal Vasko004d3152020-06-11 19:59:22 +02006751 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006752 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006753 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006754 return rc;
6755 }
6756
6757 /* number is a position */
6758 if (set2.type == LYXP_SET_NUMBER) {
6759 if ((long long)set2.val.num == orig_pos) {
6760 set2.val.num = 1;
6761 } else {
6762 set2.val.num = 0;
6763 }
6764 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006765 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006766
6767 /* predicate satisfied or not? */
Michal Vasko004d3152020-06-11 19:59:22 +02006768 if (!set2.val.bln) {
Michal Vasko2caefc12019-11-14 16:07:56 +01006769 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006770 }
6771 }
Michal Vasko2caefc12019-11-14 16:07:56 +01006772 set_remove_nodes_none(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006773
6774 } else if (set->type == LYXP_SET_SCNODE_SET) {
6775 for (i = 0; i < set->used; ++i) {
6776 if (set->val.scnodes[i].in_ctx == 1) {
6777 /* there is a currently-valid node */
6778 break;
6779 }
6780 }
6781 /* empty set, nothing to evaluate */
6782 if (i == set->used) {
6783 goto only_parse;
6784 }
6785
Michal Vasko004d3152020-06-11 19:59:22 +02006786 orig_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006787
Michal Vasko03ff5a72019-09-11 13:49:33 +02006788 /* set special in_ctx to all the valid snodes */
6789 pred_in_ctx = set_scnode_new_in_ctx(set);
6790
6791 /* use the valid snodes one-by-one */
6792 for (i = 0; i < set->used; ++i) {
6793 if (set->val.scnodes[i].in_ctx != pred_in_ctx) {
6794 continue;
6795 }
6796 set->val.scnodes[i].in_ctx = 1;
6797
Michal Vasko004d3152020-06-11 19:59:22 +02006798 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006799
Michal Vasko004d3152020-06-11 19:59:22 +02006800 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006801 LY_CHECK_RET(rc);
6802
6803 set->val.scnodes[i].in_ctx = pred_in_ctx;
6804 }
6805
6806 /* restore the state as it was before the predicate */
6807 for (i = 0; i < set->used; ++i) {
6808 if (set->val.scnodes[i].in_ctx == 1) {
6809 set->val.scnodes[i].in_ctx = 0;
6810 } else if (set->val.scnodes[i].in_ctx == pred_in_ctx) {
6811 set->val.scnodes[i].in_ctx = 1;
6812 }
6813 }
6814
6815 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02006816 set2.type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006817 set_fill_set(&set2, set);
6818
Michal Vasko004d3152020-06-11 19:59:22 +02006819 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006820 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006821 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006822 return rc;
6823 }
6824
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006825 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02006826 if (!set2.val.bln) {
Michal Vaskod3678892020-05-21 10:06:58 +02006827 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006828 }
Michal Vaskod3678892020-05-21 10:06:58 +02006829 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006830 }
6831
6832 /* ']' */
Michal Vasko004d3152020-06-11 19:59:22 +02006833 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_BRACK2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006834 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02006835 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02006836 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006837
6838 return LY_SUCCESS;
6839}
6840
6841/**
Michal Vaskod3678892020-05-21 10:06:58 +02006842 * @brief Evaluate Literal. Logs directly on error.
6843 *
6844 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02006845 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02006846 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
6847 */
6848static void
Michal Vasko40308e72020-10-20 16:38:40 +02006849eval_literal(const struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set)
Michal Vaskod3678892020-05-21 10:06:58 +02006850{
6851 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02006852 if (exp->tok_len[*tok_idx] == 2) {
Michal Vaskod3678892020-05-21 10:06:58 +02006853 set_fill_string(set, "", 0);
6854 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02006855 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 +02006856 }
6857 }
6858 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02006859 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02006860 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02006861}
6862
6863/**
Michal Vasko004d3152020-06-11 19:59:22 +02006864 * @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 +02006865 *
Michal Vasko004d3152020-06-11 19:59:22 +02006866 * @param[in] exp Full parsed XPath expression.
6867 * @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 +02006868 * @param[in] ctx_node Found schema node as the context for the predicate.
6869 * @param[in] cur_mod Current module for the expression.
6870 * @param[in] cur_node Current (original context) node.
Michal Vasko004d3152020-06-11 19:59:22 +02006871 * @param[in] format Format of any prefixes in key names/values.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006872 * @param[in] prefix_data Format-specific prefix data (see ::ly_resolve_prefix).
Michal Vasko004d3152020-06-11 19:59:22 +02006873 * @param[out] predicates Parsed predicates.
6874 * @param[out] pred_type Type of @p predicates.
6875 * @return LY_SUCCESS on success,
6876 * @return LY_ERR on any error.
Michal Vaskod3678892020-05-21 10:06:58 +02006877 */
6878static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02006879eval_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 +02006880 const struct lys_module *cur_mod, const struct lysc_node *cur_node, LY_PREFIX_FORMAT format, void *prefix_data,
6881 struct ly_path_predicate **predicates, enum ly_path_pred_type *pred_type)
Michal Vaskod3678892020-05-21 10:06:58 +02006882{
Michal Vasko004d3152020-06-11 19:59:22 +02006883 LY_ERR ret = LY_SUCCESS;
6884 uint16_t key_count, e_idx, pred_idx = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02006885 const struct lysc_node *key;
Michal Vasko004d3152020-06-11 19:59:22 +02006886 size_t pred_len;
Radek Krejci1deb5be2020-08-26 16:43:36 +02006887 uint32_t prev_lo;
Michal Vasko004d3152020-06-11 19:59:22 +02006888 struct lyxp_expr *exp2 = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02006889
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006890 assert(ctx_node->nodetype & (LYS_LIST | LYS_LEAFLIST));
Michal Vaskod3678892020-05-21 10:06:58 +02006891
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006892 if (ctx_node->nodetype == LYS_LIST) {
Michal Vasko004d3152020-06-11 19:59:22 +02006893 /* get key count */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006894 if (ctx_node->flags & LYS_KEYLESS) {
Michal Vasko004d3152020-06-11 19:59:22 +02006895 return LY_EINVAL;
6896 }
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006897 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 +02006898 assert(key_count);
Michal Vaskod3678892020-05-21 10:06:58 +02006899
Michal Vasko004d3152020-06-11 19:59:22 +02006900 /* learn where the predicates end */
6901 e_idx = *tok_idx;
6902 while (key_count) {
6903 /* '[' */
6904 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
6905 return LY_EINVAL;
6906 }
6907 ++e_idx;
6908
6909 /* ']' */
6910 while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
6911 ++e_idx;
6912 }
6913 ++e_idx;
6914
6915 /* another presumably key predicate parsed */
6916 --key_count;
6917 }
Michal Vasko004d3152020-06-11 19:59:22 +02006918 } else {
6919 /* learn just where this single predicate ends */
6920 e_idx = *tok_idx;
6921
Michal Vaskod3678892020-05-21 10:06:58 +02006922 /* '[' */
Michal Vasko004d3152020-06-11 19:59:22 +02006923 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
6924 return LY_EINVAL;
6925 }
6926 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02006927
6928 /* ']' */
Michal Vasko004d3152020-06-11 19:59:22 +02006929 while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
6930 ++e_idx;
6931 }
6932 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02006933 }
6934
Michal Vasko004d3152020-06-11 19:59:22 +02006935 /* get the joined length of all the keys predicates/of the single leaf-list predicate */
6936 pred_len = (exp->tok_pos[e_idx - 1] + exp->tok_len[e_idx - 1]) - exp->tok_pos[*tok_idx];
6937
6938 /* turn logging off */
6939 prev_lo = ly_log_options(0);
6940
6941 /* parse the predicate(s) */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006942 LY_CHECK_GOTO(ret = ly_path_parse_predicate(ctx_node->module->ctx, ctx_node, exp->expr + exp->tok_pos[*tok_idx],
6943 pred_len, LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &exp2), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02006944
6945 /* compile */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006946 ret = ly_path_compile_predicate(ctx_node->module->ctx, cur_node, cur_mod, ctx_node, exp2, &pred_idx, format,
6947 prefix_data, predicates, pred_type);
Michal Vasko004d3152020-06-11 19:59:22 +02006948 LY_CHECK_GOTO(ret, cleanup);
6949
6950 /* success, the predicate must include all the needed information for hash-based search */
6951 *tok_idx = e_idx;
6952
6953cleanup:
6954 ly_log_options(prev_lo);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006955 lyxp_expr_free(ctx_node->module->ctx, exp2);
Michal Vasko004d3152020-06-11 19:59:22 +02006956 return ret;
Michal Vaskod3678892020-05-21 10:06:58 +02006957}
6958
6959/**
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006960 * @brief Search for/check the next schema node that could be the only matching schema node meaning the
6961 * data node(s) could be found using a single hash-based search.
6962 *
6963 * @param[in] node Next context node to check.
6964 * @param[in] name Expected node name.
6965 * @param[in] name_len Length of @p name.
6966 * @param[in] moveto_mod Expected node module.
6967 * @param[in] root_type XPath root type.
6968 * @param[in] no_prefix Whether the node name was unprefixed.
6969 * @param[in] format Prefix format.
6970 * @param[in,out] found Previously found node, is updated.
6971 * @return LY_SUCCESS on success,
6972 * @return LY_ENOT if the whole check failed and hashes cannot be used.
6973 */
6974static LY_ERR
6975eval_name_test_with_predicate_get_scnode(const struct lyd_node *node, const char *name, uint16_t name_len,
6976 const struct lys_module *moveto_mod, enum lyxp_node_type root_type, ly_bool no_prefix, LY_PREFIX_FORMAT format,
6977 const struct lysc_node **found)
6978{
6979 const struct lysc_node *scnode;
6980 const struct lys_module *mod;
6981 uint32_t idx = 0;
6982
6983continue_search:
Michal Vasko7d1d0912020-10-16 08:38:30 +02006984 scnode = NULL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006985 if (!node) {
6986 if (no_prefix && (format == LY_PREF_JSON)) {
6987 /* search all modules for a single match */
6988 while ((mod = ly_ctx_get_module_iter(moveto_mod->ctx, &idx))) {
6989 scnode = lys_find_child(NULL, mod, name, name_len, 0, 0);
6990 if (scnode) {
6991 /* we have found a match */
6992 break;
6993 }
6994 }
6995
Michal Vasko7d1d0912020-10-16 08:38:30 +02006996 if (!scnode) {
6997 /* all modules searched */
6998 idx = 0;
6999 }
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007000 } else {
7001 /* search in top-level */
7002 scnode = lys_find_child(NULL, moveto_mod, name, name_len, 0, 0);
7003 }
7004 } else if (!*found || (lysc_data_parent(*found) != node->schema)) {
7005 if (no_prefix && (format == LY_PREF_JSON)) {
7006 /* we must adjust the module to inherit the one from the context node */
7007 moveto_mod = node->schema->module;
7008 }
7009
7010 /* search in children, do not repeat the same search */
7011 scnode = lys_find_child(node->schema, moveto_mod, name, name_len, 0, 0);
Michal Vasko7d1d0912020-10-16 08:38:30 +02007012 } /* else skip redundant search */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007013
7014 /* additional context check */
7015 if (scnode && (root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) {
7016 scnode = NULL;
7017 }
7018
7019 if (scnode) {
7020 if (*found) {
7021 /* we found a schema node with the same name but at different level, give up, too complicated
7022 * (more hash-based searches would be required, not supported) */
7023 return LY_ENOT;
7024 } else {
7025 /* remember the found schema node and continue to make sure it can be used */
7026 *found = scnode;
7027 }
7028 }
7029
7030 if (idx) {
7031 /* continue searching all the following models */
7032 goto continue_search;
7033 }
7034
7035 return LY_SUCCESS;
7036}
7037
7038/**
Michal Vaskod3678892020-05-21 10:06:58 +02007039 * @brief Evaluate NameTest and any following Predicates. Logs directly on error.
7040 *
7041 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
7042 * [6] NodeTest ::= NameTest | NodeType '(' ')'
7043 * [7] NameTest ::= '*' | NCName ':' '*' | QName
7044 *
7045 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007046 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02007047 * @param[in] attr_axis Whether to search attributes or standard nodes.
7048 * @param[in] all_desc Whether to search all the descendants or children only.
7049 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7050 * @param[in] options XPath options.
7051 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7052 */
7053static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007054eval_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 +02007055 struct lyxp_set *set, uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02007056{
Michal Vaskod3678892020-05-21 10:06:58 +02007057 char *path;
Michal Vasko004d3152020-06-11 19:59:22 +02007058 const char *ncname, *ncname_dict = NULL;
7059 uint16_t ncname_len;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007060 const struct lys_module *moveto_mod = NULL;
7061 const struct lysc_node *scnode = NULL, *ctx_scnode;
Michal Vasko004d3152020-06-11 19:59:22 +02007062 struct ly_path_predicate *predicates = NULL;
7063 enum ly_path_pred_type pred_type = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02007064 LY_ERR rc = LY_SUCCESS;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007065 ly_bool no_prefix;
Michal Vaskod3678892020-05-21 10:06:58 +02007066
7067 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007068 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007069 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007070
7071 if (!set) {
7072 goto moveto;
7073 }
7074
Michal Vasko004d3152020-06-11 19:59:22 +02007075 ncname = &exp->expr[exp->tok_pos[*tok_idx - 1]];
7076 ncname_len = exp->tok_len[*tok_idx - 1];
Michal Vaskod3678892020-05-21 10:06:58 +02007077
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007078 /* get the first schema context node */
7079 if (set->used) {
7080 if (set->type == LYXP_SET_NODE_SET) {
7081 ctx_scnode = set->val.nodes[0].node ? set->val.nodes[0].node->schema : NULL;
7082 } else {
7083 ctx_scnode = set->val.scnodes[0].scnode;
7084 }
7085 } else {
7086 ctx_scnode = NULL;
7087 }
7088
7089 /* parse (and skip) module name, use any context node (if available) just so we get some moveto_mod if there
7090 * is no prefix for ::LY_PREF_JSON */
7091 rc = moveto_resolve_model(&ncname, &ncname_len, set, ctx_scnode, &moveto_mod);
Michal Vaskod3678892020-05-21 10:06:58 +02007092 LY_CHECK_GOTO(rc, cleanup);
7093
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007094 if (ncname_len == exp->tok_len[*tok_idx - 1]) {
7095 /* remember that there is no prefix */
7096 no_prefix = 1;
7097 } else {
7098 no_prefix = 0;
7099 }
7100
Michal Vaskod3678892020-05-21 10:06:58 +02007101 if (moveto_mod && !attr_axis && !all_desc && (set->type == LYXP_SET_NODE_SET)) {
7102 /* find the matching schema node in some parent in the context */
Radek Krejci1deb5be2020-08-26 16:43:36 +02007103 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007104 if (eval_name_test_with_predicate_get_scnode(set->val.nodes[i].node, ncname, ncname_len, moveto_mod,
7105 set->root_type, no_prefix, set->format, &scnode)) {
7106 /* check failed */
7107 scnode = NULL;
7108 break;
Michal Vaskod3678892020-05-21 10:06:58 +02007109 }
7110 }
7111
Michal Vasko004d3152020-06-11 19:59:22 +02007112 if (scnode && (scnode->nodetype & (LYS_LIST | LYS_LEAFLIST))) {
7113 /* try to create the predicates */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007114 if (eval_name_test_try_compile_predicates(exp, tok_idx, scnode, set->cur_mod, set->cur_node ?
7115 set->cur_node->schema : NULL, set->format, set->prefix_data, &predicates, &pred_type)) {
Michal Vasko004d3152020-06-11 19:59:22 +02007116 /* hashes cannot be used */
Michal Vaskod3678892020-05-21 10:06:58 +02007117 scnode = NULL;
7118 }
7119 }
7120 }
7121
Michal Vasko004d3152020-06-11 19:59:22 +02007122 if (!scnode && moveto_mod) {
7123 /* we are not able to match based on a schema node and not all the modules match,
7124 * use dictionary for efficient comparison */
Radek Krejci011e4aa2020-09-04 15:22:31 +02007125 LY_CHECK_GOTO(rc = lydict_insert(set->ctx, ncname, ncname_len, &ncname_dict), cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02007126 }
7127
7128moveto:
7129 /* move to the attribute(s), data node(s), or schema node(s) */
7130 if (attr_axis) {
7131 if (set && (options & LYXP_SCNODE_ALL)) {
7132 set_scnode_clear_ctx(set);
7133 } else {
7134 if (all_desc) {
Michal Vaskocdad7122020-11-09 21:04:44 +01007135 rc = moveto_attr_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007136 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007137 rc = moveto_attr(set, moveto_mod, ncname_dict);
Michal Vaskod3678892020-05-21 10:06:58 +02007138 }
7139 LY_CHECK_GOTO(rc, cleanup);
7140 }
7141 } else {
7142 if (set && (options & LYXP_SCNODE_ALL)) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02007143 int64_t i;
7144
Michal Vaskod3678892020-05-21 10:06:58 +02007145 if (all_desc) {
Michal Vasko004d3152020-06-11 19:59:22 +02007146 rc = moveto_scnode_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007147 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007148 rc = moveto_scnode(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007149 }
7150 LY_CHECK_GOTO(rc, cleanup);
7151
7152 for (i = set->used - 1; i > -1; --i) {
7153 if (set->val.scnodes[i].in_ctx > 0) {
7154 break;
7155 }
7156 }
7157 if (i == -1) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007158 path = lysc_path(set->cur_scnode, LYSC_PATH_LOG, NULL, 0);
Michal Vaskod3678892020-05-21 10:06:58 +02007159 LOGWRN(set->ctx, "Schema node \"%.*s\" not found (%.*s) with context node \"%s\".",
Michal Vasko69730152020-10-09 16:30:07 +02007160 ncname_len, ncname, ncname - exp->expr, exp->expr, path);
Michal Vaskod3678892020-05-21 10:06:58 +02007161 free(path);
7162 }
7163 } else {
7164 if (all_desc) {
Michal Vaskocdad7122020-11-09 21:04:44 +01007165 rc = moveto_node_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007166 } else {
7167 if (scnode) {
7168 /* we can find the nodes using hashes */
Michal Vaskocdad7122020-11-09 21:04:44 +01007169 rc = moveto_node_hash(set, scnode, predicates, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007170 } else {
Michal Vaskocdad7122020-11-09 21:04:44 +01007171 rc = moveto_node(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007172 }
7173 }
7174 LY_CHECK_GOTO(rc, cleanup);
7175 }
7176 }
7177
7178 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007179 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7180 rc = eval_predicate(exp, tok_idx, set, options, 1);
Michal Vaskod3678892020-05-21 10:06:58 +02007181 LY_CHECK_RET(rc);
7182 }
7183
7184cleanup:
Michal Vaskodb51a8d2020-05-27 15:22:29 +02007185 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007186 lydict_remove(set->ctx, ncname_dict);
Michal Vaskof7e16e22020-10-21 09:24:39 +02007187 ly_path_predicates_free(set->ctx, pred_type, predicates);
Michal Vaskodb51a8d2020-05-27 15:22:29 +02007188 }
Michal Vaskod3678892020-05-21 10:06:58 +02007189 return rc;
7190}
7191
7192/**
7193 * @brief Evaluate NodeType and any following Predicates. Logs directly on error.
7194 *
7195 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
7196 * [6] NodeTest ::= NameTest | NodeType '(' ')'
7197 * [8] NodeType ::= 'text' | 'node'
7198 *
7199 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007200 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02007201 * @param[in] attr_axis Whether to search attributes or standard nodes.
7202 * @param[in] all_desc Whether to search all the descendants or children only.
7203 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7204 * @param[in] options XPath options.
7205 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7206 */
7207static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007208eval_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 +02007209 struct lyxp_set *set, uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02007210{
7211 LY_ERR rc;
7212
7213 /* TODO */
7214 (void)attr_axis;
7215 (void)all_desc;
7216
7217 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007218 assert(exp->tok_len[*tok_idx] == 4);
Michal Vaskod3678892020-05-21 10:06:58 +02007219 if (set->type == LYXP_SET_SCNODE_SET) {
7220 set_scnode_clear_ctx(set);
7221 /* just for the debug message below */
7222 set = NULL;
7223 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007224 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "node", 4)) {
Michal Vaskod3678892020-05-21 10:06:58 +02007225 rc = xpath_node(NULL, 0, set, options);
7226 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007227 assert(!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "text", 4));
Michal Vaskod3678892020-05-21 10:06:58 +02007228 rc = xpath_text(NULL, 0, set, options);
7229 }
7230 LY_CHECK_RET(rc);
7231 }
7232 }
7233 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007234 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007235 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007236
7237 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02007238 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
Michal Vaskod3678892020-05-21 10:06:58 +02007239 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007240 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007241 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007242
7243 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007244 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
Michal Vaskod3678892020-05-21 10:06:58 +02007245 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007246 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007247 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007248
7249 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007250 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7251 rc = eval_predicate(exp, tok_idx, set, options, 1);
Michal Vaskod3678892020-05-21 10:06:58 +02007252 LY_CHECK_RET(rc);
7253 }
7254
7255 return LY_SUCCESS;
7256}
7257
7258/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02007259 * @brief Evaluate RelativeLocationPath. Logs directly on error.
7260 *
7261 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
7262 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
Michal Vaskod3678892020-05-21 10:06:58 +02007263 * [6] NodeTest ::= NameTest | NodeType '(' ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007264 *
7265 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007266 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007267 * @param[in] all_desc Whether to search all the descendants or children only.
7268 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7269 * @param[in] options XPath options.
7270 * @return LY_ERR (YL_EINCOMPLETE on unresolved when)
7271 */
7272static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007273eval_relative_location_path(const struct lyxp_expr *exp, uint16_t *tok_idx, ly_bool all_desc, struct lyxp_set *set,
7274 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007275{
Radek Krejci857189e2020-09-01 13:26:36 +02007276 ly_bool attr_axis;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007277 LY_ERR rc;
7278
7279 goto step;
7280 do {
7281 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02007282 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007283 all_desc = 0;
7284 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007285 assert(exp->tok_len[*tok_idx] == 2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007286 all_desc = 1;
7287 }
7288 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007289 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007290 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007291
7292step:
Michal Vaskod3678892020-05-21 10:06:58 +02007293 /* evaluate abbreviated axis '@'? if any */
Michal Vasko004d3152020-06-11 19:59:22 +02007294 if (exp->tokens[*tok_idx] == LYXP_TOKEN_AT) {
Michal Vaskod3678892020-05-21 10:06:58 +02007295 attr_axis = 1;
7296
7297 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007298 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007299 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007300 } else {
7301 attr_axis = 0;
7302 }
7303
Michal Vasko03ff5a72019-09-11 13:49:33 +02007304 /* Step */
Michal Vasko004d3152020-06-11 19:59:22 +02007305 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007306 case LYXP_TOKEN_DOT:
7307 /* evaluate '.' */
7308 if (set && (options & LYXP_SCNODE_ALL)) {
7309 rc = moveto_scnode_self(set, all_desc, options);
7310 } else {
7311 rc = moveto_self(set, all_desc, options);
7312 }
7313 LY_CHECK_RET(rc);
7314 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007315 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007316 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007317 break;
7318
7319 case LYXP_TOKEN_DDOT:
7320 /* evaluate '..' */
7321 if (set && (options & LYXP_SCNODE_ALL)) {
7322 rc = moveto_scnode_parent(set, all_desc, options);
7323 } else {
7324 rc = moveto_parent(set, all_desc, options);
7325 }
7326 LY_CHECK_RET(rc);
7327 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007328 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007329 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007330 break;
7331
Michal Vasko03ff5a72019-09-11 13:49:33 +02007332 case LYXP_TOKEN_NAMETEST:
Michal Vaskod3678892020-05-21 10:06:58 +02007333 /* evaluate NameTest Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007334 rc = eval_name_test_with_predicate(exp, tok_idx, attr_axis, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007335 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02007336 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007337
Michal Vaskod3678892020-05-21 10:06:58 +02007338 case LYXP_TOKEN_NODETYPE:
7339 /* evaluate NodeType Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007340 rc = eval_node_type_with_predicate(exp, tok_idx, attr_axis, all_desc, set, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007341 LY_CHECK_RET(rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007342 break;
7343
7344 default:
Michal Vasko02a77382019-09-12 11:47:35 +02007345 LOGINT_RET(set ? set->ctx : NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007346 }
Michal Vasko004d3152020-06-11 19:59:22 +02007347 } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007348
7349 return LY_SUCCESS;
7350}
7351
7352/**
7353 * @brief Evaluate AbsoluteLocationPath. Logs directly on error.
7354 *
7355 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
7356 *
7357 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007358 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007359 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7360 * @param[in] options XPath options.
7361 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7362 */
7363static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007364eval_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 +02007365{
Radek Krejci857189e2020-09-01 13:26:36 +02007366 ly_bool all_desc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007367
7368 if (set) {
7369 /* no matter what tokens follow, we need to be at the root */
Michal Vaskob0099a92020-08-31 14:55:23 +02007370 LY_CHECK_RET(moveto_root(set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007371 }
7372
7373 /* '/' RelativeLocationPath? */
Michal Vasko004d3152020-06-11 19:59:22 +02007374 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007375 /* evaluate '/' - deferred */
7376 all_desc = 0;
7377 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007378 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007379 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007380
Michal Vasko004d3152020-06-11 19:59:22 +02007381 if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007382 return LY_SUCCESS;
7383 }
Michal Vasko004d3152020-06-11 19:59:22 +02007384 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007385 case LYXP_TOKEN_DOT:
7386 case LYXP_TOKEN_DDOT:
7387 case LYXP_TOKEN_AT:
7388 case LYXP_TOKEN_NAMETEST:
7389 case LYXP_TOKEN_NODETYPE:
Michal Vaskob0099a92020-08-31 14:55:23 +02007390 LY_CHECK_RET(eval_relative_location_path(exp, tok_idx, all_desc, set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007391 break;
7392 default:
7393 break;
7394 }
7395
Michal Vasko03ff5a72019-09-11 13:49:33 +02007396 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02007397 /* '//' RelativeLocationPath */
Michal Vasko03ff5a72019-09-11 13:49:33 +02007398 /* evaluate '//' - deferred so as not to waste memory by remembering all the nodes */
7399 all_desc = 1;
7400 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007401 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007402 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007403
Michal Vaskob0099a92020-08-31 14:55:23 +02007404 LY_CHECK_RET(eval_relative_location_path(exp, tok_idx, all_desc, set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007405 }
7406
7407 return LY_SUCCESS;
7408}
7409
7410/**
7411 * @brief Evaluate FunctionCall. Logs directly on error.
7412 *
Michal Vaskod3678892020-05-21 10:06:58 +02007413 * [11] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007414 *
7415 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007416 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007417 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7418 * @param[in] options XPath options.
7419 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7420 */
7421static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007422eval_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 +02007423{
7424 LY_ERR rc;
Michal Vasko69730152020-10-09 16:30:07 +02007425
Radek Krejci1deb5be2020-08-26 16:43:36 +02007426 LY_ERR (*xpath_func)(struct lyxp_set **, uint16_t, struct lyxp_set *, uint32_t) = NULL;
Michal Vasko0cbf54f2019-12-16 10:01:06 +01007427 uint16_t arg_count = 0, i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007428 struct lyxp_set **args = NULL, **args_aux;
7429
7430 if (set) {
7431 /* FunctionName */
Michal Vasko004d3152020-06-11 19:59:22 +02007432 switch (exp->tok_len[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007433 case 3:
Michal Vasko004d3152020-06-11 19:59:22 +02007434 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007435 xpath_func = &xpath_not;
Michal Vasko004d3152020-06-11 19:59:22 +02007436 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007437 xpath_func = &xpath_sum;
7438 }
7439 break;
7440 case 4:
Michal Vasko004d3152020-06-11 19:59:22 +02007441 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007442 xpath_func = &xpath_lang;
Michal Vasko004d3152020-06-11 19:59:22 +02007443 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007444 xpath_func = &xpath_last;
Michal Vasko004d3152020-06-11 19:59:22 +02007445 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007446 xpath_func = &xpath_name;
Michal Vasko004d3152020-06-11 19:59:22 +02007447 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007448 xpath_func = &xpath_true;
7449 }
7450 break;
7451 case 5:
Michal Vasko004d3152020-06-11 19:59:22 +02007452 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007453 xpath_func = &xpath_count;
Michal Vasko004d3152020-06-11 19:59:22 +02007454 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007455 xpath_func = &xpath_false;
Michal Vasko004d3152020-06-11 19:59:22 +02007456 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007457 xpath_func = &xpath_floor;
Michal Vasko004d3152020-06-11 19:59:22 +02007458 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007459 xpath_func = &xpath_round;
Michal Vasko004d3152020-06-11 19:59:22 +02007460 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007461 xpath_func = &xpath_deref;
7462 }
7463 break;
7464 case 6:
Michal Vasko004d3152020-06-11 19:59:22 +02007465 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007466 xpath_func = &xpath_concat;
Michal Vasko004d3152020-06-11 19:59:22 +02007467 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007468 xpath_func = &xpath_number;
Michal Vasko004d3152020-06-11 19:59:22 +02007469 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007470 xpath_func = &xpath_string;
7471 }
7472 break;
7473 case 7:
Michal Vasko004d3152020-06-11 19:59:22 +02007474 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007475 xpath_func = &xpath_boolean;
Michal Vasko004d3152020-06-11 19:59:22 +02007476 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007477 xpath_func = &xpath_ceiling;
Michal Vasko004d3152020-06-11 19:59:22 +02007478 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007479 xpath_func = &xpath_current;
7480 }
7481 break;
7482 case 8:
Michal Vasko004d3152020-06-11 19:59:22 +02007483 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007484 xpath_func = &xpath_contains;
Michal Vasko004d3152020-06-11 19:59:22 +02007485 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007486 xpath_func = &xpath_position;
Michal Vasko004d3152020-06-11 19:59:22 +02007487 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007488 xpath_func = &xpath_re_match;
7489 }
7490 break;
7491 case 9:
Michal Vasko004d3152020-06-11 19:59:22 +02007492 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007493 xpath_func = &xpath_substring;
Michal Vasko004d3152020-06-11 19:59:22 +02007494 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007495 xpath_func = &xpath_translate;
7496 }
7497 break;
7498 case 10:
Michal Vasko004d3152020-06-11 19:59:22 +02007499 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007500 xpath_func = &xpath_local_name;
Michal Vasko004d3152020-06-11 19:59:22 +02007501 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007502 xpath_func = &xpath_enum_value;
Michal Vasko004d3152020-06-11 19:59:22 +02007503 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007504 xpath_func = &xpath_bit_is_set;
7505 }
7506 break;
7507 case 11:
Michal Vasko004d3152020-06-11 19:59:22 +02007508 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007509 xpath_func = &xpath_starts_with;
7510 }
7511 break;
7512 case 12:
Michal Vasko004d3152020-06-11 19:59:22 +02007513 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007514 xpath_func = &xpath_derived_from;
7515 }
7516 break;
7517 case 13:
Michal Vasko004d3152020-06-11 19:59:22 +02007518 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007519 xpath_func = &xpath_namespace_uri;
Michal Vasko004d3152020-06-11 19:59:22 +02007520 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007521 xpath_func = &xpath_string_length;
7522 }
7523 break;
7524 case 15:
Michal Vasko004d3152020-06-11 19:59:22 +02007525 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007526 xpath_func = &xpath_normalize_space;
Michal Vasko004d3152020-06-11 19:59:22 +02007527 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007528 xpath_func = &xpath_substring_after;
7529 }
7530 break;
7531 case 16:
Michal Vasko004d3152020-06-11 19:59:22 +02007532 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007533 xpath_func = &xpath_substring_before;
7534 }
7535 break;
7536 case 20:
Michal Vasko004d3152020-06-11 19:59:22 +02007537 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007538 xpath_func = &xpath_derived_from_or_self;
7539 }
7540 break;
7541 }
7542
7543 if (!xpath_func) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007544 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 +02007545 return LY_EVALID;
7546 }
7547 }
7548
7549 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007550 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007551 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007552
7553 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02007554 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007555 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007556 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007557 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007558
7559 /* ( Expr ( ',' Expr )* )? */
Michal Vasko004d3152020-06-11 19:59:22 +02007560 if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007561 if (set) {
7562 args = malloc(sizeof *args);
7563 LY_CHECK_ERR_GOTO(!args, LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7564 arg_count = 1;
7565 args[0] = set_copy(set);
7566 if (!args[0]) {
7567 rc = LY_EMEM;
7568 goto cleanup;
7569 }
7570
Michal Vasko004d3152020-06-11 19:59:22 +02007571 rc = eval_expr_select(exp, tok_idx, 0, args[0], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007572 LY_CHECK_GOTO(rc, cleanup);
7573 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007574 rc = eval_expr_select(exp, tok_idx, 0, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007575 LY_CHECK_GOTO(rc, cleanup);
7576 }
7577 }
Michal Vasko004d3152020-06-11 19:59:22 +02007578 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007579 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007580 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007581 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007582
7583 if (set) {
7584 ++arg_count;
7585 args_aux = realloc(args, arg_count * sizeof *args);
7586 LY_CHECK_ERR_GOTO(!args_aux, arg_count--; LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7587 args = args_aux;
7588 args[arg_count - 1] = set_copy(set);
7589 if (!args[arg_count - 1]) {
7590 rc = LY_EMEM;
7591 goto cleanup;
7592 }
7593
Michal Vasko004d3152020-06-11 19:59:22 +02007594 rc = eval_expr_select(exp, tok_idx, 0, args[arg_count - 1], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007595 LY_CHECK_GOTO(rc, cleanup);
7596 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007597 rc = eval_expr_select(exp, tok_idx, 0, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007598 LY_CHECK_GOTO(rc, cleanup);
7599 }
7600 }
7601
7602 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007603 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007604 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007605 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007606 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007607
7608 if (set) {
7609 /* evaluate function */
7610 rc = xpath_func(args, arg_count, set, options);
7611
7612 if (options & LYXP_SCNODE_ALL) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007613 /* merge all nodes from arg evaluations */
7614 for (i = 0; i < arg_count; ++i) {
7615 set_scnode_clear_ctx(args[i]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007616 lyxp_set_scnode_merge(set, args[i]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007617 }
7618 }
7619 } else {
7620 rc = LY_SUCCESS;
7621 }
7622
7623cleanup:
7624 for (i = 0; i < arg_count; ++i) {
7625 lyxp_set_free(args[i]);
7626 }
7627 free(args);
7628
7629 return rc;
7630}
7631
7632/**
7633 * @brief Evaluate Number. Logs directly on error.
7634 *
7635 * @param[in] ctx Context for errors.
7636 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007637 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007638 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7639 * @return LY_ERR
7640 */
7641static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007642eval_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 +02007643{
7644 long double num;
7645 char *endptr;
7646
7647 if (set) {
7648 errno = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02007649 num = strtold(&exp->expr[exp->tok_pos[*tok_idx]], &endptr);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007650 if (errno) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007651 LOGVAL(ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]);
7652 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 +02007653 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]], strerror(errno));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007654 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02007655 } else if (endptr - &exp->expr[exp->tok_pos[*tok_idx]] != exp->tok_len[*tok_idx]) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007656 LOGVAL(ctx, LY_VLOG_LYD, set->cur_node, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]);
7657 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 +02007658 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007659 return LY_EVALID;
7660 }
7661
7662 set_fill_number(set, num);
7663 }
7664
7665 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007666 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007667 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007668 return LY_SUCCESS;
7669}
7670
7671/**
7672 * @brief Evaluate PathExpr. Logs directly on error.
7673 *
Michal Vaskod3678892020-05-21 10:06:58 +02007674 * [12] PathExpr ::= LocationPath | PrimaryExpr Predicate*
Michal Vasko03ff5a72019-09-11 13:49:33 +02007675 * | PrimaryExpr Predicate* '/' RelativeLocationPath
7676 * | PrimaryExpr Predicate* '//' RelativeLocationPath
7677 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
Michal Vaskod3678892020-05-21 10:06:58 +02007678 * [10] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall
Michal Vasko03ff5a72019-09-11 13:49:33 +02007679 *
7680 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007681 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007682 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7683 * @param[in] options XPath options.
7684 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7685 */
7686static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007687eval_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 +02007688{
Radek Krejci857189e2020-09-01 13:26:36 +02007689 ly_bool all_desc, parent_pos_pred;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007690 LY_ERR rc;
7691
Michal Vasko004d3152020-06-11 19:59:22 +02007692 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007693 case LYXP_TOKEN_PAR1:
7694 /* '(' Expr ')' */
7695
7696 /* '(' */
7697 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007698 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007699 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007700
7701 /* Expr */
Michal Vasko004d3152020-06-11 19:59:22 +02007702 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007703 LY_CHECK_RET(rc);
7704
7705 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007706 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007707 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007708 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007709 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007710
7711 parent_pos_pred = 0;
7712 goto predicate;
7713
7714 case LYXP_TOKEN_DOT:
7715 case LYXP_TOKEN_DDOT:
7716 case LYXP_TOKEN_AT:
7717 case LYXP_TOKEN_NAMETEST:
7718 case LYXP_TOKEN_NODETYPE:
7719 /* RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007720 rc = eval_relative_location_path(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007721 LY_CHECK_RET(rc);
7722 break;
7723
7724 case LYXP_TOKEN_FUNCNAME:
7725 /* FunctionCall */
7726 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007727 rc = eval_function_call(exp, tok_idx, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007728 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007729 rc = eval_function_call(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007730 }
7731 LY_CHECK_RET(rc);
7732
7733 parent_pos_pred = 1;
7734 goto predicate;
7735
Michal Vasko3e48bf32020-06-01 08:39:07 +02007736 case LYXP_TOKEN_OPER_PATH:
7737 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02007738 /* AbsoluteLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007739 rc = eval_absolute_location_path(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007740 LY_CHECK_RET(rc);
7741 break;
7742
7743 case LYXP_TOKEN_LITERAL:
7744 /* Literal */
7745 if (!set || (options & LYXP_SCNODE_ALL)) {
7746 if (set) {
7747 set_scnode_clear_ctx(set);
7748 }
Michal Vasko004d3152020-06-11 19:59:22 +02007749 eval_literal(exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007750 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007751 eval_literal(exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007752 }
7753
7754 parent_pos_pred = 1;
7755 goto predicate;
7756
7757 case LYXP_TOKEN_NUMBER:
7758 /* Number */
7759 if (!set || (options & LYXP_SCNODE_ALL)) {
7760 if (set) {
7761 set_scnode_clear_ctx(set);
7762 }
Michal Vasko004d3152020-06-11 19:59:22 +02007763 rc = eval_number(NULL, exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007764 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007765 rc = eval_number(set->ctx, exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007766 }
7767 LY_CHECK_RET(rc);
7768
7769 parent_pos_pred = 1;
7770 goto predicate;
7771
7772 default:
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007773 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 +02007774 &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007775 return LY_EVALID;
7776 }
7777
7778 return LY_SUCCESS;
7779
7780predicate:
7781 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007782 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7783 rc = eval_predicate(exp, tok_idx, set, options, parent_pos_pred);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007784 LY_CHECK_RET(rc);
7785 }
7786
7787 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007788 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007789
7790 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02007791 if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007792 all_desc = 0;
7793 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007794 all_desc = 1;
7795 }
7796
7797 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007798 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007799 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007800
Michal Vasko004d3152020-06-11 19:59:22 +02007801 rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007802 LY_CHECK_RET(rc);
7803 }
7804
7805 return LY_SUCCESS;
7806}
7807
7808/**
7809 * @brief Evaluate UnionExpr. Logs directly on error.
7810 *
Michal Vaskod3678892020-05-21 10:06:58 +02007811 * [20] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007812 *
7813 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007814 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007815 * @param[in] repeat How many times this expression is repeated.
7816 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7817 * @param[in] options XPath options.
7818 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7819 */
7820static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007821eval_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 +02007822{
7823 LY_ERR rc = LY_SUCCESS;
7824 struct lyxp_set orig_set, set2;
7825 uint16_t i;
7826
7827 assert(repeat);
7828
7829 set_init(&orig_set, set);
7830 set_init(&set2, set);
7831
7832 set_fill_set(&orig_set, set);
7833
Michal Vasko004d3152020-06-11 19:59:22 +02007834 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007835 LY_CHECK_GOTO(rc, cleanup);
7836
7837 /* ('|' PathExpr)* */
7838 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007839 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_UNI);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007840 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007841 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007842 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007843
7844 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007845 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007846 LY_CHECK_GOTO(rc, cleanup);
7847 continue;
7848 }
7849
7850 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02007851 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007852 LY_CHECK_GOTO(rc, cleanup);
7853
7854 /* eval */
7855 if (options & LYXP_SCNODE_ALL) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01007856 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007857 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007858 rc = moveto_union(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007859 LY_CHECK_GOTO(rc, cleanup);
7860 }
7861 }
7862
7863cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007864 lyxp_set_free_content(&orig_set);
7865 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007866 return rc;
7867}
7868
7869/**
7870 * @brief Evaluate UnaryExpr. Logs directly on error.
7871 *
Michal Vaskod3678892020-05-21 10:06:58 +02007872 * [19] UnaryExpr ::= UnionExpr | '-' UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007873 *
7874 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007875 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007876 * @param[in] repeat How many times this expression is repeated.
7877 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7878 * @param[in] options XPath options.
7879 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7880 */
7881static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007882eval_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 +02007883{
7884 LY_ERR rc;
7885 uint16_t this_op, i;
7886
7887 assert(repeat);
7888
7889 /* ('-')+ */
Michal Vasko004d3152020-06-11 19:59:22 +02007890 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007891 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007892 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 +02007893
7894 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007895 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007896 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007897 }
7898
Michal Vasko004d3152020-06-11 19:59:22 +02007899 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNARY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007900 LY_CHECK_RET(rc);
7901
7902 if (set && (repeat % 2)) {
7903 if (options & LYXP_SCNODE_ALL) {
7904 warn_operands(set->ctx, set, NULL, 1, exp->expr, exp->tok_pos[this_op]);
7905 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007906 rc = moveto_op_math(set, NULL, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007907 LY_CHECK_RET(rc);
7908 }
7909 }
7910
7911 return LY_SUCCESS;
7912}
7913
7914/**
7915 * @brief Evaluate MultiplicativeExpr. Logs directly on error.
7916 *
Michal Vaskod3678892020-05-21 10:06:58 +02007917 * [18] MultiplicativeExpr ::= UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007918 * | MultiplicativeExpr '*' UnaryExpr
7919 * | MultiplicativeExpr 'div' UnaryExpr
7920 * | MultiplicativeExpr 'mod' UnaryExpr
7921 *
7922 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007923 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007924 * @param[in] repeat How many times this expression is repeated.
7925 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7926 * @param[in] options XPath options.
7927 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7928 */
7929static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007930eval_multiplicative_expr(const struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set,
7931 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007932{
7933 LY_ERR rc;
7934 uint16_t this_op;
7935 struct lyxp_set orig_set, set2;
7936 uint16_t i;
7937
7938 assert(repeat);
7939
7940 set_init(&orig_set, set);
7941 set_init(&set2, set);
7942
7943 set_fill_set(&orig_set, set);
7944
Michal Vasko004d3152020-06-11 19:59:22 +02007945 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007946 LY_CHECK_GOTO(rc, cleanup);
7947
7948 /* ('*' / 'div' / 'mod' UnaryExpr)* */
7949 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007950 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007951
Michal Vasko004d3152020-06-11 19:59:22 +02007952 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007953 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007954 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007955 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007956
7957 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007958 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007959 LY_CHECK_GOTO(rc, cleanup);
7960 continue;
7961 }
7962
7963 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02007964 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007965 LY_CHECK_GOTO(rc, cleanup);
7966
7967 /* eval */
7968 if (options & LYXP_SCNODE_ALL) {
7969 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007970 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007971 set_scnode_clear_ctx(set);
7972 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007973 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007974 LY_CHECK_GOTO(rc, cleanup);
7975 }
7976 }
7977
7978cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007979 lyxp_set_free_content(&orig_set);
7980 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007981 return rc;
7982}
7983
7984/**
7985 * @brief Evaluate AdditiveExpr. Logs directly on error.
7986 *
Michal Vaskod3678892020-05-21 10:06:58 +02007987 * [17] AdditiveExpr ::= MultiplicativeExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007988 * | AdditiveExpr '+' MultiplicativeExpr
7989 * | AdditiveExpr '-' MultiplicativeExpr
7990 *
7991 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007992 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007993 * @param[in] repeat How many times this expression is repeated.
7994 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7995 * @param[in] options XPath options.
7996 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7997 */
7998static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007999eval_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 +02008000{
8001 LY_ERR rc;
8002 uint16_t this_op;
8003 struct lyxp_set orig_set, set2;
8004 uint16_t i;
8005
8006 assert(repeat);
8007
8008 set_init(&orig_set, set);
8009 set_init(&set2, set);
8010
8011 set_fill_set(&orig_set, set);
8012
Michal Vasko004d3152020-06-11 19:59:22 +02008013 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008014 LY_CHECK_GOTO(rc, cleanup);
8015
8016 /* ('+' / '-' MultiplicativeExpr)* */
8017 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008018 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008019
Michal Vasko004d3152020-06-11 19:59:22 +02008020 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008021 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02008022 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008023 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008024
8025 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02008026 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008027 LY_CHECK_GOTO(rc, cleanup);
8028 continue;
8029 }
8030
8031 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008032 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008033 LY_CHECK_GOTO(rc, cleanup);
8034
8035 /* eval */
8036 if (options & LYXP_SCNODE_ALL) {
8037 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008038 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008039 set_scnode_clear_ctx(set);
8040 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008041 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008042 LY_CHECK_GOTO(rc, cleanup);
8043 }
8044 }
8045
8046cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008047 lyxp_set_free_content(&orig_set);
8048 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008049 return rc;
8050}
8051
8052/**
8053 * @brief Evaluate RelationalExpr. Logs directly on error.
8054 *
Michal Vaskod3678892020-05-21 10:06:58 +02008055 * [16] RelationalExpr ::= AdditiveExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008056 * | RelationalExpr '<' AdditiveExpr
8057 * | RelationalExpr '>' AdditiveExpr
8058 * | RelationalExpr '<=' AdditiveExpr
8059 * | RelationalExpr '>=' AdditiveExpr
8060 *
8061 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008062 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008063 * @param[in] repeat How many times this expression is repeated.
8064 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8065 * @param[in] options XPath options.
8066 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8067 */
8068static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008069eval_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 +02008070{
8071 LY_ERR rc;
8072 uint16_t this_op;
8073 struct lyxp_set orig_set, set2;
8074 uint16_t i;
8075
8076 assert(repeat);
8077
8078 set_init(&orig_set, set);
8079 set_init(&set2, set);
8080
8081 set_fill_set(&orig_set, set);
8082
Michal Vasko004d3152020-06-11 19:59:22 +02008083 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008084 LY_CHECK_GOTO(rc, cleanup);
8085
8086 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
8087 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008088 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008089
Michal Vasko004d3152020-06-11 19:59:22 +02008090 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_COMP);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008091 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02008092 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008093 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008094
8095 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02008096 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008097 LY_CHECK_GOTO(rc, cleanup);
8098 continue;
8099 }
8100
8101 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008102 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008103 LY_CHECK_GOTO(rc, cleanup);
8104
8105 /* eval */
8106 if (options & LYXP_SCNODE_ALL) {
8107 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008108 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008109 set_scnode_clear_ctx(set);
8110 } else {
8111 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options);
8112 LY_CHECK_GOTO(rc, cleanup);
8113 }
8114 }
8115
8116cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008117 lyxp_set_free_content(&orig_set);
8118 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008119 return rc;
8120}
8121
8122/**
8123 * @brief Evaluate EqualityExpr. Logs directly on error.
8124 *
Michal Vaskod3678892020-05-21 10:06:58 +02008125 * [15] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008126 * | EqualityExpr '!=' RelationalExpr
8127 *
8128 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008129 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008130 * @param[in] repeat How many times this expression is repeated.
8131 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8132 * @param[in] options XPath options.
8133 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8134 */
8135static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008136eval_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 +02008137{
8138 LY_ERR rc;
8139 uint16_t this_op;
8140 struct lyxp_set orig_set, set2;
8141 uint16_t i;
8142
8143 assert(repeat);
8144
8145 set_init(&orig_set, set);
8146 set_init(&set2, set);
8147
8148 set_fill_set(&orig_set, set);
8149
Michal Vasko004d3152020-06-11 19:59:22 +02008150 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008151 LY_CHECK_GOTO(rc, cleanup);
8152
8153 /* ('=' / '!=' RelationalExpr)* */
8154 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008155 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008156
Michal Vasko004d3152020-06-11 19:59:22 +02008157 assert((exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_EQUAL) || (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_NEQUAL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008158 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02008159 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008160 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008161
8162 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02008163 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008164 LY_CHECK_GOTO(rc, cleanup);
8165 continue;
8166 }
8167
8168 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008169 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008170 LY_CHECK_GOTO(rc, cleanup);
8171
8172 /* eval */
8173 if (options & LYXP_SCNODE_ALL) {
8174 warn_operands(set->ctx, set, &set2, 0, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vasko004d3152020-06-11 19:59:22 +02008175 warn_equality_value(exp, set, *tok_idx - 1, this_op - 1, *tok_idx - 1);
8176 warn_equality_value(exp, &set2, this_op - 1, this_op - 1, *tok_idx - 1);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008177 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008178 set_scnode_clear_ctx(set);
8179 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008180 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options);
8181 LY_CHECK_GOTO(rc, cleanup);
8182 }
8183 }
8184
8185cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008186 lyxp_set_free_content(&orig_set);
8187 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008188 return rc;
8189}
8190
8191/**
8192 * @brief Evaluate AndExpr. Logs directly on error.
8193 *
Michal Vaskod3678892020-05-21 10:06:58 +02008194 * [14] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008195 *
8196 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008197 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008198 * @param[in] repeat How many times this expression is repeated.
8199 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8200 * @param[in] options XPath options.
8201 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8202 */
8203static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008204eval_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 +02008205{
8206 LY_ERR rc;
8207 struct lyxp_set orig_set, set2;
8208 uint16_t i;
8209
8210 assert(repeat);
8211
8212 set_init(&orig_set, set);
8213 set_init(&set2, set);
8214
8215 set_fill_set(&orig_set, set);
8216
Michal Vasko004d3152020-06-11 19:59:22 +02008217 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008218 LY_CHECK_GOTO(rc, cleanup);
8219
8220 /* cast to boolean, we know that will be the final result */
8221 if (set && (options & LYXP_SCNODE_ALL)) {
8222 set_scnode_clear_ctx(set);
8223 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008224 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008225 }
8226
8227 /* ('and' EqualityExpr)* */
8228 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008229 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
8230 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (!set || !set->val.bln ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008231 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008232 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008233
8234 /* lazy evaluation */
Michal Vasko004d3152020-06-11 19:59:22 +02008235 if (!set || ((set->type == LYXP_SET_BOOLEAN) && !set->val.bln)) {
8236 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008237 LY_CHECK_GOTO(rc, cleanup);
8238 continue;
8239 }
8240
8241 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008242 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008243 LY_CHECK_GOTO(rc, cleanup);
8244
8245 /* eval - just get boolean value actually */
8246 if (set->type == LYXP_SET_SCNODE_SET) {
8247 set_scnode_clear_ctx(&set2);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008248 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008249 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008250 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008251 set_fill_set(set, &set2);
8252 }
8253 }
8254
8255cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008256 lyxp_set_free_content(&orig_set);
8257 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008258 return rc;
8259}
8260
8261/**
8262 * @brief Evaluate OrExpr. Logs directly on error.
8263 *
Michal Vaskod3678892020-05-21 10:06:58 +02008264 * [13] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008265 *
8266 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008267 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008268 * @param[in] repeat How many times this expression is repeated.
8269 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8270 * @param[in] options XPath options.
8271 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8272 */
8273static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008274eval_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 +02008275{
8276 LY_ERR rc;
8277 struct lyxp_set orig_set, set2;
8278 uint16_t i;
8279
8280 assert(repeat);
8281
8282 set_init(&orig_set, set);
8283 set_init(&set2, set);
8284
8285 set_fill_set(&orig_set, set);
8286
Michal Vasko004d3152020-06-11 19:59:22 +02008287 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008288 LY_CHECK_GOTO(rc, cleanup);
8289
8290 /* cast to boolean, we know that will be the final result */
8291 if (set && (options & LYXP_SCNODE_ALL)) {
8292 set_scnode_clear_ctx(set);
8293 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008294 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008295 }
8296
8297 /* ('or' AndExpr)* */
8298 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008299 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
8300 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (!set || set->val.bln ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008301 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008302 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008303
8304 /* lazy evaluation */
Michal Vasko004d3152020-06-11 19:59:22 +02008305 if (!set || ((set->type == LYXP_SET_BOOLEAN) && set->val.bln)) {
8306 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008307 LY_CHECK_GOTO(rc, cleanup);
8308 continue;
8309 }
8310
8311 set_fill_set(&set2, &orig_set);
8312 /* expr_type cound have been LYXP_EXPR_NONE in all these later calls (except for the first one),
8313 * but it does not matter */
Michal Vasko004d3152020-06-11 19:59:22 +02008314 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008315 LY_CHECK_GOTO(rc, cleanup);
8316
8317 /* eval - just get boolean value actually */
8318 if (set->type == LYXP_SET_SCNODE_SET) {
8319 set_scnode_clear_ctx(&set2);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008320 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008321 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008322 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008323 set_fill_set(set, &set2);
8324 }
8325 }
8326
8327cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008328 lyxp_set_free_content(&orig_set);
8329 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008330 return rc;
8331}
8332
8333/**
Michal Vasko004d3152020-06-11 19:59:22 +02008334 * @brief Decide what expression is at the pointer @p tok_idx and evaluate it accordingly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008335 *
8336 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008337 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008338 * @param[in] etype Expression type to evaluate.
8339 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8340 * @param[in] options XPath options.
8341 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8342 */
8343static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008344eval_expr_select(const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_expr_type etype, struct lyxp_set *set,
8345 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008346{
8347 uint16_t i, count;
8348 enum lyxp_expr_type next_etype;
8349 LY_ERR rc;
8350
8351 /* process operator repeats */
Michal Vasko004d3152020-06-11 19:59:22 +02008352 if (!exp->repeat[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008353 next_etype = LYXP_EXPR_NONE;
8354 } else {
8355 /* find etype repeat */
Radek Krejci1e008d22020-08-17 11:37:37 +02008356 for (i = 0; exp->repeat[*tok_idx][i] > etype; ++i) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02008357
8358 /* select one-priority lower because etype expression called us */
8359 if (i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008360 next_etype = exp->repeat[*tok_idx][i - 1];
Michal Vasko03ff5a72019-09-11 13:49:33 +02008361 /* count repeats for that expression */
Radek Krejci1e008d22020-08-17 11:37:37 +02008362 for (count = 0; i && exp->repeat[*tok_idx][i - 1] == next_etype; ++count, --i) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02008363 } else {
8364 next_etype = LYXP_EXPR_NONE;
8365 }
8366 }
8367
8368 /* decide what expression are we parsing based on the repeat */
8369 switch (next_etype) {
8370 case LYXP_EXPR_OR:
Michal Vasko004d3152020-06-11 19:59:22 +02008371 rc = eval_or_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008372 break;
8373 case LYXP_EXPR_AND:
Michal Vasko004d3152020-06-11 19:59:22 +02008374 rc = eval_and_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008375 break;
8376 case LYXP_EXPR_EQUALITY:
Michal Vasko004d3152020-06-11 19:59:22 +02008377 rc = eval_equality_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008378 break;
8379 case LYXP_EXPR_RELATIONAL:
Michal Vasko004d3152020-06-11 19:59:22 +02008380 rc = eval_relational_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008381 break;
8382 case LYXP_EXPR_ADDITIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02008383 rc = eval_additive_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008384 break;
8385 case LYXP_EXPR_MULTIPLICATIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02008386 rc = eval_multiplicative_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008387 break;
8388 case LYXP_EXPR_UNARY:
Michal Vasko004d3152020-06-11 19:59:22 +02008389 rc = eval_unary_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008390 break;
8391 case LYXP_EXPR_UNION:
Michal Vasko004d3152020-06-11 19:59:22 +02008392 rc = eval_union_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008393 break;
8394 case LYXP_EXPR_NONE:
Michal Vasko004d3152020-06-11 19:59:22 +02008395 rc = eval_path_expr(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008396 break;
8397 default:
8398 LOGINT_RET(set->ctx);
8399 }
8400
8401 return rc;
8402}
8403
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008404/**
8405 * @brief Get root type.
8406 *
8407 * @param[in] ctx_node Context node.
8408 * @param[in] ctx_scnode Schema context node.
8409 * @param[in] options XPath options.
8410 * @return Root type.
8411 */
8412static enum lyxp_node_type
Radek Krejci1deb5be2020-08-26 16:43:36 +02008413lyxp_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 +01008414{
Michal Vasko6b26e742020-07-17 15:02:10 +02008415 const struct lysc_node *op;
8416
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008417 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008418 /* schema */
Radek Krejci1e008d22020-08-17 11:37:37 +02008419 for (op = ctx_scnode; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02008420
8421 if (op || (options & LYXP_SCNODE)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008422 /* general root that can access everything */
8423 return LYXP_NODE_ROOT;
8424 } else if (!ctx_scnode || (ctx_scnode->flags & LYS_CONFIG_W)) {
8425 /* root context node can access only config data (because we said so, it is unspecified) */
8426 return LYXP_NODE_ROOT_CONFIG;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008427 }
Michal Vasko6b26e742020-07-17 15:02:10 +02008428 return LYXP_NODE_ROOT;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008429 }
8430
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008431 /* data */
Michal Vasko6b26e742020-07-17 15:02:10 +02008432 op = ctx_node ? ctx_node->schema : NULL;
Michal Vaskod989ba02020-08-24 10:59:24 +02008433 for ( ; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02008434
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008435 if (op || !(options & LYXP_SCHEMA)) {
8436 /* general root that can access everything */
8437 return LYXP_NODE_ROOT;
8438 } else if (!ctx_node || (ctx_node->schema->flags & LYS_CONFIG_W)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008439 /* root context node can access only config data (because we said so, it is unspecified) */
8440 return LYXP_NODE_ROOT_CONFIG;
8441 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008442 return LYXP_NODE_ROOT;
8443}
8444
Michal Vasko03ff5a72019-09-11 13:49:33 +02008445LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008446lyxp_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 +02008447 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 +02008448{
Michal Vasko004d3152020-06-11 19:59:22 +02008449 uint16_t tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008450 LY_ERR rc;
8451
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008452 LY_CHECK_ARG_RET(NULL, exp, set, LY_EINVAL);
8453 if (!cur_mod && ((format == LY_PREF_SCHEMA) || (format == LY_PREF_SCHEMA_RESOLVED))) {
8454 LOGARG(NULL, "Current module must be set if schema format is used.");
8455 return LY_EINVAL;
Michal Vasko004d3152020-06-11 19:59:22 +02008456 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008457
8458 /* prepare set for evaluation */
Michal Vasko03ff5a72019-09-11 13:49:33 +02008459 memset(set, 0, sizeof *set);
Michal Vaskod3678892020-05-21 10:06:58 +02008460 set->type = LYXP_SET_NODE_SET;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008461 set->root_type = lyxp_get_root_type(ctx_node, NULL, options);
8462 set_insert_node(set, (struct lyd_node *)ctx_node, 0, ctx_node ? LYXP_NODE_ELEM : set->root_type, 0);
8463
8464 set->ctx = (struct ly_ctx *)(cur_mod ? cur_mod->ctx : (ctx_node ? LYD_CTX(ctx_node) : (tree ? LYD_CTX(tree) : NULL)));
8465 set->cur_node = ctx_node;
8466 for (set->context_op = ctx_node ? ctx_node->schema : NULL;
Radek Krejci0f969882020-08-21 16:56:47 +02008467 set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF));
8468 set->context_op = set->context_op->parent) {}
Michal Vaskof03ed032020-03-04 13:31:44 +01008469 set->tree = tree;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008470 set->cur_mod = cur_mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008471 set->format = format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008472 set->prefix_data = prefix_data;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008473
8474 /* evaluate */
Michal Vasko004d3152020-06-11 19:59:22 +02008475 rc = eval_expr_select(exp, &tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008476 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02008477 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008478 }
8479
Michal Vasko03ff5a72019-09-11 13:49:33 +02008480 return rc;
8481}
8482
8483#if 0
8484
8485/* full xml printing of set elements, not used currently */
8486
8487void
8488lyxp_set_print_xml(FILE *f, struct lyxp_set *set)
8489{
8490 uint32_t i;
8491 char *str_num;
8492 struct lyout out;
8493
8494 memset(&out, 0, sizeof out);
8495
8496 out.type = LYOUT_STREAM;
8497 out.method.f = f;
8498
8499 switch (set->type) {
8500 case LYXP_SET_EMPTY:
Michal Vasko5233e962020-08-14 14:26:20 +02008501 ly_print_(&out, "Empty XPath set\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008502 break;
8503 case LYXP_SET_BOOLEAN:
Michal Vasko5233e962020-08-14 14:26:20 +02008504 ly_print_(&out, "Boolean XPath set:\n");
8505 ly_print_(&out, "%s\n\n", set->value.bool ? "true" : "false");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008506 break;
8507 case LYXP_SET_STRING:
Michal Vasko5233e962020-08-14 14:26:20 +02008508 ly_print_(&out, "String XPath set:\n");
8509 ly_print_(&out, "\"%s\"\n\n", set->value.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008510 break;
8511 case LYXP_SET_NUMBER:
Michal Vasko5233e962020-08-14 14:26:20 +02008512 ly_print_(&out, "Number XPath set:\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008513
8514 if (isnan(set->value.num)) {
8515 str_num = strdup("NaN");
8516 } else if ((set->value.num == 0) || (set->value.num == -0.0f)) {
8517 str_num = strdup("0");
8518 } else if (isinf(set->value.num) && !signbit(set->value.num)) {
8519 str_num = strdup("Infinity");
8520 } else if (isinf(set->value.num) && signbit(set->value.num)) {
8521 str_num = strdup("-Infinity");
8522 } else if ((long long)set->value.num == set->value.num) {
8523 if (asprintf(&str_num, "%lld", (long long)set->value.num) == -1) {
8524 str_num = NULL;
8525 }
8526 } else {
8527 if (asprintf(&str_num, "%03.1Lf", set->value.num) == -1) {
8528 str_num = NULL;
8529 }
8530 }
8531 if (!str_num) {
8532 LOGMEM;
8533 return;
8534 }
Michal Vasko5233e962020-08-14 14:26:20 +02008535 ly_print_(&out, "%s\n\n", str_num);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008536 free(str_num);
8537 break;
8538 case LYXP_SET_NODE_SET:
Michal Vasko5233e962020-08-14 14:26:20 +02008539 ly_print_(&out, "Node XPath set:\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008540
8541 for (i = 0; i < set->used; ++i) {
Michal Vasko5233e962020-08-14 14:26:20 +02008542 ly_print_(&out, "%d. ", i + 1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008543 switch (set->node_type[i]) {
8544 case LYXP_NODE_ROOT_ALL:
Michal Vasko5233e962020-08-14 14:26:20 +02008545 ly_print_(&out, "ROOT all\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008546 break;
8547 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko5233e962020-08-14 14:26:20 +02008548 ly_print_(&out, "ROOT config\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008549 break;
8550 case LYXP_NODE_ROOT_STATE:
Michal Vasko5233e962020-08-14 14:26:20 +02008551 ly_print_(&out, "ROOT state\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008552 break;
8553 case LYXP_NODE_ROOT_NOTIF:
Michal Vasko5233e962020-08-14 14:26:20 +02008554 ly_print_(&out, "ROOT notification \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008555 break;
8556 case LYXP_NODE_ROOT_RPC:
Michal Vasko5233e962020-08-14 14:26:20 +02008557 ly_print_(&out, "ROOT rpc \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008558 break;
8559 case LYXP_NODE_ROOT_OUTPUT:
Michal Vasko5233e962020-08-14 14:26:20 +02008560 ly_print_(&out, "ROOT output \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008561 break;
8562 case LYXP_NODE_ELEM:
Michal Vasko5233e962020-08-14 14:26:20 +02008563 ly_print_(&out, "ELEM \"%s\"\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008564 xml_print_node(&out, 1, set->value.nodes[i], 1, LYP_FORMAT);
Michal Vasko5233e962020-08-14 14:26:20 +02008565 ly_print_(&out, "\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008566 break;
8567 case LYXP_NODE_TEXT:
Michal Vasko5233e962020-08-14 14:26:20 +02008568 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 +02008569 break;
8570 case LYXP_NODE_ATTR:
Michal Vasko5233e962020-08-14 14:26:20 +02008571 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 +02008572 break;
8573 }
8574 }
8575 break;
8576 }
8577}
8578
8579#endif
8580
8581LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008582lyxp_set_cast(struct lyxp_set *set, enum lyxp_set_type target)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008583{
8584 long double num;
8585 char *str;
8586 LY_ERR rc;
8587
8588 if (!set || (set->type == target)) {
8589 return LY_SUCCESS;
8590 }
8591
8592 /* it's not possible to convert anything into a node set */
Michal Vaskod3678892020-05-21 10:06:58 +02008593 assert(target != LYXP_SET_NODE_SET);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008594
8595 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vaskod3678892020-05-21 10:06:58 +02008596 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008597 return LY_EINVAL;
8598 }
8599
8600 /* to STRING */
Michal Vaskod3678892020-05-21 10:06:58 +02008601 if ((target == LYXP_SET_STRING) || ((target == LYXP_SET_NUMBER) && (set->type == LYXP_SET_NODE_SET))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008602 switch (set->type) {
8603 case LYXP_SET_NUMBER:
8604 if (isnan(set->val.num)) {
8605 set->val.str = strdup("NaN");
8606 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8607 } else if ((set->val.num == 0) || (set->val.num == -0.0f)) {
8608 set->val.str = strdup("0");
8609 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8610 } else if (isinf(set->val.num) && !signbit(set->val.num)) {
8611 set->val.str = strdup("Infinity");
8612 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8613 } else if (isinf(set->val.num) && signbit(set->val.num)) {
8614 set->val.str = strdup("-Infinity");
8615 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8616 } else if ((long long)set->val.num == set->val.num) {
8617 if (asprintf(&str, "%lld", (long long)set->val.num) == -1) {
8618 LOGMEM_RET(set->ctx);
8619 }
8620 set->val.str = str;
8621 } else {
8622 if (asprintf(&str, "%03.1Lf", set->val.num) == -1) {
8623 LOGMEM_RET(set->ctx);
8624 }
8625 set->val.str = str;
8626 }
8627 break;
8628 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02008629 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008630 set->val.str = strdup("true");
8631 } else {
8632 set->val.str = strdup("false");
8633 }
8634 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM);
8635 break;
8636 case LYXP_SET_NODE_SET:
8637 assert(set->used);
8638
8639 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008640 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008641
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008642 rc = cast_node_set_to_string(set, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008643 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02008644 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008645 set->val.str = str;
8646 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008647 default:
8648 LOGINT_RET(set->ctx);
8649 }
8650 set->type = LYXP_SET_STRING;
8651 }
8652
8653 /* to NUMBER */
8654 if (target == LYXP_SET_NUMBER) {
8655 switch (set->type) {
8656 case LYXP_SET_STRING:
8657 num = cast_string_to_number(set->val.str);
Michal Vaskod3678892020-05-21 10:06:58 +02008658 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008659 set->val.num = num;
8660 break;
8661 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02008662 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008663 set->val.num = 1;
8664 } else {
8665 set->val.num = 0;
8666 }
8667 break;
8668 default:
8669 LOGINT_RET(set->ctx);
8670 }
8671 set->type = LYXP_SET_NUMBER;
8672 }
8673
8674 /* to BOOLEAN */
8675 if (target == LYXP_SET_BOOLEAN) {
8676 switch (set->type) {
8677 case LYXP_SET_NUMBER:
8678 if ((set->val.num == 0) || (set->val.num == -0.0f) || isnan(set->val.num)) {
Michal Vasko004d3152020-06-11 19:59:22 +02008679 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008680 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02008681 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008682 }
8683 break;
8684 case LYXP_SET_STRING:
8685 if (set->val.str[0]) {
Michal Vaskod3678892020-05-21 10:06:58 +02008686 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008687 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008688 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02008689 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008690 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008691 }
8692 break;
8693 case LYXP_SET_NODE_SET:
Michal Vaskod3678892020-05-21 10:06:58 +02008694 if (set->used) {
8695 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008696 set->val.bln = 1;
Michal Vaskod3678892020-05-21 10:06:58 +02008697 } else {
8698 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008699 set->val.bln = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02008700 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008701 break;
8702 default:
8703 LOGINT_RET(set->ctx);
8704 }
8705 set->type = LYXP_SET_BOOLEAN;
8706 }
8707
Michal Vasko03ff5a72019-09-11 13:49:33 +02008708 return LY_SUCCESS;
8709}
8710
8711LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008712lyxp_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 +02008713 const struct lysc_node *ctx_scnode, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008714{
Michal Vasko004d3152020-06-11 19:59:22 +02008715 uint16_t tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008716
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008717 LY_CHECK_ARG_RET(NULL, exp, set, LY_EINVAL);
8718 if (!cur_mod && ((format == LY_PREF_SCHEMA) || (format == LY_PREF_SCHEMA_RESOLVED))) {
8719 LOGARG(NULL, "Current module must be set if schema format is used.");
8720 return LY_EINVAL;
Michal Vasko004d3152020-06-11 19:59:22 +02008721 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008722
8723 /* prepare set for evaluation */
Michal Vasko03ff5a72019-09-11 13:49:33 +02008724 memset(set, 0, sizeof *set);
8725 set->type = LYXP_SET_SCNODE_SET;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008726 set->root_type = lyxp_get_root_type(NULL, ctx_scnode, options);
8727 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 +01008728 set->val.scnodes[0].in_ctx = -2;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008729
8730 set->ctx = cur_mod ? cur_mod->ctx : (ctx_scnode ? ctx_scnode->module->ctx : NULL);
8731 set->cur_scnode = ctx_scnode;
Michal Vasko6b26e742020-07-17 15:02:10 +02008732 for (set->context_op = ctx_scnode;
Radek Krejci0f969882020-08-21 16:56:47 +02008733 set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF));
8734 set->context_op = set->context_op->parent) {}
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008735 set->cur_mod = cur_mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008736 set->format = format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008737 set->prefix_data = prefix_data;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008738
8739 /* evaluate */
Michal Vasko004d3152020-06-11 19:59:22 +02008740 return eval_expr_select(exp, &tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008741}
Michal Vaskod43d71a2020-08-07 14:54:58 +02008742
8743API const char *
8744lyxp_get_expr(const struct lyxp_expr *path)
8745{
8746 if (!path) {
8747 return NULL;
8748 }
8749
8750 return path->expr;
8751}