blob: 27fb0f79d259beefe8a953acfd81bdcb4c03ac89 [file] [log] [blame]
Radek Krejcib1646a92018-11-02 16:08:26 +01001/**
2 * @file xpath.c
3 * @author Michal Vasko <mvasko@cesnet.cz>
4 * @brief YANG XPath evaluation functions
5 *
Michal Vasko61ac2f62020-05-25 12:39:51 +02006 * Copyright (c) 2015 - 2020 CESNET, z.s.p.o.
Radek Krejcib1646a92018-11-02 16:08:26 +01007 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
Michal Vasko03ff5a72019-09-11 13:49:33 +020014#define _GNU_SOURCE
15
16/* needed by libmath functions isfinite(), isinf(), isnan(), signbit(), ... */
17#define _ISOC99_SOURCE
Radek Krejcib1646a92018-11-02 16:08:26 +010018
Radek Krejci535ea9f2020-05-29 16:01:05 +020019#include "xpath.h"
Radek Krejcib1646a92018-11-02 16:08:26 +010020
Radek Krejci535ea9f2020-05-29 16:01:05 +020021#include <assert.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010022#include <ctype.h>
Radek Krejci535ea9f2020-05-29 16:01:05 +020023#include <errno.h>
24#include <limits.h>
25#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"
32#include "context.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020033#include "dict.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020034#include "hash_table.h"
Michal Vasko03ff5a72019-09-11 13:49:33 +020035#include "plugins_types.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020036#include "printer.h"
37#include "printer_data.h"
38#include "tree.h"
39#include "tree_data_internal.h"
40#include "tree_schema_internal.h"
41#include "xml.h"
Michal Vasko03ff5a72019-09-11 13:49:33 +020042
Michal Vasko14676352020-05-29 11:35:55 +020043static LY_ERR reparse_or_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *exp_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +020044static LY_ERR eval_expr_select(struct lyxp_expr *exp, uint16_t *exp_idx, enum lyxp_expr_type etype, struct lyxp_set *set, int options);
45
46/**
47 * @brief Print the type of an XPath \p set.
48 *
49 * @param[in] set Set to use.
50 * @return Set type string.
51 */
52static const char *
53print_set_type(struct lyxp_set *set)
54{
55 switch (set->type) {
Michal Vasko03ff5a72019-09-11 13:49:33 +020056 case LYXP_SET_NODE_SET:
57 return "node set";
58 case LYXP_SET_SCNODE_SET:
59 return "schema node set";
60 case LYXP_SET_BOOLEAN:
61 return "boolean";
62 case LYXP_SET_NUMBER:
63 return "number";
64 case LYXP_SET_STRING:
65 return "string";
66 }
67
68 return NULL;
69}
70
Michal Vasko24cddf82020-06-01 08:17:01 +020071const char *
72lyxp_print_token(enum lyxp_token tok)
Michal Vasko03ff5a72019-09-11 13:49:33 +020073{
74 switch (tok) {
75 case LYXP_TOKEN_PAR1:
76 return "(";
77 case LYXP_TOKEN_PAR2:
78 return ")";
79 case LYXP_TOKEN_BRACK1:
80 return "[";
81 case LYXP_TOKEN_BRACK2:
82 return "]";
83 case LYXP_TOKEN_DOT:
84 return ".";
85 case LYXP_TOKEN_DDOT:
86 return "..";
87 case LYXP_TOKEN_AT:
88 return "@";
89 case LYXP_TOKEN_COMMA:
90 return ",";
91 case LYXP_TOKEN_NAMETEST:
92 return "NameTest";
93 case LYXP_TOKEN_NODETYPE:
94 return "NodeType";
95 case LYXP_TOKEN_FUNCNAME:
96 return "FunctionName";
Michal Vasko3e48bf32020-06-01 08:39:07 +020097 case LYXP_TOKEN_OPER_LOG:
Michal Vasko03ff5a72019-09-11 13:49:33 +020098 return "Operator(Logic)";
Michal Vasko3e48bf32020-06-01 08:39:07 +020099 case LYXP_TOKEN_OPER_EQUAL:
100 return "Operator(Equal)";
101 case LYXP_TOKEN_OPER_NEQUAL:
102 return "Operator(Non-equal)";
103 case LYXP_TOKEN_OPER_COMP:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200104 return "Operator(Comparison)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200105 case LYXP_TOKEN_OPER_MATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200106 return "Operator(Math)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200107 case LYXP_TOKEN_OPER_UNI:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200108 return "Operator(Union)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200109 case LYXP_TOKEN_OPER_PATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200110 return "Operator(Path)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200111 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko14676352020-05-29 11:35:55 +0200112 return "Operator(Recursive Path)";
Michal Vasko03ff5a72019-09-11 13:49:33 +0200113 case LYXP_TOKEN_LITERAL:
114 return "Literal";
115 case LYXP_TOKEN_NUMBER:
116 return "Number";
117 default:
118 LOGINT(NULL);
119 return "";
120 }
121}
122
123/**
124 * @brief Print the whole expression \p exp to debug output.
125 *
126 * @param[in] exp Expression to use.
127 */
128static void
129print_expr_struct_debug(struct lyxp_expr *exp)
130{
131 uint16_t i, j;
132 char tmp[128];
133
134 if (!exp || (ly_log_level < LY_LLDBG)) {
135 return;
136 }
137
138 LOGDBG(LY_LDGXPATH, "expression \"%s\":", exp->expr);
139 for (i = 0; i < exp->used; ++i) {
Michal Vasko24cddf82020-06-01 08:17:01 +0200140 sprintf(tmp, "\ttoken %s, in expression \"%.*s\"", lyxp_print_token(exp->tokens[i]), exp->tok_len[i],
Michal Vasko03ff5a72019-09-11 13:49:33 +0200141 &exp->expr[exp->tok_pos[i]]);
142 if (exp->repeat[i]) {
143 sprintf(tmp + strlen(tmp), " (repeat %d", exp->repeat[i][0]);
144 for (j = 1; exp->repeat[i][j]; ++j) {
145 sprintf(tmp + strlen(tmp), ", %d", exp->repeat[i][j]);
146 }
147 strcat(tmp, ")");
148 }
149 LOGDBG(LY_LDGXPATH, tmp);
150 }
151}
152
153#ifndef NDEBUG
154
155/**
156 * @brief Print XPath set content to debug output.
157 *
158 * @param[in] set Set to print.
159 */
160static void
161print_set_debug(struct lyxp_set *set)
162{
163 uint32_t i;
164 char *str;
165 int dynamic;
166 struct lyxp_set_node *item;
167 struct lyxp_set_scnode *sitem;
168
169 if (ly_log_level < LY_LLDBG) {
170 return;
171 }
172
173 switch (set->type) {
174 case LYXP_SET_NODE_SET:
175 LOGDBG(LY_LDGXPATH, "set NODE SET:");
176 for (i = 0; i < set->used; ++i) {
177 item = &set->val.nodes[i];
178
179 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +0100180 case LYXP_NODE_NONE:
181 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): NONE", i + 1, item->pos);
182 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200183 case LYXP_NODE_ROOT:
184 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT", i + 1, item->pos);
185 break;
186 case LYXP_NODE_ROOT_CONFIG:
187 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT CONFIG", i + 1, item->pos);
188 break;
189 case LYXP_NODE_ELEM:
190 if ((item->node->schema->nodetype == LYS_LIST)
191 && (((struct lyd_node_inner *)item->node)->child->schema->nodetype == LYS_LEAF)) {
192 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (1st child val: %s)", i + 1, item->pos,
193 item->node->schema->name,
194 (str = (char *)lyd_value2str((struct lyd_node_term *)lyd_node_children(item->node), &dynamic)));
195 if (dynamic) {
196 free(str);
197 }
198 } else if (((struct lyd_node_inner *)item->node)->schema->nodetype == LYS_LEAFLIST) {
199 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (val: %s)", i + 1, item->pos,
200 item->node->schema->name,
201 (str = (char *)lyd_value2str((struct lyd_node_term *)item->node, &dynamic)));
202 if (dynamic) {
203 free(str);
204 }
205 } else {
206 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s", i + 1, item->pos, item->node->schema->name);
207 }
208 break;
209 case LYXP_NODE_TEXT:
210 if (item->node->schema->nodetype & LYS_ANYDATA) {
211 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): TEXT <%s>", i + 1, item->pos,
212 item->node->schema->nodetype == LYS_ANYXML ? "anyxml" : "anydata");
213 } else {
214 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): TEXT %s", i + 1, item->pos,
215 (str = (char *)lyd_value2str((struct lyd_node_term *)item->node, &dynamic)));
216 if (dynamic) {
217 free(str);
218 }
219 }
220 break;
Michal Vasko9f96a052020-03-10 09:41:45 +0100221 case LYXP_NODE_META:
222 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): META %s = %s", i + 1, item->pos, set->val.meta[i].meta->name,
223 set->val.meta[i].meta->value);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200224 break;
225 }
226 }
227 break;
228
229 case LYXP_SET_SCNODE_SET:
230 LOGDBG(LY_LDGXPATH, "set SCNODE SET:");
231 for (i = 0; i < set->used; ++i) {
232 sitem = &set->val.scnodes[i];
233
234 switch (sitem->type) {
235 case LYXP_NODE_ROOT:
236 LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT", i + 1, sitem->in_ctx);
237 break;
238 case LYXP_NODE_ROOT_CONFIG:
239 LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT CONFIG", i + 1, sitem->in_ctx);
240 break;
241 case LYXP_NODE_ELEM:
242 LOGDBG(LY_LDGXPATH, "\t%d (%u): ELEM %s", i + 1, sitem->in_ctx, sitem->scnode->name);
243 break;
244 default:
245 LOGINT(NULL);
246 break;
247 }
248 }
249 break;
250
Michal Vasko03ff5a72019-09-11 13:49:33 +0200251 case LYXP_SET_BOOLEAN:
252 LOGDBG(LY_LDGXPATH, "set BOOLEAN");
253 LOGDBG(LY_LDGXPATH, "\t%s", (set->val.bool ? "true" : "false"));
254 break;
255
256 case LYXP_SET_STRING:
257 LOGDBG(LY_LDGXPATH, "set STRING");
258 LOGDBG(LY_LDGXPATH, "\t%s", set->val.str);
259 break;
260
261 case LYXP_SET_NUMBER:
262 LOGDBG(LY_LDGXPATH, "set NUMBER");
263
264 if (isnan(set->val.num)) {
265 str = strdup("NaN");
266 } else if ((set->val.num == 0) || (set->val.num == -0.0f)) {
267 str = strdup("0");
268 } else if (isinf(set->val.num) && !signbit(set->val.num)) {
269 str = strdup("Infinity");
270 } else if (isinf(set->val.num) && signbit(set->val.num)) {
271 str = strdup("-Infinity");
272 } else if ((long long)set->val.num == set->val.num) {
273 if (asprintf(&str, "%lld", (long long)set->val.num) == -1) {
274 str = NULL;
275 }
276 } else {
277 if (asprintf(&str, "%03.1Lf", set->val.num) == -1) {
278 str = NULL;
279 }
280 }
281 LY_CHECK_ERR_RET(!str, LOGMEM(NULL), );
282
283 LOGDBG(LY_LDGXPATH, "\t%s", str);
284 free(str);
285 }
286}
287
288#endif
289
290/**
291 * @brief Realloc the string \p str.
292 *
293 * @param[in] ctx libyang context for logging.
294 * @param[in] needed How much free space is required.
295 * @param[in,out] str Pointer to the string to use.
296 * @param[in,out] used Used bytes in \p str.
297 * @param[in,out] size Allocated bytes in \p str.
298 * @return LY_ERR
299 */
300static LY_ERR
Michal Vasko52927e22020-03-16 17:26:14 +0100301cast_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 +0200302{
303 if (*size - *used < needed) {
304 do {
305 if ((UINT16_MAX - *size) < LYXP_STRING_CAST_SIZE_STEP) {
306 LOGERR(ctx, LY_EINVAL, "XPath string length limit (%u) reached.", UINT16_MAX);
307 return LY_EINVAL;
308 }
309 *size += LYXP_STRING_CAST_SIZE_STEP;
310 } while (*size - *used < needed);
311 *str = ly_realloc(*str, *size * sizeof(char));
312 LY_CHECK_ERR_RET(!(*str), LOGMEM(ctx), LY_EMEM);
313 }
314
315 return LY_SUCCESS;
316}
317
318/**
319 * @brief Cast nodes recursively to one string @p str.
320 *
321 * @param[in] node Node to cast.
322 * @param[in] fake_cont Whether to put the data into a "fake" container.
323 * @param[in] root_type Type of the XPath root.
324 * @param[in] indent Current indent.
325 * @param[in,out] str Resulting string.
326 * @param[in,out] used Used bytes in @p str.
327 * @param[in,out] size Allocated bytes in @p str.
328 * @return LY_ERR
329 */
330static LY_ERR
331cast_string_recursive(const struct lyd_node *node, int fake_cont, enum lyxp_node_type root_type, uint16_t indent, char **str,
332 uint16_t *used, uint16_t *size)
333{
334 char *buf, *line, *ptr;
335 const char *value_str;
336 int dynamic;
337 const struct lyd_node *child;
338 struct lyd_node_any *any;
339 LY_ERR rc;
340
341 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) {
342 return LY_SUCCESS;
343 }
344
345 if (fake_cont) {
346 rc = cast_string_realloc(LYD_NODE_CTX(node), 1, str, used, size);
347 LY_CHECK_RET(rc);
348 strcpy(*str + (*used - 1), "\n");
349 ++(*used);
350
351 ++indent;
352 }
353
354 switch (node->schema->nodetype) {
355 case LYS_CONTAINER:
356 case LYS_LIST:
357 case LYS_RPC:
358 case LYS_NOTIF:
359 rc = cast_string_realloc(LYD_NODE_CTX(node), 1, str, used, size);
360 LY_CHECK_RET(rc);
361 strcpy(*str + (*used - 1), "\n");
362 ++(*used);
363
364 for (child = lyd_node_children(node); child; child = child->next) {
365 rc = cast_string_recursive(child, 0, root_type, indent + 1, str, used, size);
366 LY_CHECK_RET(rc);
367 }
368
369 break;
370
371 case LYS_LEAF:
372 case LYS_LEAFLIST:
373 value_str = lyd_value2str(((struct lyd_node_term *)node), &dynamic);
374
375 /* print indent */
376 rc = cast_string_realloc(LYD_NODE_CTX(node), indent * 2 + strlen(value_str) + 1, str, used, size);
377 if (rc != LY_SUCCESS) {
378 if (dynamic) {
379 free((char *)value_str);
380 }
381 return rc;
382 }
383 memset(*str + (*used - 1), ' ', indent * 2);
384 *used += indent * 2;
385
386 /* print value */
387 if (*used == 1) {
388 sprintf(*str + (*used - 1), "%s", value_str);
389 *used += strlen(value_str);
390 } else {
391 sprintf(*str + (*used - 1), "%s\n", value_str);
392 *used += strlen(value_str) + 1;
393 }
394 if (dynamic) {
395 free((char *)value_str);
396 }
397
398 break;
399
400 case LYS_ANYXML:
401 case LYS_ANYDATA:
402 any = (struct lyd_node_any *)node;
403 if (!(void *)any->value.tree) {
404 /* no content */
405 buf = strdup("");
406 LY_CHECK_ERR_RET(!buf, LOGMEM(LYD_NODE_CTX(node)), LY_EMEM);
407 } else {
Radek Krejci241f6b52020-05-21 18:13:49 +0200408 struct ly_out *out;
Radek Krejcia5bba312020-01-09 15:41:20 +0100409
Michal Vasko03ff5a72019-09-11 13:49:33 +0200410 switch (any->value_type) {
411 case LYD_ANYDATA_STRING:
412 case LYD_ANYDATA_XML:
413 case LYD_ANYDATA_JSON:
414 buf = strdup(any->value.json);
415 LY_CHECK_ERR_RET(!buf, LOGMEM(LYD_NODE_CTX(node)), LY_EMEM);
416 break;
417 case LYD_ANYDATA_DATATREE:
Radek Krejci241f6b52020-05-21 18:13:49 +0200418 out = ly_out_new_memory(&buf, 0);
Radek Krejcia5bba312020-01-09 15:41:20 +0100419 rc = lyd_print(out, any->value.tree, LYD_XML, LYDP_WITHSIBLINGS);
Radek Krejci241f6b52020-05-21 18:13:49 +0200420 ly_out_free(out, NULL, 0);
Radek Krejcia5bba312020-01-09 15:41:20 +0100421 LY_CHECK_RET(rc < 0, -rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200422 break;
423 /* TODO case LYD_ANYDATA_LYB:
424 LOGERR(LYD_NODE_CTX(node), LY_EINVAL, "Cannot convert LYB anydata into string.");
425 return -1;*/
426 }
427 }
428
429 line = strtok_r(buf, "\n", &ptr);
430 do {
431 rc = cast_string_realloc(LYD_NODE_CTX(node), indent * 2 + strlen(line) + 1, str, used, size);
432 if (rc != LY_SUCCESS) {
433 free(buf);
434 return rc;
435 }
436 memset(*str + (*used - 1), ' ', indent * 2);
437 *used += indent * 2;
438
439 strcpy(*str + (*used - 1), line);
440 *used += strlen(line);
441
442 strcpy(*str + (*used - 1), "\n");
443 *used += 1;
444 } while ((line = strtok_r(NULL, "\n", &ptr)));
445
446 free(buf);
447 break;
448
449 default:
450 LOGINT_RET(LYD_NODE_CTX(node));
451 }
452
453 if (fake_cont) {
454 rc = cast_string_realloc(LYD_NODE_CTX(node), 1, str, used, size);
455 LY_CHECK_RET(rc);
456 strcpy(*str + (*used - 1), "\n");
457 ++(*used);
458
459 --indent;
460 }
461
462 return LY_SUCCESS;
463}
464
465/**
466 * @brief Cast an element into a string.
467 *
468 * @param[in] node Node to cast.
469 * @param[in] fake_cont Whether to put the data into a "fake" container.
470 * @param[in] root_type Type of the XPath root.
471 * @param[out] str Element cast to dynamically-allocated string.
472 * @return LY_ERR
473 */
474static LY_ERR
475cast_string_elem(struct lyd_node *node, int fake_cont, enum lyxp_node_type root_type, char **str)
476{
477 uint16_t used, size;
478 LY_ERR rc;
479
480 *str = malloc(LYXP_STRING_CAST_SIZE_START * sizeof(char));
481 LY_CHECK_ERR_RET(!*str, LOGMEM(LYD_NODE_CTX(node)), LY_EMEM);
482 (*str)[0] = '\0';
483 used = 1;
484 size = LYXP_STRING_CAST_SIZE_START;
485
486 rc = cast_string_recursive(node, fake_cont, root_type, 0, str, &used, &size);
487 if (rc != LY_SUCCESS) {
488 free(*str);
489 return rc;
490 }
491
492 if (size > used) {
493 *str = ly_realloc(*str, used * sizeof(char));
494 LY_CHECK_ERR_RET(!*str, LOGMEM(LYD_NODE_CTX(node)), LY_EMEM);
495 }
496 return LY_SUCCESS;
497}
498
499/**
500 * @brief Cast a LYXP_SET_NODE_SET set into a string.
501 * Context position aware.
502 *
503 * @param[in] set Set to cast.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200504 * @param[out] str Cast dynamically-allocated string.
505 * @return LY_ERR
506 */
507static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100508cast_node_set_to_string(struct lyxp_set *set, char **str)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200509{
Michal Vasko03ff5a72019-09-11 13:49:33 +0200510 int dynamic;
511
Michal Vasko03ff5a72019-09-11 13:49:33 +0200512 switch (set->val.nodes[0].type) {
Michal Vasko2caefc12019-11-14 16:07:56 +0100513 case LYXP_NODE_NONE:
514 /* invalid */
515 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200516 case LYXP_NODE_ROOT:
517 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100518 return cast_string_elem(set->val.nodes[0].node, 1, set->root_type, str);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200519 case LYXP_NODE_ELEM:
520 case LYXP_NODE_TEXT:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100521 return cast_string_elem(set->val.nodes[0].node, 0, set->root_type, str);
Michal Vasko9f96a052020-03-10 09:41:45 +0100522 case LYXP_NODE_META:
523 *str = (char *)lyd_meta2str(set->val.meta[0].meta, &dynamic);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200524 if (!dynamic) {
525 *str = strdup(*str);
526 if (!*str) {
527 LOGMEM_RET(set->ctx);
528 }
529 }
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 *
559 * @param[in] val1_p First value.
560 * @param[in] val2_p Second value.
561 * @param[in] mod Whether hash table is being modified.
562 * @param[in] cb_data Callback data.
563 * @return 0 if not equal, non-zero if equal.
564 */
565static int
566set_values_equal_cb(void *val1_p, void *val2_p, int UNUSED(mod), void *UNUSED(cb_data))
567{
568 struct lyxp_set_hash_node *val1, *val2;
569
570 val1 = (struct lyxp_set_hash_node *)val1_p;
571 val2 = (struct lyxp_set_hash_node *)val2_p;
572
573 if ((val1->node == val2->node) && (val1->type == val2->type)) {
574 return 1;
575 }
576
577 return 0;
578}
579
580/**
581 * @brief Insert node and its hash into set.
582 *
583 * @param[in] set et to insert to.
584 * @param[in] node Node with hash.
585 * @param[in] type Node type.
586 */
587static void
588set_insert_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type)
589{
590 int r;
591 uint32_t i, hash;
592 struct lyxp_set_hash_node hnode;
593
594 if (!set->ht && (set->used >= LYD_HT_MIN_ITEMS)) {
595 /* create hash table and add all the nodes */
596 set->ht = lyht_new(1, sizeof(struct lyxp_set_hash_node), set_values_equal_cb, NULL, 1);
597 for (i = 0; i < set->used; ++i) {
598 hnode.node = set->val.nodes[i].node;
599 hnode.type = set->val.nodes[i].type;
600
601 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
602 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
603 hash = dict_hash_multi(hash, NULL, 0);
604
605 r = lyht_insert(set->ht, &hnode, hash, NULL);
606 assert(!r);
607 (void)r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200608
Michal Vasko9d6befd2019-12-11 14:56:56 +0100609 if (hnode.node == node) {
610 /* it was just added, do not add it twice */
611 node = NULL;
612 }
613 }
614 }
615
616 if (set->ht && node) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200617 /* add the new node into hash table */
618 hnode.node = node;
619 hnode.type = type;
620
621 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
622 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
623 hash = dict_hash_multi(hash, NULL, 0);
624
625 r = lyht_insert(set->ht, &hnode, hash, NULL);
626 assert(!r);
627 (void)r;
628 }
629}
630
631/**
632 * @brief Remove node and its hash from set.
633 *
634 * @param[in] set Set to remove from.
635 * @param[in] node Node to remove.
636 * @param[in] type Node type.
637 */
638static void
639set_remove_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type)
640{
641 int r;
642 struct lyxp_set_hash_node hnode;
643 uint32_t hash;
644
645 if (set->ht) {
646 hnode.node = node;
647 hnode.type = type;
648
649 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
650 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
651 hash = dict_hash_multi(hash, NULL, 0);
652
653 r = lyht_remove(set->ht, &hnode, hash);
654 assert(!r);
655 (void)r;
656
657 if (!set->ht->used) {
658 lyht_free(set->ht);
659 set->ht = NULL;
660 }
661 }
662}
663
664/**
665 * @brief Check whether node is in set based on its hash.
666 *
667 * @param[in] set Set to search in.
668 * @param[in] node Node to search for.
669 * @param[in] type Node type.
670 * @param[in] skip_idx Index in @p set to skip.
671 * @return LY_ERR
672 */
673static LY_ERR
674set_dup_node_hash_check(const struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type, int skip_idx)
675{
676 struct lyxp_set_hash_node hnode, *match_p;
677 uint32_t hash;
678
679 hnode.node = node;
680 hnode.type = type;
681
682 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
683 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
684 hash = dict_hash_multi(hash, NULL, 0);
685
686 if (!lyht_find(set->ht, &hnode, hash, (void **)&match_p)) {
687 if ((skip_idx > -1) && (set->val.nodes[skip_idx].node == match_p->node) && (set->val.nodes[skip_idx].type == match_p->type)) {
688 /* we found it on the index that should be skipped, find another */
689 hnode = *match_p;
690 if (lyht_find_next(set->ht, &hnode, hash, (void **)&match_p)) {
691 /* none other found */
692 return LY_SUCCESS;
693 }
694 }
695
696 return LY_EEXIST;
697 }
698
699 /* not found */
700 return LY_SUCCESS;
701}
702
Michal Vaskod3678892020-05-21 10:06:58 +0200703void
704lyxp_set_free_content(struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200705{
706 if (!set) {
707 return;
708 }
709
710 if (set->type == LYXP_SET_NODE_SET) {
711 free(set->val.nodes);
712 lyht_free(set->ht);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200713 } else if (set->type == LYXP_SET_SCNODE_SET) {
714 free(set->val.scnodes);
Michal Vaskod3678892020-05-21 10:06:58 +0200715 lyht_free(set->ht);
716 } else {
717 if (set->type == LYXP_SET_STRING) {
718 free(set->val.str);
719 }
720 set->type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200721 }
Michal Vaskod3678892020-05-21 10:06:58 +0200722
723 set->val.nodes = NULL;
724 set->used = 0;
725 set->size = 0;
726 set->ht = NULL;
727 set->ctx_pos = 0;
728 set->ctx_pos = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200729}
730
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100731/**
732 * @brief Free dynamically-allocated set.
733 *
734 * @param[in] set Set to free.
735 */
736static void
Michal Vasko03ff5a72019-09-11 13:49:33 +0200737lyxp_set_free(struct lyxp_set *set)
738{
739 if (!set) {
740 return;
741 }
742
Michal Vaskod3678892020-05-21 10:06:58 +0200743 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200744 free(set);
745}
746
747/**
748 * @brief Initialize set context.
749 *
750 * @param[in] new Set to initialize.
751 * @param[in] set Arbitrary initialized set.
752 */
753static void
754set_init(struct lyxp_set *new, struct lyxp_set *set)
755{
756 memset(new, 0, sizeof *new);
Michal Vasko02a77382019-09-12 11:47:35 +0200757 if (set) {
758 new->ctx = set->ctx;
759 new->ctx_node = set->ctx_node;
Michal Vasko588112f2019-12-10 14:51:53 +0100760 new->root_type = set->root_type;
Michal Vasko02a77382019-09-12 11:47:35 +0200761 new->local_mod = set->local_mod;
Michal Vaskof03ed032020-03-04 13:31:44 +0100762 new->tree = set->tree;
Michal Vasko02a77382019-09-12 11:47:35 +0200763 new->format = set->format;
764 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200765}
766
767/**
768 * @brief Create a deep copy of a set.
769 *
770 * @param[in] set Set to copy.
771 * @return Copy of @p set.
772 */
773static struct lyxp_set *
774set_copy(struct lyxp_set *set)
775{
776 struct lyxp_set *ret;
777 uint16_t i;
Michal Vaskoba716542019-12-16 10:01:58 +0100778 int idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200779
780 if (!set) {
781 return NULL;
782 }
783
784 ret = malloc(sizeof *ret);
785 LY_CHECK_ERR_RET(!ret, LOGMEM(set->ctx), NULL);
786 set_init(ret, set);
787
788 if (set->type == LYXP_SET_SCNODE_SET) {
789 ret->type = set->type;
790
791 for (i = 0; i < set->used; ++i) {
Michal Vaskoba716542019-12-16 10:01:58 +0100792 if ((set->val.scnodes[i].in_ctx == 1) || (set->val.scnodes[i].in_ctx == -2)) {
793 idx = lyxp_set_scnode_insert_node(ret, set->val.scnodes[i].scnode, set->val.scnodes[i].type);
Michal Vasko3f27c522020-01-06 08:37:49 +0100794 /* coverity seems to think scnodes can be NULL */
795 if ((idx == -1) || !ret->val.scnodes) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200796 lyxp_set_free(ret);
797 return NULL;
798 }
Michal Vaskoba716542019-12-16 10:01:58 +0100799 ret->val.scnodes[idx].in_ctx = set->val.scnodes[i].in_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200800 }
801 }
802 } else if (set->type == LYXP_SET_NODE_SET) {
803 ret->type = set->type;
804 ret->val.nodes = malloc(set->used * sizeof *ret->val.nodes);
805 LY_CHECK_ERR_RET(!ret->val.nodes, LOGMEM(set->ctx); free(ret), NULL);
806 memcpy(ret->val.nodes, set->val.nodes, set->used * sizeof *ret->val.nodes);
807
808 ret->used = ret->size = set->used;
809 ret->ctx_pos = set->ctx_pos;
810 ret->ctx_size = set->ctx_size;
811 ret->ht = lyht_dup(set->ht);
812 } else {
813 memcpy(ret, set, sizeof *ret);
814 if (set->type == LYXP_SET_STRING) {
815 ret->val.str = strdup(set->val.str);
816 LY_CHECK_ERR_RET(!ret->val.str, LOGMEM(set->ctx); free(ret), NULL);
817 }
818 }
819
820 return ret;
821}
822
823/**
824 * @brief Fill XPath set with a string. Any current data are disposed of.
825 *
826 * @param[in] set Set to fill.
827 * @param[in] string String to fill into \p set.
828 * @param[in] str_len Length of \p string. 0 is a valid value!
829 */
830static void
831set_fill_string(struct lyxp_set *set, const char *string, uint16_t str_len)
832{
Michal Vaskod3678892020-05-21 10:06:58 +0200833 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200834
835 set->type = LYXP_SET_STRING;
836 if ((str_len == 0) && (string[0] != '\0')) {
837 string = "";
838 }
839 set->val.str = strndup(string, str_len);
840}
841
842/**
843 * @brief Fill XPath set with a number. Any current data are disposed of.
844 *
845 * @param[in] set Set to fill.
846 * @param[in] number Number to fill into \p set.
847 */
848static void
849set_fill_number(struct lyxp_set *set, long double number)
850{
Michal Vaskod3678892020-05-21 10:06:58 +0200851 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200852
853 set->type = LYXP_SET_NUMBER;
854 set->val.num = number;
855}
856
857/**
858 * @brief Fill XPath set with a boolean. Any current data are disposed of.
859 *
860 * @param[in] set Set to fill.
861 * @param[in] boolean Boolean to fill into \p set.
862 */
863static void
864set_fill_boolean(struct lyxp_set *set, int boolean)
865{
Michal Vaskod3678892020-05-21 10:06:58 +0200866 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200867
868 set->type = LYXP_SET_BOOLEAN;
869 set->val.bool = boolean;
870}
871
872/**
873 * @brief Fill XPath set with the value from another set (deep assign).
874 * Any current data are disposed of.
875 *
876 * @param[in] trg Set to fill.
877 * @param[in] src Source set to copy into \p trg.
878 */
879static void
880set_fill_set(struct lyxp_set *trg, struct lyxp_set *src)
881{
882 if (!trg || !src) {
883 return;
884 }
885
886 if (trg->type == LYXP_SET_NODE_SET) {
887 free(trg->val.nodes);
888 } else if (trg->type == LYXP_SET_STRING) {
889 free(trg->val.str);
890 }
891 set_init(trg, src);
892
893 if (src->type == LYXP_SET_SCNODE_SET) {
894 trg->type = LYXP_SET_SCNODE_SET;
895 trg->used = src->used;
896 trg->size = src->used;
897
898 trg->val.scnodes = ly_realloc(trg->val.scnodes, trg->size * sizeof *trg->val.scnodes);
899 LY_CHECK_ERR_RET(!trg->val.scnodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), );
900 memcpy(trg->val.scnodes, src->val.scnodes, src->used * sizeof *src->val.scnodes);
901 } else if (src->type == LYXP_SET_BOOLEAN) {
902 set_fill_boolean(trg, src->val.bool);
903 } else if (src->type == LYXP_SET_NUMBER) {
904 set_fill_number(trg, src->val.num);
905 } else if (src->type == LYXP_SET_STRING) {
906 set_fill_string(trg, src->val.str, strlen(src->val.str));
907 } else {
908 if (trg->type == LYXP_SET_NODE_SET) {
909 free(trg->val.nodes);
910 } else if (trg->type == LYXP_SET_STRING) {
911 free(trg->val.str);
912 }
913
Michal Vaskod3678892020-05-21 10:06:58 +0200914 assert(src->type == LYXP_SET_NODE_SET);
915
916 trg->type = LYXP_SET_NODE_SET;
917 trg->used = src->used;
918 trg->size = src->used;
919 trg->ctx_pos = src->ctx_pos;
920 trg->ctx_size = src->ctx_size;
921
922 trg->val.nodes = malloc(trg->used * sizeof *trg->val.nodes);
923 LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), );
924 memcpy(trg->val.nodes, src->val.nodes, src->used * sizeof *src->val.nodes);
925 if (src->ht) {
926 trg->ht = lyht_dup(src->ht);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200927 } else {
Michal Vaskod3678892020-05-21 10:06:58 +0200928 trg->ht = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200929 }
930 }
931}
932
933/**
934 * @brief Clear context of all schema nodes.
935 *
936 * @param[in] set Set to clear.
937 */
938static void
939set_scnode_clear_ctx(struct lyxp_set *set)
940{
941 uint32_t i;
942
943 for (i = 0; i < set->used; ++i) {
944 if (set->val.scnodes[i].in_ctx == 1) {
945 set->val.scnodes[i].in_ctx = 0;
Michal Vasko5c4e5892019-11-14 12:31:38 +0100946 } else if (set->val.scnodes[i].in_ctx == -2) {
947 set->val.scnodes[i].in_ctx = -1;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200948 }
949 }
950}
951
952/**
953 * @brief Remove a node from a set. Removing last node changes
954 * set into LYXP_SET_EMPTY. Context position aware.
955 *
956 * @param[in] set Set to use.
957 * @param[in] idx Index from @p set of the node to be removed.
958 */
959static void
960set_remove_node(struct lyxp_set *set, uint32_t idx)
961{
962 assert(set && (set->type == LYXP_SET_NODE_SET));
963 assert(idx < set->used);
964
965 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
966
967 --set->used;
968 if (set->used) {
969 memmove(&set->val.nodes[idx], &set->val.nodes[idx + 1],
970 (set->used - idx) * sizeof *set->val.nodes);
971 } else {
Michal Vaskod3678892020-05-21 10:06:58 +0200972 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200973 }
974}
975
976/**
Michal Vasko2caefc12019-11-14 16:07:56 +0100977 * @brief Remove a node from a set by setting its type to LYXP_NODE_NONE.
Michal Vasko57eab132019-09-24 11:46:26 +0200978 *
979 * @param[in] set Set to use.
980 * @param[in] idx Index from @p set of the node to be removed.
981 */
982static void
Michal Vasko2caefc12019-11-14 16:07:56 +0100983set_remove_node_none(struct lyxp_set *set, uint32_t idx)
Michal Vasko57eab132019-09-24 11:46:26 +0200984{
985 assert(set && (set->type == LYXP_SET_NODE_SET));
986 assert(idx < set->used);
987
Michal Vasko2caefc12019-11-14 16:07:56 +0100988 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
989 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
990 }
991 set->val.nodes[idx].type = LYXP_NODE_NONE;
Michal Vasko57eab132019-09-24 11:46:26 +0200992}
993
994/**
Michal Vasko2caefc12019-11-14 16:07:56 +0100995 * @brief Remove all LYXP_NODE_NONE nodes from a set. Removing last node changes
Michal Vasko57eab132019-09-24 11:46:26 +0200996 * set into LYXP_SET_EMPTY. Context position aware.
997 *
998 * @param[in] set Set to consolidate.
999 */
1000static void
Michal Vasko2caefc12019-11-14 16:07:56 +01001001set_remove_nodes_none(struct lyxp_set *set)
Michal Vasko57eab132019-09-24 11:46:26 +02001002{
1003 uint16_t i, orig_used, end;
1004 int32_t start;
1005
Michal Vaskod3678892020-05-21 10:06:58 +02001006 assert(set);
Michal Vasko57eab132019-09-24 11:46:26 +02001007
1008 orig_used = set->used;
1009 set->used = 0;
1010 for (i = 0; i < orig_used;) {
1011 start = -1;
1012 do {
Michal Vasko2caefc12019-11-14 16:07:56 +01001013 if ((set->val.nodes[i].type != LYXP_NODE_NONE) && (start == -1)) {
Michal Vasko57eab132019-09-24 11:46:26 +02001014 start = i;
Michal Vasko2caefc12019-11-14 16:07:56 +01001015 } else if ((start > -1) && (set->val.nodes[i].type == LYXP_NODE_NONE)) {
Michal Vasko57eab132019-09-24 11:46:26 +02001016 end = i;
1017 ++i;
1018 break;
1019 }
1020
1021 ++i;
1022 if (i == orig_used) {
1023 end = i;
1024 }
1025 } while (i < orig_used);
1026
1027 if (start > -1) {
1028 /* move the whole chunk of valid nodes together */
1029 if (set->used != (unsigned)start) {
1030 memmove(&set->val.nodes[set->used], &set->val.nodes[start], (end - start) * sizeof *set->val.nodes);
1031 }
1032 set->used += end - start;
1033 }
1034 }
Michal Vasko57eab132019-09-24 11:46:26 +02001035}
1036
1037/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02001038 * @brief Check for duplicates in a node set.
1039 *
1040 * @param[in] set Set to check.
1041 * @param[in] node Node to look for in @p set.
1042 * @param[in] node_type Type of @p node.
1043 * @param[in] skip_idx Index from @p set to skip.
1044 * @return LY_ERR
1045 */
1046static LY_ERR
1047set_dup_node_check(const struct lyxp_set *set, const struct lyd_node *node, enum lyxp_node_type node_type, int skip_idx)
1048{
1049 uint32_t i;
1050
Michal Vasko2caefc12019-11-14 16:07:56 +01001051 if (set->ht && node) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001052 return set_dup_node_hash_check(set, (struct lyd_node *)node, node_type, skip_idx);
1053 }
1054
1055 for (i = 0; i < set->used; ++i) {
1056 if ((skip_idx > -1) && (i == (unsigned)skip_idx)) {
1057 continue;
1058 }
1059
1060 if ((set->val.nodes[i].node == node) && (set->val.nodes[i].type == node_type)) {
1061 return LY_EEXIST;
1062 }
1063 }
1064
1065 return LY_SUCCESS;
1066}
1067
Michal Vaskoecd62de2019-11-13 12:35:11 +01001068int
1069lyxp_set_scnode_dup_node_check(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type, int skip_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001070{
1071 uint32_t i;
1072
1073 for (i = 0; i < set->used; ++i) {
1074 if ((skip_idx > -1) && (i == (unsigned)skip_idx)) {
1075 continue;
1076 }
1077
1078 if ((set->val.scnodes[i].scnode == node) && (set->val.scnodes[i].type == node_type)) {
1079 return i;
1080 }
1081 }
1082
1083 return -1;
1084}
1085
Michal Vaskoecd62de2019-11-13 12:35:11 +01001086void
1087lyxp_set_scnode_merge(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001088{
1089 uint32_t orig_used, i, j;
1090
Michal Vaskod3678892020-05-21 10:06:58 +02001091 assert((set1->type == LYXP_SET_SCNODE_SET) && (set2->type == LYXP_SET_SCNODE_SET));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001092
Michal Vaskod3678892020-05-21 10:06:58 +02001093 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001094 return;
1095 }
1096
Michal Vaskod3678892020-05-21 10:06:58 +02001097 if (!set1->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001098 memcpy(set1, set2, sizeof *set1);
1099 return;
1100 }
1101
1102 if (set1->used + set2->used > set1->size) {
1103 set1->size = set1->used + set2->used;
1104 set1->val.scnodes = ly_realloc(set1->val.scnodes, set1->size * sizeof *set1->val.scnodes);
1105 LY_CHECK_ERR_RET(!set1->val.scnodes, LOGMEM(set1->ctx), );
1106 }
1107
1108 orig_used = set1->used;
1109
1110 for (i = 0; i < set2->used; ++i) {
1111 for (j = 0; j < orig_used; ++j) {
1112 /* detect duplicities */
1113 if (set1->val.scnodes[j].scnode == set2->val.scnodes[i].scnode) {
1114 break;
1115 }
1116 }
1117
1118 if (j == orig_used) {
1119 memcpy(&set1->val.scnodes[set1->used], &set2->val.scnodes[i], sizeof *set2->val.scnodes);
1120 ++set1->used;
1121 }
1122 }
1123
Michal Vaskod3678892020-05-21 10:06:58 +02001124 lyxp_set_free_content(set2);
1125 set2->type = LYXP_SET_SCNODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001126}
1127
1128/**
1129 * @brief Insert a node into a set. Context position aware.
1130 *
1131 * @param[in] set Set to use.
1132 * @param[in] node Node to insert to @p set.
1133 * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting.
1134 * @param[in] node_type Node type of @p node.
1135 * @param[in] idx Index in @p set to insert into.
1136 */
1137static void
1138set_insert_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx)
1139{
Michal Vaskod3678892020-05-21 10:06:58 +02001140 assert(set && (set->type == LYXP_SET_NODE_SET));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001141
Michal Vaskod3678892020-05-21 10:06:58 +02001142 if (!set->size) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001143 /* first item */
1144 if (idx) {
1145 /* no real harm done, but it is a bug */
1146 LOGINT(set->ctx);
1147 idx = 0;
1148 }
1149 set->val.nodes = malloc(LYXP_SET_SIZE_START * sizeof *set->val.nodes);
1150 LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), );
1151 set->type = LYXP_SET_NODE_SET;
1152 set->used = 0;
1153 set->size = LYXP_SET_SIZE_START;
1154 set->ctx_pos = 1;
1155 set->ctx_size = 1;
1156 set->ht = NULL;
1157 } else {
1158 /* not an empty set */
1159 if (set->used == set->size) {
1160
1161 /* set is full */
1162 set->val.nodes = ly_realloc(set->val.nodes, (set->size + LYXP_SET_SIZE_STEP) * sizeof *set->val.nodes);
1163 LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), );
1164 set->size += LYXP_SET_SIZE_STEP;
1165 }
1166
1167 if (idx > set->used) {
1168 LOGINT(set->ctx);
1169 idx = set->used;
1170 }
1171
1172 /* make space for the new node */
1173 if (idx < set->used) {
1174 memmove(&set->val.nodes[idx + 1], &set->val.nodes[idx], (set->used - idx) * sizeof *set->val.nodes);
1175 }
1176 }
1177
1178 /* finally assign the value */
1179 set->val.nodes[idx].node = (struct lyd_node *)node;
1180 set->val.nodes[idx].type = node_type;
1181 set->val.nodes[idx].pos = pos;
1182 ++set->used;
1183
Michal Vasko2caefc12019-11-14 16:07:56 +01001184 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1185 set_insert_node_hash(set, (struct lyd_node *)node, node_type);
1186 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001187}
1188
Michal Vaskoecd62de2019-11-13 12:35:11 +01001189int
1190lyxp_set_scnode_insert_node(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001191{
1192 int ret;
1193
1194 assert(set->type == LYXP_SET_SCNODE_SET);
1195
Michal Vaskoecd62de2019-11-13 12:35:11 +01001196 ret = lyxp_set_scnode_dup_node_check(set, node, node_type, -1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001197 if (ret > -1) {
1198 set->val.scnodes[ret].in_ctx = 1;
1199 } else {
1200 if (set->used == set->size) {
1201 set->val.scnodes = ly_realloc(set->val.scnodes, (set->size + LYXP_SET_SIZE_STEP) * sizeof *set->val.scnodes);
1202 LY_CHECK_ERR_RET(!set->val.scnodes, LOGMEM(set->ctx), -1);
1203 set->size += LYXP_SET_SIZE_STEP;
1204 }
1205
1206 ret = set->used;
1207 set->val.scnodes[ret].scnode = (struct lysc_node *)node;
1208 set->val.scnodes[ret].type = node_type;
1209 set->val.scnodes[ret].in_ctx = 1;
1210 ++set->used;
1211 }
1212
1213 return ret;
1214}
1215
1216/**
1217 * @brief Replace a node in a set with another. Context position aware.
1218 *
1219 * @param[in] set Set to use.
1220 * @param[in] node Node to insert to @p set.
1221 * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting.
1222 * @param[in] node_type Node type of @p node.
1223 * @param[in] idx Index in @p set of the node to replace.
1224 */
1225static void
1226set_replace_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx)
1227{
1228 assert(set && (idx < set->used));
1229
Michal Vasko2caefc12019-11-14 16:07:56 +01001230 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1231 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1232 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001233 set->val.nodes[idx].node = (struct lyd_node *)node;
1234 set->val.nodes[idx].type = node_type;
1235 set->val.nodes[idx].pos = pos;
Michal Vasko2caefc12019-11-14 16:07:56 +01001236 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1237 set_insert_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1238 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001239}
1240
1241/**
1242 * @brief Set all nodes with ctx 1 to a new unique context value.
1243 *
1244 * @param[in] set Set to modify.
1245 * @return New context value.
1246 */
Michal Vasko5c4e5892019-11-14 12:31:38 +01001247static int32_t
Michal Vasko03ff5a72019-09-11 13:49:33 +02001248set_scnode_new_in_ctx(struct lyxp_set *set)
1249{
Michal Vasko5c4e5892019-11-14 12:31:38 +01001250 uint32_t i;
1251 int32_t ret_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001252
1253 assert(set->type == LYXP_SET_SCNODE_SET);
1254
1255 ret_ctx = 3;
1256retry:
1257 for (i = 0; i < set->used; ++i) {
1258 if (set->val.scnodes[i].in_ctx >= ret_ctx) {
1259 ret_ctx = set->val.scnodes[i].in_ctx + 1;
1260 goto retry;
1261 }
1262 }
1263 for (i = 0; i < set->used; ++i) {
1264 if (set->val.scnodes[i].in_ctx == 1) {
1265 set->val.scnodes[i].in_ctx = ret_ctx;
1266 }
1267 }
1268
1269 return ret_ctx;
1270}
1271
1272/**
1273 * @brief Get unique @p node position in the data.
1274 *
1275 * @param[in] node Node to find.
1276 * @param[in] node_type Node type of @p node.
1277 * @param[in] root Root node.
1278 * @param[in] root_type Type of the XPath @p root node.
1279 * @param[in] prev Node that we think is before @p node in DFS from @p root. Can optionally
1280 * be used to increase efficiency and start the DFS from this node.
1281 * @param[in] prev_pos Node @p prev position. Optional, but must be set if @p prev is set.
1282 * @return Node position.
1283 */
1284static uint32_t
1285get_node_pos(const struct lyd_node *node, enum lyxp_node_type node_type, const struct lyd_node *root,
1286 enum lyxp_node_type root_type, const struct lyd_node **prev, uint32_t *prev_pos)
1287{
1288 const struct lyd_node *next, *elem, *top_sibling;
1289 uint32_t pos = 1;
1290
1291 assert(prev && prev_pos && !root->prev->next);
1292
1293 if ((node_type == LYXP_NODE_ROOT) || (node_type == LYXP_NODE_ROOT_CONFIG)) {
1294 return 0;
1295 }
1296
1297 if (*prev) {
1298 /* start from the previous element instead from the root */
1299 elem = next = *prev;
1300 pos = *prev_pos;
1301 for (top_sibling = elem; top_sibling->parent; top_sibling = (struct lyd_node *)top_sibling->parent);
1302 goto dfs_search;
1303 }
1304
1305 for (top_sibling = root; top_sibling; top_sibling = top_sibling->next) {
1306 /* TREE DFS */
1307 LYD_TREE_DFS_BEGIN(top_sibling, next, elem) {
1308dfs_search:
1309 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (elem->schema->flags & LYS_CONFIG_R)) {
1310 goto skip_children;
1311 }
1312
1313 if (elem == node) {
1314 break;
1315 }
1316 ++pos;
1317
1318 /* TREE DFS END */
1319 /* select element for the next run - children first,
1320 * child exception for lyd_node_leaf and lyd_node_leaflist, but not the root */
1321 if (elem->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) {
1322 next = NULL;
1323 } else {
1324 next = lyd_node_children(elem);
1325 }
1326 if (!next) {
1327skip_children:
1328 /* no children */
1329 if (elem == top_sibling) {
1330 /* we are done, root has no children */
1331 elem = NULL;
1332 break;
1333 }
1334 /* try siblings */
1335 next = elem->next;
1336 }
1337 while (!next) {
1338 /* no siblings, go back through parents */
1339 if (elem->parent == top_sibling->parent) {
1340 /* we are done, no next element to process */
1341 elem = NULL;
1342 break;
1343 }
1344 /* parent is already processed, go to its sibling */
1345 elem = (struct lyd_node *)elem->parent;
1346 next = elem->next;
1347 }
1348 }
1349
1350 /* node found */
1351 if (elem) {
1352 break;
1353 }
1354 }
1355
1356 if (!elem) {
1357 if (!(*prev)) {
1358 /* we went from root and failed to find it, cannot be */
1359 LOGINT(node->schema->module->ctx);
1360 return 0;
1361 } else {
1362 *prev = NULL;
1363 *prev_pos = 0;
1364
1365 elem = next = top_sibling = root;
1366 pos = 1;
1367 goto dfs_search;
1368 }
1369 }
1370
1371 /* remember the last found node for next time */
1372 *prev = node;
1373 *prev_pos = pos;
1374
1375 return pos;
1376}
1377
1378/**
1379 * @brief Assign (fill) missing node positions.
1380 *
1381 * @param[in] set Set to fill positions in.
1382 * @param[in] root Context root node.
1383 * @param[in] root_type Context root type.
1384 * @return LY_ERR
1385 */
1386static LY_ERR
1387set_assign_pos(struct lyxp_set *set, const struct lyd_node *root, enum lyxp_node_type root_type)
1388{
1389 const struct lyd_node *prev = NULL, *tmp_node;
1390 uint32_t i, tmp_pos = 0;
1391
1392 for (i = 0; i < set->used; ++i) {
1393 if (!set->val.nodes[i].pos) {
1394 tmp_node = NULL;
1395 switch (set->val.nodes[i].type) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001396 case LYXP_NODE_META:
1397 tmp_node = set->val.meta[i].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001398 if (!tmp_node) {
1399 LOGINT_RET(root->schema->module->ctx);
1400 }
1401 /* fallthrough */
1402 case LYXP_NODE_ELEM:
1403 case LYXP_NODE_TEXT:
1404 if (!tmp_node) {
1405 tmp_node = set->val.nodes[i].node;
1406 }
1407 set->val.nodes[i].pos = get_node_pos(tmp_node, set->val.nodes[i].type, root, root_type, &prev, &tmp_pos);
1408 break;
1409 default:
1410 /* all roots have position 0 */
1411 break;
1412 }
1413 }
1414 }
1415
1416 return LY_SUCCESS;
1417}
1418
1419/**
Michal Vasko9f96a052020-03-10 09:41:45 +01001420 * @brief Get unique @p meta position in the parent metadata.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001421 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001422 * @param[in] meta Metadata to use.
1423 * @return Metadata position.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001424 */
1425static uint16_t
Michal Vasko9f96a052020-03-10 09:41:45 +01001426get_meta_pos(struct lyd_meta *meta)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001427{
1428 uint16_t pos = 0;
Michal Vasko9f96a052020-03-10 09:41:45 +01001429 struct lyd_meta *meta2;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001430
Michal Vasko9f96a052020-03-10 09:41:45 +01001431 for (meta2 = meta->parent->meta; meta2 && (meta2 != meta); meta2 = meta2->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001432 ++pos;
1433 }
1434
Michal Vasko9f96a052020-03-10 09:41:45 +01001435 assert(meta2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001436 return pos;
1437}
1438
1439/**
1440 * @brief Compare 2 nodes in respect to XPath document order.
1441 *
1442 * @param[in] item1 1st node.
1443 * @param[in] item2 2nd node.
1444 * @return If 1st > 2nd returns 1, 1st == 2nd returns 0, and 1st < 2nd returns -1.
1445 */
1446static int
1447set_sort_compare(struct lyxp_set_node *item1, struct lyxp_set_node *item2)
1448{
Michal Vasko9f96a052020-03-10 09:41:45 +01001449 uint32_t meta_pos1 = 0, meta_pos2 = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001450
1451 if (item1->pos < item2->pos) {
1452 return -1;
1453 }
1454
1455 if (item1->pos > item2->pos) {
1456 return 1;
1457 }
1458
1459 /* node positions are equal, the fun case */
1460
1461 /* 1st ELEM - == - 2nd TEXT, 1st TEXT - == - 2nd ELEM */
1462 /* special case since text nodes are actually saved as their parents */
1463 if ((item1->node == item2->node) && (item1->type != item2->type)) {
1464 if (item1->type == LYXP_NODE_ELEM) {
1465 assert(item2->type == LYXP_NODE_TEXT);
1466 return -1;
1467 } else {
1468 assert((item1->type == LYXP_NODE_TEXT) && (item2->type == LYXP_NODE_ELEM));
1469 return 1;
1470 }
1471 }
1472
Michal Vasko9f96a052020-03-10 09:41:45 +01001473 /* we need meta positions now */
1474 if (item1->type == LYXP_NODE_META) {
1475 meta_pos1 = get_meta_pos((struct lyd_meta *)item1->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001476 }
Michal Vasko9f96a052020-03-10 09:41:45 +01001477 if (item2->type == LYXP_NODE_META) {
1478 meta_pos2 = get_meta_pos((struct lyd_meta *)item2->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001479 }
1480
Michal Vasko9f96a052020-03-10 09:41:45 +01001481 /* 1st ROOT - 2nd ROOT, 1st ELEM - 2nd ELEM, 1st TEXT - 2nd TEXT, 1st META - =pos= - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001482 /* check for duplicates */
1483 if (item1->node == item2->node) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001484 assert((item1->type == item2->type) && ((item1->type != LYXP_NODE_META) || (meta_pos1 == meta_pos2)));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001485 return 0;
1486 }
1487
Michal Vasko9f96a052020-03-10 09:41:45 +01001488 /* 1st ELEM - 2nd TEXT, 1st ELEM - any pos - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001489 /* elem is always first, 2nd node is after it */
1490 if (item1->type == LYXP_NODE_ELEM) {
1491 assert(item2->type != LYXP_NODE_ELEM);
1492 return -1;
1493 }
1494
Michal Vasko9f96a052020-03-10 09:41:45 +01001495 /* 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 +02001496 /* 2nd is before 1st */
1497 if (((item1->type == LYXP_NODE_TEXT)
Michal Vasko9f96a052020-03-10 09:41:45 +01001498 && ((item2->type == LYXP_NODE_ELEM) || (item2->type == LYXP_NODE_META)))
1499 || ((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_ELEM))
1500 || (((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_META))
1501 && (meta_pos1 > meta_pos2))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001502 return 1;
1503 }
1504
Michal Vasko9f96a052020-03-10 09:41:45 +01001505 /* 1st META - any pos - 2nd TEXT, 1st META <pos< - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001506 /* 2nd is after 1st */
1507 return -1;
1508}
1509
1510/**
1511 * @brief Set cast for comparisons.
1512 *
1513 * @param[in] trg Target set to cast source into.
1514 * @param[in] src Source set.
1515 * @param[in] type Target set type.
1516 * @param[in] src_idx Source set node index.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001517 * @return LY_ERR
1518 */
1519static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001520set_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 +02001521{
1522 assert(src->type == LYXP_SET_NODE_SET);
1523
1524 set_init(trg, src);
1525
1526 /* insert node into target set */
1527 set_insert_node(trg, src->val.nodes[src_idx].node, src->val.nodes[src_idx].pos, src->val.nodes[src_idx].type, 0);
1528
1529 /* cast target set appropriately */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001530 return lyxp_set_cast(trg, type);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001531}
1532
1533#ifndef NDEBUG
1534
1535/**
1536 * @brief Bubble sort @p set into XPath document order.
1537 * Context position aware. Unused in the 'Release' build target.
1538 *
1539 * @param[in] set Set to sort.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001540 * @return How many times the whole set was traversed - 1 (if set was sorted, returns 0).
1541 */
1542static int
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001543set_sort(struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001544{
1545 uint32_t i, j;
1546 int ret = 0, cmp, inverted, change;
1547 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001548 struct lyxp_set_node item;
1549 struct lyxp_set_hash_node hnode;
1550 uint64_t hash;
1551
Michal Vasko3cf8fbf2020-05-27 15:21:21 +02001552 if ((set->type != LYXP_SET_NODE_SET) || (set->used < 2)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001553 return 0;
1554 }
1555
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001556 /* find first top-level node to be used as anchor for positions */
1557 for (root = set->ctx_node; root->parent; root = (const struct lyd_node *)root->parent);
1558 for (; root->prev->next; root = root->prev);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001559
1560 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001561 if (set_assign_pos(set, root, set->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001562 return -1;
1563 }
1564
1565 LOGDBG(LY_LDGXPATH, "SORT BEGIN");
1566 print_set_debug(set);
1567
1568 for (i = 0; i < set->used; ++i) {
1569 inverted = 0;
1570 change = 0;
1571
1572 for (j = 1; j < set->used - i; ++j) {
1573 /* compare node positions */
1574 if (inverted) {
1575 cmp = set_sort_compare(&set->val.nodes[j], &set->val.nodes[j - 1]);
1576 } else {
1577 cmp = set_sort_compare(&set->val.nodes[j - 1], &set->val.nodes[j]);
1578 }
1579
1580 /* swap if needed */
1581 if ((inverted && (cmp < 0)) || (!inverted && (cmp > 0))) {
1582 change = 1;
1583
1584 item = set->val.nodes[j - 1];
1585 set->val.nodes[j - 1] = set->val.nodes[j];
1586 set->val.nodes[j] = item;
1587 } else {
1588 /* whether node_pos1 should be smaller than node_pos2 or the other way around */
1589 inverted = !inverted;
1590 }
1591 }
1592
1593 ++ret;
1594
1595 if (!change) {
1596 break;
1597 }
1598 }
1599
1600 LOGDBG(LY_LDGXPATH, "SORT END %d", ret);
1601 print_set_debug(set);
1602
1603 /* check node hashes */
1604 if (set->used >= LYD_HT_MIN_ITEMS) {
1605 assert(set->ht);
1606 for (i = 0; i < set->used; ++i) {
1607 hnode.node = set->val.nodes[i].node;
1608 hnode.type = set->val.nodes[i].type;
1609
1610 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
1611 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
1612 hash = dict_hash_multi(hash, NULL, 0);
1613
1614 assert(!lyht_find(set->ht, &hnode, hash, NULL));
1615 }
1616 }
1617
1618 return ret - 1;
1619}
1620
1621/**
1622 * @brief Remove duplicate entries in a sorted node set.
1623 *
1624 * @param[in] set Sorted set to check.
1625 * @return LY_ERR (LY_EEXIST if some duplicates are found)
1626 */
1627static LY_ERR
1628set_sorted_dup_node_clean(struct lyxp_set *set)
1629{
1630 uint32_t i = 0;
1631 LY_ERR ret = LY_SUCCESS;
1632
1633 if (set->used > 1) {
1634 while (i < set->used - 1) {
1635 if ((set->val.nodes[i].node == set->val.nodes[i + 1].node)
1636 && (set->val.nodes[i].type == set->val.nodes[i + 1].type)) {
Michal Vasko2caefc12019-11-14 16:07:56 +01001637 set_remove_node_none(set, i + 1);
Michal Vasko57eab132019-09-24 11:46:26 +02001638 ret = LY_EEXIST;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001639 }
Michal Vasko57eab132019-09-24 11:46:26 +02001640 ++i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001641 }
1642 }
1643
Michal Vasko2caefc12019-11-14 16:07:56 +01001644 set_remove_nodes_none(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001645 return ret;
1646}
1647
1648#endif
1649
1650/**
1651 * @brief Merge 2 sorted sets into one.
1652 *
1653 * @param[in,out] trg Set to merge into. Duplicates are removed.
1654 * @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 +02001655 * @return LY_ERR
1656 */
1657static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001658set_sorted_merge(struct lyxp_set *trg, struct lyxp_set *src)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001659{
1660 uint32_t i, j, k, count, dup_count;
1661 int cmp;
1662 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001663
Michal Vaskod3678892020-05-21 10:06:58 +02001664 if ((trg->type != LYXP_SET_NODE_SET) || (src->type != LYXP_SET_NODE_SET)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001665 return LY_EINVAL;
1666 }
1667
Michal Vaskod3678892020-05-21 10:06:58 +02001668 if (!src->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001669 return LY_SUCCESS;
Michal Vaskod3678892020-05-21 10:06:58 +02001670 } else if (!trg->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001671 set_fill_set(trg, src);
Michal Vaskod3678892020-05-21 10:06:58 +02001672 lyxp_set_free_content(src);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001673 return LY_SUCCESS;
1674 }
1675
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001676 /* find first top-level node to be used as anchor for positions */
1677 for (root = trg->ctx_node; root->parent; root = (const struct lyd_node *)root->parent);
1678 for (; root->prev->next; root = root->prev);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001679
1680 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001681 if (set_assign_pos(trg, root, trg->root_type) || set_assign_pos(src, root, src->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001682 return LY_EINT;
1683 }
1684
1685#ifndef NDEBUG
1686 LOGDBG(LY_LDGXPATH, "MERGE target");
1687 print_set_debug(trg);
1688 LOGDBG(LY_LDGXPATH, "MERGE source");
1689 print_set_debug(src);
1690#endif
1691
1692 /* make memory for the merge (duplicates are not detected yet, so space
1693 * will likely be wasted on them, too bad) */
1694 if (trg->size - trg->used < src->used) {
1695 trg->size = trg->used + src->used;
1696
1697 trg->val.nodes = ly_realloc(trg->val.nodes, trg->size * sizeof *trg->val.nodes);
1698 LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx), LY_EMEM);
1699 }
1700
1701 i = 0;
1702 j = 0;
1703 count = 0;
1704 dup_count = 0;
1705 do {
1706 cmp = set_sort_compare(&src->val.nodes[i], &trg->val.nodes[j]);
1707 if (!cmp) {
1708 if (!count) {
1709 /* duplicate, just skip it */
1710 ++i;
1711 ++j;
1712 } else {
1713 /* we are copying something already, so let's copy the duplicate too,
1714 * we are hoping that afterwards there are some more nodes to
1715 * copy and this way we can copy them all together */
1716 ++count;
1717 ++dup_count;
1718 ++i;
1719 ++j;
1720 }
1721 } else if (cmp < 0) {
1722 /* inserting src node into trg, just remember it for now */
1723 ++count;
1724 ++i;
1725
1726 /* insert the hash now */
1727 set_insert_node_hash(trg, src->val.nodes[i - 1].node, src->val.nodes[i - 1].type);
1728 } else if (count) {
1729copy_nodes:
1730 /* time to actually copy the nodes, we have found the largest block of nodes */
1731 memmove(&trg->val.nodes[j + (count - dup_count)],
1732 &trg->val.nodes[j],
1733 (trg->used - j) * sizeof *trg->val.nodes);
1734 memcpy(&trg->val.nodes[j - dup_count], &src->val.nodes[i - count], count * sizeof *src->val.nodes);
1735
1736 trg->used += count - dup_count;
1737 /* do not change i, except the copying above, we are basically doing exactly what is in the else branch below */
1738 j += count - dup_count;
1739
1740 count = 0;
1741 dup_count = 0;
1742 } else {
1743 ++j;
1744 }
1745 } while ((i < src->used) && (j < trg->used));
1746
1747 if ((i < src->used) || count) {
1748 /* insert all the hashes first */
1749 for (k = i; k < src->used; ++k) {
1750 set_insert_node_hash(trg, src->val.nodes[k].node, src->val.nodes[k].type);
1751 }
1752
1753 /* loop ended, but we need to copy something at trg end */
1754 count += src->used - i;
1755 i = src->used;
1756 goto copy_nodes;
1757 }
1758
1759 /* we are inserting hashes before the actual node insert, which causes
1760 * situations when there were initially not enough items for a hash table,
1761 * but even after some were inserted, hash table was not created (during
1762 * insertion the number of items is not updated yet) */
1763 if (!trg->ht && (trg->used >= LYD_HT_MIN_ITEMS)) {
1764 set_insert_node_hash(trg, NULL, 0);
1765 }
1766
1767#ifndef NDEBUG
1768 LOGDBG(LY_LDGXPATH, "MERGE result");
1769 print_set_debug(trg);
1770#endif
1771
Michal Vaskod3678892020-05-21 10:06:58 +02001772 lyxp_set_free_content(src);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001773 return LY_SUCCESS;
1774}
1775
1776/*
1777 * (re)parse functions
1778 *
1779 * Parse functions parse the expression into
1780 * tokens (syntactic analysis).
1781 *
1782 * Reparse functions perform semantic analysis
1783 * (do not save the result, just a check) of
1784 * the expression and fill repeat indices.
1785 */
1786
Michal Vasko14676352020-05-29 11:35:55 +02001787LY_ERR
Michal Vasko24cddf82020-06-01 08:17:01 +02001788lyxp_check_token(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t exp_idx, enum lyxp_token want_tok)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001789{
1790 if (exp->used == exp_idx) {
Michal Vasko14676352020-05-29 11:35:55 +02001791 if (ctx) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001792 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOF);
1793 }
Michal Vasko14676352020-05-29 11:35:55 +02001794 return LY_EINCOMPLETE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001795 }
1796
1797 if (want_tok && (exp->tokens[exp_idx] != want_tok)) {
Michal Vasko14676352020-05-29 11:35:55 +02001798 if (ctx) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001799 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK,
Michal Vasko24cddf82020-06-01 08:17:01 +02001800 lyxp_print_token(exp->tokens[exp_idx]), &exp->expr[exp->tok_pos[exp_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001801 }
Michal Vasko14676352020-05-29 11:35:55 +02001802 return LY_ENOT;
1803 }
1804
1805 return LY_SUCCESS;
1806}
1807
1808/* just like lyxp_check_token() but tests for 2 tokens */
1809static LY_ERR
1810exp_check_token2(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t exp_idx, enum lyxp_token want_tok1,
1811 enum lyxp_token want_tok2)
1812{
1813 if (exp->used == exp_idx) {
1814 if (ctx) {
1815 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOF);
1816 }
1817 return LY_EINCOMPLETE;
1818 }
1819
1820 if ((exp->tokens[exp_idx] != want_tok1) && (exp->tokens[exp_idx] != want_tok2)) {
1821 if (ctx) {
1822 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK,
Michal Vasko24cddf82020-06-01 08:17:01 +02001823 lyxp_print_token(exp->tokens[exp_idx]), &exp->expr[exp->tok_pos[exp_idx]]);
Michal Vasko14676352020-05-29 11:35:55 +02001824 }
1825 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001826 }
1827
1828 return LY_SUCCESS;
1829}
1830
1831/**
1832 * @brief Stack operation push on the repeat array.
1833 *
1834 * @param[in] exp Expression to use.
1835 * @param[in] exp_idx Position in the expresion \p exp.
1836 * @param[in] repeat_op_idx Index from \p exp of the operator token. This value is pushed.
1837 */
1838static void
1839exp_repeat_push(struct lyxp_expr *exp, uint16_t exp_idx, uint16_t repeat_op_idx)
1840{
1841 uint16_t i;
1842
1843 if (exp->repeat[exp_idx]) {
1844 for (i = 0; exp->repeat[exp_idx][i]; ++i);
1845 exp->repeat[exp_idx] = realloc(exp->repeat[exp_idx], (i + 2) * sizeof *exp->repeat[exp_idx]);
1846 LY_CHECK_ERR_RET(!exp->repeat[exp_idx], LOGMEM(NULL), );
1847 exp->repeat[exp_idx][i] = repeat_op_idx;
1848 exp->repeat[exp_idx][i + 1] = 0;
1849 } else {
1850 exp->repeat[exp_idx] = calloc(2, sizeof *exp->repeat[exp_idx]);
1851 LY_CHECK_ERR_RET(!exp->repeat[exp_idx], LOGMEM(NULL), );
1852 exp->repeat[exp_idx][0] = repeat_op_idx;
1853 }
1854}
1855
1856/**
1857 * @brief Reparse Predicate. Logs directly on error.
1858 *
1859 * [7] Predicate ::= '[' Expr ']'
1860 *
1861 * @param[in] ctx Context for logging.
1862 * @param[in] exp Parsed XPath expression.
1863 * @param[in] exp_idx Position in the expression @p exp.
1864 * @return LY_ERR
1865 */
1866static LY_ERR
Michal Vasko14676352020-05-29 11:35:55 +02001867reparse_predicate(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *exp_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001868{
1869 LY_ERR rc;
1870
Michal Vasko14676352020-05-29 11:35:55 +02001871 rc = lyxp_check_token(ctx, exp, *exp_idx, LYXP_TOKEN_BRACK1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001872 LY_CHECK_RET(rc);
1873 ++(*exp_idx);
1874
1875 rc = reparse_or_expr(ctx, exp, exp_idx);
1876 LY_CHECK_RET(rc);
1877
Michal Vasko14676352020-05-29 11:35:55 +02001878 rc = lyxp_check_token(ctx, exp, *exp_idx, LYXP_TOKEN_BRACK2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001879 LY_CHECK_RET(rc);
1880 ++(*exp_idx);
1881
1882 return LY_SUCCESS;
1883}
1884
1885/**
1886 * @brief Reparse RelativeLocationPath. Logs directly on error.
1887 *
1888 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
1889 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
1890 * [6] NodeTest ::= NameTest | NodeType '(' ')'
1891 *
1892 * @param[in] ctx Context for logging.
1893 * @param[in] exp Parsed XPath expression.
1894 * @param[in] exp_idx Position in the expression \p exp.
1895 * @return LY_ERR (LY_EINCOMPLETE on forward reference)
1896 */
1897static LY_ERR
Michal Vasko14676352020-05-29 11:35:55 +02001898reparse_relative_location_path(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *exp_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001899{
1900 LY_ERR rc;
1901
Michal Vasko14676352020-05-29 11:35:55 +02001902 rc = lyxp_check_token(ctx, exp, *exp_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001903 LY_CHECK_RET(rc);
1904
1905 goto step;
1906 do {
1907 /* '/' or '//' */
1908 ++(*exp_idx);
1909
Michal Vasko14676352020-05-29 11:35:55 +02001910 rc = lyxp_check_token(ctx, exp, *exp_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001911 LY_CHECK_RET(rc);
1912step:
1913 /* Step */
1914 switch (exp->tokens[*exp_idx]) {
1915 case LYXP_TOKEN_DOT:
1916 ++(*exp_idx);
1917 break;
1918
1919 case LYXP_TOKEN_DDOT:
1920 ++(*exp_idx);
1921 break;
1922
1923 case LYXP_TOKEN_AT:
1924 ++(*exp_idx);
1925
Michal Vasko14676352020-05-29 11:35:55 +02001926 rc = lyxp_check_token(ctx, exp, *exp_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001927 LY_CHECK_RET(rc);
1928 if ((exp->tokens[*exp_idx] != LYXP_TOKEN_NAMETEST) && (exp->tokens[*exp_idx] != LYXP_TOKEN_NODETYPE)) {
1929 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK,
Michal Vasko24cddf82020-06-01 08:17:01 +02001930 lyxp_print_token(exp->tokens[*exp_idx]), &exp->expr[exp->tok_pos[*exp_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001931 return LY_EVALID;
1932 }
1933 /* fall through */
1934 case LYXP_TOKEN_NAMETEST:
1935 ++(*exp_idx);
1936 goto reparse_predicate;
1937 break;
1938
1939 case LYXP_TOKEN_NODETYPE:
1940 ++(*exp_idx);
1941
1942 /* '(' */
Michal Vasko14676352020-05-29 11:35:55 +02001943 rc = lyxp_check_token(ctx, exp, *exp_idx, LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001944 LY_CHECK_RET(rc);
1945 ++(*exp_idx);
1946
1947 /* ')' */
Michal Vasko14676352020-05-29 11:35:55 +02001948 rc = lyxp_check_token(ctx, exp, *exp_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001949 LY_CHECK_RET(rc);
1950 ++(*exp_idx);
1951
1952reparse_predicate:
1953 /* Predicate* */
Michal Vasko14676352020-05-29 11:35:55 +02001954 while (!lyxp_check_token(NULL, exp, *exp_idx, LYXP_TOKEN_BRACK1)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001955 rc = reparse_predicate(ctx, exp, exp_idx);
1956 LY_CHECK_RET(rc);
1957 }
1958 break;
1959 default:
1960 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK,
Michal Vasko24cddf82020-06-01 08:17:01 +02001961 lyxp_print_token(exp->tokens[*exp_idx]), &exp->expr[exp->tok_pos[*exp_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001962 return LY_EVALID;
1963 }
Michal Vasko3e48bf32020-06-01 08:39:07 +02001964 } while (!exp_check_token2(NULL, exp, *exp_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001965
1966 return LY_SUCCESS;
1967}
1968
1969/**
1970 * @brief Reparse AbsoluteLocationPath. Logs directly on error.
1971 *
1972 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
1973 *
1974 * @param[in] ctx Context for logging.
1975 * @param[in] exp Parsed XPath expression.
1976 * @param[in] exp_idx Position in the expression \p exp.
1977 * @return LY_ERR
1978 */
1979static LY_ERR
Michal Vasko14676352020-05-29 11:35:55 +02001980reparse_absolute_location_path(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *exp_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001981{
1982 LY_ERR rc;
1983
Michal Vasko3e48bf32020-06-01 08:39:07 +02001984 rc = exp_check_token2(ctx, exp, *exp_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001985 LY_CHECK_RET(rc);
1986
1987 /* '/' RelativeLocationPath? */
Michal Vasko3e48bf32020-06-01 08:39:07 +02001988 if (exp->tokens[*exp_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001989 /* '/' */
1990 ++(*exp_idx);
1991
Michal Vasko14676352020-05-29 11:35:55 +02001992 if (lyxp_check_token(NULL, exp, *exp_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001993 return LY_SUCCESS;
1994 }
1995 switch (exp->tokens[*exp_idx]) {
1996 case LYXP_TOKEN_DOT:
1997 case LYXP_TOKEN_DDOT:
1998 case LYXP_TOKEN_AT:
1999 case LYXP_TOKEN_NAMETEST:
2000 case LYXP_TOKEN_NODETYPE:
2001 rc = reparse_relative_location_path(ctx, exp, exp_idx);
2002 LY_CHECK_RET(rc);
2003 /* fall through */
2004 default:
2005 break;
2006 }
2007
2008 /* '//' RelativeLocationPath */
2009 } else {
2010 /* '//' */
2011 ++(*exp_idx);
2012
2013 rc = reparse_relative_location_path(ctx, exp, exp_idx);
2014 LY_CHECK_RET(rc);
2015 }
2016
2017 return LY_SUCCESS;
2018}
2019
2020/**
2021 * @brief Reparse FunctionCall. Logs directly on error.
2022 *
2023 * [9] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
2024 *
2025 * @param[in] ctx Context for logging.
2026 * @param[in] exp Parsed XPath expression.
2027 * @param[in] exp_idx Position in the expression @p exp.
2028 * @return LY_ERR
2029 */
2030static LY_ERR
Michal Vasko14676352020-05-29 11:35:55 +02002031reparse_function_call(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *exp_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002032{
2033 int min_arg_count = -1, max_arg_count, arg_count;
2034 uint16_t func_exp_idx;
2035 LY_ERR rc;
2036
Michal Vasko14676352020-05-29 11:35:55 +02002037 rc = lyxp_check_token(ctx, exp, *exp_idx, LYXP_TOKEN_FUNCNAME);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002038 LY_CHECK_RET(rc);
2039 func_exp_idx = *exp_idx;
2040 switch (exp->tok_len[*exp_idx]) {
2041 case 3:
2042 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "not", 3)) {
2043 min_arg_count = 1;
2044 max_arg_count = 1;
2045 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "sum", 3)) {
2046 min_arg_count = 1;
2047 max_arg_count = 1;
2048 }
2049 break;
2050 case 4:
2051 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "lang", 4)) {
2052 min_arg_count = 1;
2053 max_arg_count = 1;
2054 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "last", 4)) {
2055 min_arg_count = 0;
2056 max_arg_count = 0;
2057 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "name", 4)) {
2058 min_arg_count = 0;
2059 max_arg_count = 1;
2060 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "true", 4)) {
2061 min_arg_count = 0;
2062 max_arg_count = 0;
2063 }
2064 break;
2065 case 5:
2066 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "count", 5)) {
2067 min_arg_count = 1;
2068 max_arg_count = 1;
2069 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "false", 5)) {
2070 min_arg_count = 0;
2071 max_arg_count = 0;
2072 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "floor", 5)) {
2073 min_arg_count = 1;
2074 max_arg_count = 1;
2075 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "round", 5)) {
2076 min_arg_count = 1;
2077 max_arg_count = 1;
2078 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "deref", 5)) {
2079 min_arg_count = 1;
2080 max_arg_count = 1;
2081 }
2082 break;
2083 case 6:
2084 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "concat", 6)) {
2085 min_arg_count = 2;
Michal Vaskobe2e3562019-10-15 15:35:35 +02002086 max_arg_count = INT_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002087 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "number", 6)) {
2088 min_arg_count = 0;
2089 max_arg_count = 1;
2090 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "string", 6)) {
2091 min_arg_count = 0;
2092 max_arg_count = 1;
2093 }
2094 break;
2095 case 7:
2096 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "boolean", 7)) {
2097 min_arg_count = 1;
2098 max_arg_count = 1;
2099 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "ceiling", 7)) {
2100 min_arg_count = 1;
2101 max_arg_count = 1;
2102 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "current", 7)) {
2103 min_arg_count = 0;
2104 max_arg_count = 0;
2105 }
2106 break;
2107 case 8:
2108 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "contains", 8)) {
2109 min_arg_count = 2;
2110 max_arg_count = 2;
2111 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "position", 8)) {
2112 min_arg_count = 0;
2113 max_arg_count = 0;
2114 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "re-match", 8)) {
2115 min_arg_count = 2;
2116 max_arg_count = 2;
2117 }
2118 break;
2119 case 9:
2120 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "substring", 9)) {
2121 min_arg_count = 2;
2122 max_arg_count = 3;
2123 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "translate", 9)) {
2124 min_arg_count = 3;
2125 max_arg_count = 3;
2126 }
2127 break;
2128 case 10:
2129 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "local-name", 10)) {
2130 min_arg_count = 0;
2131 max_arg_count = 1;
2132 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "enum-value", 10)) {
2133 min_arg_count = 1;
2134 max_arg_count = 1;
2135 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "bit-is-set", 10)) {
2136 min_arg_count = 2;
2137 max_arg_count = 2;
2138 }
2139 break;
2140 case 11:
2141 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "starts-with", 11)) {
2142 min_arg_count = 2;
2143 max_arg_count = 2;
2144 }
2145 break;
2146 case 12:
2147 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "derived-from", 12)) {
2148 min_arg_count = 2;
2149 max_arg_count = 2;
2150 }
2151 break;
2152 case 13:
2153 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "namespace-uri", 13)) {
2154 min_arg_count = 0;
2155 max_arg_count = 1;
2156 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "string-length", 13)) {
2157 min_arg_count = 0;
2158 max_arg_count = 1;
2159 }
2160 break;
2161 case 15:
2162 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "normalize-space", 15)) {
2163 min_arg_count = 0;
2164 max_arg_count = 1;
2165 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "substring-after", 15)) {
2166 min_arg_count = 2;
2167 max_arg_count = 2;
2168 }
2169 break;
2170 case 16:
2171 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "substring-before", 16)) {
2172 min_arg_count = 2;
2173 max_arg_count = 2;
2174 }
2175 break;
2176 case 20:
2177 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "derived-from-or-self", 20)) {
2178 min_arg_count = 2;
2179 max_arg_count = 2;
2180 }
2181 break;
2182 }
2183 if (min_arg_count == -1) {
2184 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INFUNC, exp->tok_len[*exp_idx], &exp->expr[exp->tok_pos[*exp_idx]]);
2185 return LY_EINVAL;
2186 }
2187 ++(*exp_idx);
2188
2189 /* '(' */
Michal Vasko14676352020-05-29 11:35:55 +02002190 rc = lyxp_check_token(ctx, exp, *exp_idx, LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002191 LY_CHECK_RET(rc);
2192 ++(*exp_idx);
2193
2194 /* ( Expr ( ',' Expr )* )? */
2195 arg_count = 0;
Michal Vasko14676352020-05-29 11:35:55 +02002196 rc = lyxp_check_token(ctx, exp, *exp_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002197 LY_CHECK_RET(rc);
2198 if (exp->tokens[*exp_idx] != LYXP_TOKEN_PAR2) {
2199 ++arg_count;
2200 rc = reparse_or_expr(ctx, exp, exp_idx);
2201 LY_CHECK_RET(rc);
2202 }
Michal Vasko14676352020-05-29 11:35:55 +02002203 while (!lyxp_check_token(NULL, exp, *exp_idx, LYXP_TOKEN_COMMA)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002204 ++(*exp_idx);
2205
2206 ++arg_count;
2207 rc = reparse_or_expr(ctx, exp, exp_idx);
2208 LY_CHECK_RET(rc);
2209 }
2210
2211 /* ')' */
Michal Vasko14676352020-05-29 11:35:55 +02002212 rc = lyxp_check_token(ctx, exp, *exp_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002213 LY_CHECK_RET(rc);
2214 ++(*exp_idx);
2215
2216 if ((arg_count < min_arg_count) || (arg_count > max_arg_count)) {
2217 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INARGCOUNT, arg_count, exp->tok_len[func_exp_idx],
2218 &exp->expr[exp->tok_pos[func_exp_idx]]);
2219 return LY_EVALID;
2220 }
2221
2222 return LY_SUCCESS;
2223}
2224
2225/**
2226 * @brief Reparse PathExpr. Logs directly on error.
2227 *
2228 * [10] PathExpr ::= LocationPath | PrimaryExpr Predicate*
2229 * | PrimaryExpr Predicate* '/' RelativeLocationPath
2230 * | PrimaryExpr Predicate* '//' RelativeLocationPath
2231 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
2232 * [8] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall
2233 *
2234 * @param[in] ctx Context for logging.
2235 * @param[in] exp Parsed XPath expression.
2236 * @param[in] exp_idx Position in the expression @p exp.
2237 * @return LY_ERR
2238 */
2239static LY_ERR
Michal Vasko14676352020-05-29 11:35:55 +02002240reparse_path_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *exp_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002241{
2242 LY_ERR rc;
2243
Michal Vasko14676352020-05-29 11:35:55 +02002244 if (lyxp_check_token(ctx, exp, *exp_idx, LYXP_TOKEN_NONE)) {
2245 return LY_EVALID;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002246 }
2247
2248 switch (exp->tokens[*exp_idx]) {
2249 case LYXP_TOKEN_PAR1:
2250 /* '(' Expr ')' Predicate* */
2251 ++(*exp_idx);
2252
2253 rc = reparse_or_expr(ctx, exp, exp_idx);
2254 LY_CHECK_RET(rc);
2255
Michal Vasko14676352020-05-29 11:35:55 +02002256 rc = lyxp_check_token(ctx, exp, *exp_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002257 LY_CHECK_RET(rc);
2258 ++(*exp_idx);
2259 goto predicate;
2260 break;
2261 case LYXP_TOKEN_DOT:
2262 case LYXP_TOKEN_DDOT:
2263 case LYXP_TOKEN_AT:
2264 case LYXP_TOKEN_NAMETEST:
2265 case LYXP_TOKEN_NODETYPE:
2266 /* RelativeLocationPath */
2267 rc = reparse_relative_location_path(ctx, exp, exp_idx);
2268 LY_CHECK_RET(rc);
2269 break;
2270 case LYXP_TOKEN_FUNCNAME:
2271 /* FunctionCall */
2272 rc = reparse_function_call(ctx, exp, exp_idx);
2273 LY_CHECK_RET(rc);
2274 goto predicate;
2275 break;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002276 case LYXP_TOKEN_OPER_PATH:
2277 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02002278 /* AbsoluteLocationPath */
2279 rc = reparse_absolute_location_path(ctx, exp, exp_idx);
2280 LY_CHECK_RET(rc);
2281 break;
2282 case LYXP_TOKEN_LITERAL:
2283 /* Literal */
2284 ++(*exp_idx);
2285 goto predicate;
2286 break;
2287 case LYXP_TOKEN_NUMBER:
2288 /* Number */
2289 ++(*exp_idx);
2290 goto predicate;
2291 break;
2292 default:
2293 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK,
Michal Vasko24cddf82020-06-01 08:17:01 +02002294 lyxp_print_token(exp->tokens[*exp_idx]), &exp->expr[exp->tok_pos[*exp_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002295 return LY_EVALID;
2296 }
2297
2298 return LY_SUCCESS;
2299
2300predicate:
2301 /* Predicate* */
Michal Vasko14676352020-05-29 11:35:55 +02002302 while (!lyxp_check_token(NULL, exp, *exp_idx, LYXP_TOKEN_BRACK1)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002303 rc = reparse_predicate(ctx, exp, exp_idx);
2304 LY_CHECK_RET(rc);
2305 }
2306
2307 /* ('/' or '//') RelativeLocationPath */
Michal Vasko3e48bf32020-06-01 08:39:07 +02002308 if (!exp_check_token2(NULL, exp, *exp_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002309
2310 /* '/' or '//' */
2311 ++(*exp_idx);
2312
2313 rc = reparse_relative_location_path(ctx, exp, exp_idx);
2314 LY_CHECK_RET(rc);
2315 }
2316
2317 return LY_SUCCESS;
2318}
2319
2320/**
2321 * @brief Reparse UnaryExpr. Logs directly on error.
2322 *
2323 * [17] UnaryExpr ::= UnionExpr | '-' UnaryExpr
2324 * [18] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
2325 *
2326 * @param[in] ctx Context for logging.
2327 * @param[in] exp Parsed XPath expression.
2328 * @param[in] exp_idx Position in the expression @p exp.
2329 * @return LY_ERR
2330 */
2331static LY_ERR
Michal Vasko14676352020-05-29 11:35:55 +02002332reparse_unary_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *exp_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002333{
2334 uint16_t prev_exp;
2335 LY_ERR rc;
2336
2337 /* ('-')* */
2338 prev_exp = *exp_idx;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002339 while (!lyxp_check_token(NULL, exp, *exp_idx, LYXP_TOKEN_OPER_MATH)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002340 && (exp->expr[exp->tok_pos[*exp_idx]] == '-')) {
2341 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNARY);
2342 ++(*exp_idx);
2343 }
2344
2345 /* PathExpr */
2346 prev_exp = *exp_idx;
2347 rc = reparse_path_expr(ctx, exp, exp_idx);
2348 LY_CHECK_RET(rc);
2349
2350 /* ('|' PathExpr)* */
Michal Vasko3e48bf32020-06-01 08:39:07 +02002351 while (!lyxp_check_token(NULL, exp, *exp_idx, LYXP_TOKEN_OPER_UNI)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002352 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNION);
2353 ++(*exp_idx);
2354
2355 rc = reparse_path_expr(ctx, exp, exp_idx);
2356 LY_CHECK_RET(rc);
2357 }
2358
2359 return LY_SUCCESS;
2360}
2361
2362/**
2363 * @brief Reparse AdditiveExpr. Logs directly on error.
2364 *
2365 * [15] AdditiveExpr ::= MultiplicativeExpr
2366 * | AdditiveExpr '+' MultiplicativeExpr
2367 * | AdditiveExpr '-' MultiplicativeExpr
2368 * [16] MultiplicativeExpr ::= UnaryExpr
2369 * | MultiplicativeExpr '*' UnaryExpr
2370 * | MultiplicativeExpr 'div' UnaryExpr
2371 * | MultiplicativeExpr 'mod' UnaryExpr
2372 *
2373 * @param[in] ctx Context for logging.
2374 * @param[in] exp Parsed XPath expression.
2375 * @param[in] exp_idx Position in the expression @p exp.
2376 * @return LY_ERR
2377 */
2378static LY_ERR
Michal Vasko14676352020-05-29 11:35:55 +02002379reparse_additive_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *exp_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002380{
2381 uint16_t prev_add_exp, prev_mul_exp;
2382 LY_ERR rc;
2383
2384 prev_add_exp = *exp_idx;
2385 goto reparse_multiplicative_expr;
2386
2387 /* ('+' / '-' MultiplicativeExpr)* */
Michal Vasko3e48bf32020-06-01 08:39:07 +02002388 while (!lyxp_check_token(NULL, exp, *exp_idx, LYXP_TOKEN_OPER_MATH)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002389 && ((exp->expr[exp->tok_pos[*exp_idx]] == '+') || (exp->expr[exp->tok_pos[*exp_idx]] == '-'))) {
2390 exp_repeat_push(exp, prev_add_exp, LYXP_EXPR_ADDITIVE);
2391 ++(*exp_idx);
2392
2393reparse_multiplicative_expr:
2394 /* UnaryExpr */
2395 prev_mul_exp = *exp_idx;
2396 rc = reparse_unary_expr(ctx, exp, exp_idx);
2397 LY_CHECK_RET(rc);
2398
2399 /* ('*' / 'div' / 'mod' UnaryExpr)* */
Michal Vasko3e48bf32020-06-01 08:39:07 +02002400 while (!lyxp_check_token(NULL, exp, *exp_idx, LYXP_TOKEN_OPER_MATH)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002401 && ((exp->expr[exp->tok_pos[*exp_idx]] == '*') || (exp->tok_len[*exp_idx] == 3))) {
2402 exp_repeat_push(exp, prev_mul_exp, LYXP_EXPR_MULTIPLICATIVE);
2403 ++(*exp_idx);
2404
2405 rc = reparse_unary_expr(ctx, exp, exp_idx);
2406 LY_CHECK_RET(rc);
2407 }
2408 }
2409
2410 return LY_SUCCESS;
2411}
2412
2413/**
2414 * @brief Reparse EqualityExpr. Logs directly on error.
2415 *
2416 * [13] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
2417 * | EqualityExpr '!=' RelationalExpr
2418 * [14] RelationalExpr ::= AdditiveExpr
2419 * | RelationalExpr '<' AdditiveExpr
2420 * | RelationalExpr '>' AdditiveExpr
2421 * | RelationalExpr '<=' AdditiveExpr
2422 * | RelationalExpr '>=' AdditiveExpr
2423 *
2424 * @param[in] ctx Context for logging.
2425 * @param[in] exp Parsed XPath expression.
2426 * @param[in] exp_idx Position in the expression @p exp.
2427 * @return LY_ERR
2428 */
2429static LY_ERR
Michal Vasko14676352020-05-29 11:35:55 +02002430reparse_equality_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *exp_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002431{
2432 uint16_t prev_eq_exp, prev_rel_exp;
2433 LY_ERR rc;
2434
2435 prev_eq_exp = *exp_idx;
2436 goto reparse_additive_expr;
2437
2438 /* ('=' / '!=' RelationalExpr)* */
Michal Vasko3e48bf32020-06-01 08:39:07 +02002439 while (!exp_check_token2(NULL, exp, *exp_idx, LYXP_TOKEN_OPER_EQUAL, LYXP_TOKEN_OPER_NEQUAL)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002440 exp_repeat_push(exp, prev_eq_exp, LYXP_EXPR_EQUALITY);
2441 ++(*exp_idx);
2442
2443reparse_additive_expr:
2444 /* AdditiveExpr */
2445 prev_rel_exp = *exp_idx;
2446 rc = reparse_additive_expr(ctx, exp, exp_idx);
2447 LY_CHECK_RET(rc);
2448
2449 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
Michal Vasko3e48bf32020-06-01 08:39:07 +02002450 while (!lyxp_check_token(NULL, exp, *exp_idx, LYXP_TOKEN_OPER_COMP)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002451 exp_repeat_push(exp, prev_rel_exp, LYXP_EXPR_RELATIONAL);
2452 ++(*exp_idx);
2453
2454 rc = reparse_additive_expr(ctx, exp, exp_idx);
2455 LY_CHECK_RET(rc);
2456 }
2457 }
2458
2459 return LY_SUCCESS;
2460}
2461
2462/**
2463 * @brief Reparse OrExpr. Logs directly on error.
2464 *
2465 * [11] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
2466 * [12] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
2467 *
2468 * @param[in] ctx Context for logging.
2469 * @param[in] exp Parsed XPath expression.
2470 * @param[in] exp_idx Position in the expression @p exp.
2471 * @return LY_ERR
2472 */
2473static LY_ERR
Michal Vasko14676352020-05-29 11:35:55 +02002474reparse_or_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *exp_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002475{
2476 uint16_t prev_or_exp, prev_and_exp;
2477 LY_ERR rc;
2478
2479 prev_or_exp = *exp_idx;
2480 goto reparse_equality_expr;
2481
2482 /* ('or' AndExpr)* */
Michal Vasko3e48bf32020-06-01 08:39:07 +02002483 while (!lyxp_check_token(NULL, exp, *exp_idx, LYXP_TOKEN_OPER_LOG) && (exp->tok_len[*exp_idx] == 2)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002484 exp_repeat_push(exp, prev_or_exp, LYXP_EXPR_OR);
2485 ++(*exp_idx);
2486
2487reparse_equality_expr:
2488 /* EqualityExpr */
2489 prev_and_exp = *exp_idx;
2490 rc = reparse_equality_expr(ctx, exp, exp_idx);
2491 LY_CHECK_RET(rc);
2492
2493 /* ('and' EqualityExpr)* */
Michal Vasko3e48bf32020-06-01 08:39:07 +02002494 while (!lyxp_check_token(NULL, exp, *exp_idx, LYXP_TOKEN_OPER_LOG) && (exp->tok_len[*exp_idx] == 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002495 exp_repeat_push(exp, prev_and_exp, LYXP_EXPR_AND);
2496 ++(*exp_idx);
2497
2498 rc = reparse_equality_expr(ctx, exp, exp_idx);
2499 LY_CHECK_RET(rc);
2500 }
2501 }
2502
2503 return LY_SUCCESS;
2504}
Radek Krejcib1646a92018-11-02 16:08:26 +01002505
2506/**
2507 * @brief Parse NCName.
2508 *
2509 * @param[in] ncname Name to parse.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002510 * @return Length of @p ncname valid bytes.
Radek Krejcib1646a92018-11-02 16:08:26 +01002511 */
Radek Krejcid4270262019-01-07 15:07:25 +01002512static long int
Radek Krejcib1646a92018-11-02 16:08:26 +01002513parse_ncname(const char *ncname)
2514{
2515 unsigned int uc;
Radek Krejcid4270262019-01-07 15:07:25 +01002516 size_t size;
2517 long int len = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002518
2519 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), 0);
2520 if (!is_xmlqnamestartchar(uc) || (uc == ':')) {
2521 return len;
2522 }
2523
2524 do {
2525 len += size;
Radek Krejci9a564c92019-01-07 14:53:57 +01002526 if (!*ncname) {
2527 break;
2528 }
Radek Krejcid4270262019-01-07 15:07:25 +01002529 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), -len);
Radek Krejcib1646a92018-11-02 16:08:26 +01002530 } while (is_xmlqnamechar(uc) && (uc != ':'));
2531
2532 return len;
2533}
2534
2535/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02002536 * @brief Add @p token into the expression @p exp.
Radek Krejcib1646a92018-11-02 16:08:26 +01002537 *
Michal Vasko03ff5a72019-09-11 13:49:33 +02002538 * @param[in] ctx Context for logging.
Radek Krejcib1646a92018-11-02 16:08:26 +01002539 * @param[in] exp Expression to use.
2540 * @param[in] token Token to add.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002541 * @param[in] tok_pos Token position in the XPath expression.
Radek Krejcib1646a92018-11-02 16:08:26 +01002542 * @param[in] tok_len Token length in the XPath expression.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002543 * @return LY_ERR
Radek Krejcib1646a92018-11-02 16:08:26 +01002544 */
2545static LY_ERR
Michal Vasko14676352020-05-29 11:35:55 +02002546exp_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 +01002547{
2548 uint32_t prev;
2549
2550 if (exp->used == exp->size) {
2551 prev = exp->size;
2552 exp->size += LYXP_EXPR_SIZE_STEP;
2553 if (prev > exp->size) {
2554 LOGINT(ctx);
2555 return LY_EINT;
2556 }
2557
2558 exp->tokens = ly_realloc(exp->tokens, exp->size * sizeof *exp->tokens);
2559 LY_CHECK_ERR_RET(!exp->tokens, LOGMEM(ctx), LY_EMEM);
2560 exp->tok_pos = ly_realloc(exp->tok_pos, exp->size * sizeof *exp->tok_pos);
2561 LY_CHECK_ERR_RET(!exp->tok_pos, LOGMEM(ctx), LY_EMEM);
2562 exp->tok_len = ly_realloc(exp->tok_len, exp->size * sizeof *exp->tok_len);
2563 LY_CHECK_ERR_RET(!exp->tok_len, LOGMEM(ctx), LY_EMEM);
2564 }
2565
2566 exp->tokens[exp->used] = token;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002567 exp->tok_pos[exp->used] = tok_pos;
Radek Krejcib1646a92018-11-02 16:08:26 +01002568 exp->tok_len[exp->used] = tok_len;
2569 ++exp->used;
2570 return LY_SUCCESS;
2571}
2572
2573void
Michal Vasko14676352020-05-29 11:35:55 +02002574lyxp_expr_free(const struct ly_ctx *ctx, struct lyxp_expr *expr)
Radek Krejcib1646a92018-11-02 16:08:26 +01002575{
2576 uint16_t i;
2577
2578 if (!expr) {
2579 return;
2580 }
2581
2582 lydict_remove(ctx, expr->expr);
2583 free(expr->tokens);
2584 free(expr->tok_pos);
2585 free(expr->tok_len);
2586 if (expr->repeat) {
2587 for (i = 0; i < expr->used; ++i) {
2588 free(expr->repeat[i]);
2589 }
2590 }
2591 free(expr->repeat);
2592 free(expr);
2593}
2594
2595struct lyxp_expr *
Michal Vasko14676352020-05-29 11:35:55 +02002596lyxp_expr_parse(const struct ly_ctx *ctx, const char *expr)
Radek Krejcib1646a92018-11-02 16:08:26 +01002597{
2598 struct lyxp_expr *ret;
Radek Krejcid4270262019-01-07 15:07:25 +01002599 size_t parsed = 0, tok_len;
2600 long int ncname_len;
Radek Krejcib1646a92018-11-02 16:08:26 +01002601 enum lyxp_token tok_type;
2602 int prev_function_check = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002603 uint16_t exp_idx = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002604
2605 if (strlen(expr) > UINT16_MAX) {
2606 LOGERR(ctx, LY_EINVAL, "XPath expression cannot be longer than %ud characters.", UINT16_MAX);
2607 return NULL;
2608 }
2609
2610 /* init lyxp_expr structure */
2611 ret = calloc(1, sizeof *ret);
2612 LY_CHECK_ERR_GOTO(!ret, LOGMEM(ctx), error);
2613 ret->expr = lydict_insert(ctx, expr, strlen(expr));
2614 LY_CHECK_ERR_GOTO(!ret->expr, LOGMEM(ctx), error);
2615 ret->used = 0;
2616 ret->size = LYXP_EXPR_SIZE_START;
2617 ret->tokens = malloc(ret->size * sizeof *ret->tokens);
2618 LY_CHECK_ERR_GOTO(!ret->tokens, LOGMEM(ctx), error);
2619
2620 ret->tok_pos = malloc(ret->size * sizeof *ret->tok_pos);
2621 LY_CHECK_ERR_GOTO(!ret->tok_pos, LOGMEM(ctx), error);
2622
2623 ret->tok_len = malloc(ret->size * sizeof *ret->tok_len);
2624 LY_CHECK_ERR_GOTO(!ret->tok_len, LOGMEM(ctx), error);
2625
2626 while (is_xmlws(expr[parsed])) {
2627 ++parsed;
2628 }
2629
2630 do {
2631 if (expr[parsed] == '(') {
2632
2633 /* '(' */
2634 tok_len = 1;
2635 tok_type = LYXP_TOKEN_PAR1;
2636
2637 if (prev_function_check && ret->used && (ret->tokens[ret->used - 1] == LYXP_TOKEN_NAMETEST)) {
2638 /* it is a NodeType/FunctionName after all */
2639 if (((ret->tok_len[ret->used - 1] == 4)
2640 && (!strncmp(&expr[ret->tok_pos[ret->used - 1]], "node", 4)
2641 || !strncmp(&expr[ret->tok_pos[ret->used - 1]], "text", 4))) ||
2642 ((ret->tok_len[ret->used - 1] == 7)
2643 && !strncmp(&expr[ret->tok_pos[ret->used - 1]], "comment", 7))) {
2644 ret->tokens[ret->used - 1] = LYXP_TOKEN_NODETYPE;
2645 } else {
2646 ret->tokens[ret->used - 1] = LYXP_TOKEN_FUNCNAME;
2647 }
2648 prev_function_check = 0;
2649 }
2650
2651 } else if (expr[parsed] == ')') {
2652
2653 /* ')' */
2654 tok_len = 1;
2655 tok_type = LYXP_TOKEN_PAR2;
2656
2657 } else if (expr[parsed] == '[') {
2658
2659 /* '[' */
2660 tok_len = 1;
2661 tok_type = LYXP_TOKEN_BRACK1;
2662
2663 } else if (expr[parsed] == ']') {
2664
2665 /* ']' */
2666 tok_len = 1;
2667 tok_type = LYXP_TOKEN_BRACK2;
2668
2669 } else if (!strncmp(&expr[parsed], "..", 2)) {
2670
2671 /* '..' */
2672 tok_len = 2;
2673 tok_type = LYXP_TOKEN_DDOT;
2674
2675 } else if ((expr[parsed] == '.') && (!isdigit(expr[parsed + 1]))) {
2676
2677 /* '.' */
2678 tok_len = 1;
2679 tok_type = LYXP_TOKEN_DOT;
2680
2681 } else if (expr[parsed] == '@') {
2682
2683 /* '@' */
2684 tok_len = 1;
2685 tok_type = LYXP_TOKEN_AT;
2686
2687 } else if (expr[parsed] == ',') {
2688
2689 /* ',' */
2690 tok_len = 1;
2691 tok_type = LYXP_TOKEN_COMMA;
2692
2693 } else if (expr[parsed] == '\'') {
2694
2695 /* Literal with ' */
2696 for (tok_len = 1; (expr[parsed + tok_len] != '\0') && (expr[parsed + tok_len] != '\''); ++tok_len);
2697 LY_CHECK_ERR_GOTO(expr[parsed + tok_len] == '\0',
2698 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOE, expr[parsed], &expr[parsed]), error);
2699 ++tok_len;
2700 tok_type = LYXP_TOKEN_LITERAL;
2701
2702 } else if (expr[parsed] == '\"') {
2703
2704 /* Literal with " */
2705 for (tok_len = 1; (expr[parsed + tok_len] != '\0') && (expr[parsed + tok_len] != '\"'); ++tok_len);
2706 LY_CHECK_ERR_GOTO(expr[parsed + tok_len] == '\0',
2707 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOE, expr[parsed], &expr[parsed]), error);
2708 ++tok_len;
2709 tok_type = LYXP_TOKEN_LITERAL;
2710
2711 } else if ((expr[parsed] == '.') || (isdigit(expr[parsed]))) {
2712
2713 /* Number */
2714 for (tok_len = 0; isdigit(expr[parsed + tok_len]); ++tok_len);
2715 if (expr[parsed + tok_len] == '.') {
2716 ++tok_len;
2717 for (; isdigit(expr[parsed + tok_len]); ++tok_len);
2718 }
2719 tok_type = LYXP_TOKEN_NUMBER;
2720
2721 } else if (expr[parsed] == '/') {
2722
2723 /* Operator '/', '//' */
2724 if (!strncmp(&expr[parsed], "//", 2)) {
2725 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002726 tok_type = LYXP_TOKEN_OPER_RPATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002727 } else {
2728 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002729 tok_type = LYXP_TOKEN_OPER_PATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002730 }
Radek Krejcib1646a92018-11-02 16:08:26 +01002731
Michal Vasko3e48bf32020-06-01 08:39:07 +02002732 } else if (!strncmp(&expr[parsed], "!=", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002733
Michal Vasko3e48bf32020-06-01 08:39:07 +02002734 /* Operator '!=' */
Radek Krejcib1646a92018-11-02 16:08:26 +01002735 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002736 tok_type = LYXP_TOKEN_OPER_NEQUAL;
2737
2738 } else if (!strncmp(&expr[parsed], "<=", 2) || !strncmp(&expr[parsed], ">=", 2)) {
2739
2740 /* Operator '<=', '>=' */
2741 tok_len = 2;
2742 tok_type = LYXP_TOKEN_OPER_COMP;
Radek Krejcib1646a92018-11-02 16:08:26 +01002743
2744 } else if (expr[parsed] == '|') {
2745
2746 /* Operator '|' */
2747 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002748 tok_type = LYXP_TOKEN_OPER_UNI;
Radek Krejcib1646a92018-11-02 16:08:26 +01002749
2750 } else if ((expr[parsed] == '+') || (expr[parsed] == '-')) {
2751
2752 /* Operator '+', '-' */
2753 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002754 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002755
Michal Vasko3e48bf32020-06-01 08:39:07 +02002756 } else if (expr[parsed] == '=') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002757
Michal Vasko3e48bf32020-06-01 08:39:07 +02002758 /* Operator '=' */
Radek Krejcib1646a92018-11-02 16:08:26 +01002759 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002760 tok_type = LYXP_TOKEN_OPER_EQUAL;
2761
2762 } else if ((expr[parsed] == '<') || (expr[parsed] == '>')) {
2763
2764 /* Operator '<', '>' */
2765 tok_len = 1;
2766 tok_type = LYXP_TOKEN_OPER_COMP;
Radek Krejcib1646a92018-11-02 16:08:26 +01002767
2768 } else if (ret->used && (ret->tokens[ret->used - 1] != LYXP_TOKEN_AT)
2769 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_PAR1)
2770 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_BRACK1)
2771 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_COMMA)
Michal Vasko3e48bf32020-06-01 08:39:07 +02002772 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_LOG)
2773 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_EQUAL)
2774 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_NEQUAL)
2775 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_COMP)
2776 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_MATH)
2777 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_UNI)
2778 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_PATH)
2779 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_RPATH)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002780
2781 /* Operator '*', 'or', 'and', 'mod', or 'div' */
2782 if (expr[parsed] == '*') {
2783 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002784 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002785
2786 } else if (!strncmp(&expr[parsed], "or", 2)) {
2787 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002788 tok_type = LYXP_TOKEN_OPER_LOG;
Radek Krejcib1646a92018-11-02 16:08:26 +01002789
2790 } else if (!strncmp(&expr[parsed], "and", 3)) {
2791 tok_len = 3;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002792 tok_type = LYXP_TOKEN_OPER_LOG;
Radek Krejcib1646a92018-11-02 16:08:26 +01002793
2794 } else if (!strncmp(&expr[parsed], "mod", 3) || !strncmp(&expr[parsed], "div", 3)) {
2795 tok_len = 3;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002796 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002797
2798 } else if (prev_function_check) {
Michal Vasko53078572019-05-24 08:50:15 +02002799 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "Invalid character 0x%x ('%c'), perhaps \"%.*s\" is supposed to be a function call.",
2800 expr[parsed], expr[parsed], ret->tok_len[ret->used - 1], &ret->expr[ret->tok_pos[ret->used - 1]]);
Radek Krejcib1646a92018-11-02 16:08:26 +01002801 goto error;
2802 } else {
Radek Krejcid4270262019-01-07 15:07:25 +01002803 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INEXPR, parsed + 1, expr);
Radek Krejcib1646a92018-11-02 16:08:26 +01002804 goto error;
2805 }
2806 } else if (expr[parsed] == '*') {
2807
2808 /* NameTest '*' */
2809 tok_len = 1;
2810 tok_type = LYXP_TOKEN_NAMETEST;
2811
2812 } else {
2813
2814 /* NameTest (NCName ':' '*' | QName) or NodeType/FunctionName */
2815 ncname_len = parse_ncname(&expr[parsed]);
Radek Krejcid4270262019-01-07 15:07:25 +01002816 LY_CHECK_ERR_GOTO(ncname_len < 0, LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INEXPR, parsed - ncname_len + 1, expr), error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002817 tok_len = ncname_len;
2818
2819 if (expr[parsed + tok_len] == ':') {
2820 ++tok_len;
2821 if (expr[parsed + tok_len] == '*') {
2822 ++tok_len;
2823 } else {
2824 ncname_len = parse_ncname(&expr[parsed + tok_len]);
Radek Krejcid4270262019-01-07 15:07:25 +01002825 LY_CHECK_ERR_GOTO(ncname_len < 0, LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INEXPR, parsed - ncname_len + 1, expr), error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002826 tok_len += ncname_len;
2827 }
2828 /* remove old flag to prevent ambiguities */
2829 prev_function_check = 0;
2830 tok_type = LYXP_TOKEN_NAMETEST;
2831 } else {
2832 /* there is no prefix so it can still be NodeType/FunctionName, we can't finally decide now */
2833 prev_function_check = 1;
2834 tok_type = LYXP_TOKEN_NAMETEST;
2835 }
2836 }
2837
2838 /* store the token, move on to the next one */
2839 LY_CHECK_GOTO(exp_add_token(ctx, ret, tok_type, parsed, tok_len), error);
2840 parsed += tok_len;
2841 while (is_xmlws(expr[parsed])) {
2842 ++parsed;
2843 }
2844
2845 } while (expr[parsed]);
2846
2847 /* prealloc repeat */
2848 ret->repeat = calloc(ret->size, sizeof *ret->repeat);
2849 LY_CHECK_ERR_GOTO(!ret->repeat, LOGMEM(ctx), error);
2850
Michal Vasko03ff5a72019-09-11 13:49:33 +02002851 /* fill repeat */
2852 LY_CHECK_GOTO(reparse_or_expr(ctx, ret, &exp_idx), error);
2853 if (ret->used > exp_idx) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002854 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "Unparsed characters \"%s\" left at the end of an XPath expression.",
2855 &ret->expr[ret->tok_pos[exp_idx]]);
2856 goto error;
2857 }
2858
2859 print_expr_struct_debug(ret);
2860
Radek Krejcib1646a92018-11-02 16:08:26 +01002861 return ret;
2862
2863error:
2864 lyxp_expr_free(ctx, ret);
2865 return NULL;
2866}
2867
Michal Vasko03ff5a72019-09-11 13:49:33 +02002868/*
2869 * warn functions
2870 *
2871 * Warn functions check specific reasonable conditions for schema XPath
2872 * and print a warning if they are not satisfied.
2873 */
2874
2875/**
2876 * @brief Get the last-added schema node that is currently in the context.
2877 *
2878 * @param[in] set Set to search in.
2879 * @return Last-added schema context node, NULL if no node is in context.
2880 */
2881static struct lysc_node *
2882warn_get_scnode_in_ctx(struct lyxp_set *set)
2883{
2884 uint32_t i;
2885
2886 if (!set || (set->type != LYXP_SET_SCNODE_SET)) {
2887 return NULL;
2888 }
2889
2890 i = set->used;
2891 do {
2892 --i;
2893 if (set->val.scnodes[i].in_ctx == 1) {
2894 /* if there are more, simply return the first found (last added) */
2895 return set->val.scnodes[i].scnode;
2896 }
2897 } while (i);
2898
2899 return NULL;
2900}
2901
2902/**
2903 * @brief Test whether a type is numeric - integer type or decimal64.
2904 *
2905 * @param[in] type Type to test.
2906 * @return 1 if numeric, 0 otherwise.
2907 */
2908static int
2909warn_is_numeric_type(struct lysc_type *type)
2910{
2911 struct lysc_type_union *uni;
2912 int ret;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002913 LY_ARRAY_SIZE_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002914
2915 switch (type->basetype) {
2916 case LY_TYPE_DEC64:
2917 case LY_TYPE_INT8:
2918 case LY_TYPE_UINT8:
2919 case LY_TYPE_INT16:
2920 case LY_TYPE_UINT16:
2921 case LY_TYPE_INT32:
2922 case LY_TYPE_UINT32:
2923 case LY_TYPE_INT64:
2924 case LY_TYPE_UINT64:
2925 return 1;
2926 case LY_TYPE_UNION:
2927 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002928 LY_ARRAY_FOR(uni->types, u) {
2929 ret = warn_is_numeric_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002930 if (ret) {
2931 /* found a suitable type */
2932 return 1;
2933 }
2934 }
2935 /* did not find any suitable type */
2936 return 0;
2937 case LY_TYPE_LEAFREF:
2938 return warn_is_numeric_type(((struct lysc_type_leafref *)type)->realtype);
2939 default:
2940 return 0;
2941 }
2942}
2943
2944/**
2945 * @brief Test whether a type is string-like - no integers, decimal64 or binary.
2946 *
2947 * @param[in] type Type to test.
2948 * @return 1 if string, 0 otherwise.
2949 */
2950static int
2951warn_is_string_type(struct lysc_type *type)
2952{
2953 struct lysc_type_union *uni;
2954 int ret;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002955 LY_ARRAY_SIZE_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002956
2957 switch (type->basetype) {
2958 case LY_TYPE_BITS:
2959 case LY_TYPE_ENUM:
2960 case LY_TYPE_IDENT:
2961 case LY_TYPE_INST:
2962 case LY_TYPE_STRING:
2963 return 1;
2964 case LY_TYPE_UNION:
2965 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002966 LY_ARRAY_FOR(uni->types, u) {
2967 ret = warn_is_string_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002968 if (ret) {
2969 /* found a suitable type */
2970 return 1;
2971 }
2972 }
2973 /* did not find any suitable type */
2974 return 0;
2975 case LY_TYPE_LEAFREF:
2976 return warn_is_string_type(((struct lysc_type_leafref *)type)->realtype);
2977 default:
2978 return 0;
2979 }
2980}
2981
2982/**
2983 * @brief Test whether a type is one specific type.
2984 *
2985 * @param[in] type Type to test.
2986 * @param[in] base Expected type.
2987 * @return 1 if it is, 0 otherwise.
2988 */
2989static int
2990warn_is_specific_type(struct lysc_type *type, LY_DATA_TYPE base)
2991{
2992 struct lysc_type_union *uni;
2993 int ret;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002994 LY_ARRAY_SIZE_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002995
2996 if (type->basetype == base) {
2997 return 1;
2998 } else if (type->basetype == LY_TYPE_UNION) {
2999 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003000 LY_ARRAY_FOR(uni->types, u) {
3001 ret = warn_is_specific_type(uni->types[u], base);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003002 if (ret) {
3003 /* found a suitable type */
3004 return 1;
3005 }
3006 }
3007 /* did not find any suitable type */
3008 return 0;
3009 } else if (type->basetype == LY_TYPE_LEAFREF) {
3010 return warn_is_specific_type(((struct lysc_type_leafref *)type)->realtype, base);
3011 }
3012
3013 return 0;
3014}
3015
3016/**
3017 * @brief Get next type of a (union) type.
3018 *
3019 * @param[in] type Base type.
3020 * @param[in] prev_type Previously returned type.
3021 * @return Next type or NULL.
3022 */
3023static struct lysc_type *
3024warn_is_equal_type_next_type(struct lysc_type *type, struct lysc_type *prev_type)
3025{
3026 struct lysc_type_union *uni;
3027 int found = 0;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003028 LY_ARRAY_SIZE_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003029
3030 switch (type->basetype) {
3031 case LY_TYPE_UNION:
3032 uni = (struct lysc_type_union *)type;
3033 if (!prev_type) {
3034 return uni->types[0];
3035 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003036 LY_ARRAY_FOR(uni->types, u) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003037 if (found) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003038 return uni->types[u];
Michal Vasko03ff5a72019-09-11 13:49:33 +02003039 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003040 if (prev_type == uni->types[u]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003041 found = 1;
3042 }
3043 }
3044 return NULL;
3045 default:
3046 if (prev_type) {
3047 assert(type == prev_type);
3048 return NULL;
3049 } else {
3050 return type;
3051 }
3052 }
3053}
3054
3055/**
3056 * @brief Test whether 2 types have a common type.
3057 *
3058 * @param[in] type1 First type.
3059 * @param[in] type2 Second type.
3060 * @return 1 if they do, 0 otherwise.
3061 */
3062static int
3063warn_is_equal_type(struct lysc_type *type1, struct lysc_type *type2)
3064{
3065 struct lysc_type *t1, *rt1, *t2, *rt2;
3066
3067 t1 = NULL;
3068 while ((t1 = warn_is_equal_type_next_type(type1, t1))) {
3069 if (t1->basetype == LY_TYPE_LEAFREF) {
3070 rt1 = ((struct lysc_type_leafref *)t1)->realtype;
3071 } else {
3072 rt1 = t1;
3073 }
3074
3075 t2 = NULL;
3076 while ((t2 = warn_is_equal_type_next_type(type2, t2))) {
3077 if (t2->basetype == LY_TYPE_LEAFREF) {
3078 rt2 = ((struct lysc_type_leafref *)t2)->realtype;
3079 } else {
3080 rt2 = t2;
3081 }
3082
3083 if (rt2->basetype == rt1->basetype) {
3084 /* match found */
3085 return 1;
3086 }
3087 }
3088 }
3089
3090 return 0;
3091}
3092
3093/**
3094 * @brief Check both operands of comparison operators.
3095 *
3096 * @param[in] ctx Context for errors.
3097 * @param[in] set1 First operand set.
3098 * @param[in] set2 Second operand set.
3099 * @param[in] numbers_only Whether accept only numbers or other types are fine too (for '=' and '!=').
3100 * @param[in] expr Start of the expression to print with the warning.
3101 * @param[in] tok_pos Token position.
3102 */
3103static void
3104warn_operands(struct ly_ctx *ctx, struct lyxp_set *set1, struct lyxp_set *set2, int numbers_only, const char *expr, uint16_t tok_pos)
3105{
3106 struct lysc_node_leaf *node1, *node2;
3107 int leaves = 1, warning = 0;
3108
3109 node1 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set1);
3110 node2 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set2);
3111
3112 if (!node1 && !node2) {
3113 /* no node-sets involved, nothing to do */
3114 return;
3115 }
3116
3117 if (node1) {
3118 if (!(node1->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3119 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node1->nodetype), node1->name);
3120 warning = 1;
3121 leaves = 0;
3122 } else if (numbers_only && !warn_is_numeric_type(node1->type)) {
3123 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node1->name);
3124 warning = 1;
3125 }
3126 }
3127
3128 if (node2) {
3129 if (!(node2->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3130 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node2->nodetype), node2->name);
3131 warning = 1;
3132 leaves = 0;
3133 } else if (numbers_only && !warn_is_numeric_type(node2->type)) {
3134 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node2->name);
3135 warning = 1;
3136 }
3137 }
3138
3139 if (node1 && node2 && leaves && !numbers_only) {
3140 if ((warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type))
3141 || (!warn_is_numeric_type(node1->type) && warn_is_numeric_type(node2->type))
3142 || (!warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type)
3143 && !warn_is_equal_type(node1->type, node2->type))) {
3144 LOGWRN(ctx, "Incompatible types of operands \"%s\" and \"%s\" for comparison.", node1->name, node2->name);
3145 warning = 1;
3146 }
3147 }
3148
3149 if (warning) {
3150 LOGWRN(ctx, "Previous warning generated by XPath subexpression[%u] \"%.20s\".", tok_pos, expr + tok_pos);
3151 }
3152}
3153
3154/**
3155 * @brief Check that a value is valid for a leaf. If not applicable, does nothing.
3156 *
3157 * @param[in] exp Parsed XPath expression.
3158 * @param[in] set Set with the leaf/leaf-list.
3159 * @param[in] val_exp Index of the value (literal/number) in @p exp.
3160 * @param[in] equal_exp Index of the start of the equality expression in @p exp.
3161 * @param[in] last_equal_exp Index of the end of the equality expression in @p exp.
3162 */
3163static void
3164warn_equality_value(struct lyxp_expr *exp, struct lyxp_set *set, uint16_t val_exp, uint16_t equal_exp, uint16_t last_equal_exp)
3165{
3166 struct lysc_node *scnode;
3167 struct lysc_type *type;
3168 char *value;
3169 LY_ERR rc;
3170 struct ly_err_item *err = NULL;
3171
3172 if ((scnode = warn_get_scnode_in_ctx(set)) && (scnode->nodetype & (LYS_LEAF | LYS_LEAFLIST))
3173 && ((exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) || (exp->tokens[val_exp] == LYXP_TOKEN_NUMBER))) {
3174 /* check that the node can have the specified value */
3175 if (exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) {
3176 value = strndup(exp->expr + exp->tok_pos[val_exp] + 1, exp->tok_len[val_exp] - 2);
3177 } else {
3178 value = strndup(exp->expr + exp->tok_pos[val_exp], exp->tok_len[val_exp]);
3179 }
3180 if (!value) {
3181 LOGMEM(set->ctx);
3182 return;
3183 }
3184
3185 if ((((struct lysc_node_leaf *)scnode)->type->basetype == LY_TYPE_IDENT) && !strchr(value, ':')) {
3186 LOGWRN(set->ctx, "Identityref \"%s\" comparison with identity \"%s\" without prefix, consider adding"
3187 " a prefix or best using \"derived-from(-or-self)()\" functions.", scnode->name, value);
3188 LOGWRN(set->ctx, "Previous warning generated by XPath subexpression[%u] \"%.*s\".", exp->tok_pos[equal_exp],
3189 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
3190 exp->expr + exp->tok_pos[equal_exp]);
3191 }
3192
3193 type = ((struct lysc_node_leaf *)scnode)->type;
3194 if (type->basetype != LY_TYPE_IDENT) {
3195 rc = type->plugin->store(set->ctx, type, value, strlen(value), LY_TYPE_OPTS_SCHEMA,
3196 lys_resolve_prefix, (void *)type->dflt_mod, LYD_XML, NULL, NULL, NULL, NULL, &err);
3197
3198 if (err) {
3199 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type (%s).", value, err->msg);
3200 ly_err_free(err);
3201 } else if (rc != LY_SUCCESS) {
3202 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type.", value);
3203 }
3204 if (rc != LY_SUCCESS) {
3205 LOGWRN(set->ctx, "Previous warning generated by XPath subexpression[%u] \"%.*s\".", exp->tok_pos[equal_exp],
3206 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
3207 exp->expr + exp->tok_pos[equal_exp]);
3208 }
3209 }
3210 free(value);
3211 }
3212}
3213
3214/*
3215 * XPath functions
3216 */
3217
3218/**
3219 * @brief Execute the YANG 1.1 bit-is-set(node-set, string) function. Returns LYXP_SET_BOOLEAN
3220 * depending on whether the first node bit value from the second argument is set.
3221 *
3222 * @param[in] args Array of arguments.
3223 * @param[in] arg_count Count of elements in @p args.
3224 * @param[in,out] set Context and result set at the same time.
3225 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003226 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003227 */
3228static LY_ERR
3229xpath_bit_is_set(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3230{
3231 struct lyd_node_term *leaf;
3232 struct lysc_node_leaf *sleaf;
3233 struct lysc_type_bits *bits;
3234 LY_ERR rc = LY_SUCCESS;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003235 LY_ARRAY_SIZE_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003236
3237 if (options & LYXP_SCNODE_ALL) {
3238 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3239 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003240 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3241 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 +02003242 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_BITS)) {
3243 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"bits\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003244 }
3245
3246 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3247 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3248 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 +02003249 } else if (!warn_is_string_type(sleaf->type)) {
3250 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003251 }
3252 }
3253 set_scnode_clear_ctx(set);
3254 return rc;
3255 }
3256
Michal Vaskod3678892020-05-21 10:06:58 +02003257 if (args[0]->type != LYXP_SET_NODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003258 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "bit-is-set(node-set, string)");
3259 return LY_EVALID;
3260 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003261 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003262 LY_CHECK_RET(rc);
3263
3264 set_fill_boolean(set, 0);
Michal Vaskod3678892020-05-21 10:06:58 +02003265 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003266 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
3267 if ((leaf->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST))
3268 && (((struct lysc_node_leaf *)leaf->schema)->type->basetype == LY_TYPE_BITS)) {
3269 bits = (struct lysc_type_bits *)((struct lysc_node_leaf *)leaf->schema)->type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003270 LY_ARRAY_FOR(bits->bits, u) {
3271 if (!strcmp(bits->bits[u].name, args[1]->val.str)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003272 set_fill_boolean(set, 1);
3273 break;
3274 }
3275 }
3276 }
3277 }
3278
3279 return LY_SUCCESS;
3280}
3281
3282/**
3283 * @brief Execute the XPath boolean(object) function. Returns LYXP_SET_BOOLEAN
3284 * with the argument converted to boolean.
3285 *
3286 * @param[in] args Array of arguments.
3287 * @param[in] arg_count Count of elements in @p args.
3288 * @param[in,out] set Context and result set at the same time.
3289 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003290 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003291 */
3292static LY_ERR
3293xpath_boolean(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3294{
3295 LY_ERR rc;
3296
3297 if (options & LYXP_SCNODE_ALL) {
3298 set_scnode_clear_ctx(set);
3299 return LY_SUCCESS;
3300 }
3301
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003302 rc = lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003303 LY_CHECK_RET(rc);
3304 set_fill_set(set, args[0]);
3305
3306 return LY_SUCCESS;
3307}
3308
3309/**
3310 * @brief Execute the XPath ceiling(number) function. Returns LYXP_SET_NUMBER
3311 * with the first argument rounded up to the nearest integer.
3312 *
3313 * @param[in] args Array of arguments.
3314 * @param[in] arg_count Count of elements in @p args.
3315 * @param[in,out] set Context and result set at the same time.
3316 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003317 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003318 */
3319static LY_ERR
3320xpath_ceiling(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3321{
3322 struct lysc_node_leaf *sleaf;
3323 LY_ERR rc = LY_SUCCESS;
3324
3325 if (options & LYXP_SCNODE_ALL) {
3326 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3327 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003328 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3329 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 +02003330 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
3331 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003332 }
3333 set_scnode_clear_ctx(set);
3334 return rc;
3335 }
3336
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003337 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003338 LY_CHECK_RET(rc);
3339 if ((long long)args[0]->val.num != args[0]->val.num) {
3340 set_fill_number(set, ((long long)args[0]->val.num) + 1);
3341 } else {
3342 set_fill_number(set, args[0]->val.num);
3343 }
3344
3345 return LY_SUCCESS;
3346}
3347
3348/**
3349 * @brief Execute the XPath concat(string, string, string*) function.
3350 * Returns LYXP_SET_STRING with the concatenation of all the arguments.
3351 *
3352 * @param[in] args Array of arguments.
3353 * @param[in] arg_count Count of elements in @p args.
3354 * @param[in,out] set Context and result set at the same time.
3355 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003356 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003357 */
3358static LY_ERR
3359xpath_concat(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
3360{
3361 uint16_t i;
3362 char *str = NULL;
3363 size_t used = 1;
3364 LY_ERR rc = LY_SUCCESS;
3365 struct lysc_node_leaf *sleaf;
3366
3367 if (options & LYXP_SCNODE_ALL) {
3368 for (i = 0; i < arg_count; ++i) {
3369 if ((args[i]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[i]))) {
3370 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3371 LOGWRN(set->ctx, "Argument #%u of %s is a %s node \"%s\".",
3372 i + 1, __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003373 } else if (!warn_is_string_type(sleaf->type)) {
3374 LOGWRN(set->ctx, "Argument #%u of %s is node \"%s\", not of string-type.", __func__, i + 1, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003375 }
3376 }
3377 }
3378 set_scnode_clear_ctx(set);
3379 return rc;
3380 }
3381
3382 for (i = 0; i < arg_count; ++i) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003383 rc = lyxp_set_cast(args[i], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003384 if (rc != LY_SUCCESS) {
3385 free(str);
3386 return rc;
3387 }
3388
3389 str = ly_realloc(str, (used + strlen(args[i]->val.str)) * sizeof(char));
3390 LY_CHECK_ERR_RET(!str, LOGMEM(set->ctx), LY_EMEM);
3391 strcpy(str + used - 1, args[i]->val.str);
3392 used += strlen(args[i]->val.str);
3393 }
3394
3395 /* free, kind of */
Michal Vaskod3678892020-05-21 10:06:58 +02003396 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003397 set->type = LYXP_SET_STRING;
3398 set->val.str = str;
3399
3400 return LY_SUCCESS;
3401}
3402
3403/**
3404 * @brief Execute the XPath contains(string, string) function.
3405 * Returns LYXP_SET_BOOLEAN whether the second argument can
3406 * be found in the first or not.
3407 *
3408 * @param[in] args Array of arguments.
3409 * @param[in] arg_count Count of elements in @p args.
3410 * @param[in,out] set Context and result set at the same time.
3411 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003412 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003413 */
3414static LY_ERR
3415xpath_contains(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3416{
3417 struct lysc_node_leaf *sleaf;
3418 LY_ERR rc = LY_SUCCESS;
3419
3420 if (options & LYXP_SCNODE_ALL) {
3421 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3422 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3423 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 +02003424 } else if (!warn_is_string_type(sleaf->type)) {
3425 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003426 }
3427 }
3428
3429 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3430 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3431 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 +02003432 } else if (!warn_is_string_type(sleaf->type)) {
3433 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003434 }
3435 }
3436 set_scnode_clear_ctx(set);
3437 return rc;
3438 }
3439
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003440 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003441 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003442 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003443 LY_CHECK_RET(rc);
3444
3445 if (strstr(args[0]->val.str, args[1]->val.str)) {
3446 set_fill_boolean(set, 1);
3447 } else {
3448 set_fill_boolean(set, 0);
3449 }
3450
3451 return LY_SUCCESS;
3452}
3453
3454/**
3455 * @brief Execute the XPath count(node-set) function. Returns LYXP_SET_NUMBER
3456 * with the size of the node-set from the argument.
3457 *
3458 * @param[in] args Array of arguments.
3459 * @param[in] arg_count Count of elements in @p args.
3460 * @param[in,out] set Context and result set at the same time.
3461 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003462 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003463 */
3464static LY_ERR
3465xpath_count(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3466{
3467 struct lysc_node *scnode = NULL;
3468 LY_ERR rc = LY_SUCCESS;
3469
3470 if (options & LYXP_SCNODE_ALL) {
3471 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(scnode = warn_get_scnode_in_ctx(args[0]))) {
3472 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003473 }
3474 set_scnode_clear_ctx(set);
3475 return rc;
3476 }
3477
Michal Vasko03ff5a72019-09-11 13:49:33 +02003478 if (args[0]->type != LYXP_SET_NODE_SET) {
3479 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "count(node-set)");
3480 return LY_EVALID;
3481 }
3482
3483 set_fill_number(set, args[0]->used);
3484 return LY_SUCCESS;
3485}
3486
3487/**
3488 * @brief Execute the XPath current() function. Returns LYXP_SET_NODE_SET
3489 * with the context with the intial node.
3490 *
3491 * @param[in] args Array of arguments.
3492 * @param[in] arg_count Count of elements in @p args.
3493 * @param[in,out] set Context and result set at the same time.
3494 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003495 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003496 */
3497static LY_ERR
3498xpath_current(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
3499{
3500 if (arg_count || args) {
3501 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGCOUNT, arg_count, "current()");
3502 return LY_EVALID;
3503 }
3504
3505 if (options & LYXP_SCNODE_ALL) {
3506 set_scnode_clear_ctx(set);
3507
Michal Vaskoecd62de2019-11-13 12:35:11 +01003508 lyxp_set_scnode_insert_node(set, set->ctx_scnode, LYXP_NODE_ELEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003509 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02003510 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003511
3512 /* position is filled later */
3513 set_insert_node(set, set->ctx_node, 0, LYXP_NODE_ELEM, 0);
3514 }
3515
3516 return LY_SUCCESS;
3517}
3518
3519/**
3520 * @brief Execute the YANG 1.1 deref(node-set) function. Returns LYXP_SET_NODE_SET with either
3521 * leafref or instance-identifier target node(s).
3522 *
3523 * @param[in] args Array of arguments.
3524 * @param[in] arg_count Count of elements in @p args.
3525 * @param[in,out] set Context and result set at the same time.
3526 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003527 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003528 */
3529static LY_ERR
3530xpath_deref(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3531{
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003532 struct lysc_ctx cctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003533 struct lyd_node_term *leaf;
Michal Vasko42e497c2020-01-06 08:38:25 +01003534 struct lysc_node_leaf *sleaf = NULL;
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003535 const struct lysc_node *target;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003536 const struct lyd_node *node;
3537 char *errmsg = NULL;
3538 const char *val;
3539 int dynamic;
3540 LY_ERR rc = LY_SUCCESS;
3541
3542 if (options & LYXP_SCNODE_ALL) {
3543 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3544 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003545 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3546 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 +02003547 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_LEAFREF) && !warn_is_specific_type(sleaf->type, LY_TYPE_INST)) {
3548 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"leafref\" nor \"instance-identifier\".",
3549 __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003550 }
3551 set_scnode_clear_ctx(set);
Michal Vasko42e497c2020-01-06 08:38:25 +01003552 if (sleaf && (sleaf->type->basetype == LY_TYPE_LEAFREF)) {
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003553 cctx.ctx = set->ctx;
3554 rc = lys_compile_leafref_validate(&cctx, (struct lysc_node *)sleaf, (struct lysc_type_leafref *)sleaf->type, &target);
3555 /* it was already validated, it must succeed */
3556 if (rc == LY_SUCCESS) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01003557 lyxp_set_scnode_insert_node(set, target, LYXP_NODE_ELEM);
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003558 }
3559 }
3560
Michal Vasko03ff5a72019-09-11 13:49:33 +02003561 return rc;
3562 }
3563
Michal Vaskod3678892020-05-21 10:06:58 +02003564 if (args[0]->type != LYXP_SET_NODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003565 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "deref(node-set)");
3566 return LY_EVALID;
3567 }
3568
Michal Vaskod3678892020-05-21 10:06:58 +02003569 lyxp_set_free_content(set);
3570 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003571 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
3572 sleaf = (struct lysc_node_leaf *)leaf->schema;
3573 if (sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
3574 if (sleaf->type->basetype == LY_TYPE_LEAFREF) {
3575 /* find leafref target */
3576 val = lyd_value2str(leaf, &dynamic);
3577 node = ly_type_find_leafref(set->ctx, sleaf->type, val, strlen(val), (struct lyd_node *)leaf,
Michal Vaskof03ed032020-03-04 13:31:44 +01003578 set->tree, &leaf->value, &errmsg);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003579 if (dynamic) {
3580 free((char *)val);
3581 }
3582 if (!node) {
3583 LOGERR(set->ctx, LY_EINVAL, errmsg);
3584 free(errmsg);
3585 return LY_EINVAL;
3586 }
3587
3588 /* insert it */
3589 set_insert_node(set, node, 0, LYXP_NODE_ELEM, 0);
3590 } else {
3591 assert(sleaf->type->basetype == LY_TYPE_INST);
Michal Vaskof03ed032020-03-04 13:31:44 +01003592 node = (struct lyd_node *)lyd_target(leaf->value.target, set->tree);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003593 if (!node) {
3594 val = lyd_value2str(leaf, &dynamic);
3595 LOGERR(set->ctx, LY_EVALID, "Invalid instance-identifier \"%s\" value - required instance not found.", val);
3596 if (dynamic) {
3597 free((char *)val);
3598 }
3599 return LY_EVALID;
3600 }
3601 }
3602 }
3603 }
3604
3605 return LY_SUCCESS;
3606}
3607
3608/**
3609 * @brief Execute the YANG 1.1 derived-from(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
3610 * on whether the first argument nodes contain a node of an identity derived from the second
3611 * argument identity.
3612 *
3613 * @param[in] args Array of arguments.
3614 * @param[in] arg_count Count of elements in @p args.
3615 * @param[in,out] set Context and result set at the same time.
3616 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003617 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003618 */
3619static LY_ERR
3620xpath_derived_from(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3621{
3622 uint16_t i;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003623 LY_ARRAY_SIZE_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003624 struct lyd_node_term *leaf;
3625 struct lysc_node_leaf *sleaf;
3626 struct lyd_value data = {0};
3627 struct ly_err_item *err = NULL;
3628 LY_ERR rc = LY_SUCCESS;
3629 int found;
3630
3631 if (options & LYXP_SCNODE_ALL) {
3632 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3633 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003634 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3635 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 +02003636 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_IDENT)) {
3637 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"identityref\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003638 }
3639
3640 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3641 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3642 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 +02003643 } else if (!warn_is_string_type(sleaf->type)) {
3644 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003645 }
3646 }
3647 set_scnode_clear_ctx(set);
3648 return rc;
3649 }
3650
Michal Vaskod3678892020-05-21 10:06:58 +02003651 if (args[0]->type != LYXP_SET_NODE_SET) {
3652 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
3653 "derived-from(node-set, string)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003654 return LY_EVALID;
3655 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003656 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003657 LY_CHECK_RET(rc);
3658
3659 set_fill_boolean(set, 0);
Michal Vaskod3678892020-05-21 10:06:58 +02003660 found = 0;
3661 for (i = 0; i < args[0]->used; ++i) {
3662 leaf = (struct lyd_node_term *)args[0]->val.nodes[i].node;
3663 sleaf = (struct lysc_node_leaf *)leaf->schema;
3664 if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_IDENT)) {
3665 /* store args[1] into ident */
3666 rc = sleaf->type->plugin->store(set->ctx, sleaf->type, args[1]->val.str, strlen(args[1]->val.str),
3667 LY_TYPE_OPTS_STORE, lys_resolve_prefix, (void *)sleaf->dflt_mod, set->format,
3668 (struct lyd_node *)leaf, set->tree, &data, NULL, &err);
3669 if (err) {
3670 ly_err_print(err);
3671 ly_err_free(err);
3672 }
3673 LY_CHECK_RET(rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003674
Michal Vaskod3678892020-05-21 10:06:58 +02003675 LY_ARRAY_FOR(data.ident->derived, u) {
3676 if (data.ident->derived[u] == leaf->value.ident) {
3677 set_fill_boolean(set, 1);
3678 found = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003679 break;
3680 }
3681 }
Michal Vaskod3678892020-05-21 10:06:58 +02003682 if (found) {
3683 break;
3684 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003685 }
3686 }
3687
3688 return LY_SUCCESS;
3689}
3690
3691/**
3692 * @brief Execute the YANG 1.1 derived-from-or-self(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
3693 * on whether the first argument nodes contain a node of an identity that either is or is derived from
3694 * the second argument identity.
3695 *
3696 * @param[in] args Array of arguments.
3697 * @param[in] arg_count Count of elements in @p args.
3698 * @param[in,out] set Context and result set at the same time.
3699 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003700 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003701 */
3702static LY_ERR
3703xpath_derived_from_or_self(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3704{
3705 uint16_t i;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003706 LY_ARRAY_SIZE_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003707 struct lyd_node_term *leaf;
3708 struct lysc_node_leaf *sleaf;
3709 struct lyd_value data = {0};
3710 struct ly_err_item *err = NULL;
3711 LY_ERR rc = LY_SUCCESS;
3712 int found;
3713
3714 if (options & LYXP_SCNODE_ALL) {
3715 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3716 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003717 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3718 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 +02003719 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_IDENT)) {
3720 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"identityref\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003721 }
3722
3723 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3724 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3725 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 +02003726 } else if (!warn_is_string_type(sleaf->type)) {
3727 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003728 }
3729 }
3730 set_scnode_clear_ctx(set);
3731 return rc;
3732 }
3733
Michal Vaskod3678892020-05-21 10:06:58 +02003734 if (args[0]->type != LYXP_SET_NODE_SET) {
3735 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
3736 "derived-from-or-self(node-set, string)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003737 return LY_EVALID;
3738 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003739 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003740 LY_CHECK_RET(rc);
3741
3742 set_fill_boolean(set, 0);
Michal Vaskod3678892020-05-21 10:06:58 +02003743 found = 0;
3744 for (i = 0; i < args[0]->used; ++i) {
3745 leaf = (struct lyd_node_term *)args[0]->val.nodes[i].node;
3746 sleaf = (struct lysc_node_leaf *)leaf->schema;
3747 if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_IDENT)) {
3748 /* store args[1] into ident */
3749 rc = sleaf->type->plugin->store(set->ctx, sleaf->type, args[1]->val.str, strlen(args[1]->val.str),
3750 LY_TYPE_OPTS_STORE, lys_resolve_prefix, (void *)sleaf->dflt_mod, set->format,
3751 (struct lyd_node *)leaf, set->tree, &data, NULL, &err);
3752 if (err) {
3753 ly_err_print(err);
3754 ly_err_free(err);
3755 }
3756 LY_CHECK_RET(rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003757
Michal Vaskod3678892020-05-21 10:06:58 +02003758 if (data.ident == leaf->value.ident) {
3759 set_fill_boolean(set, 1);
3760 break;
3761 }
3762 LY_ARRAY_FOR(data.ident->derived, u) {
3763 if (data.ident->derived[u] == leaf->value.ident) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003764 set_fill_boolean(set, 1);
Michal Vaskod3678892020-05-21 10:06:58 +02003765 found = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003766 break;
3767 }
Michal Vaskod3678892020-05-21 10:06:58 +02003768 }
3769 if (found) {
3770 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003771 }
3772 }
3773 }
3774
3775 return LY_SUCCESS;
3776}
3777
3778/**
3779 * @brief Execute the YANG 1.1 enum-value(node-set) function. Returns LYXP_SET_NUMBER
3780 * with the integer value of the first node's enum value, otherwise NaN.
3781 *
3782 * @param[in] args Array of arguments.
3783 * @param[in] arg_count Count of elements in @p args.
3784 * @param[in,out] set Context and result set at the same time.
3785 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003786 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003787 */
3788static LY_ERR
3789xpath_enum_value(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3790{
3791 struct lyd_node_term *leaf;
3792 struct lysc_node_leaf *sleaf;
3793 LY_ERR rc = LY_SUCCESS;
3794
3795 if (options & LYXP_SCNODE_ALL) {
3796 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3797 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003798 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3799 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 +02003800 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_ENUM)) {
3801 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"enumeration\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003802 }
3803 set_scnode_clear_ctx(set);
3804 return rc;
3805 }
3806
Michal Vaskod3678892020-05-21 10:06:58 +02003807 if (args[0]->type != LYXP_SET_NODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003808 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "enum-value(node-set)");
3809 return LY_EVALID;
3810 }
3811
3812 set_fill_number(set, NAN);
Michal Vaskod3678892020-05-21 10:06:58 +02003813 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003814 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
3815 sleaf = (struct lysc_node_leaf *)leaf->schema;
3816 if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_ENUM)) {
3817 set_fill_number(set, leaf->value.enum_item->value);
3818 }
3819 }
3820
3821 return LY_SUCCESS;
3822}
3823
3824/**
3825 * @brief Execute the XPath false() function. Returns LYXP_SET_BOOLEAN
3826 * with false value.
3827 *
3828 * @param[in] args Array of arguments.
3829 * @param[in] arg_count Count of elements in @p args.
3830 * @param[in,out] set Context and result set at the same time.
3831 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003832 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003833 */
3834static LY_ERR
3835xpath_false(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3836{
3837 if (options & LYXP_SCNODE_ALL) {
3838 set_scnode_clear_ctx(set);
3839 return LY_SUCCESS;
3840 }
3841
3842 set_fill_boolean(set, 0);
3843 return LY_SUCCESS;
3844}
3845
3846/**
3847 * @brief Execute the XPath floor(number) function. Returns LYXP_SET_NUMBER
3848 * with the first argument floored (truncated).
3849 *
3850 * @param[in] args Array of arguments.
3851 * @param[in] arg_count Count of elements in @p args.
3852 * @param[in,out] set Context and result set at the same time.
3853 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003854 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003855 */
3856static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003857xpath_floor(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int UNUSED(options))
Michal Vasko03ff5a72019-09-11 13:49:33 +02003858{
3859 LY_ERR rc;
3860
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003861 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003862 LY_CHECK_RET(rc);
3863 if (isfinite(args[0]->val.num)) {
3864 set_fill_number(set, (long long)args[0]->val.num);
3865 }
3866
3867 return LY_SUCCESS;
3868}
3869
3870/**
3871 * @brief Execute the XPath lang(string) function. Returns LYXP_SET_BOOLEAN
3872 * whether the language of the text matches the one from the argument.
3873 *
3874 * @param[in] args Array of arguments.
3875 * @param[in] arg_count Count of elements in @p args.
3876 * @param[in,out] set Context and result set at the same time.
3877 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003878 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003879 */
3880static LY_ERR
3881xpath_lang(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3882{
3883 const struct lyd_node *node;
3884 struct lysc_node_leaf *sleaf;
Michal Vasko9f96a052020-03-10 09:41:45 +01003885 struct lyd_meta *meta = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003886 const char *val;
3887 int i, dynamic;
3888 LY_ERR rc = LY_SUCCESS;
3889
3890 if (options & LYXP_SCNODE_ALL) {
3891 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3892 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3893 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 +02003894 } else if (!warn_is_string_type(sleaf->type)) {
3895 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003896 }
3897 }
3898 set_scnode_clear_ctx(set);
3899 return rc;
3900 }
3901
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003902 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003903 LY_CHECK_RET(rc);
3904
Michal Vasko03ff5a72019-09-11 13:49:33 +02003905 if (set->type != LYXP_SET_NODE_SET) {
3906 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "lang(string)");
3907 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02003908 } else if (!set->used) {
3909 set_fill_boolean(set, 0);
3910 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003911 }
3912
3913 switch (set->val.nodes[0].type) {
3914 case LYXP_NODE_ELEM:
3915 case LYXP_NODE_TEXT:
3916 node = set->val.nodes[0].node;
3917 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01003918 case LYXP_NODE_META:
3919 node = set->val.meta[0].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003920 break;
3921 default:
3922 /* nothing to do with roots */
3923 set_fill_boolean(set, 0);
3924 return LY_SUCCESS;
3925 }
3926
Michal Vasko9f96a052020-03-10 09:41:45 +01003927 /* find lang metadata */
Michal Vasko03ff5a72019-09-11 13:49:33 +02003928 for (; node; node = (struct lyd_node *)node->parent) {
Michal Vasko9f96a052020-03-10 09:41:45 +01003929 for (meta = node->meta; meta; meta = meta->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003930 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01003931 if (meta->name && !strcmp(meta->name, "lang") && !strcmp(meta->annotation->module->name, "xml")) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003932 break;
3933 }
3934 }
3935
Michal Vasko9f96a052020-03-10 09:41:45 +01003936 if (meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003937 break;
3938 }
3939 }
3940
3941 /* compare languages */
Michal Vasko9f96a052020-03-10 09:41:45 +01003942 if (!meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003943 set_fill_boolean(set, 0);
3944 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01003945 val = lyd_meta2str(meta, &dynamic);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003946 for (i = 0; args[0]->val.str[i]; ++i) {
3947 if (tolower(args[0]->val.str[i]) != tolower(val[i])) {
3948 set_fill_boolean(set, 0);
3949 break;
3950 }
3951 }
3952 if (!args[0]->val.str[i]) {
3953 if (!val[i] || (val[i] == '-')) {
3954 set_fill_boolean(set, 1);
3955 } else {
3956 set_fill_boolean(set, 0);
3957 }
3958 }
3959 if (dynamic) {
3960 free((char *)val);
3961 }
3962 }
3963
3964 return LY_SUCCESS;
3965}
3966
3967/**
3968 * @brief Execute the XPath last() function. Returns LYXP_SET_NUMBER
3969 * with the context size.
3970 *
3971 * @param[in] args Array of arguments.
3972 * @param[in] arg_count Count of elements in @p args.
3973 * @param[in,out] set Context and result set at the same time.
3974 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003975 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003976 */
3977static LY_ERR
3978xpath_last(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3979{
3980 if (options & LYXP_SCNODE_ALL) {
3981 set_scnode_clear_ctx(set);
3982 return LY_SUCCESS;
3983 }
3984
Michal Vasko03ff5a72019-09-11 13:49:33 +02003985 if (set->type != LYXP_SET_NODE_SET) {
3986 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "last()");
3987 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02003988 } else if (!set->used) {
3989 set_fill_number(set, 0);
3990 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003991 }
3992
3993 set_fill_number(set, set->ctx_size);
3994 return LY_SUCCESS;
3995}
3996
3997/**
3998 * @brief Execute the XPath local-name(node-set?) function. Returns LYXP_SET_STRING
3999 * with the node name without namespace from the argument or the context.
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
4008xpath_local_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
4009{
4010 struct lyxp_set_node *item;
4011 /* suppress unused variable warning */
4012 (void)options;
4013
4014 if (options & LYXP_SCNODE_ALL) {
4015 set_scnode_clear_ctx(set);
4016 return LY_SUCCESS;
4017 }
4018
4019 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004020 if (args[0]->type != LYXP_SET_NODE_SET) {
4021 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "local-name(node-set?)");
4022 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004023 } else if (!args[0]->used) {
4024 set_fill_string(set, "", 0);
4025 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004026 }
4027
4028 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004029 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004030
4031 item = &args[0]->val.nodes[0];
4032 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004033 if (set->type != LYXP_SET_NODE_SET) {
4034 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "local-name(node-set?)");
4035 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004036 } else if (!set->used) {
4037 set_fill_string(set, "", 0);
4038 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004039 }
4040
4041 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004042 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004043
4044 item = &set->val.nodes[0];
4045 }
4046
4047 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004048 case LYXP_NODE_NONE:
4049 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004050 case LYXP_NODE_ROOT:
4051 case LYXP_NODE_ROOT_CONFIG:
4052 case LYXP_NODE_TEXT:
4053 set_fill_string(set, "", 0);
4054 break;
4055 case LYXP_NODE_ELEM:
4056 set_fill_string(set, item->node->schema->name, strlen(item->node->schema->name));
4057 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004058 case LYXP_NODE_META:
4059 set_fill_string(set, ((struct lyd_meta *)item->node)->name, strlen(((struct lyd_meta *)item->node)->name));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004060 break;
4061 }
4062
4063 return LY_SUCCESS;
4064}
4065
4066/**
4067 * @brief Execute the XPath name(node-set?) function. Returns LYXP_SET_STRING
4068 * with the node name fully qualified (with namespace) from the argument or the context.
4069 *
4070 * @param[in] args Array of arguments.
4071 * @param[in] arg_count Count of elements in @p args.
4072 * @param[in,out] set Context and result set at the same time.
4073 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004074 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004075 */
4076static LY_ERR
4077xpath_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
4078{
4079 struct lyxp_set_node *item;
4080 struct lys_module *mod;
4081 char *str;
4082 const char *name;
4083 int rc;
4084
4085 if (options & LYXP_SCNODE_ALL) {
4086 set_scnode_clear_ctx(set);
4087 return LY_SUCCESS;
4088 }
4089
4090 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004091 if (args[0]->type != LYXP_SET_NODE_SET) {
4092 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "name(node-set?)");
4093 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004094 } else if (!args[0]->used) {
4095 set_fill_string(set, "", 0);
4096 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004097 }
4098
4099 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004100 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004101
4102 item = &args[0]->val.nodes[0];
4103 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004104 if (set->type != LYXP_SET_NODE_SET) {
4105 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "name(node-set?)");
4106 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004107 } else if (!set->used) {
4108 set_fill_string(set, "", 0);
4109 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004110 }
4111
4112 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004113 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004114
4115 item = &set->val.nodes[0];
4116 }
4117
4118 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004119 case LYXP_NODE_NONE:
4120 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004121 case LYXP_NODE_ROOT:
4122 case LYXP_NODE_ROOT_CONFIG:
4123 case LYXP_NODE_TEXT:
4124 mod = NULL;
4125 name = NULL;
4126 break;
4127 case LYXP_NODE_ELEM:
4128 mod = item->node->schema->module;
4129 name = item->node->schema->name;
4130 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004131 case LYXP_NODE_META:
4132 mod = ((struct lyd_meta *)item->node)->annotation->module;
4133 name = ((struct lyd_meta *)item->node)->name;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004134 break;
4135 }
4136
4137 if (mod && name) {
4138 switch (set->format) {
Michal Vasko52927e22020-03-16 17:26:14 +01004139 case LYD_SCHEMA:
Michal Vasko03ff5a72019-09-11 13:49:33 +02004140 rc = asprintf(&str, "%s:%s", lys_prefix_find_module(set->local_mod, mod), name);
4141 break;
4142 case LYD_JSON:
4143 rc = asprintf(&str, "%s:%s", mod->name, name);
4144 break;
Michal Vasko52927e22020-03-16 17:26:14 +01004145 case LYD_XML:
Michal Vasko9409ef62019-09-12 11:47:17 +02004146 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004147 }
4148 LY_CHECK_ERR_RET(rc == -1, LOGMEM(set->ctx), LY_EMEM);
4149 set_fill_string(set, str, strlen(str));
4150 free(str);
4151 } else {
4152 set_fill_string(set, "", 0);
4153 }
4154
4155 return LY_SUCCESS;
4156}
4157
4158/**
4159 * @brief Execute the XPath namespace-uri(node-set?) function. Returns LYXP_SET_STRING
4160 * with the namespace of the node from the argument or the context.
4161 *
4162 * @param[in] args Array of arguments.
4163 * @param[in] arg_count Count of elements in @p args.
4164 * @param[in,out] set Context and result set at the same time.
4165 * @param[in] options XPath options.
4166 * @return LY_ERR (LY_EINVAL for wrong arguments on schema)
4167 */
4168static LY_ERR
4169xpath_namespace_uri(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
4170{
4171 struct lyxp_set_node *item;
4172 struct lys_module *mod;
4173 /* suppress unused variable warning */
4174 (void)options;
4175
4176 if (options & LYXP_SCNODE_ALL) {
4177 set_scnode_clear_ctx(set);
4178 return LY_SUCCESS;
4179 }
4180
4181 if (arg_count) {
Michal Vaskod3678892020-05-21 10:06:58 +02004182 if (args[0]->type != LYXP_SET_NODE_SET) {
4183 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
4184 "namespace-uri(node-set?)");
4185 return LY_EVALID;
4186 } else if (!args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004187 set_fill_string(set, "", 0);
4188 return LY_SUCCESS;
4189 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004190
4191 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004192 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004193
4194 item = &args[0]->val.nodes[0];
4195 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004196 if (set->type != LYXP_SET_NODE_SET) {
4197 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "namespace-uri(node-set?)");
4198 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004199 } else if (!set->used) {
4200 set_fill_string(set, "", 0);
4201 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004202 }
4203
4204 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004205 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004206
4207 item = &set->val.nodes[0];
4208 }
4209
4210 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004211 case LYXP_NODE_NONE:
4212 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004213 case LYXP_NODE_ROOT:
4214 case LYXP_NODE_ROOT_CONFIG:
4215 case LYXP_NODE_TEXT:
4216 set_fill_string(set, "", 0);
4217 break;
4218 case LYXP_NODE_ELEM:
Michal Vasko9f96a052020-03-10 09:41:45 +01004219 case LYXP_NODE_META:
Michal Vasko03ff5a72019-09-11 13:49:33 +02004220 if (item->type == LYXP_NODE_ELEM) {
4221 mod = item->node->schema->module;
Michal Vasko9f96a052020-03-10 09:41:45 +01004222 } else { /* LYXP_NODE_META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004223 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004224 mod = ((struct lyd_meta *)item->node)->annotation->module;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004225 }
4226
4227 set_fill_string(set, mod->ns, strlen(mod->ns));
4228 break;
4229 }
4230
4231 return LY_SUCCESS;
4232}
4233
4234/**
4235 * @brief Execute the XPath node() function (node type). Returns LYXP_SET_NODE_SET
4236 * with only nodes from the context. In practice it either leaves the context
4237 * as it is or returns an empty node set.
4238 *
4239 * @param[in] args Array of arguments.
4240 * @param[in] arg_count Count of elements in @p args.
4241 * @param[in,out] set Context and result set at the same time.
4242 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004243 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004244 */
4245static LY_ERR
4246xpath_node(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4247{
4248 if (options & LYXP_SCNODE_ALL) {
4249 set_scnode_clear_ctx(set);
4250 return LY_SUCCESS;
4251 }
4252
4253 if (set->type != LYXP_SET_NODE_SET) {
Michal Vaskod3678892020-05-21 10:06:58 +02004254 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004255 }
4256 return LY_SUCCESS;
4257}
4258
4259/**
4260 * @brief Execute the XPath normalize-space(string?) function. Returns LYXP_SET_STRING
4261 * with normalized value (no leading, trailing, double white spaces) of the node
4262 * from the argument or the context.
4263 *
4264 * @param[in] args Array of arguments.
4265 * @param[in] arg_count Count of elements in @p args.
4266 * @param[in,out] set Context and result set at the same time.
4267 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004268 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004269 */
4270static LY_ERR
4271xpath_normalize_space(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
4272{
4273 uint16_t i, new_used;
4274 char *new;
4275 int have_spaces = 0, space_before = 0;
4276 struct lysc_node_leaf *sleaf;
4277 LY_ERR rc = LY_SUCCESS;
4278
4279 if (options & LYXP_SCNODE_ALL) {
4280 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4281 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4282 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 +02004283 } else if (!warn_is_string_type(sleaf->type)) {
4284 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004285 }
4286 }
4287 set_scnode_clear_ctx(set);
4288 return rc;
4289 }
4290
4291 if (arg_count) {
4292 set_fill_set(set, args[0]);
4293 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004294 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004295 LY_CHECK_RET(rc);
4296
4297 /* is there any normalization necessary? */
4298 for (i = 0; set->val.str[i]; ++i) {
4299 if (is_xmlws(set->val.str[i])) {
4300 if ((i == 0) || space_before || (!set->val.str[i + 1])) {
4301 have_spaces = 1;
4302 break;
4303 }
4304 space_before = 1;
4305 } else {
4306 space_before = 0;
4307 }
4308 }
4309
4310 /* yep, there is */
4311 if (have_spaces) {
4312 /* it's enough, at least one character will go, makes space for ending '\0' */
4313 new = malloc(strlen(set->val.str) * sizeof(char));
4314 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4315 new_used = 0;
4316
4317 space_before = 0;
4318 for (i = 0; set->val.str[i]; ++i) {
4319 if (is_xmlws(set->val.str[i])) {
4320 if ((i == 0) || space_before) {
4321 space_before = 1;
4322 continue;
4323 } else {
4324 space_before = 1;
4325 }
4326 } else {
4327 space_before = 0;
4328 }
4329
4330 new[new_used] = (space_before ? ' ' : set->val.str[i]);
4331 ++new_used;
4332 }
4333
4334 /* at worst there is one trailing space now */
4335 if (new_used && is_xmlws(new[new_used - 1])) {
4336 --new_used;
4337 }
4338
4339 new = ly_realloc(new, (new_used + 1) * sizeof(char));
4340 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4341 new[new_used] = '\0';
4342
4343 free(set->val.str);
4344 set->val.str = new;
4345 }
4346
4347 return LY_SUCCESS;
4348}
4349
4350/**
4351 * @brief Execute the XPath not(boolean) function. Returns LYXP_SET_BOOLEAN
4352 * with the argument converted to boolean and logically inverted.
4353 *
4354 * @param[in] args Array of arguments.
4355 * @param[in] arg_count Count of elements in @p args.
4356 * @param[in,out] set Context and result set at the same time.
4357 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004358 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004359 */
4360static LY_ERR
4361xpath_not(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4362{
4363 if (options & LYXP_SCNODE_ALL) {
4364 set_scnode_clear_ctx(set);
4365 return LY_SUCCESS;
4366 }
4367
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004368 lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004369 if (args[0]->val.bool) {
4370 set_fill_boolean(set, 0);
4371 } else {
4372 set_fill_boolean(set, 1);
4373 }
4374
4375 return LY_SUCCESS;
4376}
4377
4378/**
4379 * @brief Execute the XPath number(object?) function. Returns LYXP_SET_NUMBER
4380 * with the number representation of either the argument or the context.
4381 *
4382 * @param[in] args Array of arguments.
4383 * @param[in] arg_count Count of elements in @p args.
4384 * @param[in,out] set Context and result set at the same time.
4385 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004386 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004387 */
4388static LY_ERR
4389xpath_number(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
4390{
4391 LY_ERR rc;
4392
4393 if (options & LYXP_SCNODE_ALL) {
4394 set_scnode_clear_ctx(set);
4395 return LY_SUCCESS;
4396 }
4397
4398 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004399 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004400 LY_CHECK_RET(rc);
4401 set_fill_set(set, args[0]);
4402 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004403 rc = lyxp_set_cast(set, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004404 LY_CHECK_RET(rc);
4405 }
4406
4407 return LY_SUCCESS;
4408}
4409
4410/**
4411 * @brief Execute the XPath position() function. Returns LYXP_SET_NUMBER
4412 * with the context position.
4413 *
4414 * @param[in] args Array of arguments.
4415 * @param[in] arg_count Count of elements in @p args.
4416 * @param[in,out] set Context and result set at the same time.
4417 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004418 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004419 */
4420static LY_ERR
4421xpath_position(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4422{
4423 if (options & LYXP_SCNODE_ALL) {
4424 set_scnode_clear_ctx(set);
4425 return LY_SUCCESS;
4426 }
4427
Michal Vasko03ff5a72019-09-11 13:49:33 +02004428 if (set->type != LYXP_SET_NODE_SET) {
4429 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "position()");
4430 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004431 } else if (!set->used) {
4432 set_fill_number(set, 0);
4433 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004434 }
4435
4436 set_fill_number(set, set->ctx_pos);
4437
4438 /* UNUSED in 'Release' build type */
4439 (void)options;
4440 return LY_SUCCESS;
4441}
4442
4443/**
4444 * @brief Execute the YANG 1.1 re-match(string, string) function. Returns LYXP_SET_BOOLEAN
4445 * depending on whether the second argument regex matches the first argument string. For details refer to
4446 * YANG 1.1 RFC section 10.2.1.
4447 *
4448 * @param[in] args Array of arguments.
4449 * @param[in] arg_count Count of elements in @p args.
4450 * @param[in,out] set Context and result set at the same time.
4451 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004452 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004453 */
4454static LY_ERR
4455xpath_re_match(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4456{
4457 struct lysc_pattern **patterns = NULL, **pattern;
4458 struct lysc_node_leaf *sleaf;
4459 char *path;
4460 LY_ERR rc = LY_SUCCESS;
4461 struct ly_err_item *err;
4462
4463 if (options & LYXP_SCNODE_ALL) {
4464 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4465 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4466 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 +02004467 } else if (!warn_is_string_type(sleaf->type)) {
4468 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004469 }
4470 }
4471
4472 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4473 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4474 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 +02004475 } else if (!warn_is_string_type(sleaf->type)) {
4476 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004477 }
4478 }
4479 set_scnode_clear_ctx(set);
4480 return rc;
4481 }
4482
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004483 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004484 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004485 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004486 LY_CHECK_RET(rc);
4487
4488 LY_ARRAY_NEW_RET(set->ctx, patterns, pattern, LY_EMEM);
4489 *pattern = malloc(sizeof **pattern);
4490 path = lyd_path(set->ctx_node, LYD_PATH_LOG, NULL, 0);
4491 rc = lys_compile_type_pattern_check(set->ctx, path, args[1]->val.str, &(*pattern)->code);
4492 free(path);
4493 if (rc != LY_SUCCESS) {
4494 LY_ARRAY_FREE(patterns);
4495 return rc;
4496 }
4497
4498 rc = ly_type_validate_patterns(patterns, args[0]->val.str, strlen(args[0]->val.str), &err);
4499 pcre2_code_free((*pattern)->code);
4500 free(*pattern);
4501 LY_ARRAY_FREE(patterns);
4502 if (rc && (rc != LY_EVALID)) {
4503 ly_err_print(err);
4504 ly_err_free(err);
4505 return rc;
4506 }
4507
4508 if (rc == LY_EVALID) {
4509 ly_err_free(err);
4510 set_fill_boolean(set, 0);
4511 } else {
4512 set_fill_boolean(set, 1);
4513 }
4514
4515 return LY_SUCCESS;
4516}
4517
4518/**
4519 * @brief Execute the XPath round(number) function. Returns LYXP_SET_NUMBER
4520 * with the rounded first argument. For details refer to
4521 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-round.
4522 *
4523 * @param[in] args Array of arguments.
4524 * @param[in] arg_count Count of elements in @p args.
4525 * @param[in,out] set Context and result set at the same time.
4526 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004527 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004528 */
4529static LY_ERR
4530xpath_round(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4531{
4532 struct lysc_node_leaf *sleaf;
4533 LY_ERR rc = LY_SUCCESS;
4534
4535 if (options & LYXP_SCNODE_ALL) {
4536 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4537 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004538 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4539 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 +02004540 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
4541 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004542 }
4543 set_scnode_clear_ctx(set);
4544 return rc;
4545 }
4546
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004547 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004548 LY_CHECK_RET(rc);
4549
4550 /* cover only the cases where floor can't be used */
4551 if ((args[0]->val.num == -0.0f) || ((args[0]->val.num < 0) && (args[0]->val.num >= -0.5))) {
4552 set_fill_number(set, -0.0f);
4553 } else {
4554 args[0]->val.num += 0.5;
4555 rc = xpath_floor(args, 1, args[0], options);
4556 LY_CHECK_RET(rc);
4557 set_fill_number(set, args[0]->val.num);
4558 }
4559
4560 return LY_SUCCESS;
4561}
4562
4563/**
4564 * @brief Execute the XPath starts-with(string, string) function.
4565 * Returns LYXP_SET_BOOLEAN whether the second argument is
4566 * the prefix of the first or not.
4567 *
4568 * @param[in] args Array of arguments.
4569 * @param[in] arg_count Count of elements in @p args.
4570 * @param[in,out] set Context and result set at the same time.
4571 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004572 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004573 */
4574static LY_ERR
4575xpath_starts_with(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4576{
4577 struct lysc_node_leaf *sleaf;
4578 LY_ERR rc = LY_SUCCESS;
4579
4580 if (options & LYXP_SCNODE_ALL) {
4581 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4582 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4583 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 +02004584 } else if (!warn_is_string_type(sleaf->type)) {
4585 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004586 }
4587 }
4588
4589 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4590 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4591 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 +02004592 } else if (!warn_is_string_type(sleaf->type)) {
4593 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004594 }
4595 }
4596 set_scnode_clear_ctx(set);
4597 return rc;
4598 }
4599
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004600 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004601 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004602 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004603 LY_CHECK_RET(rc);
4604
4605 if (strncmp(args[0]->val.str, args[1]->val.str, strlen(args[1]->val.str))) {
4606 set_fill_boolean(set, 0);
4607 } else {
4608 set_fill_boolean(set, 1);
4609 }
4610
4611 return LY_SUCCESS;
4612}
4613
4614/**
4615 * @brief Execute the XPath string(object?) function. Returns LYXP_SET_STRING
4616 * with the string representation of either the argument or the context.
4617 *
4618 * @param[in] args Array of arguments.
4619 * @param[in] arg_count Count of elements in @p args.
4620 * @param[in,out] set Context and result set at the same time.
4621 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004622 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004623 */
4624static LY_ERR
4625xpath_string(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
4626{
4627 LY_ERR rc;
4628
4629 if (options & LYXP_SCNODE_ALL) {
4630 set_scnode_clear_ctx(set);
4631 return LY_SUCCESS;
4632 }
4633
4634 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004635 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004636 LY_CHECK_RET(rc);
4637 set_fill_set(set, args[0]);
4638 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004639 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004640 LY_CHECK_RET(rc);
4641 }
4642
4643 return LY_SUCCESS;
4644}
4645
4646/**
4647 * @brief Execute the XPath string-length(string?) function. Returns LYXP_SET_NUMBER
4648 * with the length of the string in either the argument or the context.
4649 *
4650 * @param[in] args Array of arguments.
4651 * @param[in] arg_count Count of elements in @p args.
4652 * @param[in,out] set Context and result set at the same time.
4653 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004654 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004655 */
4656static LY_ERR
4657xpath_string_length(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
4658{
4659 struct lysc_node_leaf *sleaf;
4660 LY_ERR rc = LY_SUCCESS;
4661
4662 if (options & LYXP_SCNODE_ALL) {
4663 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4664 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4665 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 +02004666 } else if (!warn_is_string_type(sleaf->type)) {
4667 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004668 }
4669 }
4670 if (!arg_count && (set->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set))) {
4671 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4672 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 +02004673 } else if (!warn_is_string_type(sleaf->type)) {
4674 LOGWRN(set->ctx, "Argument #0 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004675 }
4676 }
4677 set_scnode_clear_ctx(set);
4678 return rc;
4679 }
4680
4681 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004682 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004683 LY_CHECK_RET(rc);
4684 set_fill_number(set, strlen(args[0]->val.str));
4685 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004686 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004687 LY_CHECK_RET(rc);
4688 set_fill_number(set, strlen(set->val.str));
4689 }
4690
4691 return LY_SUCCESS;
4692}
4693
4694/**
4695 * @brief Execute the XPath substring(string, number, number?) function.
4696 * Returns LYXP_SET_STRING substring of the first argument starting
4697 * on the second argument index ending on the third argument index,
4698 * indexed from 1. For exact definition refer to
4699 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-substring.
4700 *
4701 * @param[in] args Array of arguments.
4702 * @param[in] arg_count Count of elements in @p args.
4703 * @param[in,out] set Context and result set at the same time.
4704 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004705 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004706 */
4707static LY_ERR
4708xpath_substring(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
4709{
4710 int start, len;
4711 uint16_t str_start, str_len, pos;
4712 struct lysc_node_leaf *sleaf;
4713 LY_ERR rc = LY_SUCCESS;
4714
4715 if (options & LYXP_SCNODE_ALL) {
4716 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4717 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4718 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 +02004719 } else if (!warn_is_string_type(sleaf->type)) {
4720 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004721 }
4722 }
4723
4724 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4725 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4726 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 +02004727 } else if (!warn_is_numeric_type(sleaf->type)) {
4728 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004729 }
4730 }
4731
4732 if ((arg_count == 3) && (args[2]->type == LYXP_SET_SCNODE_SET)
4733 && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
4734 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4735 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 +02004736 } else if (!warn_is_numeric_type(sleaf->type)) {
4737 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004738 }
4739 }
4740 set_scnode_clear_ctx(set);
4741 return rc;
4742 }
4743
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004744 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004745 LY_CHECK_RET(rc);
4746
4747 /* start */
4748 if (xpath_round(&args[1], 1, args[1], options)) {
4749 return -1;
4750 }
4751 if (isfinite(args[1]->val.num)) {
4752 start = args[1]->val.num - 1;
4753 } else if (isinf(args[1]->val.num) && signbit(args[1]->val.num)) {
4754 start = INT_MIN;
4755 } else {
4756 start = INT_MAX;
4757 }
4758
4759 /* len */
4760 if (arg_count == 3) {
4761 rc = xpath_round(&args[2], 1, args[2], options);
4762 LY_CHECK_RET(rc);
4763 if (isfinite(args[2]->val.num)) {
4764 len = args[2]->val.num;
4765 } else if (isnan(args[2]->val.num) || signbit(args[2]->val.num)) {
4766 len = 0;
4767 } else {
4768 len = INT_MAX;
4769 }
4770 } else {
4771 len = INT_MAX;
4772 }
4773
4774 /* find matching character positions */
4775 str_start = 0;
4776 str_len = 0;
4777 for (pos = 0; args[0]->val.str[pos]; ++pos) {
4778 if (pos < start) {
4779 ++str_start;
4780 } else if (pos < start + len) {
4781 ++str_len;
4782 } else {
4783 break;
4784 }
4785 }
4786
4787 set_fill_string(set, args[0]->val.str + str_start, str_len);
4788 return LY_SUCCESS;
4789}
4790
4791/**
4792 * @brief Execute the XPath substring-after(string, string) function.
4793 * Returns LYXP_SET_STRING with the string succeeding the occurance
4794 * of the second argument in the first or an empty string.
4795 *
4796 * @param[in] args Array of arguments.
4797 * @param[in] arg_count Count of elements in @p args.
4798 * @param[in,out] set Context and result set at the same time.
4799 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004800 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004801 */
4802static LY_ERR
4803xpath_substring_after(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4804{
4805 char *ptr;
4806 struct lysc_node_leaf *sleaf;
4807 LY_ERR rc = LY_SUCCESS;
4808
4809 if (options & LYXP_SCNODE_ALL) {
4810 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4811 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4812 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 +02004813 } else if (!warn_is_string_type(sleaf->type)) {
4814 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004815 }
4816 }
4817
4818 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4819 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4820 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 +02004821 } else if (!warn_is_string_type(sleaf->type)) {
4822 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004823 }
4824 }
4825 set_scnode_clear_ctx(set);
4826 return rc;
4827 }
4828
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004829 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004830 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004831 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004832 LY_CHECK_RET(rc);
4833
4834 ptr = strstr(args[0]->val.str, args[1]->val.str);
4835 if (ptr) {
4836 set_fill_string(set, ptr + strlen(args[1]->val.str), strlen(ptr + strlen(args[1]->val.str)));
4837 } else {
4838 set_fill_string(set, "", 0);
4839 }
4840
4841 return LY_SUCCESS;
4842}
4843
4844/**
4845 * @brief Execute the XPath substring-before(string, string) function.
4846 * Returns LYXP_SET_STRING with the string preceding the occurance
4847 * of the second argument in the first or an empty string.
4848 *
4849 * @param[in] args Array of arguments.
4850 * @param[in] arg_count Count of elements in @p args.
4851 * @param[in,out] set Context and result set at the same time.
4852 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004853 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004854 */
4855static LY_ERR
4856xpath_substring_before(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4857{
4858 char *ptr;
4859 struct lysc_node_leaf *sleaf;
4860 LY_ERR rc = LY_SUCCESS;
4861
4862 if (options & LYXP_SCNODE_ALL) {
4863 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4864 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4865 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 +02004866 } else if (!warn_is_string_type(sleaf->type)) {
4867 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004868 }
4869 }
4870
4871 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4872 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4873 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 +02004874 } else if (!warn_is_string_type(sleaf->type)) {
4875 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004876 }
4877 }
4878 set_scnode_clear_ctx(set);
4879 return rc;
4880 }
4881
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004882 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004883 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004884 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004885 LY_CHECK_RET(rc);
4886
4887 ptr = strstr(args[0]->val.str, args[1]->val.str);
4888 if (ptr) {
4889 set_fill_string(set, args[0]->val.str, ptr - args[0]->val.str);
4890 } else {
4891 set_fill_string(set, "", 0);
4892 }
4893
4894 return LY_SUCCESS;
4895}
4896
4897/**
4898 * @brief Execute the XPath sum(node-set) function. Returns LYXP_SET_NUMBER
4899 * with the sum of all the nodes in the context.
4900 *
4901 * @param[in] args Array of arguments.
4902 * @param[in] arg_count Count of elements in @p args.
4903 * @param[in,out] set Context and result set at the same time.
4904 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004905 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004906 */
4907static LY_ERR
4908xpath_sum(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4909{
4910 long double num;
4911 char *str;
4912 uint16_t i;
4913 struct lyxp_set set_item;
4914 struct lysc_node_leaf *sleaf;
4915 LY_ERR rc = LY_SUCCESS;
4916
4917 if (options & LYXP_SCNODE_ALL) {
4918 if (args[0]->type == LYXP_SET_SCNODE_SET) {
4919 for (i = 0; i < args[0]->used; ++i) {
4920 if (args[0]->val.scnodes[i].in_ctx == 1) {
4921 sleaf = (struct lysc_node_leaf *)args[0]->val.scnodes[i].scnode;
4922 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4923 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__,
4924 lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004925 } else if (!warn_is_numeric_type(sleaf->type)) {
4926 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004927 }
4928 }
4929 }
4930 }
4931 set_scnode_clear_ctx(set);
4932 return rc;
4933 }
4934
4935 set_fill_number(set, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004936
4937 if (args[0]->type != LYXP_SET_NODE_SET) {
4938 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "sum(node-set)");
4939 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004940 } else if (!args[0]->used) {
4941 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004942 }
4943
Michal Vasko5c4e5892019-11-14 12:31:38 +01004944 set_init(&set_item, set);
4945
Michal Vasko03ff5a72019-09-11 13:49:33 +02004946 set_item.type = LYXP_SET_NODE_SET;
4947 set_item.val.nodes = malloc(sizeof *set_item.val.nodes);
4948 LY_CHECK_ERR_RET(!set_item.val.nodes, LOGMEM(set->ctx), LY_EMEM);
4949
4950 set_item.used = 1;
4951 set_item.size = 1;
4952
4953 for (i = 0; i < args[0]->used; ++i) {
4954 set_item.val.nodes[0] = args[0]->val.nodes[i];
4955
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004956 rc = cast_node_set_to_string(&set_item, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004957 LY_CHECK_RET(rc);
4958 num = cast_string_to_number(str);
4959 free(str);
4960 set->val.num += num;
4961 }
4962
4963 free(set_item.val.nodes);
4964
4965 return LY_SUCCESS;
4966}
4967
4968/**
4969 * @brief Execute the XPath text() function (node type). Returns LYXP_SET_NODE_SET
4970 * with the text content of the nodes in the context.
4971 *
4972 * @param[in] args Array of arguments.
4973 * @param[in] arg_count Count of elements in @p args.
4974 * @param[in,out] set Context and result set at the same time.
4975 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004976 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004977 */
4978static LY_ERR
4979xpath_text(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4980{
4981 uint32_t i;
4982
4983 if (options & LYXP_SCNODE_ALL) {
4984 set_scnode_clear_ctx(set);
4985 return LY_SUCCESS;
4986 }
4987
Michal Vasko03ff5a72019-09-11 13:49:33 +02004988 if (set->type != LYXP_SET_NODE_SET) {
4989 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "text()");
4990 return LY_EVALID;
4991 }
4992
4993 for (i = 0; i < set->used;) {
4994 switch (set->val.nodes[i].type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004995 case LYXP_NODE_NONE:
4996 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004997 case LYXP_NODE_ELEM:
Michal Vasko03ff5a72019-09-11 13:49:33 +02004998 if (set->val.nodes[i].node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
4999 set->val.nodes[i].type = LYXP_NODE_TEXT;
5000 ++i;
5001 break;
5002 }
5003 /* fall through */
5004 case LYXP_NODE_ROOT:
5005 case LYXP_NODE_ROOT_CONFIG:
5006 case LYXP_NODE_TEXT:
Michal Vasko9f96a052020-03-10 09:41:45 +01005007 case LYXP_NODE_META:
Michal Vasko03ff5a72019-09-11 13:49:33 +02005008 set_remove_node(set, i);
5009 break;
5010 }
5011 }
5012
5013 return LY_SUCCESS;
5014}
5015
5016/**
5017 * @brief Execute the XPath translate(string, string, string) function.
5018 * Returns LYXP_SET_STRING with the first argument with the characters
5019 * from the second argument replaced by those on the corresponding
5020 * positions in the third argument.
5021 *
5022 * @param[in] args Array of arguments.
5023 * @param[in] arg_count Count of elements in @p args.
5024 * @param[in,out] set Context and result set at the same time.
5025 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005026 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005027 */
5028static LY_ERR
5029xpath_translate(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
5030{
5031 uint16_t i, j, new_used;
5032 char *new;
5033 int found, have_removed;
5034 struct lysc_node_leaf *sleaf;
5035 LY_ERR rc = LY_SUCCESS;
5036
5037 if (options & LYXP_SCNODE_ALL) {
5038 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5039 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5040 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 +02005041 } else if (!warn_is_string_type(sleaf->type)) {
5042 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005043 }
5044 }
5045
5046 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5047 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5048 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 +02005049 } else if (!warn_is_string_type(sleaf->type)) {
5050 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005051 }
5052 }
5053
5054 if ((args[2]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
5055 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5056 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 +02005057 } else if (!warn_is_string_type(sleaf->type)) {
5058 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005059 }
5060 }
5061 set_scnode_clear_ctx(set);
5062 return rc;
5063 }
5064
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005065 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005066 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005067 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005068 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005069 rc = lyxp_set_cast(args[2], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005070 LY_CHECK_RET(rc);
5071
5072 new = malloc((strlen(args[0]->val.str) + 1) * sizeof(char));
5073 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5074 new_used = 0;
5075
5076 have_removed = 0;
5077 for (i = 0; args[0]->val.str[i]; ++i) {
5078 found = 0;
5079
5080 for (j = 0; args[1]->val.str[j]; ++j) {
5081 if (args[0]->val.str[i] == args[1]->val.str[j]) {
5082 /* removing this char */
5083 if (j >= strlen(args[2]->val.str)) {
5084 have_removed = 1;
5085 found = 1;
5086 break;
5087 }
5088 /* replacing this char */
5089 new[new_used] = args[2]->val.str[j];
5090 ++new_used;
5091 found = 1;
5092 break;
5093 }
5094 }
5095
5096 /* copying this char */
5097 if (!found) {
5098 new[new_used] = args[0]->val.str[i];
5099 ++new_used;
5100 }
5101 }
5102
5103 if (have_removed) {
5104 new = ly_realloc(new, (new_used + 1) * sizeof(char));
5105 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5106 }
5107 new[new_used] = '\0';
5108
Michal Vaskod3678892020-05-21 10:06:58 +02005109 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005110 set->type = LYXP_SET_STRING;
5111 set->val.str = new;
5112
5113 return LY_SUCCESS;
5114}
5115
5116/**
5117 * @brief Execute the XPath true() function. Returns LYXP_SET_BOOLEAN
5118 * with true value.
5119 *
5120 * @param[in] args Array of arguments.
5121 * @param[in] arg_count Count of elements in @p args.
5122 * @param[in,out] set Context and result set at the same time.
5123 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005124 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005125 */
5126static LY_ERR
5127xpath_true(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
5128{
5129 if (options & LYXP_SCNODE_ALL) {
5130 set_scnode_clear_ctx(set);
5131 return LY_SUCCESS;
5132 }
5133
5134 set_fill_boolean(set, 1);
5135 return LY_SUCCESS;
5136}
5137
5138/*
5139 * moveto functions
5140 *
5141 * They and only they actually change the context (set).
5142 */
5143
5144/**
Michal Vasko6346ece2019-09-24 13:12:53 +02005145 * @brief Skip prefix and return corresponding model if there is a prefix. Logs directly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005146 *
Michal Vasko2104e9f2020-03-06 08:23:25 +01005147 * XPath @p set is expected to be a (sc)node set!
5148 *
Michal Vasko6346ece2019-09-24 13:12:53 +02005149 * @param[in,out] qname Qualified node name. If includes prefix, it is skipped.
5150 * @param[in,out] qname_len Length of @p qname, is updated accordingly.
5151 * @param[in] set Set with XPath context.
5152 * @param[out] moveto_mod Expected module of a matching node.
5153 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005154 */
Michal Vasko6346ece2019-09-24 13:12:53 +02005155static LY_ERR
5156moveto_resolve_model(const char **qname, uint16_t *qname_len, struct lyxp_set *set, const struct lys_module **moveto_mod)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005157{
Michal Vasko6346ece2019-09-24 13:12:53 +02005158 const struct lys_module *mod;
5159 const char *ptr;
5160 int pref_len;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005161 char *str;
5162
Michal Vasko2104e9f2020-03-06 08:23:25 +01005163 assert((set->type == LYXP_SET_NODE_SET) || (set->type == LYXP_SET_SCNODE_SET));
5164
Michal Vasko6346ece2019-09-24 13:12:53 +02005165 if ((ptr = ly_strnchr(*qname, ':', *qname_len))) {
5166 /* specific module */
5167 pref_len = ptr - *qname;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005168
Michal Vasko6346ece2019-09-24 13:12:53 +02005169 switch (set->format) {
Michal Vasko52927e22020-03-16 17:26:14 +01005170 case LYD_SCHEMA:
Michal Vasko6346ece2019-09-24 13:12:53 +02005171 /* schema, search all local module imports */
5172 mod = lys_module_find_prefix(set->local_mod, *qname, pref_len);
5173 break;
5174 case LYD_JSON:
5175 /* JSON data, search in context */
5176 str = strndup(*qname, pref_len);
Michal Vasko97e76fd2020-05-27 15:22:01 +02005177 mod = ly_ctx_get_module_implemented(set->ctx, str);
Michal Vasko6346ece2019-09-24 13:12:53 +02005178 free(str);
5179 break;
Michal Vasko52927e22020-03-16 17:26:14 +01005180 case LYD_XML:
Michal Vasko6346ece2019-09-24 13:12:53 +02005181 LOGINT_RET(set->ctx);
5182 }
5183
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005184 /* Check for errors and non-implemented modules, as they are not valid */
5185 if (!mod || !mod->implemented) {
Michal Vasko2104e9f2020-03-06 08:23:25 +01005186 if (set->type == LYXP_SET_SCNODE_SET) {
5187 LOGVAL(set->ctx, LY_VLOG_LYSC, set->ctx_scnode, LY_VCODE_XP_INMOD, pref_len, *qname);
5188 } else {
5189 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INMOD, pref_len, *qname);
5190 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005191 return LY_EVALID;
5192 }
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005193
Michal Vasko6346ece2019-09-24 13:12:53 +02005194 *qname += pref_len + 1;
5195 *qname_len -= pref_len + 1;
5196 } else if (((*qname)[0] == '*') && (*qname_len == 1)) {
5197 /* all modules - special case */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005198 mod = NULL;
Michal Vasko6346ece2019-09-24 13:12:53 +02005199 } else {
5200 /* local module */
5201 mod = set->local_mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005202 }
5203
Michal Vasko6346ece2019-09-24 13:12:53 +02005204 *moveto_mod = mod;
5205 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005206}
5207
5208/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02005209 * @brief Move context @p set to the root. Handles absolute path.
5210 * Result is LYXP_SET_NODE_SET.
5211 *
5212 * @param[in,out] set Set to use.
5213 * @param[in] options Xpath options.
5214 */
5215static void
5216moveto_root(struct lyxp_set *set, int options)
5217{
Michal Vasko03ff5a72019-09-11 13:49:33 +02005218 if (!set) {
5219 return;
5220 }
5221
5222 if (options & LYXP_SCNODE_ALL) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005223 set_scnode_clear_ctx(set);
Michal Vaskoecd62de2019-11-13 12:35:11 +01005224 lyxp_set_scnode_insert_node(set, NULL, set->root_type);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005225 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005226 set->type = LYXP_SET_NODE_SET;
5227 set->used = 0;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005228 set_insert_node(set, NULL, 0, set->root_type, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005229 }
5230}
5231
5232/**
Michal Vaskoa1424542019-11-14 16:08:52 +01005233 * @brief Check whether a node has some unresolved "when".
5234 *
5235 * @param[in] node Node to check.
5236 * @return LY_ERR value (LY_EINCOMPLETE if there are some unresolved "when")
5237 */
5238static LY_ERR
5239moveto_when_check(const struct lyd_node *node)
5240{
5241 const struct lysc_node *schema;
5242
5243 if (!node) {
5244 return LY_SUCCESS;
5245 }
5246
5247 schema = node->schema;
5248 do {
5249 if (schema->when && !(node->flags & LYD_WHEN_TRUE)) {
5250 return LY_EINCOMPLETE;
5251 }
5252 schema = schema->parent;
5253 } while (schema && (schema->nodetype & (LYS_CASE | LYS_CHOICE)));
5254
5255 return LY_SUCCESS;
5256}
5257
5258/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02005259 * @brief Check @p node as a part of NameTest processing.
5260 *
5261 * @param[in] node Node to check.
5262 * @param[in] root_type XPath root node type.
Michal Vaskod3678892020-05-21 10:06:58 +02005263 * @param[in] node_name Node name in the dictionary to move to, NULL for any node.
5264 * @param[in] moveto_mod Expected module of the node, NULL for any.
Michal Vasko6346ece2019-09-24 13:12:53 +02005265 * @return LY_ERR (LY_ENOT if node does not match, LY_EINCOMPLETE on unresolved when,
5266 * LY_EINVAL if netither node nor any children match)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005267 */
5268static LY_ERR
5269moveto_node_check(const struct lyd_node *node, enum lyxp_node_type root_type, const char *node_name,
5270 const struct lys_module *moveto_mod)
5271{
5272 /* module check */
5273 if (moveto_mod && (node->schema->module != moveto_mod)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005274 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005275 }
5276
Michal Vasko5c4e5892019-11-14 12:31:38 +01005277 /* context check */
5278 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005279 return LY_EINVAL;
5280 }
5281
5282 /* name check */
Michal Vasko61ac2f62020-05-25 12:39:51 +02005283 if (node_name && strcmp(node_name, "*") && (node->schema->name != node_name)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005284 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005285 }
5286
Michal Vaskoa1424542019-11-14 16:08:52 +01005287 /* when check */
5288 if (moveto_when_check(node)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005289 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01005290 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005291
5292 /* match */
5293 return LY_SUCCESS;
5294}
5295
5296/**
5297 * @brief Check @p node as a part of schema NameTest processing.
5298 *
5299 * @param[in] node Schema node to check.
5300 * @param[in] root_type XPath root node type.
Michal Vaskod3678892020-05-21 10:06:58 +02005301 * @param[in] node_name Node name in the dictionary to move to, NULL for any nodes.
5302 * @param[in] moveto_mod Expected module of the node, NULL for any.
Michal Vasko6346ece2019-09-24 13:12:53 +02005303 * @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 +02005304 */
5305static LY_ERR
5306moveto_scnode_check(const struct lysc_node *node, enum lyxp_node_type root_type, const char *node_name,
Michal Vaskocafad9d2019-11-07 15:20:03 +01005307 const struct lys_module *moveto_mod)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005308{
Michal Vasko03ff5a72019-09-11 13:49:33 +02005309 /* module check */
Michal Vaskod3678892020-05-21 10:06:58 +02005310 if (moveto_mod && (node->module != moveto_mod)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005311 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005312 }
5313
5314 /* context check */
5315 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (node->flags & LYS_CONFIG_R)) {
5316 return LY_EINVAL;
5317 }
5318
5319 /* name check */
Michal Vasko61ac2f62020-05-25 12:39:51 +02005320 if (node_name && strcmp(node_name, "*") && (node->name != node_name)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005321 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005322 }
5323
5324 /* match */
5325 return LY_SUCCESS;
5326}
5327
5328/**
Michal Vaskod3678892020-05-21 10:06:58 +02005329 * @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 +02005330 *
5331 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005332 * @param[in] mod Matching node module, NULL for any.
5333 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005334 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5335 */
5336static LY_ERR
Michal Vaskod3678892020-05-21 10:06:58 +02005337moveto_node(struct lyxp_set *set, const struct lys_module *mod, const char *ncname)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005338{
Michal Vaskof03ed032020-03-04 13:31:44 +01005339 uint32_t i;
Michal Vasko6346ece2019-09-24 13:12:53 +02005340 int replaced;
Michal Vaskod3678892020-05-21 10:06:58 +02005341 const struct lyd_node *siblings, *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005342 LY_ERR rc;
5343
Michal Vaskod3678892020-05-21 10:06:58 +02005344 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005345 return LY_SUCCESS;
5346 }
5347
5348 if (set->type != LYXP_SET_NODE_SET) {
5349 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
5350 return LY_EVALID;
5351 }
5352
Michal Vasko03ff5a72019-09-11 13:49:33 +02005353 for (i = 0; i < set->used; ) {
5354 replaced = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02005355 siblings = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005356
5357 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 +01005358 assert(!set->val.nodes[i].node);
Michal Vaskod3678892020-05-21 10:06:58 +02005359
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005360 /* search in all the trees */
Michal Vaskod3678892020-05-21 10:06:58 +02005361 siblings = set->tree;
Michal Vasko5c4e5892019-11-14 12:31:38 +01005362 } else if (!(set->val.nodes[i].node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA))) {
Michal Vaskod3678892020-05-21 10:06:58 +02005363 /* search in children */
5364 siblings = lyd_node_children(set->val.nodes[i].node);
5365 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005366
Michal Vaskod3678892020-05-21 10:06:58 +02005367 for (sub = siblings; sub; sub = sub->next) {
5368 rc = moveto_node_check(sub, set->root_type, ncname, mod);
5369 if (rc == LY_SUCCESS) {
5370 if (!replaced) {
5371 set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i);
5372 replaced = 1;
5373 } else {
5374 set_insert_node(set, sub, 0, LYXP_NODE_ELEM, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005375 }
Michal Vaskod3678892020-05-21 10:06:58 +02005376 ++i;
5377 } else if (rc == LY_EINCOMPLETE) {
5378 return rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005379 }
5380 }
5381
5382 if (!replaced) {
5383 /* no match */
5384 set_remove_node(set, i);
5385 }
5386 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005387
5388 return LY_SUCCESS;
5389}
5390
5391/**
Michal Vaskod3678892020-05-21 10:06:58 +02005392 * @brief Move context @p set to a node using hashes. Result is LYXP_SET_NODE_SET (or LYXP_SET_EMPTY).
5393 * Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005394 *
5395 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005396 * @param[in] scnode Matching node schema.
5397 * @param[in] list_inst If @p scnode is ::LYS_LIST, the matching list instance. Ignored otherwise.
5398 * @param[in] llist_val If @p scnode is ::LYS_LEAFLIST, the matching leaf-list value. Ignored otherwise.
5399 * @param[in] llist_val_len Length of @p llist_val.
5400 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5401 */
5402static LY_ERR
5403moveto_node_hash(struct lyxp_set *set, const struct lysc_node *scnode, const struct lyd_node *list_inst,
5404 const char *llist_val, uint16_t llist_val_len)
5405{
5406 uint32_t i;
5407 int replaced;
5408 const struct lyd_node *siblings;
5409 struct lyd_node *sub;
5410
5411 assert(scnode && ((scnode->nodetype != LYS_LIST) || list_inst) && ((scnode->nodetype != LYS_LEAFLIST) || llist_val));
5412
5413 if (!set) {
5414 return LY_SUCCESS;
5415 }
5416
5417 if (set->type != LYXP_SET_NODE_SET) {
5418 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
5419 return LY_EVALID;
5420 }
5421
5422 /* context check for all the nodes since we have the schema node */
5423 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) {
5424 lyxp_set_free_content(set);
5425 return LY_SUCCESS;
5426 }
5427
5428 for (i = 0; i < set->used; ) {
5429 replaced = 0;
5430 siblings = NULL;
5431
5432 if ((set->val.nodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.nodes[i].type == LYXP_NODE_ROOT)) {
5433 assert(!set->val.nodes[i].node);
5434
5435 /* search in all the trees */
5436 siblings = set->tree;
5437 } else if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
5438 /* search in children */
5439 siblings = lyd_node_children(set->val.nodes[i].node);
5440 }
5441
5442 /* find the node using hashes */
5443 if (scnode->nodetype == LYS_LIST) {
5444 lyd_find_sibling_first(siblings, list_inst, &sub);
5445 } else {
5446 lyd_find_sibling_val(siblings, scnode, llist_val, llist_val_len, &sub);
5447 }
5448
5449 /* when check */
5450 if (sub && moveto_when_check(sub)) {
5451 return LY_EINCOMPLETE;
5452 }
5453
5454 if (sub) {
5455 /* pos filled later */
5456 if (!replaced) {
5457 set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i);
5458 replaced = 1;
5459 } else {
5460 set_insert_node(set, sub, 0, LYXP_NODE_ELEM, i);
5461 }
5462 ++i;
5463 }
5464
5465 if (!replaced) {
5466 /* no match */
5467 set_remove_node(set, i);
5468 }
5469 }
5470
5471 return LY_SUCCESS;
5472}
5473
5474/**
5475 * @brief Move context @p set to a schema node. Result is LYXP_SET_SCNODE_SET (or LYXP_SET_EMPTY).
5476 *
5477 * @param[in,out] set Set to use.
5478 * @param[in] mod Matching node module, NULL for any.
5479 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005480 * @param[in] options XPath options.
5481 * @return LY_ERR
5482 */
5483static LY_ERR
Michal Vaskod3678892020-05-21 10:06:58 +02005484moveto_scnode(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, int options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005485{
Michal Vaskocafad9d2019-11-07 15:20:03 +01005486 int i, orig_used, idx, temp_ctx = 0, getnext_opts;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005487 uint32_t mod_idx;
Michal Vasko519fd602020-05-26 12:17:39 +02005488 const struct lysc_node *iter, *start_parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005489
Michal Vaskod3678892020-05-21 10:06:58 +02005490 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005491 return LY_SUCCESS;
5492 }
5493
5494 if (set->type != LYXP_SET_SCNODE_SET) {
Michal Vaskof6e51882019-12-16 09:59:45 +01005495 LOGVAL(set->ctx, LY_VLOG_LYSC, set->ctx_scnode, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005496 return LY_EVALID;
5497 }
5498
Michal Vaskocafad9d2019-11-07 15:20:03 +01005499 /* getnext opts */
5500 getnext_opts = LYS_GETNEXT_NOSTATECHECK;
5501 if (options & LYXP_SCNODE_OUTPUT) {
5502 getnext_opts |= LYS_GETNEXT_OUTPUT;
5503 }
5504
Michal Vasko03ff5a72019-09-11 13:49:33 +02005505 orig_used = set->used;
5506 for (i = 0; i < orig_used; ++i) {
5507 if (set->val.scnodes[i].in_ctx != 1) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01005508 if (set->val.scnodes[i].in_ctx != -2) {
5509 continue;
5510 }
5511
5512 /* remember context node */
5513 set->val.scnodes[i].in_ctx = -1;
Michal Vaskoec4df482019-12-16 10:02:18 +01005514 } else {
5515 set->val.scnodes[i].in_ctx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005516 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005517
5518 start_parent = set->val.scnodes[i].scnode;
5519
5520 if ((set->val.scnodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.scnodes[i].type == LYXP_NODE_ROOT)) {
Michal Vaskod3678892020-05-21 10:06:58 +02005521 /* it can actually be in any module, it's all <running>, but we know it's mod (if set),
Michal Vasko03ff5a72019-09-11 13:49:33 +02005522 * so use it directly (root node itself is useless in this case) */
5523 mod_idx = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02005524 while (mod || (mod = (struct lys_module *)ly_ctx_get_module_iter(set->ctx, &mod_idx))) {
Michal Vasko519fd602020-05-26 12:17:39 +02005525 iter = NULL;
Michal Vasko509de4d2019-12-10 14:51:30 +01005526 /* module may not be implemented */
Michal Vasko519fd602020-05-26 12:17:39 +02005527 while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) {
5528 if (!moveto_scnode_check(iter, set->root_type, ncname, mod)) {
5529 idx = lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005530 /* we need to prevent these nodes from being considered in this moveto */
5531 if ((idx < orig_used) && (idx > i)) {
5532 set->val.scnodes[idx].in_ctx = 2;
5533 temp_ctx = 1;
5534 }
5535 }
5536 }
5537
5538 if (!mod_idx) {
Michal Vaskod3678892020-05-21 10:06:58 +02005539 /* mod was specified, we are not going through the whole context */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005540 break;
5541 }
5542 /* next iteration */
Michal Vaskod3678892020-05-21 10:06:58 +02005543 mod = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005544 }
5545
Michal Vasko519fd602020-05-26 12:17:39 +02005546 } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
5547 iter = NULL;
5548 while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) {
5549 if (!moveto_scnode_check(iter, set->root_type, ncname, (mod ? mod : set->local_mod))) {
5550 idx = lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005551 if ((idx < orig_used) && (idx > i)) {
5552 set->val.scnodes[idx].in_ctx = 2;
5553 temp_ctx = 1;
5554 }
5555 }
5556 }
5557 }
5558 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005559
5560 /* correct temporary in_ctx values */
5561 if (temp_ctx) {
5562 for (i = 0; i < orig_used; ++i) {
5563 if (set->val.scnodes[i].in_ctx == 2) {
5564 set->val.scnodes[i].in_ctx = 1;
5565 }
5566 }
5567 }
5568
5569 return LY_SUCCESS;
5570}
5571
5572/**
Michal Vaskod3678892020-05-21 10:06:58 +02005573 * @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 +02005574 * Context position aware.
5575 *
5576 * @param[in] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005577 * @param[in] mod Matching node module, NULL for any.
5578 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005579 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5580 */
5581static LY_ERR
Michal Vaskod3678892020-05-21 10:06:58 +02005582moveto_node_alldesc(struct lyxp_set *set, const struct lys_module *mod, const char *ncname)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005583{
5584 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005585 const struct lyd_node *next, *elem, *start;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005586 struct lyxp_set ret_set;
5587 LY_ERR rc;
5588
Michal Vaskod3678892020-05-21 10:06:58 +02005589 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005590 return LY_SUCCESS;
5591 }
5592
5593 if (set->type != LYXP_SET_NODE_SET) {
5594 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
5595 return LY_EVALID;
5596 }
5597
Michal Vasko9f96a052020-03-10 09:41:45 +01005598 /* replace the original nodes (and throws away all text and meta nodes, root is replaced by a child) */
Michal Vaskod3678892020-05-21 10:06:58 +02005599 rc = moveto_node(set, NULL, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005600 LY_CHECK_RET(rc);
5601
Michal Vasko6346ece2019-09-24 13:12:53 +02005602 /* this loop traverses all the nodes in the set and adds/keeps only those that match qname */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005603 set_init(&ret_set, set);
5604 for (i = 0; i < set->used; ++i) {
5605
5606 /* TREE DFS */
5607 start = set->val.nodes[i].node;
5608 for (elem = next = start; elem; elem = next) {
Michal Vaskod3678892020-05-21 10:06:58 +02005609 rc = moveto_node_check(elem, set->root_type, ncname, mod);
Michal Vasko6346ece2019-09-24 13:12:53 +02005610 if (!rc) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005611 /* add matching node into result set */
5612 set_insert_node(&ret_set, elem, 0, LYXP_NODE_ELEM, ret_set.used);
5613 if (set_dup_node_check(set, elem, LYXP_NODE_ELEM, i)) {
5614 /* the node is a duplicate, we'll process it later in the set */
5615 goto skip_children;
5616 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005617 } else if (rc == LY_EINCOMPLETE) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005618 return rc;
5619 } else if (rc == LY_EINVAL) {
5620 goto skip_children;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005621 }
5622
5623 /* TREE DFS NEXT ELEM */
5624 /* select element for the next run - children first */
Michal Vaskod3678892020-05-21 10:06:58 +02005625 next = lyd_node_children(elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005626 if (!next) {
5627skip_children:
5628 /* no children, so try siblings, but only if it's not the start,
5629 * that is considered to be the root and it's siblings are not traversed */
5630 if (elem != start) {
5631 next = elem->next;
5632 } else {
5633 break;
5634 }
5635 }
5636 while (!next) {
5637 /* no siblings, go back through the parents */
5638 if ((struct lyd_node *)elem->parent == start) {
5639 /* we are done, no next element to process */
5640 break;
5641 }
5642 /* parent is already processed, go to its sibling */
5643 elem = (struct lyd_node *)elem->parent;
5644 next = elem->next;
5645 }
5646 }
5647 }
5648
5649 /* make the temporary set the current one */
5650 ret_set.ctx_pos = set->ctx_pos;
5651 ret_set.ctx_size = set->ctx_size;
Michal Vaskod3678892020-05-21 10:06:58 +02005652 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005653 memcpy(set, &ret_set, sizeof *set);
5654
5655 return LY_SUCCESS;
5656}
5657
5658/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005659 * @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 +02005660 *
5661 * @param[in] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005662 * @param[in] mod Matching node module, NULL for any.
5663 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005664 * @param[in] options XPath options.
5665 * @return LY_ERR
5666 */
5667static LY_ERR
Michal Vaskod3678892020-05-21 10:06:58 +02005668moveto_scnode_alldesc(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, int options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005669{
Michal Vasko6346ece2019-09-24 13:12:53 +02005670 int i, orig_used, idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005671 const struct lysc_node *next, *elem, *start;
Michal Vasko6346ece2019-09-24 13:12:53 +02005672 LY_ERR rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005673
Michal Vaskod3678892020-05-21 10:06:58 +02005674 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005675 return LY_SUCCESS;
5676 }
5677
5678 if (set->type != LYXP_SET_SCNODE_SET) {
Michal Vaskof6e51882019-12-16 09:59:45 +01005679 LOGVAL(set->ctx, LY_VLOG_LYSC, set->ctx_scnode, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005680 return LY_EVALID;
5681 }
5682
Michal Vasko03ff5a72019-09-11 13:49:33 +02005683 orig_used = set->used;
5684 for (i = 0; i < orig_used; ++i) {
5685 if (set->val.scnodes[i].in_ctx != 1) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01005686 if (set->val.scnodes[i].in_ctx != -2) {
5687 continue;
5688 }
5689
5690 /* remember context node */
5691 set->val.scnodes[i].in_ctx = -1;
Michal Vaskoec4df482019-12-16 10:02:18 +01005692 } else {
5693 set->val.scnodes[i].in_ctx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005694 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005695
5696 /* TREE DFS */
5697 start = set->val.scnodes[i].scnode;
5698 for (elem = next = start; elem; elem = next) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005699 if ((elem == start) || (elem->nodetype & (LYS_CHOICE | LYS_CASE))) {
5700 /* schema-only nodes, skip root */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005701 goto next_iter;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005702 }
5703
Michal Vaskod3678892020-05-21 10:06:58 +02005704 rc = moveto_scnode_check(elem, set->root_type, ncname, mod);
Michal Vasko6346ece2019-09-24 13:12:53 +02005705 if (!rc) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01005706 if ((idx = lyxp_set_scnode_dup_node_check(set, elem, LYXP_NODE_ELEM, i)) > -1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005707 set->val.scnodes[idx].in_ctx = 1;
5708 if (idx > i) {
5709 /* we will process it later in the set */
5710 goto skip_children;
5711 }
5712 } else {
Michal Vaskoecd62de2019-11-13 12:35:11 +01005713 lyxp_set_scnode_insert_node(set, elem, LYXP_NODE_ELEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005714 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005715 } else if (rc == LY_EINVAL) {
5716 goto skip_children;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005717 }
5718
5719next_iter:
5720 /* TREE DFS NEXT ELEM */
5721 /* select element for the next run - children first */
5722 next = lysc_node_children(elem, options & LYXP_SCNODE_OUTPUT ? LYS_CONFIG_R : LYS_CONFIG_W);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005723 if (!next) {
5724skip_children:
5725 /* no children, so try siblings, but only if it's not the start,
5726 * that is considered to be the root and it's siblings are not traversed */
5727 if (elem != start) {
5728 next = elem->next;
5729 } else {
5730 break;
5731 }
5732 }
5733 while (!next) {
5734 /* no siblings, go back through the parents */
5735 if (elem->parent == start) {
5736 /* we are done, no next element to process */
5737 break;
5738 }
5739 /* parent is already processed, go to its sibling */
5740 elem = elem->parent;
5741 next = elem->next;
5742 }
5743 }
5744 }
5745
5746 return LY_SUCCESS;
5747}
5748
5749/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005750 * @brief Move context @p set to an attribute. Result is LYXP_SET_NODE_SET.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005751 * Indirectly context position aware.
5752 *
5753 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005754 * @param[in] mod Matching metadata module, NULL for any.
5755 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005756 * @return LY_ERR
5757 */
5758static LY_ERR
Michal Vaskod3678892020-05-21 10:06:58 +02005759moveto_attr(struct lyxp_set *set, const struct lys_module *mod, const char *ncname)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005760{
5761 uint32_t i;
Michal Vaskod3678892020-05-21 10:06:58 +02005762 int replaced;
Michal Vasko9f96a052020-03-10 09:41:45 +01005763 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005764
Michal Vaskod3678892020-05-21 10:06:58 +02005765 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005766 return LY_SUCCESS;
5767 }
5768
5769 if (set->type != LYXP_SET_NODE_SET) {
5770 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
5771 return LY_EVALID;
5772 }
5773
Michal Vasko03ff5a72019-09-11 13:49:33 +02005774 for (i = 0; i < set->used; ) {
5775 replaced = 0;
5776
5777 /* only attributes of an elem (not dummy) can be in the result, skip all the rest;
5778 * our attributes are always qualified */
Michal Vasko5c4e5892019-11-14 12:31:38 +01005779 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01005780 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005781
5782 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02005783 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005784 continue;
5785 }
5786
Michal Vaskod3678892020-05-21 10:06:58 +02005787 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005788 /* match */
5789 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01005790 set->val.meta[i].meta = sub;
5791 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005792 /* pos does not change */
5793 replaced = 1;
5794 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01005795 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 +02005796 }
5797 ++i;
5798 }
5799 }
5800 }
5801
5802 if (!replaced) {
5803 /* no match */
5804 set_remove_node(set, i);
5805 }
5806 }
5807
5808 return LY_SUCCESS;
5809}
5810
5811/**
5812 * @brief Move context @p set1 to union with @p set2. @p set2 is emptied afterwards.
Michal Vasko61ac2f62020-05-25 12:39:51 +02005813 * Result is LYXP_SET_NODE_SET. Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005814 *
5815 * @param[in,out] set1 Set to use for the result.
5816 * @param[in] set2 Set that is copied to @p set1.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005817 * @return LY_ERR
5818 */
5819static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005820moveto_union(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005821{
5822 LY_ERR rc;
5823
Michal Vaskod3678892020-05-21 10:06:58 +02005824 if ((set1->type != LYXP_SET_NODE_SET) || (set2->type != LYXP_SET_NODE_SET)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005825 LOGVAL(set1->ctx, LY_VLOG_LYD, set1->ctx_node, LY_VCODE_XP_INOP_2, "union", print_set_type(set1), print_set_type(set2));
5826 return LY_EVALID;
5827 }
5828
5829 /* set2 is empty or both set1 and set2 */
Michal Vaskod3678892020-05-21 10:06:58 +02005830 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005831 return LY_SUCCESS;
5832 }
5833
Michal Vaskod3678892020-05-21 10:06:58 +02005834 if (!set1->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005835 memcpy(set1, set2, sizeof *set1);
5836 /* dynamic memory belongs to set1 now, do not free */
Michal Vaskod3678892020-05-21 10:06:58 +02005837 memset(set2, 0, sizeof *set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005838 return LY_SUCCESS;
5839 }
5840
5841 /* we assume sets are sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005842 assert(!set_sort(set1) && !set_sort(set2));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005843
5844 /* sort, remove duplicates */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005845 rc = set_sorted_merge(set1, set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005846 LY_CHECK_RET(rc);
5847
5848 /* final set must be sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005849 assert(!set_sort(set1));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005850
5851 return LY_SUCCESS;
5852}
5853
5854/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005855 * @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 +02005856 * Context position aware.
5857 *
5858 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005859 * @param[in] mod Matching metadata module, NULL for any.
5860 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005861 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5862 */
5863static int
Michal Vaskod3678892020-05-21 10:06:58 +02005864moveto_attr_alldesc(struct lyxp_set *set, const struct lys_module *mod, const char *ncname)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005865{
5866 uint32_t i;
Michal Vaskod3678892020-05-21 10:06:58 +02005867 int replaced;
Michal Vasko9f96a052020-03-10 09:41:45 +01005868 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005869 struct lyxp_set *set_all_desc = NULL;
5870 LY_ERR rc;
5871
Michal Vaskod3678892020-05-21 10:06:58 +02005872 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005873 return LY_SUCCESS;
5874 }
5875
5876 if (set->type != LYXP_SET_NODE_SET) {
5877 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
5878 return LY_EVALID;
5879 }
5880
Michal Vasko03ff5a72019-09-11 13:49:33 +02005881 /* can be optimized similarly to moveto_node_alldesc() and save considerable amount of memory,
5882 * but it likely won't be used much, so it's a waste of time */
5883 /* copy the context */
5884 set_all_desc = set_copy(set);
5885 /* get all descendant nodes (the original context nodes are removed) */
Michal Vaskod3678892020-05-21 10:06:58 +02005886 rc = moveto_node_alldesc(set_all_desc, NULL, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005887 if (rc != LY_SUCCESS) {
5888 lyxp_set_free(set_all_desc);
5889 return rc;
5890 }
5891 /* prepend the original context nodes */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005892 rc = moveto_union(set, set_all_desc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005893 if (rc != LY_SUCCESS) {
5894 lyxp_set_free(set_all_desc);
5895 return rc;
5896 }
5897 lyxp_set_free(set_all_desc);
5898
Michal Vasko03ff5a72019-09-11 13:49:33 +02005899 for (i = 0; i < set->used; ) {
5900 replaced = 0;
5901
5902 /* only attributes of an elem can be in the result, skip all the rest,
5903 * we have all attributes qualified in lyd tree */
5904 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01005905 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005906 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02005907 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005908 continue;
5909 }
5910
Michal Vaskod3678892020-05-21 10:06:58 +02005911 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005912 /* match */
5913 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01005914 set->val.meta[i].meta = sub;
5915 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005916 /* pos does not change */
5917 replaced = 1;
5918 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01005919 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 +02005920 }
5921 ++i;
5922 }
5923 }
5924 }
5925
5926 if (!replaced) {
5927 /* no match */
5928 set_remove_node(set, i);
5929 }
5930 }
5931
5932 return LY_SUCCESS;
5933}
5934
5935/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005936 * @brief Move context @p set to self and al chilren, recursively. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET.
5937 * Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005938 *
5939 * @param[in] parent Current parent.
5940 * @param[in] parent_pos Position of @p parent.
5941 * @param[in] parent_type Node type of @p parent.
5942 * @param[in,out] to_set Set to use.
5943 * @param[in] dup_check_set Set for checking duplicities.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005944 * @param[in] options XPath options.
5945 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5946 */
5947static LY_ERR
5948moveto_self_add_children_r(const struct lyd_node *parent, uint32_t parent_pos, enum lyxp_node_type parent_type,
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005949 struct lyxp_set *to_set, const struct lyxp_set *dup_check_set, int options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005950{
Michal Vasko61ac2f62020-05-25 12:39:51 +02005951 const struct lyd_node *iter, *first;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005952 LY_ERR rc;
5953
5954 switch (parent_type) {
5955 case LYXP_NODE_ROOT:
5956 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko61ac2f62020-05-25 12:39:51 +02005957 assert(!parent);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005958
Michal Vasko61ac2f62020-05-25 12:39:51 +02005959 /* add all top-level nodes as elements */
5960 first = to_set->tree;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005961 break;
5962 case LYXP_NODE_ELEM:
Michal Vasko61ac2f62020-05-25 12:39:51 +02005963 /* add just the text node of this term element node */
5964 if (parent->schema->nodetype & (LYD_NODE_TERM | LYD_NODE_ANY)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005965 if (!set_dup_node_check(dup_check_set, parent, LYXP_NODE_TEXT, -1)) {
5966 set_insert_node(to_set, parent, parent_pos, LYXP_NODE_TEXT, to_set->used);
5967 }
Michal Vasko61ac2f62020-05-25 12:39:51 +02005968 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005969 }
Michal Vasko61ac2f62020-05-25 12:39:51 +02005970
5971 /* add all the children of this node */
5972 first = lyd_node_children(parent);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005973 break;
5974 default:
5975 LOGINT_RET(parent->schema->module->ctx);
5976 }
5977
Michal Vasko61ac2f62020-05-25 12:39:51 +02005978 /* add all top-level nodes as elements */
5979 LY_LIST_FOR(first, iter) {
5980 /* context check */
5981 if ((parent_type == LYXP_NODE_ROOT_CONFIG) && (iter->schema->flags & LYS_CONFIG_R)) {
5982 continue;
5983 }
5984
5985 /* when check */
5986 if (moveto_when_check(iter)) {
5987 return LY_EINCOMPLETE;
5988 }
5989
5990 if (!set_dup_node_check(dup_check_set, iter, LYXP_NODE_ELEM, -1)) {
5991 set_insert_node(to_set, iter, 0, LYXP_NODE_ELEM, to_set->used);
5992
5993 /* also add all the children of this node, recursively */
5994 rc = moveto_self_add_children_r(iter, 0, LYXP_NODE_ELEM, to_set, dup_check_set, options);
5995 LY_CHECK_RET(rc);
5996 }
5997 }
5998
Michal Vasko03ff5a72019-09-11 13:49:33 +02005999 return LY_SUCCESS;
6000}
6001
6002/**
6003 * @brief Move context @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET
6004 * (or LYXP_SET_EMPTY). Context position aware.
6005 *
6006 * @param[in,out] set Set to use.
6007 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6008 * @param[in] options XPath options.
6009 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6010 */
6011static LY_ERR
6012moveto_self(struct lyxp_set *set, int all_desc, int options)
6013{
6014 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006015 struct lyxp_set ret_set;
6016 LY_ERR rc;
6017
Michal Vaskod3678892020-05-21 10:06:58 +02006018 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006019 return LY_SUCCESS;
6020 }
6021
6022 if (set->type != LYXP_SET_NODE_SET) {
6023 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
6024 return LY_EVALID;
6025 }
6026
6027 /* nothing to do */
6028 if (!all_desc) {
6029 return LY_SUCCESS;
6030 }
6031
Michal Vasko03ff5a72019-09-11 13:49:33 +02006032 /* add all the children, they get added recursively */
6033 set_init(&ret_set, set);
6034 for (i = 0; i < set->used; ++i) {
6035 /* copy the current node to tmp */
6036 set_insert_node(&ret_set, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, ret_set.used);
6037
6038 /* do not touch attributes and text nodes */
Michal Vasko9f96a052020-03-10 09:41:45 +01006039 if ((set->val.nodes[i].type == LYXP_NODE_TEXT) || (set->val.nodes[i].type == LYXP_NODE_META)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006040 continue;
6041 }
6042
Michal Vasko03ff5a72019-09-11 13:49:33 +02006043 /* add all the children */
6044 rc = moveto_self_add_children_r(set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, &ret_set,
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006045 set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006046 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006047 lyxp_set_free_content(&ret_set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006048 return rc;
6049 }
6050 }
6051
6052 /* use the temporary set as the current one */
6053 ret_set.ctx_pos = set->ctx_pos;
6054 ret_set.ctx_size = set->ctx_size;
Michal Vaskod3678892020-05-21 10:06:58 +02006055 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006056 memcpy(set, &ret_set, sizeof *set);
6057
6058 return LY_SUCCESS;
6059}
6060
6061/**
6062 * @brief Move context schema @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_SCNODE_SET
6063 * (or LYXP_SET_EMPTY).
6064 *
6065 * @param[in,out] set Set to use.
6066 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6067 * @param[in] options XPath options.
6068 * @return LY_ERR
6069 */
6070static LY_ERR
6071moveto_scnode_self(struct lyxp_set *set, int all_desc, int options)
6072{
Michal Vasko519fd602020-05-26 12:17:39 +02006073 int getnext_opts;
6074 uint32_t i, mod_idx;
6075 const struct lysc_node *iter, *start_parent;
6076 const struct lys_module *mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006077
Michal Vaskod3678892020-05-21 10:06:58 +02006078 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006079 return LY_SUCCESS;
6080 }
6081
6082 if (set->type != LYXP_SET_SCNODE_SET) {
Michal Vaskof6e51882019-12-16 09:59:45 +01006083 LOGVAL(set->ctx, LY_VLOG_LYSC, set->ctx_scnode, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006084 return LY_EVALID;
6085 }
6086
6087 /* nothing to do */
6088 if (!all_desc) {
6089 return LY_SUCCESS;
6090 }
6091
Michal Vasko519fd602020-05-26 12:17:39 +02006092 /* getnext opts */
6093 getnext_opts = LYS_GETNEXT_NOSTATECHECK;
6094 if (options & LYXP_SCNODE_OUTPUT) {
6095 getnext_opts |= LYS_GETNEXT_OUTPUT;
6096 }
6097
6098 /* add all the children, recursively as they are being added into the same set */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006099 for (i = 0; i < set->used; ++i) {
6100 if (set->val.scnodes[i].in_ctx != 1) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006101 if (set->val.scnodes[i].in_ctx != -2) {
6102 continue;
6103 }
6104
Michal Vasko519fd602020-05-26 12:17:39 +02006105 /* remember context node */
6106 set->val.scnodes[i].in_ctx = -1;
6107 } else {
6108 set->val.scnodes[i].in_ctx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006109 }
6110
Michal Vasko519fd602020-05-26 12:17:39 +02006111 start_parent = set->val.scnodes[i].scnode;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006112
Michal Vasko519fd602020-05-26 12:17:39 +02006113 if ((set->val.scnodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.scnodes[i].type == LYXP_NODE_ROOT)) {
6114 /* it can actually be in any module, it's all <running> */
6115 mod_idx = 0;
6116 while ((mod = (struct lys_module *)ly_ctx_get_module_iter(set->ctx, &mod_idx))) {
6117 iter = NULL;
6118 /* module may not be implemented */
6119 while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) {
6120 /* context check */
6121 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) {
6122 continue;
6123 }
6124
6125 lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM);
6126 /* throw away the insert index, we want to consider that node again, recursively */
6127 }
6128 }
6129
6130 } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
6131 iter = NULL;
6132 while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006133 /* context check */
Michal Vasko519fd602020-05-26 12:17:39 +02006134 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006135 continue;
6136 }
6137
Michal Vasko519fd602020-05-26 12:17:39 +02006138 lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006139 }
6140 }
6141 }
6142
6143 return LY_SUCCESS;
6144}
6145
6146/**
6147 * @brief Move context @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_NODE_SET
6148 * (or LYXP_SET_EMPTY). Context position aware.
6149 *
6150 * @param[in] set Set to use.
6151 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6152 * @param[in] options XPath options.
6153 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6154 */
6155static LY_ERR
6156moveto_parent(struct lyxp_set *set, int all_desc, int options)
6157{
6158 LY_ERR rc;
6159 uint32_t i;
6160 struct lyd_node *node, *new_node;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006161 enum lyxp_node_type new_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006162
Michal Vaskod3678892020-05-21 10:06:58 +02006163 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006164 return LY_SUCCESS;
6165 }
6166
6167 if (set->type != LYXP_SET_NODE_SET) {
6168 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
6169 return LY_EVALID;
6170 }
6171
6172 if (all_desc) {
6173 /* <path>//.. == <path>//./.. */
6174 rc = moveto_self(set, 1, options);
6175 LY_CHECK_RET(rc);
6176 }
6177
Michal Vasko57eab132019-09-24 11:46:26 +02006178 for (i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006179 node = set->val.nodes[i].node;
6180
6181 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
6182 new_node = (struct lyd_node *)node->parent;
6183 } else if (set->val.nodes[i].type == LYXP_NODE_TEXT) {
6184 new_node = node;
Michal Vasko9f96a052020-03-10 09:41:45 +01006185 } else if (set->val.nodes[i].type == LYXP_NODE_META) {
6186 new_node = set->val.meta[i].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006187 if (!new_node) {
6188 LOGINT_RET(set->ctx);
6189 }
6190 } else {
6191 /* root does not have a parent */
Michal Vasko2caefc12019-11-14 16:07:56 +01006192 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006193 continue;
6194 }
6195
Michal Vaskoa1424542019-11-14 16:08:52 +01006196 /* when check */
6197 if (moveto_when_check(new_node)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006198 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01006199 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006200
6201 /* node already there can also be the root */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006202 if (!new_node) {
6203 new_type = set->root_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006204
6205 /* node has a standard parent (it can equal the root, it's not the root yet since they are fake) */
6206 } else {
6207 new_type = LYXP_NODE_ELEM;
6208 }
6209
Michal Vasko03ff5a72019-09-11 13:49:33 +02006210 if (set_dup_node_check(set, new_node, new_type, -1)) {
Michal Vasko2caefc12019-11-14 16:07:56 +01006211 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006212 } else {
6213 set_replace_node(set, new_node, 0, new_type, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006214 }
6215 }
6216
Michal Vasko2caefc12019-11-14 16:07:56 +01006217 set_remove_nodes_none(set);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006218 assert(!set_sort(set) && !set_sorted_dup_node_clean(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006219
6220 return LY_SUCCESS;
6221}
6222
6223/**
6224 * @brief Move context schema @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_SCNODE_SET
6225 * (or LYXP_SET_EMPTY).
6226 *
6227 * @param[in] set Set to use.
6228 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6229 * @param[in] options XPath options.
6230 * @return LY_ERR
6231 */
6232static LY_ERR
6233moveto_scnode_parent(struct lyxp_set *set, int all_desc, int options)
6234{
6235 int idx, i, orig_used, temp_ctx = 0;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006236 const struct lysc_node *node, *new_node;
6237 enum lyxp_node_type new_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006238 LY_ERR rc;
6239
Michal Vaskod3678892020-05-21 10:06:58 +02006240 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006241 return LY_SUCCESS;
6242 }
6243
6244 if (set->type != LYXP_SET_SCNODE_SET) {
Michal Vaskof6e51882019-12-16 09:59:45 +01006245 LOGVAL(set->ctx, LY_VLOG_LYSC, set->ctx_scnode, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006246 return LY_EVALID;
6247 }
6248
6249 if (all_desc) {
6250 /* <path>//.. == <path>//./.. */
6251 rc = moveto_scnode_self(set, 1, options);
6252 LY_CHECK_RET(rc);
6253 }
6254
Michal Vasko03ff5a72019-09-11 13:49:33 +02006255 orig_used = set->used;
6256 for (i = 0; i < orig_used; ++i) {
6257 if (set->val.scnodes[i].in_ctx != 1) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006258 if (set->val.scnodes[i].in_ctx != -2) {
6259 continue;
6260 }
6261
6262 /* remember context node */
6263 set->val.scnodes[i].in_ctx = -1;
Michal Vaskoec4df482019-12-16 10:02:18 +01006264 } else {
6265 set->val.scnodes[i].in_ctx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006266 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006267
6268 node = set->val.scnodes[i].scnode;
6269
6270 if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
Michal Vaskod3678892020-05-21 10:06:58 +02006271 new_node = lysc_data_parent(node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006272 } else {
6273 /* root does not have a parent */
6274 continue;
6275 }
6276
Michal Vasko03ff5a72019-09-11 13:49:33 +02006277 /* node has no parent */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006278 if (!new_node) {
6279 new_type = set->root_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006280
6281 /* node has a standard parent (it can equal the root, it's not the root yet since they are fake) */
6282 } else {
6283 new_type = LYXP_NODE_ELEM;
6284 }
6285
Michal Vaskoecd62de2019-11-13 12:35:11 +01006286 idx = lyxp_set_scnode_insert_node(set, new_node, new_type);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006287 if ((idx < orig_used) && (idx > i)) {
6288 set->val.scnodes[idx].in_ctx = 2;
6289 temp_ctx = 1;
6290 }
6291 }
6292
6293 if (temp_ctx) {
6294 for (i = 0; i < orig_used; ++i) {
6295 if (set->val.scnodes[i].in_ctx == 2) {
6296 set->val.scnodes[i].in_ctx = 1;
6297 }
6298 }
6299 }
6300
6301 return LY_SUCCESS;
6302}
6303
6304/**
6305 * @brief Move context @p set to the result of a comparison. Handles '=', '!=', '<=', '<', '>=', or '>'.
6306 * Result is LYXP_SET_BOOLEAN. Indirectly context position aware.
6307 *
6308 * @param[in,out] set1 Set to use for the result.
6309 * @param[in] set2 Set acting as the second operand for @p op.
6310 * @param[in] op Comparison operator to process.
6311 * @param[in] options XPath options.
6312 * @return LY_ERR
6313 */
6314static LY_ERR
6315moveto_op_comp(struct lyxp_set *set1, struct lyxp_set *set2, const char *op, int options)
6316{
6317 /*
6318 * NODE SET + NODE SET = NODE SET + STRING /(1 NODE SET) 2 STRING
6319 * NODE SET + STRING = STRING + STRING /1 STRING (2 STRING)
6320 * NODE SET + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6321 * NODE SET + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6322 * STRING + NODE SET = STRING + STRING /(1 STRING) 2 STRING
6323 * NUMBER + NODE SET = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6324 * BOOLEAN + NODE SET = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6325 *
6326 * '=' or '!='
6327 * BOOLEAN + BOOLEAN
6328 * BOOLEAN + STRING = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6329 * BOOLEAN + NUMBER = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6330 * STRING + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6331 * NUMBER + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6332 * NUMBER + NUMBER
6333 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6334 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6335 * STRING + STRING
6336 *
6337 * '<=', '<', '>=', '>'
6338 * NUMBER + NUMBER
6339 * BOOLEAN + BOOLEAN = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6340 * BOOLEAN + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6341 * BOOLEAN + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6342 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6343 * STRING + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6344 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6345 * NUMBER + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6346 * STRING + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6347 */
6348 struct lyxp_set iter1, iter2;
6349 int result;
6350 int64_t i;
6351 LY_ERR rc;
6352
Michal Vaskod3678892020-05-21 10:06:58 +02006353 iter1.type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006354
6355 /* iterative evaluation with node-sets */
6356 if ((set1->type == LYXP_SET_NODE_SET) || (set2->type == LYXP_SET_NODE_SET)) {
6357 if (set1->type == LYXP_SET_NODE_SET) {
6358 for (i = 0; i < set1->used; ++i) {
6359 switch (set2->type) {
6360 case LYXP_SET_NUMBER:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006361 rc = set_comp_cast(&iter1, set1, LYXP_SET_NUMBER, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006362 break;
6363 case LYXP_SET_BOOLEAN:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006364 rc = set_comp_cast(&iter1, set1, LYXP_SET_BOOLEAN, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006365 break;
6366 default:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006367 rc = set_comp_cast(&iter1, set1, LYXP_SET_STRING, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006368 break;
6369 }
6370 LY_CHECK_RET(rc);
6371
6372 rc = moveto_op_comp(&iter1, set2, op, options);
6373 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006374 lyxp_set_free_content(&iter1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006375 return rc;
6376 }
6377
6378 /* lazy evaluation until true */
6379 if (iter1.val.bool) {
6380 set_fill_boolean(set1, 1);
6381 return LY_SUCCESS;
6382 }
6383 }
6384 } else {
6385 for (i = 0; i < set2->used; ++i) {
6386 switch (set1->type) {
6387 case LYXP_SET_NUMBER:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006388 rc = set_comp_cast(&iter2, set2, LYXP_SET_NUMBER, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006389 break;
6390 case LYXP_SET_BOOLEAN:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006391 rc = set_comp_cast(&iter2, set2, LYXP_SET_BOOLEAN, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006392 break;
6393 default:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006394 rc = set_comp_cast(&iter2, set2, LYXP_SET_STRING, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006395 break;
6396 }
6397 LY_CHECK_RET(rc);
6398
6399 set_fill_set(&iter1, set1);
6400
6401 rc = moveto_op_comp(&iter1, &iter2, op, options);
6402 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006403 lyxp_set_free_content(&iter1);
6404 lyxp_set_free_content(&iter2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006405 return rc;
6406 }
Michal Vaskod3678892020-05-21 10:06:58 +02006407 lyxp_set_free_content(&iter2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006408
6409 /* lazy evaluation until true */
6410 if (iter1.val.bool) {
6411 set_fill_boolean(set1, 1);
6412 return LY_SUCCESS;
6413 }
6414 }
6415 }
6416
6417 /* false for all nodes */
6418 set_fill_boolean(set1, 0);
6419 return LY_SUCCESS;
6420 }
6421
6422 /* first convert properly */
6423 if ((op[0] == '=') || (op[0] == '!')) {
6424 if ((set1->type == LYXP_SET_BOOLEAN) || (set2->type == LYXP_SET_BOOLEAN)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006425 lyxp_set_cast(set1, LYXP_SET_BOOLEAN);
6426 lyxp_set_cast(set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006427 } else if ((set1->type == LYXP_SET_NUMBER) || (set2->type == LYXP_SET_NUMBER)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006428 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006429 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006430 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006431 LY_CHECK_RET(rc);
6432 } /* else we have 2 strings */
6433 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006434 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006435 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006436 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006437 LY_CHECK_RET(rc);
6438 }
6439
6440 assert(set1->type == set2->type);
6441
6442 /* compute result */
6443 if (op[0] == '=') {
6444 if (set1->type == LYXP_SET_BOOLEAN) {
6445 result = (set1->val.bool == set2->val.bool);
6446 } else if (set1->type == LYXP_SET_NUMBER) {
6447 result = (set1->val.num == set2->val.num);
6448 } else {
6449 assert(set1->type == LYXP_SET_STRING);
Michal Vaskoac6c72f2019-11-14 16:09:34 +01006450 result = !strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006451 }
6452 } else if (op[0] == '!') {
6453 if (set1->type == LYXP_SET_BOOLEAN) {
6454 result = (set1->val.bool != set2->val.bool);
6455 } else if (set1->type == LYXP_SET_NUMBER) {
6456 result = (set1->val.num != set2->val.num);
6457 } else {
6458 assert(set1->type == LYXP_SET_STRING);
Michal Vaskoac6c72f2019-11-14 16:09:34 +01006459 result = !strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006460 }
6461 } else {
6462 assert(set1->type == LYXP_SET_NUMBER);
6463 if (op[0] == '<') {
6464 if (op[1] == '=') {
6465 result = (set1->val.num <= set2->val.num);
6466 } else {
6467 result = (set1->val.num < set2->val.num);
6468 }
6469 } else {
6470 if (op[1] == '=') {
6471 result = (set1->val.num >= set2->val.num);
6472 } else {
6473 result = (set1->val.num > set2->val.num);
6474 }
6475 }
6476 }
6477
6478 /* assign result */
6479 if (result) {
6480 set_fill_boolean(set1, 1);
6481 } else {
6482 set_fill_boolean(set1, 0);
6483 }
6484
6485 return LY_SUCCESS;
6486}
6487
6488/**
6489 * @brief Move context @p set to the result of a basic operation. Handles '+', '-', unary '-', '*', 'div',
6490 * or 'mod'. Result is LYXP_SET_NUMBER. Indirectly context position aware.
6491 *
6492 * @param[in,out] set1 Set to use for the result.
6493 * @param[in] set2 Set acting as the second operand for @p op.
6494 * @param[in] op Operator to process.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006495 * @return LY_ERR
6496 */
6497static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006498moveto_op_math(struct lyxp_set *set1, struct lyxp_set *set2, const char *op)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006499{
6500 LY_ERR rc;
6501
6502 /* unary '-' */
6503 if (!set2 && (op[0] == '-')) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006504 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006505 LY_CHECK_RET(rc);
6506 set1->val.num *= -1;
6507 lyxp_set_free(set2);
6508 return LY_SUCCESS;
6509 }
6510
6511 assert(set1 && set2);
6512
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006513 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006514 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006515 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006516 LY_CHECK_RET(rc);
6517
6518 switch (op[0]) {
6519 /* '+' */
6520 case '+':
6521 set1->val.num += set2->val.num;
6522 break;
6523
6524 /* '-' */
6525 case '-':
6526 set1->val.num -= set2->val.num;
6527 break;
6528
6529 /* '*' */
6530 case '*':
6531 set1->val.num *= set2->val.num;
6532 break;
6533
6534 /* 'div' */
6535 case 'd':
6536 set1->val.num /= set2->val.num;
6537 break;
6538
6539 /* 'mod' */
6540 case 'm':
6541 set1->val.num = ((long long)set1->val.num) % ((long long)set2->val.num);
6542 break;
6543
6544 default:
6545 LOGINT_RET(set1->ctx);
6546 }
6547
6548 return LY_SUCCESS;
6549}
6550
6551/*
6552 * eval functions
6553 *
6554 * They execute a parsed XPath expression on some data subtree.
6555 */
6556
6557/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02006558 * @brief Evaluate Predicate. Logs directly on error.
6559 *
Michal Vaskod3678892020-05-21 10:06:58 +02006560 * [9] Predicate ::= '[' Expr ']'
Michal Vasko03ff5a72019-09-11 13:49:33 +02006561 *
6562 * @param[in] exp Parsed XPath expression.
6563 * @param[in] exp_idx Position in the expression @p exp.
6564 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
6565 * @param[in] options XPath options.
6566 * @param[in] parent_pos_pred Whether parent predicate was a positional one.
6567 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6568 */
6569static LY_ERR
6570eval_predicate(struct lyxp_expr *exp, uint16_t *exp_idx, struct lyxp_set *set, int options, int parent_pos_pred)
6571{
6572 LY_ERR rc;
Michal Vasko57eab132019-09-24 11:46:26 +02006573 uint16_t i, orig_exp;
Michal Vasko5c4e5892019-11-14 12:31:38 +01006574 uint32_t orig_pos, orig_size;
6575 int32_t pred_in_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006576 struct lyxp_set set2;
6577 struct lyd_node *orig_parent;
6578
6579 /* '[' */
6580 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko24cddf82020-06-01 08:17:01 +02006581 lyxp_print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006582 ++(*exp_idx);
6583
6584 if (!set) {
6585only_parse:
6586 rc = eval_expr_select(exp, exp_idx, 0, NULL, options);
6587 LY_CHECK_RET(rc);
6588 } else if (set->type == LYXP_SET_NODE_SET) {
6589 /* 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 +01006590 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006591
6592 /* empty set, nothing to evaluate */
6593 if (!set->used) {
6594 goto only_parse;
6595 }
6596
6597 orig_exp = *exp_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006598 orig_pos = 0;
6599 orig_size = set->used;
6600 orig_parent = NULL;
Michal Vasko39dbf352020-05-21 10:08:59 +02006601 for (i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006602 set_init(&set2, set);
6603 set_insert_node(&set2, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, 0);
6604 /* remember the node context position for position() and context size for last(),
6605 * predicates should always be evaluated with respect to the child axis (since we do
6606 * not support explicit axes) so we assign positions based on their parents */
6607 if (parent_pos_pred && ((struct lyd_node *)set->val.nodes[i].node->parent != orig_parent)) {
6608 orig_parent = (struct lyd_node *)set->val.nodes[i].node->parent;
6609 orig_pos = 1;
6610 } else {
6611 ++orig_pos;
6612 }
6613
6614 set2.ctx_pos = orig_pos;
6615 set2.ctx_size = orig_size;
6616 *exp_idx = orig_exp;
6617
6618 rc = eval_expr_select(exp, exp_idx, 0, &set2, options);
6619 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006620 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006621 return rc;
6622 }
6623
6624 /* number is a position */
6625 if (set2.type == LYXP_SET_NUMBER) {
6626 if ((long long)set2.val.num == orig_pos) {
6627 set2.val.num = 1;
6628 } else {
6629 set2.val.num = 0;
6630 }
6631 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006632 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006633
6634 /* predicate satisfied or not? */
Michal Vasko57eab132019-09-24 11:46:26 +02006635 if (!set2.val.bool) {
Michal Vasko2caefc12019-11-14 16:07:56 +01006636 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006637 }
6638 }
Michal Vasko2caefc12019-11-14 16:07:56 +01006639 set_remove_nodes_none(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006640
6641 } else if (set->type == LYXP_SET_SCNODE_SET) {
6642 for (i = 0; i < set->used; ++i) {
6643 if (set->val.scnodes[i].in_ctx == 1) {
6644 /* there is a currently-valid node */
6645 break;
6646 }
6647 }
6648 /* empty set, nothing to evaluate */
6649 if (i == set->used) {
6650 goto only_parse;
6651 }
6652
6653 orig_exp = *exp_idx;
6654
Michal Vasko03ff5a72019-09-11 13:49:33 +02006655 /* set special in_ctx to all the valid snodes */
6656 pred_in_ctx = set_scnode_new_in_ctx(set);
6657
6658 /* use the valid snodes one-by-one */
6659 for (i = 0; i < set->used; ++i) {
6660 if (set->val.scnodes[i].in_ctx != pred_in_ctx) {
6661 continue;
6662 }
6663 set->val.scnodes[i].in_ctx = 1;
6664
6665 *exp_idx = orig_exp;
6666
6667 rc = eval_expr_select(exp, exp_idx, 0, set, options);
6668 LY_CHECK_RET(rc);
6669
6670 set->val.scnodes[i].in_ctx = pred_in_ctx;
6671 }
6672
6673 /* restore the state as it was before the predicate */
6674 for (i = 0; i < set->used; ++i) {
6675 if (set->val.scnodes[i].in_ctx == 1) {
6676 set->val.scnodes[i].in_ctx = 0;
6677 } else if (set->val.scnodes[i].in_ctx == pred_in_ctx) {
6678 set->val.scnodes[i].in_ctx = 1;
6679 }
6680 }
6681
6682 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02006683 set2.type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006684 set_fill_set(&set2, set);
6685
6686 rc = eval_expr_select(exp, exp_idx, 0, &set2, options);
6687 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006688 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006689 return rc;
6690 }
6691
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006692 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006693 if (!set2.val.bool) {
Michal Vaskod3678892020-05-21 10:06:58 +02006694 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006695 }
Michal Vaskod3678892020-05-21 10:06:58 +02006696 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006697 }
6698
6699 /* ']' */
6700 assert(exp->tokens[*exp_idx] == LYXP_TOKEN_BRACK2);
6701 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko24cddf82020-06-01 08:17:01 +02006702 lyxp_print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006703 ++(*exp_idx);
6704
6705 return LY_SUCCESS;
6706}
6707
6708/**
Michal Vaskod3678892020-05-21 10:06:58 +02006709 * @brief Evaluate Literal. Logs directly on error.
6710 *
6711 * @param[in] exp Parsed XPath expression.
6712 * @param[in] exp_idx Position in the expression @p exp.
6713 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
6714 */
6715static void
6716eval_literal(struct lyxp_expr *exp, uint16_t *exp_idx, struct lyxp_set *set)
6717{
6718 if (set) {
6719 if (exp->tok_len[*exp_idx] == 2) {
6720 set_fill_string(set, "", 0);
6721 } else {
6722 set_fill_string(set, &exp->expr[exp->tok_pos[*exp_idx] + 1], exp->tok_len[*exp_idx] - 2);
6723 }
6724 }
6725 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko24cddf82020-06-01 08:17:01 +02006726 lyxp_print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
Michal Vaskod3678892020-05-21 10:06:58 +02006727 ++(*exp_idx);
6728}
6729
6730/**
6731 * @brief Check and parse a predicate following a leaf-list to extract its value.
6732 * On success all the used tokens are skipped.
6733 *
6734 * @param[in] exp Parsed XPath expression.
6735 * @param[in] exp_idx Position in the expression @p exp.
6736 * @param[out] val Leaf-list value on success.
6737 * @param[out] val_len Length of @p val.
6738 * @return LY_ERR
6739 */
6740static LY_ERR
6741eval_name_test_parse_leaflist_predicate(struct lyxp_expr *exp, uint16_t *exp_idx, const char **val, uint16_t *val_len)
6742{
6743 uint16_t cur_idx;
6744
6745 cur_idx = *exp_idx;
6746
6747 /* '[' */
Michal Vasko3e48bf32020-06-01 08:39:07 +02006748 LY_CHECK_RET(lyxp_check_token(NULL, exp, *exp_idx, LYXP_TOKEN_BRACK1));
Michal Vaskod3678892020-05-21 10:06:58 +02006749 ++cur_idx;
6750
6751 /* '.' */
Michal Vasko3e48bf32020-06-01 08:39:07 +02006752 LY_CHECK_RET(lyxp_check_token(NULL, exp, *exp_idx, LYXP_TOKEN_DOT));
Michal Vaskod3678892020-05-21 10:06:58 +02006753 ++cur_idx;
6754
6755 /* '=' */
Michal Vasko3e48bf32020-06-01 08:39:07 +02006756 LY_CHECK_RET(lyxp_check_token(NULL, exp, *exp_idx, LYXP_TOKEN_OPER_EQUAL));
Michal Vaskod3678892020-05-21 10:06:58 +02006757 ++cur_idx;
6758
6759 /* value */
Michal Vasko3e48bf32020-06-01 08:39:07 +02006760 LY_CHECK_RET(lyxp_check_token(NULL, exp, *exp_idx, LYXP_TOKEN_LITERAL));
Michal Vaskod3678892020-05-21 10:06:58 +02006761 ++cur_idx;
6762
6763 /* ']' */
Michal Vasko3e48bf32020-06-01 08:39:07 +02006764 LY_CHECK_RET(lyxp_check_token(NULL, exp, *exp_idx, LYXP_TOKEN_BRACK2));
Michal Vaskod3678892020-05-21 10:06:58 +02006765 ++cur_idx;
6766
6767 /* success */
6768 *val = &exp->expr[exp->tok_pos[cur_idx - 2] + 1];
6769 *val_len = exp->tok_len[cur_idx - 2] - 2;
6770 *exp_idx = cur_idx;
6771 return LY_SUCCESS;
6772}
6773
6774/**
6775 * @brief Check and parse a predicate following a list to make sure all the tokens are as expected (all key values defined).
6776 * On success all the used tokens are skipped.
6777 *
6778 * @param[in] exp Parsed XPath expression.
6779 * @param[in] exp_idx Position in the expression @p exp.
6780 * @param[in] slist Schema node of the list.
6781 * @param[out] val Leaf-list value on success.
6782 * @param[out] val_len Length of @p val.
6783 * @return LY_ERR
6784 */
6785static LY_ERR
6786eval_name_test_parse_list_predicate(struct lyxp_expr *exp, uint16_t *exp_idx, const struct lysc_node *slist,
6787 const char **keys, uint16_t *keys_len)
6788{
6789 uint16_t key_count, i, cur_idx;
6790 const struct lysc_node *key;
6791
6792 if (slist->flags & LYS_KEYLESS) {
6793 /* invalid */
6794 return LY_EINVAL;
6795 }
6796
6797 /* get key count */
6798 key_count = 0;
6799 for (key = lysc_node_children(slist, 0); key && (key->flags & LYS_KEY); key = key->next) {
6800 ++key_count;
6801 }
6802
6803 /* briefly check the predicate for each key */
6804 cur_idx = *exp_idx;
6805 for (i = 0; i < key_count; ++i) {
6806 /* '[' */
Michal Vasko3e48bf32020-06-01 08:39:07 +02006807 LY_CHECK_RET(lyxp_check_token(NULL, exp, cur_idx, LYXP_TOKEN_BRACK1));
Michal Vaskod3678892020-05-21 10:06:58 +02006808 ++cur_idx;
6809
6810 /* key-name */
Michal Vasko3e48bf32020-06-01 08:39:07 +02006811 LY_CHECK_RET(lyxp_check_token(NULL, exp, cur_idx, LYXP_TOKEN_NAMETEST));
Michal Vaskod3678892020-05-21 10:06:58 +02006812 ++cur_idx;
6813
6814 /* '=' */
Michal Vasko3e48bf32020-06-01 08:39:07 +02006815 LY_CHECK_RET(lyxp_check_token(NULL, exp, cur_idx, LYXP_TOKEN_OPER_EQUAL));
Michal Vaskod3678892020-05-21 10:06:58 +02006816 ++cur_idx;
6817
6818 /* key-value */
Michal Vasko3e48bf32020-06-01 08:39:07 +02006819 LY_CHECK_RET(lyxp_check_token(NULL, exp, cur_idx, LYXP_TOKEN_LITERAL));
Michal Vaskod3678892020-05-21 10:06:58 +02006820 ++cur_idx;
6821
6822 /* ']' */
Michal Vasko3e48bf32020-06-01 08:39:07 +02006823 LY_CHECK_RET(lyxp_check_token(NULL, exp, cur_idx, LYXP_TOKEN_BRACK2));
Michal Vaskod3678892020-05-21 10:06:58 +02006824 ++cur_idx;
6825 }
6826
6827 /* success */
6828 *keys = &exp->expr[exp->tok_pos[*exp_idx]];
6829 *keys_len = (exp->expr + exp->tok_pos[cur_idx - 1] + exp->tok_len[cur_idx - 1]) - *keys;
6830 *exp_idx = cur_idx;
6831 return LY_SUCCESS;
6832}
6833
6834/**
6835 * @brief Evaluate NameTest and any following Predicates. Logs directly on error.
6836 *
6837 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
6838 * [6] NodeTest ::= NameTest | NodeType '(' ')'
6839 * [7] NameTest ::= '*' | NCName ':' '*' | QName
6840 *
6841 * @param[in] exp Parsed XPath expression.
6842 * @param[in] exp_idx Position in the expression @p exp.
6843 * @param[in] attr_axis Whether to search attributes or standard nodes.
6844 * @param[in] all_desc Whether to search all the descendants or children only.
6845 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
6846 * @param[in] options XPath options.
6847 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6848 */
6849static LY_ERR
6850eval_name_test_with_predicate(struct lyxp_expr *exp, uint16_t *exp_idx, int attr_axis, int all_desc, struct lyxp_set *set,
6851 int options)
6852{
6853 int i;
6854 char *path;
6855 const char *ncname = NULL, *key_val = NULL;
6856 uint16_t ncname_len, key_val_len, prev_exp_idx;
6857 const struct lys_module *moveto_mod;
6858 const struct lysc_node *scnode = NULL, *tmp;
6859 struct lyd_node *list_inst = NULL;
6860 LY_ERR rc = LY_SUCCESS;
6861
6862 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko24cddf82020-06-01 08:17:01 +02006863 lyxp_print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
Michal Vaskod3678892020-05-21 10:06:58 +02006864 ++(*exp_idx);
6865
6866 if (!set) {
6867 goto moveto;
6868 }
6869
6870 ncname = &exp->expr[exp->tok_pos[*exp_idx - 1]];
6871 ncname_len = exp->tok_len[*exp_idx - 1];
6872
6873 /* parse (and skip) module name */
6874 rc = moveto_resolve_model(&ncname, &ncname_len, set, &moveto_mod);
6875 LY_CHECK_GOTO(rc, cleanup);
6876
6877 if (moveto_mod && !attr_axis && !all_desc && (set->type == LYXP_SET_NODE_SET)) {
6878 /* find the matching schema node in some parent in the context */
6879 for (i = 0; i < (signed)set->used; ++i) {
6880 if (set->val.nodes[i].type == set->root_type) {
6881 tmp = lys_find_child(NULL, moveto_mod, ncname, ncname_len, 0, 0);
6882 } else if ((set->val.nodes[i].type == LYXP_NODE_ELEM)
6883 && (!scnode || (lysc_data_parent(scnode) != set->val.nodes[i].node->schema))) {
6884 /* do not repeat the same search */
6885 tmp = lys_find_child(set->val.nodes[i].node->schema, moveto_mod, ncname, ncname_len, 0, 0);
6886 }
6887
6888 /* additional context check */
6889 if (tmp && (set->root_type == LYXP_NODE_ROOT_CONFIG) && (tmp->flags & LYS_CONFIG_R)) {
6890 tmp = NULL;
6891 }
6892
6893 if (tmp) {
6894 if (scnode) {
6895 /* we found a schema node with the same name but at different level, give up, too complicated */
6896 scnode = NULL;
6897 break;
6898 } else {
6899 /* remember the found schema node and continue to make sure it can be used */
6900 scnode = tmp;
6901 }
6902 tmp = NULL;
6903 }
6904 }
6905
6906 if (scnode && (scnode->nodetype == LYS_LIST)) {
6907 /* make sure all the tokens are right */
6908 prev_exp_idx = *exp_idx;
6909 if (eval_name_test_parse_list_predicate(exp, exp_idx, scnode, &key_val, &key_val_len)) {
6910 scnode = NULL;
6911 }
6912
6913 /* try to create a list instance */
6914 if (scnode && lyd_create_list(scnode, key_val, key_val_len, set->format, 0, &list_inst)) {
6915 /* for whatever reason the list failed to be created, just use standard name comparison and
6916 * parse predicate normally */
6917 *exp_idx = prev_exp_idx;
6918 scnode = NULL;
6919 }
6920 } else if (scnode && (scnode->nodetype == LYS_LEAFLIST)) {
6921 /* make sure we know the leaf-list value */
6922 if (eval_name_test_parse_leaflist_predicate(exp, exp_idx, &key_val, &key_val_len)) {
6923 /* value could not be recognized, use standard name comparison */
6924 scnode = NULL;
6925 }
6926 }
6927 }
6928
6929 if (!scnode) {
6930 /* we are not able to match based on a schema node */
6931 if (!moveto_mod) {
6932 /* '*', all nodes match */
6933 ncname = NULL;
6934 } else {
6935 /* insert name into dictionary for efficient comparison */
6936 ncname = lydict_insert(set->ctx, ncname, ncname_len);
6937 }
6938 }
6939
6940moveto:
6941 /* move to the attribute(s), data node(s), or schema node(s) */
6942 if (attr_axis) {
6943 if (set && (options & LYXP_SCNODE_ALL)) {
6944 set_scnode_clear_ctx(set);
6945 } else {
6946 if (all_desc) {
6947 rc = moveto_attr_alldesc(set, moveto_mod, ncname);
6948 } else {
6949 rc = moveto_attr(set, moveto_mod, ncname);
6950 }
6951 LY_CHECK_GOTO(rc, cleanup);
6952 }
6953 } else {
6954 if (set && (options & LYXP_SCNODE_ALL)) {
6955 if (all_desc) {
6956 rc = moveto_scnode_alldesc(set, moveto_mod, ncname, options);
6957 } else {
6958 rc = moveto_scnode(set, moveto_mod, ncname, options);
6959 }
6960 LY_CHECK_GOTO(rc, cleanup);
6961
6962 for (i = set->used - 1; i > -1; --i) {
6963 if (set->val.scnodes[i].in_ctx > 0) {
6964 break;
6965 }
6966 }
6967 if (i == -1) {
6968 path = lysc_path(set->ctx_scnode, LYSC_PATH_LOG, NULL, 0);
6969 LOGWRN(set->ctx, "Schema node \"%.*s\" not found (%.*s) with context node \"%s\".",
6970 exp->tok_len[*exp_idx], &exp->expr[exp->tok_pos[*exp_idx]],
6971 exp->tok_pos[*exp_idx] + exp->tok_len[*exp_idx], exp->expr, path);
6972 free(path);
6973 }
6974 } else {
6975 if (all_desc) {
6976 rc = moveto_node_alldesc(set, moveto_mod, ncname);
6977 } else {
6978 if (scnode) {
6979 /* we can find the nodes using hashes */
6980 rc = moveto_node_hash(set, scnode, list_inst, key_val, key_val_len);
6981 } else {
6982 rc = moveto_node(set, moveto_mod, ncname);
6983 }
6984 }
6985 LY_CHECK_GOTO(rc, cleanup);
6986 }
6987 }
6988
6989 /* Predicate* */
Michal Vasko14676352020-05-29 11:35:55 +02006990 while (!lyxp_check_token(NULL, exp, *exp_idx, LYXP_TOKEN_BRACK1)) {
Michal Vaskod3678892020-05-21 10:06:58 +02006991 rc = eval_predicate(exp, exp_idx, set, options, 1);
6992 LY_CHECK_RET(rc);
6993 }
6994
6995cleanup:
Michal Vaskodb51a8d2020-05-27 15:22:29 +02006996 if (set) {
6997 lydict_remove(set->ctx, ncname);
6998 }
Michal Vaskod3678892020-05-21 10:06:58 +02006999 lyd_free_tree(list_inst);
7000 return rc;
7001}
7002
7003/**
7004 * @brief Evaluate NodeType and any following Predicates. Logs directly on error.
7005 *
7006 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
7007 * [6] NodeTest ::= NameTest | NodeType '(' ')'
7008 * [8] NodeType ::= 'text' | 'node'
7009 *
7010 * @param[in] exp Parsed XPath expression.
7011 * @param[in] exp_idx Position in the expression @p exp.
7012 * @param[in] attr_axis Whether to search attributes or standard nodes.
7013 * @param[in] all_desc Whether to search all the descendants or children only.
7014 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7015 * @param[in] options XPath options.
7016 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7017 */
7018static LY_ERR
7019eval_node_type_with_predicate(struct lyxp_expr *exp, uint16_t *exp_idx, int attr_axis, int all_desc,
7020 struct lyxp_set *set, int options)
7021{
7022 LY_ERR rc;
7023
7024 /* TODO */
7025 (void)attr_axis;
7026 (void)all_desc;
7027
7028 if (set) {
7029 assert(exp->tok_len[*exp_idx] == 4);
7030 if (set->type == LYXP_SET_SCNODE_SET) {
7031 set_scnode_clear_ctx(set);
7032 /* just for the debug message below */
7033 set = NULL;
7034 } else {
7035 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "node", 4)) {
7036 rc = xpath_node(NULL, 0, set, options);
7037 } else {
7038 assert(!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "text", 4));
7039 rc = xpath_text(NULL, 0, set, options);
7040 }
7041 LY_CHECK_RET(rc);
7042 }
7043 }
7044 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko24cddf82020-06-01 08:17:01 +02007045 lyxp_print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
Michal Vaskod3678892020-05-21 10:06:58 +02007046 ++(*exp_idx);
7047
7048 /* '(' */
7049 assert(exp->tokens[*exp_idx] == LYXP_TOKEN_PAR1);
7050 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko24cddf82020-06-01 08:17:01 +02007051 lyxp_print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
Michal Vaskod3678892020-05-21 10:06:58 +02007052 ++(*exp_idx);
7053
7054 /* ')' */
7055 assert(exp->tokens[*exp_idx] == LYXP_TOKEN_PAR2);
7056 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko24cddf82020-06-01 08:17:01 +02007057 lyxp_print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
Michal Vaskod3678892020-05-21 10:06:58 +02007058 ++(*exp_idx);
7059
7060 /* Predicate* */
Michal Vasko14676352020-05-29 11:35:55 +02007061 while (!lyxp_check_token(NULL, exp, *exp_idx, LYXP_TOKEN_BRACK1)) {
Michal Vaskod3678892020-05-21 10:06:58 +02007062 rc = eval_predicate(exp, exp_idx, set, options, 1);
7063 LY_CHECK_RET(rc);
7064 }
7065
7066 return LY_SUCCESS;
7067}
7068
7069/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02007070 * @brief Evaluate RelativeLocationPath. Logs directly on error.
7071 *
7072 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
7073 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
Michal Vaskod3678892020-05-21 10:06:58 +02007074 * [6] NodeTest ::= NameTest | NodeType '(' ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007075 *
7076 * @param[in] exp Parsed XPath expression.
7077 * @param[in] exp_idx Position in the expression @p exp.
7078 * @param[in] all_desc Whether to search all the descendants or children only.
7079 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7080 * @param[in] options XPath options.
7081 * @return LY_ERR (YL_EINCOMPLETE on unresolved when)
7082 */
7083static LY_ERR
7084eval_relative_location_path(struct lyxp_expr *exp, uint16_t *exp_idx, int all_desc, struct lyxp_set *set, int options)
7085{
7086 int attr_axis;
7087 LY_ERR rc;
7088
7089 goto step;
7090 do {
7091 /* evaluate '/' or '//' */
7092 if (exp->tok_len[*exp_idx] == 1) {
7093 all_desc = 0;
7094 } else {
7095 assert(exp->tok_len[*exp_idx] == 2);
7096 all_desc = 1;
7097 }
7098 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko24cddf82020-06-01 08:17:01 +02007099 lyxp_print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007100 ++(*exp_idx);
7101
7102step:
Michal Vaskod3678892020-05-21 10:06:58 +02007103 /* evaluate abbreviated axis '@'? if any */
7104 if (exp->tokens[*exp_idx] == LYXP_TOKEN_AT) {
7105 attr_axis = 1;
7106
7107 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko24cddf82020-06-01 08:17:01 +02007108 lyxp_print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
Michal Vaskod3678892020-05-21 10:06:58 +02007109 ++(*exp_idx);
7110 } else {
7111 attr_axis = 0;
7112 }
7113
Michal Vasko03ff5a72019-09-11 13:49:33 +02007114 /* Step */
Michal Vasko03ff5a72019-09-11 13:49:33 +02007115 switch (exp->tokens[*exp_idx]) {
7116 case LYXP_TOKEN_DOT:
7117 /* evaluate '.' */
7118 if (set && (options & LYXP_SCNODE_ALL)) {
7119 rc = moveto_scnode_self(set, all_desc, options);
7120 } else {
7121 rc = moveto_self(set, all_desc, options);
7122 }
7123 LY_CHECK_RET(rc);
7124 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko24cddf82020-06-01 08:17:01 +02007125 lyxp_print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007126 ++(*exp_idx);
7127 break;
7128
7129 case LYXP_TOKEN_DDOT:
7130 /* evaluate '..' */
7131 if (set && (options & LYXP_SCNODE_ALL)) {
7132 rc = moveto_scnode_parent(set, all_desc, options);
7133 } else {
7134 rc = moveto_parent(set, all_desc, options);
7135 }
7136 LY_CHECK_RET(rc);
7137 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko24cddf82020-06-01 08:17:01 +02007138 lyxp_print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007139 ++(*exp_idx);
7140 break;
7141
Michal Vasko03ff5a72019-09-11 13:49:33 +02007142 case LYXP_TOKEN_NAMETEST:
Michal Vaskod3678892020-05-21 10:06:58 +02007143 /* evaluate NameTest Predicate* */
7144 rc = eval_name_test_with_predicate(exp, exp_idx, attr_axis, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007145 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02007146 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007147
Michal Vaskod3678892020-05-21 10:06:58 +02007148 case LYXP_TOKEN_NODETYPE:
7149 /* evaluate NodeType Predicate* */
7150 rc = eval_node_type_with_predicate(exp, exp_idx, attr_axis, all_desc, set, options);
7151 LY_CHECK_RET(rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007152 break;
7153
7154 default:
Michal Vasko02a77382019-09-12 11:47:35 +02007155 LOGINT_RET(set ? set->ctx : NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007156 }
Michal Vasko3e48bf32020-06-01 08:39:07 +02007157 } while (!exp_check_token2(NULL, exp, *exp_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007158
7159 return LY_SUCCESS;
7160}
7161
7162/**
7163 * @brief Evaluate AbsoluteLocationPath. Logs directly on error.
7164 *
7165 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
7166 *
7167 * @param[in] exp Parsed XPath expression.
7168 * @param[in] exp_idx Position in the expression @p exp.
7169 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7170 * @param[in] options XPath options.
7171 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7172 */
7173static LY_ERR
7174eval_absolute_location_path(struct lyxp_expr *exp, uint16_t *exp_idx, struct lyxp_set *set, int options)
7175{
7176 int all_desc;
7177 LY_ERR rc;
7178
7179 if (set) {
7180 /* no matter what tokens follow, we need to be at the root */
7181 moveto_root(set, options);
7182 }
7183
7184 /* '/' RelativeLocationPath? */
7185 if (exp->tok_len[*exp_idx] == 1) {
7186 /* evaluate '/' - deferred */
7187 all_desc = 0;
7188 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko24cddf82020-06-01 08:17:01 +02007189 lyxp_print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007190 ++(*exp_idx);
7191
Michal Vasko14676352020-05-29 11:35:55 +02007192 if (lyxp_check_token(NULL, exp, *exp_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007193 return LY_SUCCESS;
7194 }
7195 switch (exp->tokens[*exp_idx]) {
7196 case LYXP_TOKEN_DOT:
7197 case LYXP_TOKEN_DDOT:
7198 case LYXP_TOKEN_AT:
7199 case LYXP_TOKEN_NAMETEST:
7200 case LYXP_TOKEN_NODETYPE:
7201 rc = eval_relative_location_path(exp, exp_idx, all_desc, set, options);
7202 LY_CHECK_RET(rc);
7203 break;
7204 default:
7205 break;
7206 }
7207
7208 /* '//' RelativeLocationPath */
7209 } else {
7210 /* evaluate '//' - deferred so as not to waste memory by remembering all the nodes */
7211 all_desc = 1;
7212 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko24cddf82020-06-01 08:17:01 +02007213 lyxp_print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007214 ++(*exp_idx);
7215
7216 rc = eval_relative_location_path(exp, exp_idx, all_desc, set, options);
7217 LY_CHECK_RET(rc);
7218 }
7219
7220 return LY_SUCCESS;
7221}
7222
7223/**
7224 * @brief Evaluate FunctionCall. Logs directly on error.
7225 *
Michal Vaskod3678892020-05-21 10:06:58 +02007226 * [11] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007227 *
7228 * @param[in] exp Parsed XPath expression.
7229 * @param[in] exp_idx Position in the expression @p exp.
7230 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7231 * @param[in] options XPath options.
7232 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7233 */
7234static LY_ERR
7235eval_function_call(struct lyxp_expr *exp, uint16_t *exp_idx, struct lyxp_set *set, int options)
7236{
7237 LY_ERR rc;
7238 LY_ERR (*xpath_func)(struct lyxp_set **, uint16_t, struct lyxp_set *, int) = NULL;
Michal Vasko0cbf54f2019-12-16 10:01:06 +01007239 uint16_t arg_count = 0, i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007240 struct lyxp_set **args = NULL, **args_aux;
7241
7242 if (set) {
7243 /* FunctionName */
7244 switch (exp->tok_len[*exp_idx]) {
7245 case 3:
7246 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "not", 3)) {
7247 xpath_func = &xpath_not;
7248 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "sum", 3)) {
7249 xpath_func = &xpath_sum;
7250 }
7251 break;
7252 case 4:
7253 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "lang", 4)) {
7254 xpath_func = &xpath_lang;
7255 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "last", 4)) {
7256 xpath_func = &xpath_last;
7257 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "name", 4)) {
7258 xpath_func = &xpath_name;
7259 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "true", 4)) {
7260 xpath_func = &xpath_true;
7261 }
7262 break;
7263 case 5:
7264 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "count", 5)) {
7265 xpath_func = &xpath_count;
7266 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "false", 5)) {
7267 xpath_func = &xpath_false;
7268 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "floor", 5)) {
7269 xpath_func = &xpath_floor;
7270 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "round", 5)) {
7271 xpath_func = &xpath_round;
7272 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "deref", 5)) {
7273 xpath_func = &xpath_deref;
7274 }
7275 break;
7276 case 6:
7277 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "concat", 6)) {
7278 xpath_func = &xpath_concat;
7279 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "number", 6)) {
7280 xpath_func = &xpath_number;
7281 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "string", 6)) {
7282 xpath_func = &xpath_string;
7283 }
7284 break;
7285 case 7:
7286 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "boolean", 7)) {
7287 xpath_func = &xpath_boolean;
7288 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "ceiling", 7)) {
7289 xpath_func = &xpath_ceiling;
7290 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "current", 7)) {
7291 xpath_func = &xpath_current;
7292 }
7293 break;
7294 case 8:
7295 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "contains", 8)) {
7296 xpath_func = &xpath_contains;
7297 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "position", 8)) {
7298 xpath_func = &xpath_position;
7299 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "re-match", 8)) {
7300 xpath_func = &xpath_re_match;
7301 }
7302 break;
7303 case 9:
7304 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "substring", 9)) {
7305 xpath_func = &xpath_substring;
7306 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "translate", 9)) {
7307 xpath_func = &xpath_translate;
7308 }
7309 break;
7310 case 10:
7311 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "local-name", 10)) {
7312 xpath_func = &xpath_local_name;
7313 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "enum-value", 10)) {
7314 xpath_func = &xpath_enum_value;
7315 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "bit-is-set", 10)) {
7316 xpath_func = &xpath_bit_is_set;
7317 }
7318 break;
7319 case 11:
7320 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "starts-with", 11)) {
7321 xpath_func = &xpath_starts_with;
7322 }
7323 break;
7324 case 12:
7325 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "derived-from", 12)) {
7326 xpath_func = &xpath_derived_from;
7327 }
7328 break;
7329 case 13:
7330 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "namespace-uri", 13)) {
7331 xpath_func = &xpath_namespace_uri;
7332 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "string-length", 13)) {
7333 xpath_func = &xpath_string_length;
7334 }
7335 break;
7336 case 15:
7337 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "normalize-space", 15)) {
7338 xpath_func = &xpath_normalize_space;
7339 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "substring-after", 15)) {
7340 xpath_func = &xpath_substring_after;
7341 }
7342 break;
7343 case 16:
7344 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "substring-before", 16)) {
7345 xpath_func = &xpath_substring_before;
7346 }
7347 break;
7348 case 20:
7349 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "derived-from-or-self", 20)) {
7350 xpath_func = &xpath_derived_from_or_self;
7351 }
7352 break;
7353 }
7354
7355 if (!xpath_func) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007356 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INFUNC, exp->tok_len[*exp_idx], &exp->expr[exp->tok_pos[*exp_idx]]);
7357 return LY_EVALID;
7358 }
7359 }
7360
7361 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko24cddf82020-06-01 08:17:01 +02007362 lyxp_print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007363 ++(*exp_idx);
7364
7365 /* '(' */
7366 assert(exp->tokens[*exp_idx] == LYXP_TOKEN_PAR1);
7367 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko24cddf82020-06-01 08:17:01 +02007368 lyxp_print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007369 ++(*exp_idx);
7370
7371 /* ( Expr ( ',' Expr )* )? */
7372 if (exp->tokens[*exp_idx] != LYXP_TOKEN_PAR2) {
7373 if (set) {
7374 args = malloc(sizeof *args);
7375 LY_CHECK_ERR_GOTO(!args, LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7376 arg_count = 1;
7377 args[0] = set_copy(set);
7378 if (!args[0]) {
7379 rc = LY_EMEM;
7380 goto cleanup;
7381 }
7382
7383 rc = eval_expr_select(exp, exp_idx, 0, args[0], options);
7384 LY_CHECK_GOTO(rc, cleanup);
7385 } else {
7386 rc = eval_expr_select(exp, exp_idx, 0, NULL, options);
7387 LY_CHECK_GOTO(rc, cleanup);
7388 }
7389 }
Michal Vasko14676352020-05-29 11:35:55 +02007390 while (!lyxp_check_token(NULL, exp, *exp_idx, LYXP_TOKEN_COMMA)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007391 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko24cddf82020-06-01 08:17:01 +02007392 lyxp_print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007393 ++(*exp_idx);
7394
7395 if (set) {
7396 ++arg_count;
7397 args_aux = realloc(args, arg_count * sizeof *args);
7398 LY_CHECK_ERR_GOTO(!args_aux, arg_count--; LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7399 args = args_aux;
7400 args[arg_count - 1] = set_copy(set);
7401 if (!args[arg_count - 1]) {
7402 rc = LY_EMEM;
7403 goto cleanup;
7404 }
7405
7406 rc = eval_expr_select(exp, exp_idx, 0, args[arg_count - 1], options);
7407 LY_CHECK_GOTO(rc, cleanup);
7408 } else {
7409 rc = eval_expr_select(exp, exp_idx, 0, NULL, options);
7410 LY_CHECK_GOTO(rc, cleanup);
7411 }
7412 }
7413
7414 /* ')' */
7415 assert(exp->tokens[*exp_idx] == LYXP_TOKEN_PAR2);
7416 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko24cddf82020-06-01 08:17:01 +02007417 lyxp_print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007418 ++(*exp_idx);
7419
7420 if (set) {
7421 /* evaluate function */
7422 rc = xpath_func(args, arg_count, set, options);
7423
7424 if (options & LYXP_SCNODE_ALL) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007425 /* merge all nodes from arg evaluations */
7426 for (i = 0; i < arg_count; ++i) {
7427 set_scnode_clear_ctx(args[i]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007428 lyxp_set_scnode_merge(set, args[i]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007429 }
7430 }
7431 } else {
7432 rc = LY_SUCCESS;
7433 }
7434
7435cleanup:
7436 for (i = 0; i < arg_count; ++i) {
7437 lyxp_set_free(args[i]);
7438 }
7439 free(args);
7440
7441 return rc;
7442}
7443
7444/**
7445 * @brief Evaluate Number. Logs directly on error.
7446 *
7447 * @param[in] ctx Context for errors.
7448 * @param[in] exp Parsed XPath expression.
7449 * @param[in] exp_idx Position in the expression @p exp.
7450 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7451 * @return LY_ERR
7452 */
7453static LY_ERR
7454eval_number(struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *exp_idx, struct lyxp_set *set)
7455{
7456 long double num;
7457 char *endptr;
7458
7459 if (set) {
7460 errno = 0;
7461 num = strtold(&exp->expr[exp->tok_pos[*exp_idx]], &endptr);
7462 if (errno) {
7463 LOGVAL(ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*exp_idx]]);
7464 LOGVAL(ctx, LY_VLOG_LYD, set->ctx_node, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double (%s).",
7465 exp->tok_len[*exp_idx], &exp->expr[exp->tok_pos[*exp_idx]], strerror(errno));
7466 return LY_EVALID;
7467 } else if (endptr - &exp->expr[exp->tok_pos[*exp_idx]] != exp->tok_len[*exp_idx]) {
7468 LOGVAL(ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*exp_idx]]);
7469 LOGVAL(ctx, LY_VLOG_LYD, set->ctx_node, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double.",
7470 exp->tok_len[*exp_idx], &exp->expr[exp->tok_pos[*exp_idx]]);
7471 return LY_EVALID;
7472 }
7473
7474 set_fill_number(set, num);
7475 }
7476
7477 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko24cddf82020-06-01 08:17:01 +02007478 lyxp_print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007479 ++(*exp_idx);
7480 return LY_SUCCESS;
7481}
7482
7483/**
7484 * @brief Evaluate PathExpr. Logs directly on error.
7485 *
Michal Vaskod3678892020-05-21 10:06:58 +02007486 * [12] PathExpr ::= LocationPath | PrimaryExpr Predicate*
Michal Vasko03ff5a72019-09-11 13:49:33 +02007487 * | PrimaryExpr Predicate* '/' RelativeLocationPath
7488 * | PrimaryExpr Predicate* '//' RelativeLocationPath
7489 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
Michal Vaskod3678892020-05-21 10:06:58 +02007490 * [10] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall
Michal Vasko03ff5a72019-09-11 13:49:33 +02007491 *
7492 * @param[in] exp Parsed XPath expression.
7493 * @param[in] exp_idx Position in the expression @p exp.
7494 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7495 * @param[in] options XPath options.
7496 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7497 */
7498static LY_ERR
7499eval_path_expr(struct lyxp_expr *exp, uint16_t *exp_idx, struct lyxp_set *set, int options)
7500{
7501 int all_desc, parent_pos_pred;
7502 LY_ERR rc;
7503
7504 switch (exp->tokens[*exp_idx]) {
7505 case LYXP_TOKEN_PAR1:
7506 /* '(' Expr ')' */
7507
7508 /* '(' */
7509 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko24cddf82020-06-01 08:17:01 +02007510 lyxp_print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007511 ++(*exp_idx);
7512
7513 /* Expr */
7514 rc = eval_expr_select(exp, exp_idx, 0, set, options);
7515 LY_CHECK_RET(rc);
7516
7517 /* ')' */
7518 assert(exp->tokens[*exp_idx] == LYXP_TOKEN_PAR2);
7519 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko24cddf82020-06-01 08:17:01 +02007520 lyxp_print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007521 ++(*exp_idx);
7522
7523 parent_pos_pred = 0;
7524 goto predicate;
7525
7526 case LYXP_TOKEN_DOT:
7527 case LYXP_TOKEN_DDOT:
7528 case LYXP_TOKEN_AT:
7529 case LYXP_TOKEN_NAMETEST:
7530 case LYXP_TOKEN_NODETYPE:
7531 /* RelativeLocationPath */
7532 rc = eval_relative_location_path(exp, exp_idx, 0, set, options);
7533 LY_CHECK_RET(rc);
7534 break;
7535
7536 case LYXP_TOKEN_FUNCNAME:
7537 /* FunctionCall */
7538 if (!set) {
7539 rc = eval_function_call(exp, exp_idx, NULL, options);
7540 } else {
7541 rc = eval_function_call(exp, exp_idx, set, options);
7542 }
7543 LY_CHECK_RET(rc);
7544
7545 parent_pos_pred = 1;
7546 goto predicate;
7547
Michal Vasko3e48bf32020-06-01 08:39:07 +02007548 case LYXP_TOKEN_OPER_PATH:
7549 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02007550 /* AbsoluteLocationPath */
7551 rc = eval_absolute_location_path(exp, exp_idx, set, options);
7552 LY_CHECK_RET(rc);
7553 break;
7554
7555 case LYXP_TOKEN_LITERAL:
7556 /* Literal */
7557 if (!set || (options & LYXP_SCNODE_ALL)) {
7558 if (set) {
7559 set_scnode_clear_ctx(set);
7560 }
7561 eval_literal(exp, exp_idx, NULL);
7562 } else {
7563 eval_literal(exp, exp_idx, set);
7564 }
7565
7566 parent_pos_pred = 1;
7567 goto predicate;
7568
7569 case LYXP_TOKEN_NUMBER:
7570 /* Number */
7571 if (!set || (options & LYXP_SCNODE_ALL)) {
7572 if (set) {
7573 set_scnode_clear_ctx(set);
7574 }
Michal Vasko02a77382019-09-12 11:47:35 +02007575 rc = eval_number(NULL, exp, exp_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007576 } else {
7577 rc = eval_number(set->ctx, exp, exp_idx, set);
7578 }
7579 LY_CHECK_RET(rc);
7580
7581 parent_pos_pred = 1;
7582 goto predicate;
7583
7584 default:
Michal Vasko24cddf82020-06-01 08:17:01 +02007585 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INTOK, lyxp_print_token(exp->tokens[*exp_idx]),
Michal Vasko03ff5a72019-09-11 13:49:33 +02007586 &exp->expr[exp->tok_pos[*exp_idx]]);
7587 return LY_EVALID;
7588 }
7589
7590 return LY_SUCCESS;
7591
7592predicate:
7593 /* Predicate* */
Michal Vasko14676352020-05-29 11:35:55 +02007594 while (!lyxp_check_token(NULL, exp, *exp_idx, LYXP_TOKEN_BRACK1)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007595 rc = eval_predicate(exp, exp_idx, set, options, parent_pos_pred);
7596 LY_CHECK_RET(rc);
7597 }
7598
7599 /* ('/' or '//') RelativeLocationPath */
Michal Vasko3e48bf32020-06-01 08:39:07 +02007600 if (!exp_check_token2(NULL, exp, *exp_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007601
7602 /* evaluate '/' or '//' */
Michal Vasko3e48bf32020-06-01 08:39:07 +02007603 if (exp->tokens[*exp_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007604 all_desc = 0;
7605 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007606 all_desc = 1;
7607 }
7608
7609 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko24cddf82020-06-01 08:17:01 +02007610 lyxp_print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007611 ++(*exp_idx);
7612
7613 rc = eval_relative_location_path(exp, exp_idx, all_desc, set, options);
7614 LY_CHECK_RET(rc);
7615 }
7616
7617 return LY_SUCCESS;
7618}
7619
7620/**
7621 * @brief Evaluate UnionExpr. Logs directly on error.
7622 *
Michal Vaskod3678892020-05-21 10:06:58 +02007623 * [20] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007624 *
7625 * @param[in] exp Parsed XPath expression.
7626 * @param[in] exp_idx Position in the expression @p exp.
7627 * @param[in] repeat How many times this expression is repeated.
7628 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7629 * @param[in] options XPath options.
7630 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7631 */
7632static LY_ERR
7633eval_union_expr(struct lyxp_expr *exp, uint16_t *exp_idx, uint16_t repeat, struct lyxp_set *set, int options)
7634{
7635 LY_ERR rc = LY_SUCCESS;
7636 struct lyxp_set orig_set, set2;
7637 uint16_t i;
7638
7639 assert(repeat);
7640
7641 set_init(&orig_set, set);
7642 set_init(&set2, set);
7643
7644 set_fill_set(&orig_set, set);
7645
7646 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_UNION, set, options);
7647 LY_CHECK_GOTO(rc, cleanup);
7648
7649 /* ('|' PathExpr)* */
7650 for (i = 0; i < repeat; ++i) {
Michal Vasko3e48bf32020-06-01 08:39:07 +02007651 assert(exp->tokens[*exp_idx] == LYXP_TOKEN_OPER_UNI);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007652 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko24cddf82020-06-01 08:17:01 +02007653 lyxp_print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007654 ++(*exp_idx);
7655
7656 if (!set) {
7657 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_UNION, NULL, options);
7658 LY_CHECK_GOTO(rc, cleanup);
7659 continue;
7660 }
7661
7662 set_fill_set(&set2, &orig_set);
7663 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_UNION, &set2, options);
7664 LY_CHECK_GOTO(rc, cleanup);
7665
7666 /* eval */
7667 if (options & LYXP_SCNODE_ALL) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01007668 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007669 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007670 rc = moveto_union(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007671 LY_CHECK_GOTO(rc, cleanup);
7672 }
7673 }
7674
7675cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007676 lyxp_set_free_content(&orig_set);
7677 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007678 return rc;
7679}
7680
7681/**
7682 * @brief Evaluate UnaryExpr. Logs directly on error.
7683 *
Michal Vaskod3678892020-05-21 10:06:58 +02007684 * [19] UnaryExpr ::= UnionExpr | '-' UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007685 *
7686 * @param[in] exp Parsed XPath expression.
7687 * @param[in] exp_idx Position in the expression @p exp.
7688 * @param[in] repeat How many times this expression is repeated.
7689 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7690 * @param[in] options XPath options.
7691 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7692 */
7693static LY_ERR
7694eval_unary_expr(struct lyxp_expr *exp, uint16_t *exp_idx, uint16_t repeat, struct lyxp_set *set, int options)
7695{
7696 LY_ERR rc;
7697 uint16_t this_op, i;
7698
7699 assert(repeat);
7700
7701 /* ('-')+ */
7702 this_op = *exp_idx;
7703 for (i = 0; i < repeat; ++i) {
Michal Vasko3e48bf32020-06-01 08:39:07 +02007704 assert(!lyxp_check_token(NULL, exp, *exp_idx, LYXP_TOKEN_OPER_MATH) && (exp->expr[exp->tok_pos[*exp_idx]] == '-'));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007705
7706 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko24cddf82020-06-01 08:17:01 +02007707 lyxp_print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007708 ++(*exp_idx);
7709 }
7710
7711 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_UNARY, set, options);
7712 LY_CHECK_RET(rc);
7713
7714 if (set && (repeat % 2)) {
7715 if (options & LYXP_SCNODE_ALL) {
7716 warn_operands(set->ctx, set, NULL, 1, exp->expr, exp->tok_pos[this_op]);
7717 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007718 rc = moveto_op_math(set, NULL, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007719 LY_CHECK_RET(rc);
7720 }
7721 }
7722
7723 return LY_SUCCESS;
7724}
7725
7726/**
7727 * @brief Evaluate MultiplicativeExpr. Logs directly on error.
7728 *
Michal Vaskod3678892020-05-21 10:06:58 +02007729 * [18] MultiplicativeExpr ::= UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007730 * | MultiplicativeExpr '*' UnaryExpr
7731 * | MultiplicativeExpr 'div' UnaryExpr
7732 * | MultiplicativeExpr 'mod' UnaryExpr
7733 *
7734 * @param[in] exp Parsed XPath expression.
7735 * @param[in] exp_idx Position in the expression @p exp.
7736 * @param[in] repeat How many times this expression is repeated.
7737 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7738 * @param[in] options XPath options.
7739 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7740 */
7741static LY_ERR
7742eval_multiplicative_expr(struct lyxp_expr *exp, uint16_t *exp_idx, uint16_t repeat, struct lyxp_set *set, int options)
7743{
7744 LY_ERR rc;
7745 uint16_t this_op;
7746 struct lyxp_set orig_set, set2;
7747 uint16_t i;
7748
7749 assert(repeat);
7750
7751 set_init(&orig_set, set);
7752 set_init(&set2, set);
7753
7754 set_fill_set(&orig_set, set);
7755
7756 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_MULTIPLICATIVE, set, options);
7757 LY_CHECK_GOTO(rc, cleanup);
7758
7759 /* ('*' / 'div' / 'mod' UnaryExpr)* */
7760 for (i = 0; i < repeat; ++i) {
7761 this_op = *exp_idx;
7762
Michal Vasko3e48bf32020-06-01 08:39:07 +02007763 assert(exp->tokens[*exp_idx] == LYXP_TOKEN_OPER_MATH);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007764 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko24cddf82020-06-01 08:17:01 +02007765 lyxp_print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007766 ++(*exp_idx);
7767
7768 if (!set) {
7769 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_MULTIPLICATIVE, NULL, options);
7770 LY_CHECK_GOTO(rc, cleanup);
7771 continue;
7772 }
7773
7774 set_fill_set(&set2, &orig_set);
7775 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_MULTIPLICATIVE, &set2, options);
7776 LY_CHECK_GOTO(rc, cleanup);
7777
7778 /* eval */
7779 if (options & LYXP_SCNODE_ALL) {
7780 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007781 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007782 set_scnode_clear_ctx(set);
7783 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007784 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007785 LY_CHECK_GOTO(rc, cleanup);
7786 }
7787 }
7788
7789cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007790 lyxp_set_free_content(&orig_set);
7791 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007792 return rc;
7793}
7794
7795/**
7796 * @brief Evaluate AdditiveExpr. Logs directly on error.
7797 *
Michal Vaskod3678892020-05-21 10:06:58 +02007798 * [17] AdditiveExpr ::= MultiplicativeExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007799 * | AdditiveExpr '+' MultiplicativeExpr
7800 * | AdditiveExpr '-' MultiplicativeExpr
7801 *
7802 * @param[in] exp Parsed XPath expression.
7803 * @param[in] exp_idx Position in the expression @p exp.
7804 * @param[in] repeat How many times this expression is repeated.
7805 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7806 * @param[in] options XPath options.
7807 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7808 */
7809static LY_ERR
7810eval_additive_expr(struct lyxp_expr *exp, uint16_t *exp_idx, uint16_t repeat, struct lyxp_set *set, int options)
7811{
7812 LY_ERR rc;
7813 uint16_t this_op;
7814 struct lyxp_set orig_set, set2;
7815 uint16_t i;
7816
7817 assert(repeat);
7818
7819 set_init(&orig_set, set);
7820 set_init(&set2, set);
7821
7822 set_fill_set(&orig_set, set);
7823
7824 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_ADDITIVE, set, options);
7825 LY_CHECK_GOTO(rc, cleanup);
7826
7827 /* ('+' / '-' MultiplicativeExpr)* */
7828 for (i = 0; i < repeat; ++i) {
7829 this_op = *exp_idx;
7830
Michal Vasko3e48bf32020-06-01 08:39:07 +02007831 assert(exp->tokens[*exp_idx] == LYXP_TOKEN_OPER_MATH);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007832 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko24cddf82020-06-01 08:17:01 +02007833 lyxp_print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007834 ++(*exp_idx);
7835
7836 if (!set) {
7837 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_ADDITIVE, NULL, options);
7838 LY_CHECK_GOTO(rc, cleanup);
7839 continue;
7840 }
7841
7842 set_fill_set(&set2, &orig_set);
7843 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_ADDITIVE, &set2, options);
7844 LY_CHECK_GOTO(rc, cleanup);
7845
7846 /* eval */
7847 if (options & LYXP_SCNODE_ALL) {
7848 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007849 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007850 set_scnode_clear_ctx(set);
7851 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007852 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007853 LY_CHECK_GOTO(rc, cleanup);
7854 }
7855 }
7856
7857cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007858 lyxp_set_free_content(&orig_set);
7859 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007860 return rc;
7861}
7862
7863/**
7864 * @brief Evaluate RelationalExpr. Logs directly on error.
7865 *
Michal Vaskod3678892020-05-21 10:06:58 +02007866 * [16] RelationalExpr ::= AdditiveExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007867 * | RelationalExpr '<' AdditiveExpr
7868 * | RelationalExpr '>' AdditiveExpr
7869 * | RelationalExpr '<=' AdditiveExpr
7870 * | RelationalExpr '>=' AdditiveExpr
7871 *
7872 * @param[in] exp Parsed XPath expression.
7873 * @param[in] exp_idx Position in the expression @p exp.
7874 * @param[in] repeat How many times this expression is repeated.
7875 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7876 * @param[in] options XPath options.
7877 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7878 */
7879static LY_ERR
7880eval_relational_expr(struct lyxp_expr *exp, uint16_t *exp_idx, uint16_t repeat, struct lyxp_set *set, int options)
7881{
7882 LY_ERR rc;
7883 uint16_t this_op;
7884 struct lyxp_set orig_set, set2;
7885 uint16_t i;
7886
7887 assert(repeat);
7888
7889 set_init(&orig_set, set);
7890 set_init(&set2, set);
7891
7892 set_fill_set(&orig_set, set);
7893
7894 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_RELATIONAL, set, options);
7895 LY_CHECK_GOTO(rc, cleanup);
7896
7897 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
7898 for (i = 0; i < repeat; ++i) {
7899 this_op = *exp_idx;
7900
Michal Vasko3e48bf32020-06-01 08:39:07 +02007901 assert(exp->tokens[*exp_idx] == LYXP_TOKEN_OPER_COMP);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007902 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko24cddf82020-06-01 08:17:01 +02007903 lyxp_print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007904 ++(*exp_idx);
7905
7906 if (!set) {
7907 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_RELATIONAL, NULL, options);
7908 LY_CHECK_GOTO(rc, cleanup);
7909 continue;
7910 }
7911
7912 set_fill_set(&set2, &orig_set);
7913 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_RELATIONAL, &set2, options);
7914 LY_CHECK_GOTO(rc, cleanup);
7915
7916 /* eval */
7917 if (options & LYXP_SCNODE_ALL) {
7918 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007919 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007920 set_scnode_clear_ctx(set);
7921 } else {
7922 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options);
7923 LY_CHECK_GOTO(rc, cleanup);
7924 }
7925 }
7926
7927cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007928 lyxp_set_free_content(&orig_set);
7929 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007930 return rc;
7931}
7932
7933/**
7934 * @brief Evaluate EqualityExpr. Logs directly on error.
7935 *
Michal Vaskod3678892020-05-21 10:06:58 +02007936 * [15] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007937 * | EqualityExpr '!=' RelationalExpr
7938 *
7939 * @param[in] exp Parsed XPath expression.
7940 * @param[in] exp_idx Position in the expression @p exp.
7941 * @param[in] repeat How many times this expression is repeated.
7942 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7943 * @param[in] options XPath options.
7944 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7945 */
7946static LY_ERR
7947eval_equality_expr(struct lyxp_expr *exp, uint16_t *exp_idx, uint16_t repeat, struct lyxp_set *set, int options)
7948{
7949 LY_ERR rc;
7950 uint16_t this_op;
7951 struct lyxp_set orig_set, set2;
7952 uint16_t i;
7953
7954 assert(repeat);
7955
7956 set_init(&orig_set, set);
7957 set_init(&set2, set);
7958
7959 set_fill_set(&orig_set, set);
7960
7961 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_EQUALITY, set, options);
7962 LY_CHECK_GOTO(rc, cleanup);
7963
7964 /* ('=' / '!=' RelationalExpr)* */
7965 for (i = 0; i < repeat; ++i) {
7966 this_op = *exp_idx;
7967
Michal Vasko3e48bf32020-06-01 08:39:07 +02007968 assert((exp->tokens[*exp_idx] == LYXP_TOKEN_OPER_EQUAL) || (exp->tokens[*exp_idx] == LYXP_TOKEN_OPER_NEQUAL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007969 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko24cddf82020-06-01 08:17:01 +02007970 lyxp_print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007971 ++(*exp_idx);
7972
7973 if (!set) {
7974 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_EQUALITY, NULL, options);
7975 LY_CHECK_GOTO(rc, cleanup);
7976 continue;
7977 }
7978
7979 set_fill_set(&set2, &orig_set);
7980 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_EQUALITY, &set2, options);
7981 LY_CHECK_GOTO(rc, cleanup);
7982
7983 /* eval */
7984 if (options & LYXP_SCNODE_ALL) {
7985 warn_operands(set->ctx, set, &set2, 0, exp->expr, exp->tok_pos[this_op - 1]);
7986 warn_equality_value(exp, set, *exp_idx - 1, this_op - 1, *exp_idx - 1);
7987 warn_equality_value(exp, &set2, this_op - 1, this_op - 1, *exp_idx - 1);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007988 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007989 set_scnode_clear_ctx(set);
7990 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007991 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options);
7992 LY_CHECK_GOTO(rc, cleanup);
7993 }
7994 }
7995
7996cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007997 lyxp_set_free_content(&orig_set);
7998 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007999 return rc;
8000}
8001
8002/**
8003 * @brief Evaluate AndExpr. Logs directly on error.
8004 *
Michal Vaskod3678892020-05-21 10:06:58 +02008005 * [14] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008006 *
8007 * @param[in] exp Parsed XPath expression.
8008 * @param[in] exp_idx Position in the expression @p exp.
8009 * @param[in] repeat How many times this expression is repeated.
8010 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8011 * @param[in] options XPath options.
8012 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8013 */
8014static LY_ERR
8015eval_and_expr(struct lyxp_expr *exp, uint16_t *exp_idx, uint16_t repeat, struct lyxp_set *set, int options)
8016{
8017 LY_ERR rc;
8018 struct lyxp_set orig_set, set2;
8019 uint16_t i;
8020
8021 assert(repeat);
8022
8023 set_init(&orig_set, set);
8024 set_init(&set2, set);
8025
8026 set_fill_set(&orig_set, set);
8027
8028 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_AND, set, options);
8029 LY_CHECK_GOTO(rc, cleanup);
8030
8031 /* cast to boolean, we know that will be the final result */
8032 if (set && (options & LYXP_SCNODE_ALL)) {
8033 set_scnode_clear_ctx(set);
8034 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008035 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008036 }
8037
8038 /* ('and' EqualityExpr)* */
8039 for (i = 0; i < repeat; ++i) {
Michal Vasko3e48bf32020-06-01 08:39:07 +02008040 assert(exp->tokens[*exp_idx] == LYXP_TOKEN_OPER_LOG);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008041 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (!set || !set->val.bool ? "skipped" : "parsed"),
Michal Vasko24cddf82020-06-01 08:17:01 +02008042 lyxp_print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008043 ++(*exp_idx);
8044
8045 /* lazy evaluation */
8046 if (!set || ((set->type == LYXP_SET_BOOLEAN) && !set->val.bool)) {
8047 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_AND, NULL, options);
8048 LY_CHECK_GOTO(rc, cleanup);
8049 continue;
8050 }
8051
8052 set_fill_set(&set2, &orig_set);
8053 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_AND, &set2, options);
8054 LY_CHECK_GOTO(rc, cleanup);
8055
8056 /* eval - just get boolean value actually */
8057 if (set->type == LYXP_SET_SCNODE_SET) {
8058 set_scnode_clear_ctx(&set2);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008059 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008060 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008061 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008062 set_fill_set(set, &set2);
8063 }
8064 }
8065
8066cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008067 lyxp_set_free_content(&orig_set);
8068 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008069 return rc;
8070}
8071
8072/**
8073 * @brief Evaluate OrExpr. Logs directly on error.
8074 *
Michal Vaskod3678892020-05-21 10:06:58 +02008075 * [13] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008076 *
8077 * @param[in] exp Parsed XPath expression.
8078 * @param[in] exp_idx Position in the expression @p exp.
8079 * @param[in] repeat How many times this expression is repeated.
8080 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8081 * @param[in] options XPath options.
8082 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8083 */
8084static LY_ERR
8085eval_or_expr(struct lyxp_expr *exp, uint16_t *exp_idx, uint16_t repeat, struct lyxp_set *set, int options)
8086{
8087 LY_ERR rc;
8088 struct lyxp_set orig_set, set2;
8089 uint16_t i;
8090
8091 assert(repeat);
8092
8093 set_init(&orig_set, set);
8094 set_init(&set2, set);
8095
8096 set_fill_set(&orig_set, set);
8097
8098 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_OR, set, options);
8099 LY_CHECK_GOTO(rc, cleanup);
8100
8101 /* cast to boolean, we know that will be the final result */
8102 if (set && (options & LYXP_SCNODE_ALL)) {
8103 set_scnode_clear_ctx(set);
8104 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008105 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008106 }
8107
8108 /* ('or' AndExpr)* */
8109 for (i = 0; i < repeat; ++i) {
Michal Vasko3e48bf32020-06-01 08:39:07 +02008110 assert(exp->tokens[*exp_idx] == LYXP_TOKEN_OPER_LOG);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008111 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (!set || set->val.bool ? "skipped" : "parsed"),
Michal Vasko24cddf82020-06-01 08:17:01 +02008112 lyxp_print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008113 ++(*exp_idx);
8114
8115 /* lazy evaluation */
8116 if (!set || ((set->type == LYXP_SET_BOOLEAN) && set->val.bool)) {
8117 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_OR, NULL, options);
8118 LY_CHECK_GOTO(rc, cleanup);
8119 continue;
8120 }
8121
8122 set_fill_set(&set2, &orig_set);
8123 /* expr_type cound have been LYXP_EXPR_NONE in all these later calls (except for the first one),
8124 * but it does not matter */
8125 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_OR, &set2, options);
8126 LY_CHECK_GOTO(rc, cleanup);
8127
8128 /* eval - just get boolean value actually */
8129 if (set->type == LYXP_SET_SCNODE_SET) {
8130 set_scnode_clear_ctx(&set2);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008131 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008132 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008133 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008134 set_fill_set(set, &set2);
8135 }
8136 }
8137
8138cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008139 lyxp_set_free_content(&orig_set);
8140 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008141 return rc;
8142}
8143
8144/**
8145 * @brief Decide what expression is at the pointer @p exp_idx and evaluate it accordingly.
8146 *
8147 * @param[in] exp Parsed XPath expression.
8148 * @param[in] exp_idx Position in the expression @p exp.
8149 * @param[in] etype Expression type to evaluate.
8150 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8151 * @param[in] options XPath options.
8152 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8153 */
8154static LY_ERR
8155eval_expr_select(struct lyxp_expr *exp, uint16_t *exp_idx, enum lyxp_expr_type etype, struct lyxp_set *set, int options)
8156{
8157 uint16_t i, count;
8158 enum lyxp_expr_type next_etype;
8159 LY_ERR rc;
8160
8161 /* process operator repeats */
8162 if (!exp->repeat[*exp_idx]) {
8163 next_etype = LYXP_EXPR_NONE;
8164 } else {
8165 /* find etype repeat */
8166 for (i = 0; exp->repeat[*exp_idx][i] > etype; ++i);
8167
8168 /* select one-priority lower because etype expression called us */
8169 if (i) {
8170 next_etype = exp->repeat[*exp_idx][i - 1];
8171 /* count repeats for that expression */
8172 for (count = 0; i && exp->repeat[*exp_idx][i - 1] == next_etype; ++count, --i);
8173 } else {
8174 next_etype = LYXP_EXPR_NONE;
8175 }
8176 }
8177
8178 /* decide what expression are we parsing based on the repeat */
8179 switch (next_etype) {
8180 case LYXP_EXPR_OR:
8181 rc = eval_or_expr(exp, exp_idx, count, set, options);
8182 break;
8183 case LYXP_EXPR_AND:
8184 rc = eval_and_expr(exp, exp_idx, count, set, options);
8185 break;
8186 case LYXP_EXPR_EQUALITY:
8187 rc = eval_equality_expr(exp, exp_idx, count, set, options);
8188 break;
8189 case LYXP_EXPR_RELATIONAL:
8190 rc = eval_relational_expr(exp, exp_idx, count, set, options);
8191 break;
8192 case LYXP_EXPR_ADDITIVE:
8193 rc = eval_additive_expr(exp, exp_idx, count, set, options);
8194 break;
8195 case LYXP_EXPR_MULTIPLICATIVE:
8196 rc = eval_multiplicative_expr(exp, exp_idx, count, set, options);
8197 break;
8198 case LYXP_EXPR_UNARY:
8199 rc = eval_unary_expr(exp, exp_idx, count, set, options);
8200 break;
8201 case LYXP_EXPR_UNION:
8202 rc = eval_union_expr(exp, exp_idx, count, set, options);
8203 break;
8204 case LYXP_EXPR_NONE:
8205 rc = eval_path_expr(exp, exp_idx, set, options);
8206 break;
8207 default:
8208 LOGINT_RET(set->ctx);
8209 }
8210
8211 return rc;
8212}
8213
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008214/**
8215 * @brief Get root type.
8216 *
8217 * @param[in] ctx_node Context node.
8218 * @param[in] ctx_scnode Schema context node.
8219 * @param[in] options XPath options.
8220 * @return Root type.
8221 */
8222static enum lyxp_node_type
8223lyxp_get_root_type(const struct lyd_node *ctx_node, const struct lysc_node *ctx_scnode, int options)
8224{
8225 if (options & LYXP_SCNODE_ALL) {
8226 if (options & LYXP_SCNODE) {
8227 /* general root that can access everything */
8228 return LYXP_NODE_ROOT;
8229 } else if (!ctx_scnode || (ctx_scnode->flags & LYS_CONFIG_W)) {
8230 /* root context node can access only config data (because we said so, it is unspecified) */
8231 return LYXP_NODE_ROOT_CONFIG;
8232 } else {
8233 return LYXP_NODE_ROOT;
8234 }
8235 }
8236
8237 if (!ctx_node || (ctx_node->schema->flags & LYS_CONFIG_W)) {
8238 /* root context node can access only config data (because we said so, it is unspecified) */
8239 return LYXP_NODE_ROOT_CONFIG;
8240 }
8241
8242 return LYXP_NODE_ROOT;
8243}
8244
Michal Vasko03ff5a72019-09-11 13:49:33 +02008245LY_ERR
Michal Vaskoecd62de2019-11-13 12:35:11 +01008246lyxp_eval(struct lyxp_expr *exp, LYD_FORMAT format, const struct lys_module *local_mod, const struct lyd_node *ctx_node,
Michal Vaskof03ed032020-03-04 13:31:44 +01008247 enum lyxp_node_type ctx_node_type, const struct lyd_node *tree, struct lyxp_set *set, int options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008248{
Michal Vasko03ff5a72019-09-11 13:49:33 +02008249 uint16_t exp_idx = 0;
8250 LY_ERR rc;
8251
Michal Vaskoecd62de2019-11-13 12:35:11 +01008252 LY_CHECK_ARG_RET(NULL, exp, local_mod, set, LY_EINVAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008253
8254 /* prepare set for evaluation */
8255 exp_idx = 0;
8256 memset(set, 0, sizeof *set);
Michal Vaskod3678892020-05-21 10:06:58 +02008257 set->type = LYXP_SET_NODE_SET;
Michal Vasko9b368d32020-02-14 13:53:31 +01008258 set_insert_node(set, (struct lyd_node *)ctx_node, 0, ctx_node_type, 0);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008259 set->ctx = local_mod->ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008260 set->ctx_node = ctx_node;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008261 set->root_type = lyxp_get_root_type(ctx_node, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008262 set->local_mod = local_mod;
Michal Vaskof03ed032020-03-04 13:31:44 +01008263 set->tree = tree;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008264 set->format = format;
8265
8266 /* evaluate */
8267 rc = eval_expr_select(exp, &exp_idx, 0, set, options);
8268 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02008269 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008270 }
8271
Michal Vasko03ff5a72019-09-11 13:49:33 +02008272 return rc;
8273}
8274
8275#if 0
8276
8277/* full xml printing of set elements, not used currently */
8278
8279void
8280lyxp_set_print_xml(FILE *f, struct lyxp_set *set)
8281{
8282 uint32_t i;
8283 char *str_num;
8284 struct lyout out;
8285
8286 memset(&out, 0, sizeof out);
8287
8288 out.type = LYOUT_STREAM;
8289 out.method.f = f;
8290
8291 switch (set->type) {
8292 case LYXP_SET_EMPTY:
8293 ly_print(&out, "Empty XPath set\n\n");
8294 break;
8295 case LYXP_SET_BOOLEAN:
8296 ly_print(&out, "Boolean XPath set:\n");
8297 ly_print(&out, "%s\n\n", set->value.bool ? "true" : "false");
8298 break;
8299 case LYXP_SET_STRING:
8300 ly_print(&out, "String XPath set:\n");
8301 ly_print(&out, "\"%s\"\n\n", set->value.str);
8302 break;
8303 case LYXP_SET_NUMBER:
8304 ly_print(&out, "Number XPath set:\n");
8305
8306 if (isnan(set->value.num)) {
8307 str_num = strdup("NaN");
8308 } else if ((set->value.num == 0) || (set->value.num == -0.0f)) {
8309 str_num = strdup("0");
8310 } else if (isinf(set->value.num) && !signbit(set->value.num)) {
8311 str_num = strdup("Infinity");
8312 } else if (isinf(set->value.num) && signbit(set->value.num)) {
8313 str_num = strdup("-Infinity");
8314 } else if ((long long)set->value.num == set->value.num) {
8315 if (asprintf(&str_num, "%lld", (long long)set->value.num) == -1) {
8316 str_num = NULL;
8317 }
8318 } else {
8319 if (asprintf(&str_num, "%03.1Lf", set->value.num) == -1) {
8320 str_num = NULL;
8321 }
8322 }
8323 if (!str_num) {
8324 LOGMEM;
8325 return;
8326 }
8327 ly_print(&out, "%s\n\n", str_num);
8328 free(str_num);
8329 break;
8330 case LYXP_SET_NODE_SET:
8331 ly_print(&out, "Node XPath set:\n");
8332
8333 for (i = 0; i < set->used; ++i) {
8334 ly_print(&out, "%d. ", i + 1);
8335 switch (set->node_type[i]) {
8336 case LYXP_NODE_ROOT_ALL:
8337 ly_print(&out, "ROOT all\n\n");
8338 break;
8339 case LYXP_NODE_ROOT_CONFIG:
8340 ly_print(&out, "ROOT config\n\n");
8341 break;
8342 case LYXP_NODE_ROOT_STATE:
8343 ly_print(&out, "ROOT state\n\n");
8344 break;
8345 case LYXP_NODE_ROOT_NOTIF:
8346 ly_print(&out, "ROOT notification \"%s\"\n\n", set->value.nodes[i]->schema->name);
8347 break;
8348 case LYXP_NODE_ROOT_RPC:
8349 ly_print(&out, "ROOT rpc \"%s\"\n\n", set->value.nodes[i]->schema->name);
8350 break;
8351 case LYXP_NODE_ROOT_OUTPUT:
8352 ly_print(&out, "ROOT output \"%s\"\n\n", set->value.nodes[i]->schema->name);
8353 break;
8354 case LYXP_NODE_ELEM:
8355 ly_print(&out, "ELEM \"%s\"\n", set->value.nodes[i]->schema->name);
8356 xml_print_node(&out, 1, set->value.nodes[i], 1, LYP_FORMAT);
8357 ly_print(&out, "\n");
8358 break;
8359 case LYXP_NODE_TEXT:
8360 ly_print(&out, "TEXT \"%s\"\n\n", ((struct lyd_node_leaf_list *)set->value.nodes[i])->value_str);
8361 break;
8362 case LYXP_NODE_ATTR:
8363 ly_print(&out, "ATTR \"%s\" = \"%s\"\n\n", set->value.attrs[i]->name, set->value.attrs[i]->value);
8364 break;
8365 }
8366 }
8367 break;
8368 }
8369}
8370
8371#endif
8372
8373LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008374lyxp_set_cast(struct lyxp_set *set, enum lyxp_set_type target)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008375{
8376 long double num;
8377 char *str;
8378 LY_ERR rc;
8379
8380 if (!set || (set->type == target)) {
8381 return LY_SUCCESS;
8382 }
8383
8384 /* it's not possible to convert anything into a node set */
Michal Vaskod3678892020-05-21 10:06:58 +02008385 assert(target != LYXP_SET_NODE_SET);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008386
8387 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vaskod3678892020-05-21 10:06:58 +02008388 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008389 return LY_EINVAL;
8390 }
8391
8392 /* to STRING */
Michal Vaskod3678892020-05-21 10:06:58 +02008393 if ((target == LYXP_SET_STRING) || ((target == LYXP_SET_NUMBER) && (set->type == LYXP_SET_NODE_SET))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008394 switch (set->type) {
8395 case LYXP_SET_NUMBER:
8396 if (isnan(set->val.num)) {
8397 set->val.str = strdup("NaN");
8398 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8399 } else if ((set->val.num == 0) || (set->val.num == -0.0f)) {
8400 set->val.str = strdup("0");
8401 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8402 } else if (isinf(set->val.num) && !signbit(set->val.num)) {
8403 set->val.str = strdup("Infinity");
8404 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8405 } else if (isinf(set->val.num) && signbit(set->val.num)) {
8406 set->val.str = strdup("-Infinity");
8407 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8408 } else if ((long long)set->val.num == set->val.num) {
8409 if (asprintf(&str, "%lld", (long long)set->val.num) == -1) {
8410 LOGMEM_RET(set->ctx);
8411 }
8412 set->val.str = str;
8413 } else {
8414 if (asprintf(&str, "%03.1Lf", set->val.num) == -1) {
8415 LOGMEM_RET(set->ctx);
8416 }
8417 set->val.str = str;
8418 }
8419 break;
8420 case LYXP_SET_BOOLEAN:
8421 if (set->val.bool) {
8422 set->val.str = strdup("true");
8423 } else {
8424 set->val.str = strdup("false");
8425 }
8426 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM);
8427 break;
8428 case LYXP_SET_NODE_SET:
8429 assert(set->used);
8430
8431 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008432 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008433
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008434 rc = cast_node_set_to_string(set, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008435 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02008436 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008437 set->val.str = str;
8438 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008439 default:
8440 LOGINT_RET(set->ctx);
8441 }
8442 set->type = LYXP_SET_STRING;
8443 }
8444
8445 /* to NUMBER */
8446 if (target == LYXP_SET_NUMBER) {
8447 switch (set->type) {
8448 case LYXP_SET_STRING:
8449 num = cast_string_to_number(set->val.str);
Michal Vaskod3678892020-05-21 10:06:58 +02008450 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008451 set->val.num = num;
8452 break;
8453 case LYXP_SET_BOOLEAN:
8454 if (set->val.bool) {
8455 set->val.num = 1;
8456 } else {
8457 set->val.num = 0;
8458 }
8459 break;
8460 default:
8461 LOGINT_RET(set->ctx);
8462 }
8463 set->type = LYXP_SET_NUMBER;
8464 }
8465
8466 /* to BOOLEAN */
8467 if (target == LYXP_SET_BOOLEAN) {
8468 switch (set->type) {
8469 case LYXP_SET_NUMBER:
8470 if ((set->val.num == 0) || (set->val.num == -0.0f) || isnan(set->val.num)) {
8471 set->val.bool = 0;
8472 } else {
8473 set->val.bool = 1;
8474 }
8475 break;
8476 case LYXP_SET_STRING:
8477 if (set->val.str[0]) {
Michal Vaskod3678892020-05-21 10:06:58 +02008478 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008479 set->val.bool = 1;
8480 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02008481 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008482 set->val.bool = 0;
8483 }
8484 break;
8485 case LYXP_SET_NODE_SET:
Michal Vaskod3678892020-05-21 10:06:58 +02008486 if (set->used) {
8487 lyxp_set_free_content(set);
8488 set->val.bool = 1;
8489 } else {
8490 lyxp_set_free_content(set);
8491 set->val.bool = 0;
8492 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008493 break;
8494 default:
8495 LOGINT_RET(set->ctx);
8496 }
8497 set->type = LYXP_SET_BOOLEAN;
8498 }
8499
Michal Vasko03ff5a72019-09-11 13:49:33 +02008500 return LY_SUCCESS;
8501}
8502
8503LY_ERR
8504lyxp_atomize(struct lyxp_expr *exp, LYD_FORMAT format, const struct lys_module *local_mod, const struct lysc_node *ctx_scnode,
8505 enum lyxp_node_type ctx_scnode_type, struct lyxp_set *set, int options)
8506{
8507 struct ly_ctx *ctx;
8508 uint16_t exp_idx = 0;
8509
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008510 LY_CHECK_ARG_RET(NULL, exp, local_mod, set, LY_EINVAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008511
8512 ctx = local_mod->ctx;
8513
8514 /* prepare set for evaluation */
8515 exp_idx = 0;
8516 memset(set, 0, sizeof *set);
8517 set->type = LYXP_SET_SCNODE_SET;
Michal Vaskoecd62de2019-11-13 12:35:11 +01008518 lyxp_set_scnode_insert_node(set, ctx_scnode, ctx_scnode_type);
Michal Vasko5c4e5892019-11-14 12:31:38 +01008519 set->val.scnodes[0].in_ctx = -2;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008520 set->ctx = ctx;
8521 set->ctx_scnode = ctx_scnode;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008522 set->root_type = lyxp_get_root_type(NULL, ctx_scnode, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008523 set->local_mod = local_mod;
8524 set->format = format;
8525
8526 /* evaluate */
8527 return eval_expr_select(exp, &exp_idx, 0, set, options);
8528}