blob: 9deae1de414f99664baf1b6d2298b34df36e79bd [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
Radek Krejcif8dc59a2020-11-25 13:47:44 +010015#define _POSIX_C_SOURCE 200809L /* strdup, strndup */
Michal Vasko03ff5a72019-09-11 13:49:33 +020016
17/* needed by libmath functions isfinite(), isinf(), isnan(), signbit(), ... */
18#define _ISOC99_SOURCE
Radek Krejcib1646a92018-11-02 16:08:26 +010019
Radek Krejci535ea9f2020-05-29 16:01:05 +020020#include "xpath.h"
Radek Krejcib1646a92018-11-02 16:08:26 +010021
Radek Krejci535ea9f2020-05-29 16:01:05 +020022#include <assert.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010023#include <ctype.h>
Radek Krejci535ea9f2020-05-29 16:01:05 +020024#include <errno.h>
Radek Krejci535ea9f2020-05-29 16:01:05 +020025#include <math.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020026#include <stdint.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010027#include <stdio.h>
28#include <stdlib.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010029#include <string.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010030
Radek Krejci535ea9f2020-05-29 16:01:05 +020031#include "common.h"
Michal Vasko5aa44c02020-06-29 11:47:02 +020032#include "compat.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020033#include "context.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020034#include "dict.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020035#include "hash_table.h"
Radek Krejci47fab892020-11-05 17:02:41 +010036#include "out.h"
Radek Krejci7931b192020-06-25 17:05:03 +020037#include "parser_data.h"
Michal Vasko004d3152020-06-11 19:59:22 +020038#include "path.h"
Michal Vasko03ff5a72019-09-11 13:49:33 +020039#include "plugins_types.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020040#include "printer_data.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020041#include "schema_compile_node.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020042#include "tree.h"
Radek Krejci77114102021-03-10 15:21:57 +010043#include "tree_data.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020044#include "tree_data_internal.h"
Radek Krejci859a15a2021-03-05 20:56:59 +010045#include "tree_edit.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020046#include "tree_schema_internal.h"
47#include "xml.h"
Michal Vasko03ff5a72019-09-11 13:49:33 +020048
Michal Vasko004d3152020-06-11 19:59:22 +020049static 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 +020050static LY_ERR eval_expr_select(const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_expr_type etype,
51 struct lyxp_set *set, uint32_t options);
Michal Vasko03ff5a72019-09-11 13:49:33 +020052
53/**
54 * @brief Print the type of an XPath \p set.
55 *
56 * @param[in] set Set to use.
57 * @return Set type string.
58 */
59static const char *
60print_set_type(struct lyxp_set *set)
61{
62 switch (set->type) {
Michal Vasko03ff5a72019-09-11 13:49:33 +020063 case LYXP_SET_NODE_SET:
64 return "node set";
65 case LYXP_SET_SCNODE_SET:
66 return "schema node set";
67 case LYXP_SET_BOOLEAN:
68 return "boolean";
69 case LYXP_SET_NUMBER:
70 return "number";
71 case LYXP_SET_STRING:
72 return "string";
73 }
74
75 return NULL;
76}
77
Michal Vasko24cddf82020-06-01 08:17:01 +020078const char *
79lyxp_print_token(enum lyxp_token tok)
Michal Vasko03ff5a72019-09-11 13:49:33 +020080{
81 switch (tok) {
82 case LYXP_TOKEN_PAR1:
83 return "(";
84 case LYXP_TOKEN_PAR2:
85 return ")";
86 case LYXP_TOKEN_BRACK1:
87 return "[";
88 case LYXP_TOKEN_BRACK2:
89 return "]";
90 case LYXP_TOKEN_DOT:
91 return ".";
92 case LYXP_TOKEN_DDOT:
93 return "..";
94 case LYXP_TOKEN_AT:
95 return "@";
96 case LYXP_TOKEN_COMMA:
97 return ",";
98 case LYXP_TOKEN_NAMETEST:
99 return "NameTest";
100 case LYXP_TOKEN_NODETYPE:
101 return "NodeType";
102 case LYXP_TOKEN_FUNCNAME:
103 return "FunctionName";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200104 case LYXP_TOKEN_OPER_LOG:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200105 return "Operator(Logic)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200106 case LYXP_TOKEN_OPER_EQUAL:
107 return "Operator(Equal)";
108 case LYXP_TOKEN_OPER_NEQUAL:
109 return "Operator(Non-equal)";
110 case LYXP_TOKEN_OPER_COMP:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200111 return "Operator(Comparison)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200112 case LYXP_TOKEN_OPER_MATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200113 return "Operator(Math)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200114 case LYXP_TOKEN_OPER_UNI:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200115 return "Operator(Union)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200116 case LYXP_TOKEN_OPER_PATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200117 return "Operator(Path)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200118 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko14676352020-05-29 11:35:55 +0200119 return "Operator(Recursive Path)";
Michal Vasko03ff5a72019-09-11 13:49:33 +0200120 case LYXP_TOKEN_LITERAL:
121 return "Literal";
122 case LYXP_TOKEN_NUMBER:
123 return "Number";
124 default:
125 LOGINT(NULL);
126 return "";
127 }
128}
129
130/**
131 * @brief Print the whole expression \p exp to debug output.
132 *
133 * @param[in] exp Expression to use.
134 */
135static void
Michal Vasko40308e72020-10-20 16:38:40 +0200136print_expr_struct_debug(const struct lyxp_expr *exp)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200137{
Radek Krejcif13b87b2020-12-01 22:02:17 +0100138#define MSG_BUFFER_SIZE 128
139 char tmp[MSG_BUFFER_SIZE];
Michal Vasko1fdd8fa2021-01-08 09:21:45 +0100140 uint32_t i, j;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200141
Radek Krejci52b6d512020-10-12 12:33:17 +0200142 if (!exp || (ly_ll < LY_LLDBG)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200143 return;
144 }
145
146 LOGDBG(LY_LDGXPATH, "expression \"%s\":", exp->expr);
147 for (i = 0; i < exp->used; ++i) {
Michal Vasko24cddf82020-06-01 08:17:01 +0200148 sprintf(tmp, "\ttoken %s, in expression \"%.*s\"", lyxp_print_token(exp->tokens[i]), exp->tok_len[i],
Michal Vasko69730152020-10-09 16:30:07 +0200149 &exp->expr[exp->tok_pos[i]]);
Michal Vasko23049552021-03-04 15:57:44 +0100150 if (exp->repeat && exp->repeat[i]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200151 sprintf(tmp + strlen(tmp), " (repeat %d", exp->repeat[i][0]);
152 for (j = 1; exp->repeat[i][j]; ++j) {
153 sprintf(tmp + strlen(tmp), ", %d", exp->repeat[i][j]);
154 }
155 strcat(tmp, ")");
156 }
157 LOGDBG(LY_LDGXPATH, tmp);
158 }
Radek Krejcif13b87b2020-12-01 22:02:17 +0100159#undef MSG_BUFFER_SIZE
Michal Vasko03ff5a72019-09-11 13:49:33 +0200160}
161
162#ifndef NDEBUG
163
164/**
165 * @brief Print XPath set content to debug output.
166 *
167 * @param[in] set Set to print.
168 */
169static void
170print_set_debug(struct lyxp_set *set)
171{
172 uint32_t i;
173 char *str;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200174 struct lyxp_set_node *item;
175 struct lyxp_set_scnode *sitem;
176
Radek Krejci52b6d512020-10-12 12:33:17 +0200177 if (ly_ll < LY_LLDBG) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200178 return;
179 }
180
181 switch (set->type) {
182 case LYXP_SET_NODE_SET:
183 LOGDBG(LY_LDGXPATH, "set NODE SET:");
184 for (i = 0; i < set->used; ++i) {
185 item = &set->val.nodes[i];
186
187 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +0100188 case LYXP_NODE_NONE:
189 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): NONE", i + 1, item->pos);
190 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200191 case LYXP_NODE_ROOT:
192 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT", i + 1, item->pos);
193 break;
194 case LYXP_NODE_ROOT_CONFIG:
195 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT CONFIG", i + 1, item->pos);
196 break;
197 case LYXP_NODE_ELEM:
Michal Vasko9e685082021-01-29 14:49:09 +0100198 if ((item->node->schema->nodetype == LYS_LIST) && (lyd_child(item->node)->schema->nodetype == LYS_LEAF)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200199 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (1st child val: %s)", i + 1, item->pos,
Michal Vasko69730152020-10-09 16:30:07 +0200200 item->node->schema->name, LYD_CANON_VALUE(lyd_child(item->node)));
Michal Vasko9e685082021-01-29 14:49:09 +0100201 } else if (item->node->schema->nodetype == LYS_LEAFLIST) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200202 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (val: %s)", i + 1, item->pos,
Michal Vasko69730152020-10-09 16:30:07 +0200203 item->node->schema->name, LYD_CANON_VALUE(item->node));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200204 } else {
205 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s", i + 1, item->pos, item->node->schema->name);
206 }
207 break;
208 case LYXP_NODE_TEXT:
209 if (item->node->schema->nodetype & LYS_ANYDATA) {
210 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): TEXT <%s>", i + 1, item->pos,
Michal Vasko69730152020-10-09 16:30:07 +0200211 item->node->schema->nodetype == LYS_ANYXML ? "anyxml" : "anydata");
Michal Vasko03ff5a72019-09-11 13:49:33 +0200212 } else {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200213 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 +0200214 }
215 break;
Michal Vasko9f96a052020-03-10 09:41:45 +0100216 case LYXP_NODE_META:
217 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 +0200218 set->val.meta[i].meta->value);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200219 break;
220 }
221 }
222 break;
223
224 case LYXP_SET_SCNODE_SET:
225 LOGDBG(LY_LDGXPATH, "set SCNODE SET:");
226 for (i = 0; i < set->used; ++i) {
227 sitem = &set->val.scnodes[i];
228
229 switch (sitem->type) {
230 case LYXP_NODE_ROOT:
231 LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT", i + 1, sitem->in_ctx);
232 break;
233 case LYXP_NODE_ROOT_CONFIG:
234 LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT CONFIG", i + 1, sitem->in_ctx);
235 break;
236 case LYXP_NODE_ELEM:
237 LOGDBG(LY_LDGXPATH, "\t%d (%u): ELEM %s", i + 1, sitem->in_ctx, sitem->scnode->name);
238 break;
239 default:
240 LOGINT(NULL);
241 break;
242 }
243 }
244 break;
245
Michal Vasko03ff5a72019-09-11 13:49:33 +0200246 case LYXP_SET_BOOLEAN:
247 LOGDBG(LY_LDGXPATH, "set BOOLEAN");
Michal Vasko004d3152020-06-11 19:59:22 +0200248 LOGDBG(LY_LDGXPATH, "\t%s", (set->val.bln ? "true" : "false"));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200249 break;
250
251 case LYXP_SET_STRING:
252 LOGDBG(LY_LDGXPATH, "set STRING");
253 LOGDBG(LY_LDGXPATH, "\t%s", set->val.str);
254 break;
255
256 case LYXP_SET_NUMBER:
257 LOGDBG(LY_LDGXPATH, "set NUMBER");
258
259 if (isnan(set->val.num)) {
260 str = strdup("NaN");
261 } else if ((set->val.num == 0) || (set->val.num == -0.0f)) {
262 str = strdup("0");
263 } else if (isinf(set->val.num) && !signbit(set->val.num)) {
264 str = strdup("Infinity");
265 } else if (isinf(set->val.num) && signbit(set->val.num)) {
266 str = strdup("-Infinity");
267 } else if ((long long)set->val.num == set->val.num) {
268 if (asprintf(&str, "%lld", (long long)set->val.num) == -1) {
269 str = NULL;
270 }
271 } else {
272 if (asprintf(&str, "%03.1Lf", set->val.num) == -1) {
273 str = NULL;
274 }
275 }
276 LY_CHECK_ERR_RET(!str, LOGMEM(NULL), );
277
278 LOGDBG(LY_LDGXPATH, "\t%s", str);
279 free(str);
280 }
281}
282
283#endif
284
285/**
286 * @brief Realloc the string \p str.
287 *
288 * @param[in] ctx libyang context for logging.
289 * @param[in] needed How much free space is required.
290 * @param[in,out] str Pointer to the string to use.
291 * @param[in,out] used Used bytes in \p str.
292 * @param[in,out] size Allocated bytes in \p str.
293 * @return LY_ERR
294 */
295static LY_ERR
Michal Vasko52927e22020-03-16 17:26:14 +0100296cast_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 +0200297{
298 if (*size - *used < needed) {
299 do {
300 if ((UINT16_MAX - *size) < LYXP_STRING_CAST_SIZE_STEP) {
301 LOGERR(ctx, LY_EINVAL, "XPath string length limit (%u) reached.", UINT16_MAX);
302 return LY_EINVAL;
303 }
304 *size += LYXP_STRING_CAST_SIZE_STEP;
305 } while (*size - *used < needed);
306 *str = ly_realloc(*str, *size * sizeof(char));
307 LY_CHECK_ERR_RET(!(*str), LOGMEM(ctx), LY_EMEM);
308 }
309
310 return LY_SUCCESS;
311}
312
313/**
314 * @brief Cast nodes recursively to one string @p str.
315 *
316 * @param[in] node Node to cast.
317 * @param[in] fake_cont Whether to put the data into a "fake" container.
318 * @param[in] root_type Type of the XPath root.
319 * @param[in] indent Current indent.
320 * @param[in,out] str Resulting string.
321 * @param[in,out] used Used bytes in @p str.
322 * @param[in,out] size Allocated bytes in @p str.
323 * @return LY_ERR
324 */
325static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200326cast_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 +0200327 uint16_t *used, uint16_t *size)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200328{
Radek Krejci7f769d72020-07-11 23:13:56 +0200329 char *buf, *line, *ptr = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200330 const char *value_str;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200331 const struct lyd_node *child;
Michal Vasko60ea6352020-06-29 13:39:39 +0200332 struct lyd_node *tree;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200333 struct lyd_node_any *any;
334 LY_ERR rc;
335
336 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) {
337 return LY_SUCCESS;
338 }
339
340 if (fake_cont) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200341 rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200342 LY_CHECK_RET(rc);
343 strcpy(*str + (*used - 1), "\n");
344 ++(*used);
345
346 ++indent;
347 }
348
349 switch (node->schema->nodetype) {
350 case LYS_CONTAINER:
351 case LYS_LIST:
352 case LYS_RPC:
353 case LYS_NOTIF:
Michal Vaskob7be7a82020-08-20 09:09:04 +0200354 rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200355 LY_CHECK_RET(rc);
356 strcpy(*str + (*used - 1), "\n");
357 ++(*used);
358
Radek Krejcia1c1e542020-09-29 16:06:52 +0200359 for (child = lyd_child(node); child; child = child->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200360 rc = cast_string_recursive(child, 0, root_type, indent + 1, str, used, size);
361 LY_CHECK_RET(rc);
362 }
363
364 break;
365
366 case LYS_LEAF:
367 case LYS_LEAFLIST:
Michal Vaskob7be7a82020-08-20 09:09:04 +0200368 value_str = LYD_CANON_VALUE(node);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200369
370 /* print indent */
Michal Vaskob7be7a82020-08-20 09:09:04 +0200371 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 +0200372 memset(*str + (*used - 1), ' ', indent * 2);
373 *used += indent * 2;
374
375 /* print value */
376 if (*used == 1) {
377 sprintf(*str + (*used - 1), "%s", value_str);
378 *used += strlen(value_str);
379 } else {
380 sprintf(*str + (*used - 1), "%s\n", value_str);
381 *used += strlen(value_str) + 1;
382 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200383
384 break;
385
386 case LYS_ANYXML:
387 case LYS_ANYDATA:
388 any = (struct lyd_node_any *)node;
389 if (!(void *)any->value.tree) {
390 /* no content */
391 buf = strdup("");
Michal Vaskob7be7a82020-08-20 09:09:04 +0200392 LY_CHECK_ERR_RET(!buf, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200393 } else {
Radek Krejci241f6b52020-05-21 18:13:49 +0200394 struct ly_out *out;
Radek Krejcia5bba312020-01-09 15:41:20 +0100395
Michal Vasko60ea6352020-06-29 13:39:39 +0200396 if (any->value_type == LYD_ANYDATA_LYB) {
397 /* try to parse it into a data tree */
Michal Vaskob7be7a82020-08-20 09:09:04 +0200398 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 +0200399 /* successfully parsed */
400 free(any->value.mem);
401 any->value.tree = tree;
402 any->value_type = LYD_ANYDATA_DATATREE;
403 }
Radek Krejci7931b192020-06-25 17:05:03 +0200404 /* error is covered by the following switch where LYD_ANYDATA_LYB causes failure */
Michal Vasko60ea6352020-06-29 13:39:39 +0200405 }
406
Michal Vasko03ff5a72019-09-11 13:49:33 +0200407 switch (any->value_type) {
408 case LYD_ANYDATA_STRING:
409 case LYD_ANYDATA_XML:
410 case LYD_ANYDATA_JSON:
411 buf = strdup(any->value.json);
Michal Vaskob7be7a82020-08-20 09:09:04 +0200412 LY_CHECK_ERR_RET(!buf, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200413 break;
414 case LYD_ANYDATA_DATATREE:
Radek Krejci84ce7b12020-06-11 17:28:25 +0200415 LY_CHECK_RET(ly_out_new_memory(&buf, 0, &out));
Michal Vasko3a41dff2020-07-15 14:30:28 +0200416 rc = lyd_print_all(out, any->value.tree, LYD_XML, 0);
Radek Krejci241f6b52020-05-21 18:13:49 +0200417 ly_out_free(out, NULL, 0);
Radek Krejcia5bba312020-01-09 15:41:20 +0100418 LY_CHECK_RET(rc < 0, -rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200419 break;
Michal Vasko60ea6352020-06-29 13:39:39 +0200420 case LYD_ANYDATA_LYB:
Michal Vaskob7be7a82020-08-20 09:09:04 +0200421 LOGERR(LYD_CTX(node), LY_EINVAL, "Cannot convert LYB anydata into string.");
Michal Vasko60ea6352020-06-29 13:39:39 +0200422 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200423 }
424 }
425
426 line = strtok_r(buf, "\n", &ptr);
427 do {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200428 rc = cast_string_realloc(LYD_CTX(node), indent * 2 + strlen(line) + 1, str, used, size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200429 if (rc != LY_SUCCESS) {
430 free(buf);
431 return rc;
432 }
433 memset(*str + (*used - 1), ' ', indent * 2);
434 *used += indent * 2;
435
436 strcpy(*str + (*used - 1), line);
437 *used += strlen(line);
438
439 strcpy(*str + (*used - 1), "\n");
440 *used += 1;
441 } while ((line = strtok_r(NULL, "\n", &ptr)));
442
443 free(buf);
444 break;
445
446 default:
Michal Vaskob7be7a82020-08-20 09:09:04 +0200447 LOGINT_RET(LYD_CTX(node));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200448 }
449
450 if (fake_cont) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200451 rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200452 LY_CHECK_RET(rc);
453 strcpy(*str + (*used - 1), "\n");
454 ++(*used);
455
456 --indent;
457 }
458
459 return LY_SUCCESS;
460}
461
462/**
463 * @brief Cast an element into a string.
464 *
465 * @param[in] node Node to cast.
466 * @param[in] fake_cont Whether to put the data into a "fake" container.
467 * @param[in] root_type Type of the XPath root.
468 * @param[out] str Element cast to dynamically-allocated string.
469 * @return LY_ERR
470 */
471static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200472cast_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 +0200473{
474 uint16_t used, size;
475 LY_ERR rc;
476
477 *str = malloc(LYXP_STRING_CAST_SIZE_START * sizeof(char));
Michal Vaskob7be7a82020-08-20 09:09:04 +0200478 LY_CHECK_ERR_RET(!*str, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200479 (*str)[0] = '\0';
480 used = 1;
481 size = LYXP_STRING_CAST_SIZE_START;
482
483 rc = cast_string_recursive(node, fake_cont, root_type, 0, str, &used, &size);
484 if (rc != LY_SUCCESS) {
485 free(*str);
486 return rc;
487 }
488
489 if (size > used) {
490 *str = ly_realloc(*str, used * sizeof(char));
Michal Vaskob7be7a82020-08-20 09:09:04 +0200491 LY_CHECK_ERR_RET(!*str, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200492 }
493 return LY_SUCCESS;
494}
495
496/**
497 * @brief Cast a LYXP_SET_NODE_SET set into a string.
498 * Context position aware.
499 *
500 * @param[in] set Set to cast.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200501 * @param[out] str Cast dynamically-allocated string.
502 * @return LY_ERR
503 */
504static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100505cast_node_set_to_string(struct lyxp_set *set, char **str)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200506{
Michal Vaskoaa677062021-03-09 13:52:53 +0100507 if (!set->used) {
508 *str = strdup("");
509 if (!*str) {
510 LOGMEM_RET(set->ctx);
511 }
512 return LY_SUCCESS;
513 }
514
Michal Vasko03ff5a72019-09-11 13:49:33 +0200515 switch (set->val.nodes[0].type) {
Michal Vasko2caefc12019-11-14 16:07:56 +0100516 case LYXP_NODE_NONE:
517 /* invalid */
518 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200519 case LYXP_NODE_ROOT:
520 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100521 return cast_string_elem(set->val.nodes[0].node, 1, set->root_type, str);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200522 case LYXP_NODE_ELEM:
523 case LYXP_NODE_TEXT:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100524 return cast_string_elem(set->val.nodes[0].node, 0, set->root_type, str);
Michal Vasko9f96a052020-03-10 09:41:45 +0100525 case LYXP_NODE_META:
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200526 *str = strdup(set->val.meta[0].meta->value.canonical);
527 if (!*str) {
528 LOGMEM_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200529 }
530 return LY_SUCCESS;
531 }
532
533 LOGINT_RET(set->ctx);
534}
535
536/**
537 * @brief Cast a string into an XPath number.
538 *
539 * @param[in] str String to use.
540 * @return Cast number.
541 */
542static long double
543cast_string_to_number(const char *str)
544{
545 long double num;
546 char *ptr;
547
548 errno = 0;
549 num = strtold(str, &ptr);
550 if (errno || *ptr) {
551 num = NAN;
552 }
553 return num;
554}
555
556/**
557 * @brief Callback for checking value equality.
558 *
Michal Vasko62524a92021-02-26 10:08:50 +0100559 * Implementation of ::lyht_value_equal_cb.
Radek Krejci857189e2020-09-01 13:26:36 +0200560 *
Michal Vasko03ff5a72019-09-11 13:49:33 +0200561 * @param[in] val1_p First value.
562 * @param[in] val2_p Second value.
563 * @param[in] mod Whether hash table is being modified.
564 * @param[in] cb_data Callback data.
Radek Krejci857189e2020-09-01 13:26:36 +0200565 * @return Boolean value whether values are equal or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200566 */
Radek Krejci857189e2020-09-01 13:26:36 +0200567static ly_bool
568set_values_equal_cb(void *val1_p, void *val2_p, ly_bool UNUSED(mod), void *UNUSED(cb_data))
Michal Vasko03ff5a72019-09-11 13:49:33 +0200569{
570 struct lyxp_set_hash_node *val1, *val2;
571
572 val1 = (struct lyxp_set_hash_node *)val1_p;
573 val2 = (struct lyxp_set_hash_node *)val2_p;
574
575 if ((val1->node == val2->node) && (val1->type == val2->type)) {
576 return 1;
577 }
578
579 return 0;
580}
581
582/**
583 * @brief Insert node and its hash into set.
584 *
585 * @param[in] set et to insert to.
586 * @param[in] node Node with hash.
587 * @param[in] type Node type.
588 */
589static void
590set_insert_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type)
591{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200592 LY_ERR r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200593 uint32_t i, hash;
594 struct lyxp_set_hash_node hnode;
595
596 if (!set->ht && (set->used >= LYD_HT_MIN_ITEMS)) {
597 /* create hash table and add all the nodes */
598 set->ht = lyht_new(1, sizeof(struct lyxp_set_hash_node), set_values_equal_cb, NULL, 1);
599 for (i = 0; i < set->used; ++i) {
600 hnode.node = set->val.nodes[i].node;
601 hnode.type = set->val.nodes[i].type;
602
603 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
604 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
605 hash = dict_hash_multi(hash, NULL, 0);
606
607 r = lyht_insert(set->ht, &hnode, hash, NULL);
608 assert(!r);
609 (void)r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200610
Michal Vasko9d6befd2019-12-11 14:56:56 +0100611 if (hnode.node == node) {
612 /* it was just added, do not add it twice */
613 node = NULL;
614 }
615 }
616 }
617
618 if (set->ht && node) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200619 /* add the new node into hash table */
620 hnode.node = node;
621 hnode.type = type;
622
623 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
624 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
625 hash = dict_hash_multi(hash, NULL, 0);
626
627 r = lyht_insert(set->ht, &hnode, hash, NULL);
628 assert(!r);
629 (void)r;
630 }
631}
632
633/**
634 * @brief Remove node and its hash from set.
635 *
636 * @param[in] set Set to remove from.
637 * @param[in] node Node to remove.
638 * @param[in] type Node type.
639 */
640static void
641set_remove_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type)
642{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200643 LY_ERR r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200644 struct lyxp_set_hash_node hnode;
645 uint32_t hash;
646
647 if (set->ht) {
648 hnode.node = node;
649 hnode.type = type;
650
651 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
652 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
653 hash = dict_hash_multi(hash, NULL, 0);
654
655 r = lyht_remove(set->ht, &hnode, hash);
656 assert(!r);
657 (void)r;
658
659 if (!set->ht->used) {
660 lyht_free(set->ht);
661 set->ht = NULL;
662 }
663 }
664}
665
666/**
667 * @brief Check whether node is in set based on its hash.
668 *
669 * @param[in] set Set to search in.
670 * @param[in] node Node to search for.
671 * @param[in] type Node type.
672 * @param[in] skip_idx Index in @p set to skip.
673 * @return LY_ERR
674 */
675static LY_ERR
676set_dup_node_hash_check(const struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type, int skip_idx)
677{
678 struct lyxp_set_hash_node hnode, *match_p;
679 uint32_t hash;
680
681 hnode.node = node;
682 hnode.type = type;
683
684 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
685 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
686 hash = dict_hash_multi(hash, NULL, 0);
687
688 if (!lyht_find(set->ht, &hnode, hash, (void **)&match_p)) {
689 if ((skip_idx > -1) && (set->val.nodes[skip_idx].node == match_p->node) && (set->val.nodes[skip_idx].type == match_p->type)) {
690 /* we found it on the index that should be skipped, find another */
691 hnode = *match_p;
692 if (lyht_find_next(set->ht, &hnode, hash, (void **)&match_p)) {
693 /* none other found */
694 return LY_SUCCESS;
695 }
696 }
697
698 return LY_EEXIST;
699 }
700
701 /* not found */
702 return LY_SUCCESS;
703}
704
Michal Vaskod3678892020-05-21 10:06:58 +0200705void
706lyxp_set_free_content(struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200707{
708 if (!set) {
709 return;
710 }
711
712 if (set->type == LYXP_SET_NODE_SET) {
713 free(set->val.nodes);
714 lyht_free(set->ht);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200715 } else if (set->type == LYXP_SET_SCNODE_SET) {
716 free(set->val.scnodes);
Michal Vaskod3678892020-05-21 10:06:58 +0200717 lyht_free(set->ht);
718 } else {
719 if (set->type == LYXP_SET_STRING) {
720 free(set->val.str);
721 }
722 set->type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200723 }
Michal Vaskod3678892020-05-21 10:06:58 +0200724
725 set->val.nodes = NULL;
726 set->used = 0;
727 set->size = 0;
728 set->ht = NULL;
729 set->ctx_pos = 0;
730 set->ctx_pos = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200731}
732
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100733/**
734 * @brief Free dynamically-allocated set.
735 *
736 * @param[in] set Set to free.
737 */
738static void
Michal Vasko03ff5a72019-09-11 13:49:33 +0200739lyxp_set_free(struct lyxp_set *set)
740{
741 if (!set) {
742 return;
743 }
744
Michal Vaskod3678892020-05-21 10:06:58 +0200745 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200746 free(set);
747}
748
749/**
750 * @brief Initialize set context.
751 *
752 * @param[in] new Set to initialize.
753 * @param[in] set Arbitrary initialized set.
754 */
755static void
Michal Vasko4c7763f2020-07-27 17:40:37 +0200756set_init(struct lyxp_set *new, const struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200757{
758 memset(new, 0, sizeof *new);
Michal Vasko02a77382019-09-12 11:47:35 +0200759 if (set) {
760 new->ctx = set->ctx;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200761 new->cur_node = set->cur_node;
Michal Vasko588112f2019-12-10 14:51:53 +0100762 new->root_type = set->root_type;
Michal Vasko6b26e742020-07-17 15:02:10 +0200763 new->context_op = set->context_op;
Michal Vaskof03ed032020-03-04 13:31:44 +0100764 new->tree = set->tree;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200765 new->cur_mod = set->cur_mod;
Michal Vasko02a77382019-09-12 11:47:35 +0200766 new->format = set->format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +0200767 new->prefix_data = set->prefix_data;
Michal Vasko02a77382019-09-12 11:47:35 +0200768 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200769}
770
771/**
772 * @brief Create a deep copy of a set.
773 *
774 * @param[in] set Set to copy.
775 * @return Copy of @p set.
776 */
777static struct lyxp_set *
778set_copy(struct lyxp_set *set)
779{
780 struct lyxp_set *ret;
Michal Vasko1fdd8fa2021-01-08 09:21:45 +0100781 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200782
783 if (!set) {
784 return NULL;
785 }
786
787 ret = malloc(sizeof *ret);
788 LY_CHECK_ERR_RET(!ret, LOGMEM(set->ctx), NULL);
789 set_init(ret, set);
790
791 if (set->type == LYXP_SET_SCNODE_SET) {
792 ret->type = set->type;
793
794 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100795 if ((set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) ||
796 (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +0200797 uint32_t idx;
798 LY_CHECK_ERR_RET(lyxp_set_scnode_insert_node(ret, set->val.scnodes[i].scnode, set->val.scnodes[i].type, &idx),
799 lyxp_set_free(ret), NULL);
Michal Vasko3f27c522020-01-06 08:37:49 +0100800 /* coverity seems to think scnodes can be NULL */
Radek Krejciaa6b53f2020-08-27 15:19:03 +0200801 if (!ret->val.scnodes) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200802 lyxp_set_free(ret);
803 return NULL;
804 }
Michal Vaskoba716542019-12-16 10:01:58 +0100805 ret->val.scnodes[idx].in_ctx = set->val.scnodes[i].in_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200806 }
807 }
808 } else if (set->type == LYXP_SET_NODE_SET) {
809 ret->type = set->type;
810 ret->val.nodes = malloc(set->used * sizeof *ret->val.nodes);
811 LY_CHECK_ERR_RET(!ret->val.nodes, LOGMEM(set->ctx); free(ret), NULL);
812 memcpy(ret->val.nodes, set->val.nodes, set->used * sizeof *ret->val.nodes);
813
814 ret->used = ret->size = set->used;
815 ret->ctx_pos = set->ctx_pos;
816 ret->ctx_size = set->ctx_size;
Michal Vasko4a04e542021-02-01 08:58:30 +0100817 if (set->ht) {
818 ret->ht = lyht_dup(set->ht);
819 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200820 } else {
Radek Krejci0f969882020-08-21 16:56:47 +0200821 memcpy(ret, set, sizeof *ret);
822 if (set->type == LYXP_SET_STRING) {
823 ret->val.str = strdup(set->val.str);
824 LY_CHECK_ERR_RET(!ret->val.str, LOGMEM(set->ctx); free(ret), NULL);
825 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200826 }
827
828 return ret;
829}
830
831/**
832 * @brief Fill XPath set with a string. Any current data are disposed of.
833 *
834 * @param[in] set Set to fill.
835 * @param[in] string String to fill into \p set.
836 * @param[in] str_len Length of \p string. 0 is a valid value!
837 */
838static void
839set_fill_string(struct lyxp_set *set, const char *string, uint16_t str_len)
840{
Michal Vaskod3678892020-05-21 10:06:58 +0200841 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200842
843 set->type = LYXP_SET_STRING;
844 if ((str_len == 0) && (string[0] != '\0')) {
845 string = "";
846 }
847 set->val.str = strndup(string, str_len);
848}
849
850/**
851 * @brief Fill XPath set with a number. Any current data are disposed of.
852 *
853 * @param[in] set Set to fill.
854 * @param[in] number Number to fill into \p set.
855 */
856static void
857set_fill_number(struct lyxp_set *set, long double number)
858{
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_NUMBER;
862 set->val.num = number;
863}
864
865/**
866 * @brief Fill XPath set with a boolean. Any current data are disposed of.
867 *
868 * @param[in] set Set to fill.
869 * @param[in] boolean Boolean to fill into \p set.
870 */
871static void
Radek Krejci857189e2020-09-01 13:26:36 +0200872set_fill_boolean(struct lyxp_set *set, ly_bool boolean)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200873{
Michal Vaskod3678892020-05-21 10:06:58 +0200874 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200875
876 set->type = LYXP_SET_BOOLEAN;
Michal Vasko004d3152020-06-11 19:59:22 +0200877 set->val.bln = boolean;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200878}
879
880/**
881 * @brief Fill XPath set with the value from another set (deep assign).
882 * Any current data are disposed of.
883 *
884 * @param[in] trg Set to fill.
885 * @param[in] src Source set to copy into \p trg.
886 */
887static void
Michal Vasko4c7763f2020-07-27 17:40:37 +0200888set_fill_set(struct lyxp_set *trg, const struct lyxp_set *src)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200889{
890 if (!trg || !src) {
891 return;
892 }
893
894 if (trg->type == LYXP_SET_NODE_SET) {
895 free(trg->val.nodes);
896 } else if (trg->type == LYXP_SET_STRING) {
897 free(trg->val.str);
898 }
899 set_init(trg, src);
900
901 if (src->type == LYXP_SET_SCNODE_SET) {
902 trg->type = LYXP_SET_SCNODE_SET;
903 trg->used = src->used;
904 trg->size = src->used;
905
906 trg->val.scnodes = ly_realloc(trg->val.scnodes, trg->size * sizeof *trg->val.scnodes);
907 LY_CHECK_ERR_RET(!trg->val.scnodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), );
908 memcpy(trg->val.scnodes, src->val.scnodes, src->used * sizeof *src->val.scnodes);
909 } else if (src->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +0200910 set_fill_boolean(trg, src->val.bln);
Michal Vasko44f3d2c2020-08-24 09:49:38 +0200911 } else if (src->type == LYXP_SET_NUMBER) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200912 set_fill_number(trg, src->val.num);
913 } else if (src->type == LYXP_SET_STRING) {
914 set_fill_string(trg, src->val.str, strlen(src->val.str));
915 } else {
916 if (trg->type == LYXP_SET_NODE_SET) {
917 free(trg->val.nodes);
918 } else if (trg->type == LYXP_SET_STRING) {
919 free(trg->val.str);
920 }
921
Michal Vaskod3678892020-05-21 10:06:58 +0200922 assert(src->type == LYXP_SET_NODE_SET);
923
924 trg->type = LYXP_SET_NODE_SET;
925 trg->used = src->used;
926 trg->size = src->used;
927 trg->ctx_pos = src->ctx_pos;
928 trg->ctx_size = src->ctx_size;
929
930 trg->val.nodes = malloc(trg->used * sizeof *trg->val.nodes);
931 LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), );
932 memcpy(trg->val.nodes, src->val.nodes, src->used * sizeof *src->val.nodes);
933 if (src->ht) {
934 trg->ht = lyht_dup(src->ht);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200935 } else {
Michal Vaskod3678892020-05-21 10:06:58 +0200936 trg->ht = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200937 }
938 }
939}
940
941/**
942 * @brief Clear context of all schema nodes.
943 *
944 * @param[in] set Set to clear.
945 */
946static void
947set_scnode_clear_ctx(struct lyxp_set *set)
948{
949 uint32_t i;
950
951 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100952 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
953 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM;
954 } else if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START) {
955 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200956 }
957 }
958}
959
960/**
961 * @brief Remove a node from a set. Removing last node changes
962 * set into LYXP_SET_EMPTY. Context position aware.
963 *
964 * @param[in] set Set to use.
965 * @param[in] idx Index from @p set of the node to be removed.
966 */
967static void
968set_remove_node(struct lyxp_set *set, uint32_t idx)
969{
970 assert(set && (set->type == LYXP_SET_NODE_SET));
971 assert(idx < set->used);
972
973 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
974
975 --set->used;
976 if (set->used) {
977 memmove(&set->val.nodes[idx], &set->val.nodes[idx + 1],
978 (set->used - idx) * sizeof *set->val.nodes);
979 } else {
Michal Vaskod3678892020-05-21 10:06:58 +0200980 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200981 }
982}
983
984/**
Michal Vasko2caefc12019-11-14 16:07:56 +0100985 * @brief Remove a node from a set by setting its type to LYXP_NODE_NONE.
Michal Vasko57eab132019-09-24 11:46:26 +0200986 *
987 * @param[in] set Set to use.
988 * @param[in] idx Index from @p set of the node to be removed.
989 */
990static void
Michal Vasko2caefc12019-11-14 16:07:56 +0100991set_remove_node_none(struct lyxp_set *set, uint32_t idx)
Michal Vasko57eab132019-09-24 11:46:26 +0200992{
993 assert(set && (set->type == LYXP_SET_NODE_SET));
994 assert(idx < set->used);
995
Michal Vasko2caefc12019-11-14 16:07:56 +0100996 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
997 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
998 }
999 set->val.nodes[idx].type = LYXP_NODE_NONE;
Michal Vasko57eab132019-09-24 11:46:26 +02001000}
1001
1002/**
Michal Vasko2caefc12019-11-14 16:07:56 +01001003 * @brief Remove all LYXP_NODE_NONE nodes from a set. Removing last node changes
Michal Vasko57eab132019-09-24 11:46:26 +02001004 * set into LYXP_SET_EMPTY. Context position aware.
1005 *
1006 * @param[in] set Set to consolidate.
1007 */
1008static void
Michal Vasko2caefc12019-11-14 16:07:56 +01001009set_remove_nodes_none(struct lyxp_set *set)
Michal Vasko57eab132019-09-24 11:46:26 +02001010{
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01001011 uint32_t i, orig_used, end = 0;
1012 int64_t start;
Michal Vasko57eab132019-09-24 11:46:26 +02001013
Michal Vaskod3678892020-05-21 10:06:58 +02001014 assert(set);
Michal Vasko57eab132019-09-24 11:46:26 +02001015
1016 orig_used = set->used;
1017 set->used = 0;
Michal Vaskod989ba02020-08-24 10:59:24 +02001018 for (i = 0; i < orig_used; ) {
Michal Vasko57eab132019-09-24 11:46:26 +02001019 start = -1;
1020 do {
Michal Vasko2caefc12019-11-14 16:07:56 +01001021 if ((set->val.nodes[i].type != LYXP_NODE_NONE) && (start == -1)) {
Michal Vasko57eab132019-09-24 11:46:26 +02001022 start = i;
Michal Vasko2caefc12019-11-14 16:07:56 +01001023 } else if ((start > -1) && (set->val.nodes[i].type == LYXP_NODE_NONE)) {
Michal Vasko57eab132019-09-24 11:46:26 +02001024 end = i;
1025 ++i;
1026 break;
1027 }
1028
1029 ++i;
1030 if (i == orig_used) {
1031 end = i;
1032 }
1033 } while (i < orig_used);
1034
1035 if (start > -1) {
1036 /* move the whole chunk of valid nodes together */
1037 if (set->used != (unsigned)start) {
1038 memmove(&set->val.nodes[set->used], &set->val.nodes[start], (end - start) * sizeof *set->val.nodes);
1039 }
1040 set->used += end - start;
1041 }
1042 }
Michal Vasko57eab132019-09-24 11:46:26 +02001043}
1044
1045/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02001046 * @brief Check for duplicates in a node set.
1047 *
1048 * @param[in] set Set to check.
1049 * @param[in] node Node to look for in @p set.
1050 * @param[in] node_type Type of @p node.
1051 * @param[in] skip_idx Index from @p set to skip.
1052 * @return LY_ERR
1053 */
1054static LY_ERR
1055set_dup_node_check(const struct lyxp_set *set, const struct lyd_node *node, enum lyxp_node_type node_type, int skip_idx)
1056{
1057 uint32_t i;
1058
Michal Vasko2caefc12019-11-14 16:07:56 +01001059 if (set->ht && node) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001060 return set_dup_node_hash_check(set, (struct lyd_node *)node, node_type, skip_idx);
1061 }
1062
1063 for (i = 0; i < set->used; ++i) {
1064 if ((skip_idx > -1) && (i == (unsigned)skip_idx)) {
1065 continue;
1066 }
1067
1068 if ((set->val.nodes[i].node == node) && (set->val.nodes[i].type == node_type)) {
1069 return LY_EEXIST;
1070 }
1071 }
1072
1073 return LY_SUCCESS;
1074}
1075
Radek Krejci857189e2020-09-01 13:26:36 +02001076ly_bool
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001077lyxp_set_scnode_contains(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type, int skip_idx,
1078 uint32_t *index_p)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001079{
1080 uint32_t i;
1081
1082 for (i = 0; i < set->used; ++i) {
1083 if ((skip_idx > -1) && (i == (unsigned)skip_idx)) {
1084 continue;
1085 }
1086
1087 if ((set->val.scnodes[i].scnode == node) && (set->val.scnodes[i].type == node_type)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001088 if (index_p) {
1089 *index_p = i;
1090 }
1091 return 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001092 }
1093 }
1094
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001095 return 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001096}
1097
Michal Vaskoecd62de2019-11-13 12:35:11 +01001098void
1099lyxp_set_scnode_merge(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001100{
1101 uint32_t orig_used, i, j;
1102
Michal Vaskod3678892020-05-21 10:06:58 +02001103 assert((set1->type == LYXP_SET_SCNODE_SET) && (set2->type == LYXP_SET_SCNODE_SET));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001104
Michal Vaskod3678892020-05-21 10:06:58 +02001105 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001106 return;
1107 }
1108
Michal Vaskod3678892020-05-21 10:06:58 +02001109 if (!set1->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001110 memcpy(set1, set2, sizeof *set1);
1111 return;
1112 }
1113
1114 if (set1->used + set2->used > set1->size) {
1115 set1->size = set1->used + set2->used;
1116 set1->val.scnodes = ly_realloc(set1->val.scnodes, set1->size * sizeof *set1->val.scnodes);
1117 LY_CHECK_ERR_RET(!set1->val.scnodes, LOGMEM(set1->ctx), );
1118 }
1119
1120 orig_used = set1->used;
1121
1122 for (i = 0; i < set2->used; ++i) {
1123 for (j = 0; j < orig_used; ++j) {
1124 /* detect duplicities */
1125 if (set1->val.scnodes[j].scnode == set2->val.scnodes[i].scnode) {
1126 break;
1127 }
1128 }
1129
Michal Vasko0587bce2020-12-10 12:19:21 +01001130 if (j < orig_used) {
1131 /* node is there, but update its status if needed */
1132 if (set1->val.scnodes[j].in_ctx == LYXP_SET_SCNODE_START_USED) {
1133 set1->val.scnodes[j].in_ctx = set2->val.scnodes[i].in_ctx;
1134 }
1135 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001136 memcpy(&set1->val.scnodes[set1->used], &set2->val.scnodes[i], sizeof *set2->val.scnodes);
1137 ++set1->used;
1138 }
1139 }
1140
Michal Vaskod3678892020-05-21 10:06:58 +02001141 lyxp_set_free_content(set2);
1142 set2->type = LYXP_SET_SCNODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001143}
1144
1145/**
1146 * @brief Insert a node into a set. Context position aware.
1147 *
1148 * @param[in] set Set to use.
1149 * @param[in] node Node to insert to @p set.
1150 * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting.
1151 * @param[in] node_type Node type of @p node.
1152 * @param[in] idx Index in @p set to insert into.
1153 */
1154static void
1155set_insert_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx)
1156{
Michal Vaskod3678892020-05-21 10:06:58 +02001157 assert(set && (set->type == LYXP_SET_NODE_SET));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001158
Michal Vaskod3678892020-05-21 10:06:58 +02001159 if (!set->size) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001160 /* first item */
1161 if (idx) {
1162 /* no real harm done, but it is a bug */
1163 LOGINT(set->ctx);
1164 idx = 0;
1165 }
1166 set->val.nodes = malloc(LYXP_SET_SIZE_START * sizeof *set->val.nodes);
1167 LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), );
1168 set->type = LYXP_SET_NODE_SET;
1169 set->used = 0;
1170 set->size = LYXP_SET_SIZE_START;
1171 set->ctx_pos = 1;
1172 set->ctx_size = 1;
1173 set->ht = NULL;
1174 } else {
1175 /* not an empty set */
1176 if (set->used == set->size) {
1177
1178 /* set is full */
1179 set->val.nodes = ly_realloc(set->val.nodes, (set->size + LYXP_SET_SIZE_STEP) * sizeof *set->val.nodes);
1180 LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), );
1181 set->size += LYXP_SET_SIZE_STEP;
1182 }
1183
1184 if (idx > set->used) {
1185 LOGINT(set->ctx);
1186 idx = set->used;
1187 }
1188
1189 /* make space for the new node */
1190 if (idx < set->used) {
1191 memmove(&set->val.nodes[idx + 1], &set->val.nodes[idx], (set->used - idx) * sizeof *set->val.nodes);
1192 }
1193 }
1194
1195 /* finally assign the value */
1196 set->val.nodes[idx].node = (struct lyd_node *)node;
1197 set->val.nodes[idx].type = node_type;
1198 set->val.nodes[idx].pos = pos;
1199 ++set->used;
1200
Michal Vasko2caefc12019-11-14 16:07:56 +01001201 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1202 set_insert_node_hash(set, (struct lyd_node *)node, node_type);
1203 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001204}
1205
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001206LY_ERR
1207lyxp_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 +02001208{
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001209 uint32_t index;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001210
1211 assert(set->type == LYXP_SET_SCNODE_SET);
1212
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001213 if (lyxp_set_scnode_contains(set, node, node_type, -1, &index)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001214 set->val.scnodes[index].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001215 } else {
1216 if (set->used == set->size) {
1217 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 +02001218 LY_CHECK_ERR_RET(!set->val.scnodes, LOGMEM(set->ctx), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001219 set->size += LYXP_SET_SIZE_STEP;
1220 }
1221
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001222 index = set->used;
1223 set->val.scnodes[index].scnode = (struct lysc_node *)node;
1224 set->val.scnodes[index].type = node_type;
Radek Krejcif13b87b2020-12-01 22:02:17 +01001225 set->val.scnodes[index].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001226 ++set->used;
1227 }
1228
Radek Krejciaa6b53f2020-08-27 15:19:03 +02001229 if (index_p) {
1230 *index_p = index;
1231 }
1232
1233 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001234}
1235
1236/**
1237 * @brief Replace a node in a set with another. Context position aware.
1238 *
1239 * @param[in] set Set to use.
1240 * @param[in] node Node to insert to @p set.
1241 * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting.
1242 * @param[in] node_type Node type of @p node.
1243 * @param[in] idx Index in @p set of the node to replace.
1244 */
1245static void
1246set_replace_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx)
1247{
1248 assert(set && (idx < set->used));
1249
Michal Vasko2caefc12019-11-14 16:07:56 +01001250 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1251 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1252 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001253 set->val.nodes[idx].node = (struct lyd_node *)node;
1254 set->val.nodes[idx].type = node_type;
1255 set->val.nodes[idx].pos = pos;
Michal Vasko2caefc12019-11-14 16:07:56 +01001256 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1257 set_insert_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1258 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001259}
1260
1261/**
1262 * @brief Set all nodes with ctx 1 to a new unique context value.
1263 *
1264 * @param[in] set Set to modify.
1265 * @return New context value.
1266 */
Michal Vasko5c4e5892019-11-14 12:31:38 +01001267static int32_t
Michal Vasko03ff5a72019-09-11 13:49:33 +02001268set_scnode_new_in_ctx(struct lyxp_set *set)
1269{
Michal Vasko5c4e5892019-11-14 12:31:38 +01001270 uint32_t i;
1271 int32_t ret_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001272
1273 assert(set->type == LYXP_SET_SCNODE_SET);
1274
Radek Krejcif13b87b2020-12-01 22:02:17 +01001275 ret_ctx = LYXP_SET_SCNODE_ATOM_PRED_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001276retry:
1277 for (i = 0; i < set->used; ++i) {
1278 if (set->val.scnodes[i].in_ctx >= ret_ctx) {
1279 ret_ctx = set->val.scnodes[i].in_ctx + 1;
1280 goto retry;
1281 }
1282 }
1283 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001284 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001285 set->val.scnodes[i].in_ctx = ret_ctx;
1286 }
1287 }
1288
1289 return ret_ctx;
1290}
1291
1292/**
1293 * @brief Get unique @p node position in the data.
1294 *
1295 * @param[in] node Node to find.
1296 * @param[in] node_type Node type of @p node.
1297 * @param[in] root Root node.
1298 * @param[in] root_type Type of the XPath @p root node.
1299 * @param[in] prev Node that we think is before @p node in DFS from @p root. Can optionally
1300 * be used to increase efficiency and start the DFS from this node.
1301 * @param[in] prev_pos Node @p prev position. Optional, but must be set if @p prev is set.
1302 * @return Node position.
1303 */
1304static uint32_t
1305get_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 +02001306 enum lyxp_node_type root_type, const struct lyd_node **prev, uint32_t *prev_pos)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001307{
Michal Vasko56daf732020-08-10 10:57:18 +02001308 const struct lyd_node *elem = NULL, *top_sibling;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001309 uint32_t pos = 1;
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001310 ly_bool found = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001311
1312 assert(prev && prev_pos && !root->prev->next);
1313
1314 if ((node_type == LYXP_NODE_ROOT) || (node_type == LYXP_NODE_ROOT_CONFIG)) {
1315 return 0;
1316 }
1317
1318 if (*prev) {
1319 /* start from the previous element instead from the root */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001320 pos = *prev_pos;
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001321 for (top_sibling = *prev; top_sibling->parent; top_sibling = lyd_parent(top_sibling)) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001322 goto dfs_search;
1323 }
1324
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001325 LY_LIST_FOR(root, top_sibling) {
Michal Vasko56daf732020-08-10 10:57:18 +02001326 LYD_TREE_DFS_BEGIN(top_sibling, elem) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001327dfs_search:
Michal Vasko56daf732020-08-10 10:57:18 +02001328 if (*prev && !elem) {
1329 /* resume previous DFS */
1330 elem = LYD_TREE_DFS_next = (struct lyd_node *)*prev;
1331 LYD_TREE_DFS_continue = 0;
1332 }
1333
Michal Vasko03ff5a72019-09-11 13:49:33 +02001334 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (elem->schema->flags & LYS_CONFIG_R)) {
Michal Vasko56daf732020-08-10 10:57:18 +02001335 /* skip */
1336 LYD_TREE_DFS_continue = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001337 } else {
Michal Vasko56daf732020-08-10 10:57:18 +02001338 if (elem == node) {
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001339 found = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001340 break;
1341 }
Michal Vasko56daf732020-08-10 10:57:18 +02001342 ++pos;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001343 }
Michal Vasko56daf732020-08-10 10:57:18 +02001344
1345 LYD_TREE_DFS_END(top_sibling, elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001346 }
1347
1348 /* node found */
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001349 if (found) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001350 break;
1351 }
1352 }
1353
Michal Vaskofb6c9dd2020-11-18 18:18:47 +01001354 if (!found) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001355 if (!(*prev)) {
1356 /* we went from root and failed to find it, cannot be */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001357 LOGINT(LYD_CTX(node));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001358 return 0;
1359 } else {
Michal Vasko56daf732020-08-10 10:57:18 +02001360 /* start the search again from the beginning */
1361 *prev = root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001362
Michal Vasko56daf732020-08-10 10:57:18 +02001363 top_sibling = root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001364 pos = 1;
1365 goto dfs_search;
1366 }
1367 }
1368
1369 /* remember the last found node for next time */
1370 *prev = node;
1371 *prev_pos = pos;
1372
1373 return pos;
1374}
1375
1376/**
1377 * @brief Assign (fill) missing node positions.
1378 *
1379 * @param[in] set Set to fill positions in.
1380 * @param[in] root Context root node.
1381 * @param[in] root_type Context root type.
1382 * @return LY_ERR
1383 */
1384static LY_ERR
1385set_assign_pos(struct lyxp_set *set, const struct lyd_node *root, enum lyxp_node_type root_type)
1386{
1387 const struct lyd_node *prev = NULL, *tmp_node;
1388 uint32_t i, tmp_pos = 0;
1389
1390 for (i = 0; i < set->used; ++i) {
1391 if (!set->val.nodes[i].pos) {
1392 tmp_node = NULL;
1393 switch (set->val.nodes[i].type) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001394 case LYXP_NODE_META:
1395 tmp_node = set->val.meta[i].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001396 if (!tmp_node) {
1397 LOGINT_RET(root->schema->module->ctx);
1398 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01001399 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001400 case LYXP_NODE_ELEM:
1401 case LYXP_NODE_TEXT:
1402 if (!tmp_node) {
1403 tmp_node = set->val.nodes[i].node;
1404 }
1405 set->val.nodes[i].pos = get_node_pos(tmp_node, set->val.nodes[i].type, root, root_type, &prev, &tmp_pos);
1406 break;
1407 default:
1408 /* all roots have position 0 */
1409 break;
1410 }
1411 }
1412 }
1413
1414 return LY_SUCCESS;
1415}
1416
1417/**
Michal Vasko9f96a052020-03-10 09:41:45 +01001418 * @brief Get unique @p meta position in the parent metadata.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001419 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001420 * @param[in] meta Metadata to use.
1421 * @return Metadata position.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001422 */
1423static uint16_t
Michal Vasko9f96a052020-03-10 09:41:45 +01001424get_meta_pos(struct lyd_meta *meta)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001425{
1426 uint16_t pos = 0;
Michal Vasko9f96a052020-03-10 09:41:45 +01001427 struct lyd_meta *meta2;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001428
Michal Vasko9f96a052020-03-10 09:41:45 +01001429 for (meta2 = meta->parent->meta; meta2 && (meta2 != meta); meta2 = meta2->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001430 ++pos;
1431 }
1432
Michal Vasko9f96a052020-03-10 09:41:45 +01001433 assert(meta2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001434 return pos;
1435}
1436
1437/**
1438 * @brief Compare 2 nodes in respect to XPath document order.
1439 *
1440 * @param[in] item1 1st node.
1441 * @param[in] item2 2nd node.
1442 * @return If 1st > 2nd returns 1, 1st == 2nd returns 0, and 1st < 2nd returns -1.
1443 */
1444static int
1445set_sort_compare(struct lyxp_set_node *item1, struct lyxp_set_node *item2)
1446{
Michal Vasko9f96a052020-03-10 09:41:45 +01001447 uint32_t meta_pos1 = 0, meta_pos2 = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001448
1449 if (item1->pos < item2->pos) {
1450 return -1;
1451 }
1452
1453 if (item1->pos > item2->pos) {
1454 return 1;
1455 }
1456
1457 /* node positions are equal, the fun case */
1458
1459 /* 1st ELEM - == - 2nd TEXT, 1st TEXT - == - 2nd ELEM */
1460 /* special case since text nodes are actually saved as their parents */
1461 if ((item1->node == item2->node) && (item1->type != item2->type)) {
1462 if (item1->type == LYXP_NODE_ELEM) {
1463 assert(item2->type == LYXP_NODE_TEXT);
1464 return -1;
1465 } else {
1466 assert((item1->type == LYXP_NODE_TEXT) && (item2->type == LYXP_NODE_ELEM));
1467 return 1;
1468 }
1469 }
1470
Michal Vasko9f96a052020-03-10 09:41:45 +01001471 /* we need meta positions now */
1472 if (item1->type == LYXP_NODE_META) {
1473 meta_pos1 = get_meta_pos((struct lyd_meta *)item1->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001474 }
Michal Vasko9f96a052020-03-10 09:41:45 +01001475 if (item2->type == LYXP_NODE_META) {
1476 meta_pos2 = get_meta_pos((struct lyd_meta *)item2->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001477 }
1478
Michal Vasko9f96a052020-03-10 09:41:45 +01001479 /* 1st ROOT - 2nd ROOT, 1st ELEM - 2nd ELEM, 1st TEXT - 2nd TEXT, 1st META - =pos= - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001480 /* check for duplicates */
1481 if (item1->node == item2->node) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001482 assert((item1->type == item2->type) && ((item1->type != LYXP_NODE_META) || (meta_pos1 == meta_pos2)));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001483 return 0;
1484 }
1485
Michal Vasko9f96a052020-03-10 09:41:45 +01001486 /* 1st ELEM - 2nd TEXT, 1st ELEM - any pos - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001487 /* elem is always first, 2nd node is after it */
1488 if (item1->type == LYXP_NODE_ELEM) {
1489 assert(item2->type != LYXP_NODE_ELEM);
1490 return -1;
1491 }
1492
Michal Vasko9f96a052020-03-10 09:41:45 +01001493 /* 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 +02001494 /* 2nd is before 1st */
Michal Vasko69730152020-10-09 16:30:07 +02001495 if (((item1->type == LYXP_NODE_TEXT) &&
1496 ((item2->type == LYXP_NODE_ELEM) || (item2->type == LYXP_NODE_META))) ||
1497 ((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_ELEM)) ||
1498 (((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_META)) &&
1499 (meta_pos1 > meta_pos2))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001500 return 1;
1501 }
1502
Michal Vasko9f96a052020-03-10 09:41:45 +01001503 /* 1st META - any pos - 2nd TEXT, 1st META <pos< - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001504 /* 2nd is after 1st */
1505 return -1;
1506}
1507
1508/**
1509 * @brief Set cast for comparisons.
1510 *
1511 * @param[in] trg Target set to cast source into.
1512 * @param[in] src Source set.
1513 * @param[in] type Target set type.
1514 * @param[in] src_idx Source set node index.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001515 * @return LY_ERR
1516 */
1517static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001518set_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 +02001519{
1520 assert(src->type == LYXP_SET_NODE_SET);
1521
1522 set_init(trg, src);
1523
1524 /* insert node into target set */
1525 set_insert_node(trg, src->val.nodes[src_idx].node, src->val.nodes[src_idx].pos, src->val.nodes[src_idx].type, 0);
1526
1527 /* cast target set appropriately */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001528 return lyxp_set_cast(trg, type);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001529}
1530
Michal Vasko4c7763f2020-07-27 17:40:37 +02001531/**
1532 * @brief Set content canonization for comparisons.
1533 *
1534 * @param[in] trg Target set to put the canononized source into.
1535 * @param[in] src Source set.
1536 * @param[in] xp_node Source XPath node/meta to use for canonization.
1537 * @return LY_ERR
1538 */
1539static LY_ERR
1540set_comp_canonize(struct lyxp_set *trg, const struct lyxp_set *src, const struct lyxp_set_node *xp_node)
1541{
Michal Vaskofeca4fb2020-10-05 08:58:40 +02001542 const struct lysc_type *type = NULL;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001543 struct lyd_value val;
1544 struct ly_err_item *err = NULL;
1545 char *str, *ptr;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001546 LY_ERR rc;
1547
1548 /* is there anything to canonize even? */
1549 if ((src->type == LYXP_SET_NUMBER) || (src->type == LYXP_SET_STRING)) {
1550 /* do we have a type to use for canonization? */
1551 if ((xp_node->type == LYXP_NODE_ELEM) && (xp_node->node->schema->nodetype & LYD_NODE_TERM)) {
1552 type = ((struct lyd_node_term *)xp_node->node)->value.realtype;
1553 } else if (xp_node->type == LYXP_NODE_META) {
1554 type = ((struct lyd_meta *)xp_node->node)->value.realtype;
1555 }
1556 }
1557 if (!type) {
1558 goto fill;
1559 }
1560
1561 if (src->type == LYXP_SET_NUMBER) {
1562 /* canonize number */
1563 if (asprintf(&str, "%Lf", src->val.num) == -1) {
1564 LOGMEM(src->ctx);
1565 return LY_EMEM;
1566 }
1567 } else {
1568 /* canonize string */
1569 str = strdup(src->val.str);
1570 }
1571
1572 /* ignore errors, the value may not satisfy schema constraints */
Radek Krejci0b013302021-03-29 15:22:32 +02001573 rc = type->plugin->store(src->ctx, type, str, strlen(str), LYPLG_TYPE_STORE_DYNAMIC, src->format, src->prefix_data,
Michal Vasko405cc9e2020-12-01 12:01:27 +01001574 LYD_HINT_DATA, xp_node->node->schema, &val, NULL, &err);
Michal Vasko4c7763f2020-07-27 17:40:37 +02001575 ly_err_free(err);
1576 if (rc) {
1577 /* invalid value */
Radek IÅ¡a48460222021-03-02 15:18:32 +01001578 /* function store automaticaly dealloc value when fail */
Michal Vasko4c7763f2020-07-27 17:40:37 +02001579 goto fill;
1580 }
1581
Michal Vasko4c7763f2020-07-27 17:40:37 +02001582 /* use the canonized value */
1583 set_init(trg, src);
1584 trg->type = src->type;
1585 if (src->type == LYXP_SET_NUMBER) {
Michal Vasko0fdb0d92020-11-06 17:25:50 +01001586 trg->val.num = strtold(val.canonical, &ptr);
Michal Vasko4c7763f2020-07-27 17:40:37 +02001587 LY_CHECK_ERR_RET(ptr[0], LOGINT(src->ctx), LY_EINT);
1588 } else {
Michal Vasko0fdb0d92020-11-06 17:25:50 +01001589 trg->val.str = strdup(val.canonical);
Michal Vasko4c7763f2020-07-27 17:40:37 +02001590 }
1591 type->plugin->free(src->ctx, &val);
1592 return LY_SUCCESS;
1593
1594fill:
1595 /* no canonization needed/possible */
1596 set_fill_set(trg, src);
1597 return LY_SUCCESS;
1598}
1599
Michal Vasko03ff5a72019-09-11 13:49:33 +02001600#ifndef NDEBUG
1601
1602/**
1603 * @brief Bubble sort @p set into XPath document order.
1604 * Context position aware. Unused in the 'Release' build target.
1605 *
1606 * @param[in] set Set to sort.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001607 * @return How many times the whole set was traversed - 1 (if set was sorted, returns 0).
1608 */
1609static int
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001610set_sort(struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001611{
1612 uint32_t i, j;
Radek Krejci1deb5be2020-08-26 16:43:36 +02001613 int ret = 0, cmp;
Radek Krejci857189e2020-09-01 13:26:36 +02001614 ly_bool inverted, change;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001615 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001616 struct lyxp_set_node item;
1617 struct lyxp_set_hash_node hnode;
1618 uint64_t hash;
1619
Michal Vasko3cf8fbf2020-05-27 15:21:21 +02001620 if ((set->type != LYXP_SET_NODE_SET) || (set->used < 2)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001621 return 0;
1622 }
1623
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001624 /* find first top-level node to be used as anchor for positions */
Michal Vasko9e685082021-01-29 14:49:09 +01001625 for (root = set->cur_node; root->parent; root = lyd_parent(root)) {}
Michal Vaskod989ba02020-08-24 10:59:24 +02001626 for ( ; root->prev->next; root = root->prev) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001627
1628 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001629 if (set_assign_pos(set, root, set->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001630 return -1;
1631 }
1632
1633 LOGDBG(LY_LDGXPATH, "SORT BEGIN");
1634 print_set_debug(set);
1635
1636 for (i = 0; i < set->used; ++i) {
1637 inverted = 0;
1638 change = 0;
1639
1640 for (j = 1; j < set->used - i; ++j) {
1641 /* compare node positions */
1642 if (inverted) {
1643 cmp = set_sort_compare(&set->val.nodes[j], &set->val.nodes[j - 1]);
1644 } else {
1645 cmp = set_sort_compare(&set->val.nodes[j - 1], &set->val.nodes[j]);
1646 }
1647
1648 /* swap if needed */
1649 if ((inverted && (cmp < 0)) || (!inverted && (cmp > 0))) {
1650 change = 1;
1651
1652 item = set->val.nodes[j - 1];
1653 set->val.nodes[j - 1] = set->val.nodes[j];
1654 set->val.nodes[j] = item;
1655 } else {
1656 /* whether node_pos1 should be smaller than node_pos2 or the other way around */
1657 inverted = !inverted;
1658 }
1659 }
1660
1661 ++ret;
1662
1663 if (!change) {
1664 break;
1665 }
1666 }
1667
1668 LOGDBG(LY_LDGXPATH, "SORT END %d", ret);
1669 print_set_debug(set);
1670
1671 /* check node hashes */
1672 if (set->used >= LYD_HT_MIN_ITEMS) {
1673 assert(set->ht);
1674 for (i = 0; i < set->used; ++i) {
1675 hnode.node = set->val.nodes[i].node;
1676 hnode.type = set->val.nodes[i].type;
1677
1678 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
1679 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
1680 hash = dict_hash_multi(hash, NULL, 0);
1681
1682 assert(!lyht_find(set->ht, &hnode, hash, NULL));
1683 }
1684 }
1685
1686 return ret - 1;
1687}
1688
1689/**
1690 * @brief Remove duplicate entries in a sorted node set.
1691 *
1692 * @param[in] set Sorted set to check.
1693 * @return LY_ERR (LY_EEXIST if some duplicates are found)
1694 */
1695static LY_ERR
1696set_sorted_dup_node_clean(struct lyxp_set *set)
1697{
1698 uint32_t i = 0;
1699 LY_ERR ret = LY_SUCCESS;
1700
1701 if (set->used > 1) {
1702 while (i < set->used - 1) {
Michal Vasko69730152020-10-09 16:30:07 +02001703 if ((set->val.nodes[i].node == set->val.nodes[i + 1].node) &&
1704 (set->val.nodes[i].type == set->val.nodes[i + 1].type)) {
Michal Vasko2caefc12019-11-14 16:07:56 +01001705 set_remove_node_none(set, i + 1);
Michal Vasko57eab132019-09-24 11:46:26 +02001706 ret = LY_EEXIST;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001707 }
Michal Vasko57eab132019-09-24 11:46:26 +02001708 ++i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001709 }
1710 }
1711
Michal Vasko2caefc12019-11-14 16:07:56 +01001712 set_remove_nodes_none(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001713 return ret;
1714}
1715
1716#endif
1717
1718/**
1719 * @brief Merge 2 sorted sets into one.
1720 *
1721 * @param[in,out] trg Set to merge into. Duplicates are removed.
1722 * @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 +02001723 * @return LY_ERR
1724 */
1725static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001726set_sorted_merge(struct lyxp_set *trg, struct lyxp_set *src)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001727{
1728 uint32_t i, j, k, count, dup_count;
1729 int cmp;
1730 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001731
Michal Vaskod3678892020-05-21 10:06:58 +02001732 if ((trg->type != LYXP_SET_NODE_SET) || (src->type != LYXP_SET_NODE_SET)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001733 return LY_EINVAL;
1734 }
1735
Michal Vaskod3678892020-05-21 10:06:58 +02001736 if (!src->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001737 return LY_SUCCESS;
Michal Vaskod3678892020-05-21 10:06:58 +02001738 } else if (!trg->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001739 set_fill_set(trg, src);
Michal Vaskod3678892020-05-21 10:06:58 +02001740 lyxp_set_free_content(src);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001741 return LY_SUCCESS;
1742 }
1743
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001744 /* find first top-level node to be used as anchor for positions */
Michal Vasko9e685082021-01-29 14:49:09 +01001745 for (root = trg->cur_node; root->parent; root = lyd_parent(root)) {}
Michal Vaskod989ba02020-08-24 10:59:24 +02001746 for ( ; root->prev->next; root = root->prev) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001747
1748 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001749 if (set_assign_pos(trg, root, trg->root_type) || set_assign_pos(src, root, src->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001750 return LY_EINT;
1751 }
1752
1753#ifndef NDEBUG
1754 LOGDBG(LY_LDGXPATH, "MERGE target");
1755 print_set_debug(trg);
1756 LOGDBG(LY_LDGXPATH, "MERGE source");
1757 print_set_debug(src);
1758#endif
1759
1760 /* make memory for the merge (duplicates are not detected yet, so space
1761 * will likely be wasted on them, too bad) */
1762 if (trg->size - trg->used < src->used) {
1763 trg->size = trg->used + src->used;
1764
1765 trg->val.nodes = ly_realloc(trg->val.nodes, trg->size * sizeof *trg->val.nodes);
1766 LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx), LY_EMEM);
1767 }
1768
1769 i = 0;
1770 j = 0;
1771 count = 0;
1772 dup_count = 0;
1773 do {
1774 cmp = set_sort_compare(&src->val.nodes[i], &trg->val.nodes[j]);
1775 if (!cmp) {
1776 if (!count) {
1777 /* duplicate, just skip it */
1778 ++i;
1779 ++j;
1780 } else {
1781 /* we are copying something already, so let's copy the duplicate too,
1782 * we are hoping that afterwards there are some more nodes to
1783 * copy and this way we can copy them all together */
1784 ++count;
1785 ++dup_count;
1786 ++i;
1787 ++j;
1788 }
1789 } else if (cmp < 0) {
1790 /* inserting src node into trg, just remember it for now */
1791 ++count;
1792 ++i;
1793
1794 /* insert the hash now */
1795 set_insert_node_hash(trg, src->val.nodes[i - 1].node, src->val.nodes[i - 1].type);
1796 } else if (count) {
1797copy_nodes:
1798 /* time to actually copy the nodes, we have found the largest block of nodes */
1799 memmove(&trg->val.nodes[j + (count - dup_count)],
1800 &trg->val.nodes[j],
1801 (trg->used - j) * sizeof *trg->val.nodes);
1802 memcpy(&trg->val.nodes[j - dup_count], &src->val.nodes[i - count], count * sizeof *src->val.nodes);
1803
1804 trg->used += count - dup_count;
1805 /* do not change i, except the copying above, we are basically doing exactly what is in the else branch below */
1806 j += count - dup_count;
1807
1808 count = 0;
1809 dup_count = 0;
1810 } else {
1811 ++j;
1812 }
1813 } while ((i < src->used) && (j < trg->used));
1814
1815 if ((i < src->used) || count) {
1816 /* insert all the hashes first */
1817 for (k = i; k < src->used; ++k) {
1818 set_insert_node_hash(trg, src->val.nodes[k].node, src->val.nodes[k].type);
1819 }
1820
1821 /* loop ended, but we need to copy something at trg end */
1822 count += src->used - i;
1823 i = src->used;
1824 goto copy_nodes;
1825 }
1826
1827 /* we are inserting hashes before the actual node insert, which causes
1828 * situations when there were initially not enough items for a hash table,
1829 * but even after some were inserted, hash table was not created (during
1830 * insertion the number of items is not updated yet) */
1831 if (!trg->ht && (trg->used >= LYD_HT_MIN_ITEMS)) {
1832 set_insert_node_hash(trg, NULL, 0);
1833 }
1834
1835#ifndef NDEBUG
1836 LOGDBG(LY_LDGXPATH, "MERGE result");
1837 print_set_debug(trg);
1838#endif
1839
Michal Vaskod3678892020-05-21 10:06:58 +02001840 lyxp_set_free_content(src);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001841 return LY_SUCCESS;
1842}
1843
1844/*
1845 * (re)parse functions
1846 *
1847 * Parse functions parse the expression into
1848 * tokens (syntactic analysis).
1849 *
1850 * Reparse functions perform semantic analysis
1851 * (do not save the result, just a check) of
1852 * the expression and fill repeat indices.
1853 */
1854
Michal Vasko14676352020-05-29 11:35:55 +02001855LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02001856lyxp_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 +02001857{
Michal Vasko004d3152020-06-11 19:59:22 +02001858 if (exp->used == tok_idx) {
Michal Vasko14676352020-05-29 11:35:55 +02001859 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001860 LOGVAL(ctx, LY_VCODE_XP_EOF);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001861 }
Michal Vasko14676352020-05-29 11:35:55 +02001862 return LY_EINCOMPLETE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001863 }
1864
Michal Vasko004d3152020-06-11 19:59:22 +02001865 if (want_tok && (exp->tokens[tok_idx] != want_tok)) {
Michal Vasko14676352020-05-29 11:35:55 +02001866 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001867 LOGVAL(ctx, LY_VCODE_XP_INTOK2, lyxp_print_token(exp->tokens[tok_idx]),
Michal Vasko0b468e62020-10-19 09:33:04 +02001868 &exp->expr[exp->tok_pos[tok_idx]], lyxp_print_token(want_tok));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001869 }
Michal Vasko14676352020-05-29 11:35:55 +02001870 return LY_ENOT;
1871 }
1872
1873 return LY_SUCCESS;
1874}
1875
Michal Vasko004d3152020-06-11 19:59:22 +02001876LY_ERR
1877lyxp_next_token(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_token want_tok)
1878{
1879 LY_CHECK_RET(lyxp_check_token(ctx, exp, *tok_idx, want_tok));
1880
1881 /* skip the token */
1882 ++(*tok_idx);
1883
1884 return LY_SUCCESS;
1885}
1886
Michal Vasko14676352020-05-29 11:35:55 +02001887/* just like lyxp_check_token() but tests for 2 tokens */
1888static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02001889exp_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 +02001890 enum lyxp_token want_tok2)
Michal Vasko14676352020-05-29 11:35:55 +02001891{
Michal Vasko004d3152020-06-11 19:59:22 +02001892 if (exp->used == tok_idx) {
Michal Vasko14676352020-05-29 11:35:55 +02001893 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001894 LOGVAL(ctx, LY_VCODE_XP_EOF);
Michal Vasko14676352020-05-29 11:35:55 +02001895 }
1896 return LY_EINCOMPLETE;
1897 }
1898
Michal Vasko004d3152020-06-11 19:59:22 +02001899 if ((exp->tokens[tok_idx] != want_tok1) && (exp->tokens[tok_idx] != want_tok2)) {
Michal Vasko14676352020-05-29 11:35:55 +02001900 if (ctx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001901 LOGVAL(ctx, LY_VCODE_XP_INTOK, lyxp_print_token(exp->tokens[tok_idx]),
Michal Vasko0b468e62020-10-19 09:33:04 +02001902 &exp->expr[exp->tok_pos[tok_idx]]);
Michal Vasko14676352020-05-29 11:35:55 +02001903 }
1904 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001905 }
1906
1907 return LY_SUCCESS;
1908}
1909
1910/**
1911 * @brief Stack operation push on the repeat array.
1912 *
1913 * @param[in] exp Expression to use.
Michal Vasko004d3152020-06-11 19:59:22 +02001914 * @param[in] tok_idx Position in the expresion \p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001915 * @param[in] repeat_op_idx Index from \p exp of the operator token. This value is pushed.
1916 */
1917static void
Michal Vasko004d3152020-06-11 19:59:22 +02001918exp_repeat_push(struct lyxp_expr *exp, uint16_t tok_idx, uint16_t repeat_op_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001919{
1920 uint16_t i;
1921
Michal Vasko004d3152020-06-11 19:59:22 +02001922 if (exp->repeat[tok_idx]) {
Radek Krejci1e008d22020-08-17 11:37:37 +02001923 for (i = 0; exp->repeat[tok_idx][i]; ++i) {}
Michal Vasko004d3152020-06-11 19:59:22 +02001924 exp->repeat[tok_idx] = realloc(exp->repeat[tok_idx], (i + 2) * sizeof *exp->repeat[tok_idx]);
1925 LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), );
1926 exp->repeat[tok_idx][i] = repeat_op_idx;
1927 exp->repeat[tok_idx][i + 1] = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001928 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02001929 exp->repeat[tok_idx] = calloc(2, sizeof *exp->repeat[tok_idx]);
1930 LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), );
1931 exp->repeat[tok_idx][0] = repeat_op_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001932 }
1933}
1934
1935/**
1936 * @brief Reparse Predicate. Logs directly on error.
1937 *
1938 * [7] Predicate ::= '[' Expr ']'
1939 *
1940 * @param[in] ctx Context for logging.
1941 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02001942 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001943 * @return LY_ERR
1944 */
1945static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02001946reparse_predicate(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001947{
1948 LY_ERR rc;
1949
Michal Vasko004d3152020-06-11 19:59:22 +02001950 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001951 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02001952 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001953
Michal Vasko004d3152020-06-11 19:59:22 +02001954 rc = reparse_or_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001955 LY_CHECK_RET(rc);
1956
Michal Vasko004d3152020-06-11 19:59:22 +02001957 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001958 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02001959 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001960
1961 return LY_SUCCESS;
1962}
1963
1964/**
1965 * @brief Reparse RelativeLocationPath. Logs directly on error.
1966 *
1967 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
1968 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
1969 * [6] NodeTest ::= NameTest | NodeType '(' ')'
1970 *
1971 * @param[in] ctx Context for logging.
1972 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02001973 * @param[in] tok_idx Position in the expression \p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001974 * @return LY_ERR (LY_EINCOMPLETE on forward reference)
1975 */
1976static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02001977reparse_relative_location_path(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001978{
1979 LY_ERR rc;
1980
Michal Vasko004d3152020-06-11 19:59:22 +02001981 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001982 LY_CHECK_RET(rc);
1983
1984 goto step;
1985 do {
1986 /* '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02001987 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001988
Michal Vasko004d3152020-06-11 19:59:22 +02001989 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001990 LY_CHECK_RET(rc);
1991step:
1992 /* Step */
Michal Vasko004d3152020-06-11 19:59:22 +02001993 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001994 case LYXP_TOKEN_DOT:
Michal Vasko004d3152020-06-11 19:59:22 +02001995 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001996 break;
1997
1998 case LYXP_TOKEN_DDOT:
Michal Vasko004d3152020-06-11 19:59:22 +02001999 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002000 break;
2001
2002 case LYXP_TOKEN_AT:
Michal Vasko004d3152020-06-11 19:59:22 +02002003 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002004
Michal Vasko004d3152020-06-11 19:59:22 +02002005 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002006 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002007 if ((exp->tokens[*tok_idx] != LYXP_TOKEN_NAMETEST) && (exp->tokens[*tok_idx] != LYXP_TOKEN_NODETYPE)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002008 LOGVAL(ctx, LY_VCODE_XP_INTOK, lyxp_print_token(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002009 return LY_EVALID;
2010 }
Radek Krejci0f969882020-08-21 16:56:47 +02002011 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02002012 case LYXP_TOKEN_NAMETEST:
Michal Vasko004d3152020-06-11 19:59:22 +02002013 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002014 goto reparse_predicate;
2015 break;
2016
2017 case LYXP_TOKEN_NODETYPE:
Michal Vasko004d3152020-06-11 19:59:22 +02002018 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002019
2020 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02002021 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002022 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002023 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002024
2025 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02002026 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002027 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002028 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002029
2030reparse_predicate:
2031 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002032 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
2033 rc = reparse_predicate(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002034 LY_CHECK_RET(rc);
2035 }
2036 break;
2037 default:
Radek Krejci2efc45b2020-12-22 16:25:44 +01002038 LOGVAL(ctx, LY_VCODE_XP_INTOK, lyxp_print_token(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002039 return LY_EVALID;
2040 }
Michal Vasko004d3152020-06-11 19:59:22 +02002041 } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02002042
2043 return LY_SUCCESS;
2044}
2045
2046/**
2047 * @brief Reparse AbsoluteLocationPath. Logs directly on error.
2048 *
2049 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
2050 *
2051 * @param[in] ctx Context for logging.
2052 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002053 * @param[in] tok_idx Position in the expression \p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002054 * @return LY_ERR
2055 */
2056static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002057reparse_absolute_location_path(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002058{
2059 LY_ERR rc;
2060
Michal Vasko004d3152020-06-11 19:59:22 +02002061 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 +02002062
2063 /* '/' RelativeLocationPath? */
Michal Vasko004d3152020-06-11 19:59:22 +02002064 if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002065 /* '/' */
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 if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002069 return LY_SUCCESS;
2070 }
Michal Vasko004d3152020-06-11 19:59:22 +02002071 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002072 case LYXP_TOKEN_DOT:
2073 case LYXP_TOKEN_DDOT:
2074 case LYXP_TOKEN_AT:
2075 case LYXP_TOKEN_NAMETEST:
2076 case LYXP_TOKEN_NODETYPE:
Michal Vasko004d3152020-06-11 19:59:22 +02002077 rc = reparse_relative_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002078 LY_CHECK_RET(rc);
Radek Krejci0f969882020-08-21 16:56:47 +02002079 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02002080 default:
2081 break;
2082 }
2083
Michal Vasko03ff5a72019-09-11 13:49:33 +02002084 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02002085 /* '//' RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002086 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002087
Michal Vasko004d3152020-06-11 19:59:22 +02002088 rc = reparse_relative_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002089 LY_CHECK_RET(rc);
2090 }
2091
2092 return LY_SUCCESS;
2093}
2094
2095/**
2096 * @brief Reparse FunctionCall. Logs directly on error.
2097 *
2098 * [9] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
2099 *
2100 * @param[in] ctx Context for logging.
2101 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002102 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002103 * @return LY_ERR
2104 */
2105static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002106reparse_function_call(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002107{
Radek Krejci1deb5be2020-08-26 16:43:36 +02002108 int8_t min_arg_count = -1;
2109 uint32_t arg_count, max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002110 uint16_t func_tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002111 LY_ERR rc;
2112
Michal Vasko004d3152020-06-11 19:59:22 +02002113 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_FUNCNAME);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002114 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002115 func_tok_idx = *tok_idx;
2116 switch (exp->tok_len[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002117 case 3:
Michal Vasko004d3152020-06-11 19:59:22 +02002118 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002119 min_arg_count = 1;
2120 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002121 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002122 min_arg_count = 1;
2123 max_arg_count = 1;
2124 }
2125 break;
2126 case 4:
Michal Vasko004d3152020-06-11 19:59:22 +02002127 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002128 min_arg_count = 1;
2129 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002130 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002131 min_arg_count = 0;
2132 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002133 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002134 min_arg_count = 0;
2135 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002136 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002137 min_arg_count = 0;
2138 max_arg_count = 0;
2139 }
2140 break;
2141 case 5:
Michal Vasko004d3152020-06-11 19:59:22 +02002142 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002143 min_arg_count = 1;
2144 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002145 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002146 min_arg_count = 0;
2147 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002148 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002149 min_arg_count = 1;
2150 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002151 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002152 min_arg_count = 1;
2153 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002154 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002155 min_arg_count = 1;
2156 max_arg_count = 1;
2157 }
2158 break;
2159 case 6:
Michal Vasko004d3152020-06-11 19:59:22 +02002160 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002161 min_arg_count = 2;
Radek Krejci1deb5be2020-08-26 16:43:36 +02002162 max_arg_count = UINT32_MAX;
Michal Vasko004d3152020-06-11 19:59:22 +02002163 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002164 min_arg_count = 0;
2165 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002166 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002167 min_arg_count = 0;
2168 max_arg_count = 1;
2169 }
2170 break;
2171 case 7:
Michal Vasko004d3152020-06-11 19:59:22 +02002172 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002173 min_arg_count = 1;
2174 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002175 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002176 min_arg_count = 1;
2177 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002178 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002179 min_arg_count = 0;
2180 max_arg_count = 0;
2181 }
2182 break;
2183 case 8:
Michal Vasko004d3152020-06-11 19:59:22 +02002184 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002185 min_arg_count = 2;
2186 max_arg_count = 2;
Michal Vasko004d3152020-06-11 19:59:22 +02002187 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002188 min_arg_count = 0;
2189 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002190 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002191 min_arg_count = 2;
2192 max_arg_count = 2;
2193 }
2194 break;
2195 case 9:
Michal Vasko004d3152020-06-11 19:59:22 +02002196 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002197 min_arg_count = 2;
2198 max_arg_count = 3;
Michal Vasko004d3152020-06-11 19:59:22 +02002199 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002200 min_arg_count = 3;
2201 max_arg_count = 3;
2202 }
2203 break;
2204 case 10:
Michal Vasko004d3152020-06-11 19:59:22 +02002205 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002206 min_arg_count = 0;
2207 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002208 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002209 min_arg_count = 1;
2210 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002211 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002212 min_arg_count = 2;
2213 max_arg_count = 2;
2214 }
2215 break;
2216 case 11:
Michal Vasko004d3152020-06-11 19:59:22 +02002217 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002218 min_arg_count = 2;
2219 max_arg_count = 2;
2220 }
2221 break;
2222 case 12:
Michal Vasko004d3152020-06-11 19:59:22 +02002223 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002224 min_arg_count = 2;
2225 max_arg_count = 2;
2226 }
2227 break;
2228 case 13:
Michal Vasko004d3152020-06-11 19:59:22 +02002229 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002230 min_arg_count = 0;
2231 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002232 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002233 min_arg_count = 0;
2234 max_arg_count = 1;
2235 }
2236 break;
2237 case 15:
Michal Vasko004d3152020-06-11 19:59:22 +02002238 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002239 min_arg_count = 0;
2240 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002241 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002242 min_arg_count = 2;
2243 max_arg_count = 2;
2244 }
2245 break;
2246 case 16:
Michal Vasko004d3152020-06-11 19:59:22 +02002247 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002248 min_arg_count = 2;
2249 max_arg_count = 2;
2250 }
2251 break;
2252 case 20:
Michal Vasko004d3152020-06-11 19:59:22 +02002253 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002254 min_arg_count = 2;
2255 max_arg_count = 2;
2256 }
2257 break;
2258 }
2259 if (min_arg_count == -1) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002260 LOGVAL(ctx, LY_VCODE_XP_INFUNC, exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002261 return LY_EINVAL;
2262 }
Michal Vasko004d3152020-06-11 19:59:22 +02002263 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002264
2265 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02002266 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002267 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002268 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002269
2270 /* ( Expr ( ',' Expr )* )? */
2271 arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002272 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002273 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002274 if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002275 ++arg_count;
Michal Vasko004d3152020-06-11 19:59:22 +02002276 rc = reparse_or_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002277 LY_CHECK_RET(rc);
2278 }
Michal Vasko004d3152020-06-11 19:59:22 +02002279 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) {
2280 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002281
2282 ++arg_count;
Michal Vasko004d3152020-06-11 19:59:22 +02002283 rc = reparse_or_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002284 LY_CHECK_RET(rc);
2285 }
2286
2287 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02002288 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002289 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002290 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002291
Radek Krejci857189e2020-09-01 13:26:36 +02002292 if ((arg_count < (uint32_t)min_arg_count) || (arg_count > max_arg_count)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002293 LOGVAL(ctx, LY_VCODE_XP_INARGCOUNT, arg_count, exp->tok_len[func_tok_idx], &exp->expr[exp->tok_pos[func_tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002294 return LY_EVALID;
2295 }
2296
2297 return LY_SUCCESS;
2298}
2299
2300/**
2301 * @brief Reparse PathExpr. Logs directly on error.
2302 *
2303 * [10] PathExpr ::= LocationPath | PrimaryExpr Predicate*
2304 * | PrimaryExpr Predicate* '/' RelativeLocationPath
2305 * | PrimaryExpr Predicate* '//' RelativeLocationPath
2306 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
2307 * [8] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall
2308 *
2309 * @param[in] ctx Context for logging.
2310 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002311 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002312 * @return LY_ERR
2313 */
2314static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002315reparse_path_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002316{
2317 LY_ERR rc;
2318
Michal Vasko004d3152020-06-11 19:59:22 +02002319 if (lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko14676352020-05-29 11:35:55 +02002320 return LY_EVALID;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002321 }
2322
Michal Vasko004d3152020-06-11 19:59:22 +02002323 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002324 case LYXP_TOKEN_PAR1:
2325 /* '(' Expr ')' Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002326 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002327
Michal Vasko004d3152020-06-11 19:59:22 +02002328 rc = reparse_or_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002329 LY_CHECK_RET(rc);
2330
Michal Vasko004d3152020-06-11 19:59:22 +02002331 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002332 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002333 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002334 goto predicate;
2335 break;
2336 case LYXP_TOKEN_DOT:
2337 case LYXP_TOKEN_DDOT:
2338 case LYXP_TOKEN_AT:
2339 case LYXP_TOKEN_NAMETEST:
2340 case LYXP_TOKEN_NODETYPE:
2341 /* RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002342 rc = reparse_relative_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002343 LY_CHECK_RET(rc);
2344 break;
2345 case LYXP_TOKEN_FUNCNAME:
2346 /* FunctionCall */
Michal Vasko004d3152020-06-11 19:59:22 +02002347 rc = reparse_function_call(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002348 LY_CHECK_RET(rc);
2349 goto predicate;
2350 break;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002351 case LYXP_TOKEN_OPER_PATH:
2352 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02002353 /* AbsoluteLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002354 rc = reparse_absolute_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002355 LY_CHECK_RET(rc);
2356 break;
2357 case LYXP_TOKEN_LITERAL:
2358 /* Literal */
Michal Vasko004d3152020-06-11 19:59:22 +02002359 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002360 goto predicate;
2361 break;
2362 case LYXP_TOKEN_NUMBER:
2363 /* Number */
Michal Vasko004d3152020-06-11 19:59:22 +02002364 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002365 goto predicate;
2366 break;
2367 default:
Radek Krejci2efc45b2020-12-22 16:25:44 +01002368 LOGVAL(ctx, LY_VCODE_XP_INTOK, lyxp_print_token(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002369 return LY_EVALID;
2370 }
2371
2372 return LY_SUCCESS;
2373
2374predicate:
2375 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002376 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
2377 rc = reparse_predicate(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002378 LY_CHECK_RET(rc);
2379 }
2380
2381 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002382 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002383
2384 /* '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02002385 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002386
Michal Vasko004d3152020-06-11 19:59:22 +02002387 rc = reparse_relative_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002388 LY_CHECK_RET(rc);
2389 }
2390
2391 return LY_SUCCESS;
2392}
2393
2394/**
2395 * @brief Reparse UnaryExpr. Logs directly on error.
2396 *
2397 * [17] UnaryExpr ::= UnionExpr | '-' UnaryExpr
2398 * [18] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
2399 *
2400 * @param[in] ctx Context for logging.
2401 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002402 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002403 * @return LY_ERR
2404 */
2405static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002406reparse_unary_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002407{
2408 uint16_t prev_exp;
2409 LY_ERR rc;
2410
2411 /* ('-')* */
Michal Vasko004d3152020-06-11 19:59:22 +02002412 prev_exp = *tok_idx;
Michal Vasko69730152020-10-09 16:30:07 +02002413 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2414 (exp->expr[exp->tok_pos[*tok_idx]] == '-')) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002415 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNARY);
Michal Vasko004d3152020-06-11 19:59:22 +02002416 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002417 }
2418
2419 /* PathExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002420 prev_exp = *tok_idx;
2421 rc = reparse_path_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002422 LY_CHECK_RET(rc);
2423
2424 /* ('|' PathExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002425 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_UNI)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002426 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNION);
Michal Vasko004d3152020-06-11 19:59:22 +02002427 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002428
Michal Vasko004d3152020-06-11 19:59:22 +02002429 rc = reparse_path_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002430 LY_CHECK_RET(rc);
2431 }
2432
2433 return LY_SUCCESS;
2434}
2435
2436/**
2437 * @brief Reparse AdditiveExpr. Logs directly on error.
2438 *
2439 * [15] AdditiveExpr ::= MultiplicativeExpr
2440 * | AdditiveExpr '+' MultiplicativeExpr
2441 * | AdditiveExpr '-' MultiplicativeExpr
2442 * [16] MultiplicativeExpr ::= UnaryExpr
2443 * | MultiplicativeExpr '*' UnaryExpr
2444 * | MultiplicativeExpr 'div' UnaryExpr
2445 * | MultiplicativeExpr 'mod' UnaryExpr
2446 *
2447 * @param[in] ctx Context for logging.
2448 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002449 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002450 * @return LY_ERR
2451 */
2452static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002453reparse_additive_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002454{
2455 uint16_t prev_add_exp, prev_mul_exp;
2456 LY_ERR rc;
2457
Michal Vasko004d3152020-06-11 19:59:22 +02002458 prev_add_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002459 goto reparse_multiplicative_expr;
2460
2461 /* ('+' / '-' MultiplicativeExpr)* */
Michal Vasko69730152020-10-09 16:30:07 +02002462 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2463 ((exp->expr[exp->tok_pos[*tok_idx]] == '+') || (exp->expr[exp->tok_pos[*tok_idx]] == '-'))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002464 exp_repeat_push(exp, prev_add_exp, LYXP_EXPR_ADDITIVE);
Michal Vasko004d3152020-06-11 19:59:22 +02002465 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002466
2467reparse_multiplicative_expr:
2468 /* UnaryExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002469 prev_mul_exp = *tok_idx;
2470 rc = reparse_unary_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002471 LY_CHECK_RET(rc);
2472
2473 /* ('*' / 'div' / 'mod' UnaryExpr)* */
Michal Vasko69730152020-10-09 16:30:07 +02002474 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) &&
2475 ((exp->expr[exp->tok_pos[*tok_idx]] == '*') || (exp->tok_len[*tok_idx] == 3))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002476 exp_repeat_push(exp, prev_mul_exp, LYXP_EXPR_MULTIPLICATIVE);
Michal Vasko004d3152020-06-11 19:59:22 +02002477 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002478
Michal Vasko004d3152020-06-11 19:59:22 +02002479 rc = reparse_unary_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002480 LY_CHECK_RET(rc);
2481 }
2482 }
2483
2484 return LY_SUCCESS;
2485}
2486
2487/**
2488 * @brief Reparse EqualityExpr. Logs directly on error.
2489 *
2490 * [13] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
2491 * | EqualityExpr '!=' RelationalExpr
2492 * [14] RelationalExpr ::= AdditiveExpr
2493 * | RelationalExpr '<' AdditiveExpr
2494 * | RelationalExpr '>' AdditiveExpr
2495 * | RelationalExpr '<=' AdditiveExpr
2496 * | RelationalExpr '>=' AdditiveExpr
2497 *
2498 * @param[in] ctx Context for logging.
2499 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002500 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002501 * @return LY_ERR
2502 */
2503static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002504reparse_equality_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002505{
2506 uint16_t prev_eq_exp, prev_rel_exp;
2507 LY_ERR rc;
2508
Michal Vasko004d3152020-06-11 19:59:22 +02002509 prev_eq_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002510 goto reparse_additive_expr;
2511
2512 /* ('=' / '!=' RelationalExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002513 while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_EQUAL, LYXP_TOKEN_OPER_NEQUAL)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002514 exp_repeat_push(exp, prev_eq_exp, LYXP_EXPR_EQUALITY);
Michal Vasko004d3152020-06-11 19:59:22 +02002515 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002516
2517reparse_additive_expr:
2518 /* AdditiveExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002519 prev_rel_exp = *tok_idx;
2520 rc = reparse_additive_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002521 LY_CHECK_RET(rc);
2522
2523 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002524 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_COMP)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002525 exp_repeat_push(exp, prev_rel_exp, LYXP_EXPR_RELATIONAL);
Michal Vasko004d3152020-06-11 19:59:22 +02002526 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002527
Michal Vasko004d3152020-06-11 19:59:22 +02002528 rc = reparse_additive_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002529 LY_CHECK_RET(rc);
2530 }
2531 }
2532
2533 return LY_SUCCESS;
2534}
2535
2536/**
2537 * @brief Reparse OrExpr. Logs directly on error.
2538 *
2539 * [11] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
2540 * [12] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
2541 *
2542 * @param[in] ctx Context for logging.
2543 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002544 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002545 * @return LY_ERR
2546 */
2547static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002548reparse_or_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002549{
2550 uint16_t prev_or_exp, prev_and_exp;
2551 LY_ERR rc;
2552
Michal Vasko004d3152020-06-11 19:59:22 +02002553 prev_or_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002554 goto reparse_equality_expr;
2555
2556 /* ('or' AndExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002557 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 +02002558 exp_repeat_push(exp, prev_or_exp, LYXP_EXPR_OR);
Michal Vasko004d3152020-06-11 19:59:22 +02002559 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002560
2561reparse_equality_expr:
2562 /* EqualityExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002563 prev_and_exp = *tok_idx;
2564 rc = reparse_equality_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002565 LY_CHECK_RET(rc);
2566
2567 /* ('and' EqualityExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002568 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 +02002569 exp_repeat_push(exp, prev_and_exp, LYXP_EXPR_AND);
Michal Vasko004d3152020-06-11 19:59:22 +02002570 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002571
Michal Vasko004d3152020-06-11 19:59:22 +02002572 rc = reparse_equality_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002573 LY_CHECK_RET(rc);
2574 }
2575 }
2576
2577 return LY_SUCCESS;
2578}
Radek Krejcib1646a92018-11-02 16:08:26 +01002579
2580/**
2581 * @brief Parse NCName.
2582 *
2583 * @param[in] ncname Name to parse.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002584 * @return Length of @p ncname valid bytes.
Radek Krejcib1646a92018-11-02 16:08:26 +01002585 */
Radek Krejcid4270262019-01-07 15:07:25 +01002586static long int
Radek Krejcib1646a92018-11-02 16:08:26 +01002587parse_ncname(const char *ncname)
2588{
Radek Krejci1deb5be2020-08-26 16:43:36 +02002589 uint32_t uc;
Radek Krejcid4270262019-01-07 15:07:25 +01002590 size_t size;
2591 long int len = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002592
2593 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), 0);
2594 if (!is_xmlqnamestartchar(uc) || (uc == ':')) {
2595 return len;
2596 }
2597
2598 do {
2599 len += size;
Radek Krejci9a564c92019-01-07 14:53:57 +01002600 if (!*ncname) {
2601 break;
2602 }
Radek Krejcid4270262019-01-07 15:07:25 +01002603 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), -len);
Radek Krejcib1646a92018-11-02 16:08:26 +01002604 } while (is_xmlqnamechar(uc) && (uc != ':'));
2605
2606 return len;
2607}
2608
2609/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02002610 * @brief Add @p token into the expression @p exp.
Radek Krejcib1646a92018-11-02 16:08:26 +01002611 *
Michal Vasko03ff5a72019-09-11 13:49:33 +02002612 * @param[in] ctx Context for logging.
Radek Krejcib1646a92018-11-02 16:08:26 +01002613 * @param[in] exp Expression to use.
2614 * @param[in] token Token to add.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002615 * @param[in] tok_pos Token position in the XPath expression.
Radek Krejcib1646a92018-11-02 16:08:26 +01002616 * @param[in] tok_len Token length in the XPath expression.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002617 * @return LY_ERR
Radek Krejcib1646a92018-11-02 16:08:26 +01002618 */
2619static LY_ERR
Michal Vasko14676352020-05-29 11:35:55 +02002620exp_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 +01002621{
2622 uint32_t prev;
2623
2624 if (exp->used == exp->size) {
2625 prev = exp->size;
2626 exp->size += LYXP_EXPR_SIZE_STEP;
2627 if (prev > exp->size) {
2628 LOGINT(ctx);
2629 return LY_EINT;
2630 }
2631
2632 exp->tokens = ly_realloc(exp->tokens, exp->size * sizeof *exp->tokens);
2633 LY_CHECK_ERR_RET(!exp->tokens, LOGMEM(ctx), LY_EMEM);
2634 exp->tok_pos = ly_realloc(exp->tok_pos, exp->size * sizeof *exp->tok_pos);
2635 LY_CHECK_ERR_RET(!exp->tok_pos, LOGMEM(ctx), LY_EMEM);
2636 exp->tok_len = ly_realloc(exp->tok_len, exp->size * sizeof *exp->tok_len);
2637 LY_CHECK_ERR_RET(!exp->tok_len, LOGMEM(ctx), LY_EMEM);
2638 }
2639
2640 exp->tokens[exp->used] = token;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002641 exp->tok_pos[exp->used] = tok_pos;
Radek Krejcib1646a92018-11-02 16:08:26 +01002642 exp->tok_len[exp->used] = tok_len;
2643 ++exp->used;
2644 return LY_SUCCESS;
2645}
2646
2647void
Michal Vasko14676352020-05-29 11:35:55 +02002648lyxp_expr_free(const struct ly_ctx *ctx, struct lyxp_expr *expr)
Radek Krejcib1646a92018-11-02 16:08:26 +01002649{
2650 uint16_t i;
2651
2652 if (!expr) {
2653 return;
2654 }
2655
2656 lydict_remove(ctx, expr->expr);
2657 free(expr->tokens);
2658 free(expr->tok_pos);
2659 free(expr->tok_len);
2660 if (expr->repeat) {
2661 for (i = 0; i < expr->used; ++i) {
2662 free(expr->repeat[i]);
2663 }
2664 }
2665 free(expr->repeat);
2666 free(expr);
2667}
2668
Radek Krejcif03a9e22020-09-18 20:09:31 +02002669LY_ERR
2670lyxp_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 +01002671{
Radek Krejcif03a9e22020-09-18 20:09:31 +02002672 LY_ERR ret = LY_SUCCESS;
2673 struct lyxp_expr *expr;
Radek Krejcid4270262019-01-07 15:07:25 +01002674 size_t parsed = 0, tok_len;
Radek Krejcib1646a92018-11-02 16:08:26 +01002675 enum lyxp_token tok_type;
Radek Krejci857189e2020-09-01 13:26:36 +02002676 ly_bool prev_function_check = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002677 uint16_t tok_idx = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002678
Radek Krejcif03a9e22020-09-18 20:09:31 +02002679 assert(expr_p);
2680
2681 if (!expr_str[0]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002682 LOGVAL(ctx, LY_VCODE_XP_EOF);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002683 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02002684 }
2685
2686 if (!expr_len) {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002687 expr_len = strlen(expr_str);
Michal Vasko004d3152020-06-11 19:59:22 +02002688 }
2689 if (expr_len > UINT16_MAX) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002690 LOGVAL(ctx, LYVE_XPATH, "XPath expression cannot be longer than %ud characters.", UINT16_MAX);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002691 return LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01002692 }
2693
2694 /* init lyxp_expr structure */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002695 expr = calloc(1, sizeof *expr);
2696 LY_CHECK_ERR_GOTO(!expr, LOGMEM(ctx); ret = LY_EMEM, error);
2697 LY_CHECK_GOTO(ret = lydict_insert(ctx, expr_str, expr_len, &expr->expr), error);
2698 expr->used = 0;
2699 expr->size = LYXP_EXPR_SIZE_START;
2700 expr->tokens = malloc(expr->size * sizeof *expr->tokens);
2701 LY_CHECK_ERR_GOTO(!expr->tokens, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002702
Radek Krejcif03a9e22020-09-18 20:09:31 +02002703 expr->tok_pos = malloc(expr->size * sizeof *expr->tok_pos);
2704 LY_CHECK_ERR_GOTO(!expr->tok_pos, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002705
Radek Krejcif03a9e22020-09-18 20:09:31 +02002706 expr->tok_len = malloc(expr->size * sizeof *expr->tok_len);
2707 LY_CHECK_ERR_GOTO(!expr->tok_len, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002708
Michal Vasko004d3152020-06-11 19:59:22 +02002709 /* make expr 0-terminated */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002710 expr_str = expr->expr;
Michal Vasko004d3152020-06-11 19:59:22 +02002711
Radek Krejcif03a9e22020-09-18 20:09:31 +02002712 while (is_xmlws(expr_str[parsed])) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002713 ++parsed;
2714 }
2715
2716 do {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002717 if (expr_str[parsed] == '(') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002718
2719 /* '(' */
2720 tok_len = 1;
2721 tok_type = LYXP_TOKEN_PAR1;
2722
Radek Krejcif03a9e22020-09-18 20:09:31 +02002723 if (prev_function_check && expr->used && (expr->tokens[expr->used - 1] == LYXP_TOKEN_NAMETEST)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002724 /* it is a NodeType/FunctionName after all */
Michal Vasko69730152020-10-09 16:30:07 +02002725 if (((expr->tok_len[expr->used - 1] == 4) &&
2726 (!strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "node", 4) ||
2727 !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "text", 4))) ||
2728 ((expr->tok_len[expr->used - 1] == 7) &&
2729 !strncmp(&expr_str[expr->tok_pos[expr->used - 1]], "comment", 7))) {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002730 expr->tokens[expr->used - 1] = LYXP_TOKEN_NODETYPE;
Radek Krejcib1646a92018-11-02 16:08:26 +01002731 } else {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002732 expr->tokens[expr->used - 1] = LYXP_TOKEN_FUNCNAME;
Radek Krejcib1646a92018-11-02 16:08:26 +01002733 }
2734 prev_function_check = 0;
2735 }
2736
Radek Krejcif03a9e22020-09-18 20:09:31 +02002737 } else if (expr_str[parsed] == ')') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002738
2739 /* ')' */
2740 tok_len = 1;
2741 tok_type = LYXP_TOKEN_PAR2;
2742
Radek Krejcif03a9e22020-09-18 20:09:31 +02002743 } else if (expr_str[parsed] == '[') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002744
2745 /* '[' */
2746 tok_len = 1;
2747 tok_type = LYXP_TOKEN_BRACK1;
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_BRACK2;
2754
Radek Krejcif03a9e22020-09-18 20:09:31 +02002755 } else if (!strncmp(&expr_str[parsed], "..", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002756
2757 /* '..' */
2758 tok_len = 2;
2759 tok_type = LYXP_TOKEN_DDOT;
2760
Radek Krejcif03a9e22020-09-18 20:09:31 +02002761 } else if ((expr_str[parsed] == '.') && (!isdigit(expr_str[parsed + 1]))) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002762
2763 /* '.' */
2764 tok_len = 1;
2765 tok_type = LYXP_TOKEN_DOT;
2766
Radek Krejcif03a9e22020-09-18 20:09:31 +02002767 } else if (expr_str[parsed] == '@') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002768
2769 /* '@' */
2770 tok_len = 1;
2771 tok_type = LYXP_TOKEN_AT;
2772
Radek Krejcif03a9e22020-09-18 20:09:31 +02002773 } else if (expr_str[parsed] == ',') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002774
2775 /* ',' */
2776 tok_len = 1;
2777 tok_type = LYXP_TOKEN_COMMA;
2778
Radek Krejcif03a9e22020-09-18 20:09:31 +02002779 } else if (expr_str[parsed] == '\'') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002780
2781 /* Literal with ' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002782 for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\''); ++tok_len) {}
2783 LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0',
Radek Krejci2efc45b2020-12-22 16:25:44 +01002784 LOGVAL(ctx, LY_VCODE_XP_EOE, expr_str[parsed], &expr_str[parsed]); ret = LY_EVALID,
Michal Vasko69730152020-10-09 16:30:07 +02002785 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002786 ++tok_len;
2787 tok_type = LYXP_TOKEN_LITERAL;
2788
Radek Krejcif03a9e22020-09-18 20:09:31 +02002789 } else if (expr_str[parsed] == '\"') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002790
2791 /* Literal with " */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002792 for (tok_len = 1; (expr_str[parsed + tok_len] != '\0') && (expr_str[parsed + tok_len] != '\"'); ++tok_len) {}
2793 LY_CHECK_ERR_GOTO(expr_str[parsed + tok_len] == '\0',
Radek Krejci2efc45b2020-12-22 16:25:44 +01002794 LOGVAL(ctx, LY_VCODE_XP_EOE, expr_str[parsed], &expr_str[parsed]); ret = LY_EVALID,
Michal Vasko69730152020-10-09 16:30:07 +02002795 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002796 ++tok_len;
2797 tok_type = LYXP_TOKEN_LITERAL;
2798
Radek Krejcif03a9e22020-09-18 20:09:31 +02002799 } else if ((expr_str[parsed] == '.') || (isdigit(expr_str[parsed]))) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002800
2801 /* Number */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002802 for (tok_len = 0; isdigit(expr_str[parsed + tok_len]); ++tok_len) {}
2803 if (expr_str[parsed + tok_len] == '.') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002804 ++tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02002805 for ( ; isdigit(expr_str[parsed + tok_len]); ++tok_len) {}
Radek Krejcib1646a92018-11-02 16:08:26 +01002806 }
2807 tok_type = LYXP_TOKEN_NUMBER;
2808
Radek Krejcif03a9e22020-09-18 20:09:31 +02002809 } else if (expr_str[parsed] == '/') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002810
2811 /* Operator '/', '//' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002812 if (!strncmp(&expr_str[parsed], "//", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002813 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002814 tok_type = LYXP_TOKEN_OPER_RPATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002815 } else {
2816 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002817 tok_type = LYXP_TOKEN_OPER_PATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002818 }
Radek Krejcib1646a92018-11-02 16:08:26 +01002819
Radek Krejcif03a9e22020-09-18 20:09:31 +02002820 } else if (!strncmp(&expr_str[parsed], "!=", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002821
Michal Vasko3e48bf32020-06-01 08:39:07 +02002822 /* Operator '!=' */
Radek Krejcib1646a92018-11-02 16:08:26 +01002823 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002824 tok_type = LYXP_TOKEN_OPER_NEQUAL;
2825
Radek Krejcif03a9e22020-09-18 20:09:31 +02002826 } else if (!strncmp(&expr_str[parsed], "<=", 2) || !strncmp(&expr_str[parsed], ">=", 2)) {
Michal Vasko3e48bf32020-06-01 08:39:07 +02002827
2828 /* Operator '<=', '>=' */
2829 tok_len = 2;
2830 tok_type = LYXP_TOKEN_OPER_COMP;
Radek Krejcib1646a92018-11-02 16:08:26 +01002831
Radek Krejcif03a9e22020-09-18 20:09:31 +02002832 } else if (expr_str[parsed] == '|') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002833
2834 /* Operator '|' */
2835 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002836 tok_type = LYXP_TOKEN_OPER_UNI;
Radek Krejcib1646a92018-11-02 16:08:26 +01002837
Radek Krejcif03a9e22020-09-18 20:09:31 +02002838 } else if ((expr_str[parsed] == '+') || (expr_str[parsed] == '-')) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002839
2840 /* Operator '+', '-' */
2841 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002842 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002843
Radek Krejcif03a9e22020-09-18 20:09:31 +02002844 } else if (expr_str[parsed] == '=') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002845
Michal Vasko3e48bf32020-06-01 08:39:07 +02002846 /* Operator '=' */
Radek Krejcib1646a92018-11-02 16:08:26 +01002847 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002848 tok_type = LYXP_TOKEN_OPER_EQUAL;
2849
Radek Krejcif03a9e22020-09-18 20:09:31 +02002850 } else if ((expr_str[parsed] == '<') || (expr_str[parsed] == '>')) {
Michal Vasko3e48bf32020-06-01 08:39:07 +02002851
2852 /* Operator '<', '>' */
2853 tok_len = 1;
2854 tok_type = LYXP_TOKEN_OPER_COMP;
Radek Krejcib1646a92018-11-02 16:08:26 +01002855
Michal Vasko69730152020-10-09 16:30:07 +02002856 } else if (expr->used && (expr->tokens[expr->used - 1] != LYXP_TOKEN_AT) &&
2857 (expr->tokens[expr->used - 1] != LYXP_TOKEN_PAR1) &&
2858 (expr->tokens[expr->used - 1] != LYXP_TOKEN_BRACK1) &&
2859 (expr->tokens[expr->used - 1] != LYXP_TOKEN_COMMA) &&
2860 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_LOG) &&
2861 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_EQUAL) &&
2862 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_NEQUAL) &&
2863 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_COMP) &&
2864 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_MATH) &&
2865 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_UNI) &&
2866 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_PATH) &&
2867 (expr->tokens[expr->used - 1] != LYXP_TOKEN_OPER_RPATH)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002868
2869 /* Operator '*', 'or', 'and', 'mod', or 'div' */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002870 if (expr_str[parsed] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002871 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002872 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002873
Radek Krejcif03a9e22020-09-18 20:09:31 +02002874 } else if (!strncmp(&expr_str[parsed], "or", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002875 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002876 tok_type = LYXP_TOKEN_OPER_LOG;
Radek Krejcib1646a92018-11-02 16:08:26 +01002877
Radek Krejcif03a9e22020-09-18 20:09:31 +02002878 } else if (!strncmp(&expr_str[parsed], "and", 3)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002879 tok_len = 3;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002880 tok_type = LYXP_TOKEN_OPER_LOG;
Radek Krejcib1646a92018-11-02 16:08:26 +01002881
Radek Krejcif03a9e22020-09-18 20:09:31 +02002882 } else if (!strncmp(&expr_str[parsed], "mod", 3) || !strncmp(&expr_str[parsed], "div", 3)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002883 tok_len = 3;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002884 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002885
2886 } else if (prev_function_check) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002887 LOGVAL(ctx, LYVE_XPATH, "Invalid character 0x%x ('%c'), perhaps \"%.*s\" is supposed to be a function call.",
Michal Vasko69730152020-10-09 16:30:07 +02002888 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 +02002889 ret = LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01002890 goto error;
2891 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002892 LOGVAL(ctx, LY_VCODE_XP_INEXPR, parsed + 1, expr_str);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002893 ret = LY_EVALID;
Radek Krejcib1646a92018-11-02 16:08:26 +01002894 goto error;
2895 }
Radek Krejcif03a9e22020-09-18 20:09:31 +02002896 } else if (expr_str[parsed] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002897
2898 /* NameTest '*' */
2899 tok_len = 1;
2900 tok_type = LYXP_TOKEN_NAMETEST;
2901
2902 } else {
2903
2904 /* NameTest (NCName ':' '*' | QName) or NodeType/FunctionName */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002905 long int ncname_len = parse_ncname(&expr_str[parsed]);
2906 LY_CHECK_ERR_GOTO(ncname_len < 0,
Radek Krejci2efc45b2020-12-22 16:25:44 +01002907 LOGVAL(ctx, LY_VCODE_XP_INEXPR, parsed - ncname_len + 1, expr_str); ret = LY_EVALID,
Michal Vasko69730152020-10-09 16:30:07 +02002908 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002909 tok_len = ncname_len;
2910
Radek Krejcif03a9e22020-09-18 20:09:31 +02002911 if (expr_str[parsed + tok_len] == ':') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002912 ++tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02002913 if (expr_str[parsed + tok_len] == '*') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002914 ++tok_len;
2915 } else {
Radek Krejcif03a9e22020-09-18 20:09:31 +02002916 ncname_len = parse_ncname(&expr_str[parsed + tok_len]);
2917 LY_CHECK_ERR_GOTO(ncname_len < 0,
Radek Krejci2efc45b2020-12-22 16:25:44 +01002918 LOGVAL(ctx, LY_VCODE_XP_INEXPR, parsed - ncname_len + 1, expr_str); ret = LY_EVALID,
Michal Vasko69730152020-10-09 16:30:07 +02002919 error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002920 tok_len += ncname_len;
2921 }
2922 /* remove old flag to prevent ambiguities */
2923 prev_function_check = 0;
2924 tok_type = LYXP_TOKEN_NAMETEST;
2925 } else {
2926 /* there is no prefix so it can still be NodeType/FunctionName, we can't finally decide now */
2927 prev_function_check = 1;
2928 tok_type = LYXP_TOKEN_NAMETEST;
2929 }
2930 }
2931
2932 /* store the token, move on to the next one */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002933 LY_CHECK_GOTO(ret = exp_add_token(ctx, expr, tok_type, parsed, tok_len), error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002934 parsed += tok_len;
Radek Krejcif03a9e22020-09-18 20:09:31 +02002935 while (is_xmlws(expr_str[parsed])) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002936 ++parsed;
2937 }
2938
Radek Krejcif03a9e22020-09-18 20:09:31 +02002939 } while (expr_str[parsed]);
Radek Krejcib1646a92018-11-02 16:08:26 +01002940
Michal Vasko004d3152020-06-11 19:59:22 +02002941 if (reparse) {
2942 /* prealloc repeat */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002943 expr->repeat = calloc(expr->size, sizeof *expr->repeat);
2944 LY_CHECK_ERR_GOTO(!expr->repeat, LOGMEM(ctx); ret = LY_EMEM, error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002945
Michal Vasko004d3152020-06-11 19:59:22 +02002946 /* fill repeat */
Radek Krejcif03a9e22020-09-18 20:09:31 +02002947 LY_CHECK_ERR_GOTO(reparse_or_expr(ctx, expr, &tok_idx), ret = LY_EVALID, error);
2948 if (expr->used > tok_idx) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002949 LOGVAL(ctx, LYVE_XPATH, "Unparsed characters \"%s\" left at the end of an XPath expression.",
Michal Vasko69730152020-10-09 16:30:07 +02002950 &expr->expr[expr->tok_pos[tok_idx]]);
Radek Krejcif03a9e22020-09-18 20:09:31 +02002951 ret = LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02002952 goto error;
2953 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02002954 }
2955
Radek Krejcif03a9e22020-09-18 20:09:31 +02002956 print_expr_struct_debug(expr);
2957 *expr_p = expr;
2958 return LY_SUCCESS;
Radek Krejcib1646a92018-11-02 16:08:26 +01002959
2960error:
Radek Krejcif03a9e22020-09-18 20:09:31 +02002961 lyxp_expr_free(ctx, expr);
2962 return ret;
Radek Krejcib1646a92018-11-02 16:08:26 +01002963}
2964
Michal Vasko1734be92020-09-22 08:55:10 +02002965LY_ERR
2966lyxp_expr_dup(const struct ly_ctx *ctx, const struct lyxp_expr *exp, struct lyxp_expr **dup_p)
Michal Vasko004d3152020-06-11 19:59:22 +02002967{
Michal Vasko1734be92020-09-22 08:55:10 +02002968 LY_ERR ret = LY_SUCCESS;
2969 struct lyxp_expr *dup = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02002970 uint32_t i, j;
2971
Michal Vasko7f45cf22020-10-01 12:49:44 +02002972 if (!exp) {
2973 goto cleanup;
2974 }
2975
Michal Vasko004d3152020-06-11 19:59:22 +02002976 dup = calloc(1, sizeof *dup);
Michal Vasko1734be92020-09-22 08:55:10 +02002977 LY_CHECK_ERR_GOTO(!dup, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02002978
2979 dup->tokens = malloc(exp->used * sizeof *dup->tokens);
Michal Vasko1734be92020-09-22 08:55:10 +02002980 LY_CHECK_ERR_GOTO(!dup->tokens, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02002981 memcpy(dup->tokens, exp->tokens, exp->used * sizeof *dup->tokens);
2982
2983 dup->tok_pos = malloc(exp->used * sizeof *dup->tok_pos);
Michal Vasko1734be92020-09-22 08:55:10 +02002984 LY_CHECK_ERR_GOTO(!dup->tok_pos, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02002985 memcpy(dup->tok_pos, exp->tok_pos, exp->used * sizeof *dup->tok_pos);
2986
2987 dup->tok_len = malloc(exp->used * sizeof *dup->tok_len);
Michal Vasko1734be92020-09-22 08:55:10 +02002988 LY_CHECK_ERR_GOTO(!dup->tok_len, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02002989 memcpy(dup->tok_len, exp->tok_len, exp->used * sizeof *dup->tok_len);
2990
2991 dup->repeat = malloc(exp->used * sizeof *dup->repeat);
Michal Vasko1734be92020-09-22 08:55:10 +02002992 LY_CHECK_ERR_GOTO(!dup->repeat, LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02002993 for (i = 0; i < exp->used; ++i) {
2994 if (!exp->repeat[i]) {
2995 dup->repeat[i] = NULL;
2996 } else {
Radek Krejci1e008d22020-08-17 11:37:37 +02002997 for (j = 0; exp->repeat[i][j]; ++j) {}
Michal Vasko004d3152020-06-11 19:59:22 +02002998 /* the ending 0 as well */
2999 ++j;
3000
Michal Vasko99c71642020-07-03 13:33:36 +02003001 dup->repeat[i] = malloc(j * sizeof **dup->repeat);
Michal Vasko1734be92020-09-22 08:55:10 +02003002 LY_CHECK_ERR_GOTO(!dup->repeat[i], LOGMEM(ctx); ret = LY_EMEM, cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02003003 memcpy(dup->repeat[i], exp->repeat[i], j * sizeof **dup->repeat);
3004 dup->repeat[i][j - 1] = 0;
3005 }
3006 }
3007
3008 dup->used = exp->used;
3009 dup->size = exp->used;
Michal Vasko1734be92020-09-22 08:55:10 +02003010 LY_CHECK_GOTO(ret = lydict_insert(ctx, exp->expr, 0, &dup->expr), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02003011
Michal Vasko1734be92020-09-22 08:55:10 +02003012cleanup:
3013 if (ret) {
3014 lyxp_expr_free(ctx, dup);
3015 } else {
3016 *dup_p = dup;
3017 }
3018 return ret;
Michal Vasko004d3152020-06-11 19:59:22 +02003019}
3020
Michal Vasko03ff5a72019-09-11 13:49:33 +02003021/*
3022 * warn functions
3023 *
3024 * Warn functions check specific reasonable conditions for schema XPath
3025 * and print a warning if they are not satisfied.
3026 */
3027
3028/**
3029 * @brief Get the last-added schema node that is currently in the context.
3030 *
3031 * @param[in] set Set to search in.
3032 * @return Last-added schema context node, NULL if no node is in context.
3033 */
3034static struct lysc_node *
3035warn_get_scnode_in_ctx(struct lyxp_set *set)
3036{
3037 uint32_t i;
3038
3039 if (!set || (set->type != LYXP_SET_SCNODE_SET)) {
3040 return NULL;
3041 }
3042
3043 i = set->used;
3044 do {
3045 --i;
Radek Krejcif13b87b2020-12-01 22:02:17 +01003046 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003047 /* if there are more, simply return the first found (last added) */
3048 return set->val.scnodes[i].scnode;
3049 }
3050 } while (i);
3051
3052 return NULL;
3053}
3054
3055/**
3056 * @brief Test whether a type is numeric - integer type or decimal64.
3057 *
3058 * @param[in] type Type to test.
Radek Krejci857189e2020-09-01 13:26:36 +02003059 * @return Boolean value whether @p type is numeric type or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003060 */
Radek Krejci857189e2020-09-01 13:26:36 +02003061static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003062warn_is_numeric_type(struct lysc_type *type)
3063{
3064 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003065 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003066 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003067
3068 switch (type->basetype) {
3069 case LY_TYPE_DEC64:
3070 case LY_TYPE_INT8:
3071 case LY_TYPE_UINT8:
3072 case LY_TYPE_INT16:
3073 case LY_TYPE_UINT16:
3074 case LY_TYPE_INT32:
3075 case LY_TYPE_UINT32:
3076 case LY_TYPE_INT64:
3077 case LY_TYPE_UINT64:
3078 return 1;
3079 case LY_TYPE_UNION:
3080 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003081 LY_ARRAY_FOR(uni->types, u) {
3082 ret = warn_is_numeric_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003083 if (ret) {
3084 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003085 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003086 }
3087 }
3088 /* did not find any suitable type */
3089 return 0;
3090 case LY_TYPE_LEAFREF:
3091 return warn_is_numeric_type(((struct lysc_type_leafref *)type)->realtype);
3092 default:
3093 return 0;
3094 }
3095}
3096
3097/**
3098 * @brief Test whether a type is string-like - no integers, decimal64 or binary.
3099 *
3100 * @param[in] type Type to test.
Radek Krejci857189e2020-09-01 13:26:36 +02003101 * @return Boolean value whether @p type's basetype is string type or not.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003102 */
Radek Krejci857189e2020-09-01 13:26:36 +02003103static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003104warn_is_string_type(struct lysc_type *type)
3105{
3106 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003107 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003108 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003109
3110 switch (type->basetype) {
3111 case LY_TYPE_BITS:
3112 case LY_TYPE_ENUM:
3113 case LY_TYPE_IDENT:
3114 case LY_TYPE_INST:
3115 case LY_TYPE_STRING:
3116 return 1;
3117 case LY_TYPE_UNION:
3118 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003119 LY_ARRAY_FOR(uni->types, u) {
3120 ret = warn_is_string_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003121 if (ret) {
3122 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003123 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003124 }
3125 }
3126 /* did not find any suitable type */
3127 return 0;
3128 case LY_TYPE_LEAFREF:
3129 return warn_is_string_type(((struct lysc_type_leafref *)type)->realtype);
3130 default:
3131 return 0;
3132 }
3133}
3134
3135/**
3136 * @brief Test whether a type is one specific type.
3137 *
3138 * @param[in] type Type to test.
3139 * @param[in] base Expected type.
Radek Krejci857189e2020-09-01 13:26:36 +02003140 * @return Boolean value whether the given @p type is of the specific basetype @p base.
Michal Vasko03ff5a72019-09-11 13:49:33 +02003141 */
Radek Krejci857189e2020-09-01 13:26:36 +02003142static ly_bool
Michal Vasko03ff5a72019-09-11 13:49:33 +02003143warn_is_specific_type(struct lysc_type *type, LY_DATA_TYPE base)
3144{
3145 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003146 ly_bool ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003147 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003148
3149 if (type->basetype == base) {
3150 return 1;
3151 } else if (type->basetype == LY_TYPE_UNION) {
3152 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003153 LY_ARRAY_FOR(uni->types, u) {
3154 ret = warn_is_specific_type(uni->types[u], base);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003155 if (ret) {
3156 /* found a suitable type */
Radek Krejci857189e2020-09-01 13:26:36 +02003157 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003158 }
3159 }
3160 /* did not find any suitable type */
3161 return 0;
3162 } else if (type->basetype == LY_TYPE_LEAFREF) {
3163 return warn_is_specific_type(((struct lysc_type_leafref *)type)->realtype, base);
3164 }
3165
3166 return 0;
3167}
3168
3169/**
3170 * @brief Get next type of a (union) type.
3171 *
3172 * @param[in] type Base type.
3173 * @param[in] prev_type Previously returned type.
3174 * @return Next type or NULL.
3175 */
3176static struct lysc_type *
3177warn_is_equal_type_next_type(struct lysc_type *type, struct lysc_type *prev_type)
3178{
3179 struct lysc_type_union *uni;
Radek Krejci857189e2020-09-01 13:26:36 +02003180 ly_bool found = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003181 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003182
3183 switch (type->basetype) {
3184 case LY_TYPE_UNION:
3185 uni = (struct lysc_type_union *)type;
3186 if (!prev_type) {
3187 return uni->types[0];
3188 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003189 LY_ARRAY_FOR(uni->types, u) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003190 if (found) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003191 return uni->types[u];
Michal Vasko03ff5a72019-09-11 13:49:33 +02003192 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003193 if (prev_type == uni->types[u]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003194 found = 1;
3195 }
3196 }
3197 return NULL;
3198 default:
3199 if (prev_type) {
3200 assert(type == prev_type);
3201 return NULL;
3202 } else {
3203 return type;
3204 }
3205 }
3206}
3207
3208/**
3209 * @brief Test whether 2 types have a common type.
3210 *
3211 * @param[in] type1 First type.
3212 * @param[in] type2 Second type.
3213 * @return 1 if they do, 0 otherwise.
3214 */
3215static int
3216warn_is_equal_type(struct lysc_type *type1, struct lysc_type *type2)
3217{
3218 struct lysc_type *t1, *rt1, *t2, *rt2;
3219
3220 t1 = NULL;
3221 while ((t1 = warn_is_equal_type_next_type(type1, t1))) {
3222 if (t1->basetype == LY_TYPE_LEAFREF) {
3223 rt1 = ((struct lysc_type_leafref *)t1)->realtype;
3224 } else {
3225 rt1 = t1;
3226 }
3227
3228 t2 = NULL;
3229 while ((t2 = warn_is_equal_type_next_type(type2, t2))) {
3230 if (t2->basetype == LY_TYPE_LEAFREF) {
3231 rt2 = ((struct lysc_type_leafref *)t2)->realtype;
3232 } else {
3233 rt2 = t2;
3234 }
3235
3236 if (rt2->basetype == rt1->basetype) {
3237 /* match found */
3238 return 1;
3239 }
3240 }
3241 }
3242
3243 return 0;
3244}
3245
3246/**
3247 * @brief Check both operands of comparison operators.
3248 *
3249 * @param[in] ctx Context for errors.
3250 * @param[in] set1 First operand set.
3251 * @param[in] set2 Second operand set.
3252 * @param[in] numbers_only Whether accept only numbers or other types are fine too (for '=' and '!=').
3253 * @param[in] expr Start of the expression to print with the warning.
3254 * @param[in] tok_pos Token position.
3255 */
3256static void
Radek Krejci857189e2020-09-01 13:26:36 +02003257warn_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 +02003258{
3259 struct lysc_node_leaf *node1, *node2;
Radek Krejci857189e2020-09-01 13:26:36 +02003260 ly_bool leaves = 1, warning = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003261
3262 node1 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set1);
3263 node2 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set2);
3264
3265 if (!node1 && !node2) {
3266 /* no node-sets involved, nothing to do */
3267 return;
3268 }
3269
3270 if (node1) {
3271 if (!(node1->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3272 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node1->nodetype), node1->name);
3273 warning = 1;
3274 leaves = 0;
3275 } else if (numbers_only && !warn_is_numeric_type(node1->type)) {
3276 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node1->name);
3277 warning = 1;
3278 }
3279 }
3280
3281 if (node2) {
3282 if (!(node2->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3283 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node2->nodetype), node2->name);
3284 warning = 1;
3285 leaves = 0;
3286 } else if (numbers_only && !warn_is_numeric_type(node2->type)) {
3287 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node2->name);
3288 warning = 1;
3289 }
3290 }
3291
3292 if (node1 && node2 && leaves && !numbers_only) {
Michal Vasko69730152020-10-09 16:30:07 +02003293 if ((warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type)) ||
3294 (!warn_is_numeric_type(node1->type) && warn_is_numeric_type(node2->type)) ||
3295 (!warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type) &&
3296 !warn_is_equal_type(node1->type, node2->type))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003297 LOGWRN(ctx, "Incompatible types of operands \"%s\" and \"%s\" for comparison.", node1->name, node2->name);
3298 warning = 1;
3299 }
3300 }
3301
3302 if (warning) {
3303 LOGWRN(ctx, "Previous warning generated by XPath subexpression[%u] \"%.20s\".", tok_pos, expr + tok_pos);
3304 }
3305}
3306
3307/**
3308 * @brief Check that a value is valid for a leaf. If not applicable, does nothing.
3309 *
3310 * @param[in] exp Parsed XPath expression.
3311 * @param[in] set Set with the leaf/leaf-list.
3312 * @param[in] val_exp Index of the value (literal/number) in @p exp.
3313 * @param[in] equal_exp Index of the start of the equality expression in @p exp.
3314 * @param[in] last_equal_exp Index of the end of the equality expression in @p exp.
3315 */
3316static void
Michal Vasko40308e72020-10-20 16:38:40 +02003317warn_equality_value(const struct lyxp_expr *exp, struct lyxp_set *set, uint16_t val_exp, uint16_t equal_exp,
3318 uint16_t last_equal_exp)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003319{
3320 struct lysc_node *scnode;
3321 struct lysc_type *type;
3322 char *value;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003323 struct lyd_value storage;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003324 LY_ERR rc;
3325 struct ly_err_item *err = NULL;
3326
Michal Vasko69730152020-10-09 16:30:07 +02003327 if ((scnode = warn_get_scnode_in_ctx(set)) && (scnode->nodetype & (LYS_LEAF | LYS_LEAFLIST)) &&
3328 ((exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) || (exp->tokens[val_exp] == LYXP_TOKEN_NUMBER))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003329 /* check that the node can have the specified value */
3330 if (exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) {
3331 value = strndup(exp->expr + exp->tok_pos[val_exp] + 1, exp->tok_len[val_exp] - 2);
3332 } else {
3333 value = strndup(exp->expr + exp->tok_pos[val_exp], exp->tok_len[val_exp]);
3334 }
3335 if (!value) {
3336 LOGMEM(set->ctx);
3337 return;
3338 }
3339
3340 if ((((struct lysc_node_leaf *)scnode)->type->basetype == LY_TYPE_IDENT) && !strchr(value, ':')) {
3341 LOGWRN(set->ctx, "Identityref \"%s\" comparison with identity \"%s\" without prefix, consider adding"
Michal Vasko69730152020-10-09 16:30:07 +02003342 " a prefix or best using \"derived-from(-or-self)()\" functions.", scnode->name, value);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003343 LOGWRN(set->ctx, "Previous warning generated by XPath subexpression[%u] \"%.*s\".", exp->tok_pos[equal_exp],
Radek Krejci0f969882020-08-21 16:56:47 +02003344 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
Michal Vasko69730152020-10-09 16:30:07 +02003345 exp->expr + exp->tok_pos[equal_exp]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003346 }
3347
3348 type = ((struct lysc_node_leaf *)scnode)->type;
3349 if (type->basetype != LY_TYPE_IDENT) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003350 rc = type->plugin->store(set->ctx, type, value, strlen(value), 0, set->format, set->prefix_data,
Michal Vasko405cc9e2020-12-01 12:01:27 +01003351 LYD_HINT_DATA, scnode, &storage, NULL, &err);
Michal Vaskobf42e832020-11-23 16:59:42 +01003352 if (rc == LY_EINCOMPLETE) {
3353 rc = LY_SUCCESS;
3354 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003355
3356 if (err) {
3357 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type (%s).", value, err->msg);
3358 ly_err_free(err);
3359 } else if (rc != LY_SUCCESS) {
3360 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type.", value);
3361 }
3362 if (rc != LY_SUCCESS) {
3363 LOGWRN(set->ctx, "Previous warning generated by XPath subexpression[%u] \"%.*s\".", exp->tok_pos[equal_exp],
Radek Krejci0f969882020-08-21 16:56:47 +02003364 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
Michal Vasko69730152020-10-09 16:30:07 +02003365 exp->expr + exp->tok_pos[equal_exp]);
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003366 } else {
3367 type->plugin->free(set->ctx, &storage);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003368 }
3369 }
3370 free(value);
3371 }
3372}
3373
3374/*
3375 * XPath functions
3376 */
3377
3378/**
3379 * @brief Execute the YANG 1.1 bit-is-set(node-set, string) function. Returns LYXP_SET_BOOLEAN
3380 * depending on whether the first node bit value from the second argument is set.
3381 *
3382 * @param[in] args Array of arguments.
3383 * @param[in] arg_count Count of elements in @p args.
3384 * @param[in,out] set Context and result set at the same time.
3385 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003386 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003387 */
3388static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003389xpath_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 +02003390{
3391 struct lyd_node_term *leaf;
3392 struct lysc_node_leaf *sleaf;
3393 struct lysc_type_bits *bits;
3394 LY_ERR rc = LY_SUCCESS;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003395 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003396
3397 if (options & LYXP_SCNODE_ALL) {
3398 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3399 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003400 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3401 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 +02003402 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_BITS)) {
3403 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"bits\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003404 }
3405
3406 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3407 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3408 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 +02003409 } else if (!warn_is_string_type(sleaf->type)) {
3410 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003411 }
3412 }
3413 set_scnode_clear_ctx(set);
3414 return rc;
3415 }
3416
Michal Vaskod3678892020-05-21 10:06:58 +02003417 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003418 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "bit-is-set(node-set, string)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003419 return LY_EVALID;
3420 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003421 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003422 LY_CHECK_RET(rc);
3423
3424 set_fill_boolean(set, 0);
Michal Vaskod3678892020-05-21 10:06:58 +02003425 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003426 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
Michal Vasko69730152020-10-09 16:30:07 +02003427 if ((leaf->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) &&
3428 (((struct lysc_node_leaf *)leaf->schema)->type->basetype == LY_TYPE_BITS)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003429 bits = (struct lysc_type_bits *)((struct lysc_node_leaf *)leaf->schema)->type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003430 LY_ARRAY_FOR(bits->bits, u) {
3431 if (!strcmp(bits->bits[u].name, args[1]->val.str)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003432 set_fill_boolean(set, 1);
3433 break;
3434 }
3435 }
3436 }
3437 }
3438
3439 return LY_SUCCESS;
3440}
3441
3442/**
3443 * @brief Execute the XPath boolean(object) function. Returns LYXP_SET_BOOLEAN
3444 * with the argument converted to boolean.
3445 *
3446 * @param[in] args Array of arguments.
3447 * @param[in] arg_count Count of elements in @p args.
3448 * @param[in,out] set Context and result set at the same time.
3449 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003450 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003451 */
3452static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003453xpath_boolean(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003454{
3455 LY_ERR rc;
3456
3457 if (options & LYXP_SCNODE_ALL) {
3458 set_scnode_clear_ctx(set);
3459 return LY_SUCCESS;
3460 }
3461
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003462 rc = lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003463 LY_CHECK_RET(rc);
3464 set_fill_set(set, args[0]);
3465
3466 return LY_SUCCESS;
3467}
3468
3469/**
3470 * @brief Execute the XPath ceiling(number) function. Returns LYXP_SET_NUMBER
3471 * with the first argument rounded up to the nearest integer.
3472 *
3473 * @param[in] args Array of arguments.
3474 * @param[in] arg_count Count of elements in @p args.
3475 * @param[in,out] set Context and result set at the same time.
3476 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003477 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003478 */
3479static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003480xpath_ceiling(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003481{
3482 struct lysc_node_leaf *sleaf;
3483 LY_ERR rc = LY_SUCCESS;
3484
3485 if (options & LYXP_SCNODE_ALL) {
3486 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3487 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003488 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3489 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 +02003490 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
3491 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003492 }
3493 set_scnode_clear_ctx(set);
3494 return rc;
3495 }
3496
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003497 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003498 LY_CHECK_RET(rc);
3499 if ((long long)args[0]->val.num != args[0]->val.num) {
3500 set_fill_number(set, ((long long)args[0]->val.num) + 1);
3501 } else {
3502 set_fill_number(set, args[0]->val.num);
3503 }
3504
3505 return LY_SUCCESS;
3506}
3507
3508/**
3509 * @brief Execute the XPath concat(string, string, string*) function.
3510 * Returns LYXP_SET_STRING with the concatenation of all the arguments.
3511 *
3512 * @param[in] args Array of arguments.
3513 * @param[in] arg_count Count of elements in @p args.
3514 * @param[in,out] set Context and result set at the same time.
3515 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003516 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003517 */
3518static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003519xpath_concat(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003520{
3521 uint16_t i;
3522 char *str = NULL;
3523 size_t used = 1;
3524 LY_ERR rc = LY_SUCCESS;
3525 struct lysc_node_leaf *sleaf;
3526
3527 if (options & LYXP_SCNODE_ALL) {
3528 for (i = 0; i < arg_count; ++i) {
3529 if ((args[i]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[i]))) {
3530 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3531 LOGWRN(set->ctx, "Argument #%u of %s is a %s node \"%s\".",
Michal Vasko69730152020-10-09 16:30:07 +02003532 i + 1, __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003533 } else if (!warn_is_string_type(sleaf->type)) {
Radek Krejci70124c82020-08-14 22:17:03 +02003534 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 +02003535 }
3536 }
3537 }
3538 set_scnode_clear_ctx(set);
3539 return rc;
3540 }
3541
3542 for (i = 0; i < arg_count; ++i) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003543 rc = lyxp_set_cast(args[i], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003544 if (rc != LY_SUCCESS) {
3545 free(str);
3546 return rc;
3547 }
3548
3549 str = ly_realloc(str, (used + strlen(args[i]->val.str)) * sizeof(char));
3550 LY_CHECK_ERR_RET(!str, LOGMEM(set->ctx), LY_EMEM);
3551 strcpy(str + used - 1, args[i]->val.str);
3552 used += strlen(args[i]->val.str);
3553 }
3554
3555 /* free, kind of */
Michal Vaskod3678892020-05-21 10:06:58 +02003556 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003557 set->type = LYXP_SET_STRING;
3558 set->val.str = str;
3559
3560 return LY_SUCCESS;
3561}
3562
3563/**
3564 * @brief Execute the XPath contains(string, string) function.
3565 * Returns LYXP_SET_BOOLEAN whether the second argument can
3566 * be found in the first or not.
3567 *
3568 * @param[in] args Array of arguments.
3569 * @param[in] arg_count Count of elements in @p args.
3570 * @param[in,out] set Context and result set at the same time.
3571 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003572 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003573 */
3574static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003575xpath_contains(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003576{
3577 struct lysc_node_leaf *sleaf;
3578 LY_ERR rc = LY_SUCCESS;
3579
3580 if (options & LYXP_SCNODE_ALL) {
3581 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3582 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3583 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 +02003584 } else if (!warn_is_string_type(sleaf->type)) {
3585 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003586 }
3587 }
3588
3589 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3590 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3591 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 +02003592 } else if (!warn_is_string_type(sleaf->type)) {
3593 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003594 }
3595 }
3596 set_scnode_clear_ctx(set);
3597 return rc;
3598 }
3599
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003600 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003601 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003602 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003603 LY_CHECK_RET(rc);
3604
3605 if (strstr(args[0]->val.str, args[1]->val.str)) {
3606 set_fill_boolean(set, 1);
3607 } else {
3608 set_fill_boolean(set, 0);
3609 }
3610
3611 return LY_SUCCESS;
3612}
3613
3614/**
3615 * @brief Execute the XPath count(node-set) function. Returns LYXP_SET_NUMBER
3616 * with the size of the node-set from the argument.
3617 *
3618 * @param[in] args Array of arguments.
3619 * @param[in] arg_count Count of elements in @p args.
3620 * @param[in,out] set Context and result set at the same time.
3621 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003622 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003623 */
3624static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003625xpath_count(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003626{
3627 struct lysc_node *scnode = NULL;
3628 LY_ERR rc = LY_SUCCESS;
3629
3630 if (options & LYXP_SCNODE_ALL) {
3631 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(scnode = warn_get_scnode_in_ctx(args[0]))) {
3632 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003633 }
3634 set_scnode_clear_ctx(set);
3635 return rc;
3636 }
3637
Michal Vasko03ff5a72019-09-11 13:49:33 +02003638 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003639 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "count(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003640 return LY_EVALID;
3641 }
3642
3643 set_fill_number(set, args[0]->used);
3644 return LY_SUCCESS;
3645}
3646
3647/**
3648 * @brief Execute the XPath current() function. Returns LYXP_SET_NODE_SET
3649 * with the context with the intial node.
3650 *
3651 * @param[in] args Array of arguments.
3652 * @param[in] arg_count Count of elements in @p args.
3653 * @param[in,out] set Context and result set at the same time.
3654 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003655 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003656 */
3657static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003658xpath_current(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003659{
3660 if (arg_count || args) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003661 LOGVAL(set->ctx, LY_VCODE_XP_INARGCOUNT, arg_count, "current()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003662 return LY_EVALID;
3663 }
3664
3665 if (options & LYXP_SCNODE_ALL) {
3666 set_scnode_clear_ctx(set);
3667
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003668 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, set->cur_scnode, LYXP_NODE_ELEM, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02003669 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02003670 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003671
3672 /* position is filled later */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02003673 set_insert_node(set, set->cur_node, 0, LYXP_NODE_ELEM, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003674 }
3675
3676 return LY_SUCCESS;
3677}
3678
3679/**
3680 * @brief Execute the YANG 1.1 deref(node-set) function. Returns LYXP_SET_NODE_SET with either
3681 * leafref or instance-identifier target node(s).
3682 *
3683 * @param[in] args Array of arguments.
3684 * @param[in] arg_count Count of elements in @p args.
3685 * @param[in,out] set Context and result set at the same time.
3686 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003687 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003688 */
3689static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003690xpath_deref(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003691{
3692 struct lyd_node_term *leaf;
Michal Vasko42e497c2020-01-06 08:38:25 +01003693 struct lysc_node_leaf *sleaf = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02003694 struct lysc_type_leafref *lref;
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003695 const struct lysc_node *target;
Michal Vasko004d3152020-06-11 19:59:22 +02003696 struct ly_path *p;
3697 struct lyd_node *node;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003698 char *errmsg = NULL;
Michal Vasko00cbf532020-06-15 13:58:47 +02003699 uint8_t oper;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003700 LY_ERR rc = LY_SUCCESS;
3701
3702 if (options & LYXP_SCNODE_ALL) {
3703 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3704 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003705 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3706 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 +02003707 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_LEAFREF) && !warn_is_specific_type(sleaf->type, LY_TYPE_INST)) {
3708 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"leafref\" nor \"instance-identifier\".",
Michal Vasko69730152020-10-09 16:30:07 +02003709 __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003710 }
3711 set_scnode_clear_ctx(set);
Michal Vasko42e497c2020-01-06 08:38:25 +01003712 if (sleaf && (sleaf->type->basetype == LY_TYPE_LEAFREF)) {
Michal Vasko004d3152020-06-11 19:59:22 +02003713 lref = (struct lysc_type_leafref *)sleaf->type;
Michal Vaskod1e53b92021-01-28 13:11:06 +01003714 oper = (sleaf->flags & LYS_IS_OUTPUT) ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT;
Michal Vasko004d3152020-06-11 19:59:22 +02003715
3716 /* it was already evaluated on schema, it must succeed */
Radek Krejcid5d37432021-03-12 13:46:40 +01003717 rc = ly_path_compile(set->ctx, lref->cur_mod, &sleaf->node, NULL, lref->path, LY_PATH_LREF_TRUE, oper,
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003718 LY_PATH_TARGET_MANY, LY_PREF_SCHEMA_RESOLVED, lref->prefixes, NULL, &p);
Michal Vasko004d3152020-06-11 19:59:22 +02003719 assert(!rc);
3720
3721 /* get the target node */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003722 target = p[LY_ARRAY_COUNT(p) - 1].node;
Michal Vasko004d3152020-06-11 19:59:22 +02003723 ly_path_free(set->ctx, p);
3724
Radek Krejciaa6b53f2020-08-27 15:19:03 +02003725 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, target, LYXP_NODE_ELEM, NULL));
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003726 }
3727
Michal Vasko03ff5a72019-09-11 13:49:33 +02003728 return rc;
3729 }
3730
Michal Vaskod3678892020-05-21 10:06:58 +02003731 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003732 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "deref(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003733 return LY_EVALID;
3734 }
3735
Michal Vaskod3678892020-05-21 10:06:58 +02003736 lyxp_set_free_content(set);
3737 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003738 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
3739 sleaf = (struct lysc_node_leaf *)leaf->schema;
3740 if (sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
3741 if (sleaf->type->basetype == LY_TYPE_LEAFREF) {
3742 /* find leafref target */
Radek Krejci0b013302021-03-29 15:22:32 +02003743 if (lyplg_type_resolve_leafref((struct lysc_type_leafref *)sleaf->type, &leaf->node, &leaf->value, set->tree,
Michal Vasko9e685082021-01-29 14:49:09 +01003744 &node, &errmsg)) {
Michal Vasko004d3152020-06-11 19:59:22 +02003745 LOGERR(set->ctx, LY_EVALID, errmsg);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003746 free(errmsg);
Michal Vasko004d3152020-06-11 19:59:22 +02003747 return LY_EVALID;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003748 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003749 } else {
3750 assert(sleaf->type->basetype == LY_TYPE_INST);
Michal Vasko004d3152020-06-11 19:59:22 +02003751 if (ly_path_eval(leaf->value.target, set->tree, &node)) {
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003752 LOGERR(set->ctx, LY_EVALID, "Invalid instance-identifier \"%s\" value - required instance not found.",
Michal Vasko69730152020-10-09 16:30:07 +02003753 LYD_CANON_VALUE(leaf));
Michal Vasko03ff5a72019-09-11 13:49:33 +02003754 return LY_EVALID;
3755 }
3756 }
Michal Vasko004d3152020-06-11 19:59:22 +02003757
3758 /* insert it */
3759 set_insert_node(set, node, 0, LYXP_NODE_ELEM, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003760 }
3761 }
3762
3763 return LY_SUCCESS;
3764}
3765
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003766static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02003767xpath_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 +02003768{
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01003769 uint32_t i;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003770 LY_ARRAY_COUNT_TYPE u;
3771 struct lyd_node_term *leaf;
3772 struct lysc_node_leaf *sleaf;
3773 struct lyd_meta *meta;
3774 struct lyd_value data = {0}, *val;
3775 struct ly_err_item *err = NULL;
3776 LY_ERR rc = LY_SUCCESS;
Radek Krejci857189e2020-09-01 13:26:36 +02003777 ly_bool found;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003778
3779 if (options & LYXP_SCNODE_ALL) {
3780 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3781 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", func);
3782 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3783 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype),
Michal Vasko69730152020-10-09 16:30:07 +02003784 sleaf->name);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003785 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_IDENT)) {
3786 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"identityref\".", func, sleaf->name);
3787 }
3788
3789 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3790 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3791 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype),
Michal Vasko69730152020-10-09 16:30:07 +02003792 sleaf->name);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003793 } else if (!warn_is_string_type(sleaf->type)) {
3794 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", func, sleaf->name);
3795 }
3796 }
3797 set_scnode_clear_ctx(set);
3798 return rc;
3799 }
3800
3801 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003802 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "derived-from(-or-self)(node-set, string)");
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003803 return LY_EVALID;
3804 }
3805 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
3806 LY_CHECK_RET(rc);
3807
3808 set_fill_boolean(set, 0);
3809 found = 0;
3810 for (i = 0; i < args[0]->used; ++i) {
3811 if ((args[0]->val.nodes[i].type != LYXP_NODE_ELEM) && (args[0]->val.nodes[i].type != LYXP_NODE_META)) {
3812 continue;
3813 }
3814
3815 if (args[0]->val.nodes[i].type == LYXP_NODE_ELEM) {
3816 leaf = (struct lyd_node_term *)args[0]->val.nodes[i].node;
3817 sleaf = (struct lysc_node_leaf *)leaf->schema;
3818 val = &leaf->value;
3819 if (!(sleaf->nodetype & LYD_NODE_TERM) || (leaf->value.realtype->basetype != LY_TYPE_IDENT)) {
3820 /* uninteresting */
3821 continue;
3822 }
3823
3824 /* store args[1] as ident */
3825 rc = val->realtype->plugin->store(set->ctx, val->realtype, args[1]->val.str, strlen(args[1]->val.str),
Michal Vasko405cc9e2020-12-01 12:01:27 +01003826 0, set->format, set->prefix_data, LYD_HINT_DATA, leaf->schema, &data, NULL, &err);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003827 } else {
3828 meta = args[0]->val.meta[i].meta;
3829 val = &meta->value;
3830 if (val->realtype->basetype != LY_TYPE_IDENT) {
3831 /* uninteresting */
3832 continue;
3833 }
3834
3835 /* store args[1] as ident */
Michal Vaskofeca4fb2020-10-05 08:58:40 +02003836 rc = val->realtype->plugin->store(set->ctx, val->realtype, args[1]->val.str, strlen(args[1]->val.str), 0,
Michal Vasko405cc9e2020-12-01 12:01:27 +01003837 set->format, set->prefix_data, LYD_HINT_DATA, meta->parent->schema, &data, NULL, &err);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003838 }
3839
3840 if (err) {
Michal Vasko177d0ed2020-11-23 16:43:03 +01003841 ly_err_print(set->ctx, err);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003842 ly_err_free(err);
3843 }
3844 LY_CHECK_RET(rc);
3845
3846 /* finally check the identity itself */
3847 if (self_match && (data.ident == val->ident)) {
3848 set_fill_boolean(set, 1);
3849 found = 1;
3850 }
3851 if (!found) {
3852 LY_ARRAY_FOR(data.ident->derived, u) {
3853 if (data.ident->derived[u] == val->ident) {
3854 set_fill_boolean(set, 1);
3855 found = 1;
3856 break;
3857 }
3858 }
3859 }
3860
3861 /* free temporary value */
3862 val->realtype->plugin->free(set->ctx, &data);
3863 if (found) {
3864 break;
3865 }
3866 }
3867
3868 return LY_SUCCESS;
3869}
3870
Michal Vasko03ff5a72019-09-11 13:49:33 +02003871/**
3872 * @brief Execute the YANG 1.1 derived-from(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
3873 * on whether the first argument nodes contain a node of an identity derived from the second
3874 * argument identity.
3875 *
3876 * @param[in] args Array of arguments.
3877 * @param[in] arg_count Count of elements in @p args.
3878 * @param[in,out] set Context and result set at the same time.
3879 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003880 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003881 */
3882static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003883xpath_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 +02003884{
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003885 return xpath_derived_(args, set, options, 0, __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003886}
3887
3888/**
3889 * @brief Execute the YANG 1.1 derived-from-or-self(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
3890 * on whether the first argument nodes contain a node of an identity that either is or is derived from
3891 * the second argument identity.
3892 *
3893 * @param[in] args Array of arguments.
3894 * @param[in] arg_count Count of elements in @p args.
3895 * @param[in,out] set Context and result set at the same time.
3896 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003897 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003898 */
3899static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003900xpath_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 +02003901{
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003902 return xpath_derived_(args, set, options, 1, __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003903}
3904
3905/**
3906 * @brief Execute the YANG 1.1 enum-value(node-set) function. Returns LYXP_SET_NUMBER
3907 * with the integer value of the first node's enum value, otherwise NaN.
3908 *
3909 * @param[in] args Array of arguments.
3910 * @param[in] arg_count Count of elements in @p args.
3911 * @param[in,out] set Context and result set at the same time.
3912 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003913 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003914 */
3915static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003916xpath_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 +02003917{
3918 struct lyd_node_term *leaf;
3919 struct lysc_node_leaf *sleaf;
3920 LY_ERR rc = LY_SUCCESS;
3921
3922 if (options & LYXP_SCNODE_ALL) {
3923 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3924 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003925 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3926 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 +02003927 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_ENUM)) {
3928 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"enumeration\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003929 }
3930 set_scnode_clear_ctx(set);
3931 return rc;
3932 }
3933
Michal Vaskod3678892020-05-21 10:06:58 +02003934 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003935 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "enum-value(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003936 return LY_EVALID;
3937 }
3938
3939 set_fill_number(set, NAN);
Michal Vaskod3678892020-05-21 10:06:58 +02003940 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003941 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
3942 sleaf = (struct lysc_node_leaf *)leaf->schema;
3943 if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_ENUM)) {
3944 set_fill_number(set, leaf->value.enum_item->value);
3945 }
3946 }
3947
3948 return LY_SUCCESS;
3949}
3950
3951/**
3952 * @brief Execute the XPath false() function. Returns LYXP_SET_BOOLEAN
3953 * with false value.
3954 *
3955 * @param[in] args Array of arguments.
3956 * @param[in] arg_count Count of elements in @p args.
3957 * @param[in,out] set Context and result set at the same time.
3958 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003959 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003960 */
3961static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003962xpath_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 +02003963{
3964 if (options & LYXP_SCNODE_ALL) {
3965 set_scnode_clear_ctx(set);
3966 return LY_SUCCESS;
3967 }
3968
3969 set_fill_boolean(set, 0);
3970 return LY_SUCCESS;
3971}
3972
3973/**
3974 * @brief Execute the XPath floor(number) function. Returns LYXP_SET_NUMBER
3975 * with the first argument floored (truncated).
3976 *
3977 * @param[in] args Array of arguments.
3978 * @param[in] arg_count Count of elements in @p args.
3979 * @param[in,out] set Context and result set at the same time.
3980 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003981 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003982 */
3983static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003984xpath_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 +02003985{
3986 LY_ERR rc;
3987
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003988 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003989 LY_CHECK_RET(rc);
3990 if (isfinite(args[0]->val.num)) {
3991 set_fill_number(set, (long long)args[0]->val.num);
3992 }
3993
3994 return LY_SUCCESS;
3995}
3996
3997/**
3998 * @brief Execute the XPath lang(string) function. Returns LYXP_SET_BOOLEAN
3999 * whether the language of the text matches the one from the argument.
4000 *
4001 * @param[in] args Array of arguments.
4002 * @param[in] arg_count Count of elements in @p args.
4003 * @param[in,out] set Context and result set at the same time.
4004 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004005 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004006 */
4007static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004008xpath_lang(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004009{
4010 const struct lyd_node *node;
4011 struct lysc_node_leaf *sleaf;
Michal Vasko9f96a052020-03-10 09:41:45 +01004012 struct lyd_meta *meta = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004013 const char *val;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004014 LY_ERR rc = LY_SUCCESS;
4015
4016 if (options & LYXP_SCNODE_ALL) {
4017 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4018 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4019 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 +02004020 } else if (!warn_is_string_type(sleaf->type)) {
4021 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004022 }
4023 }
4024 set_scnode_clear_ctx(set);
4025 return rc;
4026 }
4027
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004028 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004029 LY_CHECK_RET(rc);
4030
Michal Vasko03ff5a72019-09-11 13:49:33 +02004031 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004032 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "lang(string)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004033 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004034 } else if (!set->used) {
4035 set_fill_boolean(set, 0);
4036 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004037 }
4038
4039 switch (set->val.nodes[0].type) {
4040 case LYXP_NODE_ELEM:
4041 case LYXP_NODE_TEXT:
4042 node = set->val.nodes[0].node;
4043 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004044 case LYXP_NODE_META:
4045 node = set->val.meta[0].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004046 break;
4047 default:
4048 /* nothing to do with roots */
4049 set_fill_boolean(set, 0);
4050 return LY_SUCCESS;
4051 }
4052
Michal Vasko9f96a052020-03-10 09:41:45 +01004053 /* find lang metadata */
Michal Vasko9e685082021-01-29 14:49:09 +01004054 for ( ; node; node = lyd_parent(node)) {
Michal Vasko9f96a052020-03-10 09:41:45 +01004055 for (meta = node->meta; meta; meta = meta->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004056 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004057 if (meta->name && !strcmp(meta->name, "lang") && !strcmp(meta->annotation->module->name, "xml")) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004058 break;
4059 }
4060 }
4061
Michal Vasko9f96a052020-03-10 09:41:45 +01004062 if (meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004063 break;
4064 }
4065 }
4066
4067 /* compare languages */
Michal Vasko9f96a052020-03-10 09:41:45 +01004068 if (!meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004069 set_fill_boolean(set, 0);
4070 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004071 uint64_t i;
4072
Michal Vaskoba99a3e2020-08-18 15:50:05 +02004073 val = meta->value.canonical;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004074 for (i = 0; args[0]->val.str[i]; ++i) {
4075 if (tolower(args[0]->val.str[i]) != tolower(val[i])) {
4076 set_fill_boolean(set, 0);
4077 break;
4078 }
4079 }
4080 if (!args[0]->val.str[i]) {
4081 if (!val[i] || (val[i] == '-')) {
4082 set_fill_boolean(set, 1);
4083 } else {
4084 set_fill_boolean(set, 0);
4085 }
4086 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004087 }
4088
4089 return LY_SUCCESS;
4090}
4091
4092/**
4093 * @brief Execute the XPath last() function. Returns LYXP_SET_NUMBER
4094 * with the context size.
4095 *
4096 * @param[in] args Array of arguments.
4097 * @param[in] arg_count Count of elements in @p args.
4098 * @param[in,out] set Context and result set at the same time.
4099 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004100 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004101 */
4102static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004103xpath_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 +02004104{
4105 if (options & LYXP_SCNODE_ALL) {
4106 set_scnode_clear_ctx(set);
4107 return LY_SUCCESS;
4108 }
4109
Michal Vasko03ff5a72019-09-11 13:49:33 +02004110 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004111 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "last()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004112 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004113 } else if (!set->used) {
4114 set_fill_number(set, 0);
4115 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004116 }
4117
4118 set_fill_number(set, set->ctx_size);
4119 return LY_SUCCESS;
4120}
4121
4122/**
4123 * @brief Execute the XPath local-name(node-set?) function. Returns LYXP_SET_STRING
4124 * with the node name without namespace from the argument or the context.
4125 *
4126 * @param[in] args Array of arguments.
4127 * @param[in] arg_count Count of elements in @p args.
4128 * @param[in,out] set Context and result set at the same time.
4129 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004130 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004131 */
4132static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004133xpath_local_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004134{
4135 struct lyxp_set_node *item;
Michal Vasko69730152020-10-09 16:30:07 +02004136
Michal Vasko03ff5a72019-09-11 13:49:33 +02004137 /* suppress unused variable warning */
4138 (void)options;
4139
4140 if (options & LYXP_SCNODE_ALL) {
4141 set_scnode_clear_ctx(set);
4142 return LY_SUCCESS;
4143 }
4144
4145 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004146 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004147 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004148 "local-name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004149 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004150 } else if (!args[0]->used) {
4151 set_fill_string(set, "", 0);
4152 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004153 }
4154
4155 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004156 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004157
4158 item = &args[0]->val.nodes[0];
4159 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004160 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004161 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "local-name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004162 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004163 } else if (!set->used) {
4164 set_fill_string(set, "", 0);
4165 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004166 }
4167
4168 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004169 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004170
4171 item = &set->val.nodes[0];
4172 }
4173
4174 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004175 case LYXP_NODE_NONE:
4176 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004177 case LYXP_NODE_ROOT:
4178 case LYXP_NODE_ROOT_CONFIG:
4179 case LYXP_NODE_TEXT:
4180 set_fill_string(set, "", 0);
4181 break;
4182 case LYXP_NODE_ELEM:
4183 set_fill_string(set, item->node->schema->name, strlen(item->node->schema->name));
4184 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004185 case LYXP_NODE_META:
4186 set_fill_string(set, ((struct lyd_meta *)item->node)->name, strlen(((struct lyd_meta *)item->node)->name));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004187 break;
4188 }
4189
4190 return LY_SUCCESS;
4191}
4192
4193/**
4194 * @brief Execute the XPath name(node-set?) function. Returns LYXP_SET_STRING
4195 * with the node name fully qualified (with namespace) from the argument or the context.
4196 *
4197 * @param[in] args Array of arguments.
4198 * @param[in] arg_count Count of elements in @p args.
4199 * @param[in,out] set Context and result set at the same time.
4200 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004201 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004202 */
4203static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004204xpath_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004205{
4206 struct lyxp_set_node *item;
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004207 struct lys_module *mod = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004208 char *str;
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004209 const char *name = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004210
4211 if (options & LYXP_SCNODE_ALL) {
4212 set_scnode_clear_ctx(set);
4213 return LY_SUCCESS;
4214 }
4215
4216 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004217 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004218 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004219 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004220 } else if (!args[0]->used) {
4221 set_fill_string(set, "", 0);
4222 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004223 }
4224
4225 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004226 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004227
4228 item = &args[0]->val.nodes[0];
4229 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004230 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004231 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "name(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004232 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004233 } else if (!set->used) {
4234 set_fill_string(set, "", 0);
4235 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004236 }
4237
4238 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004239 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004240
4241 item = &set->val.nodes[0];
4242 }
4243
4244 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004245 case LYXP_NODE_NONE:
4246 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004247 case LYXP_NODE_ROOT:
4248 case LYXP_NODE_ROOT_CONFIG:
4249 case LYXP_NODE_TEXT:
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004250 /* keep NULL */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004251 break;
4252 case LYXP_NODE_ELEM:
4253 mod = item->node->schema->module;
4254 name = item->node->schema->name;
4255 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004256 case LYXP_NODE_META:
4257 mod = ((struct lyd_meta *)item->node)->annotation->module;
4258 name = ((struct lyd_meta *)item->node)->name;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004259 break;
4260 }
4261
4262 if (mod && name) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02004263 int rc = asprintf(&str, "%s:%s", ly_get_prefix(mod, set->format, set->prefix_data), name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004264 LY_CHECK_ERR_RET(rc == -1, LOGMEM(set->ctx), LY_EMEM);
4265 set_fill_string(set, str, strlen(str));
4266 free(str);
4267 } else {
4268 set_fill_string(set, "", 0);
4269 }
4270
4271 return LY_SUCCESS;
4272}
4273
4274/**
4275 * @brief Execute the XPath namespace-uri(node-set?) function. Returns LYXP_SET_STRING
4276 * with the namespace of the node from the argument or the context.
4277 *
4278 * @param[in] args Array of arguments.
4279 * @param[in] arg_count Count of elements in @p args.
4280 * @param[in,out] set Context and result set at the same time.
4281 * @param[in] options XPath options.
4282 * @return LY_ERR (LY_EINVAL for wrong arguments on schema)
4283 */
4284static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004285xpath_namespace_uri(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004286{
4287 struct lyxp_set_node *item;
4288 struct lys_module *mod;
Michal Vasko69730152020-10-09 16:30:07 +02004289
Michal Vasko03ff5a72019-09-11 13:49:33 +02004290 /* suppress unused variable warning */
4291 (void)options;
4292
4293 if (options & LYXP_SCNODE_ALL) {
4294 set_scnode_clear_ctx(set);
4295 return LY_SUCCESS;
4296 }
4297
4298 if (arg_count) {
Michal Vaskod3678892020-05-21 10:06:58 +02004299 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004300 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
Michal Vasko69730152020-10-09 16:30:07 +02004301 "namespace-uri(node-set?)");
Michal Vaskod3678892020-05-21 10:06:58 +02004302 return LY_EVALID;
4303 } else if (!args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004304 set_fill_string(set, "", 0);
4305 return LY_SUCCESS;
4306 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004307
4308 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004309 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004310
4311 item = &args[0]->val.nodes[0];
4312 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004313 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004314 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "namespace-uri(node-set?)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004315 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004316 } else if (!set->used) {
4317 set_fill_string(set, "", 0);
4318 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004319 }
4320
4321 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004322 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004323
4324 item = &set->val.nodes[0];
4325 }
4326
4327 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004328 case LYXP_NODE_NONE:
4329 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004330 case LYXP_NODE_ROOT:
4331 case LYXP_NODE_ROOT_CONFIG:
4332 case LYXP_NODE_TEXT:
4333 set_fill_string(set, "", 0);
4334 break;
4335 case LYXP_NODE_ELEM:
Michal Vasko9f96a052020-03-10 09:41:45 +01004336 case LYXP_NODE_META:
Michal Vasko03ff5a72019-09-11 13:49:33 +02004337 if (item->type == LYXP_NODE_ELEM) {
4338 mod = item->node->schema->module;
Michal Vasko9f96a052020-03-10 09:41:45 +01004339 } else { /* LYXP_NODE_META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004340 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004341 mod = ((struct lyd_meta *)item->node)->annotation->module;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004342 }
4343
4344 set_fill_string(set, mod->ns, strlen(mod->ns));
4345 break;
4346 }
4347
4348 return LY_SUCCESS;
4349}
4350
4351/**
4352 * @brief Execute the XPath node() function (node type). Returns LYXP_SET_NODE_SET
4353 * with only nodes from the context. In practice it either leaves the context
4354 * as it is or returns an empty node set.
4355 *
4356 * @param[in] args Array of arguments.
4357 * @param[in] arg_count Count of elements in @p args.
4358 * @param[in,out] set Context and result set at the same time.
4359 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004360 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004361 */
4362static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004363xpath_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 +02004364{
4365 if (options & LYXP_SCNODE_ALL) {
4366 set_scnode_clear_ctx(set);
4367 return LY_SUCCESS;
4368 }
4369
4370 if (set->type != LYXP_SET_NODE_SET) {
Michal Vaskod3678892020-05-21 10:06:58 +02004371 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004372 }
4373 return LY_SUCCESS;
4374}
4375
4376/**
4377 * @brief Execute the XPath normalize-space(string?) function. Returns LYXP_SET_STRING
4378 * with normalized value (no leading, trailing, double white spaces) of the node
4379 * from the argument or the context.
4380 *
4381 * @param[in] args Array of arguments.
4382 * @param[in] arg_count Count of elements in @p args.
4383 * @param[in,out] set Context and result set at the same time.
4384 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004385 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004386 */
4387static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004388xpath_normalize_space(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004389{
4390 uint16_t i, new_used;
4391 char *new;
Radek Krejci857189e2020-09-01 13:26:36 +02004392 ly_bool have_spaces = 0, space_before = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004393 struct lysc_node_leaf *sleaf;
4394 LY_ERR rc = LY_SUCCESS;
4395
4396 if (options & LYXP_SCNODE_ALL) {
4397 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4398 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4399 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 +02004400 } else if (!warn_is_string_type(sleaf->type)) {
4401 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004402 }
4403 }
4404 set_scnode_clear_ctx(set);
4405 return rc;
4406 }
4407
4408 if (arg_count) {
4409 set_fill_set(set, args[0]);
4410 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004411 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004412 LY_CHECK_RET(rc);
4413
4414 /* is there any normalization necessary? */
4415 for (i = 0; set->val.str[i]; ++i) {
4416 if (is_xmlws(set->val.str[i])) {
4417 if ((i == 0) || space_before || (!set->val.str[i + 1])) {
4418 have_spaces = 1;
4419 break;
4420 }
4421 space_before = 1;
4422 } else {
4423 space_before = 0;
4424 }
4425 }
4426
4427 /* yep, there is */
4428 if (have_spaces) {
4429 /* it's enough, at least one character will go, makes space for ending '\0' */
4430 new = malloc(strlen(set->val.str) * sizeof(char));
4431 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4432 new_used = 0;
4433
4434 space_before = 0;
4435 for (i = 0; set->val.str[i]; ++i) {
4436 if (is_xmlws(set->val.str[i])) {
4437 if ((i == 0) || space_before) {
4438 space_before = 1;
4439 continue;
4440 } else {
4441 space_before = 1;
4442 }
4443 } else {
4444 space_before = 0;
4445 }
4446
4447 new[new_used] = (space_before ? ' ' : set->val.str[i]);
4448 ++new_used;
4449 }
4450
4451 /* at worst there is one trailing space now */
4452 if (new_used && is_xmlws(new[new_used - 1])) {
4453 --new_used;
4454 }
4455
4456 new = ly_realloc(new, (new_used + 1) * sizeof(char));
4457 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4458 new[new_used] = '\0';
4459
4460 free(set->val.str);
4461 set->val.str = new;
4462 }
4463
4464 return LY_SUCCESS;
4465}
4466
4467/**
4468 * @brief Execute the XPath not(boolean) function. Returns LYXP_SET_BOOLEAN
4469 * with the argument converted to boolean and logically inverted.
4470 *
4471 * @param[in] args Array of arguments.
4472 * @param[in] arg_count Count of elements in @p args.
4473 * @param[in,out] set Context and result set at the same time.
4474 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004475 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004476 */
4477static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004478xpath_not(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004479{
4480 if (options & LYXP_SCNODE_ALL) {
4481 set_scnode_clear_ctx(set);
4482 return LY_SUCCESS;
4483 }
4484
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004485 lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02004486 if (args[0]->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004487 set_fill_boolean(set, 0);
4488 } else {
4489 set_fill_boolean(set, 1);
4490 }
4491
4492 return LY_SUCCESS;
4493}
4494
4495/**
4496 * @brief Execute the XPath number(object?) function. Returns LYXP_SET_NUMBER
4497 * with the number representation of either the argument or the context.
4498 *
4499 * @param[in] args Array of arguments.
4500 * @param[in] arg_count Count of elements in @p args.
4501 * @param[in,out] set Context and result set at the same time.
4502 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004503 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004504 */
4505static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004506xpath_number(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004507{
4508 LY_ERR rc;
4509
4510 if (options & LYXP_SCNODE_ALL) {
4511 set_scnode_clear_ctx(set);
4512 return LY_SUCCESS;
4513 }
4514
4515 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004516 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004517 LY_CHECK_RET(rc);
4518 set_fill_set(set, args[0]);
4519 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004520 rc = lyxp_set_cast(set, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004521 LY_CHECK_RET(rc);
4522 }
4523
4524 return LY_SUCCESS;
4525}
4526
4527/**
4528 * @brief Execute the XPath position() function. Returns LYXP_SET_NUMBER
4529 * with the context position.
4530 *
4531 * @param[in] args Array of arguments.
4532 * @param[in] arg_count Count of elements in @p args.
4533 * @param[in,out] set Context and result set at the same time.
4534 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004535 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004536 */
4537static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004538xpath_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 +02004539{
4540 if (options & LYXP_SCNODE_ALL) {
4541 set_scnode_clear_ctx(set);
4542 return LY_SUCCESS;
4543 }
4544
Michal Vasko03ff5a72019-09-11 13:49:33 +02004545 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01004546 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "position()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02004547 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004548 } else if (!set->used) {
4549 set_fill_number(set, 0);
4550 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004551 }
4552
4553 set_fill_number(set, set->ctx_pos);
4554
4555 /* UNUSED in 'Release' build type */
4556 (void)options;
4557 return LY_SUCCESS;
4558}
4559
4560/**
4561 * @brief Execute the YANG 1.1 re-match(string, string) function. Returns LYXP_SET_BOOLEAN
4562 * depending on whether the second argument regex matches the first argument string. For details refer to
4563 * YANG 1.1 RFC section 10.2.1.
4564 *
4565 * @param[in] args Array of arguments.
4566 * @param[in] arg_count Count of elements in @p args.
4567 * @param[in,out] set Context and result set at the same time.
4568 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004569 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004570 */
4571static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004572xpath_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 +02004573{
4574 struct lysc_pattern **patterns = NULL, **pattern;
4575 struct lysc_node_leaf *sleaf;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004576 LY_ERR rc = LY_SUCCESS;
4577 struct ly_err_item *err;
4578
4579 if (options & LYXP_SCNODE_ALL) {
4580 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4581 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4582 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 +02004583 } else if (!warn_is_string_type(sleaf->type)) {
4584 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004585 }
4586 }
4587
4588 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4589 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4590 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 +02004591 } else if (!warn_is_string_type(sleaf->type)) {
4592 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004593 }
4594 }
4595 set_scnode_clear_ctx(set);
4596 return rc;
4597 }
4598
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004599 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004600 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004601 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004602 LY_CHECK_RET(rc);
4603
4604 LY_ARRAY_NEW_RET(set->ctx, patterns, pattern, LY_EMEM);
Radek IÅ¡a45802b52021-02-09 09:21:58 +01004605 *pattern = calloc(1, sizeof **pattern);
Radek Krejciddace2c2021-01-08 11:30:56 +01004606 LOG_LOCSET(NULL, set->cur_node, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01004607 rc = lys_compile_type_pattern_check(set->ctx, args[1]->val.str, &(*pattern)->code);
Radek Krejciddace2c2021-01-08 11:30:56 +01004608 LOG_LOCBACK(0, 1, 0, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004609 if (rc != LY_SUCCESS) {
4610 LY_ARRAY_FREE(patterns);
4611 return rc;
4612 }
4613
Radek Krejci0b013302021-03-29 15:22:32 +02004614 rc = lyplg_type_validate_patterns(patterns, args[0]->val.str, strlen(args[0]->val.str), &err);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004615 pcre2_code_free((*pattern)->code);
4616 free(*pattern);
4617 LY_ARRAY_FREE(patterns);
4618 if (rc && (rc != LY_EVALID)) {
Michal Vasko177d0ed2020-11-23 16:43:03 +01004619 ly_err_print(set->ctx, err);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004620 ly_err_free(err);
4621 return rc;
4622 }
4623
4624 if (rc == LY_EVALID) {
4625 ly_err_free(err);
4626 set_fill_boolean(set, 0);
4627 } else {
4628 set_fill_boolean(set, 1);
4629 }
4630
4631 return LY_SUCCESS;
4632}
4633
4634/**
4635 * @brief Execute the XPath round(number) function. Returns LYXP_SET_NUMBER
4636 * with the rounded first argument. For details refer to
4637 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-round.
4638 *
4639 * @param[in] args Array of arguments.
4640 * @param[in] arg_count Count of elements in @p args.
4641 * @param[in,out] set Context and result set at the same time.
4642 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004643 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004644 */
4645static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004646xpath_round(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004647{
4648 struct lysc_node_leaf *sleaf;
4649 LY_ERR rc = LY_SUCCESS;
4650
4651 if (options & LYXP_SCNODE_ALL) {
4652 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4653 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004654 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4655 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 +02004656 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
4657 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004658 }
4659 set_scnode_clear_ctx(set);
4660 return rc;
4661 }
4662
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004663 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004664 LY_CHECK_RET(rc);
4665
4666 /* cover only the cases where floor can't be used */
4667 if ((args[0]->val.num == -0.0f) || ((args[0]->val.num < 0) && (args[0]->val.num >= -0.5))) {
4668 set_fill_number(set, -0.0f);
4669 } else {
4670 args[0]->val.num += 0.5;
4671 rc = xpath_floor(args, 1, args[0], options);
4672 LY_CHECK_RET(rc);
4673 set_fill_number(set, args[0]->val.num);
4674 }
4675
4676 return LY_SUCCESS;
4677}
4678
4679/**
4680 * @brief Execute the XPath starts-with(string, string) function.
4681 * Returns LYXP_SET_BOOLEAN whether the second argument is
4682 * the prefix of the first or not.
4683 *
4684 * @param[in] args Array of arguments.
4685 * @param[in] arg_count Count of elements in @p args.
4686 * @param[in,out] set Context and result set at the same time.
4687 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004688 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004689 */
4690static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004691xpath_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 +02004692{
4693 struct lysc_node_leaf *sleaf;
4694 LY_ERR rc = LY_SUCCESS;
4695
4696 if (options & LYXP_SCNODE_ALL) {
4697 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4698 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4699 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 +02004700 } else if (!warn_is_string_type(sleaf->type)) {
4701 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004702 }
4703 }
4704
4705 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4706 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4707 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 +02004708 } else if (!warn_is_string_type(sleaf->type)) {
4709 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004710 }
4711 }
4712 set_scnode_clear_ctx(set);
4713 return rc;
4714 }
4715
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004716 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004717 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004718 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004719 LY_CHECK_RET(rc);
4720
4721 if (strncmp(args[0]->val.str, args[1]->val.str, strlen(args[1]->val.str))) {
4722 set_fill_boolean(set, 0);
4723 } else {
4724 set_fill_boolean(set, 1);
4725 }
4726
4727 return LY_SUCCESS;
4728}
4729
4730/**
4731 * @brief Execute the XPath string(object?) function. Returns LYXP_SET_STRING
4732 * with the string representation of either the argument or the context.
4733 *
4734 * @param[in] args Array of arguments.
4735 * @param[in] arg_count Count of elements in @p args.
4736 * @param[in,out] set Context and result set at the same time.
4737 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004738 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004739 */
4740static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004741xpath_string(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004742{
4743 LY_ERR rc;
4744
4745 if (options & LYXP_SCNODE_ALL) {
4746 set_scnode_clear_ctx(set);
4747 return LY_SUCCESS;
4748 }
4749
4750 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004751 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004752 LY_CHECK_RET(rc);
4753 set_fill_set(set, args[0]);
4754 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004755 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004756 LY_CHECK_RET(rc);
4757 }
4758
4759 return LY_SUCCESS;
4760}
4761
4762/**
4763 * @brief Execute the XPath string-length(string?) function. Returns LYXP_SET_NUMBER
4764 * with the length of the string in either the argument or the context.
4765 *
4766 * @param[in] args Array of arguments.
4767 * @param[in] arg_count Count of elements in @p args.
4768 * @param[in,out] set Context and result set at the same time.
4769 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004770 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004771 */
4772static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004773xpath_string_length(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004774{
4775 struct lysc_node_leaf *sleaf;
4776 LY_ERR rc = LY_SUCCESS;
4777
4778 if (options & LYXP_SCNODE_ALL) {
4779 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4780 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4781 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 +02004782 } else if (!warn_is_string_type(sleaf->type)) {
4783 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004784 }
4785 }
4786 if (!arg_count && (set->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set))) {
4787 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4788 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 +02004789 } else if (!warn_is_string_type(sleaf->type)) {
4790 LOGWRN(set->ctx, "Argument #0 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004791 }
4792 }
4793 set_scnode_clear_ctx(set);
4794 return rc;
4795 }
4796
4797 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004798 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004799 LY_CHECK_RET(rc);
4800 set_fill_number(set, strlen(args[0]->val.str));
4801 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004802 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004803 LY_CHECK_RET(rc);
4804 set_fill_number(set, strlen(set->val.str));
4805 }
4806
4807 return LY_SUCCESS;
4808}
4809
4810/**
4811 * @brief Execute the XPath substring(string, number, number?) function.
4812 * Returns LYXP_SET_STRING substring of the first argument starting
4813 * on the second argument index ending on the third argument index,
4814 * indexed from 1. For exact definition refer to
4815 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-substring.
4816 *
4817 * @param[in] args Array of arguments.
4818 * @param[in] arg_count Count of elements in @p args.
4819 * @param[in,out] set Context and result set at the same time.
4820 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004821 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004822 */
4823static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004824xpath_substring(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004825{
Radek Krejci1deb5be2020-08-26 16:43:36 +02004826 int32_t start, len;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004827 uint16_t str_start, str_len, pos;
4828 struct lysc_node_leaf *sleaf;
4829 LY_ERR rc = LY_SUCCESS;
4830
4831 if (options & LYXP_SCNODE_ALL) {
4832 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4833 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4834 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 +02004835 } else if (!warn_is_string_type(sleaf->type)) {
4836 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004837 }
4838 }
4839
4840 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4841 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4842 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 +02004843 } else if (!warn_is_numeric_type(sleaf->type)) {
4844 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004845 }
4846 }
4847
Michal Vasko69730152020-10-09 16:30:07 +02004848 if ((arg_count == 3) && (args[2]->type == LYXP_SET_SCNODE_SET) &&
4849 (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004850 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4851 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 +02004852 } else if (!warn_is_numeric_type(sleaf->type)) {
4853 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004854 }
4855 }
4856 set_scnode_clear_ctx(set);
4857 return rc;
4858 }
4859
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004860 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004861 LY_CHECK_RET(rc);
4862
4863 /* start */
4864 if (xpath_round(&args[1], 1, args[1], options)) {
4865 return -1;
4866 }
4867 if (isfinite(args[1]->val.num)) {
4868 start = args[1]->val.num - 1;
4869 } else if (isinf(args[1]->val.num) && signbit(args[1]->val.num)) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004870 start = INT32_MIN;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004871 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004872 start = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004873 }
4874
4875 /* len */
4876 if (arg_count == 3) {
4877 rc = xpath_round(&args[2], 1, args[2], options);
4878 LY_CHECK_RET(rc);
Radek Krejci1deb5be2020-08-26 16:43:36 +02004879 if (isnan(args[2]->val.num) || signbit(args[2]->val.num)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004880 len = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02004881 } else if (isfinite(args[2]->val.num)) {
4882 len = args[2]->val.num;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004883 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004884 len = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004885 }
4886 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004887 len = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004888 }
4889
4890 /* find matching character positions */
4891 str_start = 0;
4892 str_len = 0;
4893 for (pos = 0; args[0]->val.str[pos]; ++pos) {
4894 if (pos < start) {
4895 ++str_start;
4896 } else if (pos < start + len) {
4897 ++str_len;
4898 } else {
4899 break;
4900 }
4901 }
4902
4903 set_fill_string(set, args[0]->val.str + str_start, str_len);
4904 return LY_SUCCESS;
4905}
4906
4907/**
4908 * @brief Execute the XPath substring-after(string, string) function.
4909 * Returns LYXP_SET_STRING with the string succeeding the occurance
4910 * of the second argument in the first or an empty string.
4911 *
4912 * @param[in] args Array of arguments.
4913 * @param[in] arg_count Count of elements in @p args.
4914 * @param[in,out] set Context and result set at the same time.
4915 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004916 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004917 */
4918static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004919xpath_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 +02004920{
4921 char *ptr;
4922 struct lysc_node_leaf *sleaf;
4923 LY_ERR rc = LY_SUCCESS;
4924
4925 if (options & LYXP_SCNODE_ALL) {
4926 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4927 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4928 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 +02004929 } else if (!warn_is_string_type(sleaf->type)) {
4930 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004931 }
4932 }
4933
4934 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4935 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4936 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 +02004937 } else if (!warn_is_string_type(sleaf->type)) {
4938 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004939 }
4940 }
4941 set_scnode_clear_ctx(set);
4942 return rc;
4943 }
4944
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004945 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004946 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004947 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004948 LY_CHECK_RET(rc);
4949
4950 ptr = strstr(args[0]->val.str, args[1]->val.str);
4951 if (ptr) {
4952 set_fill_string(set, ptr + strlen(args[1]->val.str), strlen(ptr + strlen(args[1]->val.str)));
4953 } else {
4954 set_fill_string(set, "", 0);
4955 }
4956
4957 return LY_SUCCESS;
4958}
4959
4960/**
4961 * @brief Execute the XPath substring-before(string, string) function.
4962 * Returns LYXP_SET_STRING with the string preceding the occurance
4963 * of the second argument in the first or an empty string.
4964 *
4965 * @param[in] args Array of arguments.
4966 * @param[in] arg_count Count of elements in @p args.
4967 * @param[in,out] set Context and result set at the same time.
4968 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004969 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004970 */
4971static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004972xpath_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 +02004973{
4974 char *ptr;
4975 struct lysc_node_leaf *sleaf;
4976 LY_ERR rc = LY_SUCCESS;
4977
4978 if (options & LYXP_SCNODE_ALL) {
4979 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4980 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4981 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 +02004982 } else if (!warn_is_string_type(sleaf->type)) {
4983 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004984 }
4985 }
4986
4987 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4988 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4989 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 +02004990 } else if (!warn_is_string_type(sleaf->type)) {
4991 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004992 }
4993 }
4994 set_scnode_clear_ctx(set);
4995 return rc;
4996 }
4997
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004998 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004999 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005000 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005001 LY_CHECK_RET(rc);
5002
5003 ptr = strstr(args[0]->val.str, args[1]->val.str);
5004 if (ptr) {
5005 set_fill_string(set, args[0]->val.str, ptr - args[0]->val.str);
5006 } else {
5007 set_fill_string(set, "", 0);
5008 }
5009
5010 return LY_SUCCESS;
5011}
5012
5013/**
5014 * @brief Execute the XPath sum(node-set) function. Returns LYXP_SET_NUMBER
5015 * with the sum of all the nodes in the context.
5016 *
5017 * @param[in] args Array of arguments.
5018 * @param[in] arg_count Count of elements in @p args.
5019 * @param[in,out] set Context and result set at the same time.
5020 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005021 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005022 */
5023static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005024xpath_sum(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005025{
5026 long double num;
5027 char *str;
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01005028 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005029 struct lyxp_set set_item;
5030 struct lysc_node_leaf *sleaf;
5031 LY_ERR rc = LY_SUCCESS;
5032
5033 if (options & LYXP_SCNODE_ALL) {
5034 if (args[0]->type == LYXP_SET_SCNODE_SET) {
5035 for (i = 0; i < args[0]->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005036 if (args[0]->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005037 sleaf = (struct lysc_node_leaf *)args[0]->val.scnodes[i].scnode;
5038 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5039 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__,
Michal Vasko69730152020-10-09 16:30:07 +02005040 lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005041 } else if (!warn_is_numeric_type(sleaf->type)) {
5042 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005043 }
5044 }
5045 }
5046 }
5047 set_scnode_clear_ctx(set);
5048 return rc;
5049 }
5050
5051 set_fill_number(set, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005052
5053 if (args[0]->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005054 LOGVAL(set->ctx, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "sum(node-set)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02005055 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02005056 } else if (!args[0]->used) {
5057 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005058 }
5059
Michal Vasko5c4e5892019-11-14 12:31:38 +01005060 set_init(&set_item, set);
5061
Michal Vasko03ff5a72019-09-11 13:49:33 +02005062 set_item.type = LYXP_SET_NODE_SET;
5063 set_item.val.nodes = malloc(sizeof *set_item.val.nodes);
5064 LY_CHECK_ERR_RET(!set_item.val.nodes, LOGMEM(set->ctx), LY_EMEM);
5065
5066 set_item.used = 1;
5067 set_item.size = 1;
5068
5069 for (i = 0; i < args[0]->used; ++i) {
5070 set_item.val.nodes[0] = args[0]->val.nodes[i];
5071
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005072 rc = cast_node_set_to_string(&set_item, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005073 LY_CHECK_RET(rc);
5074 num = cast_string_to_number(str);
5075 free(str);
5076 set->val.num += num;
5077 }
5078
5079 free(set_item.val.nodes);
5080
5081 return LY_SUCCESS;
5082}
5083
5084/**
5085 * @brief Execute the XPath text() function (node type). Returns LYXP_SET_NODE_SET
5086 * with the text content of the nodes in the context.
5087 *
5088 * @param[in] args Array of arguments.
5089 * @param[in] arg_count Count of elements in @p args.
5090 * @param[in,out] set Context and result set at the same time.
5091 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005092 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005093 */
5094static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005095xpath_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 +02005096{
5097 uint32_t i;
5098
5099 if (options & LYXP_SCNODE_ALL) {
5100 set_scnode_clear_ctx(set);
5101 return LY_SUCCESS;
5102 }
5103
Michal Vasko03ff5a72019-09-11 13:49:33 +02005104 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005105 LOGVAL(set->ctx, LY_VCODE_XP_INCTX, print_set_type(set), "text()");
Michal Vasko03ff5a72019-09-11 13:49:33 +02005106 return LY_EVALID;
5107 }
5108
Michal Vaskod989ba02020-08-24 10:59:24 +02005109 for (i = 0; i < set->used; ) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005110 switch (set->val.nodes[i].type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01005111 case LYXP_NODE_NONE:
5112 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005113 case LYXP_NODE_ELEM:
Michal Vasko03ff5a72019-09-11 13:49:33 +02005114 if (set->val.nodes[i].node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
5115 set->val.nodes[i].type = LYXP_NODE_TEXT;
5116 ++i;
5117 break;
5118 }
Radek Krejci0f969882020-08-21 16:56:47 +02005119 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005120 case LYXP_NODE_ROOT:
5121 case LYXP_NODE_ROOT_CONFIG:
5122 case LYXP_NODE_TEXT:
Michal Vasko9f96a052020-03-10 09:41:45 +01005123 case LYXP_NODE_META:
Michal Vasko03ff5a72019-09-11 13:49:33 +02005124 set_remove_node(set, i);
5125 break;
5126 }
5127 }
5128
5129 return LY_SUCCESS;
5130}
5131
5132/**
5133 * @brief Execute the XPath translate(string, string, string) function.
5134 * Returns LYXP_SET_STRING with the first argument with the characters
5135 * from the second argument replaced by those on the corresponding
5136 * positions in the third argument.
5137 *
5138 * @param[in] args Array of arguments.
5139 * @param[in] arg_count Count of elements in @p args.
5140 * @param[in,out] set Context and result set at the same time.
5141 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005142 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005143 */
5144static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005145xpath_translate(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005146{
5147 uint16_t i, j, new_used;
5148 char *new;
Radek Krejci857189e2020-09-01 13:26:36 +02005149 ly_bool have_removed;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005150 struct lysc_node_leaf *sleaf;
5151 LY_ERR rc = LY_SUCCESS;
5152
5153 if (options & LYXP_SCNODE_ALL) {
5154 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5155 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5156 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 +02005157 } else if (!warn_is_string_type(sleaf->type)) {
5158 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005159 }
5160 }
5161
5162 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5163 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5164 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 +02005165 } else if (!warn_is_string_type(sleaf->type)) {
5166 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005167 }
5168 }
5169
5170 if ((args[2]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
5171 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5172 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 +02005173 } else if (!warn_is_string_type(sleaf->type)) {
5174 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005175 }
5176 }
5177 set_scnode_clear_ctx(set);
5178 return rc;
5179 }
5180
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005181 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005182 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005183 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005184 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005185 rc = lyxp_set_cast(args[2], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005186 LY_CHECK_RET(rc);
5187
5188 new = malloc((strlen(args[0]->val.str) + 1) * sizeof(char));
5189 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5190 new_used = 0;
5191
5192 have_removed = 0;
5193 for (i = 0; args[0]->val.str[i]; ++i) {
Radek Krejci857189e2020-09-01 13:26:36 +02005194 ly_bool found = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005195
5196 for (j = 0; args[1]->val.str[j]; ++j) {
5197 if (args[0]->val.str[i] == args[1]->val.str[j]) {
5198 /* removing this char */
5199 if (j >= strlen(args[2]->val.str)) {
5200 have_removed = 1;
5201 found = 1;
5202 break;
5203 }
5204 /* replacing this char */
5205 new[new_used] = args[2]->val.str[j];
5206 ++new_used;
5207 found = 1;
5208 break;
5209 }
5210 }
5211
5212 /* copying this char */
5213 if (!found) {
5214 new[new_used] = args[0]->val.str[i];
5215 ++new_used;
5216 }
5217 }
5218
5219 if (have_removed) {
5220 new = ly_realloc(new, (new_used + 1) * sizeof(char));
5221 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5222 }
5223 new[new_used] = '\0';
5224
Michal Vaskod3678892020-05-21 10:06:58 +02005225 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005226 set->type = LYXP_SET_STRING;
5227 set->val.str = new;
5228
5229 return LY_SUCCESS;
5230}
5231
5232/**
5233 * @brief Execute the XPath true() function. Returns LYXP_SET_BOOLEAN
5234 * with true value.
5235 *
5236 * @param[in] args Array of arguments.
5237 * @param[in] arg_count Count of elements in @p args.
5238 * @param[in,out] set Context and result set at the same time.
5239 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005240 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005241 */
5242static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005243xpath_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 +02005244{
5245 if (options & LYXP_SCNODE_ALL) {
5246 set_scnode_clear_ctx(set);
5247 return LY_SUCCESS;
5248 }
5249
5250 set_fill_boolean(set, 1);
5251 return LY_SUCCESS;
5252}
5253
5254/*
5255 * moveto functions
5256 *
5257 * They and only they actually change the context (set).
5258 */
5259
5260/**
Michal Vasko6346ece2019-09-24 13:12:53 +02005261 * @brief Skip prefix and return corresponding model if there is a prefix. Logs directly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005262 *
Michal Vasko2104e9f2020-03-06 08:23:25 +01005263 * XPath @p set is expected to be a (sc)node set!
5264 *
Michal Vasko6346ece2019-09-24 13:12:53 +02005265 * @param[in,out] qname Qualified node name. If includes prefix, it is skipped.
5266 * @param[in,out] qname_len Length of @p qname, is updated accordingly.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005267 * @param[in] set Set with general XPath context.
5268 * @param[in] ctx_scnode Context node to inherit module for unprefixed node for ::LY_PREF_JSON.
Michal Vasko6346ece2019-09-24 13:12:53 +02005269 * @param[out] moveto_mod Expected module of a matching node.
5270 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005271 */
Michal Vasko6346ece2019-09-24 13:12:53 +02005272static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005273moveto_resolve_model(const char **qname, uint16_t *qname_len, const struct lyxp_set *set,
5274 const struct lysc_node *ctx_scnode, const struct lys_module **moveto_mod)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005275{
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02005276 const struct lys_module *mod = NULL;
Michal Vasko6346ece2019-09-24 13:12:53 +02005277 const char *ptr;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005278 size_t pref_len;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005279
Michal Vasko2104e9f2020-03-06 08:23:25 +01005280 assert((set->type == LYXP_SET_NODE_SET) || (set->type == LYXP_SET_SCNODE_SET));
5281
Michal Vasko6346ece2019-09-24 13:12:53 +02005282 if ((ptr = ly_strnchr(*qname, ':', *qname_len))) {
5283 /* specific module */
5284 pref_len = ptr - *qname;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005285 mod = ly_resolve_prefix(set->ctx, *qname, pref_len, set->format, set->prefix_data);
Michal Vasko6346ece2019-09-24 13:12:53 +02005286
Michal Vasko004d3152020-06-11 19:59:22 +02005287 /* check for errors and non-implemented modules, as they are not valid */
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005288 if (!mod || !mod->implemented) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005289 LOGVAL(set->ctx, LY_VCODE_XP_INMOD, pref_len, *qname);
Michal Vasko6346ece2019-09-24 13:12:53 +02005290 return LY_EVALID;
5291 }
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005292
Michal Vasko6346ece2019-09-24 13:12:53 +02005293 *qname += pref_len + 1;
5294 *qname_len -= pref_len + 1;
5295 } else if (((*qname)[0] == '*') && (*qname_len == 1)) {
5296 /* all modules - special case */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005297 mod = NULL;
Michal Vasko6346ece2019-09-24 13:12:53 +02005298 } else {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005299 switch (set->format) {
5300 case LY_PREF_SCHEMA:
5301 case LY_PREF_SCHEMA_RESOLVED:
5302 /* current module */
5303 mod = set->cur_mod;
5304 break;
5305 case LY_PREF_JSON:
5306 /* inherit parent (context node) module */
5307 if (ctx_scnode) {
5308 mod = ctx_scnode->module;
5309 } else {
5310 mod = NULL;
5311 }
5312 break;
5313 case LY_PREF_XML:
5314 /* not defined */
5315 LOGINT_RET(set->ctx);
5316 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005317 }
5318
Michal Vasko6346ece2019-09-24 13:12:53 +02005319 *moveto_mod = mod;
5320 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005321}
5322
5323/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02005324 * @brief Move context @p set to the root. Handles absolute path.
5325 * Result is LYXP_SET_NODE_SET.
5326 *
5327 * @param[in,out] set Set to use.
5328 * @param[in] options Xpath options.
Michal Vaskob0099a92020-08-31 14:55:23 +02005329 * @return LY_ERR value.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005330 */
Michal Vaskob0099a92020-08-31 14:55:23 +02005331static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005332moveto_root(struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005333{
Michal Vasko03ff5a72019-09-11 13:49:33 +02005334 if (!set) {
Michal Vaskob0099a92020-08-31 14:55:23 +02005335 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005336 }
5337
5338 if (options & LYXP_SCNODE_ALL) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005339 set_scnode_clear_ctx(set);
Michal Vaskob0099a92020-08-31 14:55:23 +02005340 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, NULL, set->root_type, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005341 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005342 set->type = LYXP_SET_NODE_SET;
5343 set->used = 0;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005344 set_insert_node(set, NULL, 0, set->root_type, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005345 }
Michal Vaskob0099a92020-08-31 14:55:23 +02005346
5347 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005348}
5349
5350/**
5351 * @brief Check @p node as a part of NameTest processing.
5352 *
5353 * @param[in] node Node to check.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005354 * @param[in] ctx_node Context node.
5355 * @param[in] set Set to read general context from.
Michal Vaskod3678892020-05-21 10:06:58 +02005356 * @param[in] node_name Node name in the dictionary to move to, NULL for any node.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005357 * @param[in] moveto_mod Expected module of the node, NULL for no prefix.
Michal Vaskocdad7122020-11-09 21:04:44 +01005358 * @param[in] options XPath options.
Michal Vasko6346ece2019-09-24 13:12:53 +02005359 * @return LY_ERR (LY_ENOT if node does not match, LY_EINCOMPLETE on unresolved when,
5360 * LY_EINVAL if netither node nor any children match)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005361 */
5362static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005363moveto_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 +01005364 const char *node_name, const struct lys_module *moveto_mod, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005365{
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005366 if (!moveto_mod && (!node_name || strcmp(node_name, "*"))) {
5367 switch (set->format) {
5368 case LY_PREF_SCHEMA:
5369 case LY_PREF_SCHEMA_RESOLVED:
5370 /* use current module */
5371 moveto_mod = set->cur_mod;
5372 break;
5373 case LY_PREF_JSON:
5374 /* inherit module of the context node, if any */
5375 if (ctx_node) {
5376 moveto_mod = ctx_node->schema->module;
5377 }
5378 break;
5379 case LY_PREF_XML:
5380 /* not defined */
5381 LOGINT(set->ctx);
5382 return LY_EINVAL;
5383 }
5384 }
5385
Michal Vasko03ff5a72019-09-11 13:49:33 +02005386 /* module check */
5387 if (moveto_mod && (node->schema->module != moveto_mod)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005388 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005389 }
5390
Michal Vasko5c4e5892019-11-14 12:31:38 +01005391 /* context check */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005392 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005393 return LY_EINVAL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005394 } else if (set->context_op && (node->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) &&
5395 (node->schema != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005396 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005397 }
5398
5399 /* name check */
Michal Vasko61ac2f62020-05-25 12:39:51 +02005400 if (node_name && strcmp(node_name, "*") && (node->schema->name != node_name)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005401 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005402 }
5403
Michal Vaskoa1424542019-11-14 16:08:52 +01005404 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01005405 if (!(options & LYXP_IGNORE_WHEN) && lysc_has_when(node->schema) && !(node->flags & LYD_WHEN_TRUE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005406 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01005407 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005408
5409 /* match */
5410 return LY_SUCCESS;
5411}
5412
5413/**
5414 * @brief Check @p node as a part of schema NameTest processing.
5415 *
5416 * @param[in] node Schema node to check.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005417 * @param[in] ctx_scnode Context node.
5418 * @param[in] set Set to read general context from.
Michal Vaskod3678892020-05-21 10:06:58 +02005419 * @param[in] node_name Node name in the dictionary to move to, NULL for any nodes.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005420 * @param[in] moveto_mod Expected module of the node, NULL for no prefix.
Michal Vasko6346ece2019-09-24 13:12:53 +02005421 * @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 +02005422 */
5423static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005424moveto_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 +02005425 const char *node_name, const struct lys_module *moveto_mod)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005426{
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005427 if (!moveto_mod && (!node_name || strcmp(node_name, "*"))) {
5428 switch (set->format) {
5429 case LY_PREF_SCHEMA:
5430 case LY_PREF_SCHEMA_RESOLVED:
5431 /* use current module */
5432 moveto_mod = set->cur_mod;
5433 break;
5434 case LY_PREF_JSON:
5435 /* inherit module of the context node, if any */
5436 if (ctx_scnode) {
5437 moveto_mod = ctx_scnode->module;
5438 }
5439 break;
5440 case LY_PREF_XML:
5441 /* not defined */
5442 LOGINT(set->ctx);
5443 return LY_EINVAL;
5444 }
5445 }
5446
Michal Vasko03ff5a72019-09-11 13:49:33 +02005447 /* module check */
Michal Vaskod3678892020-05-21 10:06:58 +02005448 if (moveto_mod && (node->module != moveto_mod)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005449 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005450 }
5451
5452 /* context check */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005453 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (node->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005454 return LY_EINVAL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005455 } else if (set->context_op && (node->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && (node != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005456 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005457 }
5458
5459 /* name check */
Michal Vasko61ac2f62020-05-25 12:39:51 +02005460 if (node_name && strcmp(node_name, "*") && (node->name != node_name)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005461 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005462 }
5463
5464 /* match */
5465 return LY_SUCCESS;
5466}
5467
5468/**
Michal Vaskod3678892020-05-21 10:06:58 +02005469 * @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 +02005470 *
5471 * @param[in,out] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005472 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005473 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01005474 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005475 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5476 */
5477static LY_ERR
Michal Vaskocdad7122020-11-09 21:04:44 +01005478moveto_node(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005479{
Michal Vaskof03ed032020-03-04 13:31:44 +01005480 uint32_t i;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005481 const struct lyd_node *siblings, *sub, *ctx_node;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005482 LY_ERR rc;
5483
Michal Vaskod3678892020-05-21 10:06:58 +02005484 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005485 return LY_SUCCESS;
5486 }
5487
5488 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005489 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005490 return LY_EVALID;
5491 }
5492
Michal Vasko03ff5a72019-09-11 13:49:33 +02005493 for (i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02005494 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005495
5496 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 +01005497 assert(!set->val.nodes[i].node);
Michal Vaskod3678892020-05-21 10:06:58 +02005498
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005499 /* search in all the trees */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005500 ctx_node = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02005501 siblings = set->tree;
Michal Vasko5bfd4be2020-06-23 13:26:19 +02005502 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005503 /* search in children */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005504 ctx_node = set->val.nodes[i].node;
5505 siblings = lyd_child(ctx_node);
Michal Vaskod3678892020-05-21 10:06:58 +02005506 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005507
Michal Vaskod3678892020-05-21 10:06:58 +02005508 for (sub = siblings; sub; sub = sub->next) {
Michal Vaskocdad7122020-11-09 21:04:44 +01005509 rc = moveto_node_check(sub, ctx_node, set, ncname, moveto_mod, options);
Michal Vaskod3678892020-05-21 10:06:58 +02005510 if (rc == LY_SUCCESS) {
5511 if (!replaced) {
5512 set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i);
5513 replaced = 1;
5514 } else {
5515 set_insert_node(set, sub, 0, LYXP_NODE_ELEM, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005516 }
Michal Vaskod3678892020-05-21 10:06:58 +02005517 ++i;
5518 } else if (rc == LY_EINCOMPLETE) {
5519 return rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005520 }
5521 }
5522
5523 if (!replaced) {
5524 /* no match */
5525 set_remove_node(set, i);
5526 }
5527 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005528
5529 return LY_SUCCESS;
5530}
5531
5532/**
Michal Vaskod3678892020-05-21 10:06:58 +02005533 * @brief Move context @p set to a node using hashes. Result is LYXP_SET_NODE_SET (or LYXP_SET_EMPTY).
5534 * Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005535 *
5536 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005537 * @param[in] scnode Matching node schema.
Michal Vasko004d3152020-06-11 19:59:22 +02005538 * @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 +01005539 * @param[in] options XPath options.
Michal Vaskod3678892020-05-21 10:06:58 +02005540 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5541 */
5542static LY_ERR
Michal Vaskocdad7122020-11-09 21:04:44 +01005543moveto_node_hash(struct lyxp_set *set, const struct lysc_node *scnode, const struct ly_path_predicate *predicates,
5544 uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02005545{
Michal Vasko004d3152020-06-11 19:59:22 +02005546 LY_ERR ret = LY_SUCCESS;
Michal Vaskod3678892020-05-21 10:06:58 +02005547 uint32_t i;
Michal Vaskod3678892020-05-21 10:06:58 +02005548 const struct lyd_node *siblings;
Michal Vasko004d3152020-06-11 19:59:22 +02005549 struct lyd_node *sub, *inst = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02005550
Michal Vasko004d3152020-06-11 19:59:22 +02005551 assert(scnode && (!(scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)) || predicates));
Michal Vaskod3678892020-05-21 10:06:58 +02005552
5553 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02005554 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005555 }
5556
5557 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005558 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko004d3152020-06-11 19:59:22 +02005559 ret = LY_EVALID;
5560 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005561 }
5562
5563 /* context check for all the nodes since we have the schema node */
5564 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) {
5565 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02005566 goto cleanup;
Michal Vasko69730152020-10-09 16:30:07 +02005567 } else if (set->context_op && (scnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) &&
5568 (scnode != set->context_op)) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005569 lyxp_set_free_content(set);
5570 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +02005571 }
5572
5573 /* create specific data instance if needed */
5574 if (scnode->nodetype == LYS_LIST) {
5575 LY_CHECK_GOTO(ret = lyd_create_list(scnode, predicates, &inst), cleanup);
5576 } else if (scnode->nodetype == LYS_LEAFLIST) {
5577 LY_CHECK_GOTO(ret = lyd_create_term2(scnode, &predicates[0].value, &inst), cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02005578 }
5579
5580 for (i = 0; i < set->used; ) {
Michal Vaskod3678892020-05-21 10:06:58 +02005581 siblings = NULL;
5582
5583 if ((set->val.nodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.nodes[i].type == LYXP_NODE_ROOT)) {
5584 assert(!set->val.nodes[i].node);
5585
5586 /* search in all the trees */
5587 siblings = set->tree;
5588 } else if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
5589 /* search in children */
Radek Krejcia1c1e542020-09-29 16:06:52 +02005590 siblings = lyd_child(set->val.nodes[i].node);
Michal Vaskod3678892020-05-21 10:06:58 +02005591 }
5592
5593 /* find the node using hashes */
Michal Vasko004d3152020-06-11 19:59:22 +02005594 if (inst) {
5595 lyd_find_sibling_first(siblings, inst, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02005596 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02005597 lyd_find_sibling_val(siblings, scnode, NULL, 0, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02005598 }
5599
5600 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01005601 if (!(options & LYXP_IGNORE_WHEN) && sub && lysc_has_when(sub->schema) && !(sub->flags & LYD_WHEN_TRUE)) {
Michal Vasko004d3152020-06-11 19:59:22 +02005602 ret = LY_EINCOMPLETE;
5603 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005604 }
5605
5606 if (sub) {
5607 /* pos filled later */
Michal Vaskocb1b7c02020-07-03 13:38:12 +02005608 set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i);
Michal Vaskod3678892020-05-21 10:06:58 +02005609 ++i;
Michal Vaskocb1b7c02020-07-03 13:38:12 +02005610 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005611 /* no match */
5612 set_remove_node(set, i);
5613 }
5614 }
5615
Michal Vasko004d3152020-06-11 19:59:22 +02005616cleanup:
5617 lyd_free_tree(inst);
5618 return ret;
Michal Vaskod3678892020-05-21 10:06:58 +02005619}
5620
5621/**
5622 * @brief Move context @p set to a schema node. Result is LYXP_SET_SCNODE_SET (or LYXP_SET_EMPTY).
5623 *
5624 * @param[in,out] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005625 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005626 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005627 * @param[in] options XPath options.
5628 * @return LY_ERR
5629 */
5630static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005631moveto_scnode(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005632{
Radek Krejci857189e2020-09-01 13:26:36 +02005633 ly_bool temp_ctx = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005634 uint32_t getnext_opts;
5635 uint32_t orig_used, i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005636 uint32_t mod_idx;
Michal Vasko519fd602020-05-26 12:17:39 +02005637 const struct lysc_node *iter, *start_parent;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005638 const struct lys_module *mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005639
Michal Vaskod3678892020-05-21 10:06:58 +02005640 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005641 return LY_SUCCESS;
5642 }
5643
5644 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005645 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005646 return LY_EVALID;
5647 }
5648
Michal Vaskocafad9d2019-11-07 15:20:03 +01005649 /* getnext opts */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01005650 getnext_opts = 0;
Michal Vaskocafad9d2019-11-07 15:20:03 +01005651 if (options & LYXP_SCNODE_OUTPUT) {
5652 getnext_opts |= LYS_GETNEXT_OUTPUT;
5653 }
5654
Michal Vasko03ff5a72019-09-11 13:49:33 +02005655 orig_used = set->used;
5656 for (i = 0; i < orig_used; ++i) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005657 uint32_t idx;
5658
Radek Krejcif13b87b2020-12-01 22:02:17 +01005659 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
5660 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01005661 continue;
5662 }
5663
5664 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01005665 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vaskoec4df482019-12-16 10:02:18 +01005666 } else {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005667 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005668 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005669
5670 start_parent = set->val.scnodes[i].scnode;
5671
5672 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 +02005673 /* 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 +02005674 * it can be in a top-level augment (the root node itself is useless in this case) */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005675 mod_idx = 0;
Michal Vasko9e9f26d2020-10-12 16:31:33 +02005676 while ((mod = (struct lys_module *)ly_ctx_get_module_iter(set->ctx, &mod_idx))) {
Michal Vasko519fd602020-05-26 12:17:39 +02005677 iter = NULL;
Michal Vasko509de4d2019-12-10 14:51:30 +01005678 /* module may not be implemented */
Michal Vasko519fd602020-05-26 12:17:39 +02005679 while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005680 if (!moveto_scnode_check(iter, NULL, set, ncname, moveto_mod)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005681 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, &idx));
5682
Michal Vasko03ff5a72019-09-11 13:49:33 +02005683 /* we need to prevent these nodes from being considered in this moveto */
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005684 if ((idx < orig_used) && (idx > i)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005685 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005686 temp_ctx = 1;
5687 }
5688 }
5689 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005690 }
5691
Michal Vasko519fd602020-05-26 12:17:39 +02005692 } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
5693 iter = NULL;
5694 while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005695 if (!moveto_scnode_check(iter, start_parent, set, ncname, moveto_mod)) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005696 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, &idx));
5697
5698 if ((idx < orig_used) && (idx > i)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005699 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005700 temp_ctx = 1;
5701 }
5702 }
5703 }
5704 }
5705 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005706
5707 /* correct temporary in_ctx values */
5708 if (temp_ctx) {
5709 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005710 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_NEW_CTX) {
5711 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005712 }
5713 }
5714 }
5715
5716 return LY_SUCCESS;
5717}
5718
5719/**
Michal Vaskod3678892020-05-21 10:06:58 +02005720 * @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 +02005721 * Context position aware.
5722 *
5723 * @param[in] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005724 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005725 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01005726 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005727 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5728 */
5729static LY_ERR
Michal Vaskocdad7122020-11-09 21:04:44 +01005730moveto_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 +02005731{
5732 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005733 const struct lyd_node *next, *elem, *start;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005734 struct lyxp_set ret_set;
5735 LY_ERR rc;
5736
Michal Vaskod3678892020-05-21 10:06:58 +02005737 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005738 return LY_SUCCESS;
5739 }
5740
5741 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005742 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005743 return LY_EVALID;
5744 }
5745
Michal Vasko9f96a052020-03-10 09:41:45 +01005746 /* 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 +01005747 rc = moveto_node(set, NULL, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005748 LY_CHECK_RET(rc);
5749
Michal Vasko6346ece2019-09-24 13:12:53 +02005750 /* this loop traverses all the nodes in the set and adds/keeps only those that match qname */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005751 set_init(&ret_set, set);
5752 for (i = 0; i < set->used; ++i) {
5753
5754 /* TREE DFS */
5755 start = set->val.nodes[i].node;
5756 for (elem = next = start; elem; elem = next) {
Michal Vaskocdad7122020-11-09 21:04:44 +01005757 rc = moveto_node_check(elem, start, set, ncname, moveto_mod, options);
Michal Vasko6346ece2019-09-24 13:12:53 +02005758 if (!rc) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005759 /* add matching node into result set */
5760 set_insert_node(&ret_set, elem, 0, LYXP_NODE_ELEM, ret_set.used);
5761 if (set_dup_node_check(set, elem, LYXP_NODE_ELEM, i)) {
5762 /* the node is a duplicate, we'll process it later in the set */
5763 goto skip_children;
5764 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005765 } else if (rc == LY_EINCOMPLETE) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005766 return rc;
5767 } else if (rc == LY_EINVAL) {
5768 goto skip_children;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005769 }
5770
5771 /* TREE DFS NEXT ELEM */
5772 /* select element for the next run - children first */
Radek Krejcia1c1e542020-09-29 16:06:52 +02005773 next = lyd_child(elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005774 if (!next) {
5775skip_children:
5776 /* no children, so try siblings, but only if it's not the start,
5777 * that is considered to be the root and it's siblings are not traversed */
5778 if (elem != start) {
5779 next = elem->next;
5780 } else {
5781 break;
5782 }
5783 }
5784 while (!next) {
5785 /* no siblings, go back through the parents */
Michal Vasko9e685082021-01-29 14:49:09 +01005786 if (lyd_parent(elem) == start) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005787 /* we are done, no next element to process */
5788 break;
5789 }
5790 /* parent is already processed, go to its sibling */
Michal Vasko9e685082021-01-29 14:49:09 +01005791 elem = lyd_parent(elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005792 next = elem->next;
5793 }
5794 }
5795 }
5796
5797 /* make the temporary set the current one */
5798 ret_set.ctx_pos = set->ctx_pos;
5799 ret_set.ctx_size = set->ctx_size;
Michal Vaskod3678892020-05-21 10:06:58 +02005800 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005801 memcpy(set, &ret_set, sizeof *set);
5802
5803 return LY_SUCCESS;
5804}
5805
5806/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005807 * @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 +02005808 *
5809 * @param[in] set Set to use.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005810 * @param[in] moveto_mod Matching node module, NULL for no prefix.
Michal Vaskod3678892020-05-21 10:06:58 +02005811 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005812 * @param[in] options XPath options.
5813 * @return LY_ERR
5814 */
5815static LY_ERR
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005816moveto_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 +02005817{
Radek Krejci1deb5be2020-08-26 16:43:36 +02005818 uint32_t i, orig_used;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005819 const struct lysc_node *next, *elem, *start;
Michal Vasko6346ece2019-09-24 13:12:53 +02005820 LY_ERR rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005821
Michal Vaskod3678892020-05-21 10:06:58 +02005822 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005823 return LY_SUCCESS;
5824 }
5825
5826 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005827 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005828 return LY_EVALID;
5829 }
5830
Michal Vasko03ff5a72019-09-11 13:49:33 +02005831 orig_used = set->used;
5832 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005833 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
5834 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01005835 continue;
5836 }
5837
5838 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01005839 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vaskoec4df482019-12-16 10:02:18 +01005840 } else {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005841 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005842 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005843
5844 /* TREE DFS */
5845 start = set->val.scnodes[i].scnode;
5846 for (elem = next = start; elem; elem = next) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005847 if ((elem == start) || (elem->nodetype & (LYS_CHOICE | LYS_CASE))) {
5848 /* schema-only nodes, skip root */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005849 goto next_iter;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005850 }
5851
Michal Vasko5d24f6c2020-10-13 13:49:06 +02005852 rc = moveto_scnode_check(elem, start, set, ncname, moveto_mod);
Michal Vasko6346ece2019-09-24 13:12:53 +02005853 if (!rc) {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005854 uint32_t idx;
5855
5856 if (lyxp_set_scnode_contains(set, elem, LYXP_NODE_ELEM, i, &idx)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01005857 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005858 if ((uint32_t)idx > i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005859 /* we will process it later in the set */
5860 goto skip_children;
5861 }
5862 } else {
Radek Krejciaa6b53f2020-08-27 15:19:03 +02005863 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, elem, LYXP_NODE_ELEM, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005864 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005865 } else if (rc == LY_EINVAL) {
5866 goto skip_children;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005867 }
5868
5869next_iter:
5870 /* TREE DFS NEXT ELEM */
5871 /* select element for the next run - children first */
Michal Vasko544e58a2021-01-28 14:33:41 +01005872 next = lysc_node_child(elem);
5873 if (next && (next->nodetype == LYS_INPUT) && (options & LYXP_SCNODE_OUTPUT)) {
5874 next = next->next;
5875 } else if (next && (next->nodetype == LYS_OUTPUT) && !(options & LYXP_SCNODE_OUTPUT)) {
5876 next = next->next;
5877 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005878 if (!next) {
5879skip_children:
5880 /* no children, so try siblings, but only if it's not the start,
5881 * that is considered to be the root and it's siblings are not traversed */
5882 if (elem != start) {
5883 next = elem->next;
5884 } else {
5885 break;
5886 }
5887 }
5888 while (!next) {
5889 /* no siblings, go back through the parents */
5890 if (elem->parent == start) {
5891 /* we are done, no next element to process */
5892 break;
5893 }
5894 /* parent is already processed, go to its sibling */
5895 elem = elem->parent;
5896 next = elem->next;
5897 }
5898 }
5899 }
5900
5901 return LY_SUCCESS;
5902}
5903
5904/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005905 * @brief Move context @p set to an attribute. Result is LYXP_SET_NODE_SET.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005906 * Indirectly context position aware.
5907 *
5908 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005909 * @param[in] mod Matching metadata module, NULL for any.
5910 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005911 * @return LY_ERR
5912 */
5913static LY_ERR
Michal Vaskod3678892020-05-21 10:06:58 +02005914moveto_attr(struct lyxp_set *set, const struct lys_module *mod, const char *ncname)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005915{
Michal Vasko9f96a052020-03-10 09:41:45 +01005916 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005917
Michal Vaskod3678892020-05-21 10:06:58 +02005918 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005919 return LY_SUCCESS;
5920 }
5921
5922 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005923 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005924 return LY_EVALID;
5925 }
5926
Radek Krejci1deb5be2020-08-26 16:43:36 +02005927 for (uint32_t i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02005928 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005929
5930 /* only attributes of an elem (not dummy) can be in the result, skip all the rest;
5931 * our attributes are always qualified */
Michal Vasko5c4e5892019-11-14 12:31:38 +01005932 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01005933 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005934
5935 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02005936 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005937 continue;
5938 }
5939
Michal Vaskod3678892020-05-21 10:06:58 +02005940 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005941 /* match */
5942 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01005943 set->val.meta[i].meta = sub;
5944 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005945 /* pos does not change */
5946 replaced = 1;
5947 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01005948 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 +02005949 }
5950 ++i;
5951 }
5952 }
5953 }
5954
5955 if (!replaced) {
5956 /* no match */
5957 set_remove_node(set, i);
5958 }
5959 }
5960
5961 return LY_SUCCESS;
5962}
5963
5964/**
5965 * @brief Move context @p set1 to union with @p set2. @p set2 is emptied afterwards.
Michal Vasko61ac2f62020-05-25 12:39:51 +02005966 * Result is LYXP_SET_NODE_SET. Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005967 *
5968 * @param[in,out] set1 Set to use for the result.
5969 * @param[in] set2 Set that is copied to @p set1.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005970 * @return LY_ERR
5971 */
5972static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005973moveto_union(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005974{
5975 LY_ERR rc;
5976
Michal Vaskod3678892020-05-21 10:06:58 +02005977 if ((set1->type != LYXP_SET_NODE_SET) || (set2->type != LYXP_SET_NODE_SET)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01005978 LOGVAL(set1->ctx, LY_VCODE_XP_INOP_2, "union", print_set_type(set1), print_set_type(set2));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005979 return LY_EVALID;
5980 }
5981
5982 /* set2 is empty or both set1 and set2 */
Michal Vaskod3678892020-05-21 10:06:58 +02005983 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005984 return LY_SUCCESS;
5985 }
5986
Michal Vaskod3678892020-05-21 10:06:58 +02005987 if (!set1->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005988 memcpy(set1, set2, sizeof *set1);
5989 /* dynamic memory belongs to set1 now, do not free */
Michal Vaskod3678892020-05-21 10:06:58 +02005990 memset(set2, 0, sizeof *set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005991 return LY_SUCCESS;
5992 }
5993
5994 /* we assume sets are sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005995 assert(!set_sort(set1) && !set_sort(set2));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005996
5997 /* sort, remove duplicates */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005998 rc = set_sorted_merge(set1, set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005999 LY_CHECK_RET(rc);
6000
6001 /* final set must be sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006002 assert(!set_sort(set1));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006003
6004 return LY_SUCCESS;
6005}
6006
6007/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02006008 * @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 +02006009 * Context position aware.
6010 *
6011 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02006012 * @param[in] mod Matching metadata module, NULL for any.
6013 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
Michal Vaskocdad7122020-11-09 21:04:44 +01006014 * @param[in] options XPath options.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006015 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6016 */
6017static int
Michal Vaskocdad7122020-11-09 21:04:44 +01006018moveto_attr_alldesc(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006019{
Michal Vasko9f96a052020-03-10 09:41:45 +01006020 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006021 struct lyxp_set *set_all_desc = NULL;
6022 LY_ERR rc;
6023
Michal Vaskod3678892020-05-21 10:06:58 +02006024 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006025 return LY_SUCCESS;
6026 }
6027
6028 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006029 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006030 return LY_EVALID;
6031 }
6032
Michal Vasko03ff5a72019-09-11 13:49:33 +02006033 /* can be optimized similarly to moveto_node_alldesc() and save considerable amount of memory,
6034 * but it likely won't be used much, so it's a waste of time */
6035 /* copy the context */
6036 set_all_desc = set_copy(set);
6037 /* get all descendant nodes (the original context nodes are removed) */
Michal Vaskocdad7122020-11-09 21:04:44 +01006038 rc = moveto_node_alldesc(set_all_desc, NULL, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006039 if (rc != LY_SUCCESS) {
6040 lyxp_set_free(set_all_desc);
6041 return rc;
6042 }
6043 /* prepend the original context nodes */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006044 rc = moveto_union(set, set_all_desc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006045 if (rc != LY_SUCCESS) {
6046 lyxp_set_free(set_all_desc);
6047 return rc;
6048 }
6049 lyxp_set_free(set_all_desc);
6050
Radek Krejci1deb5be2020-08-26 16:43:36 +02006051 for (uint32_t i = 0; i < set->used; ) {
Radek Krejci857189e2020-09-01 13:26:36 +02006052 ly_bool replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006053
6054 /* only attributes of an elem can be in the result, skip all the rest,
6055 * we have all attributes qualified in lyd tree */
6056 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01006057 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006058 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02006059 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006060 continue;
6061 }
6062
Michal Vaskod3678892020-05-21 10:06:58 +02006063 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006064 /* match */
6065 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01006066 set->val.meta[i].meta = sub;
6067 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006068 /* pos does not change */
6069 replaced = 1;
6070 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01006071 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 +02006072 }
6073 ++i;
6074 }
6075 }
6076 }
6077
6078 if (!replaced) {
6079 /* no match */
6080 set_remove_node(set, i);
6081 }
6082 }
6083
6084 return LY_SUCCESS;
6085}
6086
6087/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02006088 * @brief Move context @p set to self and al chilren, recursively. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET.
6089 * Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006090 *
6091 * @param[in] parent Current parent.
6092 * @param[in] parent_pos Position of @p parent.
6093 * @param[in] parent_type Node type of @p parent.
6094 * @param[in,out] to_set Set to use.
6095 * @param[in] dup_check_set Set for checking duplicities.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006096 * @param[in] options XPath options.
6097 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6098 */
6099static LY_ERR
6100moveto_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 +02006101 struct lyxp_set *to_set, const struct lyxp_set *dup_check_set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006102{
Michal Vasko61ac2f62020-05-25 12:39:51 +02006103 const struct lyd_node *iter, *first;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006104 LY_ERR rc;
6105
6106 switch (parent_type) {
6107 case LYXP_NODE_ROOT:
6108 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko61ac2f62020-05-25 12:39:51 +02006109 assert(!parent);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006110
Michal Vasko61ac2f62020-05-25 12:39:51 +02006111 /* add all top-level nodes as elements */
6112 first = to_set->tree;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006113 break;
6114 case LYXP_NODE_ELEM:
Michal Vasko61ac2f62020-05-25 12:39:51 +02006115 /* add just the text node of this term element node */
6116 if (parent->schema->nodetype & (LYD_NODE_TERM | LYD_NODE_ANY)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006117 if (!set_dup_node_check(dup_check_set, parent, LYXP_NODE_TEXT, -1)) {
6118 set_insert_node(to_set, parent, parent_pos, LYXP_NODE_TEXT, to_set->used);
6119 }
Michal Vasko61ac2f62020-05-25 12:39:51 +02006120 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006121 }
Michal Vasko61ac2f62020-05-25 12:39:51 +02006122
6123 /* add all the children of this node */
Radek Krejcia1c1e542020-09-29 16:06:52 +02006124 first = lyd_child(parent);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006125 break;
6126 default:
6127 LOGINT_RET(parent->schema->module->ctx);
6128 }
6129
Michal Vasko61ac2f62020-05-25 12:39:51 +02006130 /* add all top-level nodes as elements */
6131 LY_LIST_FOR(first, iter) {
6132 /* context check */
6133 if ((parent_type == LYXP_NODE_ROOT_CONFIG) && (iter->schema->flags & LYS_CONFIG_R)) {
6134 continue;
6135 }
6136
6137 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01006138 if (!(options & LYXP_IGNORE_WHEN) && lysc_has_when(iter->schema) && !(iter->flags & LYD_WHEN_TRUE)) {
Michal Vasko61ac2f62020-05-25 12:39:51 +02006139 return LY_EINCOMPLETE;
6140 }
6141
6142 if (!set_dup_node_check(dup_check_set, iter, LYXP_NODE_ELEM, -1)) {
6143 set_insert_node(to_set, iter, 0, LYXP_NODE_ELEM, to_set->used);
6144
6145 /* also add all the children of this node, recursively */
6146 rc = moveto_self_add_children_r(iter, 0, LYXP_NODE_ELEM, to_set, dup_check_set, options);
6147 LY_CHECK_RET(rc);
6148 }
6149 }
6150
Michal Vasko03ff5a72019-09-11 13:49:33 +02006151 return LY_SUCCESS;
6152}
6153
6154/**
6155 * @brief Move context @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET
6156 * (or LYXP_SET_EMPTY). Context position aware.
6157 *
6158 * @param[in,out] set Set to use.
6159 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6160 * @param[in] options XPath options.
6161 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6162 */
6163static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006164moveto_self(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006165{
Michal Vasko03ff5a72019-09-11 13:49:33 +02006166 struct lyxp_set ret_set;
6167 LY_ERR rc;
6168
Michal Vaskod3678892020-05-21 10:06:58 +02006169 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006170 return LY_SUCCESS;
6171 }
6172
6173 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006174 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006175 return LY_EVALID;
6176 }
6177
6178 /* nothing to do */
6179 if (!all_desc) {
6180 return LY_SUCCESS;
6181 }
6182
Michal Vasko03ff5a72019-09-11 13:49:33 +02006183 /* add all the children, they get added recursively */
6184 set_init(&ret_set, set);
Radek Krejci1deb5be2020-08-26 16:43:36 +02006185 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006186 /* copy the current node to tmp */
6187 set_insert_node(&ret_set, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, ret_set.used);
6188
6189 /* do not touch attributes and text nodes */
Michal Vasko9f96a052020-03-10 09:41:45 +01006190 if ((set->val.nodes[i].type == LYXP_NODE_TEXT) || (set->val.nodes[i].type == LYXP_NODE_META)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006191 continue;
6192 }
6193
Michal Vasko03ff5a72019-09-11 13:49:33 +02006194 /* add all the children */
6195 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 +02006196 set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006197 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006198 lyxp_set_free_content(&ret_set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006199 return rc;
6200 }
6201 }
6202
6203 /* use the temporary set as the current one */
6204 ret_set.ctx_pos = set->ctx_pos;
6205 ret_set.ctx_size = set->ctx_size;
Michal Vaskod3678892020-05-21 10:06:58 +02006206 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006207 memcpy(set, &ret_set, sizeof *set);
6208
6209 return LY_SUCCESS;
6210}
6211
6212/**
6213 * @brief Move context schema @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_SCNODE_SET
6214 * (or LYXP_SET_EMPTY).
6215 *
6216 * @param[in,out] set Set to use.
6217 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6218 * @param[in] options XPath options.
6219 * @return LY_ERR
6220 */
6221static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006222moveto_scnode_self(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006223{
Radek Krejci1deb5be2020-08-26 16:43:36 +02006224 uint32_t getnext_opts;
6225 uint32_t mod_idx;
Michal Vasko519fd602020-05-26 12:17:39 +02006226 const struct lysc_node *iter, *start_parent;
6227 const struct lys_module *mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006228
Michal Vaskod3678892020-05-21 10:06:58 +02006229 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006230 return LY_SUCCESS;
6231 }
6232
6233 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006234 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006235 return LY_EVALID;
6236 }
6237
Michal Vasko519fd602020-05-26 12:17:39 +02006238 /* getnext opts */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01006239 getnext_opts = 0;
Michal Vasko519fd602020-05-26 12:17:39 +02006240 if (options & LYXP_SCNODE_OUTPUT) {
6241 getnext_opts |= LYS_GETNEXT_OUTPUT;
6242 }
6243
6244 /* add all the children, recursively as they are being added into the same set */
Radek Krejci1deb5be2020-08-26 16:43:36 +02006245 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vaskoe657f962020-12-10 12:19:48 +01006246 if (!all_desc) {
6247 /* traverse the start node */
6248 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_START) {
6249 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
6250 }
6251 continue;
6252 }
6253
Radek Krejcif13b87b2020-12-01 22:02:17 +01006254 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
6255 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006256 continue;
6257 }
6258
Michal Vasko519fd602020-05-26 12:17:39 +02006259 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01006260 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vasko519fd602020-05-26 12:17:39 +02006261 } else {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006262 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006263 }
6264
Michal Vasko519fd602020-05-26 12:17:39 +02006265 start_parent = set->val.scnodes[i].scnode;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006266
Michal Vasko519fd602020-05-26 12:17:39 +02006267 if ((set->val.scnodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.scnodes[i].type == LYXP_NODE_ROOT)) {
6268 /* it can actually be in any module, it's all <running> */
6269 mod_idx = 0;
6270 while ((mod = (struct lys_module *)ly_ctx_get_module_iter(set->ctx, &mod_idx))) {
6271 iter = NULL;
6272 /* module may not be implemented */
6273 while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) {
6274 /* context check */
6275 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) {
6276 continue;
6277 }
6278
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006279 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, NULL));
Michal Vasko519fd602020-05-26 12:17:39 +02006280 /* throw away the insert index, we want to consider that node again, recursively */
6281 }
6282 }
6283
6284 } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
6285 iter = NULL;
6286 while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006287 /* context check */
Michal Vasko519fd602020-05-26 12:17:39 +02006288 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006289 continue;
6290 }
6291
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006292 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM, NULL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006293 }
6294 }
6295 }
6296
6297 return LY_SUCCESS;
6298}
6299
6300/**
6301 * @brief Move context @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_NODE_SET
6302 * (or LYXP_SET_EMPTY). Context position aware.
6303 *
6304 * @param[in] set Set to use.
6305 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6306 * @param[in] options XPath options.
6307 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6308 */
6309static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006310moveto_parent(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006311{
6312 LY_ERR rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006313 struct lyd_node *node, *new_node;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006314 enum lyxp_node_type new_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006315
Michal Vaskod3678892020-05-21 10:06:58 +02006316 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006317 return LY_SUCCESS;
6318 }
6319
6320 if (set->type != LYXP_SET_NODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006321 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006322 return LY_EVALID;
6323 }
6324
6325 if (all_desc) {
6326 /* <path>//.. == <path>//./.. */
6327 rc = moveto_self(set, 1, options);
6328 LY_CHECK_RET(rc);
6329 }
6330
Radek Krejci1deb5be2020-08-26 16:43:36 +02006331 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006332 node = set->val.nodes[i].node;
6333
6334 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9e685082021-01-29 14:49:09 +01006335 new_node = lyd_parent(node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006336 } else if (set->val.nodes[i].type == LYXP_NODE_TEXT) {
6337 new_node = node;
Michal Vasko9f96a052020-03-10 09:41:45 +01006338 } else if (set->val.nodes[i].type == LYXP_NODE_META) {
6339 new_node = set->val.meta[i].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006340 if (!new_node) {
6341 LOGINT_RET(set->ctx);
6342 }
6343 } else {
6344 /* root does not have a parent */
Michal Vasko2caefc12019-11-14 16:07:56 +01006345 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006346 continue;
6347 }
6348
Michal Vaskoa1424542019-11-14 16:08:52 +01006349 /* when check */
Michal Vaskod5cfa6e2020-11-23 16:56:08 +01006350 if (!(options & LYXP_IGNORE_WHEN) && new_node && lysc_has_when(new_node->schema) && !(new_node->flags & LYD_WHEN_TRUE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006351 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01006352 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006353
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006354 if (!new_node) {
Radek Krejcif6a11002020-08-21 13:29:07 +02006355 /* node already there can also be the root */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006356 new_type = set->root_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006357
Michal Vasko03ff5a72019-09-11 13:49:33 +02006358 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02006359 /* 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 +02006360 new_type = LYXP_NODE_ELEM;
6361 }
6362
Michal Vasko03ff5a72019-09-11 13:49:33 +02006363 if (set_dup_node_check(set, new_node, new_type, -1)) {
Michal Vasko2caefc12019-11-14 16:07:56 +01006364 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006365 } else {
6366 set_replace_node(set, new_node, 0, new_type, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006367 }
6368 }
6369
Michal Vasko2caefc12019-11-14 16:07:56 +01006370 set_remove_nodes_none(set);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006371 assert(!set_sort(set) && !set_sorted_dup_node_clean(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006372
6373 return LY_SUCCESS;
6374}
6375
6376/**
6377 * @brief Move context schema @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_SCNODE_SET
6378 * (or LYXP_SET_EMPTY).
6379 *
6380 * @param[in] set Set to use.
6381 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6382 * @param[in] options XPath options.
6383 * @return LY_ERR
6384 */
6385static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +02006386moveto_scnode_parent(struct lyxp_set *set, ly_bool all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006387{
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006388 uint32_t i, orig_used, idx;
Radek Krejci857189e2020-09-01 13:26:36 +02006389 ly_bool temp_ctx = 0;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006390 const struct lysc_node *node, *new_node;
6391 enum lyxp_node_type new_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006392
Michal Vaskod3678892020-05-21 10:06:58 +02006393 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006394 return LY_SUCCESS;
6395 }
6396
6397 if (set->type != LYXP_SET_SCNODE_SET) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01006398 LOGVAL(set->ctx, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006399 return LY_EVALID;
6400 }
6401
6402 if (all_desc) {
6403 /* <path>//.. == <path>//./.. */
Radek Krejci1deb5be2020-08-26 16:43:36 +02006404 LY_CHECK_RET(moveto_scnode_self(set, 1, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006405 }
6406
Michal Vasko03ff5a72019-09-11 13:49:33 +02006407 orig_used = set->used;
6408 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006409 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_ATOM_CTX) {
6410 if (set->val.scnodes[i].in_ctx != LYXP_SET_SCNODE_START) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006411 continue;
6412 }
6413
6414 /* remember context node */
Radek Krejcif13b87b2020-12-01 22:02:17 +01006415 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_START_USED;
Michal Vaskoec4df482019-12-16 10:02:18 +01006416 } else {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006417 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006418 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006419
6420 node = set->val.scnodes[i].scnode;
6421
6422 if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
Michal Vaskod3678892020-05-21 10:06:58 +02006423 new_node = lysc_data_parent(node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006424 } else {
6425 /* root does not have a parent */
6426 continue;
6427 }
6428
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006429 if (!new_node) {
Radek Krejcif6a11002020-08-21 13:29:07 +02006430 /* node has no parent */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006431 new_type = set->root_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006432
Michal Vasko03ff5a72019-09-11 13:49:33 +02006433 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02006434 /* 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 +02006435 new_type = LYXP_NODE_ELEM;
6436 }
6437
Radek Krejciaa6b53f2020-08-27 15:19:03 +02006438 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, new_node, new_type, &idx));
6439 if ((idx < orig_used) && (idx > i)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006440 set->val.scnodes[idx].in_ctx = LYXP_SET_SCNODE_ATOM_NEW_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006441 temp_ctx = 1;
6442 }
6443 }
6444
6445 if (temp_ctx) {
6446 for (i = 0; i < orig_used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006447 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_NEW_CTX) {
6448 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006449 }
6450 }
6451 }
6452
6453 return LY_SUCCESS;
6454}
6455
6456/**
6457 * @brief Move context @p set to the result of a comparison. Handles '=', '!=', '<=', '<', '>=', or '>'.
6458 * Result is LYXP_SET_BOOLEAN. Indirectly context position aware.
6459 *
6460 * @param[in,out] set1 Set to use for the result.
6461 * @param[in] set2 Set acting as the second operand for @p op.
6462 * @param[in] op Comparison operator to process.
6463 * @param[in] options XPath options.
6464 * @return LY_ERR
6465 */
6466static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02006467moveto_op_comp(struct lyxp_set *set1, struct lyxp_set *set2, const char *op, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006468{
6469 /*
6470 * NODE SET + NODE SET = NODE SET + STRING /(1 NODE SET) 2 STRING
6471 * NODE SET + STRING = STRING + STRING /1 STRING (2 STRING)
6472 * NODE SET + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6473 * NODE SET + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6474 * STRING + NODE SET = STRING + STRING /(1 STRING) 2 STRING
6475 * NUMBER + NODE SET = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6476 * BOOLEAN + NODE SET = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6477 *
6478 * '=' or '!='
6479 * BOOLEAN + BOOLEAN
6480 * BOOLEAN + STRING = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6481 * BOOLEAN + NUMBER = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6482 * STRING + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6483 * NUMBER + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6484 * NUMBER + NUMBER
6485 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6486 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6487 * STRING + STRING
6488 *
6489 * '<=', '<', '>=', '>'
6490 * NUMBER + NUMBER
6491 * BOOLEAN + BOOLEAN = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6492 * BOOLEAN + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6493 * BOOLEAN + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6494 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6495 * STRING + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6496 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6497 * NUMBER + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6498 * STRING + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6499 */
6500 struct lyxp_set iter1, iter2;
6501 int result;
6502 int64_t i;
6503 LY_ERR rc;
6504
Michal Vasko004d3152020-06-11 19:59:22 +02006505 memset(&iter1, 0, sizeof iter1);
6506 memset(&iter2, 0, sizeof iter2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006507
6508 /* iterative evaluation with node-sets */
6509 if ((set1->type == LYXP_SET_NODE_SET) || (set2->type == LYXP_SET_NODE_SET)) {
6510 if (set1->type == LYXP_SET_NODE_SET) {
6511 for (i = 0; i < set1->used; ++i) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006512 /* cast set1 */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006513 switch (set2->type) {
6514 case LYXP_SET_NUMBER:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006515 rc = set_comp_cast(&iter1, set1, LYXP_SET_NUMBER, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006516 break;
6517 case LYXP_SET_BOOLEAN:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006518 rc = set_comp_cast(&iter1, set1, LYXP_SET_BOOLEAN, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006519 break;
6520 default:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006521 rc = set_comp_cast(&iter1, set1, LYXP_SET_STRING, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006522 break;
6523 }
6524 LY_CHECK_RET(rc);
6525
Michal Vasko4c7763f2020-07-27 17:40:37 +02006526 /* canonize set2 */
6527 LY_CHECK_ERR_RET(rc = set_comp_canonize(&iter2, set2, &set1->val.nodes[i]), lyxp_set_free_content(&iter1), rc);
6528
6529 /* compare recursively */
6530 rc = moveto_op_comp(&iter1, &iter2, op, options);
6531 lyxp_set_free_content(&iter2);
6532 LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006533
6534 /* lazy evaluation until true */
Michal Vasko004d3152020-06-11 19:59:22 +02006535 if (iter1.val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006536 set_fill_boolean(set1, 1);
6537 return LY_SUCCESS;
6538 }
6539 }
6540 } else {
6541 for (i = 0; i < set2->used; ++i) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006542 /* set set2 */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006543 switch (set1->type) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006544 case LYXP_SET_NUMBER:
6545 rc = set_comp_cast(&iter2, set2, LYXP_SET_NUMBER, i);
6546 break;
6547 case LYXP_SET_BOOLEAN:
6548 rc = set_comp_cast(&iter2, set2, LYXP_SET_BOOLEAN, i);
6549 break;
6550 default:
6551 rc = set_comp_cast(&iter2, set2, LYXP_SET_STRING, i);
6552 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006553 }
6554 LY_CHECK_RET(rc);
6555
Michal Vasko4c7763f2020-07-27 17:40:37 +02006556 /* canonize set1 */
6557 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 +02006558
Michal Vasko4c7763f2020-07-27 17:40:37 +02006559 /* compare recursively */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006560 rc = moveto_op_comp(&iter1, &iter2, op, options);
Michal Vaskod3678892020-05-21 10:06:58 +02006561 lyxp_set_free_content(&iter2);
Michal Vasko4c7763f2020-07-27 17:40:37 +02006562 LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006563
6564 /* lazy evaluation until true */
Michal Vasko004d3152020-06-11 19:59:22 +02006565 if (iter1.val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006566 set_fill_boolean(set1, 1);
6567 return LY_SUCCESS;
6568 }
6569 }
6570 }
6571
6572 /* false for all nodes */
6573 set_fill_boolean(set1, 0);
6574 return LY_SUCCESS;
6575 }
6576
6577 /* first convert properly */
6578 if ((op[0] == '=') || (op[0] == '!')) {
6579 if ((set1->type == LYXP_SET_BOOLEAN) || (set2->type == LYXP_SET_BOOLEAN)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006580 lyxp_set_cast(set1, LYXP_SET_BOOLEAN);
6581 lyxp_set_cast(set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006582 } else if ((set1->type == LYXP_SET_NUMBER) || (set2->type == LYXP_SET_NUMBER)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006583 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006584 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006585 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006586 LY_CHECK_RET(rc);
6587 } /* else we have 2 strings */
6588 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006589 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006590 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006591 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006592 LY_CHECK_RET(rc);
6593 }
6594
6595 assert(set1->type == set2->type);
6596
6597 /* compute result */
6598 if (op[0] == '=') {
6599 if (set1->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02006600 result = (set1->val.bln == set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006601 } else if (set1->type == LYXP_SET_NUMBER) {
6602 result = (set1->val.num == set2->val.num);
6603 } else {
6604 assert(set1->type == LYXP_SET_STRING);
Michal Vaskoac6c72f2019-11-14 16:09:34 +01006605 result = !strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006606 }
6607 } else if (op[0] == '!') {
6608 if (set1->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02006609 result = (set1->val.bln != set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006610 } else if (set1->type == LYXP_SET_NUMBER) {
6611 result = (set1->val.num != set2->val.num);
6612 } else {
6613 assert(set1->type == LYXP_SET_STRING);
Michal Vaskoc2058432020-11-06 17:26:21 +01006614 result = strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006615 }
6616 } else {
6617 assert(set1->type == LYXP_SET_NUMBER);
6618 if (op[0] == '<') {
6619 if (op[1] == '=') {
6620 result = (set1->val.num <= set2->val.num);
6621 } else {
6622 result = (set1->val.num < set2->val.num);
6623 }
6624 } else {
6625 if (op[1] == '=') {
6626 result = (set1->val.num >= set2->val.num);
6627 } else {
6628 result = (set1->val.num > set2->val.num);
6629 }
6630 }
6631 }
6632
6633 /* assign result */
6634 if (result) {
6635 set_fill_boolean(set1, 1);
6636 } else {
6637 set_fill_boolean(set1, 0);
6638 }
6639
6640 return LY_SUCCESS;
6641}
6642
6643/**
6644 * @brief Move context @p set to the result of a basic operation. Handles '+', '-', unary '-', '*', 'div',
6645 * or 'mod'. Result is LYXP_SET_NUMBER. Indirectly context position aware.
6646 *
6647 * @param[in,out] set1 Set to use for the result.
6648 * @param[in] set2 Set acting as the second operand for @p op.
6649 * @param[in] op Operator to process.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006650 * @return LY_ERR
6651 */
6652static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006653moveto_op_math(struct lyxp_set *set1, struct lyxp_set *set2, const char *op)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006654{
6655 LY_ERR rc;
6656
6657 /* unary '-' */
6658 if (!set2 && (op[0] == '-')) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006659 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006660 LY_CHECK_RET(rc);
6661 set1->val.num *= -1;
6662 lyxp_set_free(set2);
6663 return LY_SUCCESS;
6664 }
6665
6666 assert(set1 && set2);
6667
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006668 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006669 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006670 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006671 LY_CHECK_RET(rc);
6672
6673 switch (op[0]) {
6674 /* '+' */
6675 case '+':
6676 set1->val.num += set2->val.num;
6677 break;
6678
6679 /* '-' */
6680 case '-':
6681 set1->val.num -= set2->val.num;
6682 break;
6683
6684 /* '*' */
6685 case '*':
6686 set1->val.num *= set2->val.num;
6687 break;
6688
6689 /* 'div' */
6690 case 'd':
6691 set1->val.num /= set2->val.num;
6692 break;
6693
6694 /* 'mod' */
6695 case 'm':
6696 set1->val.num = ((long long)set1->val.num) % ((long long)set2->val.num);
6697 break;
6698
6699 default:
6700 LOGINT_RET(set1->ctx);
6701 }
6702
6703 return LY_SUCCESS;
6704}
6705
6706/*
6707 * eval functions
6708 *
6709 * They execute a parsed XPath expression on some data subtree.
6710 */
6711
6712/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02006713 * @brief Evaluate Predicate. Logs directly on error.
6714 *
Michal Vaskod3678892020-05-21 10:06:58 +02006715 * [9] Predicate ::= '[' Expr ']'
Michal Vasko03ff5a72019-09-11 13:49:33 +02006716 *
6717 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02006718 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006719 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
6720 * @param[in] options XPath options.
6721 * @param[in] parent_pos_pred Whether parent predicate was a positional one.
6722 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6723 */
6724static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02006725eval_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 +02006726{
6727 LY_ERR rc;
Michal Vasko1fdd8fa2021-01-08 09:21:45 +01006728 uint16_t orig_exp;
6729 uint32_t i, orig_pos, orig_size;
Michal Vasko5c4e5892019-11-14 12:31:38 +01006730 int32_t pred_in_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006731 struct lyxp_set set2;
6732 struct lyd_node *orig_parent;
6733
6734 /* '[' */
6735 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02006736 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02006737 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006738
6739 if (!set) {
6740only_parse:
Michal Vasko004d3152020-06-11 19:59:22 +02006741 rc = eval_expr_select(exp, tok_idx, 0, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006742 LY_CHECK_RET(rc);
6743 } else if (set->type == LYXP_SET_NODE_SET) {
6744 /* 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 +01006745 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006746
6747 /* empty set, nothing to evaluate */
6748 if (!set->used) {
6749 goto only_parse;
6750 }
6751
Michal Vasko004d3152020-06-11 19:59:22 +02006752 orig_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006753 orig_pos = 0;
6754 orig_size = set->used;
6755 orig_parent = NULL;
Michal Vasko39dbf352020-05-21 10:08:59 +02006756 for (i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006757 set_init(&set2, set);
6758 set_insert_node(&set2, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, 0);
6759 /* remember the node context position for position() and context size for last(),
6760 * predicates should always be evaluated with respect to the child axis (since we do
6761 * not support explicit axes) so we assign positions based on their parents */
Michal Vasko9e685082021-01-29 14:49:09 +01006762 if (parent_pos_pred && (lyd_parent(set->val.nodes[i].node) != orig_parent)) {
6763 orig_parent = lyd_parent(set->val.nodes[i].node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006764 orig_pos = 1;
6765 } else {
6766 ++orig_pos;
6767 }
6768
6769 set2.ctx_pos = orig_pos;
6770 set2.ctx_size = orig_size;
Michal Vasko004d3152020-06-11 19:59:22 +02006771 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006772
Michal Vasko004d3152020-06-11 19:59:22 +02006773 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006774 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006775 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006776 return rc;
6777 }
6778
6779 /* number is a position */
6780 if (set2.type == LYXP_SET_NUMBER) {
6781 if ((long long)set2.val.num == orig_pos) {
6782 set2.val.num = 1;
6783 } else {
6784 set2.val.num = 0;
6785 }
6786 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006787 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006788
6789 /* predicate satisfied or not? */
Michal Vasko004d3152020-06-11 19:59:22 +02006790 if (!set2.val.bln) {
Michal Vasko2caefc12019-11-14 16:07:56 +01006791 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006792 }
6793 }
Michal Vasko2caefc12019-11-14 16:07:56 +01006794 set_remove_nodes_none(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006795
6796 } else if (set->type == LYXP_SET_SCNODE_SET) {
6797 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006798 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006799 /* there is a currently-valid node */
6800 break;
6801 }
6802 }
6803 /* empty set, nothing to evaluate */
6804 if (i == set->used) {
6805 goto only_parse;
6806 }
6807
Michal Vasko004d3152020-06-11 19:59:22 +02006808 orig_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006809
Michal Vasko03ff5a72019-09-11 13:49:33 +02006810 /* set special in_ctx to all the valid snodes */
6811 pred_in_ctx = set_scnode_new_in_ctx(set);
6812
6813 /* use the valid snodes one-by-one */
6814 for (i = 0; i < set->used; ++i) {
6815 if (set->val.scnodes[i].in_ctx != pred_in_ctx) {
6816 continue;
6817 }
Radek Krejcif13b87b2020-12-01 22:02:17 +01006818 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006819
Michal Vasko004d3152020-06-11 19:59:22 +02006820 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006821
Michal Vasko004d3152020-06-11 19:59:22 +02006822 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006823 LY_CHECK_RET(rc);
6824
6825 set->val.scnodes[i].in_ctx = pred_in_ctx;
6826 }
6827
6828 /* restore the state as it was before the predicate */
6829 for (i = 0; i < set->used; ++i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006830 if (set->val.scnodes[i].in_ctx == LYXP_SET_SCNODE_ATOM_CTX) {
6831 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006832 } else if (set->val.scnodes[i].in_ctx == pred_in_ctx) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01006833 set->val.scnodes[i].in_ctx = LYXP_SET_SCNODE_ATOM_CTX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006834 }
6835 }
6836
6837 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02006838 set2.type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006839 set_fill_set(&set2, set);
6840
Michal Vasko004d3152020-06-11 19:59:22 +02006841 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006842 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006843 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006844 return rc;
6845 }
6846
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006847 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02006848 if (!set2.val.bln) {
Michal Vaskod3678892020-05-21 10:06:58 +02006849 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006850 }
Michal Vaskod3678892020-05-21 10:06:58 +02006851 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006852 }
6853
6854 /* ']' */
Michal Vasko004d3152020-06-11 19:59:22 +02006855 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_BRACK2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006856 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02006857 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02006858 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006859
6860 return LY_SUCCESS;
6861}
6862
6863/**
Michal Vaskod3678892020-05-21 10:06:58 +02006864 * @brief Evaluate Literal. Logs directly on error.
6865 *
6866 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02006867 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02006868 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
6869 */
6870static void
Michal Vasko40308e72020-10-20 16:38:40 +02006871eval_literal(const struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set)
Michal Vaskod3678892020-05-21 10:06:58 +02006872{
6873 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02006874 if (exp->tok_len[*tok_idx] == 2) {
Michal Vaskod3678892020-05-21 10:06:58 +02006875 set_fill_string(set, "", 0);
6876 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02006877 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 +02006878 }
6879 }
6880 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02006881 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02006882 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02006883}
6884
6885/**
Michal Vasko004d3152020-06-11 19:59:22 +02006886 * @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 +02006887 *
Michal Vasko004d3152020-06-11 19:59:22 +02006888 * @param[in] exp Full parsed XPath expression.
6889 * @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 +02006890 * @param[in] ctx_node Found schema node as the context for the predicate.
6891 * @param[in] cur_mod Current module for the expression.
6892 * @param[in] cur_node Current (original context) node.
Michal Vasko004d3152020-06-11 19:59:22 +02006893 * @param[in] format Format of any prefixes in key names/values.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006894 * @param[in] prefix_data Format-specific prefix data (see ::ly_resolve_prefix).
Michal Vasko004d3152020-06-11 19:59:22 +02006895 * @param[out] predicates Parsed predicates.
6896 * @param[out] pred_type Type of @p predicates.
6897 * @return LY_SUCCESS on success,
6898 * @return LY_ERR on any error.
Michal Vaskod3678892020-05-21 10:06:58 +02006899 */
6900static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02006901eval_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 +02006902 const struct lys_module *cur_mod, const struct lysc_node *cur_node, LY_PREFIX_FORMAT format, void *prefix_data,
6903 struct ly_path_predicate **predicates, enum ly_path_pred_type *pred_type)
Michal Vaskod3678892020-05-21 10:06:58 +02006904{
Michal Vasko004d3152020-06-11 19:59:22 +02006905 LY_ERR ret = LY_SUCCESS;
6906 uint16_t key_count, e_idx, pred_idx = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02006907 const struct lysc_node *key;
Michal Vasko004d3152020-06-11 19:59:22 +02006908 size_t pred_len;
Radek Krejci1deb5be2020-08-26 16:43:36 +02006909 uint32_t prev_lo;
Michal Vasko004d3152020-06-11 19:59:22 +02006910 struct lyxp_expr *exp2 = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02006911
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006912 assert(ctx_node->nodetype & (LYS_LIST | LYS_LEAFLIST));
Michal Vaskod3678892020-05-21 10:06:58 +02006913
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006914 if (ctx_node->nodetype == LYS_LIST) {
Michal Vasko004d3152020-06-11 19:59:22 +02006915 /* get key count */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006916 if (ctx_node->flags & LYS_KEYLESS) {
Michal Vasko004d3152020-06-11 19:59:22 +02006917 return LY_EINVAL;
6918 }
Michal Vasko544e58a2021-01-28 14:33:41 +01006919 for (key_count = 0, key = lysc_node_child(ctx_node); key && (key->flags & LYS_KEY); key = key->next, ++key_count) {}
Michal Vasko004d3152020-06-11 19:59:22 +02006920 assert(key_count);
Michal Vaskod3678892020-05-21 10:06:58 +02006921
Michal Vasko004d3152020-06-11 19:59:22 +02006922 /* learn where the predicates end */
6923 e_idx = *tok_idx;
6924 while (key_count) {
6925 /* '[' */
6926 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
6927 return LY_EINVAL;
6928 }
6929 ++e_idx;
6930
6931 /* ']' */
6932 while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
6933 ++e_idx;
6934 }
6935 ++e_idx;
6936
6937 /* another presumably key predicate parsed */
6938 --key_count;
6939 }
Michal Vasko004d3152020-06-11 19:59:22 +02006940 } else {
6941 /* learn just where this single predicate ends */
6942 e_idx = *tok_idx;
6943
Michal Vaskod3678892020-05-21 10:06:58 +02006944 /* '[' */
Michal Vasko004d3152020-06-11 19:59:22 +02006945 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
6946 return LY_EINVAL;
6947 }
6948 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02006949
6950 /* ']' */
Michal Vasko004d3152020-06-11 19:59:22 +02006951 while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
6952 ++e_idx;
6953 }
6954 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02006955 }
6956
Michal Vasko004d3152020-06-11 19:59:22 +02006957 /* get the joined length of all the keys predicates/of the single leaf-list predicate */
6958 pred_len = (exp->tok_pos[e_idx - 1] + exp->tok_len[e_idx - 1]) - exp->tok_pos[*tok_idx];
6959
6960 /* turn logging off */
6961 prev_lo = ly_log_options(0);
6962
6963 /* parse the predicate(s) */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006964 LY_CHECK_GOTO(ret = ly_path_parse_predicate(ctx_node->module->ctx, ctx_node, exp->expr + exp->tok_pos[*tok_idx],
6965 pred_len, LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &exp2), cleanup);
Michal Vasko004d3152020-06-11 19:59:22 +02006966
6967 /* compile */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006968 ret = ly_path_compile_predicate(ctx_node->module->ctx, cur_node, cur_mod, ctx_node, exp2, &pred_idx, format,
6969 prefix_data, predicates, pred_type);
Michal Vasko004d3152020-06-11 19:59:22 +02006970 LY_CHECK_GOTO(ret, cleanup);
6971
6972 /* success, the predicate must include all the needed information for hash-based search */
6973 *tok_idx = e_idx;
6974
6975cleanup:
6976 ly_log_options(prev_lo);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006977 lyxp_expr_free(ctx_node->module->ctx, exp2);
Michal Vasko004d3152020-06-11 19:59:22 +02006978 return ret;
Michal Vaskod3678892020-05-21 10:06:58 +02006979}
6980
6981/**
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006982 * @brief Search for/check the next schema node that could be the only matching schema node meaning the
6983 * data node(s) could be found using a single hash-based search.
6984 *
Michal Vaskoc71c37b2021-01-11 13:40:02 +01006985 * @param[in] ctx libyang context.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006986 * @param[in] node Next context node to check.
6987 * @param[in] name Expected node name.
6988 * @param[in] name_len Length of @p name.
Michal Vaskoc71c37b2021-01-11 13:40:02 +01006989 * @param[in] moveto_mod Expected node module, can be NULL for JSON format with no prefix.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006990 * @param[in] root_type XPath root type.
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006991 * @param[in] format Prefix format.
6992 * @param[in,out] found Previously found node, is updated.
6993 * @return LY_SUCCESS on success,
6994 * @return LY_ENOT if the whole check failed and hashes cannot be used.
6995 */
6996static LY_ERR
Michal Vaskoc71c37b2021-01-11 13:40:02 +01006997eval_name_test_with_predicate_get_scnode(const struct ly_ctx *ctx, const struct lyd_node *node, const char *name,
6998 uint16_t name_len, const struct lys_module *moveto_mod, enum lyxp_node_type root_type, LY_PREFIX_FORMAT format,
Michal Vasko5d24f6c2020-10-13 13:49:06 +02006999 const struct lysc_node **found)
7000{
7001 const struct lysc_node *scnode;
7002 const struct lys_module *mod;
7003 uint32_t idx = 0;
7004
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007005 assert((format == LY_PREF_JSON) || moveto_mod);
7006
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007007continue_search:
Michal Vasko7d1d0912020-10-16 08:38:30 +02007008 scnode = NULL;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007009 if (!node) {
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007010 if ((format == LY_PREF_JSON) && !moveto_mod) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007011 /* search all modules for a single match */
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007012 while ((mod = ly_ctx_get_module_iter(ctx, &idx))) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007013 scnode = lys_find_child(NULL, mod, name, name_len, 0, 0);
7014 if (scnode) {
7015 /* we have found a match */
7016 break;
7017 }
7018 }
7019
Michal Vasko7d1d0912020-10-16 08:38:30 +02007020 if (!scnode) {
7021 /* all modules searched */
7022 idx = 0;
7023 }
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007024 } else {
7025 /* search in top-level */
7026 scnode = lys_find_child(NULL, moveto_mod, name, name_len, 0, 0);
7027 }
7028 } else if (!*found || (lysc_data_parent(*found) != node->schema)) {
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007029 if ((format == LY_PREF_JSON) && !moveto_mod) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007030 /* we must adjust the module to inherit the one from the context node */
7031 moveto_mod = node->schema->module;
7032 }
7033
7034 /* search in children, do not repeat the same search */
7035 scnode = lys_find_child(node->schema, moveto_mod, name, name_len, 0, 0);
Michal Vasko7d1d0912020-10-16 08:38:30 +02007036 } /* else skip redundant search */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007037
7038 /* additional context check */
7039 if (scnode && (root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) {
7040 scnode = NULL;
7041 }
7042
7043 if (scnode) {
7044 if (*found) {
7045 /* we found a schema node with the same name but at different level, give up, too complicated
7046 * (more hash-based searches would be required, not supported) */
7047 return LY_ENOT;
7048 } else {
7049 /* remember the found schema node and continue to make sure it can be used */
7050 *found = scnode;
7051 }
7052 }
7053
7054 if (idx) {
7055 /* continue searching all the following models */
7056 goto continue_search;
7057 }
7058
7059 return LY_SUCCESS;
7060}
7061
7062/**
Michal Vaskod3678892020-05-21 10:06:58 +02007063 * @brief Evaluate NameTest and any following Predicates. Logs directly on error.
7064 *
7065 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
7066 * [6] NodeTest ::= NameTest | NodeType '(' ')'
7067 * [7] NameTest ::= '*' | NCName ':' '*' | QName
7068 *
7069 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007070 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02007071 * @param[in] attr_axis Whether to search attributes or standard nodes.
7072 * @param[in] all_desc Whether to search all the descendants or children only.
7073 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7074 * @param[in] options XPath options.
7075 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7076 */
7077static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007078eval_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 +02007079 struct lyxp_set *set, uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02007080{
Michal Vaskod3678892020-05-21 10:06:58 +02007081 char *path;
Michal Vasko004d3152020-06-11 19:59:22 +02007082 const char *ncname, *ncname_dict = NULL;
7083 uint16_t ncname_len;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007084 const struct lys_module *moveto_mod = NULL;
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007085 const struct lysc_node *scnode = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02007086 struct ly_path_predicate *predicates = NULL;
7087 enum ly_path_pred_type pred_type = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02007088 LY_ERR rc = LY_SUCCESS;
7089
7090 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007091 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007092 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007093
7094 if (!set) {
7095 goto moveto;
7096 }
7097
Michal Vasko004d3152020-06-11 19:59:22 +02007098 ncname = &exp->expr[exp->tok_pos[*tok_idx - 1]];
7099 ncname_len = exp->tok_len[*tok_idx - 1];
Michal Vaskod3678892020-05-21 10:06:58 +02007100
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007101 if ((ncname[0] == '*') && (ncname_len == 1)) {
7102 /* all nodes will match */
7103 goto moveto;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007104 }
7105
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007106 /* parse (and skip) module name */
7107 rc = moveto_resolve_model(&ncname, &ncname_len, set, NULL, &moveto_mod);
Michal Vaskod3678892020-05-21 10:06:58 +02007108 LY_CHECK_GOTO(rc, cleanup);
7109
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007110 if (((set->format == LY_PREF_JSON) || moveto_mod) && !attr_axis && !all_desc && (set->type == LYXP_SET_NODE_SET)) {
Michal Vaskod3678892020-05-21 10:06:58 +02007111 /* find the matching schema node in some parent in the context */
Radek Krejci1deb5be2020-08-26 16:43:36 +02007112 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007113 if (eval_name_test_with_predicate_get_scnode(set->ctx, set->val.nodes[i].node, ncname, ncname_len,
7114 moveto_mod, set->root_type, set->format, &scnode)) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007115 /* check failed */
7116 scnode = NULL;
7117 break;
Michal Vaskod3678892020-05-21 10:06:58 +02007118 }
7119 }
7120
Michal Vasko004d3152020-06-11 19:59:22 +02007121 if (scnode && (scnode->nodetype & (LYS_LIST | LYS_LEAFLIST))) {
7122 /* try to create the predicates */
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007123 if (eval_name_test_try_compile_predicates(exp, tok_idx, scnode, set->cur_mod, set->cur_node ?
7124 set->cur_node->schema : NULL, set->format, set->prefix_data, &predicates, &pred_type)) {
Michal Vasko004d3152020-06-11 19:59:22 +02007125 /* hashes cannot be used */
Michal Vaskod3678892020-05-21 10:06:58 +02007126 scnode = NULL;
7127 }
7128 }
7129 }
7130
Michal Vaskoc71c37b2021-01-11 13:40:02 +01007131 if (!scnode) {
7132 /* we are not able to match based on a schema node and not all the modules match ("*"),
Michal Vasko004d3152020-06-11 19:59:22 +02007133 * use dictionary for efficient comparison */
Radek Krejci011e4aa2020-09-04 15:22:31 +02007134 LY_CHECK_GOTO(rc = lydict_insert(set->ctx, ncname, ncname_len, &ncname_dict), cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02007135 }
7136
7137moveto:
7138 /* move to the attribute(s), data node(s), or schema node(s) */
7139 if (attr_axis) {
7140 if (set && (options & LYXP_SCNODE_ALL)) {
7141 set_scnode_clear_ctx(set);
7142 } else {
7143 if (all_desc) {
Michal Vaskocdad7122020-11-09 21:04:44 +01007144 rc = moveto_attr_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007145 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007146 rc = moveto_attr(set, moveto_mod, ncname_dict);
Michal Vaskod3678892020-05-21 10:06:58 +02007147 }
7148 LY_CHECK_GOTO(rc, cleanup);
7149 }
7150 } else {
7151 if (set && (options & LYXP_SCNODE_ALL)) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02007152 int64_t i;
7153
Michal Vaskod3678892020-05-21 10:06:58 +02007154 if (all_desc) {
Michal Vasko004d3152020-06-11 19:59:22 +02007155 rc = moveto_scnode_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007156 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007157 rc = moveto_scnode(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007158 }
7159 LY_CHECK_GOTO(rc, cleanup);
7160
7161 for (i = set->used - 1; i > -1; --i) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01007162 if (set->val.scnodes[i].in_ctx > LYXP_SET_SCNODE_ATOM) {
Michal Vaskod3678892020-05-21 10:06:58 +02007163 break;
7164 }
7165 }
7166 if (i == -1) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02007167 path = lysc_path(set->cur_scnode, LYSC_PATH_LOG, NULL, 0);
Michal Vasko90f12dc2020-12-03 14:20:42 +01007168 LOGWRN(set->ctx, "Schema node \"%.*s\" not found (\"%.*s\") with context node \"%s\".",
7169 ncname_len, ncname, (ncname - exp->expr) + ncname_len, exp->expr, path);
Michal Vaskod3678892020-05-21 10:06:58 +02007170 free(path);
7171 }
7172 } else {
7173 if (all_desc) {
Michal Vaskocdad7122020-11-09 21:04:44 +01007174 rc = moveto_node_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007175 } else {
7176 if (scnode) {
7177 /* we can find the nodes using hashes */
Michal Vaskocdad7122020-11-09 21:04:44 +01007178 rc = moveto_node_hash(set, scnode, predicates, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007179 } else {
Michal Vaskocdad7122020-11-09 21:04:44 +01007180 rc = moveto_node(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007181 }
7182 }
7183 LY_CHECK_GOTO(rc, cleanup);
7184 }
7185 }
7186
7187 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007188 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7189 rc = eval_predicate(exp, tok_idx, set, options, 1);
Michal Vaskod3678892020-05-21 10:06:58 +02007190 LY_CHECK_RET(rc);
7191 }
7192
7193cleanup:
Michal Vaskodb51a8d2020-05-27 15:22:29 +02007194 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007195 lydict_remove(set->ctx, ncname_dict);
Michal Vaskof7e16e22020-10-21 09:24:39 +02007196 ly_path_predicates_free(set->ctx, pred_type, predicates);
Michal Vaskodb51a8d2020-05-27 15:22:29 +02007197 }
Michal Vaskod3678892020-05-21 10:06:58 +02007198 return rc;
7199}
7200
7201/**
7202 * @brief Evaluate NodeType and any following Predicates. Logs directly on error.
7203 *
7204 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
7205 * [6] NodeTest ::= NameTest | NodeType '(' ')'
7206 * [8] NodeType ::= 'text' | 'node'
7207 *
7208 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007209 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02007210 * @param[in] attr_axis Whether to search attributes or standard nodes.
7211 * @param[in] all_desc Whether to search all the descendants or children only.
7212 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7213 * @param[in] options XPath options.
7214 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7215 */
7216static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007217eval_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 +02007218 struct lyxp_set *set, uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02007219{
7220 LY_ERR rc;
7221
7222 /* TODO */
7223 (void)attr_axis;
7224 (void)all_desc;
7225
7226 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007227 assert(exp->tok_len[*tok_idx] == 4);
Michal Vaskod3678892020-05-21 10:06:58 +02007228 if (set->type == LYXP_SET_SCNODE_SET) {
7229 set_scnode_clear_ctx(set);
7230 /* just for the debug message below */
7231 set = NULL;
7232 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007233 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "node", 4)) {
Michal Vaskod3678892020-05-21 10:06:58 +02007234 rc = xpath_node(NULL, 0, set, options);
7235 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007236 assert(!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "text", 4));
Michal Vaskod3678892020-05-21 10:06:58 +02007237 rc = xpath_text(NULL, 0, set, options);
7238 }
7239 LY_CHECK_RET(rc);
7240 }
7241 }
7242 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007243 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007244 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007245
7246 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02007247 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
Michal Vaskod3678892020-05-21 10:06:58 +02007248 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007249 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007250 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007251
7252 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007253 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
Michal Vaskod3678892020-05-21 10:06:58 +02007254 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007255 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007256 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007257
7258 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007259 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7260 rc = eval_predicate(exp, tok_idx, set, options, 1);
Michal Vaskod3678892020-05-21 10:06:58 +02007261 LY_CHECK_RET(rc);
7262 }
7263
7264 return LY_SUCCESS;
7265}
7266
7267/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02007268 * @brief Evaluate RelativeLocationPath. Logs directly on error.
7269 *
7270 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
7271 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
Michal Vaskod3678892020-05-21 10:06:58 +02007272 * [6] NodeTest ::= NameTest | NodeType '(' ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007273 *
7274 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007275 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007276 * @param[in] all_desc Whether to search all the descendants or children only.
7277 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7278 * @param[in] options XPath options.
7279 * @return LY_ERR (YL_EINCOMPLETE on unresolved when)
7280 */
7281static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007282eval_relative_location_path(const struct lyxp_expr *exp, uint16_t *tok_idx, ly_bool all_desc, struct lyxp_set *set,
7283 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007284{
Radek Krejci857189e2020-09-01 13:26:36 +02007285 ly_bool attr_axis;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007286 LY_ERR rc;
7287
7288 goto step;
7289 do {
7290 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02007291 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007292 all_desc = 0;
7293 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007294 assert(exp->tok_len[*tok_idx] == 2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007295 all_desc = 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 Vasko03ff5a72019-09-11 13:49:33 +02007300
7301step:
Michal Vaskod3678892020-05-21 10:06:58 +02007302 /* evaluate abbreviated axis '@'? if any */
Michal Vasko004d3152020-06-11 19:59:22 +02007303 if (exp->tokens[*tok_idx] == LYXP_TOKEN_AT) {
Michal Vaskod3678892020-05-21 10:06:58 +02007304 attr_axis = 1;
7305
7306 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007307 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007308 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007309 } else {
7310 attr_axis = 0;
7311 }
7312
Michal Vasko03ff5a72019-09-11 13:49:33 +02007313 /* Step */
Michal Vasko004d3152020-06-11 19:59:22 +02007314 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007315 case LYXP_TOKEN_DOT:
7316 /* evaluate '.' */
7317 if (set && (options & LYXP_SCNODE_ALL)) {
7318 rc = moveto_scnode_self(set, all_desc, options);
7319 } else {
7320 rc = moveto_self(set, all_desc, options);
7321 }
7322 LY_CHECK_RET(rc);
7323 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007324 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007325 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007326 break;
7327
7328 case LYXP_TOKEN_DDOT:
7329 /* evaluate '..' */
7330 if (set && (options & LYXP_SCNODE_ALL)) {
7331 rc = moveto_scnode_parent(set, all_desc, options);
7332 } else {
7333 rc = moveto_parent(set, all_desc, options);
7334 }
7335 LY_CHECK_RET(rc);
7336 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007337 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007338 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007339 break;
7340
Michal Vasko03ff5a72019-09-11 13:49:33 +02007341 case LYXP_TOKEN_NAMETEST:
Michal Vaskod3678892020-05-21 10:06:58 +02007342 /* evaluate NameTest Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007343 rc = eval_name_test_with_predicate(exp, tok_idx, attr_axis, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007344 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02007345 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007346
Michal Vaskod3678892020-05-21 10:06:58 +02007347 case LYXP_TOKEN_NODETYPE:
7348 /* evaluate NodeType Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007349 rc = eval_node_type_with_predicate(exp, tok_idx, attr_axis, all_desc, set, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007350 LY_CHECK_RET(rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007351 break;
7352
7353 default:
Michal Vasko02a77382019-09-12 11:47:35 +02007354 LOGINT_RET(set ? set->ctx : NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007355 }
Michal Vasko004d3152020-06-11 19:59:22 +02007356 } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007357
7358 return LY_SUCCESS;
7359}
7360
7361/**
7362 * @brief Evaluate AbsoluteLocationPath. Logs directly on error.
7363 *
7364 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
7365 *
7366 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007367 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007368 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7369 * @param[in] options XPath options.
7370 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7371 */
7372static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007373eval_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 +02007374{
Radek Krejci857189e2020-09-01 13:26:36 +02007375 ly_bool all_desc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007376
7377 if (set) {
7378 /* no matter what tokens follow, we need to be at the root */
Michal Vaskob0099a92020-08-31 14:55:23 +02007379 LY_CHECK_RET(moveto_root(set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007380 }
7381
7382 /* '/' RelativeLocationPath? */
Michal Vasko004d3152020-06-11 19:59:22 +02007383 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007384 /* evaluate '/' - deferred */
7385 all_desc = 0;
7386 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007387 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007388 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007389
Michal Vasko004d3152020-06-11 19:59:22 +02007390 if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007391 return LY_SUCCESS;
7392 }
Michal Vasko004d3152020-06-11 19:59:22 +02007393 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007394 case LYXP_TOKEN_DOT:
7395 case LYXP_TOKEN_DDOT:
7396 case LYXP_TOKEN_AT:
7397 case LYXP_TOKEN_NAMETEST:
7398 case LYXP_TOKEN_NODETYPE:
Michal Vaskob0099a92020-08-31 14:55:23 +02007399 LY_CHECK_RET(eval_relative_location_path(exp, tok_idx, all_desc, set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007400 break;
7401 default:
7402 break;
7403 }
7404
Michal Vasko03ff5a72019-09-11 13:49:33 +02007405 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02007406 /* '//' RelativeLocationPath */
Michal Vasko03ff5a72019-09-11 13:49:33 +02007407 /* evaluate '//' - deferred so as not to waste memory by remembering all the nodes */
7408 all_desc = 1;
7409 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007410 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007411 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007412
Michal Vaskob0099a92020-08-31 14:55:23 +02007413 LY_CHECK_RET(eval_relative_location_path(exp, tok_idx, all_desc, set, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007414 }
7415
7416 return LY_SUCCESS;
7417}
7418
7419/**
7420 * @brief Evaluate FunctionCall. Logs directly on error.
7421 *
Michal Vaskod3678892020-05-21 10:06:58 +02007422 * [11] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007423 *
7424 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007425 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007426 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7427 * @param[in] options XPath options.
7428 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7429 */
7430static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007431eval_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 +02007432{
7433 LY_ERR rc;
Michal Vasko69730152020-10-09 16:30:07 +02007434
Radek Krejci1deb5be2020-08-26 16:43:36 +02007435 LY_ERR (*xpath_func)(struct lyxp_set **, uint16_t, struct lyxp_set *, uint32_t) = NULL;
Michal Vasko0cbf54f2019-12-16 10:01:06 +01007436 uint16_t arg_count = 0, i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007437 struct lyxp_set **args = NULL, **args_aux;
7438
7439 if (set) {
7440 /* FunctionName */
Michal Vasko004d3152020-06-11 19:59:22 +02007441 switch (exp->tok_len[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007442 case 3:
Michal Vasko004d3152020-06-11 19:59:22 +02007443 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007444 xpath_func = &xpath_not;
Michal Vasko004d3152020-06-11 19:59:22 +02007445 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007446 xpath_func = &xpath_sum;
7447 }
7448 break;
7449 case 4:
Michal Vasko004d3152020-06-11 19:59:22 +02007450 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007451 xpath_func = &xpath_lang;
Michal Vasko004d3152020-06-11 19:59:22 +02007452 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007453 xpath_func = &xpath_last;
Michal Vasko004d3152020-06-11 19:59:22 +02007454 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007455 xpath_func = &xpath_name;
Michal Vasko004d3152020-06-11 19:59:22 +02007456 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007457 xpath_func = &xpath_true;
7458 }
7459 break;
7460 case 5:
Michal Vasko004d3152020-06-11 19:59:22 +02007461 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007462 xpath_func = &xpath_count;
Michal Vasko004d3152020-06-11 19:59:22 +02007463 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007464 xpath_func = &xpath_false;
Michal Vasko004d3152020-06-11 19:59:22 +02007465 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007466 xpath_func = &xpath_floor;
Michal Vasko004d3152020-06-11 19:59:22 +02007467 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007468 xpath_func = &xpath_round;
Michal Vasko004d3152020-06-11 19:59:22 +02007469 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007470 xpath_func = &xpath_deref;
7471 }
7472 break;
7473 case 6:
Michal Vasko004d3152020-06-11 19:59:22 +02007474 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007475 xpath_func = &xpath_concat;
Michal Vasko004d3152020-06-11 19:59:22 +02007476 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007477 xpath_func = &xpath_number;
Michal Vasko004d3152020-06-11 19:59:22 +02007478 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007479 xpath_func = &xpath_string;
7480 }
7481 break;
7482 case 7:
Michal Vasko004d3152020-06-11 19:59:22 +02007483 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007484 xpath_func = &xpath_boolean;
Michal Vasko004d3152020-06-11 19:59:22 +02007485 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007486 xpath_func = &xpath_ceiling;
Michal Vasko004d3152020-06-11 19:59:22 +02007487 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007488 xpath_func = &xpath_current;
7489 }
7490 break;
7491 case 8:
Michal Vasko004d3152020-06-11 19:59:22 +02007492 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007493 xpath_func = &xpath_contains;
Michal Vasko004d3152020-06-11 19:59:22 +02007494 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007495 xpath_func = &xpath_position;
Michal Vasko004d3152020-06-11 19:59:22 +02007496 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007497 xpath_func = &xpath_re_match;
7498 }
7499 break;
7500 case 9:
Michal Vasko004d3152020-06-11 19:59:22 +02007501 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007502 xpath_func = &xpath_substring;
Michal Vasko004d3152020-06-11 19:59:22 +02007503 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007504 xpath_func = &xpath_translate;
7505 }
7506 break;
7507 case 10:
Michal Vasko004d3152020-06-11 19:59:22 +02007508 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007509 xpath_func = &xpath_local_name;
Michal Vasko004d3152020-06-11 19:59:22 +02007510 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007511 xpath_func = &xpath_enum_value;
Michal Vasko004d3152020-06-11 19:59:22 +02007512 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007513 xpath_func = &xpath_bit_is_set;
7514 }
7515 break;
7516 case 11:
Michal Vasko004d3152020-06-11 19:59:22 +02007517 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007518 xpath_func = &xpath_starts_with;
7519 }
7520 break;
7521 case 12:
Michal Vasko004d3152020-06-11 19:59:22 +02007522 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007523 xpath_func = &xpath_derived_from;
7524 }
7525 break;
7526 case 13:
Michal Vasko004d3152020-06-11 19:59:22 +02007527 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007528 xpath_func = &xpath_namespace_uri;
Michal Vasko004d3152020-06-11 19:59:22 +02007529 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007530 xpath_func = &xpath_string_length;
7531 }
7532 break;
7533 case 15:
Michal Vasko004d3152020-06-11 19:59:22 +02007534 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007535 xpath_func = &xpath_normalize_space;
Michal Vasko004d3152020-06-11 19:59:22 +02007536 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007537 xpath_func = &xpath_substring_after;
7538 }
7539 break;
7540 case 16:
Michal Vasko004d3152020-06-11 19:59:22 +02007541 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007542 xpath_func = &xpath_substring_before;
7543 }
7544 break;
7545 case 20:
Michal Vasko004d3152020-06-11 19:59:22 +02007546 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007547 xpath_func = &xpath_derived_from_or_self;
7548 }
7549 break;
7550 }
7551
7552 if (!xpath_func) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01007553 LOGVAL(set->ctx, LY_VCODE_XP_INFUNC, exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007554 return LY_EVALID;
7555 }
7556 }
7557
7558 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007559 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007560 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007561
7562 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02007563 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007564 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007565 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007566 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007567
7568 /* ( Expr ( ',' Expr )* )? */
Michal Vasko004d3152020-06-11 19:59:22 +02007569 if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007570 if (set) {
7571 args = malloc(sizeof *args);
7572 LY_CHECK_ERR_GOTO(!args, LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7573 arg_count = 1;
7574 args[0] = set_copy(set);
7575 if (!args[0]) {
7576 rc = LY_EMEM;
7577 goto cleanup;
7578 }
7579
Michal Vasko004d3152020-06-11 19:59:22 +02007580 rc = eval_expr_select(exp, tok_idx, 0, args[0], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007581 LY_CHECK_GOTO(rc, cleanup);
7582 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007583 rc = eval_expr_select(exp, tok_idx, 0, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007584 LY_CHECK_GOTO(rc, cleanup);
7585 }
7586 }
Michal Vasko004d3152020-06-11 19:59:22 +02007587 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007588 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007589 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007590 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007591
7592 if (set) {
7593 ++arg_count;
7594 args_aux = realloc(args, arg_count * sizeof *args);
7595 LY_CHECK_ERR_GOTO(!args_aux, arg_count--; LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7596 args = args_aux;
7597 args[arg_count - 1] = set_copy(set);
7598 if (!args[arg_count - 1]) {
7599 rc = LY_EMEM;
7600 goto cleanup;
7601 }
7602
Michal Vasko004d3152020-06-11 19:59:22 +02007603 rc = eval_expr_select(exp, tok_idx, 0, args[arg_count - 1], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007604 LY_CHECK_GOTO(rc, cleanup);
7605 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007606 rc = eval_expr_select(exp, tok_idx, 0, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007607 LY_CHECK_GOTO(rc, cleanup);
7608 }
7609 }
7610
7611 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007612 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007613 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007614 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007615 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007616
7617 if (set) {
7618 /* evaluate function */
7619 rc = xpath_func(args, arg_count, set, options);
7620
7621 if (options & LYXP_SCNODE_ALL) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007622 /* merge all nodes from arg evaluations */
7623 for (i = 0; i < arg_count; ++i) {
7624 set_scnode_clear_ctx(args[i]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007625 lyxp_set_scnode_merge(set, args[i]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007626 }
7627 }
7628 } else {
7629 rc = LY_SUCCESS;
7630 }
7631
7632cleanup:
7633 for (i = 0; i < arg_count; ++i) {
7634 lyxp_set_free(args[i]);
7635 }
7636 free(args);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007637 return rc;
7638}
7639
7640/**
7641 * @brief Evaluate Number. Logs directly on error.
7642 *
7643 * @param[in] ctx Context for errors.
7644 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007645 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007646 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7647 * @return LY_ERR
7648 */
7649static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007650eval_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 +02007651{
7652 long double num;
7653 char *endptr;
7654
7655 if (set) {
7656 errno = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02007657 num = strtold(&exp->expr[exp->tok_pos[*tok_idx]], &endptr);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007658 if (errno) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01007659 LOGVAL(ctx, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]);
7660 LOGVAL(ctx, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double (%s).",
Michal Vasko69730152020-10-09 16:30:07 +02007661 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]], strerror(errno));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007662 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02007663 } else if (endptr - &exp->expr[exp->tok_pos[*tok_idx]] != exp->tok_len[*tok_idx]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01007664 LOGVAL(ctx, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]);
7665 LOGVAL(ctx, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double.",
Michal Vasko69730152020-10-09 16:30:07 +02007666 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007667 return LY_EVALID;
7668 }
7669
7670 set_fill_number(set, num);
7671 }
7672
7673 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007674 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007675 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007676 return LY_SUCCESS;
7677}
7678
7679/**
7680 * @brief Evaluate PathExpr. Logs directly on error.
7681 *
Michal Vaskod3678892020-05-21 10:06:58 +02007682 * [12] PathExpr ::= LocationPath | PrimaryExpr Predicate*
Michal Vasko03ff5a72019-09-11 13:49:33 +02007683 * | PrimaryExpr Predicate* '/' RelativeLocationPath
7684 * | PrimaryExpr Predicate* '//' RelativeLocationPath
7685 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
Michal Vaskod3678892020-05-21 10:06:58 +02007686 * [10] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall
Michal Vasko03ff5a72019-09-11 13:49:33 +02007687 *
7688 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007689 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007690 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7691 * @param[in] options XPath options.
7692 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7693 */
7694static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007695eval_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 +02007696{
Radek Krejci857189e2020-09-01 13:26:36 +02007697 ly_bool all_desc, parent_pos_pred;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007698 LY_ERR rc;
7699
Michal Vasko004d3152020-06-11 19:59:22 +02007700 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007701 case LYXP_TOKEN_PAR1:
7702 /* '(' Expr ')' */
7703
7704 /* '(' */
7705 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007706 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007707 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007708
7709 /* Expr */
Michal Vasko004d3152020-06-11 19:59:22 +02007710 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007711 LY_CHECK_RET(rc);
7712
7713 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007714 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007715 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007716 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007717 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007718
7719 parent_pos_pred = 0;
7720 goto predicate;
7721
7722 case LYXP_TOKEN_DOT:
7723 case LYXP_TOKEN_DDOT:
7724 case LYXP_TOKEN_AT:
7725 case LYXP_TOKEN_NAMETEST:
7726 case LYXP_TOKEN_NODETYPE:
7727 /* RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007728 rc = eval_relative_location_path(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007729 LY_CHECK_RET(rc);
7730 break;
7731
7732 case LYXP_TOKEN_FUNCNAME:
7733 /* FunctionCall */
7734 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007735 rc = eval_function_call(exp, tok_idx, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007736 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007737 rc = eval_function_call(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007738 }
7739 LY_CHECK_RET(rc);
7740
7741 parent_pos_pred = 1;
7742 goto predicate;
7743
Michal Vasko3e48bf32020-06-01 08:39:07 +02007744 case LYXP_TOKEN_OPER_PATH:
7745 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02007746 /* AbsoluteLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007747 rc = eval_absolute_location_path(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007748 LY_CHECK_RET(rc);
7749 break;
7750
7751 case LYXP_TOKEN_LITERAL:
7752 /* Literal */
7753 if (!set || (options & LYXP_SCNODE_ALL)) {
7754 if (set) {
7755 set_scnode_clear_ctx(set);
7756 }
Michal Vasko004d3152020-06-11 19:59:22 +02007757 eval_literal(exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007758 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007759 eval_literal(exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007760 }
7761
7762 parent_pos_pred = 1;
7763 goto predicate;
7764
7765 case LYXP_TOKEN_NUMBER:
7766 /* Number */
7767 if (!set || (options & LYXP_SCNODE_ALL)) {
7768 if (set) {
7769 set_scnode_clear_ctx(set);
7770 }
Michal Vasko004d3152020-06-11 19:59:22 +02007771 rc = eval_number(NULL, exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007772 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007773 rc = eval_number(set->ctx, exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007774 }
7775 LY_CHECK_RET(rc);
7776
7777 parent_pos_pred = 1;
7778 goto predicate;
7779
7780 default:
Radek Krejci2efc45b2020-12-22 16:25:44 +01007781 LOGVAL(set->ctx, LY_VCODE_XP_INTOK, lyxp_print_token(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007782 return LY_EVALID;
7783 }
7784
7785 return LY_SUCCESS;
7786
7787predicate:
7788 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007789 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7790 rc = eval_predicate(exp, tok_idx, set, options, parent_pos_pred);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007791 LY_CHECK_RET(rc);
7792 }
7793
7794 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007795 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007796
7797 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02007798 if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007799 all_desc = 0;
7800 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007801 all_desc = 1;
7802 }
7803
7804 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007805 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007806 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007807
Michal Vasko004d3152020-06-11 19:59:22 +02007808 rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007809 LY_CHECK_RET(rc);
7810 }
7811
7812 return LY_SUCCESS;
7813}
7814
7815/**
7816 * @brief Evaluate UnionExpr. Logs directly on error.
7817 *
Michal Vaskod3678892020-05-21 10:06:58 +02007818 * [20] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007819 *
7820 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007821 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007822 * @param[in] repeat How many times this expression is repeated.
7823 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7824 * @param[in] options XPath options.
7825 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7826 */
7827static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007828eval_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 +02007829{
7830 LY_ERR rc = LY_SUCCESS;
7831 struct lyxp_set orig_set, set2;
7832 uint16_t i;
7833
7834 assert(repeat);
7835
7836 set_init(&orig_set, set);
7837 set_init(&set2, set);
7838
7839 set_fill_set(&orig_set, set);
7840
Michal Vasko004d3152020-06-11 19:59:22 +02007841 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007842 LY_CHECK_GOTO(rc, cleanup);
7843
7844 /* ('|' PathExpr)* */
7845 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007846 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_UNI);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007847 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007848 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007849 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007850
7851 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007852 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007853 LY_CHECK_GOTO(rc, cleanup);
7854 continue;
7855 }
7856
7857 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02007858 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007859 LY_CHECK_GOTO(rc, cleanup);
7860
7861 /* eval */
7862 if (options & LYXP_SCNODE_ALL) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01007863 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007864 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007865 rc = moveto_union(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007866 LY_CHECK_GOTO(rc, cleanup);
7867 }
7868 }
7869
7870cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007871 lyxp_set_free_content(&orig_set);
7872 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007873 return rc;
7874}
7875
7876/**
7877 * @brief Evaluate UnaryExpr. Logs directly on error.
7878 *
Michal Vaskod3678892020-05-21 10:06:58 +02007879 * [19] UnaryExpr ::= UnionExpr | '-' UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007880 *
7881 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007882 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007883 * @param[in] repeat How many times this expression is repeated.
7884 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7885 * @param[in] options XPath options.
7886 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7887 */
7888static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007889eval_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 +02007890{
7891 LY_ERR rc;
7892 uint16_t this_op, i;
7893
7894 assert(repeat);
7895
7896 /* ('-')+ */
Michal Vasko004d3152020-06-11 19:59:22 +02007897 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007898 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007899 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 +02007900
7901 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007902 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007903 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007904 }
7905
Michal Vasko004d3152020-06-11 19:59:22 +02007906 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNARY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007907 LY_CHECK_RET(rc);
7908
7909 if (set && (repeat % 2)) {
7910 if (options & LYXP_SCNODE_ALL) {
7911 warn_operands(set->ctx, set, NULL, 1, exp->expr, exp->tok_pos[this_op]);
7912 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007913 rc = moveto_op_math(set, NULL, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007914 LY_CHECK_RET(rc);
7915 }
7916 }
7917
7918 return LY_SUCCESS;
7919}
7920
7921/**
7922 * @brief Evaluate MultiplicativeExpr. Logs directly on error.
7923 *
Michal Vaskod3678892020-05-21 10:06:58 +02007924 * [18] MultiplicativeExpr ::= UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007925 * | MultiplicativeExpr '*' UnaryExpr
7926 * | MultiplicativeExpr 'div' UnaryExpr
7927 * | MultiplicativeExpr 'mod' UnaryExpr
7928 *
7929 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007930 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007931 * @param[in] repeat How many times this expression is repeated.
7932 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7933 * @param[in] options XPath options.
7934 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7935 */
7936static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02007937eval_multiplicative_expr(const struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set,
7938 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007939{
7940 LY_ERR rc;
7941 uint16_t this_op;
7942 struct lyxp_set orig_set, set2;
7943 uint16_t i;
7944
7945 assert(repeat);
7946
7947 set_init(&orig_set, set);
7948 set_init(&set2, set);
7949
7950 set_fill_set(&orig_set, set);
7951
Michal Vasko004d3152020-06-11 19:59:22 +02007952 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007953 LY_CHECK_GOTO(rc, cleanup);
7954
7955 /* ('*' / 'div' / 'mod' UnaryExpr)* */
7956 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007957 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007958
Michal Vasko004d3152020-06-11 19:59:22 +02007959 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007960 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02007961 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02007962 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007963
7964 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007965 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007966 LY_CHECK_GOTO(rc, cleanup);
7967 continue;
7968 }
7969
7970 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02007971 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007972 LY_CHECK_GOTO(rc, cleanup);
7973
7974 /* eval */
7975 if (options & LYXP_SCNODE_ALL) {
7976 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007977 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007978 set_scnode_clear_ctx(set);
7979 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007980 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007981 LY_CHECK_GOTO(rc, cleanup);
7982 }
7983 }
7984
7985cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007986 lyxp_set_free_content(&orig_set);
7987 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007988 return rc;
7989}
7990
7991/**
7992 * @brief Evaluate AdditiveExpr. Logs directly on error.
7993 *
Michal Vaskod3678892020-05-21 10:06:58 +02007994 * [17] AdditiveExpr ::= MultiplicativeExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007995 * | AdditiveExpr '+' MultiplicativeExpr
7996 * | AdditiveExpr '-' MultiplicativeExpr
7997 *
7998 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007999 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008000 * @param[in] repeat How many times this expression is repeated.
8001 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8002 * @param[in] options XPath options.
8003 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8004 */
8005static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008006eval_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 +02008007{
8008 LY_ERR rc;
8009 uint16_t this_op;
8010 struct lyxp_set orig_set, set2;
8011 uint16_t i;
8012
8013 assert(repeat);
8014
8015 set_init(&orig_set, set);
8016 set_init(&set2, set);
8017
8018 set_fill_set(&orig_set, set);
8019
Michal Vasko004d3152020-06-11 19:59:22 +02008020 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008021 LY_CHECK_GOTO(rc, cleanup);
8022
8023 /* ('+' / '-' MultiplicativeExpr)* */
8024 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008025 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008026
Michal Vasko004d3152020-06-11 19:59:22 +02008027 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008028 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02008029 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008030 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008031
8032 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02008033 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008034 LY_CHECK_GOTO(rc, cleanup);
8035 continue;
8036 }
8037
8038 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008039 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008040 LY_CHECK_GOTO(rc, cleanup);
8041
8042 /* eval */
8043 if (options & LYXP_SCNODE_ALL) {
8044 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008045 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008046 set_scnode_clear_ctx(set);
8047 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008048 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008049 LY_CHECK_GOTO(rc, cleanup);
8050 }
8051 }
8052
8053cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008054 lyxp_set_free_content(&orig_set);
8055 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008056 return rc;
8057}
8058
8059/**
8060 * @brief Evaluate RelationalExpr. Logs directly on error.
8061 *
Michal Vaskod3678892020-05-21 10:06:58 +02008062 * [16] RelationalExpr ::= AdditiveExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008063 * | RelationalExpr '<' AdditiveExpr
8064 * | RelationalExpr '>' AdditiveExpr
8065 * | RelationalExpr '<=' AdditiveExpr
8066 * | RelationalExpr '>=' AdditiveExpr
8067 *
8068 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008069 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008070 * @param[in] repeat How many times this expression is repeated.
8071 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8072 * @param[in] options XPath options.
8073 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8074 */
8075static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008076eval_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 +02008077{
8078 LY_ERR rc;
8079 uint16_t this_op;
8080 struct lyxp_set orig_set, set2;
8081 uint16_t i;
8082
8083 assert(repeat);
8084
8085 set_init(&orig_set, set);
8086 set_init(&set2, set);
8087
8088 set_fill_set(&orig_set, set);
8089
Michal Vasko004d3152020-06-11 19:59:22 +02008090 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008091 LY_CHECK_GOTO(rc, cleanup);
8092
8093 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
8094 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008095 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008096
Michal Vasko004d3152020-06-11 19:59:22 +02008097 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_COMP);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008098 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02008099 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008100 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008101
8102 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02008103 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008104 LY_CHECK_GOTO(rc, cleanup);
8105 continue;
8106 }
8107
8108 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008109 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008110 LY_CHECK_GOTO(rc, cleanup);
8111
8112 /* eval */
8113 if (options & LYXP_SCNODE_ALL) {
8114 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008115 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008116 set_scnode_clear_ctx(set);
8117 } else {
8118 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options);
8119 LY_CHECK_GOTO(rc, cleanup);
8120 }
8121 }
8122
8123cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008124 lyxp_set_free_content(&orig_set);
8125 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008126 return rc;
8127}
8128
8129/**
8130 * @brief Evaluate EqualityExpr. Logs directly on error.
8131 *
Michal Vaskod3678892020-05-21 10:06:58 +02008132 * [15] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008133 * | EqualityExpr '!=' RelationalExpr
8134 *
8135 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008136 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008137 * @param[in] repeat How many times this expression is repeated.
8138 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8139 * @param[in] options XPath options.
8140 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8141 */
8142static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008143eval_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 +02008144{
8145 LY_ERR rc;
8146 uint16_t this_op;
8147 struct lyxp_set orig_set, set2;
8148 uint16_t i;
8149
8150 assert(repeat);
8151
8152 set_init(&orig_set, set);
8153 set_init(&set2, set);
8154
8155 set_fill_set(&orig_set, set);
8156
Michal Vasko004d3152020-06-11 19:59:22 +02008157 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008158 LY_CHECK_GOTO(rc, cleanup);
8159
8160 /* ('=' / '!=' RelationalExpr)* */
8161 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008162 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008163
Michal Vasko004d3152020-06-11 19:59:22 +02008164 assert((exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_EQUAL) || (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_NEQUAL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008165 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko69730152020-10-09 16:30:07 +02008166 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008167 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008168
8169 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02008170 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008171 LY_CHECK_GOTO(rc, cleanup);
8172 continue;
8173 }
8174
8175 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008176 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008177 LY_CHECK_GOTO(rc, cleanup);
8178
8179 /* eval */
8180 if (options & LYXP_SCNODE_ALL) {
8181 warn_operands(set->ctx, set, &set2, 0, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vasko004d3152020-06-11 19:59:22 +02008182 warn_equality_value(exp, set, *tok_idx - 1, this_op - 1, *tok_idx - 1);
8183 warn_equality_value(exp, &set2, this_op - 1, this_op - 1, *tok_idx - 1);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008184 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008185 set_scnode_clear_ctx(set);
8186 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008187 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options);
8188 LY_CHECK_GOTO(rc, cleanup);
8189 }
8190 }
8191
8192cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008193 lyxp_set_free_content(&orig_set);
8194 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008195 return rc;
8196}
8197
8198/**
8199 * @brief Evaluate AndExpr. Logs directly on error.
8200 *
Michal Vaskod3678892020-05-21 10:06:58 +02008201 * [14] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008202 *
8203 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008204 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008205 * @param[in] repeat How many times this expression is repeated.
8206 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8207 * @param[in] options XPath options.
8208 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8209 */
8210static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008211eval_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 +02008212{
8213 LY_ERR rc;
8214 struct lyxp_set orig_set, set2;
8215 uint16_t i;
8216
8217 assert(repeat);
8218
8219 set_init(&orig_set, set);
8220 set_init(&set2, set);
8221
8222 set_fill_set(&orig_set, set);
8223
Michal Vasko004d3152020-06-11 19:59:22 +02008224 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008225 LY_CHECK_GOTO(rc, cleanup);
8226
8227 /* cast to boolean, we know that will be the final result */
8228 if (set && (options & LYXP_SCNODE_ALL)) {
8229 set_scnode_clear_ctx(set);
8230 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008231 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008232 }
8233
8234 /* ('and' EqualityExpr)* */
8235 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008236 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
8237 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (!set || !set->val.bln ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008238 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008239 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008240
8241 /* lazy evaluation */
Michal Vasko004d3152020-06-11 19:59:22 +02008242 if (!set || ((set->type == LYXP_SET_BOOLEAN) && !set->val.bln)) {
8243 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008244 LY_CHECK_GOTO(rc, cleanup);
8245 continue;
8246 }
8247
8248 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008249 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008250 LY_CHECK_GOTO(rc, cleanup);
8251
8252 /* eval - just get boolean value actually */
8253 if (set->type == LYXP_SET_SCNODE_SET) {
8254 set_scnode_clear_ctx(&set2);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008255 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008256 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008257 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008258 set_fill_set(set, &set2);
8259 }
8260 }
8261
8262cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008263 lyxp_set_free_content(&orig_set);
8264 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008265 return rc;
8266}
8267
8268/**
8269 * @brief Evaluate OrExpr. Logs directly on error.
8270 *
Michal Vaskod3678892020-05-21 10:06:58 +02008271 * [13] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008272 *
8273 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008274 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008275 * @param[in] repeat How many times this expression is repeated.
8276 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8277 * @param[in] options XPath options.
8278 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8279 */
8280static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008281eval_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 +02008282{
8283 LY_ERR rc;
8284 struct lyxp_set orig_set, set2;
8285 uint16_t i;
8286
8287 assert(repeat);
8288
8289 set_init(&orig_set, set);
8290 set_init(&set2, set);
8291
8292 set_fill_set(&orig_set, set);
8293
Michal Vasko004d3152020-06-11 19:59:22 +02008294 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008295 LY_CHECK_GOTO(rc, cleanup);
8296
8297 /* cast to boolean, we know that will be the final result */
8298 if (set && (options & LYXP_SCNODE_ALL)) {
8299 set_scnode_clear_ctx(set);
8300 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008301 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008302 }
8303
8304 /* ('or' AndExpr)* */
8305 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008306 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
8307 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (!set || set->val.bln ? "skipped" : "parsed"),
Michal Vasko69730152020-10-09 16:30:07 +02008308 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
Michal Vasko004d3152020-06-11 19:59:22 +02008309 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008310
8311 /* lazy evaluation */
Michal Vasko004d3152020-06-11 19:59:22 +02008312 if (!set || ((set->type == LYXP_SET_BOOLEAN) && set->val.bln)) {
8313 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008314 LY_CHECK_GOTO(rc, cleanup);
8315 continue;
8316 }
8317
8318 set_fill_set(&set2, &orig_set);
8319 /* expr_type cound have been LYXP_EXPR_NONE in all these later calls (except for the first one),
8320 * but it does not matter */
Michal Vasko004d3152020-06-11 19:59:22 +02008321 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008322 LY_CHECK_GOTO(rc, cleanup);
8323
8324 /* eval - just get boolean value actually */
8325 if (set->type == LYXP_SET_SCNODE_SET) {
8326 set_scnode_clear_ctx(&set2);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008327 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008328 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008329 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008330 set_fill_set(set, &set2);
8331 }
8332 }
8333
8334cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008335 lyxp_set_free_content(&orig_set);
8336 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008337 return rc;
8338}
8339
8340/**
Michal Vasko004d3152020-06-11 19:59:22 +02008341 * @brief Decide what expression is at the pointer @p tok_idx and evaluate it accordingly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008342 *
8343 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008344 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008345 * @param[in] etype Expression type to evaluate.
8346 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8347 * @param[in] options XPath options.
8348 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8349 */
8350static LY_ERR
Michal Vasko40308e72020-10-20 16:38:40 +02008351eval_expr_select(const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_expr_type etype, struct lyxp_set *set,
8352 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008353{
8354 uint16_t i, count;
8355 enum lyxp_expr_type next_etype;
8356 LY_ERR rc;
8357
8358 /* process operator repeats */
Michal Vasko004d3152020-06-11 19:59:22 +02008359 if (!exp->repeat[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008360 next_etype = LYXP_EXPR_NONE;
8361 } else {
8362 /* find etype repeat */
Radek Krejci1e008d22020-08-17 11:37:37 +02008363 for (i = 0; exp->repeat[*tok_idx][i] > etype; ++i) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02008364
8365 /* select one-priority lower because etype expression called us */
8366 if (i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008367 next_etype = exp->repeat[*tok_idx][i - 1];
Michal Vasko03ff5a72019-09-11 13:49:33 +02008368 /* count repeats for that expression */
Radek Krejci1e008d22020-08-17 11:37:37 +02008369 for (count = 0; i && exp->repeat[*tok_idx][i - 1] == next_etype; ++count, --i) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02008370 } else {
8371 next_etype = LYXP_EXPR_NONE;
8372 }
8373 }
8374
8375 /* decide what expression are we parsing based on the repeat */
8376 switch (next_etype) {
8377 case LYXP_EXPR_OR:
Michal Vasko004d3152020-06-11 19:59:22 +02008378 rc = eval_or_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008379 break;
8380 case LYXP_EXPR_AND:
Michal Vasko004d3152020-06-11 19:59:22 +02008381 rc = eval_and_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008382 break;
8383 case LYXP_EXPR_EQUALITY:
Michal Vasko004d3152020-06-11 19:59:22 +02008384 rc = eval_equality_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008385 break;
8386 case LYXP_EXPR_RELATIONAL:
Michal Vasko004d3152020-06-11 19:59:22 +02008387 rc = eval_relational_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008388 break;
8389 case LYXP_EXPR_ADDITIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02008390 rc = eval_additive_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008391 break;
8392 case LYXP_EXPR_MULTIPLICATIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02008393 rc = eval_multiplicative_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008394 break;
8395 case LYXP_EXPR_UNARY:
Michal Vasko004d3152020-06-11 19:59:22 +02008396 rc = eval_unary_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008397 break;
8398 case LYXP_EXPR_UNION:
Michal Vasko004d3152020-06-11 19:59:22 +02008399 rc = eval_union_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008400 break;
8401 case LYXP_EXPR_NONE:
Michal Vasko004d3152020-06-11 19:59:22 +02008402 rc = eval_path_expr(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008403 break;
8404 default:
8405 LOGINT_RET(set->ctx);
8406 }
8407
8408 return rc;
8409}
8410
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008411/**
8412 * @brief Get root type.
8413 *
8414 * @param[in] ctx_node Context node.
8415 * @param[in] ctx_scnode Schema context node.
8416 * @param[in] options XPath options.
8417 * @return Root type.
8418 */
8419static enum lyxp_node_type
Radek Krejci1deb5be2020-08-26 16:43:36 +02008420lyxp_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 +01008421{
Michal Vasko6b26e742020-07-17 15:02:10 +02008422 const struct lysc_node *op;
8423
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008424 if (options & LYXP_SCNODE_ALL) {
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008425 /* schema */
Radek Krejci1e008d22020-08-17 11:37:37 +02008426 for (op = ctx_scnode; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02008427
8428 if (op || (options & LYXP_SCNODE)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008429 /* general root that can access everything */
8430 return LYXP_NODE_ROOT;
8431 } else if (!ctx_scnode || (ctx_scnode->flags & LYS_CONFIG_W)) {
8432 /* root context node can access only config data (because we said so, it is unspecified) */
8433 return LYXP_NODE_ROOT_CONFIG;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008434 }
Michal Vasko6b26e742020-07-17 15:02:10 +02008435 return LYXP_NODE_ROOT;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008436 }
8437
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008438 /* data */
Michal Vasko6b26e742020-07-17 15:02:10 +02008439 op = ctx_node ? ctx_node->schema : NULL;
Michal Vaskod989ba02020-08-24 10:59:24 +02008440 for ( ; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02008441
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008442 if (op || !(options & LYXP_SCHEMA)) {
8443 /* general root that can access everything */
8444 return LYXP_NODE_ROOT;
Christian Hoppsb6ecaea2021-02-06 09:45:38 -05008445 } else if (!ctx_node || !ctx_node->schema || (ctx_node->schema->flags & LYS_CONFIG_W)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008446 /* root context node can access only config data (because we said so, it is unspecified) */
8447 return LYXP_NODE_ROOT_CONFIG;
8448 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008449 return LYXP_NODE_ROOT;
8450}
8451
Michal Vasko03ff5a72019-09-11 13:49:33 +02008452LY_ERR
Michal Vasko400e9672021-01-11 13:39:17 +01008453lyxp_eval(const struct ly_ctx *ctx, const struct lyxp_expr *exp, const struct lys_module *cur_mod,
8454 LY_PREFIX_FORMAT format, void *prefix_data, const struct lyd_node *ctx_node, const struct lyd_node *tree,
8455 struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008456{
Michal Vasko004d3152020-06-11 19:59:22 +02008457 uint16_t tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008458 LY_ERR rc;
8459
Michal Vasko400e9672021-01-11 13:39:17 +01008460 LY_CHECK_ARG_RET(ctx, ctx, exp, set, LY_EINVAL);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008461 if (!cur_mod && ((format == LY_PREF_SCHEMA) || (format == LY_PREF_SCHEMA_RESOLVED))) {
8462 LOGARG(NULL, "Current module must be set if schema format is used.");
8463 return LY_EINVAL;
Michal Vasko004d3152020-06-11 19:59:22 +02008464 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008465
Michal Vaskod3bb12f2020-12-04 14:33:09 +01008466 if (tree) {
8467 /* adjust the pointer to be the first top-level sibling */
8468 while (tree->parent) {
8469 tree = lyd_parent(tree);
8470 }
8471 tree = lyd_first_sibling(tree);
8472 }
8473
Michal Vasko03ff5a72019-09-11 13:49:33 +02008474 /* prepare set for evaluation */
Michal Vasko03ff5a72019-09-11 13:49:33 +02008475 memset(set, 0, sizeof *set);
Michal Vaskod3678892020-05-21 10:06:58 +02008476 set->type = LYXP_SET_NODE_SET;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008477 set->root_type = lyxp_get_root_type(ctx_node, NULL, options);
8478 set_insert_node(set, (struct lyd_node *)ctx_node, 0, ctx_node ? LYXP_NODE_ELEM : set->root_type, 0);
8479
Michal Vasko400e9672021-01-11 13:39:17 +01008480 set->ctx = (struct ly_ctx *)ctx;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008481 set->cur_node = ctx_node;
8482 for (set->context_op = ctx_node ? ctx_node->schema : NULL;
Radek Krejci0f969882020-08-21 16:56:47 +02008483 set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF));
8484 set->context_op = set->context_op->parent) {}
Michal Vaskof03ed032020-03-04 13:31:44 +01008485 set->tree = tree;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008486 set->cur_mod = cur_mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008487 set->format = format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008488 set->prefix_data = prefix_data;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008489
Radek Krejciddace2c2021-01-08 11:30:56 +01008490 LOG_LOCSET(NULL, set->cur_node, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01008491
Michal Vasko03ff5a72019-09-11 13:49:33 +02008492 /* evaluate */
Michal Vasko004d3152020-06-11 19:59:22 +02008493 rc = eval_expr_select(exp, &tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008494 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02008495 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008496 }
8497
Radek Krejciddace2c2021-01-08 11:30:56 +01008498 LOG_LOCBACK(0, 1, 0, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008499 return rc;
8500}
8501
8502#if 0
8503
8504/* full xml printing of set elements, not used currently */
8505
8506void
8507lyxp_set_print_xml(FILE *f, struct lyxp_set *set)
8508{
8509 uint32_t i;
8510 char *str_num;
8511 struct lyout out;
8512
8513 memset(&out, 0, sizeof out);
8514
8515 out.type = LYOUT_STREAM;
8516 out.method.f = f;
8517
8518 switch (set->type) {
8519 case LYXP_SET_EMPTY:
Michal Vasko5233e962020-08-14 14:26:20 +02008520 ly_print_(&out, "Empty XPath set\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008521 break;
8522 case LYXP_SET_BOOLEAN:
Michal Vasko5233e962020-08-14 14:26:20 +02008523 ly_print_(&out, "Boolean XPath set:\n");
8524 ly_print_(&out, "%s\n\n", set->value.bool ? "true" : "false");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008525 break;
8526 case LYXP_SET_STRING:
Michal Vasko5233e962020-08-14 14:26:20 +02008527 ly_print_(&out, "String XPath set:\n");
8528 ly_print_(&out, "\"%s\"\n\n", set->value.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008529 break;
8530 case LYXP_SET_NUMBER:
Michal Vasko5233e962020-08-14 14:26:20 +02008531 ly_print_(&out, "Number XPath set:\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008532
8533 if (isnan(set->value.num)) {
8534 str_num = strdup("NaN");
8535 } else if ((set->value.num == 0) || (set->value.num == -0.0f)) {
8536 str_num = strdup("0");
8537 } else if (isinf(set->value.num) && !signbit(set->value.num)) {
8538 str_num = strdup("Infinity");
8539 } else if (isinf(set->value.num) && signbit(set->value.num)) {
8540 str_num = strdup("-Infinity");
8541 } else if ((long long)set->value.num == set->value.num) {
8542 if (asprintf(&str_num, "%lld", (long long)set->value.num) == -1) {
8543 str_num = NULL;
8544 }
8545 } else {
8546 if (asprintf(&str_num, "%03.1Lf", set->value.num) == -1) {
8547 str_num = NULL;
8548 }
8549 }
8550 if (!str_num) {
8551 LOGMEM;
8552 return;
8553 }
Michal Vasko5233e962020-08-14 14:26:20 +02008554 ly_print_(&out, "%s\n\n", str_num);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008555 free(str_num);
8556 break;
8557 case LYXP_SET_NODE_SET:
Michal Vasko5233e962020-08-14 14:26:20 +02008558 ly_print_(&out, "Node XPath set:\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008559
8560 for (i = 0; i < set->used; ++i) {
Michal Vasko5233e962020-08-14 14:26:20 +02008561 ly_print_(&out, "%d. ", i + 1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008562 switch (set->node_type[i]) {
8563 case LYXP_NODE_ROOT_ALL:
Michal Vasko5233e962020-08-14 14:26:20 +02008564 ly_print_(&out, "ROOT all\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008565 break;
8566 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko5233e962020-08-14 14:26:20 +02008567 ly_print_(&out, "ROOT config\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008568 break;
8569 case LYXP_NODE_ROOT_STATE:
Michal Vasko5233e962020-08-14 14:26:20 +02008570 ly_print_(&out, "ROOT state\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008571 break;
8572 case LYXP_NODE_ROOT_NOTIF:
Michal Vasko5233e962020-08-14 14:26:20 +02008573 ly_print_(&out, "ROOT notification \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008574 break;
8575 case LYXP_NODE_ROOT_RPC:
Michal Vasko5233e962020-08-14 14:26:20 +02008576 ly_print_(&out, "ROOT rpc \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008577 break;
8578 case LYXP_NODE_ROOT_OUTPUT:
Michal Vasko5233e962020-08-14 14:26:20 +02008579 ly_print_(&out, "ROOT output \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008580 break;
8581 case LYXP_NODE_ELEM:
Michal Vasko5233e962020-08-14 14:26:20 +02008582 ly_print_(&out, "ELEM \"%s\"\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008583 xml_print_node(&out, 1, set->value.nodes[i], 1, LYP_FORMAT);
Michal Vasko5233e962020-08-14 14:26:20 +02008584 ly_print_(&out, "\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008585 break;
8586 case LYXP_NODE_TEXT:
Michal Vasko5233e962020-08-14 14:26:20 +02008587 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 +02008588 break;
8589 case LYXP_NODE_ATTR:
Michal Vasko5233e962020-08-14 14:26:20 +02008590 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 +02008591 break;
8592 }
8593 }
8594 break;
8595 }
8596}
8597
8598#endif
8599
8600LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008601lyxp_set_cast(struct lyxp_set *set, enum lyxp_set_type target)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008602{
8603 long double num;
8604 char *str;
8605 LY_ERR rc;
8606
8607 if (!set || (set->type == target)) {
8608 return LY_SUCCESS;
8609 }
8610
8611 /* it's not possible to convert anything into a node set */
Michal Vaskod3678892020-05-21 10:06:58 +02008612 assert(target != LYXP_SET_NODE_SET);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008613
8614 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vaskod3678892020-05-21 10:06:58 +02008615 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008616 return LY_EINVAL;
8617 }
8618
8619 /* to STRING */
Michal Vaskod3678892020-05-21 10:06:58 +02008620 if ((target == LYXP_SET_STRING) || ((target == LYXP_SET_NUMBER) && (set->type == LYXP_SET_NODE_SET))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008621 switch (set->type) {
8622 case LYXP_SET_NUMBER:
8623 if (isnan(set->val.num)) {
8624 set->val.str = strdup("NaN");
8625 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8626 } else if ((set->val.num == 0) || (set->val.num == -0.0f)) {
8627 set->val.str = strdup("0");
8628 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8629 } else if (isinf(set->val.num) && !signbit(set->val.num)) {
8630 set->val.str = strdup("Infinity");
8631 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8632 } else if (isinf(set->val.num) && signbit(set->val.num)) {
8633 set->val.str = strdup("-Infinity");
8634 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8635 } else if ((long long)set->val.num == set->val.num) {
8636 if (asprintf(&str, "%lld", (long long)set->val.num) == -1) {
8637 LOGMEM_RET(set->ctx);
8638 }
8639 set->val.str = str;
8640 } else {
8641 if (asprintf(&str, "%03.1Lf", set->val.num) == -1) {
8642 LOGMEM_RET(set->ctx);
8643 }
8644 set->val.str = str;
8645 }
8646 break;
8647 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02008648 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008649 set->val.str = strdup("true");
8650 } else {
8651 set->val.str = strdup("false");
8652 }
8653 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM);
8654 break;
8655 case LYXP_SET_NODE_SET:
Michal Vasko03ff5a72019-09-11 13:49:33 +02008656 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008657 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008658
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008659 rc = cast_node_set_to_string(set, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008660 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02008661 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008662 set->val.str = str;
8663 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008664 default:
8665 LOGINT_RET(set->ctx);
8666 }
8667 set->type = LYXP_SET_STRING;
8668 }
8669
8670 /* to NUMBER */
8671 if (target == LYXP_SET_NUMBER) {
8672 switch (set->type) {
8673 case LYXP_SET_STRING:
8674 num = cast_string_to_number(set->val.str);
Michal Vaskod3678892020-05-21 10:06:58 +02008675 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008676 set->val.num = num;
8677 break;
8678 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02008679 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008680 set->val.num = 1;
8681 } else {
8682 set->val.num = 0;
8683 }
8684 break;
8685 default:
8686 LOGINT_RET(set->ctx);
8687 }
8688 set->type = LYXP_SET_NUMBER;
8689 }
8690
8691 /* to BOOLEAN */
8692 if (target == LYXP_SET_BOOLEAN) {
8693 switch (set->type) {
8694 case LYXP_SET_NUMBER:
8695 if ((set->val.num == 0) || (set->val.num == -0.0f) || isnan(set->val.num)) {
Michal Vasko004d3152020-06-11 19:59:22 +02008696 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008697 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02008698 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008699 }
8700 break;
8701 case LYXP_SET_STRING:
8702 if (set->val.str[0]) {
Michal Vaskod3678892020-05-21 10:06:58 +02008703 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008704 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008705 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02008706 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008707 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008708 }
8709 break;
8710 case LYXP_SET_NODE_SET:
Michal Vaskod3678892020-05-21 10:06:58 +02008711 if (set->used) {
8712 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008713 set->val.bln = 1;
Michal Vaskod3678892020-05-21 10:06:58 +02008714 } else {
8715 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008716 set->val.bln = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02008717 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008718 break;
8719 default:
8720 LOGINT_RET(set->ctx);
8721 }
8722 set->type = LYXP_SET_BOOLEAN;
8723 }
8724
Michal Vasko03ff5a72019-09-11 13:49:33 +02008725 return LY_SUCCESS;
8726}
8727
8728LY_ERR
Michal Vasko400e9672021-01-11 13:39:17 +01008729lyxp_atomize(const struct ly_ctx *ctx, const struct lyxp_expr *exp, const struct lys_module *cur_mod,
8730 LY_PREFIX_FORMAT format, void *prefix_data, const struct lysc_node *ctx_scnode, struct lyxp_set *set,
8731 uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008732{
Radek Krejci2efc45b2020-12-22 16:25:44 +01008733 LY_ERR ret;
Michal Vasko004d3152020-06-11 19:59:22 +02008734 uint16_t tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008735
Michal Vasko400e9672021-01-11 13:39:17 +01008736 LY_CHECK_ARG_RET(ctx, ctx, exp, set, LY_EINVAL);
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008737 if (!cur_mod && ((format == LY_PREF_SCHEMA) || (format == LY_PREF_SCHEMA_RESOLVED))) {
8738 LOGARG(NULL, "Current module must be set if schema format is used.");
8739 return LY_EINVAL;
Michal Vasko004d3152020-06-11 19:59:22 +02008740 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008741
8742 /* prepare set for evaluation */
Michal Vasko03ff5a72019-09-11 13:49:33 +02008743 memset(set, 0, sizeof *set);
8744 set->type = LYXP_SET_SCNODE_SET;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008745 set->root_type = lyxp_get_root_type(NULL, ctx_scnode, options);
8746 LY_CHECK_RET(lyxp_set_scnode_insert_node(set, ctx_scnode, ctx_scnode ? LYXP_NODE_ELEM : set->root_type, NULL));
Radek Krejcif13b87b2020-12-01 22:02:17 +01008747 set->val.scnodes[0].in_ctx = LYXP_SET_SCNODE_START;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008748
Michal Vasko400e9672021-01-11 13:39:17 +01008749 set->ctx = (struct ly_ctx *)ctx;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008750 set->cur_scnode = ctx_scnode;
Michal Vasko6b26e742020-07-17 15:02:10 +02008751 for (set->context_op = ctx_scnode;
Radek Krejci0f969882020-08-21 16:56:47 +02008752 set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF));
8753 set->context_op = set->context_op->parent) {}
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008754 set->cur_mod = cur_mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008755 set->format = format;
Michal Vasko5d24f6c2020-10-13 13:49:06 +02008756 set->prefix_data = prefix_data;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008757
Radek Krejciddace2c2021-01-08 11:30:56 +01008758 LOG_LOCSET(set->cur_scnode, NULL, NULL, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01008759
Michal Vasko03ff5a72019-09-11 13:49:33 +02008760 /* evaluate */
Radek Krejci2efc45b2020-12-22 16:25:44 +01008761 ret = eval_expr_select(exp, &tok_idx, 0, set, options);
8762
Radek Krejciddace2c2021-01-08 11:30:56 +01008763 LOG_LOCBACK(1, 0, 0, 0);
Radek Krejci2efc45b2020-12-22 16:25:44 +01008764 return ret;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008765}
Michal Vaskod43d71a2020-08-07 14:54:58 +02008766
8767API const char *
8768lyxp_get_expr(const struct lyxp_expr *path)
8769{
8770 if (!path) {
8771 return NULL;
8772 }
8773
8774 return path->expr;
8775}