blob: 4bbf30559876a67e470c5a69b8f4879f0ed2b53c [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
19#include "common.h"
20
Michal Vasko03ff5a72019-09-11 13:49:33 +020021#include <math.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010022#include <ctype.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020023#include <stdint.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010024#include <stdio.h>
25#include <stdlib.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010026#include <string.h>
Michal Vasko03ff5a72019-09-11 13:49:33 +020027#include <errno.h>
28#include <assert.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010029
30#include "xpath.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020031#include "dict.h"
Radek Krejcib1646a92018-11-02 16:08:26 +010032#include "xml.h"
Michal Vasko03ff5a72019-09-11 13:49:33 +020033#include "printer_data.h"
34#include "tree_schema_internal.h"
Michal Vaskod3678892020-05-21 10:06:58 +020035#include "tree_data_internal.h"
Michal Vasko03ff5a72019-09-11 13:49:33 +020036#include "plugins_types.h"
37
Michal Vasko03ff5a72019-09-11 13:49:33 +020038static LY_ERR reparse_or_expr(struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *exp_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +020039static LY_ERR eval_expr_select(struct lyxp_expr *exp, uint16_t *exp_idx, enum lyxp_expr_type etype, struct lyxp_set *set, int options);
40
41/**
42 * @brief Print the type of an XPath \p set.
43 *
44 * @param[in] set Set to use.
45 * @return Set type string.
46 */
47static const char *
48print_set_type(struct lyxp_set *set)
49{
50 switch (set->type) {
Michal Vasko03ff5a72019-09-11 13:49:33 +020051 case LYXP_SET_NODE_SET:
52 return "node set";
53 case LYXP_SET_SCNODE_SET:
54 return "schema node set";
55 case LYXP_SET_BOOLEAN:
56 return "boolean";
57 case LYXP_SET_NUMBER:
58 return "number";
59 case LYXP_SET_STRING:
60 return "string";
61 }
62
63 return NULL;
64}
65
66/**
67 * @brief Print an XPath token \p tok type.
68 *
69 * @param[in] tok Token to use.
70 * @return Token type string.
71 */
72static const char *
73print_token(enum lyxp_token tok)
74{
75 switch (tok) {
76 case LYXP_TOKEN_PAR1:
77 return "(";
78 case LYXP_TOKEN_PAR2:
79 return ")";
80 case LYXP_TOKEN_BRACK1:
81 return "[";
82 case LYXP_TOKEN_BRACK2:
83 return "]";
84 case LYXP_TOKEN_DOT:
85 return ".";
86 case LYXP_TOKEN_DDOT:
87 return "..";
88 case LYXP_TOKEN_AT:
89 return "@";
90 case LYXP_TOKEN_COMMA:
91 return ",";
92 case LYXP_TOKEN_NAMETEST:
93 return "NameTest";
94 case LYXP_TOKEN_NODETYPE:
95 return "NodeType";
96 case LYXP_TOKEN_FUNCNAME:
97 return "FunctionName";
98 case LYXP_TOKEN_OPERATOR_LOG:
99 return "Operator(Logic)";
100 case LYXP_TOKEN_OPERATOR_COMP:
101 return "Operator(Comparison)";
102 case LYXP_TOKEN_OPERATOR_MATH:
103 return "Operator(Math)";
104 case LYXP_TOKEN_OPERATOR_UNI:
105 return "Operator(Union)";
106 case LYXP_TOKEN_OPERATOR_PATH:
107 return "Operator(Path)";
108 case LYXP_TOKEN_LITERAL:
109 return "Literal";
110 case LYXP_TOKEN_NUMBER:
111 return "Number";
112 default:
113 LOGINT(NULL);
114 return "";
115 }
116}
117
118/**
119 * @brief Print the whole expression \p exp to debug output.
120 *
121 * @param[in] exp Expression to use.
122 */
123static void
124print_expr_struct_debug(struct lyxp_expr *exp)
125{
126 uint16_t i, j;
127 char tmp[128];
128
129 if (!exp || (ly_log_level < LY_LLDBG)) {
130 return;
131 }
132
133 LOGDBG(LY_LDGXPATH, "expression \"%s\":", exp->expr);
134 for (i = 0; i < exp->used; ++i) {
135 sprintf(tmp, "\ttoken %s, in expression \"%.*s\"", print_token(exp->tokens[i]), exp->tok_len[i],
136 &exp->expr[exp->tok_pos[i]]);
137 if (exp->repeat[i]) {
138 sprintf(tmp + strlen(tmp), " (repeat %d", exp->repeat[i][0]);
139 for (j = 1; exp->repeat[i][j]; ++j) {
140 sprintf(tmp + strlen(tmp), ", %d", exp->repeat[i][j]);
141 }
142 strcat(tmp, ")");
143 }
144 LOGDBG(LY_LDGXPATH, tmp);
145 }
146}
147
148#ifndef NDEBUG
149
150/**
151 * @brief Print XPath set content to debug output.
152 *
153 * @param[in] set Set to print.
154 */
155static void
156print_set_debug(struct lyxp_set *set)
157{
158 uint32_t i;
159 char *str;
160 int dynamic;
161 struct lyxp_set_node *item;
162 struct lyxp_set_scnode *sitem;
163
164 if (ly_log_level < LY_LLDBG) {
165 return;
166 }
167
168 switch (set->type) {
169 case LYXP_SET_NODE_SET:
170 LOGDBG(LY_LDGXPATH, "set NODE SET:");
171 for (i = 0; i < set->used; ++i) {
172 item = &set->val.nodes[i];
173
174 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +0100175 case LYXP_NODE_NONE:
176 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): NONE", i + 1, item->pos);
177 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200178 case LYXP_NODE_ROOT:
179 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT", i + 1, item->pos);
180 break;
181 case LYXP_NODE_ROOT_CONFIG:
182 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT CONFIG", i + 1, item->pos);
183 break;
184 case LYXP_NODE_ELEM:
185 if ((item->node->schema->nodetype == LYS_LIST)
186 && (((struct lyd_node_inner *)item->node)->child->schema->nodetype == LYS_LEAF)) {
187 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (1st child val: %s)", i + 1, item->pos,
188 item->node->schema->name,
189 (str = (char *)lyd_value2str((struct lyd_node_term *)lyd_node_children(item->node), &dynamic)));
190 if (dynamic) {
191 free(str);
192 }
193 } else if (((struct lyd_node_inner *)item->node)->schema->nodetype == LYS_LEAFLIST) {
194 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (val: %s)", i + 1, item->pos,
195 item->node->schema->name,
196 (str = (char *)lyd_value2str((struct lyd_node_term *)item->node, &dynamic)));
197 if (dynamic) {
198 free(str);
199 }
200 } else {
201 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s", i + 1, item->pos, item->node->schema->name);
202 }
203 break;
204 case LYXP_NODE_TEXT:
205 if (item->node->schema->nodetype & LYS_ANYDATA) {
206 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): TEXT <%s>", i + 1, item->pos,
207 item->node->schema->nodetype == LYS_ANYXML ? "anyxml" : "anydata");
208 } else {
209 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): TEXT %s", i + 1, item->pos,
210 (str = (char *)lyd_value2str((struct lyd_node_term *)item->node, &dynamic)));
211 if (dynamic) {
212 free(str);
213 }
214 }
215 break;
Michal Vasko9f96a052020-03-10 09:41:45 +0100216 case LYXP_NODE_META:
217 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): META %s = %s", i + 1, item->pos, set->val.meta[i].meta->name,
218 set->val.meta[i].meta->value);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200219 break;
220 }
221 }
222 break;
223
224 case LYXP_SET_SCNODE_SET:
225 LOGDBG(LY_LDGXPATH, "set SCNODE SET:");
226 for (i = 0; i < set->used; ++i) {
227 sitem = &set->val.scnodes[i];
228
229 switch (sitem->type) {
230 case LYXP_NODE_ROOT:
231 LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT", i + 1, sitem->in_ctx);
232 break;
233 case LYXP_NODE_ROOT_CONFIG:
234 LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT CONFIG", i + 1, sitem->in_ctx);
235 break;
236 case LYXP_NODE_ELEM:
237 LOGDBG(LY_LDGXPATH, "\t%d (%u): ELEM %s", i + 1, sitem->in_ctx, sitem->scnode->name);
238 break;
239 default:
240 LOGINT(NULL);
241 break;
242 }
243 }
244 break;
245
Michal Vasko03ff5a72019-09-11 13:49:33 +0200246 case LYXP_SET_BOOLEAN:
247 LOGDBG(LY_LDGXPATH, "set BOOLEAN");
248 LOGDBG(LY_LDGXPATH, "\t%s", (set->val.bool ? "true" : "false"));
249 break;
250
251 case LYXP_SET_STRING:
252 LOGDBG(LY_LDGXPATH, "set STRING");
253 LOGDBG(LY_LDGXPATH, "\t%s", set->val.str);
254 break;
255
256 case LYXP_SET_NUMBER:
257 LOGDBG(LY_LDGXPATH, "set NUMBER");
258
259 if (isnan(set->val.num)) {
260 str = strdup("NaN");
261 } else if ((set->val.num == 0) || (set->val.num == -0.0f)) {
262 str = strdup("0");
263 } else if (isinf(set->val.num) && !signbit(set->val.num)) {
264 str = strdup("Infinity");
265 } else if (isinf(set->val.num) && signbit(set->val.num)) {
266 str = strdup("-Infinity");
267 } else if ((long long)set->val.num == set->val.num) {
268 if (asprintf(&str, "%lld", (long long)set->val.num) == -1) {
269 str = NULL;
270 }
271 } else {
272 if (asprintf(&str, "%03.1Lf", set->val.num) == -1) {
273 str = NULL;
274 }
275 }
276 LY_CHECK_ERR_RET(!str, LOGMEM(NULL), );
277
278 LOGDBG(LY_LDGXPATH, "\t%s", str);
279 free(str);
280 }
281}
282
283#endif
284
285/**
286 * @brief Realloc the string \p str.
287 *
288 * @param[in] ctx libyang context for logging.
289 * @param[in] needed How much free space is required.
290 * @param[in,out] str Pointer to the string to use.
291 * @param[in,out] used Used bytes in \p str.
292 * @param[in,out] size Allocated bytes in \p str.
293 * @return LY_ERR
294 */
295static LY_ERR
Michal Vasko52927e22020-03-16 17:26:14 +0100296cast_string_realloc(const struct ly_ctx *ctx, uint16_t needed, char **str, uint16_t *used, uint16_t *size)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200297{
298 if (*size - *used < needed) {
299 do {
300 if ((UINT16_MAX - *size) < LYXP_STRING_CAST_SIZE_STEP) {
301 LOGERR(ctx, LY_EINVAL, "XPath string length limit (%u) reached.", UINT16_MAX);
302 return LY_EINVAL;
303 }
304 *size += LYXP_STRING_CAST_SIZE_STEP;
305 } while (*size - *used < needed);
306 *str = ly_realloc(*str, *size * sizeof(char));
307 LY_CHECK_ERR_RET(!(*str), LOGMEM(ctx), LY_EMEM);
308 }
309
310 return LY_SUCCESS;
311}
312
313/**
314 * @brief Cast nodes recursively to one string @p str.
315 *
316 * @param[in] node Node to cast.
317 * @param[in] fake_cont Whether to put the data into a "fake" container.
318 * @param[in] root_type Type of the XPath root.
319 * @param[in] indent Current indent.
320 * @param[in,out] str Resulting string.
321 * @param[in,out] used Used bytes in @p str.
322 * @param[in,out] size Allocated bytes in @p str.
323 * @return LY_ERR
324 */
325static LY_ERR
326cast_string_recursive(const struct lyd_node *node, int fake_cont, enum lyxp_node_type root_type, uint16_t indent, char **str,
327 uint16_t *used, uint16_t *size)
328{
329 char *buf, *line, *ptr;
330 const char *value_str;
331 int dynamic;
332 const struct lyd_node *child;
333 struct lyd_node_any *any;
334 LY_ERR rc;
335
336 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) {
337 return LY_SUCCESS;
338 }
339
340 if (fake_cont) {
341 rc = cast_string_realloc(LYD_NODE_CTX(node), 1, str, used, size);
342 LY_CHECK_RET(rc);
343 strcpy(*str + (*used - 1), "\n");
344 ++(*used);
345
346 ++indent;
347 }
348
349 switch (node->schema->nodetype) {
350 case LYS_CONTAINER:
351 case LYS_LIST:
352 case LYS_RPC:
353 case LYS_NOTIF:
354 rc = cast_string_realloc(LYD_NODE_CTX(node), 1, str, used, size);
355 LY_CHECK_RET(rc);
356 strcpy(*str + (*used - 1), "\n");
357 ++(*used);
358
359 for (child = lyd_node_children(node); child; child = child->next) {
360 rc = cast_string_recursive(child, 0, root_type, indent + 1, str, used, size);
361 LY_CHECK_RET(rc);
362 }
363
364 break;
365
366 case LYS_LEAF:
367 case LYS_LEAFLIST:
368 value_str = lyd_value2str(((struct lyd_node_term *)node), &dynamic);
369
370 /* print indent */
371 rc = cast_string_realloc(LYD_NODE_CTX(node), indent * 2 + strlen(value_str) + 1, str, used, size);
372 if (rc != LY_SUCCESS) {
373 if (dynamic) {
374 free((char *)value_str);
375 }
376 return rc;
377 }
378 memset(*str + (*used - 1), ' ', indent * 2);
379 *used += indent * 2;
380
381 /* print value */
382 if (*used == 1) {
383 sprintf(*str + (*used - 1), "%s", value_str);
384 *used += strlen(value_str);
385 } else {
386 sprintf(*str + (*used - 1), "%s\n", value_str);
387 *used += strlen(value_str) + 1;
388 }
389 if (dynamic) {
390 free((char *)value_str);
391 }
392
393 break;
394
395 case LYS_ANYXML:
396 case LYS_ANYDATA:
397 any = (struct lyd_node_any *)node;
398 if (!(void *)any->value.tree) {
399 /* no content */
400 buf = strdup("");
401 LY_CHECK_ERR_RET(!buf, LOGMEM(LYD_NODE_CTX(node)), LY_EMEM);
402 } else {
Radek Krejci241f6b52020-05-21 18:13:49 +0200403 struct ly_out *out;
Radek Krejcia5bba312020-01-09 15:41:20 +0100404
Michal Vasko03ff5a72019-09-11 13:49:33 +0200405 switch (any->value_type) {
406 case LYD_ANYDATA_STRING:
407 case LYD_ANYDATA_XML:
408 case LYD_ANYDATA_JSON:
409 buf = strdup(any->value.json);
410 LY_CHECK_ERR_RET(!buf, LOGMEM(LYD_NODE_CTX(node)), LY_EMEM);
411 break;
412 case LYD_ANYDATA_DATATREE:
Radek Krejci241f6b52020-05-21 18:13:49 +0200413 out = ly_out_new_memory(&buf, 0);
Radek Krejcia5bba312020-01-09 15:41:20 +0100414 rc = lyd_print(out, any->value.tree, LYD_XML, LYDP_WITHSIBLINGS);
Radek Krejci241f6b52020-05-21 18:13:49 +0200415 ly_out_free(out, NULL, 0);
Radek Krejcia5bba312020-01-09 15:41:20 +0100416 LY_CHECK_RET(rc < 0, -rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200417 break;
418 /* TODO case LYD_ANYDATA_LYB:
419 LOGERR(LYD_NODE_CTX(node), LY_EINVAL, "Cannot convert LYB anydata into string.");
420 return -1;*/
421 }
422 }
423
424 line = strtok_r(buf, "\n", &ptr);
425 do {
426 rc = cast_string_realloc(LYD_NODE_CTX(node), indent * 2 + strlen(line) + 1, str, used, size);
427 if (rc != LY_SUCCESS) {
428 free(buf);
429 return rc;
430 }
431 memset(*str + (*used - 1), ' ', indent * 2);
432 *used += indent * 2;
433
434 strcpy(*str + (*used - 1), line);
435 *used += strlen(line);
436
437 strcpy(*str + (*used - 1), "\n");
438 *used += 1;
439 } while ((line = strtok_r(NULL, "\n", &ptr)));
440
441 free(buf);
442 break;
443
444 default:
445 LOGINT_RET(LYD_NODE_CTX(node));
446 }
447
448 if (fake_cont) {
449 rc = cast_string_realloc(LYD_NODE_CTX(node), 1, str, used, size);
450 LY_CHECK_RET(rc);
451 strcpy(*str + (*used - 1), "\n");
452 ++(*used);
453
454 --indent;
455 }
456
457 return LY_SUCCESS;
458}
459
460/**
461 * @brief Cast an element into a string.
462 *
463 * @param[in] node Node to cast.
464 * @param[in] fake_cont Whether to put the data into a "fake" container.
465 * @param[in] root_type Type of the XPath root.
466 * @param[out] str Element cast to dynamically-allocated string.
467 * @return LY_ERR
468 */
469static LY_ERR
470cast_string_elem(struct lyd_node *node, int fake_cont, enum lyxp_node_type root_type, char **str)
471{
472 uint16_t used, size;
473 LY_ERR rc;
474
475 *str = malloc(LYXP_STRING_CAST_SIZE_START * sizeof(char));
476 LY_CHECK_ERR_RET(!*str, LOGMEM(LYD_NODE_CTX(node)), LY_EMEM);
477 (*str)[0] = '\0';
478 used = 1;
479 size = LYXP_STRING_CAST_SIZE_START;
480
481 rc = cast_string_recursive(node, fake_cont, root_type, 0, str, &used, &size);
482 if (rc != LY_SUCCESS) {
483 free(*str);
484 return rc;
485 }
486
487 if (size > used) {
488 *str = ly_realloc(*str, used * sizeof(char));
489 LY_CHECK_ERR_RET(!*str, LOGMEM(LYD_NODE_CTX(node)), LY_EMEM);
490 }
491 return LY_SUCCESS;
492}
493
494/**
495 * @brief Cast a LYXP_SET_NODE_SET set into a string.
496 * Context position aware.
497 *
498 * @param[in] set Set to cast.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200499 * @param[out] str Cast dynamically-allocated string.
500 * @return LY_ERR
501 */
502static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100503cast_node_set_to_string(struct lyxp_set *set, char **str)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200504{
Michal Vasko03ff5a72019-09-11 13:49:33 +0200505 int dynamic;
506
Michal Vasko03ff5a72019-09-11 13:49:33 +0200507 switch (set->val.nodes[0].type) {
Michal Vasko2caefc12019-11-14 16:07:56 +0100508 case LYXP_NODE_NONE:
509 /* invalid */
510 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200511 case LYXP_NODE_ROOT:
512 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100513 return cast_string_elem(set->val.nodes[0].node, 1, set->root_type, str);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200514 case LYXP_NODE_ELEM:
515 case LYXP_NODE_TEXT:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100516 return cast_string_elem(set->val.nodes[0].node, 0, set->root_type, str);
Michal Vasko9f96a052020-03-10 09:41:45 +0100517 case LYXP_NODE_META:
518 *str = (char *)lyd_meta2str(set->val.meta[0].meta, &dynamic);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200519 if (!dynamic) {
520 *str = strdup(*str);
521 if (!*str) {
522 LOGMEM_RET(set->ctx);
523 }
524 }
525 return LY_SUCCESS;
526 }
527
528 LOGINT_RET(set->ctx);
529}
530
531/**
532 * @brief Cast a string into an XPath number.
533 *
534 * @param[in] str String to use.
535 * @return Cast number.
536 */
537static long double
538cast_string_to_number(const char *str)
539{
540 long double num;
541 char *ptr;
542
543 errno = 0;
544 num = strtold(str, &ptr);
545 if (errno || *ptr) {
546 num = NAN;
547 }
548 return num;
549}
550
551/**
552 * @brief Callback for checking value equality.
553 *
554 * @param[in] val1_p First value.
555 * @param[in] val2_p Second value.
556 * @param[in] mod Whether hash table is being modified.
557 * @param[in] cb_data Callback data.
558 * @return 0 if not equal, non-zero if equal.
559 */
560static int
561set_values_equal_cb(void *val1_p, void *val2_p, int UNUSED(mod), void *UNUSED(cb_data))
562{
563 struct lyxp_set_hash_node *val1, *val2;
564
565 val1 = (struct lyxp_set_hash_node *)val1_p;
566 val2 = (struct lyxp_set_hash_node *)val2_p;
567
568 if ((val1->node == val2->node) && (val1->type == val2->type)) {
569 return 1;
570 }
571
572 return 0;
573}
574
575/**
576 * @brief Insert node and its hash into set.
577 *
578 * @param[in] set et to insert to.
579 * @param[in] node Node with hash.
580 * @param[in] type Node type.
581 */
582static void
583set_insert_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type)
584{
585 int r;
586 uint32_t i, hash;
587 struct lyxp_set_hash_node hnode;
588
589 if (!set->ht && (set->used >= LYD_HT_MIN_ITEMS)) {
590 /* create hash table and add all the nodes */
591 set->ht = lyht_new(1, sizeof(struct lyxp_set_hash_node), set_values_equal_cb, NULL, 1);
592 for (i = 0; i < set->used; ++i) {
593 hnode.node = set->val.nodes[i].node;
594 hnode.type = set->val.nodes[i].type;
595
596 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
597 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
598 hash = dict_hash_multi(hash, NULL, 0);
599
600 r = lyht_insert(set->ht, &hnode, hash, NULL);
601 assert(!r);
602 (void)r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200603
Michal Vasko9d6befd2019-12-11 14:56:56 +0100604 if (hnode.node == node) {
605 /* it was just added, do not add it twice */
606 node = NULL;
607 }
608 }
609 }
610
611 if (set->ht && node) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200612 /* add the new node into hash table */
613 hnode.node = node;
614 hnode.type = type;
615
616 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
617 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
618 hash = dict_hash_multi(hash, NULL, 0);
619
620 r = lyht_insert(set->ht, &hnode, hash, NULL);
621 assert(!r);
622 (void)r;
623 }
624}
625
626/**
627 * @brief Remove node and its hash from set.
628 *
629 * @param[in] set Set to remove from.
630 * @param[in] node Node to remove.
631 * @param[in] type Node type.
632 */
633static void
634set_remove_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type)
635{
636 int r;
637 struct lyxp_set_hash_node hnode;
638 uint32_t hash;
639
640 if (set->ht) {
641 hnode.node = node;
642 hnode.type = type;
643
644 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
645 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
646 hash = dict_hash_multi(hash, NULL, 0);
647
648 r = lyht_remove(set->ht, &hnode, hash);
649 assert(!r);
650 (void)r;
651
652 if (!set->ht->used) {
653 lyht_free(set->ht);
654 set->ht = NULL;
655 }
656 }
657}
658
659/**
660 * @brief Check whether node is in set based on its hash.
661 *
662 * @param[in] set Set to search in.
663 * @param[in] node Node to search for.
664 * @param[in] type Node type.
665 * @param[in] skip_idx Index in @p set to skip.
666 * @return LY_ERR
667 */
668static LY_ERR
669set_dup_node_hash_check(const struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type, int skip_idx)
670{
671 struct lyxp_set_hash_node hnode, *match_p;
672 uint32_t hash;
673
674 hnode.node = node;
675 hnode.type = type;
676
677 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
678 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
679 hash = dict_hash_multi(hash, NULL, 0);
680
681 if (!lyht_find(set->ht, &hnode, hash, (void **)&match_p)) {
682 if ((skip_idx > -1) && (set->val.nodes[skip_idx].node == match_p->node) && (set->val.nodes[skip_idx].type == match_p->type)) {
683 /* we found it on the index that should be skipped, find another */
684 hnode = *match_p;
685 if (lyht_find_next(set->ht, &hnode, hash, (void **)&match_p)) {
686 /* none other found */
687 return LY_SUCCESS;
688 }
689 }
690
691 return LY_EEXIST;
692 }
693
694 /* not found */
695 return LY_SUCCESS;
696}
697
Michal Vaskod3678892020-05-21 10:06:58 +0200698void
699lyxp_set_free_content(struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200700{
701 if (!set) {
702 return;
703 }
704
705 if (set->type == LYXP_SET_NODE_SET) {
706 free(set->val.nodes);
707 lyht_free(set->ht);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200708 } else if (set->type == LYXP_SET_SCNODE_SET) {
709 free(set->val.scnodes);
Michal Vaskod3678892020-05-21 10:06:58 +0200710 lyht_free(set->ht);
711 } else {
712 if (set->type == LYXP_SET_STRING) {
713 free(set->val.str);
714 }
715 set->type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200716 }
Michal Vaskod3678892020-05-21 10:06:58 +0200717
718 set->val.nodes = NULL;
719 set->used = 0;
720 set->size = 0;
721 set->ht = NULL;
722 set->ctx_pos = 0;
723 set->ctx_pos = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200724}
725
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100726/**
727 * @brief Free dynamically-allocated set.
728 *
729 * @param[in] set Set to free.
730 */
731static void
Michal Vasko03ff5a72019-09-11 13:49:33 +0200732lyxp_set_free(struct lyxp_set *set)
733{
734 if (!set) {
735 return;
736 }
737
Michal Vaskod3678892020-05-21 10:06:58 +0200738 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200739 free(set);
740}
741
742/**
743 * @brief Initialize set context.
744 *
745 * @param[in] new Set to initialize.
746 * @param[in] set Arbitrary initialized set.
747 */
748static void
749set_init(struct lyxp_set *new, struct lyxp_set *set)
750{
751 memset(new, 0, sizeof *new);
Michal Vasko02a77382019-09-12 11:47:35 +0200752 if (set) {
753 new->ctx = set->ctx;
754 new->ctx_node = set->ctx_node;
Michal Vasko588112f2019-12-10 14:51:53 +0100755 new->root_type = set->root_type;
Michal Vasko02a77382019-09-12 11:47:35 +0200756 new->local_mod = set->local_mod;
Michal Vaskof03ed032020-03-04 13:31:44 +0100757 new->tree = set->tree;
Michal Vasko02a77382019-09-12 11:47:35 +0200758 new->format = set->format;
759 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200760}
761
762/**
763 * @brief Create a deep copy of a set.
764 *
765 * @param[in] set Set to copy.
766 * @return Copy of @p set.
767 */
768static struct lyxp_set *
769set_copy(struct lyxp_set *set)
770{
771 struct lyxp_set *ret;
772 uint16_t i;
Michal Vaskoba716542019-12-16 10:01:58 +0100773 int idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200774
775 if (!set) {
776 return NULL;
777 }
778
779 ret = malloc(sizeof *ret);
780 LY_CHECK_ERR_RET(!ret, LOGMEM(set->ctx), NULL);
781 set_init(ret, set);
782
783 if (set->type == LYXP_SET_SCNODE_SET) {
784 ret->type = set->type;
785
786 for (i = 0; i < set->used; ++i) {
Michal Vaskoba716542019-12-16 10:01:58 +0100787 if ((set->val.scnodes[i].in_ctx == 1) || (set->val.scnodes[i].in_ctx == -2)) {
788 idx = lyxp_set_scnode_insert_node(ret, set->val.scnodes[i].scnode, set->val.scnodes[i].type);
Michal Vasko3f27c522020-01-06 08:37:49 +0100789 /* coverity seems to think scnodes can be NULL */
790 if ((idx == -1) || !ret->val.scnodes) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200791 lyxp_set_free(ret);
792 return NULL;
793 }
Michal Vaskoba716542019-12-16 10:01:58 +0100794 ret->val.scnodes[idx].in_ctx = set->val.scnodes[i].in_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200795 }
796 }
797 } else if (set->type == LYXP_SET_NODE_SET) {
798 ret->type = set->type;
799 ret->val.nodes = malloc(set->used * sizeof *ret->val.nodes);
800 LY_CHECK_ERR_RET(!ret->val.nodes, LOGMEM(set->ctx); free(ret), NULL);
801 memcpy(ret->val.nodes, set->val.nodes, set->used * sizeof *ret->val.nodes);
802
803 ret->used = ret->size = set->used;
804 ret->ctx_pos = set->ctx_pos;
805 ret->ctx_size = set->ctx_size;
806 ret->ht = lyht_dup(set->ht);
807 } else {
808 memcpy(ret, set, sizeof *ret);
809 if (set->type == LYXP_SET_STRING) {
810 ret->val.str = strdup(set->val.str);
811 LY_CHECK_ERR_RET(!ret->val.str, LOGMEM(set->ctx); free(ret), NULL);
812 }
813 }
814
815 return ret;
816}
817
818/**
819 * @brief Fill XPath set with a string. Any current data are disposed of.
820 *
821 * @param[in] set Set to fill.
822 * @param[in] string String to fill into \p set.
823 * @param[in] str_len Length of \p string. 0 is a valid value!
824 */
825static void
826set_fill_string(struct lyxp_set *set, const char *string, uint16_t str_len)
827{
Michal Vaskod3678892020-05-21 10:06:58 +0200828 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200829
830 set->type = LYXP_SET_STRING;
831 if ((str_len == 0) && (string[0] != '\0')) {
832 string = "";
833 }
834 set->val.str = strndup(string, str_len);
835}
836
837/**
838 * @brief Fill XPath set with a number. Any current data are disposed of.
839 *
840 * @param[in] set Set to fill.
841 * @param[in] number Number to fill into \p set.
842 */
843static void
844set_fill_number(struct lyxp_set *set, long double number)
845{
Michal Vaskod3678892020-05-21 10:06:58 +0200846 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200847
848 set->type = LYXP_SET_NUMBER;
849 set->val.num = number;
850}
851
852/**
853 * @brief Fill XPath set with a boolean. Any current data are disposed of.
854 *
855 * @param[in] set Set to fill.
856 * @param[in] boolean Boolean to fill into \p set.
857 */
858static void
859set_fill_boolean(struct lyxp_set *set, int boolean)
860{
Michal Vaskod3678892020-05-21 10:06:58 +0200861 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200862
863 set->type = LYXP_SET_BOOLEAN;
864 set->val.bool = boolean;
865}
866
867/**
868 * @brief Fill XPath set with the value from another set (deep assign).
869 * Any current data are disposed of.
870 *
871 * @param[in] trg Set to fill.
872 * @param[in] src Source set to copy into \p trg.
873 */
874static void
875set_fill_set(struct lyxp_set *trg, struct lyxp_set *src)
876{
877 if (!trg || !src) {
878 return;
879 }
880
881 if (trg->type == LYXP_SET_NODE_SET) {
882 free(trg->val.nodes);
883 } else if (trg->type == LYXP_SET_STRING) {
884 free(trg->val.str);
885 }
886 set_init(trg, src);
887
888 if (src->type == LYXP_SET_SCNODE_SET) {
889 trg->type = LYXP_SET_SCNODE_SET;
890 trg->used = src->used;
891 trg->size = src->used;
892
893 trg->val.scnodes = ly_realloc(trg->val.scnodes, trg->size * sizeof *trg->val.scnodes);
894 LY_CHECK_ERR_RET(!trg->val.scnodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), );
895 memcpy(trg->val.scnodes, src->val.scnodes, src->used * sizeof *src->val.scnodes);
896 } else if (src->type == LYXP_SET_BOOLEAN) {
897 set_fill_boolean(trg, src->val.bool);
898 } else if (src->type == LYXP_SET_NUMBER) {
899 set_fill_number(trg, src->val.num);
900 } else if (src->type == LYXP_SET_STRING) {
901 set_fill_string(trg, src->val.str, strlen(src->val.str));
902 } else {
903 if (trg->type == LYXP_SET_NODE_SET) {
904 free(trg->val.nodes);
905 } else if (trg->type == LYXP_SET_STRING) {
906 free(trg->val.str);
907 }
908
Michal Vaskod3678892020-05-21 10:06:58 +0200909 assert(src->type == LYXP_SET_NODE_SET);
910
911 trg->type = LYXP_SET_NODE_SET;
912 trg->used = src->used;
913 trg->size = src->used;
914 trg->ctx_pos = src->ctx_pos;
915 trg->ctx_size = src->ctx_size;
916
917 trg->val.nodes = malloc(trg->used * sizeof *trg->val.nodes);
918 LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), );
919 memcpy(trg->val.nodes, src->val.nodes, src->used * sizeof *src->val.nodes);
920 if (src->ht) {
921 trg->ht = lyht_dup(src->ht);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200922 } else {
Michal Vaskod3678892020-05-21 10:06:58 +0200923 trg->ht = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200924 }
925 }
926}
927
928/**
929 * @brief Clear context of all schema nodes.
930 *
931 * @param[in] set Set to clear.
932 */
933static void
934set_scnode_clear_ctx(struct lyxp_set *set)
935{
936 uint32_t i;
937
938 for (i = 0; i < set->used; ++i) {
939 if (set->val.scnodes[i].in_ctx == 1) {
940 set->val.scnodes[i].in_ctx = 0;
Michal Vasko5c4e5892019-11-14 12:31:38 +0100941 } else if (set->val.scnodes[i].in_ctx == -2) {
942 set->val.scnodes[i].in_ctx = -1;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200943 }
944 }
945}
946
947/**
948 * @brief Remove a node from a set. Removing last node changes
949 * set into LYXP_SET_EMPTY. Context position aware.
950 *
951 * @param[in] set Set to use.
952 * @param[in] idx Index from @p set of the node to be removed.
953 */
954static void
955set_remove_node(struct lyxp_set *set, uint32_t idx)
956{
957 assert(set && (set->type == LYXP_SET_NODE_SET));
958 assert(idx < set->used);
959
960 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
961
962 --set->used;
963 if (set->used) {
964 memmove(&set->val.nodes[idx], &set->val.nodes[idx + 1],
965 (set->used - idx) * sizeof *set->val.nodes);
966 } else {
Michal Vaskod3678892020-05-21 10:06:58 +0200967 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200968 }
969}
970
971/**
Michal Vasko2caefc12019-11-14 16:07:56 +0100972 * @brief Remove a node from a set by setting its type to LYXP_NODE_NONE.
Michal Vasko57eab132019-09-24 11:46:26 +0200973 *
974 * @param[in] set Set to use.
975 * @param[in] idx Index from @p set of the node to be removed.
976 */
977static void
Michal Vasko2caefc12019-11-14 16:07:56 +0100978set_remove_node_none(struct lyxp_set *set, uint32_t idx)
Michal Vasko57eab132019-09-24 11:46:26 +0200979{
980 assert(set && (set->type == LYXP_SET_NODE_SET));
981 assert(idx < set->used);
982
Michal Vasko2caefc12019-11-14 16:07:56 +0100983 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
984 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
985 }
986 set->val.nodes[idx].type = LYXP_NODE_NONE;
Michal Vasko57eab132019-09-24 11:46:26 +0200987}
988
989/**
Michal Vasko2caefc12019-11-14 16:07:56 +0100990 * @brief Remove all LYXP_NODE_NONE nodes from a set. Removing last node changes
Michal Vasko57eab132019-09-24 11:46:26 +0200991 * set into LYXP_SET_EMPTY. Context position aware.
992 *
993 * @param[in] set Set to consolidate.
994 */
995static void
Michal Vasko2caefc12019-11-14 16:07:56 +0100996set_remove_nodes_none(struct lyxp_set *set)
Michal Vasko57eab132019-09-24 11:46:26 +0200997{
998 uint16_t i, orig_used, end;
999 int32_t start;
1000
Michal Vaskod3678892020-05-21 10:06:58 +02001001 assert(set);
Michal Vasko57eab132019-09-24 11:46:26 +02001002
1003 orig_used = set->used;
1004 set->used = 0;
1005 for (i = 0; i < orig_used;) {
1006 start = -1;
1007 do {
Michal Vasko2caefc12019-11-14 16:07:56 +01001008 if ((set->val.nodes[i].type != LYXP_NODE_NONE) && (start == -1)) {
Michal Vasko57eab132019-09-24 11:46:26 +02001009 start = i;
Michal Vasko2caefc12019-11-14 16:07:56 +01001010 } else if ((start > -1) && (set->val.nodes[i].type == LYXP_NODE_NONE)) {
Michal Vasko57eab132019-09-24 11:46:26 +02001011 end = i;
1012 ++i;
1013 break;
1014 }
1015
1016 ++i;
1017 if (i == orig_used) {
1018 end = i;
1019 }
1020 } while (i < orig_used);
1021
1022 if (start > -1) {
1023 /* move the whole chunk of valid nodes together */
1024 if (set->used != (unsigned)start) {
1025 memmove(&set->val.nodes[set->used], &set->val.nodes[start], (end - start) * sizeof *set->val.nodes);
1026 }
1027 set->used += end - start;
1028 }
1029 }
Michal Vasko57eab132019-09-24 11:46:26 +02001030}
1031
1032/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02001033 * @brief Check for duplicates in a node set.
1034 *
1035 * @param[in] set Set to check.
1036 * @param[in] node Node to look for in @p set.
1037 * @param[in] node_type Type of @p node.
1038 * @param[in] skip_idx Index from @p set to skip.
1039 * @return LY_ERR
1040 */
1041static LY_ERR
1042set_dup_node_check(const struct lyxp_set *set, const struct lyd_node *node, enum lyxp_node_type node_type, int skip_idx)
1043{
1044 uint32_t i;
1045
Michal Vasko2caefc12019-11-14 16:07:56 +01001046 if (set->ht && node) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001047 return set_dup_node_hash_check(set, (struct lyd_node *)node, node_type, skip_idx);
1048 }
1049
1050 for (i = 0; i < set->used; ++i) {
1051 if ((skip_idx > -1) && (i == (unsigned)skip_idx)) {
1052 continue;
1053 }
1054
1055 if ((set->val.nodes[i].node == node) && (set->val.nodes[i].type == node_type)) {
1056 return LY_EEXIST;
1057 }
1058 }
1059
1060 return LY_SUCCESS;
1061}
1062
Michal Vaskoecd62de2019-11-13 12:35:11 +01001063int
1064lyxp_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 +02001065{
1066 uint32_t i;
1067
1068 for (i = 0; i < set->used; ++i) {
1069 if ((skip_idx > -1) && (i == (unsigned)skip_idx)) {
1070 continue;
1071 }
1072
1073 if ((set->val.scnodes[i].scnode == node) && (set->val.scnodes[i].type == node_type)) {
1074 return i;
1075 }
1076 }
1077
1078 return -1;
1079}
1080
Michal Vaskoecd62de2019-11-13 12:35:11 +01001081void
1082lyxp_set_scnode_merge(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001083{
1084 uint32_t orig_used, i, j;
1085
Michal Vaskod3678892020-05-21 10:06:58 +02001086 assert((set1->type == LYXP_SET_SCNODE_SET) && (set2->type == LYXP_SET_SCNODE_SET));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001087
Michal Vaskod3678892020-05-21 10:06:58 +02001088 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001089 return;
1090 }
1091
Michal Vaskod3678892020-05-21 10:06:58 +02001092 if (!set1->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001093 memcpy(set1, set2, sizeof *set1);
1094 return;
1095 }
1096
1097 if (set1->used + set2->used > set1->size) {
1098 set1->size = set1->used + set2->used;
1099 set1->val.scnodes = ly_realloc(set1->val.scnodes, set1->size * sizeof *set1->val.scnodes);
1100 LY_CHECK_ERR_RET(!set1->val.scnodes, LOGMEM(set1->ctx), );
1101 }
1102
1103 orig_used = set1->used;
1104
1105 for (i = 0; i < set2->used; ++i) {
1106 for (j = 0; j < orig_used; ++j) {
1107 /* detect duplicities */
1108 if (set1->val.scnodes[j].scnode == set2->val.scnodes[i].scnode) {
1109 break;
1110 }
1111 }
1112
1113 if (j == orig_used) {
1114 memcpy(&set1->val.scnodes[set1->used], &set2->val.scnodes[i], sizeof *set2->val.scnodes);
1115 ++set1->used;
1116 }
1117 }
1118
Michal Vaskod3678892020-05-21 10:06:58 +02001119 lyxp_set_free_content(set2);
1120 set2->type = LYXP_SET_SCNODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001121}
1122
1123/**
1124 * @brief Insert a node into a set. Context position aware.
1125 *
1126 * @param[in] set Set to use.
1127 * @param[in] node Node to insert to @p set.
1128 * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting.
1129 * @param[in] node_type Node type of @p node.
1130 * @param[in] idx Index in @p set to insert into.
1131 */
1132static void
1133set_insert_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx)
1134{
Michal Vaskod3678892020-05-21 10:06:58 +02001135 assert(set && (set->type == LYXP_SET_NODE_SET));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001136
Michal Vaskod3678892020-05-21 10:06:58 +02001137 if (!set->size) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001138 /* first item */
1139 if (idx) {
1140 /* no real harm done, but it is a bug */
1141 LOGINT(set->ctx);
1142 idx = 0;
1143 }
1144 set->val.nodes = malloc(LYXP_SET_SIZE_START * sizeof *set->val.nodes);
1145 LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), );
1146 set->type = LYXP_SET_NODE_SET;
1147 set->used = 0;
1148 set->size = LYXP_SET_SIZE_START;
1149 set->ctx_pos = 1;
1150 set->ctx_size = 1;
1151 set->ht = NULL;
1152 } else {
1153 /* not an empty set */
1154 if (set->used == set->size) {
1155
1156 /* set is full */
1157 set->val.nodes = ly_realloc(set->val.nodes, (set->size + LYXP_SET_SIZE_STEP) * sizeof *set->val.nodes);
1158 LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), );
1159 set->size += LYXP_SET_SIZE_STEP;
1160 }
1161
1162 if (idx > set->used) {
1163 LOGINT(set->ctx);
1164 idx = set->used;
1165 }
1166
1167 /* make space for the new node */
1168 if (idx < set->used) {
1169 memmove(&set->val.nodes[idx + 1], &set->val.nodes[idx], (set->used - idx) * sizeof *set->val.nodes);
1170 }
1171 }
1172
1173 /* finally assign the value */
1174 set->val.nodes[idx].node = (struct lyd_node *)node;
1175 set->val.nodes[idx].type = node_type;
1176 set->val.nodes[idx].pos = pos;
1177 ++set->used;
1178
Michal Vasko2caefc12019-11-14 16:07:56 +01001179 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1180 set_insert_node_hash(set, (struct lyd_node *)node, node_type);
1181 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001182}
1183
Michal Vaskoecd62de2019-11-13 12:35:11 +01001184int
1185lyxp_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 +02001186{
1187 int ret;
1188
1189 assert(set->type == LYXP_SET_SCNODE_SET);
1190
Michal Vaskoecd62de2019-11-13 12:35:11 +01001191 ret = lyxp_set_scnode_dup_node_check(set, node, node_type, -1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001192 if (ret > -1) {
1193 set->val.scnodes[ret].in_ctx = 1;
1194 } else {
1195 if (set->used == set->size) {
1196 set->val.scnodes = ly_realloc(set->val.scnodes, (set->size + LYXP_SET_SIZE_STEP) * sizeof *set->val.scnodes);
1197 LY_CHECK_ERR_RET(!set->val.scnodes, LOGMEM(set->ctx), -1);
1198 set->size += LYXP_SET_SIZE_STEP;
1199 }
1200
1201 ret = set->used;
1202 set->val.scnodes[ret].scnode = (struct lysc_node *)node;
1203 set->val.scnodes[ret].type = node_type;
1204 set->val.scnodes[ret].in_ctx = 1;
1205 ++set->used;
1206 }
1207
1208 return ret;
1209}
1210
1211/**
1212 * @brief Replace a node in a set with another. Context position aware.
1213 *
1214 * @param[in] set Set to use.
1215 * @param[in] node Node to insert to @p set.
1216 * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting.
1217 * @param[in] node_type Node type of @p node.
1218 * @param[in] idx Index in @p set of the node to replace.
1219 */
1220static void
1221set_replace_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx)
1222{
1223 assert(set && (idx < set->used));
1224
Michal Vasko2caefc12019-11-14 16:07:56 +01001225 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1226 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1227 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001228 set->val.nodes[idx].node = (struct lyd_node *)node;
1229 set->val.nodes[idx].type = node_type;
1230 set->val.nodes[idx].pos = pos;
Michal Vasko2caefc12019-11-14 16:07:56 +01001231 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1232 set_insert_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1233 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001234}
1235
1236/**
1237 * @brief Set all nodes with ctx 1 to a new unique context value.
1238 *
1239 * @param[in] set Set to modify.
1240 * @return New context value.
1241 */
Michal Vasko5c4e5892019-11-14 12:31:38 +01001242static int32_t
Michal Vasko03ff5a72019-09-11 13:49:33 +02001243set_scnode_new_in_ctx(struct lyxp_set *set)
1244{
Michal Vasko5c4e5892019-11-14 12:31:38 +01001245 uint32_t i;
1246 int32_t ret_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001247
1248 assert(set->type == LYXP_SET_SCNODE_SET);
1249
1250 ret_ctx = 3;
1251retry:
1252 for (i = 0; i < set->used; ++i) {
1253 if (set->val.scnodes[i].in_ctx >= ret_ctx) {
1254 ret_ctx = set->val.scnodes[i].in_ctx + 1;
1255 goto retry;
1256 }
1257 }
1258 for (i = 0; i < set->used; ++i) {
1259 if (set->val.scnodes[i].in_ctx == 1) {
1260 set->val.scnodes[i].in_ctx = ret_ctx;
1261 }
1262 }
1263
1264 return ret_ctx;
1265}
1266
1267/**
1268 * @brief Get unique @p node position in the data.
1269 *
1270 * @param[in] node Node to find.
1271 * @param[in] node_type Node type of @p node.
1272 * @param[in] root Root node.
1273 * @param[in] root_type Type of the XPath @p root node.
1274 * @param[in] prev Node that we think is before @p node in DFS from @p root. Can optionally
1275 * be used to increase efficiency and start the DFS from this node.
1276 * @param[in] prev_pos Node @p prev position. Optional, but must be set if @p prev is set.
1277 * @return Node position.
1278 */
1279static uint32_t
1280get_node_pos(const struct lyd_node *node, enum lyxp_node_type node_type, const struct lyd_node *root,
1281 enum lyxp_node_type root_type, const struct lyd_node **prev, uint32_t *prev_pos)
1282{
1283 const struct lyd_node *next, *elem, *top_sibling;
1284 uint32_t pos = 1;
1285
1286 assert(prev && prev_pos && !root->prev->next);
1287
1288 if ((node_type == LYXP_NODE_ROOT) || (node_type == LYXP_NODE_ROOT_CONFIG)) {
1289 return 0;
1290 }
1291
1292 if (*prev) {
1293 /* start from the previous element instead from the root */
1294 elem = next = *prev;
1295 pos = *prev_pos;
1296 for (top_sibling = elem; top_sibling->parent; top_sibling = (struct lyd_node *)top_sibling->parent);
1297 goto dfs_search;
1298 }
1299
1300 for (top_sibling = root; top_sibling; top_sibling = top_sibling->next) {
1301 /* TREE DFS */
1302 LYD_TREE_DFS_BEGIN(top_sibling, next, elem) {
1303dfs_search:
1304 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (elem->schema->flags & LYS_CONFIG_R)) {
1305 goto skip_children;
1306 }
1307
1308 if (elem == node) {
1309 break;
1310 }
1311 ++pos;
1312
1313 /* TREE DFS END */
1314 /* select element for the next run - children first,
1315 * child exception for lyd_node_leaf and lyd_node_leaflist, but not the root */
1316 if (elem->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) {
1317 next = NULL;
1318 } else {
1319 next = lyd_node_children(elem);
1320 }
1321 if (!next) {
1322skip_children:
1323 /* no children */
1324 if (elem == top_sibling) {
1325 /* we are done, root has no children */
1326 elem = NULL;
1327 break;
1328 }
1329 /* try siblings */
1330 next = elem->next;
1331 }
1332 while (!next) {
1333 /* no siblings, go back through parents */
1334 if (elem->parent == top_sibling->parent) {
1335 /* we are done, no next element to process */
1336 elem = NULL;
1337 break;
1338 }
1339 /* parent is already processed, go to its sibling */
1340 elem = (struct lyd_node *)elem->parent;
1341 next = elem->next;
1342 }
1343 }
1344
1345 /* node found */
1346 if (elem) {
1347 break;
1348 }
1349 }
1350
1351 if (!elem) {
1352 if (!(*prev)) {
1353 /* we went from root and failed to find it, cannot be */
1354 LOGINT(node->schema->module->ctx);
1355 return 0;
1356 } else {
1357 *prev = NULL;
1358 *prev_pos = 0;
1359
1360 elem = next = top_sibling = root;
1361 pos = 1;
1362 goto dfs_search;
1363 }
1364 }
1365
1366 /* remember the last found node for next time */
1367 *prev = node;
1368 *prev_pos = pos;
1369
1370 return pos;
1371}
1372
1373/**
1374 * @brief Assign (fill) missing node positions.
1375 *
1376 * @param[in] set Set to fill positions in.
1377 * @param[in] root Context root node.
1378 * @param[in] root_type Context root type.
1379 * @return LY_ERR
1380 */
1381static LY_ERR
1382set_assign_pos(struct lyxp_set *set, const struct lyd_node *root, enum lyxp_node_type root_type)
1383{
1384 const struct lyd_node *prev = NULL, *tmp_node;
1385 uint32_t i, tmp_pos = 0;
1386
1387 for (i = 0; i < set->used; ++i) {
1388 if (!set->val.nodes[i].pos) {
1389 tmp_node = NULL;
1390 switch (set->val.nodes[i].type) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001391 case LYXP_NODE_META:
1392 tmp_node = set->val.meta[i].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001393 if (!tmp_node) {
1394 LOGINT_RET(root->schema->module->ctx);
1395 }
1396 /* fallthrough */
1397 case LYXP_NODE_ELEM:
1398 case LYXP_NODE_TEXT:
1399 if (!tmp_node) {
1400 tmp_node = set->val.nodes[i].node;
1401 }
1402 set->val.nodes[i].pos = get_node_pos(tmp_node, set->val.nodes[i].type, root, root_type, &prev, &tmp_pos);
1403 break;
1404 default:
1405 /* all roots have position 0 */
1406 break;
1407 }
1408 }
1409 }
1410
1411 return LY_SUCCESS;
1412}
1413
1414/**
Michal Vasko9f96a052020-03-10 09:41:45 +01001415 * @brief Get unique @p meta position in the parent metadata.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001416 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001417 * @param[in] meta Metadata to use.
1418 * @return Metadata position.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001419 */
1420static uint16_t
Michal Vasko9f96a052020-03-10 09:41:45 +01001421get_meta_pos(struct lyd_meta *meta)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001422{
1423 uint16_t pos = 0;
Michal Vasko9f96a052020-03-10 09:41:45 +01001424 struct lyd_meta *meta2;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001425
Michal Vasko9f96a052020-03-10 09:41:45 +01001426 for (meta2 = meta->parent->meta; meta2 && (meta2 != meta); meta2 = meta2->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001427 ++pos;
1428 }
1429
Michal Vasko9f96a052020-03-10 09:41:45 +01001430 assert(meta2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001431 return pos;
1432}
1433
1434/**
1435 * @brief Compare 2 nodes in respect to XPath document order.
1436 *
1437 * @param[in] item1 1st node.
1438 * @param[in] item2 2nd node.
1439 * @return If 1st > 2nd returns 1, 1st == 2nd returns 0, and 1st < 2nd returns -1.
1440 */
1441static int
1442set_sort_compare(struct lyxp_set_node *item1, struct lyxp_set_node *item2)
1443{
Michal Vasko9f96a052020-03-10 09:41:45 +01001444 uint32_t meta_pos1 = 0, meta_pos2 = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001445
1446 if (item1->pos < item2->pos) {
1447 return -1;
1448 }
1449
1450 if (item1->pos > item2->pos) {
1451 return 1;
1452 }
1453
1454 /* node positions are equal, the fun case */
1455
1456 /* 1st ELEM - == - 2nd TEXT, 1st TEXT - == - 2nd ELEM */
1457 /* special case since text nodes are actually saved as their parents */
1458 if ((item1->node == item2->node) && (item1->type != item2->type)) {
1459 if (item1->type == LYXP_NODE_ELEM) {
1460 assert(item2->type == LYXP_NODE_TEXT);
1461 return -1;
1462 } else {
1463 assert((item1->type == LYXP_NODE_TEXT) && (item2->type == LYXP_NODE_ELEM));
1464 return 1;
1465 }
1466 }
1467
Michal Vasko9f96a052020-03-10 09:41:45 +01001468 /* we need meta positions now */
1469 if (item1->type == LYXP_NODE_META) {
1470 meta_pos1 = get_meta_pos((struct lyd_meta *)item1->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001471 }
Michal Vasko9f96a052020-03-10 09:41:45 +01001472 if (item2->type == LYXP_NODE_META) {
1473 meta_pos2 = get_meta_pos((struct lyd_meta *)item2->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001474 }
1475
Michal Vasko9f96a052020-03-10 09:41:45 +01001476 /* 1st ROOT - 2nd ROOT, 1st ELEM - 2nd ELEM, 1st TEXT - 2nd TEXT, 1st META - =pos= - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001477 /* check for duplicates */
1478 if (item1->node == item2->node) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001479 assert((item1->type == item2->type) && ((item1->type != LYXP_NODE_META) || (meta_pos1 == meta_pos2)));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001480 return 0;
1481 }
1482
Michal Vasko9f96a052020-03-10 09:41:45 +01001483 /* 1st ELEM - 2nd TEXT, 1st ELEM - any pos - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001484 /* elem is always first, 2nd node is after it */
1485 if (item1->type == LYXP_NODE_ELEM) {
1486 assert(item2->type != LYXP_NODE_ELEM);
1487 return -1;
1488 }
1489
Michal Vasko9f96a052020-03-10 09:41:45 +01001490 /* 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 +02001491 /* 2nd is before 1st */
1492 if (((item1->type == LYXP_NODE_TEXT)
Michal Vasko9f96a052020-03-10 09:41:45 +01001493 && ((item2->type == LYXP_NODE_ELEM) || (item2->type == LYXP_NODE_META)))
1494 || ((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_ELEM))
1495 || (((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_META))
1496 && (meta_pos1 > meta_pos2))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001497 return 1;
1498 }
1499
Michal Vasko9f96a052020-03-10 09:41:45 +01001500 /* 1st META - any pos - 2nd TEXT, 1st META <pos< - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001501 /* 2nd is after 1st */
1502 return -1;
1503}
1504
1505/**
1506 * @brief Set cast for comparisons.
1507 *
1508 * @param[in] trg Target set to cast source into.
1509 * @param[in] src Source set.
1510 * @param[in] type Target set type.
1511 * @param[in] src_idx Source set node index.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001512 * @return LY_ERR
1513 */
1514static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001515set_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 +02001516{
1517 assert(src->type == LYXP_SET_NODE_SET);
1518
1519 set_init(trg, src);
1520
1521 /* insert node into target set */
1522 set_insert_node(trg, src->val.nodes[src_idx].node, src->val.nodes[src_idx].pos, src->val.nodes[src_idx].type, 0);
1523
1524 /* cast target set appropriately */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001525 return lyxp_set_cast(trg, type);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001526}
1527
1528#ifndef NDEBUG
1529
1530/**
1531 * @brief Bubble sort @p set into XPath document order.
1532 * Context position aware. Unused in the 'Release' build target.
1533 *
1534 * @param[in] set Set to sort.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001535 * @return How many times the whole set was traversed - 1 (if set was sorted, returns 0).
1536 */
1537static int
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001538set_sort(struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001539{
1540 uint32_t i, j;
1541 int ret = 0, cmp, inverted, change;
1542 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001543 struct lyxp_set_node item;
1544 struct lyxp_set_hash_node hnode;
1545 uint64_t hash;
1546
1547 if ((set->type != LYXP_SET_NODE_SET) || (set->used == 1)) {
1548 return 0;
1549 }
1550
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001551 /* find first top-level node to be used as anchor for positions */
1552 for (root = set->ctx_node; root->parent; root = (const struct lyd_node *)root->parent);
1553 for (; root->prev->next; root = root->prev);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001554
1555 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001556 if (set_assign_pos(set, root, set->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001557 return -1;
1558 }
1559
1560 LOGDBG(LY_LDGXPATH, "SORT BEGIN");
1561 print_set_debug(set);
1562
1563 for (i = 0; i < set->used; ++i) {
1564 inverted = 0;
1565 change = 0;
1566
1567 for (j = 1; j < set->used - i; ++j) {
1568 /* compare node positions */
1569 if (inverted) {
1570 cmp = set_sort_compare(&set->val.nodes[j], &set->val.nodes[j - 1]);
1571 } else {
1572 cmp = set_sort_compare(&set->val.nodes[j - 1], &set->val.nodes[j]);
1573 }
1574
1575 /* swap if needed */
1576 if ((inverted && (cmp < 0)) || (!inverted && (cmp > 0))) {
1577 change = 1;
1578
1579 item = set->val.nodes[j - 1];
1580 set->val.nodes[j - 1] = set->val.nodes[j];
1581 set->val.nodes[j] = item;
1582 } else {
1583 /* whether node_pos1 should be smaller than node_pos2 or the other way around */
1584 inverted = !inverted;
1585 }
1586 }
1587
1588 ++ret;
1589
1590 if (!change) {
1591 break;
1592 }
1593 }
1594
1595 LOGDBG(LY_LDGXPATH, "SORT END %d", ret);
1596 print_set_debug(set);
1597
1598 /* check node hashes */
1599 if (set->used >= LYD_HT_MIN_ITEMS) {
1600 assert(set->ht);
1601 for (i = 0; i < set->used; ++i) {
1602 hnode.node = set->val.nodes[i].node;
1603 hnode.type = set->val.nodes[i].type;
1604
1605 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
1606 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
1607 hash = dict_hash_multi(hash, NULL, 0);
1608
1609 assert(!lyht_find(set->ht, &hnode, hash, NULL));
1610 }
1611 }
1612
1613 return ret - 1;
1614}
1615
1616/**
1617 * @brief Remove duplicate entries in a sorted node set.
1618 *
1619 * @param[in] set Sorted set to check.
1620 * @return LY_ERR (LY_EEXIST if some duplicates are found)
1621 */
1622static LY_ERR
1623set_sorted_dup_node_clean(struct lyxp_set *set)
1624{
1625 uint32_t i = 0;
1626 LY_ERR ret = LY_SUCCESS;
1627
1628 if (set->used > 1) {
1629 while (i < set->used - 1) {
1630 if ((set->val.nodes[i].node == set->val.nodes[i + 1].node)
1631 && (set->val.nodes[i].type == set->val.nodes[i + 1].type)) {
Michal Vasko2caefc12019-11-14 16:07:56 +01001632 set_remove_node_none(set, i + 1);
Michal Vasko57eab132019-09-24 11:46:26 +02001633 ret = LY_EEXIST;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001634 }
Michal Vasko57eab132019-09-24 11:46:26 +02001635 ++i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001636 }
1637 }
1638
Michal Vasko2caefc12019-11-14 16:07:56 +01001639 set_remove_nodes_none(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001640 return ret;
1641}
1642
1643#endif
1644
1645/**
1646 * @brief Merge 2 sorted sets into one.
1647 *
1648 * @param[in,out] trg Set to merge into. Duplicates are removed.
1649 * @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 +02001650 * @return LY_ERR
1651 */
1652static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001653set_sorted_merge(struct lyxp_set *trg, struct lyxp_set *src)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001654{
1655 uint32_t i, j, k, count, dup_count;
1656 int cmp;
1657 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001658
Michal Vaskod3678892020-05-21 10:06:58 +02001659 if ((trg->type != LYXP_SET_NODE_SET) || (src->type != LYXP_SET_NODE_SET)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001660 return LY_EINVAL;
1661 }
1662
Michal Vaskod3678892020-05-21 10:06:58 +02001663 if (!src->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001664 return LY_SUCCESS;
Michal Vaskod3678892020-05-21 10:06:58 +02001665 } else if (!trg->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001666 set_fill_set(trg, src);
Michal Vaskod3678892020-05-21 10:06:58 +02001667 lyxp_set_free_content(src);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001668 return LY_SUCCESS;
1669 }
1670
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001671 /* find first top-level node to be used as anchor for positions */
1672 for (root = trg->ctx_node; root->parent; root = (const struct lyd_node *)root->parent);
1673 for (; root->prev->next; root = root->prev);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001674
1675 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001676 if (set_assign_pos(trg, root, trg->root_type) || set_assign_pos(src, root, src->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001677 return LY_EINT;
1678 }
1679
1680#ifndef NDEBUG
1681 LOGDBG(LY_LDGXPATH, "MERGE target");
1682 print_set_debug(trg);
1683 LOGDBG(LY_LDGXPATH, "MERGE source");
1684 print_set_debug(src);
1685#endif
1686
1687 /* make memory for the merge (duplicates are not detected yet, so space
1688 * will likely be wasted on them, too bad) */
1689 if (trg->size - trg->used < src->used) {
1690 trg->size = trg->used + src->used;
1691
1692 trg->val.nodes = ly_realloc(trg->val.nodes, trg->size * sizeof *trg->val.nodes);
1693 LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx), LY_EMEM);
1694 }
1695
1696 i = 0;
1697 j = 0;
1698 count = 0;
1699 dup_count = 0;
1700 do {
1701 cmp = set_sort_compare(&src->val.nodes[i], &trg->val.nodes[j]);
1702 if (!cmp) {
1703 if (!count) {
1704 /* duplicate, just skip it */
1705 ++i;
1706 ++j;
1707 } else {
1708 /* we are copying something already, so let's copy the duplicate too,
1709 * we are hoping that afterwards there are some more nodes to
1710 * copy and this way we can copy them all together */
1711 ++count;
1712 ++dup_count;
1713 ++i;
1714 ++j;
1715 }
1716 } else if (cmp < 0) {
1717 /* inserting src node into trg, just remember it for now */
1718 ++count;
1719 ++i;
1720
1721 /* insert the hash now */
1722 set_insert_node_hash(trg, src->val.nodes[i - 1].node, src->val.nodes[i - 1].type);
1723 } else if (count) {
1724copy_nodes:
1725 /* time to actually copy the nodes, we have found the largest block of nodes */
1726 memmove(&trg->val.nodes[j + (count - dup_count)],
1727 &trg->val.nodes[j],
1728 (trg->used - j) * sizeof *trg->val.nodes);
1729 memcpy(&trg->val.nodes[j - dup_count], &src->val.nodes[i - count], count * sizeof *src->val.nodes);
1730
1731 trg->used += count - dup_count;
1732 /* do not change i, except the copying above, we are basically doing exactly what is in the else branch below */
1733 j += count - dup_count;
1734
1735 count = 0;
1736 dup_count = 0;
1737 } else {
1738 ++j;
1739 }
1740 } while ((i < src->used) && (j < trg->used));
1741
1742 if ((i < src->used) || count) {
1743 /* insert all the hashes first */
1744 for (k = i; k < src->used; ++k) {
1745 set_insert_node_hash(trg, src->val.nodes[k].node, src->val.nodes[k].type);
1746 }
1747
1748 /* loop ended, but we need to copy something at trg end */
1749 count += src->used - i;
1750 i = src->used;
1751 goto copy_nodes;
1752 }
1753
1754 /* we are inserting hashes before the actual node insert, which causes
1755 * situations when there were initially not enough items for a hash table,
1756 * but even after some were inserted, hash table was not created (during
1757 * insertion the number of items is not updated yet) */
1758 if (!trg->ht && (trg->used >= LYD_HT_MIN_ITEMS)) {
1759 set_insert_node_hash(trg, NULL, 0);
1760 }
1761
1762#ifndef NDEBUG
1763 LOGDBG(LY_LDGXPATH, "MERGE result");
1764 print_set_debug(trg);
1765#endif
1766
Michal Vaskod3678892020-05-21 10:06:58 +02001767 lyxp_set_free_content(src);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001768 return LY_SUCCESS;
1769}
1770
1771/*
1772 * (re)parse functions
1773 *
1774 * Parse functions parse the expression into
1775 * tokens (syntactic analysis).
1776 *
1777 * Reparse functions perform semantic analysis
1778 * (do not save the result, just a check) of
1779 * the expression and fill repeat indices.
1780 */
1781
1782/**
1783 * @brief Look at the next token and check its kind.
1784 *
1785 * @param[in] ctx Context for logging.
1786 * @param[in] exp Expression to use.
1787 * @param[in] exp_idx Position in the expression \p exp.
1788 * @param[in] want_tok Expected token.
1789 * @param[in] strict Whether the token is strictly required (print error if
1790 * not the next one) or we simply want to check whether it is the next or not.
1791 * @return LY_ERR
1792 */
1793static LY_ERR
1794exp_check_token(struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t exp_idx, enum lyxp_token want_tok, int strict)
1795{
1796 if (exp->used == exp_idx) {
1797 if (strict) {
1798 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOF);
1799 }
1800 return LY_EINVAL;
1801 }
1802
1803 if (want_tok && (exp->tokens[exp_idx] != want_tok)) {
1804 if (strict) {
1805 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK,
1806 print_token(exp->tokens[exp_idx]), &exp->expr[exp->tok_pos[exp_idx]]);
1807 }
1808 return LY_EINVAL;
1809 }
1810
1811 return LY_SUCCESS;
1812}
1813
1814/**
1815 * @brief Stack operation push on the repeat array.
1816 *
1817 * @param[in] exp Expression to use.
1818 * @param[in] exp_idx Position in the expresion \p exp.
1819 * @param[in] repeat_op_idx Index from \p exp of the operator token. This value is pushed.
1820 */
1821static void
1822exp_repeat_push(struct lyxp_expr *exp, uint16_t exp_idx, uint16_t repeat_op_idx)
1823{
1824 uint16_t i;
1825
1826 if (exp->repeat[exp_idx]) {
1827 for (i = 0; exp->repeat[exp_idx][i]; ++i);
1828 exp->repeat[exp_idx] = realloc(exp->repeat[exp_idx], (i + 2) * sizeof *exp->repeat[exp_idx]);
1829 LY_CHECK_ERR_RET(!exp->repeat[exp_idx], LOGMEM(NULL), );
1830 exp->repeat[exp_idx][i] = repeat_op_idx;
1831 exp->repeat[exp_idx][i + 1] = 0;
1832 } else {
1833 exp->repeat[exp_idx] = calloc(2, sizeof *exp->repeat[exp_idx]);
1834 LY_CHECK_ERR_RET(!exp->repeat[exp_idx], LOGMEM(NULL), );
1835 exp->repeat[exp_idx][0] = repeat_op_idx;
1836 }
1837}
1838
1839/**
1840 * @brief Reparse Predicate. Logs directly on error.
1841 *
1842 * [7] Predicate ::= '[' Expr ']'
1843 *
1844 * @param[in] ctx Context for logging.
1845 * @param[in] exp Parsed XPath expression.
1846 * @param[in] exp_idx Position in the expression @p exp.
1847 * @return LY_ERR
1848 */
1849static LY_ERR
1850reparse_predicate(struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *exp_idx)
1851{
1852 LY_ERR rc;
1853
1854 rc = exp_check_token(ctx, exp, *exp_idx, LYXP_TOKEN_BRACK1, 1);
1855 LY_CHECK_RET(rc);
1856 ++(*exp_idx);
1857
1858 rc = reparse_or_expr(ctx, exp, exp_idx);
1859 LY_CHECK_RET(rc);
1860
1861 rc = exp_check_token(ctx, exp, *exp_idx, LYXP_TOKEN_BRACK2, 1);
1862 LY_CHECK_RET(rc);
1863 ++(*exp_idx);
1864
1865 return LY_SUCCESS;
1866}
1867
1868/**
1869 * @brief Reparse RelativeLocationPath. Logs directly on error.
1870 *
1871 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
1872 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
1873 * [6] NodeTest ::= NameTest | NodeType '(' ')'
1874 *
1875 * @param[in] ctx Context for logging.
1876 * @param[in] exp Parsed XPath expression.
1877 * @param[in] exp_idx Position in the expression \p exp.
1878 * @return LY_ERR (LY_EINCOMPLETE on forward reference)
1879 */
1880static LY_ERR
1881reparse_relative_location_path(struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *exp_idx)
1882{
1883 LY_ERR rc;
1884
1885 rc = exp_check_token(ctx, exp, *exp_idx, LYXP_TOKEN_NONE, 1);
1886 LY_CHECK_RET(rc);
1887
1888 goto step;
1889 do {
1890 /* '/' or '//' */
1891 ++(*exp_idx);
1892
1893 rc = exp_check_token(ctx, exp, *exp_idx, LYXP_TOKEN_NONE, 1);
1894 LY_CHECK_RET(rc);
1895step:
1896 /* Step */
1897 switch (exp->tokens[*exp_idx]) {
1898 case LYXP_TOKEN_DOT:
1899 ++(*exp_idx);
1900 break;
1901
1902 case LYXP_TOKEN_DDOT:
1903 ++(*exp_idx);
1904 break;
1905
1906 case LYXP_TOKEN_AT:
1907 ++(*exp_idx);
1908
1909 rc = exp_check_token(ctx, exp, *exp_idx, LYXP_TOKEN_NONE, 1);
1910 LY_CHECK_RET(rc);
1911 if ((exp->tokens[*exp_idx] != LYXP_TOKEN_NAMETEST) && (exp->tokens[*exp_idx] != LYXP_TOKEN_NODETYPE)) {
1912 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK,
1913 print_token(exp->tokens[*exp_idx]), &exp->expr[exp->tok_pos[*exp_idx]]);
1914 return LY_EVALID;
1915 }
1916 /* fall through */
1917 case LYXP_TOKEN_NAMETEST:
1918 ++(*exp_idx);
1919 goto reparse_predicate;
1920 break;
1921
1922 case LYXP_TOKEN_NODETYPE:
1923 ++(*exp_idx);
1924
1925 /* '(' */
1926 rc = exp_check_token(ctx, exp, *exp_idx, LYXP_TOKEN_PAR1, 1);
1927 LY_CHECK_RET(rc);
1928 ++(*exp_idx);
1929
1930 /* ')' */
1931 rc = exp_check_token(ctx, exp, *exp_idx, LYXP_TOKEN_PAR2, 1);
1932 LY_CHECK_RET(rc);
1933 ++(*exp_idx);
1934
1935reparse_predicate:
1936 /* Predicate* */
1937 while ((exp->used > *exp_idx) && (exp->tokens[*exp_idx] == LYXP_TOKEN_BRACK1)) {
1938 rc = reparse_predicate(ctx, exp, exp_idx);
1939 LY_CHECK_RET(rc);
1940 }
1941 break;
1942 default:
1943 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK,
1944 print_token(exp->tokens[*exp_idx]), &exp->expr[exp->tok_pos[*exp_idx]]);
1945 return LY_EVALID;
1946 }
1947 } while ((exp->used > *exp_idx) && (exp->tokens[*exp_idx] == LYXP_TOKEN_OPERATOR_PATH));
1948
1949 return LY_SUCCESS;
1950}
1951
1952/**
1953 * @brief Reparse AbsoluteLocationPath. Logs directly on error.
1954 *
1955 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
1956 *
1957 * @param[in] ctx Context for logging.
1958 * @param[in] exp Parsed XPath expression.
1959 * @param[in] exp_idx Position in the expression \p exp.
1960 * @return LY_ERR
1961 */
1962static LY_ERR
1963reparse_absolute_location_path(struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *exp_idx)
1964{
1965 LY_ERR rc;
1966
1967 rc = exp_check_token(ctx, exp, *exp_idx, LYXP_TOKEN_OPERATOR_PATH, 1);
1968 LY_CHECK_RET(rc);
1969
1970 /* '/' RelativeLocationPath? */
1971 if (exp->tok_len[*exp_idx] == 1) {
1972 /* '/' */
1973 ++(*exp_idx);
1974
1975 if (exp_check_token(ctx, exp, *exp_idx, LYXP_TOKEN_NONE, 0)) {
1976 return LY_SUCCESS;
1977 }
1978 switch (exp->tokens[*exp_idx]) {
1979 case LYXP_TOKEN_DOT:
1980 case LYXP_TOKEN_DDOT:
1981 case LYXP_TOKEN_AT:
1982 case LYXP_TOKEN_NAMETEST:
1983 case LYXP_TOKEN_NODETYPE:
1984 rc = reparse_relative_location_path(ctx, exp, exp_idx);
1985 LY_CHECK_RET(rc);
1986 /* fall through */
1987 default:
1988 break;
1989 }
1990
1991 /* '//' RelativeLocationPath */
1992 } else {
1993 /* '//' */
1994 ++(*exp_idx);
1995
1996 rc = reparse_relative_location_path(ctx, exp, exp_idx);
1997 LY_CHECK_RET(rc);
1998 }
1999
2000 return LY_SUCCESS;
2001}
2002
2003/**
2004 * @brief Reparse FunctionCall. Logs directly on error.
2005 *
2006 * [9] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
2007 *
2008 * @param[in] ctx Context for logging.
2009 * @param[in] exp Parsed XPath expression.
2010 * @param[in] exp_idx Position in the expression @p exp.
2011 * @return LY_ERR
2012 */
2013static LY_ERR
2014reparse_function_call(struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *exp_idx)
2015{
2016 int min_arg_count = -1, max_arg_count, arg_count;
2017 uint16_t func_exp_idx;
2018 LY_ERR rc;
2019
2020 rc = exp_check_token(ctx, exp, *exp_idx, LYXP_TOKEN_FUNCNAME, 1);
2021 LY_CHECK_RET(rc);
2022 func_exp_idx = *exp_idx;
2023 switch (exp->tok_len[*exp_idx]) {
2024 case 3:
2025 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "not", 3)) {
2026 min_arg_count = 1;
2027 max_arg_count = 1;
2028 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "sum", 3)) {
2029 min_arg_count = 1;
2030 max_arg_count = 1;
2031 }
2032 break;
2033 case 4:
2034 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "lang", 4)) {
2035 min_arg_count = 1;
2036 max_arg_count = 1;
2037 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "last", 4)) {
2038 min_arg_count = 0;
2039 max_arg_count = 0;
2040 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "name", 4)) {
2041 min_arg_count = 0;
2042 max_arg_count = 1;
2043 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "true", 4)) {
2044 min_arg_count = 0;
2045 max_arg_count = 0;
2046 }
2047 break;
2048 case 5:
2049 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "count", 5)) {
2050 min_arg_count = 1;
2051 max_arg_count = 1;
2052 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "false", 5)) {
2053 min_arg_count = 0;
2054 max_arg_count = 0;
2055 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "floor", 5)) {
2056 min_arg_count = 1;
2057 max_arg_count = 1;
2058 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "round", 5)) {
2059 min_arg_count = 1;
2060 max_arg_count = 1;
2061 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "deref", 5)) {
2062 min_arg_count = 1;
2063 max_arg_count = 1;
2064 }
2065 break;
2066 case 6:
2067 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "concat", 6)) {
2068 min_arg_count = 2;
Michal Vaskobe2e3562019-10-15 15:35:35 +02002069 max_arg_count = INT_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002070 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "number", 6)) {
2071 min_arg_count = 0;
2072 max_arg_count = 1;
2073 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "string", 6)) {
2074 min_arg_count = 0;
2075 max_arg_count = 1;
2076 }
2077 break;
2078 case 7:
2079 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "boolean", 7)) {
2080 min_arg_count = 1;
2081 max_arg_count = 1;
2082 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "ceiling", 7)) {
2083 min_arg_count = 1;
2084 max_arg_count = 1;
2085 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "current", 7)) {
2086 min_arg_count = 0;
2087 max_arg_count = 0;
2088 }
2089 break;
2090 case 8:
2091 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "contains", 8)) {
2092 min_arg_count = 2;
2093 max_arg_count = 2;
2094 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "position", 8)) {
2095 min_arg_count = 0;
2096 max_arg_count = 0;
2097 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "re-match", 8)) {
2098 min_arg_count = 2;
2099 max_arg_count = 2;
2100 }
2101 break;
2102 case 9:
2103 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "substring", 9)) {
2104 min_arg_count = 2;
2105 max_arg_count = 3;
2106 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "translate", 9)) {
2107 min_arg_count = 3;
2108 max_arg_count = 3;
2109 }
2110 break;
2111 case 10:
2112 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "local-name", 10)) {
2113 min_arg_count = 0;
2114 max_arg_count = 1;
2115 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "enum-value", 10)) {
2116 min_arg_count = 1;
2117 max_arg_count = 1;
2118 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "bit-is-set", 10)) {
2119 min_arg_count = 2;
2120 max_arg_count = 2;
2121 }
2122 break;
2123 case 11:
2124 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "starts-with", 11)) {
2125 min_arg_count = 2;
2126 max_arg_count = 2;
2127 }
2128 break;
2129 case 12:
2130 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "derived-from", 12)) {
2131 min_arg_count = 2;
2132 max_arg_count = 2;
2133 }
2134 break;
2135 case 13:
2136 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "namespace-uri", 13)) {
2137 min_arg_count = 0;
2138 max_arg_count = 1;
2139 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "string-length", 13)) {
2140 min_arg_count = 0;
2141 max_arg_count = 1;
2142 }
2143 break;
2144 case 15:
2145 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "normalize-space", 15)) {
2146 min_arg_count = 0;
2147 max_arg_count = 1;
2148 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "substring-after", 15)) {
2149 min_arg_count = 2;
2150 max_arg_count = 2;
2151 }
2152 break;
2153 case 16:
2154 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "substring-before", 16)) {
2155 min_arg_count = 2;
2156 max_arg_count = 2;
2157 }
2158 break;
2159 case 20:
2160 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "derived-from-or-self", 20)) {
2161 min_arg_count = 2;
2162 max_arg_count = 2;
2163 }
2164 break;
2165 }
2166 if (min_arg_count == -1) {
2167 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INFUNC, exp->tok_len[*exp_idx], &exp->expr[exp->tok_pos[*exp_idx]]);
2168 return LY_EINVAL;
2169 }
2170 ++(*exp_idx);
2171
2172 /* '(' */
2173 rc = exp_check_token(ctx, exp, *exp_idx, LYXP_TOKEN_PAR1, 1);
2174 LY_CHECK_RET(rc);
2175 ++(*exp_idx);
2176
2177 /* ( Expr ( ',' Expr )* )? */
2178 arg_count = 0;
2179 rc = exp_check_token(ctx, exp, *exp_idx, LYXP_TOKEN_NONE, 1);
2180 LY_CHECK_RET(rc);
2181 if (exp->tokens[*exp_idx] != LYXP_TOKEN_PAR2) {
2182 ++arg_count;
2183 rc = reparse_or_expr(ctx, exp, exp_idx);
2184 LY_CHECK_RET(rc);
2185 }
2186 while ((exp->used > *exp_idx) && (exp->tokens[*exp_idx] == LYXP_TOKEN_COMMA)) {
2187 ++(*exp_idx);
2188
2189 ++arg_count;
2190 rc = reparse_or_expr(ctx, exp, exp_idx);
2191 LY_CHECK_RET(rc);
2192 }
2193
2194 /* ')' */
2195 rc = exp_check_token(ctx, exp, *exp_idx, LYXP_TOKEN_PAR2, 1);
2196 LY_CHECK_RET(rc);
2197 ++(*exp_idx);
2198
2199 if ((arg_count < min_arg_count) || (arg_count > max_arg_count)) {
2200 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INARGCOUNT, arg_count, exp->tok_len[func_exp_idx],
2201 &exp->expr[exp->tok_pos[func_exp_idx]]);
2202 return LY_EVALID;
2203 }
2204
2205 return LY_SUCCESS;
2206}
2207
2208/**
2209 * @brief Reparse PathExpr. Logs directly on error.
2210 *
2211 * [10] PathExpr ::= LocationPath | PrimaryExpr Predicate*
2212 * | PrimaryExpr Predicate* '/' RelativeLocationPath
2213 * | PrimaryExpr Predicate* '//' RelativeLocationPath
2214 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
2215 * [8] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall
2216 *
2217 * @param[in] ctx Context for logging.
2218 * @param[in] exp Parsed XPath expression.
2219 * @param[in] exp_idx Position in the expression @p exp.
2220 * @return LY_ERR
2221 */
2222static LY_ERR
2223reparse_path_expr(struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *exp_idx)
2224{
2225 LY_ERR rc;
2226
2227 if (exp_check_token(ctx, exp, *exp_idx, LYXP_TOKEN_NONE, 1)) {
2228 return -1;
2229 }
2230
2231 switch (exp->tokens[*exp_idx]) {
2232 case LYXP_TOKEN_PAR1:
2233 /* '(' Expr ')' Predicate* */
2234 ++(*exp_idx);
2235
2236 rc = reparse_or_expr(ctx, exp, exp_idx);
2237 LY_CHECK_RET(rc);
2238
2239 rc = exp_check_token(ctx, exp, *exp_idx, LYXP_TOKEN_PAR2, 1);
2240 LY_CHECK_RET(rc);
2241 ++(*exp_idx);
2242 goto predicate;
2243 break;
2244 case LYXP_TOKEN_DOT:
2245 case LYXP_TOKEN_DDOT:
2246 case LYXP_TOKEN_AT:
2247 case LYXP_TOKEN_NAMETEST:
2248 case LYXP_TOKEN_NODETYPE:
2249 /* RelativeLocationPath */
2250 rc = reparse_relative_location_path(ctx, exp, exp_idx);
2251 LY_CHECK_RET(rc);
2252 break;
2253 case LYXP_TOKEN_FUNCNAME:
2254 /* FunctionCall */
2255 rc = reparse_function_call(ctx, exp, exp_idx);
2256 LY_CHECK_RET(rc);
2257 goto predicate;
2258 break;
2259 case LYXP_TOKEN_OPERATOR_PATH:
2260 /* AbsoluteLocationPath */
2261 rc = reparse_absolute_location_path(ctx, exp, exp_idx);
2262 LY_CHECK_RET(rc);
2263 break;
2264 case LYXP_TOKEN_LITERAL:
2265 /* Literal */
2266 ++(*exp_idx);
2267 goto predicate;
2268 break;
2269 case LYXP_TOKEN_NUMBER:
2270 /* Number */
2271 ++(*exp_idx);
2272 goto predicate;
2273 break;
2274 default:
2275 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK,
2276 print_token(exp->tokens[*exp_idx]), &exp->expr[exp->tok_pos[*exp_idx]]);
2277 return LY_EVALID;
2278 }
2279
2280 return LY_SUCCESS;
2281
2282predicate:
2283 /* Predicate* */
2284 while ((exp->used > *exp_idx) && (exp->tokens[*exp_idx] == LYXP_TOKEN_BRACK1)) {
2285 rc = reparse_predicate(ctx, exp, exp_idx);
2286 LY_CHECK_RET(rc);
2287 }
2288
2289 /* ('/' or '//') RelativeLocationPath */
2290 if ((exp->used > *exp_idx) && (exp->tokens[*exp_idx] == LYXP_TOKEN_OPERATOR_PATH)) {
2291
2292 /* '/' or '//' */
2293 ++(*exp_idx);
2294
2295 rc = reparse_relative_location_path(ctx, exp, exp_idx);
2296 LY_CHECK_RET(rc);
2297 }
2298
2299 return LY_SUCCESS;
2300}
2301
2302/**
2303 * @brief Reparse UnaryExpr. Logs directly on error.
2304 *
2305 * [17] UnaryExpr ::= UnionExpr | '-' UnaryExpr
2306 * [18] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
2307 *
2308 * @param[in] ctx Context for logging.
2309 * @param[in] exp Parsed XPath expression.
2310 * @param[in] exp_idx Position in the expression @p exp.
2311 * @return LY_ERR
2312 */
2313static LY_ERR
2314reparse_unary_expr(struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *exp_idx)
2315{
2316 uint16_t prev_exp;
2317 LY_ERR rc;
2318
2319 /* ('-')* */
2320 prev_exp = *exp_idx;
2321 while (!exp_check_token(ctx, exp, *exp_idx, LYXP_TOKEN_OPERATOR_MATH, 0)
2322 && (exp->expr[exp->tok_pos[*exp_idx]] == '-')) {
2323 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNARY);
2324 ++(*exp_idx);
2325 }
2326
2327 /* PathExpr */
2328 prev_exp = *exp_idx;
2329 rc = reparse_path_expr(ctx, exp, exp_idx);
2330 LY_CHECK_RET(rc);
2331
2332 /* ('|' PathExpr)* */
2333 while (!exp_check_token(ctx, exp, *exp_idx, LYXP_TOKEN_OPERATOR_UNI, 0)) {
2334 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNION);
2335 ++(*exp_idx);
2336
2337 rc = reparse_path_expr(ctx, exp, exp_idx);
2338 LY_CHECK_RET(rc);
2339 }
2340
2341 return LY_SUCCESS;
2342}
2343
2344/**
2345 * @brief Reparse AdditiveExpr. Logs directly on error.
2346 *
2347 * [15] AdditiveExpr ::= MultiplicativeExpr
2348 * | AdditiveExpr '+' MultiplicativeExpr
2349 * | AdditiveExpr '-' MultiplicativeExpr
2350 * [16] MultiplicativeExpr ::= UnaryExpr
2351 * | MultiplicativeExpr '*' UnaryExpr
2352 * | MultiplicativeExpr 'div' UnaryExpr
2353 * | MultiplicativeExpr 'mod' UnaryExpr
2354 *
2355 * @param[in] ctx Context for logging.
2356 * @param[in] exp Parsed XPath expression.
2357 * @param[in] exp_idx Position in the expression @p exp.
2358 * @return LY_ERR
2359 */
2360static LY_ERR
2361reparse_additive_expr(struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *exp_idx)
2362{
2363 uint16_t prev_add_exp, prev_mul_exp;
2364 LY_ERR rc;
2365
2366 prev_add_exp = *exp_idx;
2367 goto reparse_multiplicative_expr;
2368
2369 /* ('+' / '-' MultiplicativeExpr)* */
2370 while (!exp_check_token(ctx, exp, *exp_idx, LYXP_TOKEN_OPERATOR_MATH, 0)
2371 && ((exp->expr[exp->tok_pos[*exp_idx]] == '+') || (exp->expr[exp->tok_pos[*exp_idx]] == '-'))) {
2372 exp_repeat_push(exp, prev_add_exp, LYXP_EXPR_ADDITIVE);
2373 ++(*exp_idx);
2374
2375reparse_multiplicative_expr:
2376 /* UnaryExpr */
2377 prev_mul_exp = *exp_idx;
2378 rc = reparse_unary_expr(ctx, exp, exp_idx);
2379 LY_CHECK_RET(rc);
2380
2381 /* ('*' / 'div' / 'mod' UnaryExpr)* */
2382 while (!exp_check_token(ctx, exp, *exp_idx, LYXP_TOKEN_OPERATOR_MATH, 0)
2383 && ((exp->expr[exp->tok_pos[*exp_idx]] == '*') || (exp->tok_len[*exp_idx] == 3))) {
2384 exp_repeat_push(exp, prev_mul_exp, LYXP_EXPR_MULTIPLICATIVE);
2385 ++(*exp_idx);
2386
2387 rc = reparse_unary_expr(ctx, exp, exp_idx);
2388 LY_CHECK_RET(rc);
2389 }
2390 }
2391
2392 return LY_SUCCESS;
2393}
2394
2395/**
2396 * @brief Reparse EqualityExpr. Logs directly on error.
2397 *
2398 * [13] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
2399 * | EqualityExpr '!=' RelationalExpr
2400 * [14] RelationalExpr ::= AdditiveExpr
2401 * | RelationalExpr '<' AdditiveExpr
2402 * | RelationalExpr '>' AdditiveExpr
2403 * | RelationalExpr '<=' AdditiveExpr
2404 * | RelationalExpr '>=' AdditiveExpr
2405 *
2406 * @param[in] ctx Context for logging.
2407 * @param[in] exp Parsed XPath expression.
2408 * @param[in] exp_idx Position in the expression @p exp.
2409 * @return LY_ERR
2410 */
2411static LY_ERR
2412reparse_equality_expr(struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *exp_idx)
2413{
2414 uint16_t prev_eq_exp, prev_rel_exp;
2415 LY_ERR rc;
2416
2417 prev_eq_exp = *exp_idx;
2418 goto reparse_additive_expr;
2419
2420 /* ('=' / '!=' RelationalExpr)* */
2421 while (!exp_check_token(ctx, exp, *exp_idx, LYXP_TOKEN_OPERATOR_COMP, 0)
2422 && ((exp->expr[exp->tok_pos[*exp_idx]] == '=') || (exp->expr[exp->tok_pos[*exp_idx]] == '!'))) {
2423 exp_repeat_push(exp, prev_eq_exp, LYXP_EXPR_EQUALITY);
2424 ++(*exp_idx);
2425
2426reparse_additive_expr:
2427 /* AdditiveExpr */
2428 prev_rel_exp = *exp_idx;
2429 rc = reparse_additive_expr(ctx, exp, exp_idx);
2430 LY_CHECK_RET(rc);
2431
2432 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
2433 while (!exp_check_token(ctx, exp, *exp_idx, LYXP_TOKEN_OPERATOR_COMP, 0)
2434 && ((exp->expr[exp->tok_pos[*exp_idx]] == '<') || (exp->expr[exp->tok_pos[*exp_idx]] == '>'))) {
2435 exp_repeat_push(exp, prev_rel_exp, LYXP_EXPR_RELATIONAL);
2436 ++(*exp_idx);
2437
2438 rc = reparse_additive_expr(ctx, exp, exp_idx);
2439 LY_CHECK_RET(rc);
2440 }
2441 }
2442
2443 return LY_SUCCESS;
2444}
2445
2446/**
2447 * @brief Reparse OrExpr. Logs directly on error.
2448 *
2449 * [11] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
2450 * [12] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
2451 *
2452 * @param[in] ctx Context for logging.
2453 * @param[in] exp Parsed XPath expression.
2454 * @param[in] exp_idx Position in the expression @p exp.
2455 * @return LY_ERR
2456 */
2457static LY_ERR
2458reparse_or_expr(struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *exp_idx)
2459{
2460 uint16_t prev_or_exp, prev_and_exp;
2461 LY_ERR rc;
2462
2463 prev_or_exp = *exp_idx;
2464 goto reparse_equality_expr;
2465
2466 /* ('or' AndExpr)* */
2467 while (!exp_check_token(ctx, exp, *exp_idx, LYXP_TOKEN_OPERATOR_LOG, 0) && (exp->tok_len[*exp_idx] == 2)) {
2468 exp_repeat_push(exp, prev_or_exp, LYXP_EXPR_OR);
2469 ++(*exp_idx);
2470
2471reparse_equality_expr:
2472 /* EqualityExpr */
2473 prev_and_exp = *exp_idx;
2474 rc = reparse_equality_expr(ctx, exp, exp_idx);
2475 LY_CHECK_RET(rc);
2476
2477 /* ('and' EqualityExpr)* */
2478 while (!exp_check_token(ctx, exp, *exp_idx, LYXP_TOKEN_OPERATOR_LOG, 0) && (exp->tok_len[*exp_idx] == 3)) {
2479 exp_repeat_push(exp, prev_and_exp, LYXP_EXPR_AND);
2480 ++(*exp_idx);
2481
2482 rc = reparse_equality_expr(ctx, exp, exp_idx);
2483 LY_CHECK_RET(rc);
2484 }
2485 }
2486
2487 return LY_SUCCESS;
2488}
Radek Krejcib1646a92018-11-02 16:08:26 +01002489
2490/**
2491 * @brief Parse NCName.
2492 *
2493 * @param[in] ncname Name to parse.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002494 * @return Length of @p ncname valid bytes.
Radek Krejcib1646a92018-11-02 16:08:26 +01002495 */
Radek Krejcid4270262019-01-07 15:07:25 +01002496static long int
Radek Krejcib1646a92018-11-02 16:08:26 +01002497parse_ncname(const char *ncname)
2498{
2499 unsigned int uc;
Radek Krejcid4270262019-01-07 15:07:25 +01002500 size_t size;
2501 long int len = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002502
2503 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), 0);
2504 if (!is_xmlqnamestartchar(uc) || (uc == ':')) {
2505 return len;
2506 }
2507
2508 do {
2509 len += size;
Radek Krejci9a564c92019-01-07 14:53:57 +01002510 if (!*ncname) {
2511 break;
2512 }
Radek Krejcid4270262019-01-07 15:07:25 +01002513 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), -len);
Radek Krejcib1646a92018-11-02 16:08:26 +01002514 } while (is_xmlqnamechar(uc) && (uc != ':'));
2515
2516 return len;
2517}
2518
2519/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02002520 * @brief Add @p token into the expression @p exp.
Radek Krejcib1646a92018-11-02 16:08:26 +01002521 *
Michal Vasko03ff5a72019-09-11 13:49:33 +02002522 * @param[in] ctx Context for logging.
Radek Krejcib1646a92018-11-02 16:08:26 +01002523 * @param[in] exp Expression to use.
2524 * @param[in] token Token to add.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002525 * @param[in] tok_pos Token position in the XPath expression.
Radek Krejcib1646a92018-11-02 16:08:26 +01002526 * @param[in] tok_len Token length in the XPath expression.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002527 * @return LY_ERR
Radek Krejcib1646a92018-11-02 16:08:26 +01002528 */
2529static LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02002530exp_add_token(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 +01002531{
2532 uint32_t prev;
2533
2534 if (exp->used == exp->size) {
2535 prev = exp->size;
2536 exp->size += LYXP_EXPR_SIZE_STEP;
2537 if (prev > exp->size) {
2538 LOGINT(ctx);
2539 return LY_EINT;
2540 }
2541
2542 exp->tokens = ly_realloc(exp->tokens, exp->size * sizeof *exp->tokens);
2543 LY_CHECK_ERR_RET(!exp->tokens, LOGMEM(ctx), LY_EMEM);
2544 exp->tok_pos = ly_realloc(exp->tok_pos, exp->size * sizeof *exp->tok_pos);
2545 LY_CHECK_ERR_RET(!exp->tok_pos, LOGMEM(ctx), LY_EMEM);
2546 exp->tok_len = ly_realloc(exp->tok_len, exp->size * sizeof *exp->tok_len);
2547 LY_CHECK_ERR_RET(!exp->tok_len, LOGMEM(ctx), LY_EMEM);
2548 }
2549
2550 exp->tokens[exp->used] = token;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002551 exp->tok_pos[exp->used] = tok_pos;
Radek Krejcib1646a92018-11-02 16:08:26 +01002552 exp->tok_len[exp->used] = tok_len;
2553 ++exp->used;
2554 return LY_SUCCESS;
2555}
2556
2557void
2558lyxp_expr_free(struct ly_ctx *ctx, struct lyxp_expr *expr)
2559{
2560 uint16_t i;
2561
2562 if (!expr) {
2563 return;
2564 }
2565
2566 lydict_remove(ctx, expr->expr);
2567 free(expr->tokens);
2568 free(expr->tok_pos);
2569 free(expr->tok_len);
2570 if (expr->repeat) {
2571 for (i = 0; i < expr->used; ++i) {
2572 free(expr->repeat[i]);
2573 }
2574 }
2575 free(expr->repeat);
2576 free(expr);
2577}
2578
2579struct lyxp_expr *
2580lyxp_expr_parse(struct ly_ctx *ctx, const char *expr)
2581{
2582 struct lyxp_expr *ret;
Radek Krejcid4270262019-01-07 15:07:25 +01002583 size_t parsed = 0, tok_len;
2584 long int ncname_len;
Radek Krejcib1646a92018-11-02 16:08:26 +01002585 enum lyxp_token tok_type;
2586 int prev_function_check = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002587 uint16_t exp_idx = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002588
2589 if (strlen(expr) > UINT16_MAX) {
2590 LOGERR(ctx, LY_EINVAL, "XPath expression cannot be longer than %ud characters.", UINT16_MAX);
2591 return NULL;
2592 }
2593
2594 /* init lyxp_expr structure */
2595 ret = calloc(1, sizeof *ret);
2596 LY_CHECK_ERR_GOTO(!ret, LOGMEM(ctx), error);
2597 ret->expr = lydict_insert(ctx, expr, strlen(expr));
2598 LY_CHECK_ERR_GOTO(!ret->expr, LOGMEM(ctx), error);
2599 ret->used = 0;
2600 ret->size = LYXP_EXPR_SIZE_START;
2601 ret->tokens = malloc(ret->size * sizeof *ret->tokens);
2602 LY_CHECK_ERR_GOTO(!ret->tokens, LOGMEM(ctx), error);
2603
2604 ret->tok_pos = malloc(ret->size * sizeof *ret->tok_pos);
2605 LY_CHECK_ERR_GOTO(!ret->tok_pos, LOGMEM(ctx), error);
2606
2607 ret->tok_len = malloc(ret->size * sizeof *ret->tok_len);
2608 LY_CHECK_ERR_GOTO(!ret->tok_len, LOGMEM(ctx), error);
2609
2610 while (is_xmlws(expr[parsed])) {
2611 ++parsed;
2612 }
2613
2614 do {
2615 if (expr[parsed] == '(') {
2616
2617 /* '(' */
2618 tok_len = 1;
2619 tok_type = LYXP_TOKEN_PAR1;
2620
2621 if (prev_function_check && ret->used && (ret->tokens[ret->used - 1] == LYXP_TOKEN_NAMETEST)) {
2622 /* it is a NodeType/FunctionName after all */
2623 if (((ret->tok_len[ret->used - 1] == 4)
2624 && (!strncmp(&expr[ret->tok_pos[ret->used - 1]], "node", 4)
2625 || !strncmp(&expr[ret->tok_pos[ret->used - 1]], "text", 4))) ||
2626 ((ret->tok_len[ret->used - 1] == 7)
2627 && !strncmp(&expr[ret->tok_pos[ret->used - 1]], "comment", 7))) {
2628 ret->tokens[ret->used - 1] = LYXP_TOKEN_NODETYPE;
2629 } else {
2630 ret->tokens[ret->used - 1] = LYXP_TOKEN_FUNCNAME;
2631 }
2632 prev_function_check = 0;
2633 }
2634
2635 } else if (expr[parsed] == ')') {
2636
2637 /* ')' */
2638 tok_len = 1;
2639 tok_type = LYXP_TOKEN_PAR2;
2640
2641 } else if (expr[parsed] == '[') {
2642
2643 /* '[' */
2644 tok_len = 1;
2645 tok_type = LYXP_TOKEN_BRACK1;
2646
2647 } else if (expr[parsed] == ']') {
2648
2649 /* ']' */
2650 tok_len = 1;
2651 tok_type = LYXP_TOKEN_BRACK2;
2652
2653 } else if (!strncmp(&expr[parsed], "..", 2)) {
2654
2655 /* '..' */
2656 tok_len = 2;
2657 tok_type = LYXP_TOKEN_DDOT;
2658
2659 } else if ((expr[parsed] == '.') && (!isdigit(expr[parsed + 1]))) {
2660
2661 /* '.' */
2662 tok_len = 1;
2663 tok_type = LYXP_TOKEN_DOT;
2664
2665 } else if (expr[parsed] == '@') {
2666
2667 /* '@' */
2668 tok_len = 1;
2669 tok_type = LYXP_TOKEN_AT;
2670
2671 } else if (expr[parsed] == ',') {
2672
2673 /* ',' */
2674 tok_len = 1;
2675 tok_type = LYXP_TOKEN_COMMA;
2676
2677 } else if (expr[parsed] == '\'') {
2678
2679 /* Literal with ' */
2680 for (tok_len = 1; (expr[parsed + tok_len] != '\0') && (expr[parsed + tok_len] != '\''); ++tok_len);
2681 LY_CHECK_ERR_GOTO(expr[parsed + tok_len] == '\0',
2682 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOE, expr[parsed], &expr[parsed]), error);
2683 ++tok_len;
2684 tok_type = LYXP_TOKEN_LITERAL;
2685
2686 } else if (expr[parsed] == '\"') {
2687
2688 /* Literal with " */
2689 for (tok_len = 1; (expr[parsed + tok_len] != '\0') && (expr[parsed + tok_len] != '\"'); ++tok_len);
2690 LY_CHECK_ERR_GOTO(expr[parsed + tok_len] == '\0',
2691 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOE, expr[parsed], &expr[parsed]), error);
2692 ++tok_len;
2693 tok_type = LYXP_TOKEN_LITERAL;
2694
2695 } else if ((expr[parsed] == '.') || (isdigit(expr[parsed]))) {
2696
2697 /* Number */
2698 for (tok_len = 0; isdigit(expr[parsed + tok_len]); ++tok_len);
2699 if (expr[parsed + tok_len] == '.') {
2700 ++tok_len;
2701 for (; isdigit(expr[parsed + tok_len]); ++tok_len);
2702 }
2703 tok_type = LYXP_TOKEN_NUMBER;
2704
2705 } else if (expr[parsed] == '/') {
2706
2707 /* Operator '/', '//' */
2708 if (!strncmp(&expr[parsed], "//", 2)) {
2709 tok_len = 2;
2710 } else {
2711 tok_len = 1;
2712 }
2713 tok_type = LYXP_TOKEN_OPERATOR_PATH;
2714
2715 } else if (!strncmp(&expr[parsed], "!=", 2) || !strncmp(&expr[parsed], "<=", 2)
2716 || !strncmp(&expr[parsed], ">=", 2)) {
2717
2718 /* Operator '!=', '<=', '>=' */
2719 tok_len = 2;
2720 tok_type = LYXP_TOKEN_OPERATOR_COMP;
2721
2722 } else if (expr[parsed] == '|') {
2723
2724 /* Operator '|' */
2725 tok_len = 1;
2726 tok_type = LYXP_TOKEN_OPERATOR_UNI;
2727
2728 } else if ((expr[parsed] == '+') || (expr[parsed] == '-')) {
2729
2730 /* Operator '+', '-' */
2731 tok_len = 1;
2732 tok_type = LYXP_TOKEN_OPERATOR_MATH;
2733
2734 } else if ((expr[parsed] == '=') || (expr[parsed] == '<') || (expr[parsed] == '>')) {
2735
2736 /* Operator '=', '<', '>' */
2737 tok_len = 1;
2738 tok_type = LYXP_TOKEN_OPERATOR_COMP;
2739
2740 } else if (ret->used && (ret->tokens[ret->used - 1] != LYXP_TOKEN_AT)
2741 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_PAR1)
2742 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_BRACK1)
2743 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_COMMA)
2744 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPERATOR_LOG)
2745 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPERATOR_COMP)
2746 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPERATOR_MATH)
2747 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPERATOR_UNI)
2748 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPERATOR_PATH)) {
2749
2750 /* Operator '*', 'or', 'and', 'mod', or 'div' */
2751 if (expr[parsed] == '*') {
2752 tok_len = 1;
2753 tok_type = LYXP_TOKEN_OPERATOR_MATH;
2754
2755 } else if (!strncmp(&expr[parsed], "or", 2)) {
2756 tok_len = 2;
2757 tok_type = LYXP_TOKEN_OPERATOR_LOG;
2758
2759 } else if (!strncmp(&expr[parsed], "and", 3)) {
2760 tok_len = 3;
2761 tok_type = LYXP_TOKEN_OPERATOR_LOG;
2762
2763 } else if (!strncmp(&expr[parsed], "mod", 3) || !strncmp(&expr[parsed], "div", 3)) {
2764 tok_len = 3;
2765 tok_type = LYXP_TOKEN_OPERATOR_MATH;
2766
2767 } else if (prev_function_check) {
Michal Vasko53078572019-05-24 08:50:15 +02002768 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "Invalid character 0x%x ('%c'), perhaps \"%.*s\" is supposed to be a function call.",
2769 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 +01002770 goto error;
2771 } else {
Radek Krejcid4270262019-01-07 15:07:25 +01002772 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INEXPR, parsed + 1, expr);
Radek Krejcib1646a92018-11-02 16:08:26 +01002773 goto error;
2774 }
2775 } else if (expr[parsed] == '*') {
2776
2777 /* NameTest '*' */
2778 tok_len = 1;
2779 tok_type = LYXP_TOKEN_NAMETEST;
2780
2781 } else {
2782
2783 /* NameTest (NCName ':' '*' | QName) or NodeType/FunctionName */
2784 ncname_len = parse_ncname(&expr[parsed]);
Radek Krejcid4270262019-01-07 15:07:25 +01002785 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 +01002786 tok_len = ncname_len;
2787
2788 if (expr[parsed + tok_len] == ':') {
2789 ++tok_len;
2790 if (expr[parsed + tok_len] == '*') {
2791 ++tok_len;
2792 } else {
2793 ncname_len = parse_ncname(&expr[parsed + tok_len]);
Radek Krejcid4270262019-01-07 15:07:25 +01002794 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 +01002795 tok_len += ncname_len;
2796 }
2797 /* remove old flag to prevent ambiguities */
2798 prev_function_check = 0;
2799 tok_type = LYXP_TOKEN_NAMETEST;
2800 } else {
2801 /* there is no prefix so it can still be NodeType/FunctionName, we can't finally decide now */
2802 prev_function_check = 1;
2803 tok_type = LYXP_TOKEN_NAMETEST;
2804 }
2805 }
2806
2807 /* store the token, move on to the next one */
2808 LY_CHECK_GOTO(exp_add_token(ctx, ret, tok_type, parsed, tok_len), error);
2809 parsed += tok_len;
2810 while (is_xmlws(expr[parsed])) {
2811 ++parsed;
2812 }
2813
2814 } while (expr[parsed]);
2815
2816 /* prealloc repeat */
2817 ret->repeat = calloc(ret->size, sizeof *ret->repeat);
2818 LY_CHECK_ERR_GOTO(!ret->repeat, LOGMEM(ctx), error);
2819
Michal Vasko03ff5a72019-09-11 13:49:33 +02002820 /* fill repeat */
2821 LY_CHECK_GOTO(reparse_or_expr(ctx, ret, &exp_idx), error);
2822 if (ret->used > exp_idx) {
2823 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK, "Unknown", &ret->expr[ret->tok_pos[exp_idx]]);
2824 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "Unparsed characters \"%s\" left at the end of an XPath expression.",
2825 &ret->expr[ret->tok_pos[exp_idx]]);
2826 goto error;
2827 }
2828
2829 print_expr_struct_debug(ret);
2830
Radek Krejcib1646a92018-11-02 16:08:26 +01002831 return ret;
2832
2833error:
2834 lyxp_expr_free(ctx, ret);
2835 return NULL;
2836}
2837
Michal Vasko03ff5a72019-09-11 13:49:33 +02002838/*
2839 * warn functions
2840 *
2841 * Warn functions check specific reasonable conditions for schema XPath
2842 * and print a warning if they are not satisfied.
2843 */
2844
2845/**
2846 * @brief Get the last-added schema node that is currently in the context.
2847 *
2848 * @param[in] set Set to search in.
2849 * @return Last-added schema context node, NULL if no node is in context.
2850 */
2851static struct lysc_node *
2852warn_get_scnode_in_ctx(struct lyxp_set *set)
2853{
2854 uint32_t i;
2855
2856 if (!set || (set->type != LYXP_SET_SCNODE_SET)) {
2857 return NULL;
2858 }
2859
2860 i = set->used;
2861 do {
2862 --i;
2863 if (set->val.scnodes[i].in_ctx == 1) {
2864 /* if there are more, simply return the first found (last added) */
2865 return set->val.scnodes[i].scnode;
2866 }
2867 } while (i);
2868
2869 return NULL;
2870}
2871
2872/**
2873 * @brief Test whether a type is numeric - integer type or decimal64.
2874 *
2875 * @param[in] type Type to test.
2876 * @return 1 if numeric, 0 otherwise.
2877 */
2878static int
2879warn_is_numeric_type(struct lysc_type *type)
2880{
2881 struct lysc_type_union *uni;
2882 int ret;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002883 LY_ARRAY_SIZE_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002884
2885 switch (type->basetype) {
2886 case LY_TYPE_DEC64:
2887 case LY_TYPE_INT8:
2888 case LY_TYPE_UINT8:
2889 case LY_TYPE_INT16:
2890 case LY_TYPE_UINT16:
2891 case LY_TYPE_INT32:
2892 case LY_TYPE_UINT32:
2893 case LY_TYPE_INT64:
2894 case LY_TYPE_UINT64:
2895 return 1;
2896 case LY_TYPE_UNION:
2897 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002898 LY_ARRAY_FOR(uni->types, u) {
2899 ret = warn_is_numeric_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002900 if (ret) {
2901 /* found a suitable type */
2902 return 1;
2903 }
2904 }
2905 /* did not find any suitable type */
2906 return 0;
2907 case LY_TYPE_LEAFREF:
2908 return warn_is_numeric_type(((struct lysc_type_leafref *)type)->realtype);
2909 default:
2910 return 0;
2911 }
2912}
2913
2914/**
2915 * @brief Test whether a type is string-like - no integers, decimal64 or binary.
2916 *
2917 * @param[in] type Type to test.
2918 * @return 1 if string, 0 otherwise.
2919 */
2920static int
2921warn_is_string_type(struct lysc_type *type)
2922{
2923 struct lysc_type_union *uni;
2924 int ret;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002925 LY_ARRAY_SIZE_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002926
2927 switch (type->basetype) {
2928 case LY_TYPE_BITS:
2929 case LY_TYPE_ENUM:
2930 case LY_TYPE_IDENT:
2931 case LY_TYPE_INST:
2932 case LY_TYPE_STRING:
2933 return 1;
2934 case LY_TYPE_UNION:
2935 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002936 LY_ARRAY_FOR(uni->types, u) {
2937 ret = warn_is_string_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002938 if (ret) {
2939 /* found a suitable type */
2940 return 1;
2941 }
2942 }
2943 /* did not find any suitable type */
2944 return 0;
2945 case LY_TYPE_LEAFREF:
2946 return warn_is_string_type(((struct lysc_type_leafref *)type)->realtype);
2947 default:
2948 return 0;
2949 }
2950}
2951
2952/**
2953 * @brief Test whether a type is one specific type.
2954 *
2955 * @param[in] type Type to test.
2956 * @param[in] base Expected type.
2957 * @return 1 if it is, 0 otherwise.
2958 */
2959static int
2960warn_is_specific_type(struct lysc_type *type, LY_DATA_TYPE base)
2961{
2962 struct lysc_type_union *uni;
2963 int ret;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002964 LY_ARRAY_SIZE_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002965
2966 if (type->basetype == base) {
2967 return 1;
2968 } else if (type->basetype == LY_TYPE_UNION) {
2969 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002970 LY_ARRAY_FOR(uni->types, u) {
2971 ret = warn_is_specific_type(uni->types[u], base);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002972 if (ret) {
2973 /* found a suitable type */
2974 return 1;
2975 }
2976 }
2977 /* did not find any suitable type */
2978 return 0;
2979 } else if (type->basetype == LY_TYPE_LEAFREF) {
2980 return warn_is_specific_type(((struct lysc_type_leafref *)type)->realtype, base);
2981 }
2982
2983 return 0;
2984}
2985
2986/**
2987 * @brief Get next type of a (union) type.
2988 *
2989 * @param[in] type Base type.
2990 * @param[in] prev_type Previously returned type.
2991 * @return Next type or NULL.
2992 */
2993static struct lysc_type *
2994warn_is_equal_type_next_type(struct lysc_type *type, struct lysc_type *prev_type)
2995{
2996 struct lysc_type_union *uni;
2997 int found = 0;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002998 LY_ARRAY_SIZE_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002999
3000 switch (type->basetype) {
3001 case LY_TYPE_UNION:
3002 uni = (struct lysc_type_union *)type;
3003 if (!prev_type) {
3004 return uni->types[0];
3005 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003006 LY_ARRAY_FOR(uni->types, u) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003007 if (found) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003008 return uni->types[u];
Michal Vasko03ff5a72019-09-11 13:49:33 +02003009 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003010 if (prev_type == uni->types[u]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003011 found = 1;
3012 }
3013 }
3014 return NULL;
3015 default:
3016 if (prev_type) {
3017 assert(type == prev_type);
3018 return NULL;
3019 } else {
3020 return type;
3021 }
3022 }
3023}
3024
3025/**
3026 * @brief Test whether 2 types have a common type.
3027 *
3028 * @param[in] type1 First type.
3029 * @param[in] type2 Second type.
3030 * @return 1 if they do, 0 otherwise.
3031 */
3032static int
3033warn_is_equal_type(struct lysc_type *type1, struct lysc_type *type2)
3034{
3035 struct lysc_type *t1, *rt1, *t2, *rt2;
3036
3037 t1 = NULL;
3038 while ((t1 = warn_is_equal_type_next_type(type1, t1))) {
3039 if (t1->basetype == LY_TYPE_LEAFREF) {
3040 rt1 = ((struct lysc_type_leafref *)t1)->realtype;
3041 } else {
3042 rt1 = t1;
3043 }
3044
3045 t2 = NULL;
3046 while ((t2 = warn_is_equal_type_next_type(type2, t2))) {
3047 if (t2->basetype == LY_TYPE_LEAFREF) {
3048 rt2 = ((struct lysc_type_leafref *)t2)->realtype;
3049 } else {
3050 rt2 = t2;
3051 }
3052
3053 if (rt2->basetype == rt1->basetype) {
3054 /* match found */
3055 return 1;
3056 }
3057 }
3058 }
3059
3060 return 0;
3061}
3062
3063/**
3064 * @brief Check both operands of comparison operators.
3065 *
3066 * @param[in] ctx Context for errors.
3067 * @param[in] set1 First operand set.
3068 * @param[in] set2 Second operand set.
3069 * @param[in] numbers_only Whether accept only numbers or other types are fine too (for '=' and '!=').
3070 * @param[in] expr Start of the expression to print with the warning.
3071 * @param[in] tok_pos Token position.
3072 */
3073static void
3074warn_operands(struct ly_ctx *ctx, struct lyxp_set *set1, struct lyxp_set *set2, int numbers_only, const char *expr, uint16_t tok_pos)
3075{
3076 struct lysc_node_leaf *node1, *node2;
3077 int leaves = 1, warning = 0;
3078
3079 node1 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set1);
3080 node2 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set2);
3081
3082 if (!node1 && !node2) {
3083 /* no node-sets involved, nothing to do */
3084 return;
3085 }
3086
3087 if (node1) {
3088 if (!(node1->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3089 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node1->nodetype), node1->name);
3090 warning = 1;
3091 leaves = 0;
3092 } else if (numbers_only && !warn_is_numeric_type(node1->type)) {
3093 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node1->name);
3094 warning = 1;
3095 }
3096 }
3097
3098 if (node2) {
3099 if (!(node2->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3100 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node2->nodetype), node2->name);
3101 warning = 1;
3102 leaves = 0;
3103 } else if (numbers_only && !warn_is_numeric_type(node2->type)) {
3104 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node2->name);
3105 warning = 1;
3106 }
3107 }
3108
3109 if (node1 && node2 && leaves && !numbers_only) {
3110 if ((warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type))
3111 || (!warn_is_numeric_type(node1->type) && warn_is_numeric_type(node2->type))
3112 || (!warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type)
3113 && !warn_is_equal_type(node1->type, node2->type))) {
3114 LOGWRN(ctx, "Incompatible types of operands \"%s\" and \"%s\" for comparison.", node1->name, node2->name);
3115 warning = 1;
3116 }
3117 }
3118
3119 if (warning) {
3120 LOGWRN(ctx, "Previous warning generated by XPath subexpression[%u] \"%.20s\".", tok_pos, expr + tok_pos);
3121 }
3122}
3123
3124/**
3125 * @brief Check that a value is valid for a leaf. If not applicable, does nothing.
3126 *
3127 * @param[in] exp Parsed XPath expression.
3128 * @param[in] set Set with the leaf/leaf-list.
3129 * @param[in] val_exp Index of the value (literal/number) in @p exp.
3130 * @param[in] equal_exp Index of the start of the equality expression in @p exp.
3131 * @param[in] last_equal_exp Index of the end of the equality expression in @p exp.
3132 */
3133static void
3134warn_equality_value(struct lyxp_expr *exp, struct lyxp_set *set, uint16_t val_exp, uint16_t equal_exp, uint16_t last_equal_exp)
3135{
3136 struct lysc_node *scnode;
3137 struct lysc_type *type;
3138 char *value;
3139 LY_ERR rc;
3140 struct ly_err_item *err = NULL;
3141
3142 if ((scnode = warn_get_scnode_in_ctx(set)) && (scnode->nodetype & (LYS_LEAF | LYS_LEAFLIST))
3143 && ((exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) || (exp->tokens[val_exp] == LYXP_TOKEN_NUMBER))) {
3144 /* check that the node can have the specified value */
3145 if (exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) {
3146 value = strndup(exp->expr + exp->tok_pos[val_exp] + 1, exp->tok_len[val_exp] - 2);
3147 } else {
3148 value = strndup(exp->expr + exp->tok_pos[val_exp], exp->tok_len[val_exp]);
3149 }
3150 if (!value) {
3151 LOGMEM(set->ctx);
3152 return;
3153 }
3154
3155 if ((((struct lysc_node_leaf *)scnode)->type->basetype == LY_TYPE_IDENT) && !strchr(value, ':')) {
3156 LOGWRN(set->ctx, "Identityref \"%s\" comparison with identity \"%s\" without prefix, consider adding"
3157 " a prefix or best using \"derived-from(-or-self)()\" functions.", scnode->name, value);
3158 LOGWRN(set->ctx, "Previous warning generated by XPath subexpression[%u] \"%.*s\".", exp->tok_pos[equal_exp],
3159 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
3160 exp->expr + exp->tok_pos[equal_exp]);
3161 }
3162
3163 type = ((struct lysc_node_leaf *)scnode)->type;
3164 if (type->basetype != LY_TYPE_IDENT) {
3165 rc = type->plugin->store(set->ctx, type, value, strlen(value), LY_TYPE_OPTS_SCHEMA,
3166 lys_resolve_prefix, (void *)type->dflt_mod, LYD_XML, NULL, NULL, NULL, NULL, &err);
3167
3168 if (err) {
3169 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type (%s).", value, err->msg);
3170 ly_err_free(err);
3171 } else if (rc != LY_SUCCESS) {
3172 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type.", value);
3173 }
3174 if (rc != LY_SUCCESS) {
3175 LOGWRN(set->ctx, "Previous warning generated by XPath subexpression[%u] \"%.*s\".", exp->tok_pos[equal_exp],
3176 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
3177 exp->expr + exp->tok_pos[equal_exp]);
3178 }
3179 }
3180 free(value);
3181 }
3182}
3183
3184/*
3185 * XPath functions
3186 */
3187
3188/**
3189 * @brief Execute the YANG 1.1 bit-is-set(node-set, string) function. Returns LYXP_SET_BOOLEAN
3190 * depending on whether the first node bit value from the second argument is set.
3191 *
3192 * @param[in] args Array of arguments.
3193 * @param[in] arg_count Count of elements in @p args.
3194 * @param[in,out] set Context and result set at the same time.
3195 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003196 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003197 */
3198static LY_ERR
3199xpath_bit_is_set(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3200{
3201 struct lyd_node_term *leaf;
3202 struct lysc_node_leaf *sleaf;
3203 struct lysc_type_bits *bits;
3204 LY_ERR rc = LY_SUCCESS;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003205 LY_ARRAY_SIZE_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003206
3207 if (options & LYXP_SCNODE_ALL) {
3208 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3209 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003210 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3211 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 +02003212 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_BITS)) {
3213 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"bits\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003214 }
3215
3216 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3217 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3218 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 +02003219 } else if (!warn_is_string_type(sleaf->type)) {
3220 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003221 }
3222 }
3223 set_scnode_clear_ctx(set);
3224 return rc;
3225 }
3226
Michal Vaskod3678892020-05-21 10:06:58 +02003227 if (args[0]->type != LYXP_SET_NODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003228 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)");
3229 return LY_EVALID;
3230 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003231 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003232 LY_CHECK_RET(rc);
3233
3234 set_fill_boolean(set, 0);
Michal Vaskod3678892020-05-21 10:06:58 +02003235 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003236 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
3237 if ((leaf->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST))
3238 && (((struct lysc_node_leaf *)leaf->schema)->type->basetype == LY_TYPE_BITS)) {
3239 bits = (struct lysc_type_bits *)((struct lysc_node_leaf *)leaf->schema)->type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003240 LY_ARRAY_FOR(bits->bits, u) {
3241 if (!strcmp(bits->bits[u].name, args[1]->val.str)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003242 set_fill_boolean(set, 1);
3243 break;
3244 }
3245 }
3246 }
3247 }
3248
3249 return LY_SUCCESS;
3250}
3251
3252/**
3253 * @brief Execute the XPath boolean(object) function. Returns LYXP_SET_BOOLEAN
3254 * with the argument converted to boolean.
3255 *
3256 * @param[in] args Array of arguments.
3257 * @param[in] arg_count Count of elements in @p args.
3258 * @param[in,out] set Context and result set at the same time.
3259 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003260 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003261 */
3262static LY_ERR
3263xpath_boolean(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3264{
3265 LY_ERR rc;
3266
3267 if (options & LYXP_SCNODE_ALL) {
3268 set_scnode_clear_ctx(set);
3269 return LY_SUCCESS;
3270 }
3271
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003272 rc = lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003273 LY_CHECK_RET(rc);
3274 set_fill_set(set, args[0]);
3275
3276 return LY_SUCCESS;
3277}
3278
3279/**
3280 * @brief Execute the XPath ceiling(number) function. Returns LYXP_SET_NUMBER
3281 * with the first argument rounded up to the nearest integer.
3282 *
3283 * @param[in] args Array of arguments.
3284 * @param[in] arg_count Count of elements in @p args.
3285 * @param[in,out] set Context and result set at the same time.
3286 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003287 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003288 */
3289static LY_ERR
3290xpath_ceiling(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3291{
3292 struct lysc_node_leaf *sleaf;
3293 LY_ERR rc = LY_SUCCESS;
3294
3295 if (options & LYXP_SCNODE_ALL) {
3296 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3297 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003298 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3299 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 +02003300 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
3301 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003302 }
3303 set_scnode_clear_ctx(set);
3304 return rc;
3305 }
3306
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003307 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003308 LY_CHECK_RET(rc);
3309 if ((long long)args[0]->val.num != args[0]->val.num) {
3310 set_fill_number(set, ((long long)args[0]->val.num) + 1);
3311 } else {
3312 set_fill_number(set, args[0]->val.num);
3313 }
3314
3315 return LY_SUCCESS;
3316}
3317
3318/**
3319 * @brief Execute the XPath concat(string, string, string*) function.
3320 * Returns LYXP_SET_STRING with the concatenation of all the arguments.
3321 *
3322 * @param[in] args Array of arguments.
3323 * @param[in] arg_count Count of elements in @p args.
3324 * @param[in,out] set Context and result set at the same time.
3325 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003326 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003327 */
3328static LY_ERR
3329xpath_concat(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
3330{
3331 uint16_t i;
3332 char *str = NULL;
3333 size_t used = 1;
3334 LY_ERR rc = LY_SUCCESS;
3335 struct lysc_node_leaf *sleaf;
3336
3337 if (options & LYXP_SCNODE_ALL) {
3338 for (i = 0; i < arg_count; ++i) {
3339 if ((args[i]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[i]))) {
3340 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3341 LOGWRN(set->ctx, "Argument #%u of %s is a %s node \"%s\".",
3342 i + 1, __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003343 } else if (!warn_is_string_type(sleaf->type)) {
3344 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 +02003345 }
3346 }
3347 }
3348 set_scnode_clear_ctx(set);
3349 return rc;
3350 }
3351
3352 for (i = 0; i < arg_count; ++i) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003353 rc = lyxp_set_cast(args[i], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003354 if (rc != LY_SUCCESS) {
3355 free(str);
3356 return rc;
3357 }
3358
3359 str = ly_realloc(str, (used + strlen(args[i]->val.str)) * sizeof(char));
3360 LY_CHECK_ERR_RET(!str, LOGMEM(set->ctx), LY_EMEM);
3361 strcpy(str + used - 1, args[i]->val.str);
3362 used += strlen(args[i]->val.str);
3363 }
3364
3365 /* free, kind of */
Michal Vaskod3678892020-05-21 10:06:58 +02003366 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003367 set->type = LYXP_SET_STRING;
3368 set->val.str = str;
3369
3370 return LY_SUCCESS;
3371}
3372
3373/**
3374 * @brief Execute the XPath contains(string, string) function.
3375 * Returns LYXP_SET_BOOLEAN whether the second argument can
3376 * be found in the first or not.
3377 *
3378 * @param[in] args Array of arguments.
3379 * @param[in] arg_count Count of elements in @p args.
3380 * @param[in,out] set Context and result set at the same time.
3381 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003382 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003383 */
3384static LY_ERR
3385xpath_contains(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3386{
3387 struct lysc_node_leaf *sleaf;
3388 LY_ERR rc = LY_SUCCESS;
3389
3390 if (options & LYXP_SCNODE_ALL) {
3391 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3392 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3393 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 +02003394 } else if (!warn_is_string_type(sleaf->type)) {
3395 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003396 }
3397 }
3398
3399 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3400 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3401 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 +02003402 } else if (!warn_is_string_type(sleaf->type)) {
3403 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003404 }
3405 }
3406 set_scnode_clear_ctx(set);
3407 return rc;
3408 }
3409
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003410 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003411 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003412 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003413 LY_CHECK_RET(rc);
3414
3415 if (strstr(args[0]->val.str, args[1]->val.str)) {
3416 set_fill_boolean(set, 1);
3417 } else {
3418 set_fill_boolean(set, 0);
3419 }
3420
3421 return LY_SUCCESS;
3422}
3423
3424/**
3425 * @brief Execute the XPath count(node-set) function. Returns LYXP_SET_NUMBER
3426 * with the size of the node-set from the argument.
3427 *
3428 * @param[in] args Array of arguments.
3429 * @param[in] arg_count Count of elements in @p args.
3430 * @param[in,out] set Context and result set at the same time.
3431 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003432 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003433 */
3434static LY_ERR
3435xpath_count(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3436{
3437 struct lysc_node *scnode = NULL;
3438 LY_ERR rc = LY_SUCCESS;
3439
3440 if (options & LYXP_SCNODE_ALL) {
3441 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(scnode = warn_get_scnode_in_ctx(args[0]))) {
3442 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003443 }
3444 set_scnode_clear_ctx(set);
3445 return rc;
3446 }
3447
Michal Vasko03ff5a72019-09-11 13:49:33 +02003448 if (args[0]->type != LYXP_SET_NODE_SET) {
3449 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "count(node-set)");
3450 return LY_EVALID;
3451 }
3452
3453 set_fill_number(set, args[0]->used);
3454 return LY_SUCCESS;
3455}
3456
3457/**
3458 * @brief Execute the XPath current() function. Returns LYXP_SET_NODE_SET
3459 * with the context with the intial node.
3460 *
3461 * @param[in] args Array of arguments.
3462 * @param[in] arg_count Count of elements in @p args.
3463 * @param[in,out] set Context and result set at the same time.
3464 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003465 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003466 */
3467static LY_ERR
3468xpath_current(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
3469{
3470 if (arg_count || args) {
3471 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGCOUNT, arg_count, "current()");
3472 return LY_EVALID;
3473 }
3474
3475 if (options & LYXP_SCNODE_ALL) {
3476 set_scnode_clear_ctx(set);
3477
Michal Vaskoecd62de2019-11-13 12:35:11 +01003478 lyxp_set_scnode_insert_node(set, set->ctx_scnode, LYXP_NODE_ELEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003479 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02003480 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003481
3482 /* position is filled later */
3483 set_insert_node(set, set->ctx_node, 0, LYXP_NODE_ELEM, 0);
3484 }
3485
3486 return LY_SUCCESS;
3487}
3488
3489/**
3490 * @brief Execute the YANG 1.1 deref(node-set) function. Returns LYXP_SET_NODE_SET with either
3491 * leafref or instance-identifier target node(s).
3492 *
3493 * @param[in] args Array of arguments.
3494 * @param[in] arg_count Count of elements in @p args.
3495 * @param[in,out] set Context and result set at the same time.
3496 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003497 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003498 */
3499static LY_ERR
3500xpath_deref(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3501{
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003502 struct lysc_ctx cctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003503 struct lyd_node_term *leaf;
Michal Vasko42e497c2020-01-06 08:38:25 +01003504 struct lysc_node_leaf *sleaf = NULL;
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003505 const struct lysc_node *target;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003506 const struct lyd_node *node;
3507 char *errmsg = NULL;
3508 const char *val;
3509 int dynamic;
3510 LY_ERR rc = LY_SUCCESS;
3511
3512 if (options & LYXP_SCNODE_ALL) {
3513 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3514 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003515 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3516 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 +02003517 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_LEAFREF) && !warn_is_specific_type(sleaf->type, LY_TYPE_INST)) {
3518 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"leafref\" nor \"instance-identifier\".",
3519 __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003520 }
3521 set_scnode_clear_ctx(set);
Michal Vasko42e497c2020-01-06 08:38:25 +01003522 if (sleaf && (sleaf->type->basetype == LY_TYPE_LEAFREF)) {
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003523 cctx.ctx = set->ctx;
3524 rc = lys_compile_leafref_validate(&cctx, (struct lysc_node *)sleaf, (struct lysc_type_leafref *)sleaf->type, &target);
3525 /* it was already validated, it must succeed */
3526 if (rc == LY_SUCCESS) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01003527 lyxp_set_scnode_insert_node(set, target, LYXP_NODE_ELEM);
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003528 }
3529 }
3530
Michal Vasko03ff5a72019-09-11 13:49:33 +02003531 return rc;
3532 }
3533
Michal Vaskod3678892020-05-21 10:06:58 +02003534 if (args[0]->type != LYXP_SET_NODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003535 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "deref(node-set)");
3536 return LY_EVALID;
3537 }
3538
Michal Vaskod3678892020-05-21 10:06:58 +02003539 lyxp_set_free_content(set);
3540 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003541 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
3542 sleaf = (struct lysc_node_leaf *)leaf->schema;
3543 if (sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
3544 if (sleaf->type->basetype == LY_TYPE_LEAFREF) {
3545 /* find leafref target */
3546 val = lyd_value2str(leaf, &dynamic);
3547 node = ly_type_find_leafref(set->ctx, sleaf->type, val, strlen(val), (struct lyd_node *)leaf,
Michal Vaskof03ed032020-03-04 13:31:44 +01003548 set->tree, &leaf->value, &errmsg);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003549 if (dynamic) {
3550 free((char *)val);
3551 }
3552 if (!node) {
3553 LOGERR(set->ctx, LY_EINVAL, errmsg);
3554 free(errmsg);
3555 return LY_EINVAL;
3556 }
3557
3558 /* insert it */
3559 set_insert_node(set, node, 0, LYXP_NODE_ELEM, 0);
3560 } else {
3561 assert(sleaf->type->basetype == LY_TYPE_INST);
Michal Vaskof03ed032020-03-04 13:31:44 +01003562 node = (struct lyd_node *)lyd_target(leaf->value.target, set->tree);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003563 if (!node) {
3564 val = lyd_value2str(leaf, &dynamic);
3565 LOGERR(set->ctx, LY_EVALID, "Invalid instance-identifier \"%s\" value - required instance not found.", val);
3566 if (dynamic) {
3567 free((char *)val);
3568 }
3569 return LY_EVALID;
3570 }
3571 }
3572 }
3573 }
3574
3575 return LY_SUCCESS;
3576}
3577
3578/**
3579 * @brief Execute the YANG 1.1 derived-from(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
3580 * on whether the first argument nodes contain a node of an identity derived from the second
3581 * argument identity.
3582 *
3583 * @param[in] args Array of arguments.
3584 * @param[in] arg_count Count of elements in @p args.
3585 * @param[in,out] set Context and result set at the same time.
3586 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003587 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003588 */
3589static LY_ERR
3590xpath_derived_from(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3591{
3592 uint16_t i;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003593 LY_ARRAY_SIZE_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003594 struct lyd_node_term *leaf;
3595 struct lysc_node_leaf *sleaf;
3596 struct lyd_value data = {0};
3597 struct ly_err_item *err = NULL;
3598 LY_ERR rc = LY_SUCCESS;
3599 int found;
3600
3601 if (options & LYXP_SCNODE_ALL) {
3602 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3603 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003604 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3605 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 +02003606 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_IDENT)) {
3607 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"identityref\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003608 }
3609
3610 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3611 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3612 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 +02003613 } else if (!warn_is_string_type(sleaf->type)) {
3614 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003615 }
3616 }
3617 set_scnode_clear_ctx(set);
3618 return rc;
3619 }
3620
Michal Vaskod3678892020-05-21 10:06:58 +02003621 if (args[0]->type != LYXP_SET_NODE_SET) {
3622 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
3623 "derived-from(node-set, string)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003624 return LY_EVALID;
3625 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003626 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003627 LY_CHECK_RET(rc);
3628
3629 set_fill_boolean(set, 0);
Michal Vaskod3678892020-05-21 10:06:58 +02003630 found = 0;
3631 for (i = 0; i < args[0]->used; ++i) {
3632 leaf = (struct lyd_node_term *)args[0]->val.nodes[i].node;
3633 sleaf = (struct lysc_node_leaf *)leaf->schema;
3634 if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_IDENT)) {
3635 /* store args[1] into ident */
3636 rc = sleaf->type->plugin->store(set->ctx, sleaf->type, args[1]->val.str, strlen(args[1]->val.str),
3637 LY_TYPE_OPTS_STORE, lys_resolve_prefix, (void *)sleaf->dflt_mod, set->format,
3638 (struct lyd_node *)leaf, set->tree, &data, NULL, &err);
3639 if (err) {
3640 ly_err_print(err);
3641 ly_err_free(err);
3642 }
3643 LY_CHECK_RET(rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003644
Michal Vaskod3678892020-05-21 10:06:58 +02003645 LY_ARRAY_FOR(data.ident->derived, u) {
3646 if (data.ident->derived[u] == leaf->value.ident) {
3647 set_fill_boolean(set, 1);
3648 found = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003649 break;
3650 }
3651 }
Michal Vaskod3678892020-05-21 10:06:58 +02003652 if (found) {
3653 break;
3654 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003655 }
3656 }
3657
3658 return LY_SUCCESS;
3659}
3660
3661/**
3662 * @brief Execute the YANG 1.1 derived-from-or-self(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
3663 * on whether the first argument nodes contain a node of an identity that either is or is derived from
3664 * the second argument identity.
3665 *
3666 * @param[in] args Array of arguments.
3667 * @param[in] arg_count Count of elements in @p args.
3668 * @param[in,out] set Context and result set at the same time.
3669 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003670 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003671 */
3672static LY_ERR
3673xpath_derived_from_or_self(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3674{
3675 uint16_t i;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003676 LY_ARRAY_SIZE_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003677 struct lyd_node_term *leaf;
3678 struct lysc_node_leaf *sleaf;
3679 struct lyd_value data = {0};
3680 struct ly_err_item *err = NULL;
3681 LY_ERR rc = LY_SUCCESS;
3682 int found;
3683
3684 if (options & LYXP_SCNODE_ALL) {
3685 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3686 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003687 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3688 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 +02003689 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_IDENT)) {
3690 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"identityref\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003691 }
3692
3693 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3694 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3695 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 +02003696 } else if (!warn_is_string_type(sleaf->type)) {
3697 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003698 }
3699 }
3700 set_scnode_clear_ctx(set);
3701 return rc;
3702 }
3703
Michal Vaskod3678892020-05-21 10:06:58 +02003704 if (args[0]->type != LYXP_SET_NODE_SET) {
3705 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
3706 "derived-from-or-self(node-set, string)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003707 return LY_EVALID;
3708 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003709 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003710 LY_CHECK_RET(rc);
3711
3712 set_fill_boolean(set, 0);
Michal Vaskod3678892020-05-21 10:06:58 +02003713 found = 0;
3714 for (i = 0; i < args[0]->used; ++i) {
3715 leaf = (struct lyd_node_term *)args[0]->val.nodes[i].node;
3716 sleaf = (struct lysc_node_leaf *)leaf->schema;
3717 if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_IDENT)) {
3718 /* store args[1] into ident */
3719 rc = sleaf->type->plugin->store(set->ctx, sleaf->type, args[1]->val.str, strlen(args[1]->val.str),
3720 LY_TYPE_OPTS_STORE, lys_resolve_prefix, (void *)sleaf->dflt_mod, set->format,
3721 (struct lyd_node *)leaf, set->tree, &data, NULL, &err);
3722 if (err) {
3723 ly_err_print(err);
3724 ly_err_free(err);
3725 }
3726 LY_CHECK_RET(rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003727
Michal Vaskod3678892020-05-21 10:06:58 +02003728 if (data.ident == leaf->value.ident) {
3729 set_fill_boolean(set, 1);
3730 break;
3731 }
3732 LY_ARRAY_FOR(data.ident->derived, u) {
3733 if (data.ident->derived[u] == leaf->value.ident) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003734 set_fill_boolean(set, 1);
Michal Vaskod3678892020-05-21 10:06:58 +02003735 found = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003736 break;
3737 }
Michal Vaskod3678892020-05-21 10:06:58 +02003738 }
3739 if (found) {
3740 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003741 }
3742 }
3743 }
3744
3745 return LY_SUCCESS;
3746}
3747
3748/**
3749 * @brief Execute the YANG 1.1 enum-value(node-set) function. Returns LYXP_SET_NUMBER
3750 * with the integer value of the first node's enum value, otherwise NaN.
3751 *
3752 * @param[in] args Array of arguments.
3753 * @param[in] arg_count Count of elements in @p args.
3754 * @param[in,out] set Context and result set at the same time.
3755 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003756 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003757 */
3758static LY_ERR
3759xpath_enum_value(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3760{
3761 struct lyd_node_term *leaf;
3762 struct lysc_node_leaf *sleaf;
3763 LY_ERR rc = LY_SUCCESS;
3764
3765 if (options & LYXP_SCNODE_ALL) {
3766 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3767 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003768 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3769 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 +02003770 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_ENUM)) {
3771 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"enumeration\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003772 }
3773 set_scnode_clear_ctx(set);
3774 return rc;
3775 }
3776
Michal Vaskod3678892020-05-21 10:06:58 +02003777 if (args[0]->type != LYXP_SET_NODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003778 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "enum-value(node-set)");
3779 return LY_EVALID;
3780 }
3781
3782 set_fill_number(set, NAN);
Michal Vaskod3678892020-05-21 10:06:58 +02003783 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003784 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
3785 sleaf = (struct lysc_node_leaf *)leaf->schema;
3786 if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_ENUM)) {
3787 set_fill_number(set, leaf->value.enum_item->value);
3788 }
3789 }
3790
3791 return LY_SUCCESS;
3792}
3793
3794/**
3795 * @brief Execute the XPath false() function. Returns LYXP_SET_BOOLEAN
3796 * with false value.
3797 *
3798 * @param[in] args Array of arguments.
3799 * @param[in] arg_count Count of elements in @p args.
3800 * @param[in,out] set Context and result set at the same time.
3801 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003802 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003803 */
3804static LY_ERR
3805xpath_false(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3806{
3807 if (options & LYXP_SCNODE_ALL) {
3808 set_scnode_clear_ctx(set);
3809 return LY_SUCCESS;
3810 }
3811
3812 set_fill_boolean(set, 0);
3813 return LY_SUCCESS;
3814}
3815
3816/**
3817 * @brief Execute the XPath floor(number) function. Returns LYXP_SET_NUMBER
3818 * with the first argument floored (truncated).
3819 *
3820 * @param[in] args Array of arguments.
3821 * @param[in] arg_count Count of elements in @p args.
3822 * @param[in,out] set Context and result set at the same time.
3823 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003824 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003825 */
3826static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003827xpath_floor(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int UNUSED(options))
Michal Vasko03ff5a72019-09-11 13:49:33 +02003828{
3829 LY_ERR rc;
3830
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003831 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003832 LY_CHECK_RET(rc);
3833 if (isfinite(args[0]->val.num)) {
3834 set_fill_number(set, (long long)args[0]->val.num);
3835 }
3836
3837 return LY_SUCCESS;
3838}
3839
3840/**
3841 * @brief Execute the XPath lang(string) function. Returns LYXP_SET_BOOLEAN
3842 * whether the language of the text matches the one from the argument.
3843 *
3844 * @param[in] args Array of arguments.
3845 * @param[in] arg_count Count of elements in @p args.
3846 * @param[in,out] set Context and result set at the same time.
3847 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003848 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003849 */
3850static LY_ERR
3851xpath_lang(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3852{
3853 const struct lyd_node *node;
3854 struct lysc_node_leaf *sleaf;
Michal Vasko9f96a052020-03-10 09:41:45 +01003855 struct lyd_meta *meta = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003856 const char *val;
3857 int i, dynamic;
3858 LY_ERR rc = LY_SUCCESS;
3859
3860 if (options & LYXP_SCNODE_ALL) {
3861 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3862 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3863 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 +02003864 } else if (!warn_is_string_type(sleaf->type)) {
3865 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003866 }
3867 }
3868 set_scnode_clear_ctx(set);
3869 return rc;
3870 }
3871
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003872 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003873 LY_CHECK_RET(rc);
3874
Michal Vasko03ff5a72019-09-11 13:49:33 +02003875 if (set->type != LYXP_SET_NODE_SET) {
3876 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "lang(string)");
3877 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02003878 } else if (!set->used) {
3879 set_fill_boolean(set, 0);
3880 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003881 }
3882
3883 switch (set->val.nodes[0].type) {
3884 case LYXP_NODE_ELEM:
3885 case LYXP_NODE_TEXT:
3886 node = set->val.nodes[0].node;
3887 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01003888 case LYXP_NODE_META:
3889 node = set->val.meta[0].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003890 break;
3891 default:
3892 /* nothing to do with roots */
3893 set_fill_boolean(set, 0);
3894 return LY_SUCCESS;
3895 }
3896
Michal Vasko9f96a052020-03-10 09:41:45 +01003897 /* find lang metadata */
Michal Vasko03ff5a72019-09-11 13:49:33 +02003898 for (; node; node = (struct lyd_node *)node->parent) {
Michal Vasko9f96a052020-03-10 09:41:45 +01003899 for (meta = node->meta; meta; meta = meta->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003900 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01003901 if (meta->name && !strcmp(meta->name, "lang") && !strcmp(meta->annotation->module->name, "xml")) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003902 break;
3903 }
3904 }
3905
Michal Vasko9f96a052020-03-10 09:41:45 +01003906 if (meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003907 break;
3908 }
3909 }
3910
3911 /* compare languages */
Michal Vasko9f96a052020-03-10 09:41:45 +01003912 if (!meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003913 set_fill_boolean(set, 0);
3914 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01003915 val = lyd_meta2str(meta, &dynamic);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003916 for (i = 0; args[0]->val.str[i]; ++i) {
3917 if (tolower(args[0]->val.str[i]) != tolower(val[i])) {
3918 set_fill_boolean(set, 0);
3919 break;
3920 }
3921 }
3922 if (!args[0]->val.str[i]) {
3923 if (!val[i] || (val[i] == '-')) {
3924 set_fill_boolean(set, 1);
3925 } else {
3926 set_fill_boolean(set, 0);
3927 }
3928 }
3929 if (dynamic) {
3930 free((char *)val);
3931 }
3932 }
3933
3934 return LY_SUCCESS;
3935}
3936
3937/**
3938 * @brief Execute the XPath last() function. Returns LYXP_SET_NUMBER
3939 * with the context size.
3940 *
3941 * @param[in] args Array of arguments.
3942 * @param[in] arg_count Count of elements in @p args.
3943 * @param[in,out] set Context and result set at the same time.
3944 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003945 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003946 */
3947static LY_ERR
3948xpath_last(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3949{
3950 if (options & LYXP_SCNODE_ALL) {
3951 set_scnode_clear_ctx(set);
3952 return LY_SUCCESS;
3953 }
3954
Michal Vasko03ff5a72019-09-11 13:49:33 +02003955 if (set->type != LYXP_SET_NODE_SET) {
3956 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "last()");
3957 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02003958 } else if (!set->used) {
3959 set_fill_number(set, 0);
3960 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003961 }
3962
3963 set_fill_number(set, set->ctx_size);
3964 return LY_SUCCESS;
3965}
3966
3967/**
3968 * @brief Execute the XPath local-name(node-set?) function. Returns LYXP_SET_STRING
3969 * with the node name without namespace from the argument or the context.
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_local_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
3979{
3980 struct lyxp_set_node *item;
3981 /* suppress unused variable warning */
3982 (void)options;
3983
3984 if (options & LYXP_SCNODE_ALL) {
3985 set_scnode_clear_ctx(set);
3986 return LY_SUCCESS;
3987 }
3988
3989 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003990 if (args[0]->type != LYXP_SET_NODE_SET) {
3991 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "local-name(node-set?)");
3992 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02003993 } else if (!args[0]->used) {
3994 set_fill_string(set, "", 0);
3995 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003996 }
3997
3998 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003999 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004000
4001 item = &args[0]->val.nodes[0];
4002 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004003 if (set->type != LYXP_SET_NODE_SET) {
4004 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "local-name(node-set?)");
4005 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004006 } else if (!set->used) {
4007 set_fill_string(set, "", 0);
4008 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004009 }
4010
4011 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004012 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004013
4014 item = &set->val.nodes[0];
4015 }
4016
4017 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004018 case LYXP_NODE_NONE:
4019 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004020 case LYXP_NODE_ROOT:
4021 case LYXP_NODE_ROOT_CONFIG:
4022 case LYXP_NODE_TEXT:
4023 set_fill_string(set, "", 0);
4024 break;
4025 case LYXP_NODE_ELEM:
4026 set_fill_string(set, item->node->schema->name, strlen(item->node->schema->name));
4027 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004028 case LYXP_NODE_META:
4029 set_fill_string(set, ((struct lyd_meta *)item->node)->name, strlen(((struct lyd_meta *)item->node)->name));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004030 break;
4031 }
4032
4033 return LY_SUCCESS;
4034}
4035
4036/**
4037 * @brief Execute the XPath name(node-set?) function. Returns LYXP_SET_STRING
4038 * with the node name fully qualified (with namespace) from the argument or the context.
4039 *
4040 * @param[in] args Array of arguments.
4041 * @param[in] arg_count Count of elements in @p args.
4042 * @param[in,out] set Context and result set at the same time.
4043 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004044 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004045 */
4046static LY_ERR
4047xpath_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
4048{
4049 struct lyxp_set_node *item;
4050 struct lys_module *mod;
4051 char *str;
4052 const char *name;
4053 int rc;
4054
4055 if (options & LYXP_SCNODE_ALL) {
4056 set_scnode_clear_ctx(set);
4057 return LY_SUCCESS;
4058 }
4059
4060 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004061 if (args[0]->type != LYXP_SET_NODE_SET) {
4062 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "name(node-set?)");
4063 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004064 } else if (!args[0]->used) {
4065 set_fill_string(set, "", 0);
4066 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004067 }
4068
4069 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004070 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004071
4072 item = &args[0]->val.nodes[0];
4073 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004074 if (set->type != LYXP_SET_NODE_SET) {
4075 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "name(node-set?)");
4076 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004077 } else if (!set->used) {
4078 set_fill_string(set, "", 0);
4079 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004080 }
4081
4082 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004083 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004084
4085 item = &set->val.nodes[0];
4086 }
4087
4088 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004089 case LYXP_NODE_NONE:
4090 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004091 case LYXP_NODE_ROOT:
4092 case LYXP_NODE_ROOT_CONFIG:
4093 case LYXP_NODE_TEXT:
4094 mod = NULL;
4095 name = NULL;
4096 break;
4097 case LYXP_NODE_ELEM:
4098 mod = item->node->schema->module;
4099 name = item->node->schema->name;
4100 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004101 case LYXP_NODE_META:
4102 mod = ((struct lyd_meta *)item->node)->annotation->module;
4103 name = ((struct lyd_meta *)item->node)->name;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004104 break;
4105 }
4106
4107 if (mod && name) {
4108 switch (set->format) {
Michal Vasko52927e22020-03-16 17:26:14 +01004109 case LYD_SCHEMA:
Michal Vasko03ff5a72019-09-11 13:49:33 +02004110 rc = asprintf(&str, "%s:%s", lys_prefix_find_module(set->local_mod, mod), name);
4111 break;
4112 case LYD_JSON:
4113 rc = asprintf(&str, "%s:%s", mod->name, name);
4114 break;
Michal Vasko52927e22020-03-16 17:26:14 +01004115 case LYD_XML:
Michal Vasko9409ef62019-09-12 11:47:17 +02004116 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004117 }
4118 LY_CHECK_ERR_RET(rc == -1, LOGMEM(set->ctx), LY_EMEM);
4119 set_fill_string(set, str, strlen(str));
4120 free(str);
4121 } else {
4122 set_fill_string(set, "", 0);
4123 }
4124
4125 return LY_SUCCESS;
4126}
4127
4128/**
4129 * @brief Execute the XPath namespace-uri(node-set?) function. Returns LYXP_SET_STRING
4130 * with the namespace of the node from the argument or the context.
4131 *
4132 * @param[in] args Array of arguments.
4133 * @param[in] arg_count Count of elements in @p args.
4134 * @param[in,out] set Context and result set at the same time.
4135 * @param[in] options XPath options.
4136 * @return LY_ERR (LY_EINVAL for wrong arguments on schema)
4137 */
4138static LY_ERR
4139xpath_namespace_uri(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
4140{
4141 struct lyxp_set_node *item;
4142 struct lys_module *mod;
4143 /* suppress unused variable warning */
4144 (void)options;
4145
4146 if (options & LYXP_SCNODE_ALL) {
4147 set_scnode_clear_ctx(set);
4148 return LY_SUCCESS;
4149 }
4150
4151 if (arg_count) {
Michal Vaskod3678892020-05-21 10:06:58 +02004152 if (args[0]->type != LYXP_SET_NODE_SET) {
4153 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
4154 "namespace-uri(node-set?)");
4155 return LY_EVALID;
4156 } else if (!args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004157 set_fill_string(set, "", 0);
4158 return LY_SUCCESS;
4159 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004160
4161 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004162 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004163
4164 item = &args[0]->val.nodes[0];
4165 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004166 if (set->type != LYXP_SET_NODE_SET) {
4167 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "namespace-uri(node-set?)");
4168 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004169 } else if (!set->used) {
4170 set_fill_string(set, "", 0);
4171 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004172 }
4173
4174 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004175 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004176
4177 item = &set->val.nodes[0];
4178 }
4179
4180 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004181 case LYXP_NODE_NONE:
4182 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004183 case LYXP_NODE_ROOT:
4184 case LYXP_NODE_ROOT_CONFIG:
4185 case LYXP_NODE_TEXT:
4186 set_fill_string(set, "", 0);
4187 break;
4188 case LYXP_NODE_ELEM:
Michal Vasko9f96a052020-03-10 09:41:45 +01004189 case LYXP_NODE_META:
Michal Vasko03ff5a72019-09-11 13:49:33 +02004190 if (item->type == LYXP_NODE_ELEM) {
4191 mod = item->node->schema->module;
Michal Vasko9f96a052020-03-10 09:41:45 +01004192 } else { /* LYXP_NODE_META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004193 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004194 mod = ((struct lyd_meta *)item->node)->annotation->module;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004195 }
4196
4197 set_fill_string(set, mod->ns, strlen(mod->ns));
4198 break;
4199 }
4200
4201 return LY_SUCCESS;
4202}
4203
4204/**
4205 * @brief Execute the XPath node() function (node type). Returns LYXP_SET_NODE_SET
4206 * with only nodes from the context. In practice it either leaves the context
4207 * as it is or returns an empty node set.
4208 *
4209 * @param[in] args Array of arguments.
4210 * @param[in] arg_count Count of elements in @p args.
4211 * @param[in,out] set Context and result set at the same time.
4212 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004213 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004214 */
4215static LY_ERR
4216xpath_node(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4217{
4218 if (options & LYXP_SCNODE_ALL) {
4219 set_scnode_clear_ctx(set);
4220 return LY_SUCCESS;
4221 }
4222
4223 if (set->type != LYXP_SET_NODE_SET) {
Michal Vaskod3678892020-05-21 10:06:58 +02004224 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004225 }
4226 return LY_SUCCESS;
4227}
4228
4229/**
4230 * @brief Execute the XPath normalize-space(string?) function. Returns LYXP_SET_STRING
4231 * with normalized value (no leading, trailing, double white spaces) of the node
4232 * from the argument or the context.
4233 *
4234 * @param[in] args Array of arguments.
4235 * @param[in] arg_count Count of elements in @p args.
4236 * @param[in,out] set Context and result set at the same time.
4237 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004238 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004239 */
4240static LY_ERR
4241xpath_normalize_space(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
4242{
4243 uint16_t i, new_used;
4244 char *new;
4245 int have_spaces = 0, space_before = 0;
4246 struct lysc_node_leaf *sleaf;
4247 LY_ERR rc = LY_SUCCESS;
4248
4249 if (options & LYXP_SCNODE_ALL) {
4250 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4251 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4252 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 +02004253 } else if (!warn_is_string_type(sleaf->type)) {
4254 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004255 }
4256 }
4257 set_scnode_clear_ctx(set);
4258 return rc;
4259 }
4260
4261 if (arg_count) {
4262 set_fill_set(set, args[0]);
4263 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004264 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004265 LY_CHECK_RET(rc);
4266
4267 /* is there any normalization necessary? */
4268 for (i = 0; set->val.str[i]; ++i) {
4269 if (is_xmlws(set->val.str[i])) {
4270 if ((i == 0) || space_before || (!set->val.str[i + 1])) {
4271 have_spaces = 1;
4272 break;
4273 }
4274 space_before = 1;
4275 } else {
4276 space_before = 0;
4277 }
4278 }
4279
4280 /* yep, there is */
4281 if (have_spaces) {
4282 /* it's enough, at least one character will go, makes space for ending '\0' */
4283 new = malloc(strlen(set->val.str) * sizeof(char));
4284 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4285 new_used = 0;
4286
4287 space_before = 0;
4288 for (i = 0; set->val.str[i]; ++i) {
4289 if (is_xmlws(set->val.str[i])) {
4290 if ((i == 0) || space_before) {
4291 space_before = 1;
4292 continue;
4293 } else {
4294 space_before = 1;
4295 }
4296 } else {
4297 space_before = 0;
4298 }
4299
4300 new[new_used] = (space_before ? ' ' : set->val.str[i]);
4301 ++new_used;
4302 }
4303
4304 /* at worst there is one trailing space now */
4305 if (new_used && is_xmlws(new[new_used - 1])) {
4306 --new_used;
4307 }
4308
4309 new = ly_realloc(new, (new_used + 1) * sizeof(char));
4310 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4311 new[new_used] = '\0';
4312
4313 free(set->val.str);
4314 set->val.str = new;
4315 }
4316
4317 return LY_SUCCESS;
4318}
4319
4320/**
4321 * @brief Execute the XPath not(boolean) function. Returns LYXP_SET_BOOLEAN
4322 * with the argument converted to boolean and logically inverted.
4323 *
4324 * @param[in] args Array of arguments.
4325 * @param[in] arg_count Count of elements in @p args.
4326 * @param[in,out] set Context and result set at the same time.
4327 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004328 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004329 */
4330static LY_ERR
4331xpath_not(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4332{
4333 if (options & LYXP_SCNODE_ALL) {
4334 set_scnode_clear_ctx(set);
4335 return LY_SUCCESS;
4336 }
4337
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004338 lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004339 if (args[0]->val.bool) {
4340 set_fill_boolean(set, 0);
4341 } else {
4342 set_fill_boolean(set, 1);
4343 }
4344
4345 return LY_SUCCESS;
4346}
4347
4348/**
4349 * @brief Execute the XPath number(object?) function. Returns LYXP_SET_NUMBER
4350 * with the number representation of either the argument or the context.
4351 *
4352 * @param[in] args Array of arguments.
4353 * @param[in] arg_count Count of elements in @p args.
4354 * @param[in,out] set Context and result set at the same time.
4355 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004356 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004357 */
4358static LY_ERR
4359xpath_number(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
4360{
4361 LY_ERR rc;
4362
4363 if (options & LYXP_SCNODE_ALL) {
4364 set_scnode_clear_ctx(set);
4365 return LY_SUCCESS;
4366 }
4367
4368 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004369 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004370 LY_CHECK_RET(rc);
4371 set_fill_set(set, args[0]);
4372 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004373 rc = lyxp_set_cast(set, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004374 LY_CHECK_RET(rc);
4375 }
4376
4377 return LY_SUCCESS;
4378}
4379
4380/**
4381 * @brief Execute the XPath position() function. Returns LYXP_SET_NUMBER
4382 * with the context position.
4383 *
4384 * @param[in] args Array of arguments.
4385 * @param[in] arg_count Count of elements in @p args.
4386 * @param[in,out] set Context and result set at the same time.
4387 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004388 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004389 */
4390static LY_ERR
4391xpath_position(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4392{
4393 if (options & LYXP_SCNODE_ALL) {
4394 set_scnode_clear_ctx(set);
4395 return LY_SUCCESS;
4396 }
4397
Michal Vasko03ff5a72019-09-11 13:49:33 +02004398 if (set->type != LYXP_SET_NODE_SET) {
4399 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "position()");
4400 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004401 } else if (!set->used) {
4402 set_fill_number(set, 0);
4403 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004404 }
4405
4406 set_fill_number(set, set->ctx_pos);
4407
4408 /* UNUSED in 'Release' build type */
4409 (void)options;
4410 return LY_SUCCESS;
4411}
4412
4413/**
4414 * @brief Execute the YANG 1.1 re-match(string, string) function. Returns LYXP_SET_BOOLEAN
4415 * depending on whether the second argument regex matches the first argument string. For details refer to
4416 * YANG 1.1 RFC section 10.2.1.
4417 *
4418 * @param[in] args Array of arguments.
4419 * @param[in] arg_count Count of elements in @p args.
4420 * @param[in,out] set Context and result set at the same time.
4421 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004422 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004423 */
4424static LY_ERR
4425xpath_re_match(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4426{
4427 struct lysc_pattern **patterns = NULL, **pattern;
4428 struct lysc_node_leaf *sleaf;
4429 char *path;
4430 LY_ERR rc = LY_SUCCESS;
4431 struct ly_err_item *err;
4432
4433 if (options & LYXP_SCNODE_ALL) {
4434 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4435 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4436 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 +02004437 } else if (!warn_is_string_type(sleaf->type)) {
4438 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004439 }
4440 }
4441
4442 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4443 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4444 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 +02004445 } else if (!warn_is_string_type(sleaf->type)) {
4446 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004447 }
4448 }
4449 set_scnode_clear_ctx(set);
4450 return rc;
4451 }
4452
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004453 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004454 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004455 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004456 LY_CHECK_RET(rc);
4457
4458 LY_ARRAY_NEW_RET(set->ctx, patterns, pattern, LY_EMEM);
4459 *pattern = malloc(sizeof **pattern);
4460 path = lyd_path(set->ctx_node, LYD_PATH_LOG, NULL, 0);
4461 rc = lys_compile_type_pattern_check(set->ctx, path, args[1]->val.str, &(*pattern)->code);
4462 free(path);
4463 if (rc != LY_SUCCESS) {
4464 LY_ARRAY_FREE(patterns);
4465 return rc;
4466 }
4467
4468 rc = ly_type_validate_patterns(patterns, args[0]->val.str, strlen(args[0]->val.str), &err);
4469 pcre2_code_free((*pattern)->code);
4470 free(*pattern);
4471 LY_ARRAY_FREE(patterns);
4472 if (rc && (rc != LY_EVALID)) {
4473 ly_err_print(err);
4474 ly_err_free(err);
4475 return rc;
4476 }
4477
4478 if (rc == LY_EVALID) {
4479 ly_err_free(err);
4480 set_fill_boolean(set, 0);
4481 } else {
4482 set_fill_boolean(set, 1);
4483 }
4484
4485 return LY_SUCCESS;
4486}
4487
4488/**
4489 * @brief Execute the XPath round(number) function. Returns LYXP_SET_NUMBER
4490 * with the rounded first argument. For details refer to
4491 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-round.
4492 *
4493 * @param[in] args Array of arguments.
4494 * @param[in] arg_count Count of elements in @p args.
4495 * @param[in,out] set Context and result set at the same time.
4496 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004497 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004498 */
4499static LY_ERR
4500xpath_round(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4501{
4502 struct lysc_node_leaf *sleaf;
4503 LY_ERR rc = LY_SUCCESS;
4504
4505 if (options & LYXP_SCNODE_ALL) {
4506 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4507 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004508 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4509 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 +02004510 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
4511 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004512 }
4513 set_scnode_clear_ctx(set);
4514 return rc;
4515 }
4516
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004517 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004518 LY_CHECK_RET(rc);
4519
4520 /* cover only the cases where floor can't be used */
4521 if ((args[0]->val.num == -0.0f) || ((args[0]->val.num < 0) && (args[0]->val.num >= -0.5))) {
4522 set_fill_number(set, -0.0f);
4523 } else {
4524 args[0]->val.num += 0.5;
4525 rc = xpath_floor(args, 1, args[0], options);
4526 LY_CHECK_RET(rc);
4527 set_fill_number(set, args[0]->val.num);
4528 }
4529
4530 return LY_SUCCESS;
4531}
4532
4533/**
4534 * @brief Execute the XPath starts-with(string, string) function.
4535 * Returns LYXP_SET_BOOLEAN whether the second argument is
4536 * the prefix of the first or not.
4537 *
4538 * @param[in] args Array of arguments.
4539 * @param[in] arg_count Count of elements in @p args.
4540 * @param[in,out] set Context and result set at the same time.
4541 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004542 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004543 */
4544static LY_ERR
4545xpath_starts_with(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4546{
4547 struct lysc_node_leaf *sleaf;
4548 LY_ERR rc = LY_SUCCESS;
4549
4550 if (options & LYXP_SCNODE_ALL) {
4551 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4552 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4553 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 +02004554 } else if (!warn_is_string_type(sleaf->type)) {
4555 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004556 }
4557 }
4558
4559 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4560 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4561 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 +02004562 } else if (!warn_is_string_type(sleaf->type)) {
4563 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004564 }
4565 }
4566 set_scnode_clear_ctx(set);
4567 return rc;
4568 }
4569
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004570 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004571 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004572 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004573 LY_CHECK_RET(rc);
4574
4575 if (strncmp(args[0]->val.str, args[1]->val.str, strlen(args[1]->val.str))) {
4576 set_fill_boolean(set, 0);
4577 } else {
4578 set_fill_boolean(set, 1);
4579 }
4580
4581 return LY_SUCCESS;
4582}
4583
4584/**
4585 * @brief Execute the XPath string(object?) function. Returns LYXP_SET_STRING
4586 * with the string representation of either the argument or the context.
4587 *
4588 * @param[in] args Array of arguments.
4589 * @param[in] arg_count Count of elements in @p args.
4590 * @param[in,out] set Context and result set at the same time.
4591 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004592 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004593 */
4594static LY_ERR
4595xpath_string(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
4596{
4597 LY_ERR rc;
4598
4599 if (options & LYXP_SCNODE_ALL) {
4600 set_scnode_clear_ctx(set);
4601 return LY_SUCCESS;
4602 }
4603
4604 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004605 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004606 LY_CHECK_RET(rc);
4607 set_fill_set(set, args[0]);
4608 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004609 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004610 LY_CHECK_RET(rc);
4611 }
4612
4613 return LY_SUCCESS;
4614}
4615
4616/**
4617 * @brief Execute the XPath string-length(string?) function. Returns LYXP_SET_NUMBER
4618 * with the length of the string in either the argument or the context.
4619 *
4620 * @param[in] args Array of arguments.
4621 * @param[in] arg_count Count of elements in @p args.
4622 * @param[in,out] set Context and result set at the same time.
4623 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004624 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004625 */
4626static LY_ERR
4627xpath_string_length(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
4628{
4629 struct lysc_node_leaf *sleaf;
4630 LY_ERR rc = LY_SUCCESS;
4631
4632 if (options & LYXP_SCNODE_ALL) {
4633 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4634 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4635 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 +02004636 } else if (!warn_is_string_type(sleaf->type)) {
4637 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004638 }
4639 }
4640 if (!arg_count && (set->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set))) {
4641 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4642 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 +02004643 } else if (!warn_is_string_type(sleaf->type)) {
4644 LOGWRN(set->ctx, "Argument #0 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004645 }
4646 }
4647 set_scnode_clear_ctx(set);
4648 return rc;
4649 }
4650
4651 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004652 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004653 LY_CHECK_RET(rc);
4654 set_fill_number(set, strlen(args[0]->val.str));
4655 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004656 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004657 LY_CHECK_RET(rc);
4658 set_fill_number(set, strlen(set->val.str));
4659 }
4660
4661 return LY_SUCCESS;
4662}
4663
4664/**
4665 * @brief Execute the XPath substring(string, number, number?) function.
4666 * Returns LYXP_SET_STRING substring of the first argument starting
4667 * on the second argument index ending on the third argument index,
4668 * indexed from 1. For exact definition refer to
4669 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-substring.
4670 *
4671 * @param[in] args Array of arguments.
4672 * @param[in] arg_count Count of elements in @p args.
4673 * @param[in,out] set Context and result set at the same time.
4674 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004675 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004676 */
4677static LY_ERR
4678xpath_substring(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
4679{
4680 int start, len;
4681 uint16_t str_start, str_len, pos;
4682 struct lysc_node_leaf *sleaf;
4683 LY_ERR rc = LY_SUCCESS;
4684
4685 if (options & LYXP_SCNODE_ALL) {
4686 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4687 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4688 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 +02004689 } else if (!warn_is_string_type(sleaf->type)) {
4690 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004691 }
4692 }
4693
4694 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4695 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4696 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 +02004697 } else if (!warn_is_numeric_type(sleaf->type)) {
4698 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004699 }
4700 }
4701
4702 if ((arg_count == 3) && (args[2]->type == LYXP_SET_SCNODE_SET)
4703 && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
4704 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4705 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 +02004706 } else if (!warn_is_numeric_type(sleaf->type)) {
4707 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004708 }
4709 }
4710 set_scnode_clear_ctx(set);
4711 return rc;
4712 }
4713
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004714 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004715 LY_CHECK_RET(rc);
4716
4717 /* start */
4718 if (xpath_round(&args[1], 1, args[1], options)) {
4719 return -1;
4720 }
4721 if (isfinite(args[1]->val.num)) {
4722 start = args[1]->val.num - 1;
4723 } else if (isinf(args[1]->val.num) && signbit(args[1]->val.num)) {
4724 start = INT_MIN;
4725 } else {
4726 start = INT_MAX;
4727 }
4728
4729 /* len */
4730 if (arg_count == 3) {
4731 rc = xpath_round(&args[2], 1, args[2], options);
4732 LY_CHECK_RET(rc);
4733 if (isfinite(args[2]->val.num)) {
4734 len = args[2]->val.num;
4735 } else if (isnan(args[2]->val.num) || signbit(args[2]->val.num)) {
4736 len = 0;
4737 } else {
4738 len = INT_MAX;
4739 }
4740 } else {
4741 len = INT_MAX;
4742 }
4743
4744 /* find matching character positions */
4745 str_start = 0;
4746 str_len = 0;
4747 for (pos = 0; args[0]->val.str[pos]; ++pos) {
4748 if (pos < start) {
4749 ++str_start;
4750 } else if (pos < start + len) {
4751 ++str_len;
4752 } else {
4753 break;
4754 }
4755 }
4756
4757 set_fill_string(set, args[0]->val.str + str_start, str_len);
4758 return LY_SUCCESS;
4759}
4760
4761/**
4762 * @brief Execute the XPath substring-after(string, string) function.
4763 * Returns LYXP_SET_STRING with the string succeeding the occurance
4764 * of the second argument in the first or an empty string.
4765 *
4766 * @param[in] args Array of arguments.
4767 * @param[in] arg_count Count of elements in @p args.
4768 * @param[in,out] set Context and result set at the same time.
4769 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004770 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004771 */
4772static LY_ERR
4773xpath_substring_after(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4774{
4775 char *ptr;
4776 struct lysc_node_leaf *sleaf;
4777 LY_ERR rc = LY_SUCCESS;
4778
4779 if (options & LYXP_SCNODE_ALL) {
4780 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4781 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4782 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 +02004783 } else if (!warn_is_string_type(sleaf->type)) {
4784 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004785 }
4786 }
4787
4788 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4789 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4790 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 +02004791 } else if (!warn_is_string_type(sleaf->type)) {
4792 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004793 }
4794 }
4795 set_scnode_clear_ctx(set);
4796 return rc;
4797 }
4798
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004799 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004800 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004801 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004802 LY_CHECK_RET(rc);
4803
4804 ptr = strstr(args[0]->val.str, args[1]->val.str);
4805 if (ptr) {
4806 set_fill_string(set, ptr + strlen(args[1]->val.str), strlen(ptr + strlen(args[1]->val.str)));
4807 } else {
4808 set_fill_string(set, "", 0);
4809 }
4810
4811 return LY_SUCCESS;
4812}
4813
4814/**
4815 * @brief Execute the XPath substring-before(string, string) function.
4816 * Returns LYXP_SET_STRING with the string preceding the occurance
4817 * of the second argument in the first or an empty string.
4818 *
4819 * @param[in] args Array of arguments.
4820 * @param[in] arg_count Count of elements in @p args.
4821 * @param[in,out] set Context and result set at the same time.
4822 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004823 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004824 */
4825static LY_ERR
4826xpath_substring_before(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4827{
4828 char *ptr;
4829 struct lysc_node_leaf *sleaf;
4830 LY_ERR rc = LY_SUCCESS;
4831
4832 if (options & LYXP_SCNODE_ALL) {
4833 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4834 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4835 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 +02004836 } else if (!warn_is_string_type(sleaf->type)) {
4837 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004838 }
4839 }
4840
4841 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4842 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4843 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 +02004844 } else if (!warn_is_string_type(sleaf->type)) {
4845 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004846 }
4847 }
4848 set_scnode_clear_ctx(set);
4849 return rc;
4850 }
4851
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004852 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004853 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004854 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004855 LY_CHECK_RET(rc);
4856
4857 ptr = strstr(args[0]->val.str, args[1]->val.str);
4858 if (ptr) {
4859 set_fill_string(set, args[0]->val.str, ptr - args[0]->val.str);
4860 } else {
4861 set_fill_string(set, "", 0);
4862 }
4863
4864 return LY_SUCCESS;
4865}
4866
4867/**
4868 * @brief Execute the XPath sum(node-set) function. Returns LYXP_SET_NUMBER
4869 * with the sum of all the nodes in the context.
4870 *
4871 * @param[in] args Array of arguments.
4872 * @param[in] arg_count Count of elements in @p args.
4873 * @param[in,out] set Context and result set at the same time.
4874 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004875 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004876 */
4877static LY_ERR
4878xpath_sum(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4879{
4880 long double num;
4881 char *str;
4882 uint16_t i;
4883 struct lyxp_set set_item;
4884 struct lysc_node_leaf *sleaf;
4885 LY_ERR rc = LY_SUCCESS;
4886
4887 if (options & LYXP_SCNODE_ALL) {
4888 if (args[0]->type == LYXP_SET_SCNODE_SET) {
4889 for (i = 0; i < args[0]->used; ++i) {
4890 if (args[0]->val.scnodes[i].in_ctx == 1) {
4891 sleaf = (struct lysc_node_leaf *)args[0]->val.scnodes[i].scnode;
4892 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4893 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__,
4894 lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004895 } else if (!warn_is_numeric_type(sleaf->type)) {
4896 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004897 }
4898 }
4899 }
4900 }
4901 set_scnode_clear_ctx(set);
4902 return rc;
4903 }
4904
4905 set_fill_number(set, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004906
4907 if (args[0]->type != LYXP_SET_NODE_SET) {
4908 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "sum(node-set)");
4909 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004910 } else if (!args[0]->used) {
4911 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004912 }
4913
Michal Vasko5c4e5892019-11-14 12:31:38 +01004914 set_init(&set_item, set);
4915
Michal Vasko03ff5a72019-09-11 13:49:33 +02004916 set_item.type = LYXP_SET_NODE_SET;
4917 set_item.val.nodes = malloc(sizeof *set_item.val.nodes);
4918 LY_CHECK_ERR_RET(!set_item.val.nodes, LOGMEM(set->ctx), LY_EMEM);
4919
4920 set_item.used = 1;
4921 set_item.size = 1;
4922
4923 for (i = 0; i < args[0]->used; ++i) {
4924 set_item.val.nodes[0] = args[0]->val.nodes[i];
4925
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004926 rc = cast_node_set_to_string(&set_item, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004927 LY_CHECK_RET(rc);
4928 num = cast_string_to_number(str);
4929 free(str);
4930 set->val.num += num;
4931 }
4932
4933 free(set_item.val.nodes);
4934
4935 return LY_SUCCESS;
4936}
4937
4938/**
4939 * @brief Execute the XPath text() function (node type). Returns LYXP_SET_NODE_SET
4940 * with the text content of the nodes in the context.
4941 *
4942 * @param[in] args Array of arguments.
4943 * @param[in] arg_count Count of elements in @p args.
4944 * @param[in,out] set Context and result set at the same time.
4945 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004946 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004947 */
4948static LY_ERR
4949xpath_text(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4950{
4951 uint32_t i;
4952
4953 if (options & LYXP_SCNODE_ALL) {
4954 set_scnode_clear_ctx(set);
4955 return LY_SUCCESS;
4956 }
4957
Michal Vasko03ff5a72019-09-11 13:49:33 +02004958 if (set->type != LYXP_SET_NODE_SET) {
4959 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "text()");
4960 return LY_EVALID;
4961 }
4962
4963 for (i = 0; i < set->used;) {
4964 switch (set->val.nodes[i].type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004965 case LYXP_NODE_NONE:
4966 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004967 case LYXP_NODE_ELEM:
Michal Vasko03ff5a72019-09-11 13:49:33 +02004968 if (set->val.nodes[i].node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
4969 set->val.nodes[i].type = LYXP_NODE_TEXT;
4970 ++i;
4971 break;
4972 }
4973 /* fall through */
4974 case LYXP_NODE_ROOT:
4975 case LYXP_NODE_ROOT_CONFIG:
4976 case LYXP_NODE_TEXT:
Michal Vasko9f96a052020-03-10 09:41:45 +01004977 case LYXP_NODE_META:
Michal Vasko03ff5a72019-09-11 13:49:33 +02004978 set_remove_node(set, i);
4979 break;
4980 }
4981 }
4982
4983 return LY_SUCCESS;
4984}
4985
4986/**
4987 * @brief Execute the XPath translate(string, string, string) function.
4988 * Returns LYXP_SET_STRING with the first argument with the characters
4989 * from the second argument replaced by those on the corresponding
4990 * positions in the third argument.
4991 *
4992 * @param[in] args Array of arguments.
4993 * @param[in] arg_count Count of elements in @p args.
4994 * @param[in,out] set Context and result set at the same time.
4995 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004996 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004997 */
4998static LY_ERR
4999xpath_translate(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
5000{
5001 uint16_t i, j, new_used;
5002 char *new;
5003 int found, have_removed;
5004 struct lysc_node_leaf *sleaf;
5005 LY_ERR rc = LY_SUCCESS;
5006
5007 if (options & LYXP_SCNODE_ALL) {
5008 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5009 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5010 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 +02005011 } else if (!warn_is_string_type(sleaf->type)) {
5012 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005013 }
5014 }
5015
5016 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5017 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5018 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 +02005019 } else if (!warn_is_string_type(sleaf->type)) {
5020 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005021 }
5022 }
5023
5024 if ((args[2]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
5025 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5026 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 +02005027 } else if (!warn_is_string_type(sleaf->type)) {
5028 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005029 }
5030 }
5031 set_scnode_clear_ctx(set);
5032 return rc;
5033 }
5034
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005035 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005036 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005037 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005038 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005039 rc = lyxp_set_cast(args[2], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005040 LY_CHECK_RET(rc);
5041
5042 new = malloc((strlen(args[0]->val.str) + 1) * sizeof(char));
5043 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5044 new_used = 0;
5045
5046 have_removed = 0;
5047 for (i = 0; args[0]->val.str[i]; ++i) {
5048 found = 0;
5049
5050 for (j = 0; args[1]->val.str[j]; ++j) {
5051 if (args[0]->val.str[i] == args[1]->val.str[j]) {
5052 /* removing this char */
5053 if (j >= strlen(args[2]->val.str)) {
5054 have_removed = 1;
5055 found = 1;
5056 break;
5057 }
5058 /* replacing this char */
5059 new[new_used] = args[2]->val.str[j];
5060 ++new_used;
5061 found = 1;
5062 break;
5063 }
5064 }
5065
5066 /* copying this char */
5067 if (!found) {
5068 new[new_used] = args[0]->val.str[i];
5069 ++new_used;
5070 }
5071 }
5072
5073 if (have_removed) {
5074 new = ly_realloc(new, (new_used + 1) * sizeof(char));
5075 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5076 }
5077 new[new_used] = '\0';
5078
Michal Vaskod3678892020-05-21 10:06:58 +02005079 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005080 set->type = LYXP_SET_STRING;
5081 set->val.str = new;
5082
5083 return LY_SUCCESS;
5084}
5085
5086/**
5087 * @brief Execute the XPath true() function. Returns LYXP_SET_BOOLEAN
5088 * with true value.
5089 *
5090 * @param[in] args Array of arguments.
5091 * @param[in] arg_count Count of elements in @p args.
5092 * @param[in,out] set Context and result set at the same time.
5093 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005094 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005095 */
5096static LY_ERR
5097xpath_true(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
5098{
5099 if (options & LYXP_SCNODE_ALL) {
5100 set_scnode_clear_ctx(set);
5101 return LY_SUCCESS;
5102 }
5103
5104 set_fill_boolean(set, 1);
5105 return LY_SUCCESS;
5106}
5107
5108/*
5109 * moveto functions
5110 *
5111 * They and only they actually change the context (set).
5112 */
5113
5114/**
Michal Vasko6346ece2019-09-24 13:12:53 +02005115 * @brief Skip prefix and return corresponding model if there is a prefix. Logs directly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005116 *
Michal Vasko2104e9f2020-03-06 08:23:25 +01005117 * XPath @p set is expected to be a (sc)node set!
5118 *
Michal Vasko6346ece2019-09-24 13:12:53 +02005119 * @param[in,out] qname Qualified node name. If includes prefix, it is skipped.
5120 * @param[in,out] qname_len Length of @p qname, is updated accordingly.
5121 * @param[in] set Set with XPath context.
5122 * @param[out] moveto_mod Expected module of a matching node.
5123 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005124 */
Michal Vasko6346ece2019-09-24 13:12:53 +02005125static LY_ERR
5126moveto_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 +02005127{
Michal Vasko6346ece2019-09-24 13:12:53 +02005128 const struct lys_module *mod;
5129 const char *ptr;
5130 int pref_len;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005131 char *str;
5132
Michal Vasko2104e9f2020-03-06 08:23:25 +01005133 assert((set->type == LYXP_SET_NODE_SET) || (set->type == LYXP_SET_SCNODE_SET));
5134
Michal Vasko6346ece2019-09-24 13:12:53 +02005135 if ((ptr = ly_strnchr(*qname, ':', *qname_len))) {
5136 /* specific module */
5137 pref_len = ptr - *qname;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005138
Michal Vasko6346ece2019-09-24 13:12:53 +02005139 switch (set->format) {
Michal Vasko52927e22020-03-16 17:26:14 +01005140 case LYD_SCHEMA:
Michal Vasko6346ece2019-09-24 13:12:53 +02005141 /* schema, search all local module imports */
5142 mod = lys_module_find_prefix(set->local_mod, *qname, pref_len);
5143 break;
5144 case LYD_JSON:
5145 /* JSON data, search in context */
5146 str = strndup(*qname, pref_len);
5147 mod = ly_ctx_get_module(set->ctx, str, NULL);
5148 free(str);
5149 break;
Michal Vasko52927e22020-03-16 17:26:14 +01005150 case LYD_XML:
Michal Vasko6346ece2019-09-24 13:12:53 +02005151 LOGINT_RET(set->ctx);
5152 }
5153
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005154 /* Check for errors and non-implemented modules, as they are not valid */
5155 if (!mod || !mod->implemented) {
Michal Vasko2104e9f2020-03-06 08:23:25 +01005156 if (set->type == LYXP_SET_SCNODE_SET) {
5157 LOGVAL(set->ctx, LY_VLOG_LYSC, set->ctx_scnode, LY_VCODE_XP_INMOD, pref_len, *qname);
5158 } else {
5159 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INMOD, pref_len, *qname);
5160 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005161 return LY_EVALID;
5162 }
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005163
Michal Vasko6346ece2019-09-24 13:12:53 +02005164 *qname += pref_len + 1;
5165 *qname_len -= pref_len + 1;
5166 } else if (((*qname)[0] == '*') && (*qname_len == 1)) {
5167 /* all modules - special case */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005168 mod = NULL;
Michal Vasko6346ece2019-09-24 13:12:53 +02005169 } else {
5170 /* local module */
5171 mod = set->local_mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005172 }
5173
Michal Vasko6346ece2019-09-24 13:12:53 +02005174 *moveto_mod = mod;
5175 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005176}
5177
5178/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02005179 * @brief Move context @p set to the root. Handles absolute path.
5180 * Result is LYXP_SET_NODE_SET.
5181 *
5182 * @param[in,out] set Set to use.
5183 * @param[in] options Xpath options.
5184 */
5185static void
5186moveto_root(struct lyxp_set *set, int options)
5187{
Michal Vasko03ff5a72019-09-11 13:49:33 +02005188 if (!set) {
5189 return;
5190 }
5191
5192 if (options & LYXP_SCNODE_ALL) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005193 set_scnode_clear_ctx(set);
Michal Vaskoecd62de2019-11-13 12:35:11 +01005194 lyxp_set_scnode_insert_node(set, NULL, set->root_type);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005195 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005196 set->type = LYXP_SET_NODE_SET;
5197 set->used = 0;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005198 set_insert_node(set, NULL, 0, set->root_type, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005199 }
5200}
5201
5202/**
Michal Vaskoa1424542019-11-14 16:08:52 +01005203 * @brief Check whether a node has some unresolved "when".
5204 *
5205 * @param[in] node Node to check.
5206 * @return LY_ERR value (LY_EINCOMPLETE if there are some unresolved "when")
5207 */
5208static LY_ERR
5209moveto_when_check(const struct lyd_node *node)
5210{
5211 const struct lysc_node *schema;
5212
5213 if (!node) {
5214 return LY_SUCCESS;
5215 }
5216
5217 schema = node->schema;
5218 do {
5219 if (schema->when && !(node->flags & LYD_WHEN_TRUE)) {
5220 return LY_EINCOMPLETE;
5221 }
5222 schema = schema->parent;
5223 } while (schema && (schema->nodetype & (LYS_CASE | LYS_CHOICE)));
5224
5225 return LY_SUCCESS;
5226}
5227
5228/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02005229 * @brief Check @p node as a part of NameTest processing.
5230 *
5231 * @param[in] node Node to check.
5232 * @param[in] root_type XPath root node type.
Michal Vaskod3678892020-05-21 10:06:58 +02005233 * @param[in] node_name Node name in the dictionary to move to, NULL for any node.
5234 * @param[in] moveto_mod Expected module of the node, NULL for any.
Michal Vasko6346ece2019-09-24 13:12:53 +02005235 * @return LY_ERR (LY_ENOT if node does not match, LY_EINCOMPLETE on unresolved when,
5236 * LY_EINVAL if netither node nor any children match)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005237 */
5238static LY_ERR
5239moveto_node_check(const struct lyd_node *node, enum lyxp_node_type root_type, const char *node_name,
5240 const struct lys_module *moveto_mod)
5241{
5242 /* module check */
5243 if (moveto_mod && (node->schema->module != moveto_mod)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005244 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005245 }
5246
Michal Vasko5c4e5892019-11-14 12:31:38 +01005247 /* context check */
5248 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005249 return LY_EINVAL;
5250 }
5251
5252 /* name check */
Michal Vasko61ac2f62020-05-25 12:39:51 +02005253 if (node_name && strcmp(node_name, "*") && (node->schema->name != node_name)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005254 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005255 }
5256
Michal Vaskoa1424542019-11-14 16:08:52 +01005257 /* when check */
5258 if (moveto_when_check(node)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005259 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01005260 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005261
5262 /* match */
5263 return LY_SUCCESS;
5264}
5265
5266/**
5267 * @brief Check @p node as a part of schema NameTest processing.
5268 *
5269 * @param[in] node Schema node to check.
5270 * @param[in] root_type XPath root node type.
Michal Vaskod3678892020-05-21 10:06:58 +02005271 * @param[in] node_name Node name in the dictionary to move to, NULL for any nodes.
5272 * @param[in] moveto_mod Expected module of the node, NULL for any.
Michal Vasko6346ece2019-09-24 13:12:53 +02005273 * @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 +02005274 */
5275static LY_ERR
5276moveto_scnode_check(const struct lysc_node *node, enum lyxp_node_type root_type, const char *node_name,
Michal Vaskocafad9d2019-11-07 15:20:03 +01005277 const struct lys_module *moveto_mod)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005278{
Michal Vasko03ff5a72019-09-11 13:49:33 +02005279 /* module check */
Michal Vaskod3678892020-05-21 10:06:58 +02005280 if (moveto_mod && (node->module != moveto_mod)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005281 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005282 }
5283
5284 /* context check */
5285 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (node->flags & LYS_CONFIG_R)) {
5286 return LY_EINVAL;
5287 }
5288
5289 /* name check */
Michal Vasko61ac2f62020-05-25 12:39:51 +02005290 if (node_name && strcmp(node_name, "*") && (node->name != node_name)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005291 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005292 }
5293
5294 /* match */
5295 return LY_SUCCESS;
5296}
5297
5298/**
Michal Vaskod3678892020-05-21 10:06:58 +02005299 * @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 +02005300 *
5301 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005302 * @param[in] mod Matching node module, NULL for any.
5303 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005304 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5305 */
5306static LY_ERR
Michal Vaskod3678892020-05-21 10:06:58 +02005307moveto_node(struct lyxp_set *set, const struct lys_module *mod, const char *ncname)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005308{
Michal Vaskof03ed032020-03-04 13:31:44 +01005309 uint32_t i;
Michal Vasko6346ece2019-09-24 13:12:53 +02005310 int replaced;
Michal Vaskod3678892020-05-21 10:06:58 +02005311 const struct lyd_node *siblings, *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005312 LY_ERR rc;
5313
Michal Vaskod3678892020-05-21 10:06:58 +02005314 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005315 return LY_SUCCESS;
5316 }
5317
5318 if (set->type != LYXP_SET_NODE_SET) {
5319 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
5320 return LY_EVALID;
5321 }
5322
Michal Vasko03ff5a72019-09-11 13:49:33 +02005323 for (i = 0; i < set->used; ) {
5324 replaced = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02005325 siblings = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005326
5327 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 +01005328 assert(!set->val.nodes[i].node);
Michal Vaskod3678892020-05-21 10:06:58 +02005329
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005330 /* search in all the trees */
Michal Vaskod3678892020-05-21 10:06:58 +02005331 siblings = set->tree;
Michal Vasko5c4e5892019-11-14 12:31:38 +01005332 } else if (!(set->val.nodes[i].node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA))) {
Michal Vaskod3678892020-05-21 10:06:58 +02005333 /* search in children */
5334 siblings = lyd_node_children(set->val.nodes[i].node);
5335 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005336
Michal Vaskod3678892020-05-21 10:06:58 +02005337 for (sub = siblings; sub; sub = sub->next) {
5338 rc = moveto_node_check(sub, set->root_type, ncname, mod);
5339 if (rc == LY_SUCCESS) {
5340 if (!replaced) {
5341 set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i);
5342 replaced = 1;
5343 } else {
5344 set_insert_node(set, sub, 0, LYXP_NODE_ELEM, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005345 }
Michal Vaskod3678892020-05-21 10:06:58 +02005346 ++i;
5347 } else if (rc == LY_EINCOMPLETE) {
5348 return rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005349 }
5350 }
5351
5352 if (!replaced) {
5353 /* no match */
5354 set_remove_node(set, i);
5355 }
5356 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005357
5358 return LY_SUCCESS;
5359}
5360
5361/**
Michal Vaskod3678892020-05-21 10:06:58 +02005362 * @brief Move context @p set to a node using hashes. Result is LYXP_SET_NODE_SET (or LYXP_SET_EMPTY).
5363 * Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005364 *
5365 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005366 * @param[in] scnode Matching node schema.
5367 * @param[in] list_inst If @p scnode is ::LYS_LIST, the matching list instance. Ignored otherwise.
5368 * @param[in] llist_val If @p scnode is ::LYS_LEAFLIST, the matching leaf-list value. Ignored otherwise.
5369 * @param[in] llist_val_len Length of @p llist_val.
5370 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5371 */
5372static LY_ERR
5373moveto_node_hash(struct lyxp_set *set, const struct lysc_node *scnode, const struct lyd_node *list_inst,
5374 const char *llist_val, uint16_t llist_val_len)
5375{
5376 uint32_t i;
5377 int replaced;
5378 const struct lyd_node *siblings;
5379 struct lyd_node *sub;
5380
5381 assert(scnode && ((scnode->nodetype != LYS_LIST) || list_inst) && ((scnode->nodetype != LYS_LEAFLIST) || llist_val));
5382
5383 if (!set) {
5384 return LY_SUCCESS;
5385 }
5386
5387 if (set->type != LYXP_SET_NODE_SET) {
5388 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
5389 return LY_EVALID;
5390 }
5391
5392 /* context check for all the nodes since we have the schema node */
5393 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) {
5394 lyxp_set_free_content(set);
5395 return LY_SUCCESS;
5396 }
5397
5398 for (i = 0; i < set->used; ) {
5399 replaced = 0;
5400 siblings = NULL;
5401
5402 if ((set->val.nodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.nodes[i].type == LYXP_NODE_ROOT)) {
5403 assert(!set->val.nodes[i].node);
5404
5405 /* search in all the trees */
5406 siblings = set->tree;
5407 } else if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
5408 /* search in children */
5409 siblings = lyd_node_children(set->val.nodes[i].node);
5410 }
5411
5412 /* find the node using hashes */
5413 if (scnode->nodetype == LYS_LIST) {
5414 lyd_find_sibling_first(siblings, list_inst, &sub);
5415 } else {
5416 lyd_find_sibling_val(siblings, scnode, llist_val, llist_val_len, &sub);
5417 }
5418
5419 /* when check */
5420 if (sub && moveto_when_check(sub)) {
5421 return LY_EINCOMPLETE;
5422 }
5423
5424 if (sub) {
5425 /* pos filled later */
5426 if (!replaced) {
5427 set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i);
5428 replaced = 1;
5429 } else {
5430 set_insert_node(set, sub, 0, LYXP_NODE_ELEM, i);
5431 }
5432 ++i;
5433 }
5434
5435 if (!replaced) {
5436 /* no match */
5437 set_remove_node(set, i);
5438 }
5439 }
5440
5441 return LY_SUCCESS;
5442}
5443
5444/**
5445 * @brief Move context @p set to a schema node. Result is LYXP_SET_SCNODE_SET (or LYXP_SET_EMPTY).
5446 *
5447 * @param[in,out] set Set to use.
5448 * @param[in] mod Matching node module, NULL for any.
5449 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005450 * @param[in] options XPath options.
5451 * @return LY_ERR
5452 */
5453static LY_ERR
Michal Vaskod3678892020-05-21 10:06:58 +02005454moveto_scnode(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, int options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005455{
Michal Vaskocafad9d2019-11-07 15:20:03 +01005456 int i, orig_used, idx, temp_ctx = 0, getnext_opts;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005457 uint32_t mod_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005458 const struct lysc_node *sub, *start_parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005459
Michal Vaskod3678892020-05-21 10:06:58 +02005460 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005461 return LY_SUCCESS;
5462 }
5463
5464 if (set->type != LYXP_SET_SCNODE_SET) {
Michal Vaskof6e51882019-12-16 09:59:45 +01005465 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 +02005466 return LY_EVALID;
5467 }
5468
Michal Vaskocafad9d2019-11-07 15:20:03 +01005469 /* getnext opts */
5470 getnext_opts = LYS_GETNEXT_NOSTATECHECK;
5471 if (options & LYXP_SCNODE_OUTPUT) {
5472 getnext_opts |= LYS_GETNEXT_OUTPUT;
5473 }
5474
Michal Vasko03ff5a72019-09-11 13:49:33 +02005475 orig_used = set->used;
5476 for (i = 0; i < orig_used; ++i) {
5477 if (set->val.scnodes[i].in_ctx != 1) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01005478 if (set->val.scnodes[i].in_ctx != -2) {
5479 continue;
5480 }
5481
5482 /* remember context node */
5483 set->val.scnodes[i].in_ctx = -1;
Michal Vaskoec4df482019-12-16 10:02:18 +01005484 } else {
5485 set->val.scnodes[i].in_ctx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005486 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005487
5488 start_parent = set->val.scnodes[i].scnode;
5489
5490 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 +02005491 /* 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 +02005492 * so use it directly (root node itself is useless in this case) */
5493 mod_idx = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02005494 while (mod || (mod = (struct lys_module *)ly_ctx_get_module_iter(set->ctx, &mod_idx))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005495 sub = NULL;
Michal Vasko509de4d2019-12-10 14:51:30 +01005496 /* module may not be implemented */
Michal Vaskod3678892020-05-21 10:06:58 +02005497 while (mod->implemented && (sub = lys_getnext(sub, NULL, mod->compiled, getnext_opts))) {
5498 if (!moveto_scnode_check(sub, set->root_type, ncname, mod)) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01005499 idx = lyxp_set_scnode_insert_node(set, sub, LYXP_NODE_ELEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005500 /* we need to prevent these nodes from being considered in this moveto */
5501 if ((idx < orig_used) && (idx > i)) {
5502 set->val.scnodes[idx].in_ctx = 2;
5503 temp_ctx = 1;
5504 }
5505 }
5506 }
5507
5508 if (!mod_idx) {
Michal Vaskod3678892020-05-21 10:06:58 +02005509 /* mod was specified, we are not going through the whole context */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005510 break;
5511 }
5512 /* next iteration */
Michal Vaskod3678892020-05-21 10:06:58 +02005513 mod = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005514 }
5515
5516 /* skip nodes without children - leaves, leaflists, and anyxmls (ouput root will eval to true) */
5517 } else if (!(start_parent->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA))) {
5518 sub = NULL;
Michal Vaskocafad9d2019-11-07 15:20:03 +01005519 while ((sub = lys_getnext(sub, start_parent, NULL, getnext_opts))) {
Michal Vaskod3678892020-05-21 10:06:58 +02005520 if (!moveto_scnode_check(sub, set->root_type, ncname, (mod ? mod : set->local_mod))) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01005521 idx = lyxp_set_scnode_insert_node(set, sub, LYXP_NODE_ELEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005522 if ((idx < orig_used) && (idx > i)) {
5523 set->val.scnodes[idx].in_ctx = 2;
5524 temp_ctx = 1;
5525 }
5526 }
5527 }
5528 }
5529 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005530
5531 /* correct temporary in_ctx values */
5532 if (temp_ctx) {
5533 for (i = 0; i < orig_used; ++i) {
5534 if (set->val.scnodes[i].in_ctx == 2) {
5535 set->val.scnodes[i].in_ctx = 1;
5536 }
5537 }
5538 }
5539
5540 return LY_SUCCESS;
5541}
5542
5543/**
Michal Vaskod3678892020-05-21 10:06:58 +02005544 * @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 +02005545 * Context position aware.
5546 *
5547 * @param[in] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005548 * @param[in] mod Matching node module, NULL for any.
5549 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005550 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5551 */
5552static LY_ERR
Michal Vaskod3678892020-05-21 10:06:58 +02005553moveto_node_alldesc(struct lyxp_set *set, const struct lys_module *mod, const char *ncname)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005554{
5555 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005556 const struct lyd_node *next, *elem, *start;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005557 struct lyxp_set ret_set;
5558 LY_ERR rc;
5559
Michal Vaskod3678892020-05-21 10:06:58 +02005560 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005561 return LY_SUCCESS;
5562 }
5563
5564 if (set->type != LYXP_SET_NODE_SET) {
5565 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
5566 return LY_EVALID;
5567 }
5568
Michal Vasko9f96a052020-03-10 09:41:45 +01005569 /* 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 +02005570 rc = moveto_node(set, NULL, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005571 LY_CHECK_RET(rc);
5572
Michal Vasko6346ece2019-09-24 13:12:53 +02005573 /* this loop traverses all the nodes in the set and adds/keeps only those that match qname */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005574 set_init(&ret_set, set);
5575 for (i = 0; i < set->used; ++i) {
5576
5577 /* TREE DFS */
5578 start = set->val.nodes[i].node;
5579 for (elem = next = start; elem; elem = next) {
Michal Vaskod3678892020-05-21 10:06:58 +02005580 rc = moveto_node_check(elem, set->root_type, ncname, mod);
Michal Vasko6346ece2019-09-24 13:12:53 +02005581 if (!rc) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005582 /* add matching node into result set */
5583 set_insert_node(&ret_set, elem, 0, LYXP_NODE_ELEM, ret_set.used);
5584 if (set_dup_node_check(set, elem, LYXP_NODE_ELEM, i)) {
5585 /* the node is a duplicate, we'll process it later in the set */
5586 goto skip_children;
5587 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005588 } else if (rc == LY_EINCOMPLETE) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005589 return rc;
5590 } else if (rc == LY_EINVAL) {
5591 goto skip_children;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005592 }
5593
5594 /* TREE DFS NEXT ELEM */
5595 /* select element for the next run - children first */
Michal Vaskod3678892020-05-21 10:06:58 +02005596 next = lyd_node_children(elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005597 if (!next) {
5598skip_children:
5599 /* no children, so try siblings, but only if it's not the start,
5600 * that is considered to be the root and it's siblings are not traversed */
5601 if (elem != start) {
5602 next = elem->next;
5603 } else {
5604 break;
5605 }
5606 }
5607 while (!next) {
5608 /* no siblings, go back through the parents */
5609 if ((struct lyd_node *)elem->parent == start) {
5610 /* we are done, no next element to process */
5611 break;
5612 }
5613 /* parent is already processed, go to its sibling */
5614 elem = (struct lyd_node *)elem->parent;
5615 next = elem->next;
5616 }
5617 }
5618 }
5619
5620 /* make the temporary set the current one */
5621 ret_set.ctx_pos = set->ctx_pos;
5622 ret_set.ctx_size = set->ctx_size;
Michal Vaskod3678892020-05-21 10:06:58 +02005623 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005624 memcpy(set, &ret_set, sizeof *set);
5625
5626 return LY_SUCCESS;
5627}
5628
5629/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005630 * @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 +02005631 *
5632 * @param[in] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005633 * @param[in] mod Matching node module, NULL for any.
5634 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005635 * @param[in] options XPath options.
5636 * @return LY_ERR
5637 */
5638static LY_ERR
Michal Vaskod3678892020-05-21 10:06:58 +02005639moveto_scnode_alldesc(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, int options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005640{
Michal Vasko6346ece2019-09-24 13:12:53 +02005641 int i, orig_used, idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005642 const struct lysc_node *next, *elem, *start;
Michal Vasko6346ece2019-09-24 13:12:53 +02005643 LY_ERR rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005644
Michal Vaskod3678892020-05-21 10:06:58 +02005645 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005646 return LY_SUCCESS;
5647 }
5648
5649 if (set->type != LYXP_SET_SCNODE_SET) {
Michal Vaskof6e51882019-12-16 09:59:45 +01005650 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 +02005651 return LY_EVALID;
5652 }
5653
Michal Vasko03ff5a72019-09-11 13:49:33 +02005654 orig_used = set->used;
5655 for (i = 0; i < orig_used; ++i) {
5656 if (set->val.scnodes[i].in_ctx != 1) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01005657 if (set->val.scnodes[i].in_ctx != -2) {
5658 continue;
5659 }
5660
5661 /* remember context node */
5662 set->val.scnodes[i].in_ctx = -1;
Michal Vaskoec4df482019-12-16 10:02:18 +01005663 } else {
5664 set->val.scnodes[i].in_ctx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005665 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005666
5667 /* TREE DFS */
5668 start = set->val.scnodes[i].scnode;
5669 for (elem = next = start; elem; elem = next) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005670 if ((elem == start) || (elem->nodetype & (LYS_CHOICE | LYS_CASE))) {
5671 /* schema-only nodes, skip root */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005672 goto next_iter;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005673 }
5674
Michal Vaskod3678892020-05-21 10:06:58 +02005675 rc = moveto_scnode_check(elem, set->root_type, ncname, mod);
Michal Vasko6346ece2019-09-24 13:12:53 +02005676 if (!rc) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01005677 if ((idx = lyxp_set_scnode_dup_node_check(set, elem, LYXP_NODE_ELEM, i)) > -1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005678 set->val.scnodes[idx].in_ctx = 1;
5679 if (idx > i) {
5680 /* we will process it later in the set */
5681 goto skip_children;
5682 }
5683 } else {
Michal Vaskoecd62de2019-11-13 12:35:11 +01005684 lyxp_set_scnode_insert_node(set, elem, LYXP_NODE_ELEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005685 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005686 } else if (rc == LY_EINVAL) {
5687 goto skip_children;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005688 }
5689
5690next_iter:
5691 /* TREE DFS NEXT ELEM */
5692 /* select element for the next run - children first */
5693 next = lysc_node_children(elem, options & LYXP_SCNODE_OUTPUT ? LYS_CONFIG_R : LYS_CONFIG_W);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005694 if (!next) {
5695skip_children:
5696 /* no children, so try siblings, but only if it's not the start,
5697 * that is considered to be the root and it's siblings are not traversed */
5698 if (elem != start) {
5699 next = elem->next;
5700 } else {
5701 break;
5702 }
5703 }
5704 while (!next) {
5705 /* no siblings, go back through the parents */
5706 if (elem->parent == start) {
5707 /* we are done, no next element to process */
5708 break;
5709 }
5710 /* parent is already processed, go to its sibling */
5711 elem = elem->parent;
5712 next = elem->next;
5713 }
5714 }
5715 }
5716
5717 return LY_SUCCESS;
5718}
5719
5720/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005721 * @brief Move context @p set to an attribute. Result is LYXP_SET_NODE_SET.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005722 * Indirectly context position aware.
5723 *
5724 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005725 * @param[in] mod Matching metadata module, NULL for any.
5726 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005727 * @return LY_ERR
5728 */
5729static LY_ERR
Michal Vaskod3678892020-05-21 10:06:58 +02005730moveto_attr(struct lyxp_set *set, const struct lys_module *mod, const char *ncname)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005731{
5732 uint32_t i;
Michal Vaskod3678892020-05-21 10:06:58 +02005733 int replaced;
Michal Vasko9f96a052020-03-10 09:41:45 +01005734 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005735
Michal Vaskod3678892020-05-21 10:06:58 +02005736 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005737 return LY_SUCCESS;
5738 }
5739
5740 if (set->type != LYXP_SET_NODE_SET) {
5741 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
5742 return LY_EVALID;
5743 }
5744
Michal Vasko03ff5a72019-09-11 13:49:33 +02005745 for (i = 0; i < set->used; ) {
5746 replaced = 0;
5747
5748 /* only attributes of an elem (not dummy) can be in the result, skip all the rest;
5749 * our attributes are always qualified */
Michal Vasko5c4e5892019-11-14 12:31:38 +01005750 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01005751 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005752
5753 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02005754 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005755 continue;
5756 }
5757
Michal Vaskod3678892020-05-21 10:06:58 +02005758 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005759 /* match */
5760 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01005761 set->val.meta[i].meta = sub;
5762 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005763 /* pos does not change */
5764 replaced = 1;
5765 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01005766 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 +02005767 }
5768 ++i;
5769 }
5770 }
5771 }
5772
5773 if (!replaced) {
5774 /* no match */
5775 set_remove_node(set, i);
5776 }
5777 }
5778
5779 return LY_SUCCESS;
5780}
5781
5782/**
5783 * @brief Move context @p set1 to union with @p set2. @p set2 is emptied afterwards.
Michal Vasko61ac2f62020-05-25 12:39:51 +02005784 * Result is LYXP_SET_NODE_SET. Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005785 *
5786 * @param[in,out] set1 Set to use for the result.
5787 * @param[in] set2 Set that is copied to @p set1.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005788 * @return LY_ERR
5789 */
5790static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005791moveto_union(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005792{
5793 LY_ERR rc;
5794
Michal Vaskod3678892020-05-21 10:06:58 +02005795 if ((set1->type != LYXP_SET_NODE_SET) || (set2->type != LYXP_SET_NODE_SET)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005796 LOGVAL(set1->ctx, LY_VLOG_LYD, set1->ctx_node, LY_VCODE_XP_INOP_2, "union", print_set_type(set1), print_set_type(set2));
5797 return LY_EVALID;
5798 }
5799
5800 /* set2 is empty or both set1 and set2 */
Michal Vaskod3678892020-05-21 10:06:58 +02005801 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005802 return LY_SUCCESS;
5803 }
5804
Michal Vaskod3678892020-05-21 10:06:58 +02005805 if (!set1->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005806 memcpy(set1, set2, sizeof *set1);
5807 /* dynamic memory belongs to set1 now, do not free */
Michal Vaskod3678892020-05-21 10:06:58 +02005808 memset(set2, 0, sizeof *set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005809 return LY_SUCCESS;
5810 }
5811
5812 /* we assume sets are sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005813 assert(!set_sort(set1) && !set_sort(set2));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005814
5815 /* sort, remove duplicates */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005816 rc = set_sorted_merge(set1, set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005817 LY_CHECK_RET(rc);
5818
5819 /* final set must be sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005820 assert(!set_sort(set1));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005821
5822 return LY_SUCCESS;
5823}
5824
5825/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005826 * @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 +02005827 * Context position aware.
5828 *
5829 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005830 * @param[in] mod Matching metadata module, NULL for any.
5831 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005832 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5833 */
5834static int
Michal Vaskod3678892020-05-21 10:06:58 +02005835moveto_attr_alldesc(struct lyxp_set *set, const struct lys_module *mod, const char *ncname)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005836{
5837 uint32_t i;
Michal Vaskod3678892020-05-21 10:06:58 +02005838 int replaced;
Michal Vasko9f96a052020-03-10 09:41:45 +01005839 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005840 struct lyxp_set *set_all_desc = NULL;
5841 LY_ERR rc;
5842
Michal Vaskod3678892020-05-21 10:06:58 +02005843 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005844 return LY_SUCCESS;
5845 }
5846
5847 if (set->type != LYXP_SET_NODE_SET) {
5848 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
5849 return LY_EVALID;
5850 }
5851
Michal Vasko03ff5a72019-09-11 13:49:33 +02005852 /* can be optimized similarly to moveto_node_alldesc() and save considerable amount of memory,
5853 * but it likely won't be used much, so it's a waste of time */
5854 /* copy the context */
5855 set_all_desc = set_copy(set);
5856 /* get all descendant nodes (the original context nodes are removed) */
Michal Vaskod3678892020-05-21 10:06:58 +02005857 rc = moveto_node_alldesc(set_all_desc, NULL, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005858 if (rc != LY_SUCCESS) {
5859 lyxp_set_free(set_all_desc);
5860 return rc;
5861 }
5862 /* prepend the original context nodes */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005863 rc = moveto_union(set, set_all_desc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005864 if (rc != LY_SUCCESS) {
5865 lyxp_set_free(set_all_desc);
5866 return rc;
5867 }
5868 lyxp_set_free(set_all_desc);
5869
Michal Vasko03ff5a72019-09-11 13:49:33 +02005870 for (i = 0; i < set->used; ) {
5871 replaced = 0;
5872
5873 /* only attributes of an elem can be in the result, skip all the rest,
5874 * we have all attributes qualified in lyd tree */
5875 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01005876 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005877 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02005878 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005879 continue;
5880 }
5881
Michal Vaskod3678892020-05-21 10:06:58 +02005882 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005883 /* match */
5884 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01005885 set->val.meta[i].meta = sub;
5886 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005887 /* pos does not change */
5888 replaced = 1;
5889 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01005890 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 +02005891 }
5892 ++i;
5893 }
5894 }
5895 }
5896
5897 if (!replaced) {
5898 /* no match */
5899 set_remove_node(set, i);
5900 }
5901 }
5902
5903 return LY_SUCCESS;
5904}
5905
5906/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005907 * @brief Move context @p set to self and al chilren, recursively. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET.
5908 * Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005909 *
5910 * @param[in] parent Current parent.
5911 * @param[in] parent_pos Position of @p parent.
5912 * @param[in] parent_type Node type of @p parent.
5913 * @param[in,out] to_set Set to use.
5914 * @param[in] dup_check_set Set for checking duplicities.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005915 * @param[in] options XPath options.
5916 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5917 */
5918static LY_ERR
5919moveto_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 +01005920 struct lyxp_set *to_set, const struct lyxp_set *dup_check_set, int options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005921{
Michal Vasko61ac2f62020-05-25 12:39:51 +02005922 const struct lyd_node *iter, *first;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005923 LY_ERR rc;
5924
5925 switch (parent_type) {
5926 case LYXP_NODE_ROOT:
5927 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko61ac2f62020-05-25 12:39:51 +02005928 assert(!parent);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005929
Michal Vasko61ac2f62020-05-25 12:39:51 +02005930 /* add all top-level nodes as elements */
5931 first = to_set->tree;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005932 break;
5933 case LYXP_NODE_ELEM:
Michal Vasko61ac2f62020-05-25 12:39:51 +02005934 /* add just the text node of this term element node */
5935 if (parent->schema->nodetype & (LYD_NODE_TERM | LYD_NODE_ANY)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005936 if (!set_dup_node_check(dup_check_set, parent, LYXP_NODE_TEXT, -1)) {
5937 set_insert_node(to_set, parent, parent_pos, LYXP_NODE_TEXT, to_set->used);
5938 }
Michal Vasko61ac2f62020-05-25 12:39:51 +02005939 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005940 }
Michal Vasko61ac2f62020-05-25 12:39:51 +02005941
5942 /* add all the children of this node */
5943 first = lyd_node_children(parent);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005944 break;
5945 default:
5946 LOGINT_RET(parent->schema->module->ctx);
5947 }
5948
Michal Vasko61ac2f62020-05-25 12:39:51 +02005949 /* add all top-level nodes as elements */
5950 LY_LIST_FOR(first, iter) {
5951 /* context check */
5952 if ((parent_type == LYXP_NODE_ROOT_CONFIG) && (iter->schema->flags & LYS_CONFIG_R)) {
5953 continue;
5954 }
5955
5956 /* when check */
5957 if (moveto_when_check(iter)) {
5958 return LY_EINCOMPLETE;
5959 }
5960
5961 if (!set_dup_node_check(dup_check_set, iter, LYXP_NODE_ELEM, -1)) {
5962 set_insert_node(to_set, iter, 0, LYXP_NODE_ELEM, to_set->used);
5963
5964 /* also add all the children of this node, recursively */
5965 rc = moveto_self_add_children_r(iter, 0, LYXP_NODE_ELEM, to_set, dup_check_set, options);
5966 LY_CHECK_RET(rc);
5967 }
5968 }
5969
Michal Vasko03ff5a72019-09-11 13:49:33 +02005970 return LY_SUCCESS;
5971}
5972
5973/**
5974 * @brief Move context @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET
5975 * (or LYXP_SET_EMPTY). Context position aware.
5976 *
5977 * @param[in,out] set Set to use.
5978 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
5979 * @param[in] options XPath options.
5980 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5981 */
5982static LY_ERR
5983moveto_self(struct lyxp_set *set, int all_desc, int options)
5984{
5985 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005986 struct lyxp_set ret_set;
5987 LY_ERR rc;
5988
Michal Vaskod3678892020-05-21 10:06:58 +02005989 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005990 return LY_SUCCESS;
5991 }
5992
5993 if (set->type != LYXP_SET_NODE_SET) {
5994 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
5995 return LY_EVALID;
5996 }
5997
5998 /* nothing to do */
5999 if (!all_desc) {
6000 return LY_SUCCESS;
6001 }
6002
Michal Vasko03ff5a72019-09-11 13:49:33 +02006003 /* add all the children, they get added recursively */
6004 set_init(&ret_set, set);
6005 for (i = 0; i < set->used; ++i) {
6006 /* copy the current node to tmp */
6007 set_insert_node(&ret_set, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, ret_set.used);
6008
6009 /* do not touch attributes and text nodes */
Michal Vasko9f96a052020-03-10 09:41:45 +01006010 if ((set->val.nodes[i].type == LYXP_NODE_TEXT) || (set->val.nodes[i].type == LYXP_NODE_META)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006011 continue;
6012 }
6013
Michal Vasko03ff5a72019-09-11 13:49:33 +02006014 /* add all the children */
6015 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 +01006016 set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006017 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006018 lyxp_set_free_content(&ret_set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006019 return rc;
6020 }
6021 }
6022
6023 /* use the temporary set as the current one */
6024 ret_set.ctx_pos = set->ctx_pos;
6025 ret_set.ctx_size = set->ctx_size;
Michal Vaskod3678892020-05-21 10:06:58 +02006026 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006027 memcpy(set, &ret_set, sizeof *set);
6028
6029 return LY_SUCCESS;
6030}
6031
6032/**
6033 * @brief Move context schema @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_SCNODE_SET
6034 * (or LYXP_SET_EMPTY).
6035 *
6036 * @param[in,out] set Set to use.
6037 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6038 * @param[in] options XPath options.
6039 * @return LY_ERR
6040 */
6041static LY_ERR
6042moveto_scnode_self(struct lyxp_set *set, int all_desc, int options)
6043{
6044 const struct lysc_node *sub;
6045 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006046
Michal Vaskod3678892020-05-21 10:06:58 +02006047 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006048 return LY_SUCCESS;
6049 }
6050
6051 if (set->type != LYXP_SET_SCNODE_SET) {
Michal Vaskof6e51882019-12-16 09:59:45 +01006052 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 +02006053 return LY_EVALID;
6054 }
6055
6056 /* nothing to do */
6057 if (!all_desc) {
6058 return LY_SUCCESS;
6059 }
6060
Michal Vasko03ff5a72019-09-11 13:49:33 +02006061 /* add all the children, they get added recursively */
6062 for (i = 0; i < set->used; ++i) {
6063 if (set->val.scnodes[i].in_ctx != 1) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006064 if (set->val.scnodes[i].in_ctx != -2) {
6065 continue;
6066 }
6067
6068 /* remember context node (it was traversed again so it changes to a normal node) */
6069 set->val.scnodes[i].in_ctx = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006070 }
6071
6072 /* add all the children */
6073 if (set->val.scnodes[i].scnode->nodetype & (LYS_LIST | LYS_CONTAINER)) {
6074 sub = NULL;
6075 while ((sub = lys_getnext(sub, set->val.scnodes[i].scnode, NULL, LYS_GETNEXT_NOSTATECHECK))) {
6076 /* RPC input/output check */
6077 if (options & LYXP_SCNODE_OUTPUT) {
6078 if (sub->parent->nodetype == LYS_INPUT) {
6079 continue;
6080 }
6081 } else {
6082 if (sub->parent->nodetype == LYS_OUTPUT) {
6083 continue;
6084 }
6085 }
6086
6087 /* context check */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006088 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (sub->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006089 continue;
6090 }
6091
Michal Vaskoecd62de2019-11-13 12:35:11 +01006092 lyxp_set_scnode_insert_node(set, sub, LYXP_NODE_ELEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006093 /* throw away the insert index, we want to consider that node again, recursively */
6094 }
6095 }
6096 }
6097
6098 return LY_SUCCESS;
6099}
6100
6101/**
6102 * @brief Move context @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_NODE_SET
6103 * (or LYXP_SET_EMPTY). Context position aware.
6104 *
6105 * @param[in] set Set to use.
6106 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6107 * @param[in] options XPath options.
6108 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6109 */
6110static LY_ERR
6111moveto_parent(struct lyxp_set *set, int all_desc, int options)
6112{
6113 LY_ERR rc;
6114 uint32_t i;
6115 struct lyd_node *node, *new_node;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006116 enum lyxp_node_type new_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006117
Michal Vaskod3678892020-05-21 10:06:58 +02006118 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006119 return LY_SUCCESS;
6120 }
6121
6122 if (set->type != LYXP_SET_NODE_SET) {
6123 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
6124 return LY_EVALID;
6125 }
6126
6127 if (all_desc) {
6128 /* <path>//.. == <path>//./.. */
6129 rc = moveto_self(set, 1, options);
6130 LY_CHECK_RET(rc);
6131 }
6132
Michal Vasko57eab132019-09-24 11:46:26 +02006133 for (i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006134 node = set->val.nodes[i].node;
6135
6136 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
6137 new_node = (struct lyd_node *)node->parent;
6138 } else if (set->val.nodes[i].type == LYXP_NODE_TEXT) {
6139 new_node = node;
Michal Vasko9f96a052020-03-10 09:41:45 +01006140 } else if (set->val.nodes[i].type == LYXP_NODE_META) {
6141 new_node = set->val.meta[i].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006142 if (!new_node) {
6143 LOGINT_RET(set->ctx);
6144 }
6145 } else {
6146 /* root does not have a parent */
Michal Vasko2caefc12019-11-14 16:07:56 +01006147 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006148 continue;
6149 }
6150
Michal Vaskoa1424542019-11-14 16:08:52 +01006151 /* when check */
6152 if (moveto_when_check(new_node)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006153 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01006154 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006155
6156 /* node already there can also be the root */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006157 if (!new_node) {
6158 new_type = set->root_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006159
6160 /* node has a standard parent (it can equal the root, it's not the root yet since they are fake) */
6161 } else {
6162 new_type = LYXP_NODE_ELEM;
6163 }
6164
Michal Vasko03ff5a72019-09-11 13:49:33 +02006165 if (set_dup_node_check(set, new_node, new_type, -1)) {
Michal Vasko2caefc12019-11-14 16:07:56 +01006166 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006167 } else {
6168 set_replace_node(set, new_node, 0, new_type, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006169 }
6170 }
6171
Michal Vasko2caefc12019-11-14 16:07:56 +01006172 set_remove_nodes_none(set);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006173 assert(!set_sort(set) && !set_sorted_dup_node_clean(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006174
6175 return LY_SUCCESS;
6176}
6177
6178/**
6179 * @brief Move context schema @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_SCNODE_SET
6180 * (or LYXP_SET_EMPTY).
6181 *
6182 * @param[in] set Set to use.
6183 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6184 * @param[in] options XPath options.
6185 * @return LY_ERR
6186 */
6187static LY_ERR
6188moveto_scnode_parent(struct lyxp_set *set, int all_desc, int options)
6189{
6190 int idx, i, orig_used, temp_ctx = 0;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006191 const struct lysc_node *node, *new_node;
6192 enum lyxp_node_type new_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006193 LY_ERR rc;
6194
Michal Vaskod3678892020-05-21 10:06:58 +02006195 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006196 return LY_SUCCESS;
6197 }
6198
6199 if (set->type != LYXP_SET_SCNODE_SET) {
Michal Vaskof6e51882019-12-16 09:59:45 +01006200 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 +02006201 return LY_EVALID;
6202 }
6203
6204 if (all_desc) {
6205 /* <path>//.. == <path>//./.. */
6206 rc = moveto_scnode_self(set, 1, options);
6207 LY_CHECK_RET(rc);
6208 }
6209
Michal Vasko03ff5a72019-09-11 13:49:33 +02006210 orig_used = set->used;
6211 for (i = 0; i < orig_used; ++i) {
6212 if (set->val.scnodes[i].in_ctx != 1) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006213 if (set->val.scnodes[i].in_ctx != -2) {
6214 continue;
6215 }
6216
6217 /* remember context node */
6218 set->val.scnodes[i].in_ctx = -1;
Michal Vaskoec4df482019-12-16 10:02:18 +01006219 } else {
6220 set->val.scnodes[i].in_ctx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006221 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006222
6223 node = set->val.scnodes[i].scnode;
6224
6225 if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
Michal Vaskod3678892020-05-21 10:06:58 +02006226 new_node = lysc_data_parent(node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006227 } else {
6228 /* root does not have a parent */
6229 continue;
6230 }
6231
Michal Vasko03ff5a72019-09-11 13:49:33 +02006232 /* node has no parent */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006233 if (!new_node) {
6234 new_type = set->root_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006235
6236 /* node has a standard parent (it can equal the root, it's not the root yet since they are fake) */
6237 } else {
6238 new_type = LYXP_NODE_ELEM;
6239 }
6240
Michal Vaskoecd62de2019-11-13 12:35:11 +01006241 idx = lyxp_set_scnode_insert_node(set, new_node, new_type);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006242 if ((idx < orig_used) && (idx > i)) {
6243 set->val.scnodes[idx].in_ctx = 2;
6244 temp_ctx = 1;
6245 }
6246 }
6247
6248 if (temp_ctx) {
6249 for (i = 0; i < orig_used; ++i) {
6250 if (set->val.scnodes[i].in_ctx == 2) {
6251 set->val.scnodes[i].in_ctx = 1;
6252 }
6253 }
6254 }
6255
6256 return LY_SUCCESS;
6257}
6258
6259/**
6260 * @brief Move context @p set to the result of a comparison. Handles '=', '!=', '<=', '<', '>=', or '>'.
6261 * Result is LYXP_SET_BOOLEAN. Indirectly context position aware.
6262 *
6263 * @param[in,out] set1 Set to use for the result.
6264 * @param[in] set2 Set acting as the second operand for @p op.
6265 * @param[in] op Comparison operator to process.
6266 * @param[in] options XPath options.
6267 * @return LY_ERR
6268 */
6269static LY_ERR
6270moveto_op_comp(struct lyxp_set *set1, struct lyxp_set *set2, const char *op, int options)
6271{
6272 /*
6273 * NODE SET + NODE SET = NODE SET + STRING /(1 NODE SET) 2 STRING
6274 * NODE SET + STRING = STRING + STRING /1 STRING (2 STRING)
6275 * NODE SET + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6276 * NODE SET + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6277 * STRING + NODE SET = STRING + STRING /(1 STRING) 2 STRING
6278 * NUMBER + NODE SET = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6279 * BOOLEAN + NODE SET = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6280 *
6281 * '=' or '!='
6282 * BOOLEAN + BOOLEAN
6283 * BOOLEAN + STRING = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6284 * BOOLEAN + NUMBER = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6285 * STRING + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6286 * NUMBER + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6287 * NUMBER + NUMBER
6288 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6289 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6290 * STRING + STRING
6291 *
6292 * '<=', '<', '>=', '>'
6293 * NUMBER + NUMBER
6294 * BOOLEAN + BOOLEAN = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6295 * BOOLEAN + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6296 * BOOLEAN + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6297 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6298 * STRING + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6299 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6300 * NUMBER + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6301 * STRING + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6302 */
6303 struct lyxp_set iter1, iter2;
6304 int result;
6305 int64_t i;
6306 LY_ERR rc;
6307
Michal Vaskod3678892020-05-21 10:06:58 +02006308 iter1.type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006309
6310 /* iterative evaluation with node-sets */
6311 if ((set1->type == LYXP_SET_NODE_SET) || (set2->type == LYXP_SET_NODE_SET)) {
6312 if (set1->type == LYXP_SET_NODE_SET) {
6313 for (i = 0; i < set1->used; ++i) {
6314 switch (set2->type) {
6315 case LYXP_SET_NUMBER:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006316 rc = set_comp_cast(&iter1, set1, LYXP_SET_NUMBER, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006317 break;
6318 case LYXP_SET_BOOLEAN:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006319 rc = set_comp_cast(&iter1, set1, LYXP_SET_BOOLEAN, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006320 break;
6321 default:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006322 rc = set_comp_cast(&iter1, set1, LYXP_SET_STRING, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006323 break;
6324 }
6325 LY_CHECK_RET(rc);
6326
6327 rc = moveto_op_comp(&iter1, set2, op, options);
6328 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006329 lyxp_set_free_content(&iter1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006330 return rc;
6331 }
6332
6333 /* lazy evaluation until true */
6334 if (iter1.val.bool) {
6335 set_fill_boolean(set1, 1);
6336 return LY_SUCCESS;
6337 }
6338 }
6339 } else {
6340 for (i = 0; i < set2->used; ++i) {
6341 switch (set1->type) {
6342 case LYXP_SET_NUMBER:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006343 rc = set_comp_cast(&iter2, set2, LYXP_SET_NUMBER, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006344 break;
6345 case LYXP_SET_BOOLEAN:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006346 rc = set_comp_cast(&iter2, set2, LYXP_SET_BOOLEAN, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006347 break;
6348 default:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006349 rc = set_comp_cast(&iter2, set2, LYXP_SET_STRING, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006350 break;
6351 }
6352 LY_CHECK_RET(rc);
6353
6354 set_fill_set(&iter1, set1);
6355
6356 rc = moveto_op_comp(&iter1, &iter2, op, options);
6357 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006358 lyxp_set_free_content(&iter1);
6359 lyxp_set_free_content(&iter2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006360 return rc;
6361 }
Michal Vaskod3678892020-05-21 10:06:58 +02006362 lyxp_set_free_content(&iter2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006363
6364 /* lazy evaluation until true */
6365 if (iter1.val.bool) {
6366 set_fill_boolean(set1, 1);
6367 return LY_SUCCESS;
6368 }
6369 }
6370 }
6371
6372 /* false for all nodes */
6373 set_fill_boolean(set1, 0);
6374 return LY_SUCCESS;
6375 }
6376
6377 /* first convert properly */
6378 if ((op[0] == '=') || (op[0] == '!')) {
6379 if ((set1->type == LYXP_SET_BOOLEAN) || (set2->type == LYXP_SET_BOOLEAN)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006380 lyxp_set_cast(set1, LYXP_SET_BOOLEAN);
6381 lyxp_set_cast(set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006382 } else if ((set1->type == LYXP_SET_NUMBER) || (set2->type == LYXP_SET_NUMBER)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006383 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006384 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006385 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006386 LY_CHECK_RET(rc);
6387 } /* else we have 2 strings */
6388 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006389 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006390 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006391 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006392 LY_CHECK_RET(rc);
6393 }
6394
6395 assert(set1->type == set2->type);
6396
6397 /* compute result */
6398 if (op[0] == '=') {
6399 if (set1->type == LYXP_SET_BOOLEAN) {
6400 result = (set1->val.bool == set2->val.bool);
6401 } else if (set1->type == LYXP_SET_NUMBER) {
6402 result = (set1->val.num == set2->val.num);
6403 } else {
6404 assert(set1->type == LYXP_SET_STRING);
Michal Vaskoac6c72f2019-11-14 16:09:34 +01006405 result = !strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006406 }
6407 } else if (op[0] == '!') {
6408 if (set1->type == LYXP_SET_BOOLEAN) {
6409 result = (set1->val.bool != set2->val.bool);
6410 } else if (set1->type == LYXP_SET_NUMBER) {
6411 result = (set1->val.num != set2->val.num);
6412 } else {
6413 assert(set1->type == LYXP_SET_STRING);
Michal Vaskoac6c72f2019-11-14 16:09:34 +01006414 result = !strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006415 }
6416 } else {
6417 assert(set1->type == LYXP_SET_NUMBER);
6418 if (op[0] == '<') {
6419 if (op[1] == '=') {
6420 result = (set1->val.num <= set2->val.num);
6421 } else {
6422 result = (set1->val.num < set2->val.num);
6423 }
6424 } else {
6425 if (op[1] == '=') {
6426 result = (set1->val.num >= set2->val.num);
6427 } else {
6428 result = (set1->val.num > set2->val.num);
6429 }
6430 }
6431 }
6432
6433 /* assign result */
6434 if (result) {
6435 set_fill_boolean(set1, 1);
6436 } else {
6437 set_fill_boolean(set1, 0);
6438 }
6439
6440 return LY_SUCCESS;
6441}
6442
6443/**
6444 * @brief Move context @p set to the result of a basic operation. Handles '+', '-', unary '-', '*', 'div',
6445 * or 'mod'. Result is LYXP_SET_NUMBER. Indirectly context position aware.
6446 *
6447 * @param[in,out] set1 Set to use for the result.
6448 * @param[in] set2 Set acting as the second operand for @p op.
6449 * @param[in] op Operator to process.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006450 * @return LY_ERR
6451 */
6452static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006453moveto_op_math(struct lyxp_set *set1, struct lyxp_set *set2, const char *op)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006454{
6455 LY_ERR rc;
6456
6457 /* unary '-' */
6458 if (!set2 && (op[0] == '-')) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006459 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006460 LY_CHECK_RET(rc);
6461 set1->val.num *= -1;
6462 lyxp_set_free(set2);
6463 return LY_SUCCESS;
6464 }
6465
6466 assert(set1 && set2);
6467
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006468 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006469 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006470 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006471 LY_CHECK_RET(rc);
6472
6473 switch (op[0]) {
6474 /* '+' */
6475 case '+':
6476 set1->val.num += set2->val.num;
6477 break;
6478
6479 /* '-' */
6480 case '-':
6481 set1->val.num -= set2->val.num;
6482 break;
6483
6484 /* '*' */
6485 case '*':
6486 set1->val.num *= set2->val.num;
6487 break;
6488
6489 /* 'div' */
6490 case 'd':
6491 set1->val.num /= set2->val.num;
6492 break;
6493
6494 /* 'mod' */
6495 case 'm':
6496 set1->val.num = ((long long)set1->val.num) % ((long long)set2->val.num);
6497 break;
6498
6499 default:
6500 LOGINT_RET(set1->ctx);
6501 }
6502
6503 return LY_SUCCESS;
6504}
6505
6506/*
6507 * eval functions
6508 *
6509 * They execute a parsed XPath expression on some data subtree.
6510 */
6511
6512/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02006513 * @brief Evaluate Predicate. Logs directly on error.
6514 *
Michal Vaskod3678892020-05-21 10:06:58 +02006515 * [9] Predicate ::= '[' Expr ']'
Michal Vasko03ff5a72019-09-11 13:49:33 +02006516 *
6517 * @param[in] exp Parsed XPath expression.
6518 * @param[in] exp_idx Position in the expression @p exp.
6519 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
6520 * @param[in] options XPath options.
6521 * @param[in] parent_pos_pred Whether parent predicate was a positional one.
6522 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6523 */
6524static LY_ERR
6525eval_predicate(struct lyxp_expr *exp, uint16_t *exp_idx, struct lyxp_set *set, int options, int parent_pos_pred)
6526{
6527 LY_ERR rc;
Michal Vasko57eab132019-09-24 11:46:26 +02006528 uint16_t i, orig_exp;
Michal Vasko5c4e5892019-11-14 12:31:38 +01006529 uint32_t orig_pos, orig_size;
6530 int32_t pred_in_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006531 struct lyxp_set set2;
6532 struct lyd_node *orig_parent;
6533
6534 /* '[' */
6535 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
6536 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
6537 ++(*exp_idx);
6538
6539 if (!set) {
6540only_parse:
6541 rc = eval_expr_select(exp, exp_idx, 0, NULL, options);
6542 LY_CHECK_RET(rc);
6543 } else if (set->type == LYXP_SET_NODE_SET) {
6544 /* 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 +01006545 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006546
6547 /* empty set, nothing to evaluate */
6548 if (!set->used) {
6549 goto only_parse;
6550 }
6551
6552 orig_exp = *exp_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006553 orig_pos = 0;
6554 orig_size = set->used;
6555 orig_parent = NULL;
Michal Vasko39dbf352020-05-21 10:08:59 +02006556 for (i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006557 set_init(&set2, set);
6558 set_insert_node(&set2, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, 0);
6559 /* remember the node context position for position() and context size for last(),
6560 * predicates should always be evaluated with respect to the child axis (since we do
6561 * not support explicit axes) so we assign positions based on their parents */
6562 if (parent_pos_pred && ((struct lyd_node *)set->val.nodes[i].node->parent != orig_parent)) {
6563 orig_parent = (struct lyd_node *)set->val.nodes[i].node->parent;
6564 orig_pos = 1;
6565 } else {
6566 ++orig_pos;
6567 }
6568
6569 set2.ctx_pos = orig_pos;
6570 set2.ctx_size = orig_size;
6571 *exp_idx = orig_exp;
6572
6573 rc = eval_expr_select(exp, exp_idx, 0, &set2, options);
6574 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006575 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006576 return rc;
6577 }
6578
6579 /* number is a position */
6580 if (set2.type == LYXP_SET_NUMBER) {
6581 if ((long long)set2.val.num == orig_pos) {
6582 set2.val.num = 1;
6583 } else {
6584 set2.val.num = 0;
6585 }
6586 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006587 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006588
6589 /* predicate satisfied or not? */
Michal Vasko57eab132019-09-24 11:46:26 +02006590 if (!set2.val.bool) {
Michal Vasko2caefc12019-11-14 16:07:56 +01006591 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006592 }
6593 }
Michal Vasko2caefc12019-11-14 16:07:56 +01006594 set_remove_nodes_none(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006595
6596 } else if (set->type == LYXP_SET_SCNODE_SET) {
6597 for (i = 0; i < set->used; ++i) {
6598 if (set->val.scnodes[i].in_ctx == 1) {
6599 /* there is a currently-valid node */
6600 break;
6601 }
6602 }
6603 /* empty set, nothing to evaluate */
6604 if (i == set->used) {
6605 goto only_parse;
6606 }
6607
6608 orig_exp = *exp_idx;
6609
Michal Vasko03ff5a72019-09-11 13:49:33 +02006610 /* set special in_ctx to all the valid snodes */
6611 pred_in_ctx = set_scnode_new_in_ctx(set);
6612
6613 /* use the valid snodes one-by-one */
6614 for (i = 0; i < set->used; ++i) {
6615 if (set->val.scnodes[i].in_ctx != pred_in_ctx) {
6616 continue;
6617 }
6618 set->val.scnodes[i].in_ctx = 1;
6619
6620 *exp_idx = orig_exp;
6621
6622 rc = eval_expr_select(exp, exp_idx, 0, set, options);
6623 LY_CHECK_RET(rc);
6624
6625 set->val.scnodes[i].in_ctx = pred_in_ctx;
6626 }
6627
6628 /* restore the state as it was before the predicate */
6629 for (i = 0; i < set->used; ++i) {
6630 if (set->val.scnodes[i].in_ctx == 1) {
6631 set->val.scnodes[i].in_ctx = 0;
6632 } else if (set->val.scnodes[i].in_ctx == pred_in_ctx) {
6633 set->val.scnodes[i].in_ctx = 1;
6634 }
6635 }
6636
6637 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02006638 set2.type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006639 set_fill_set(&set2, set);
6640
6641 rc = eval_expr_select(exp, exp_idx, 0, &set2, options);
6642 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006643 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006644 return rc;
6645 }
6646
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006647 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006648 if (!set2.val.bool) {
Michal Vaskod3678892020-05-21 10:06:58 +02006649 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006650 }
Michal Vaskod3678892020-05-21 10:06:58 +02006651 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006652 }
6653
6654 /* ']' */
6655 assert(exp->tokens[*exp_idx] == LYXP_TOKEN_BRACK2);
6656 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
6657 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
6658 ++(*exp_idx);
6659
6660 return LY_SUCCESS;
6661}
6662
6663/**
Michal Vaskod3678892020-05-21 10:06:58 +02006664 * @brief Evaluate Literal. Logs directly on error.
6665 *
6666 * @param[in] exp Parsed XPath expression.
6667 * @param[in] exp_idx Position in the expression @p exp.
6668 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
6669 */
6670static void
6671eval_literal(struct lyxp_expr *exp, uint16_t *exp_idx, struct lyxp_set *set)
6672{
6673 if (set) {
6674 if (exp->tok_len[*exp_idx] == 2) {
6675 set_fill_string(set, "", 0);
6676 } else {
6677 set_fill_string(set, &exp->expr[exp->tok_pos[*exp_idx] + 1], exp->tok_len[*exp_idx] - 2);
6678 }
6679 }
6680 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
6681 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
6682 ++(*exp_idx);
6683}
6684
6685/**
6686 * @brief Check and parse a predicate following a leaf-list to extract its value.
6687 * On success all the used tokens are skipped.
6688 *
6689 * @param[in] exp Parsed XPath expression.
6690 * @param[in] exp_idx Position in the expression @p exp.
6691 * @param[out] val Leaf-list value on success.
6692 * @param[out] val_len Length of @p val.
6693 * @return LY_ERR
6694 */
6695static LY_ERR
6696eval_name_test_parse_leaflist_predicate(struct lyxp_expr *exp, uint16_t *exp_idx, const char **val, uint16_t *val_len)
6697{
6698 uint16_t cur_idx;
6699
6700 cur_idx = *exp_idx;
6701
6702 /* '[' */
6703 if ((exp->used == cur_idx) || (exp->tokens[cur_idx] != LYXP_TOKEN_BRACK1)) {
6704 return LY_EINVAL;
6705 }
6706 ++cur_idx;
6707
6708 /* '.' */
6709 if ((exp->used == cur_idx) || (exp->tokens[cur_idx] != LYXP_TOKEN_DOT)) {
6710 return LY_EINVAL;
6711 }
6712 ++cur_idx;
6713
6714 /* '=' */
6715 if ((exp->used == cur_idx) || (exp->tokens[cur_idx] != LYXP_TOKEN_OPERATOR_COMP)
6716 || (exp->expr[exp->tok_pos[cur_idx]] != '=')) {
6717 return LY_EINVAL;
6718 }
6719 ++cur_idx;
6720
6721 /* value */
6722 if ((exp->used == cur_idx) || (exp->tokens[cur_idx] != LYXP_TOKEN_LITERAL)) {
6723 return LY_EINVAL;
6724 }
6725 ++cur_idx;
6726
6727 /* ']' */
6728 if ((exp->used == cur_idx) || (exp->tokens[cur_idx] != LYXP_TOKEN_BRACK2)) {
6729 return LY_EINVAL;
6730 }
6731 ++cur_idx;
6732
6733 /* success */
6734 *val = &exp->expr[exp->tok_pos[cur_idx - 2] + 1];
6735 *val_len = exp->tok_len[cur_idx - 2] - 2;
6736 *exp_idx = cur_idx;
6737 return LY_SUCCESS;
6738}
6739
6740/**
6741 * @brief Check and parse a predicate following a list to make sure all the tokens are as expected (all key values defined).
6742 * On success all the used tokens are skipped.
6743 *
6744 * @param[in] exp Parsed XPath expression.
6745 * @param[in] exp_idx Position in the expression @p exp.
6746 * @param[in] slist Schema node of the list.
6747 * @param[out] val Leaf-list value on success.
6748 * @param[out] val_len Length of @p val.
6749 * @return LY_ERR
6750 */
6751static LY_ERR
6752eval_name_test_parse_list_predicate(struct lyxp_expr *exp, uint16_t *exp_idx, const struct lysc_node *slist,
6753 const char **keys, uint16_t *keys_len)
6754{
6755 uint16_t key_count, i, cur_idx;
6756 const struct lysc_node *key;
6757
6758 if (slist->flags & LYS_KEYLESS) {
6759 /* invalid */
6760 return LY_EINVAL;
6761 }
6762
6763 /* get key count */
6764 key_count = 0;
6765 for (key = lysc_node_children(slist, 0); key && (key->flags & LYS_KEY); key = key->next) {
6766 ++key_count;
6767 }
6768
6769 /* briefly check the predicate for each key */
6770 cur_idx = *exp_idx;
6771 for (i = 0; i < key_count; ++i) {
6772 /* '[' */
6773 if ((exp->used == cur_idx) || (exp->tokens[cur_idx] != LYXP_TOKEN_BRACK1)) {
6774 return LY_EINVAL;
6775 }
6776 ++cur_idx;
6777
6778 /* key-name */
6779 if ((exp->used == cur_idx) || (exp->tokens[cur_idx] != LYXP_TOKEN_NAMETEST)) {
6780 return LY_EINVAL;
6781 }
6782 ++cur_idx;
6783
6784 /* '=' */
6785 if ((exp->used == cur_idx) || (exp->tokens[cur_idx] != LYXP_TOKEN_OPERATOR_COMP)
6786 || (exp->expr[exp->tok_pos[cur_idx]] != '=')) {
6787 return LY_EINVAL;
6788 }
6789 ++cur_idx;
6790
6791 /* key-value */
6792 if ((exp->used == cur_idx) || (exp->tokens[cur_idx] != LYXP_TOKEN_LITERAL)) {
6793 return LY_EINVAL;
6794 }
6795 ++cur_idx;
6796
6797 /* ']' */
6798 if ((exp->used == cur_idx) || (exp->tokens[cur_idx] != LYXP_TOKEN_BRACK2)) {
6799 return LY_EINVAL;
6800 }
6801 ++cur_idx;
6802 }
6803
6804 /* success */
6805 *keys = &exp->expr[exp->tok_pos[*exp_idx]];
6806 *keys_len = (exp->expr + exp->tok_pos[cur_idx - 1] + exp->tok_len[cur_idx - 1]) - *keys;
6807 *exp_idx = cur_idx;
6808 return LY_SUCCESS;
6809}
6810
6811/**
6812 * @brief Evaluate NameTest and any following Predicates. Logs directly on error.
6813 *
6814 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
6815 * [6] NodeTest ::= NameTest | NodeType '(' ')'
6816 * [7] NameTest ::= '*' | NCName ':' '*' | QName
6817 *
6818 * @param[in] exp Parsed XPath expression.
6819 * @param[in] exp_idx Position in the expression @p exp.
6820 * @param[in] attr_axis Whether to search attributes or standard nodes.
6821 * @param[in] all_desc Whether to search all the descendants or children only.
6822 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
6823 * @param[in] options XPath options.
6824 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6825 */
6826static LY_ERR
6827eval_name_test_with_predicate(struct lyxp_expr *exp, uint16_t *exp_idx, int attr_axis, int all_desc, struct lyxp_set *set,
6828 int options)
6829{
6830 int i;
6831 char *path;
6832 const char *ncname = NULL, *key_val = NULL;
6833 uint16_t ncname_len, key_val_len, prev_exp_idx;
6834 const struct lys_module *moveto_mod;
6835 const struct lysc_node *scnode = NULL, *tmp;
6836 struct lyd_node *list_inst = NULL;
6837 LY_ERR rc = LY_SUCCESS;
6838
6839 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
6840 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
6841 ++(*exp_idx);
6842
6843 if (!set) {
6844 goto moveto;
6845 }
6846
6847 ncname = &exp->expr[exp->tok_pos[*exp_idx - 1]];
6848 ncname_len = exp->tok_len[*exp_idx - 1];
6849
6850 /* parse (and skip) module name */
6851 rc = moveto_resolve_model(&ncname, &ncname_len, set, &moveto_mod);
6852 LY_CHECK_GOTO(rc, cleanup);
6853
6854 if (moveto_mod && !attr_axis && !all_desc && (set->type == LYXP_SET_NODE_SET)) {
6855 /* find the matching schema node in some parent in the context */
6856 for (i = 0; i < (signed)set->used; ++i) {
6857 if (set->val.nodes[i].type == set->root_type) {
6858 tmp = lys_find_child(NULL, moveto_mod, ncname, ncname_len, 0, 0);
6859 } else if ((set->val.nodes[i].type == LYXP_NODE_ELEM)
6860 && (!scnode || (lysc_data_parent(scnode) != set->val.nodes[i].node->schema))) {
6861 /* do not repeat the same search */
6862 tmp = lys_find_child(set->val.nodes[i].node->schema, moveto_mod, ncname, ncname_len, 0, 0);
6863 }
6864
6865 /* additional context check */
6866 if (tmp && (set->root_type == LYXP_NODE_ROOT_CONFIG) && (tmp->flags & LYS_CONFIG_R)) {
6867 tmp = NULL;
6868 }
6869
6870 if (tmp) {
6871 if (scnode) {
6872 /* we found a schema node with the same name but at different level, give up, too complicated */
6873 scnode = NULL;
6874 break;
6875 } else {
6876 /* remember the found schema node and continue to make sure it can be used */
6877 scnode = tmp;
6878 }
6879 tmp = NULL;
6880 }
6881 }
6882
6883 if (scnode && (scnode->nodetype == LYS_LIST)) {
6884 /* make sure all the tokens are right */
6885 prev_exp_idx = *exp_idx;
6886 if (eval_name_test_parse_list_predicate(exp, exp_idx, scnode, &key_val, &key_val_len)) {
6887 scnode = NULL;
6888 }
6889
6890 /* try to create a list instance */
6891 if (scnode && lyd_create_list(scnode, key_val, key_val_len, set->format, 0, &list_inst)) {
6892 /* for whatever reason the list failed to be created, just use standard name comparison and
6893 * parse predicate normally */
6894 *exp_idx = prev_exp_idx;
6895 scnode = NULL;
6896 }
6897 } else if (scnode && (scnode->nodetype == LYS_LEAFLIST)) {
6898 /* make sure we know the leaf-list value */
6899 if (eval_name_test_parse_leaflist_predicate(exp, exp_idx, &key_val, &key_val_len)) {
6900 /* value could not be recognized, use standard name comparison */
6901 scnode = NULL;
6902 }
6903 }
6904 }
6905
6906 if (!scnode) {
6907 /* we are not able to match based on a schema node */
6908 if (!moveto_mod) {
6909 /* '*', all nodes match */
6910 ncname = NULL;
6911 } else {
6912 /* insert name into dictionary for efficient comparison */
6913 ncname = lydict_insert(set->ctx, ncname, ncname_len);
6914 }
6915 }
6916
6917moveto:
6918 /* move to the attribute(s), data node(s), or schema node(s) */
6919 if (attr_axis) {
6920 if (set && (options & LYXP_SCNODE_ALL)) {
6921 set_scnode_clear_ctx(set);
6922 } else {
6923 if (all_desc) {
6924 rc = moveto_attr_alldesc(set, moveto_mod, ncname);
6925 } else {
6926 rc = moveto_attr(set, moveto_mod, ncname);
6927 }
6928 LY_CHECK_GOTO(rc, cleanup);
6929 }
6930 } else {
6931 if (set && (options & LYXP_SCNODE_ALL)) {
6932 if (all_desc) {
6933 rc = moveto_scnode_alldesc(set, moveto_mod, ncname, options);
6934 } else {
6935 rc = moveto_scnode(set, moveto_mod, ncname, options);
6936 }
6937 LY_CHECK_GOTO(rc, cleanup);
6938
6939 for (i = set->used - 1; i > -1; --i) {
6940 if (set->val.scnodes[i].in_ctx > 0) {
6941 break;
6942 }
6943 }
6944 if (i == -1) {
6945 path = lysc_path(set->ctx_scnode, LYSC_PATH_LOG, NULL, 0);
6946 LOGWRN(set->ctx, "Schema node \"%.*s\" not found (%.*s) with context node \"%s\".",
6947 exp->tok_len[*exp_idx], &exp->expr[exp->tok_pos[*exp_idx]],
6948 exp->tok_pos[*exp_idx] + exp->tok_len[*exp_idx], exp->expr, path);
6949 free(path);
6950 }
6951 } else {
6952 if (all_desc) {
6953 rc = moveto_node_alldesc(set, moveto_mod, ncname);
6954 } else {
6955 if (scnode) {
6956 /* we can find the nodes using hashes */
6957 rc = moveto_node_hash(set, scnode, list_inst, key_val, key_val_len);
6958 } else {
6959 rc = moveto_node(set, moveto_mod, ncname);
6960 }
6961 }
6962 LY_CHECK_GOTO(rc, cleanup);
6963 }
6964 }
6965
6966 /* Predicate* */
6967 while ((exp->used > *exp_idx) && (exp->tokens[*exp_idx] == LYXP_TOKEN_BRACK1)) {
6968 rc = eval_predicate(exp, exp_idx, set, options, 1);
6969 LY_CHECK_RET(rc);
6970 }
6971
6972cleanup:
6973 lydict_remove(set->ctx, ncname);
6974 lyd_free_tree(list_inst);
6975 return rc;
6976}
6977
6978/**
6979 * @brief Evaluate NodeType and any following Predicates. Logs directly on error.
6980 *
6981 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
6982 * [6] NodeTest ::= NameTest | NodeType '(' ')'
6983 * [8] NodeType ::= 'text' | 'node'
6984 *
6985 * @param[in] exp Parsed XPath expression.
6986 * @param[in] exp_idx Position in the expression @p exp.
6987 * @param[in] attr_axis Whether to search attributes or standard nodes.
6988 * @param[in] all_desc Whether to search all the descendants or children only.
6989 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
6990 * @param[in] options XPath options.
6991 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6992 */
6993static LY_ERR
6994eval_node_type_with_predicate(struct lyxp_expr *exp, uint16_t *exp_idx, int attr_axis, int all_desc,
6995 struct lyxp_set *set, int options)
6996{
6997 LY_ERR rc;
6998
6999 /* TODO */
7000 (void)attr_axis;
7001 (void)all_desc;
7002
7003 if (set) {
7004 assert(exp->tok_len[*exp_idx] == 4);
7005 if (set->type == LYXP_SET_SCNODE_SET) {
7006 set_scnode_clear_ctx(set);
7007 /* just for the debug message below */
7008 set = NULL;
7009 } else {
7010 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "node", 4)) {
7011 rc = xpath_node(NULL, 0, set, options);
7012 } else {
7013 assert(!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "text", 4));
7014 rc = xpath_text(NULL, 0, set, options);
7015 }
7016 LY_CHECK_RET(rc);
7017 }
7018 }
7019 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7020 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7021 ++(*exp_idx);
7022
7023 /* '(' */
7024 assert(exp->tokens[*exp_idx] == LYXP_TOKEN_PAR1);
7025 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7026 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7027 ++(*exp_idx);
7028
7029 /* ')' */
7030 assert(exp->tokens[*exp_idx] == LYXP_TOKEN_PAR2);
7031 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7032 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7033 ++(*exp_idx);
7034
7035 /* Predicate* */
7036 while ((exp->used > *exp_idx) && (exp->tokens[*exp_idx] == LYXP_TOKEN_BRACK1)) {
7037 rc = eval_predicate(exp, exp_idx, set, options, 1);
7038 LY_CHECK_RET(rc);
7039 }
7040
7041 return LY_SUCCESS;
7042}
7043
7044/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02007045 * @brief Evaluate RelativeLocationPath. Logs directly on error.
7046 *
7047 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
7048 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
Michal Vaskod3678892020-05-21 10:06:58 +02007049 * [6] NodeTest ::= NameTest | NodeType '(' ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007050 *
7051 * @param[in] exp Parsed XPath expression.
7052 * @param[in] exp_idx Position in the expression @p exp.
7053 * @param[in] all_desc Whether to search all the descendants or children only.
7054 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7055 * @param[in] options XPath options.
7056 * @return LY_ERR (YL_EINCOMPLETE on unresolved when)
7057 */
7058static LY_ERR
7059eval_relative_location_path(struct lyxp_expr *exp, uint16_t *exp_idx, int all_desc, struct lyxp_set *set, int options)
7060{
7061 int attr_axis;
7062 LY_ERR rc;
7063
7064 goto step;
7065 do {
7066 /* evaluate '/' or '//' */
7067 if (exp->tok_len[*exp_idx] == 1) {
7068 all_desc = 0;
7069 } else {
7070 assert(exp->tok_len[*exp_idx] == 2);
7071 all_desc = 1;
7072 }
7073 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7074 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7075 ++(*exp_idx);
7076
7077step:
Michal Vaskod3678892020-05-21 10:06:58 +02007078 /* evaluate abbreviated axis '@'? if any */
7079 if (exp->tokens[*exp_idx] == LYXP_TOKEN_AT) {
7080 attr_axis = 1;
7081
7082 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7083 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7084 ++(*exp_idx);
7085 } else {
7086 attr_axis = 0;
7087 }
7088
Michal Vasko03ff5a72019-09-11 13:49:33 +02007089 /* Step */
Michal Vasko03ff5a72019-09-11 13:49:33 +02007090 switch (exp->tokens[*exp_idx]) {
7091 case LYXP_TOKEN_DOT:
7092 /* evaluate '.' */
7093 if (set && (options & LYXP_SCNODE_ALL)) {
7094 rc = moveto_scnode_self(set, all_desc, options);
7095 } else {
7096 rc = moveto_self(set, all_desc, options);
7097 }
7098 LY_CHECK_RET(rc);
7099 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7100 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7101 ++(*exp_idx);
7102 break;
7103
7104 case LYXP_TOKEN_DDOT:
7105 /* evaluate '..' */
7106 if (set && (options & LYXP_SCNODE_ALL)) {
7107 rc = moveto_scnode_parent(set, all_desc, options);
7108 } else {
7109 rc = moveto_parent(set, all_desc, options);
7110 }
7111 LY_CHECK_RET(rc);
7112 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7113 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7114 ++(*exp_idx);
7115 break;
7116
Michal Vasko03ff5a72019-09-11 13:49:33 +02007117 case LYXP_TOKEN_NAMETEST:
Michal Vaskod3678892020-05-21 10:06:58 +02007118 /* evaluate NameTest Predicate* */
7119 rc = eval_name_test_with_predicate(exp, exp_idx, attr_axis, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007120 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02007121 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007122
Michal Vaskod3678892020-05-21 10:06:58 +02007123 case LYXP_TOKEN_NODETYPE:
7124 /* evaluate NodeType Predicate* */
7125 rc = eval_node_type_with_predicate(exp, exp_idx, attr_axis, all_desc, set, options);
7126 LY_CHECK_RET(rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007127 break;
7128
7129 default:
Michal Vasko02a77382019-09-12 11:47:35 +02007130 LOGINT_RET(set ? set->ctx : NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007131 }
7132 } while ((exp->used > *exp_idx) && (exp->tokens[*exp_idx] == LYXP_TOKEN_OPERATOR_PATH));
7133
7134 return LY_SUCCESS;
7135}
7136
7137/**
7138 * @brief Evaluate AbsoluteLocationPath. Logs directly on error.
7139 *
7140 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
7141 *
7142 * @param[in] exp Parsed XPath expression.
7143 * @param[in] exp_idx Position in the expression @p exp.
7144 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7145 * @param[in] options XPath options.
7146 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7147 */
7148static LY_ERR
7149eval_absolute_location_path(struct lyxp_expr *exp, uint16_t *exp_idx, struct lyxp_set *set, int options)
7150{
7151 int all_desc;
7152 LY_ERR rc;
7153
7154 if (set) {
7155 /* no matter what tokens follow, we need to be at the root */
7156 moveto_root(set, options);
7157 }
7158
7159 /* '/' RelativeLocationPath? */
7160 if (exp->tok_len[*exp_idx] == 1) {
7161 /* evaluate '/' - deferred */
7162 all_desc = 0;
7163 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7164 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7165 ++(*exp_idx);
7166
Michal Vasko4b9e1052019-09-13 11:25:37 +02007167 if (exp_check_token(set ? set->ctx : NULL, exp, *exp_idx, LYXP_TOKEN_NONE, 0)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007168 return LY_SUCCESS;
7169 }
7170 switch (exp->tokens[*exp_idx]) {
7171 case LYXP_TOKEN_DOT:
7172 case LYXP_TOKEN_DDOT:
7173 case LYXP_TOKEN_AT:
7174 case LYXP_TOKEN_NAMETEST:
7175 case LYXP_TOKEN_NODETYPE:
7176 rc = eval_relative_location_path(exp, exp_idx, all_desc, set, options);
7177 LY_CHECK_RET(rc);
7178 break;
7179 default:
7180 break;
7181 }
7182
7183 /* '//' RelativeLocationPath */
7184 } else {
7185 /* evaluate '//' - deferred so as not to waste memory by remembering all the nodes */
7186 all_desc = 1;
7187 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7188 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7189 ++(*exp_idx);
7190
7191 rc = eval_relative_location_path(exp, exp_idx, all_desc, set, options);
7192 LY_CHECK_RET(rc);
7193 }
7194
7195 return LY_SUCCESS;
7196}
7197
7198/**
7199 * @brief Evaluate FunctionCall. Logs directly on error.
7200 *
Michal Vaskod3678892020-05-21 10:06:58 +02007201 * [11] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007202 *
7203 * @param[in] exp Parsed XPath expression.
7204 * @param[in] exp_idx Position in the expression @p exp.
7205 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7206 * @param[in] options XPath options.
7207 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7208 */
7209static LY_ERR
7210eval_function_call(struct lyxp_expr *exp, uint16_t *exp_idx, struct lyxp_set *set, int options)
7211{
7212 LY_ERR rc;
7213 LY_ERR (*xpath_func)(struct lyxp_set **, uint16_t, struct lyxp_set *, int) = NULL;
Michal Vasko0cbf54f2019-12-16 10:01:06 +01007214 uint16_t arg_count = 0, i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007215 struct lyxp_set **args = NULL, **args_aux;
7216
7217 if (set) {
7218 /* FunctionName */
7219 switch (exp->tok_len[*exp_idx]) {
7220 case 3:
7221 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "not", 3)) {
7222 xpath_func = &xpath_not;
7223 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "sum", 3)) {
7224 xpath_func = &xpath_sum;
7225 }
7226 break;
7227 case 4:
7228 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "lang", 4)) {
7229 xpath_func = &xpath_lang;
7230 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "last", 4)) {
7231 xpath_func = &xpath_last;
7232 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "name", 4)) {
7233 xpath_func = &xpath_name;
7234 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "true", 4)) {
7235 xpath_func = &xpath_true;
7236 }
7237 break;
7238 case 5:
7239 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "count", 5)) {
7240 xpath_func = &xpath_count;
7241 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "false", 5)) {
7242 xpath_func = &xpath_false;
7243 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "floor", 5)) {
7244 xpath_func = &xpath_floor;
7245 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "round", 5)) {
7246 xpath_func = &xpath_round;
7247 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "deref", 5)) {
7248 xpath_func = &xpath_deref;
7249 }
7250 break;
7251 case 6:
7252 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "concat", 6)) {
7253 xpath_func = &xpath_concat;
7254 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "number", 6)) {
7255 xpath_func = &xpath_number;
7256 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "string", 6)) {
7257 xpath_func = &xpath_string;
7258 }
7259 break;
7260 case 7:
7261 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "boolean", 7)) {
7262 xpath_func = &xpath_boolean;
7263 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "ceiling", 7)) {
7264 xpath_func = &xpath_ceiling;
7265 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "current", 7)) {
7266 xpath_func = &xpath_current;
7267 }
7268 break;
7269 case 8:
7270 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "contains", 8)) {
7271 xpath_func = &xpath_contains;
7272 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "position", 8)) {
7273 xpath_func = &xpath_position;
7274 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "re-match", 8)) {
7275 xpath_func = &xpath_re_match;
7276 }
7277 break;
7278 case 9:
7279 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "substring", 9)) {
7280 xpath_func = &xpath_substring;
7281 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "translate", 9)) {
7282 xpath_func = &xpath_translate;
7283 }
7284 break;
7285 case 10:
7286 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "local-name", 10)) {
7287 xpath_func = &xpath_local_name;
7288 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "enum-value", 10)) {
7289 xpath_func = &xpath_enum_value;
7290 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "bit-is-set", 10)) {
7291 xpath_func = &xpath_bit_is_set;
7292 }
7293 break;
7294 case 11:
7295 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "starts-with", 11)) {
7296 xpath_func = &xpath_starts_with;
7297 }
7298 break;
7299 case 12:
7300 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "derived-from", 12)) {
7301 xpath_func = &xpath_derived_from;
7302 }
7303 break;
7304 case 13:
7305 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "namespace-uri", 13)) {
7306 xpath_func = &xpath_namespace_uri;
7307 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "string-length", 13)) {
7308 xpath_func = &xpath_string_length;
7309 }
7310 break;
7311 case 15:
7312 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "normalize-space", 15)) {
7313 xpath_func = &xpath_normalize_space;
7314 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "substring-after", 15)) {
7315 xpath_func = &xpath_substring_after;
7316 }
7317 break;
7318 case 16:
7319 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "substring-before", 16)) {
7320 xpath_func = &xpath_substring_before;
7321 }
7322 break;
7323 case 20:
7324 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "derived-from-or-self", 20)) {
7325 xpath_func = &xpath_derived_from_or_self;
7326 }
7327 break;
7328 }
7329
7330 if (!xpath_func) {
7331 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*exp_idx]]);
7332 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]]);
7333 return LY_EVALID;
7334 }
7335 }
7336
7337 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7338 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7339 ++(*exp_idx);
7340
7341 /* '(' */
7342 assert(exp->tokens[*exp_idx] == LYXP_TOKEN_PAR1);
7343 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7344 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7345 ++(*exp_idx);
7346
7347 /* ( Expr ( ',' Expr )* )? */
7348 if (exp->tokens[*exp_idx] != LYXP_TOKEN_PAR2) {
7349 if (set) {
7350 args = malloc(sizeof *args);
7351 LY_CHECK_ERR_GOTO(!args, LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7352 arg_count = 1;
7353 args[0] = set_copy(set);
7354 if (!args[0]) {
7355 rc = LY_EMEM;
7356 goto cleanup;
7357 }
7358
7359 rc = eval_expr_select(exp, exp_idx, 0, args[0], options);
7360 LY_CHECK_GOTO(rc, cleanup);
7361 } else {
7362 rc = eval_expr_select(exp, exp_idx, 0, NULL, options);
7363 LY_CHECK_GOTO(rc, cleanup);
7364 }
7365 }
7366 while ((exp->used > *exp_idx) && (exp->tokens[*exp_idx] == LYXP_TOKEN_COMMA)) {
7367 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7368 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7369 ++(*exp_idx);
7370
7371 if (set) {
7372 ++arg_count;
7373 args_aux = realloc(args, arg_count * sizeof *args);
7374 LY_CHECK_ERR_GOTO(!args_aux, arg_count--; LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7375 args = args_aux;
7376 args[arg_count - 1] = set_copy(set);
7377 if (!args[arg_count - 1]) {
7378 rc = LY_EMEM;
7379 goto cleanup;
7380 }
7381
7382 rc = eval_expr_select(exp, exp_idx, 0, args[arg_count - 1], options);
7383 LY_CHECK_GOTO(rc, cleanup);
7384 } else {
7385 rc = eval_expr_select(exp, exp_idx, 0, NULL, options);
7386 LY_CHECK_GOTO(rc, cleanup);
7387 }
7388 }
7389
7390 /* ')' */
7391 assert(exp->tokens[*exp_idx] == LYXP_TOKEN_PAR2);
7392 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7393 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7394 ++(*exp_idx);
7395
7396 if (set) {
7397 /* evaluate function */
7398 rc = xpath_func(args, arg_count, set, options);
7399
7400 if (options & LYXP_SCNODE_ALL) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007401 /* merge all nodes from arg evaluations */
7402 for (i = 0; i < arg_count; ++i) {
7403 set_scnode_clear_ctx(args[i]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007404 lyxp_set_scnode_merge(set, args[i]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007405 }
7406 }
7407 } else {
7408 rc = LY_SUCCESS;
7409 }
7410
7411cleanup:
7412 for (i = 0; i < arg_count; ++i) {
7413 lyxp_set_free(args[i]);
7414 }
7415 free(args);
7416
7417 return rc;
7418}
7419
7420/**
7421 * @brief Evaluate Number. Logs directly on error.
7422 *
7423 * @param[in] ctx Context for errors.
7424 * @param[in] exp Parsed XPath expression.
7425 * @param[in] exp_idx Position in the expression @p exp.
7426 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7427 * @return LY_ERR
7428 */
7429static LY_ERR
7430eval_number(struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *exp_idx, struct lyxp_set *set)
7431{
7432 long double num;
7433 char *endptr;
7434
7435 if (set) {
7436 errno = 0;
7437 num = strtold(&exp->expr[exp->tok_pos[*exp_idx]], &endptr);
7438 if (errno) {
7439 LOGVAL(ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*exp_idx]]);
7440 LOGVAL(ctx, LY_VLOG_LYD, set->ctx_node, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double (%s).",
7441 exp->tok_len[*exp_idx], &exp->expr[exp->tok_pos[*exp_idx]], strerror(errno));
7442 return LY_EVALID;
7443 } else if (endptr - &exp->expr[exp->tok_pos[*exp_idx]] != exp->tok_len[*exp_idx]) {
7444 LOGVAL(ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*exp_idx]]);
7445 LOGVAL(ctx, LY_VLOG_LYD, set->ctx_node, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double.",
7446 exp->tok_len[*exp_idx], &exp->expr[exp->tok_pos[*exp_idx]]);
7447 return LY_EVALID;
7448 }
7449
7450 set_fill_number(set, num);
7451 }
7452
7453 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7454 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7455 ++(*exp_idx);
7456 return LY_SUCCESS;
7457}
7458
7459/**
7460 * @brief Evaluate PathExpr. Logs directly on error.
7461 *
Michal Vaskod3678892020-05-21 10:06:58 +02007462 * [12] PathExpr ::= LocationPath | PrimaryExpr Predicate*
Michal Vasko03ff5a72019-09-11 13:49:33 +02007463 * | PrimaryExpr Predicate* '/' RelativeLocationPath
7464 * | PrimaryExpr Predicate* '//' RelativeLocationPath
7465 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
Michal Vaskod3678892020-05-21 10:06:58 +02007466 * [10] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall
Michal Vasko03ff5a72019-09-11 13:49:33 +02007467 *
7468 * @param[in] exp Parsed XPath expression.
7469 * @param[in] exp_idx Position in the expression @p exp.
7470 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7471 * @param[in] options XPath options.
7472 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7473 */
7474static LY_ERR
7475eval_path_expr(struct lyxp_expr *exp, uint16_t *exp_idx, struct lyxp_set *set, int options)
7476{
7477 int all_desc, parent_pos_pred;
7478 LY_ERR rc;
7479
7480 switch (exp->tokens[*exp_idx]) {
7481 case LYXP_TOKEN_PAR1:
7482 /* '(' Expr ')' */
7483
7484 /* '(' */
7485 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7486 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7487 ++(*exp_idx);
7488
7489 /* Expr */
7490 rc = eval_expr_select(exp, exp_idx, 0, set, options);
7491 LY_CHECK_RET(rc);
7492
7493 /* ')' */
7494 assert(exp->tokens[*exp_idx] == LYXP_TOKEN_PAR2);
7495 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7496 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7497 ++(*exp_idx);
7498
7499 parent_pos_pred = 0;
7500 goto predicate;
7501
7502 case LYXP_TOKEN_DOT:
7503 case LYXP_TOKEN_DDOT:
7504 case LYXP_TOKEN_AT:
7505 case LYXP_TOKEN_NAMETEST:
7506 case LYXP_TOKEN_NODETYPE:
7507 /* RelativeLocationPath */
7508 rc = eval_relative_location_path(exp, exp_idx, 0, set, options);
7509 LY_CHECK_RET(rc);
7510 break;
7511
7512 case LYXP_TOKEN_FUNCNAME:
7513 /* FunctionCall */
7514 if (!set) {
7515 rc = eval_function_call(exp, exp_idx, NULL, options);
7516 } else {
7517 rc = eval_function_call(exp, exp_idx, set, options);
7518 }
7519 LY_CHECK_RET(rc);
7520
7521 parent_pos_pred = 1;
7522 goto predicate;
7523
7524 case LYXP_TOKEN_OPERATOR_PATH:
7525 /* AbsoluteLocationPath */
7526 rc = eval_absolute_location_path(exp, exp_idx, set, options);
7527 LY_CHECK_RET(rc);
7528 break;
7529
7530 case LYXP_TOKEN_LITERAL:
7531 /* Literal */
7532 if (!set || (options & LYXP_SCNODE_ALL)) {
7533 if (set) {
7534 set_scnode_clear_ctx(set);
7535 }
7536 eval_literal(exp, exp_idx, NULL);
7537 } else {
7538 eval_literal(exp, exp_idx, set);
7539 }
7540
7541 parent_pos_pred = 1;
7542 goto predicate;
7543
7544 case LYXP_TOKEN_NUMBER:
7545 /* Number */
7546 if (!set || (options & LYXP_SCNODE_ALL)) {
7547 if (set) {
7548 set_scnode_clear_ctx(set);
7549 }
Michal Vasko02a77382019-09-12 11:47:35 +02007550 rc = eval_number(NULL, exp, exp_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007551 } else {
7552 rc = eval_number(set->ctx, exp, exp_idx, set);
7553 }
7554 LY_CHECK_RET(rc);
7555
7556 parent_pos_pred = 1;
7557 goto predicate;
7558
7559 default:
7560 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INTOK, print_token(exp->tokens[*exp_idx]),
7561 &exp->expr[exp->tok_pos[*exp_idx]]);
7562 return LY_EVALID;
7563 }
7564
7565 return LY_SUCCESS;
7566
7567predicate:
7568 /* Predicate* */
7569 while ((exp->used > *exp_idx) && (exp->tokens[*exp_idx] == LYXP_TOKEN_BRACK1)) {
7570 rc = eval_predicate(exp, exp_idx, set, options, parent_pos_pred);
7571 LY_CHECK_RET(rc);
7572 }
7573
7574 /* ('/' or '//') RelativeLocationPath */
7575 if ((exp->used > *exp_idx) && (exp->tokens[*exp_idx] == LYXP_TOKEN_OPERATOR_PATH)) {
7576
7577 /* evaluate '/' or '//' */
7578 if (exp->tok_len[*exp_idx] == 1) {
7579 all_desc = 0;
7580 } else {
7581 assert(exp->tok_len[*exp_idx] == 2);
7582 all_desc = 1;
7583 }
7584
7585 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7586 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7587 ++(*exp_idx);
7588
7589 rc = eval_relative_location_path(exp, exp_idx, all_desc, set, options);
7590 LY_CHECK_RET(rc);
7591 }
7592
7593 return LY_SUCCESS;
7594}
7595
7596/**
7597 * @brief Evaluate UnionExpr. Logs directly on error.
7598 *
Michal Vaskod3678892020-05-21 10:06:58 +02007599 * [20] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007600 *
7601 * @param[in] exp Parsed XPath expression.
7602 * @param[in] exp_idx Position in the expression @p exp.
7603 * @param[in] repeat How many times this expression is repeated.
7604 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7605 * @param[in] options XPath options.
7606 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7607 */
7608static LY_ERR
7609eval_union_expr(struct lyxp_expr *exp, uint16_t *exp_idx, uint16_t repeat, struct lyxp_set *set, int options)
7610{
7611 LY_ERR rc = LY_SUCCESS;
7612 struct lyxp_set orig_set, set2;
7613 uint16_t i;
7614
7615 assert(repeat);
7616
7617 set_init(&orig_set, set);
7618 set_init(&set2, set);
7619
7620 set_fill_set(&orig_set, set);
7621
7622 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_UNION, set, options);
7623 LY_CHECK_GOTO(rc, cleanup);
7624
7625 /* ('|' PathExpr)* */
7626 for (i = 0; i < repeat; ++i) {
7627 assert(exp->tokens[*exp_idx] == LYXP_TOKEN_OPERATOR_UNI);
7628 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7629 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7630 ++(*exp_idx);
7631
7632 if (!set) {
7633 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_UNION, NULL, options);
7634 LY_CHECK_GOTO(rc, cleanup);
7635 continue;
7636 }
7637
7638 set_fill_set(&set2, &orig_set);
7639 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_UNION, &set2, options);
7640 LY_CHECK_GOTO(rc, cleanup);
7641
7642 /* eval */
7643 if (options & LYXP_SCNODE_ALL) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01007644 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007645 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007646 rc = moveto_union(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007647 LY_CHECK_GOTO(rc, cleanup);
7648 }
7649 }
7650
7651cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007652 lyxp_set_free_content(&orig_set);
7653 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007654 return rc;
7655}
7656
7657/**
7658 * @brief Evaluate UnaryExpr. Logs directly on error.
7659 *
Michal Vaskod3678892020-05-21 10:06:58 +02007660 * [19] UnaryExpr ::= UnionExpr | '-' UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007661 *
7662 * @param[in] exp Parsed XPath expression.
7663 * @param[in] exp_idx Position in the expression @p exp.
7664 * @param[in] repeat How many times this expression is repeated.
7665 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7666 * @param[in] options XPath options.
7667 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7668 */
7669static LY_ERR
7670eval_unary_expr(struct lyxp_expr *exp, uint16_t *exp_idx, uint16_t repeat, struct lyxp_set *set, int options)
7671{
7672 LY_ERR rc;
7673 uint16_t this_op, i;
7674
7675 assert(repeat);
7676
7677 /* ('-')+ */
7678 this_op = *exp_idx;
7679 for (i = 0; i < repeat; ++i) {
7680 assert(!exp_check_token(set->ctx, exp, *exp_idx, LYXP_TOKEN_OPERATOR_MATH, 0) && (exp->expr[exp->tok_pos[*exp_idx]] == '-'));
7681
7682 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7683 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7684 ++(*exp_idx);
7685 }
7686
7687 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_UNARY, set, options);
7688 LY_CHECK_RET(rc);
7689
7690 if (set && (repeat % 2)) {
7691 if (options & LYXP_SCNODE_ALL) {
7692 warn_operands(set->ctx, set, NULL, 1, exp->expr, exp->tok_pos[this_op]);
7693 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007694 rc = moveto_op_math(set, NULL, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007695 LY_CHECK_RET(rc);
7696 }
7697 }
7698
7699 return LY_SUCCESS;
7700}
7701
7702/**
7703 * @brief Evaluate MultiplicativeExpr. Logs directly on error.
7704 *
Michal Vaskod3678892020-05-21 10:06:58 +02007705 * [18] MultiplicativeExpr ::= UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007706 * | MultiplicativeExpr '*' UnaryExpr
7707 * | MultiplicativeExpr 'div' UnaryExpr
7708 * | MultiplicativeExpr 'mod' UnaryExpr
7709 *
7710 * @param[in] exp Parsed XPath expression.
7711 * @param[in] exp_idx Position in the expression @p exp.
7712 * @param[in] repeat How many times this expression is repeated.
7713 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7714 * @param[in] options XPath options.
7715 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7716 */
7717static LY_ERR
7718eval_multiplicative_expr(struct lyxp_expr *exp, uint16_t *exp_idx, uint16_t repeat, struct lyxp_set *set, int options)
7719{
7720 LY_ERR rc;
7721 uint16_t this_op;
7722 struct lyxp_set orig_set, set2;
7723 uint16_t i;
7724
7725 assert(repeat);
7726
7727 set_init(&orig_set, set);
7728 set_init(&set2, set);
7729
7730 set_fill_set(&orig_set, set);
7731
7732 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_MULTIPLICATIVE, set, options);
7733 LY_CHECK_GOTO(rc, cleanup);
7734
7735 /* ('*' / 'div' / 'mod' UnaryExpr)* */
7736 for (i = 0; i < repeat; ++i) {
7737 this_op = *exp_idx;
7738
7739 assert(exp->tokens[*exp_idx] == LYXP_TOKEN_OPERATOR_MATH);
7740 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7741 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7742 ++(*exp_idx);
7743
7744 if (!set) {
7745 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_MULTIPLICATIVE, NULL, options);
7746 LY_CHECK_GOTO(rc, cleanup);
7747 continue;
7748 }
7749
7750 set_fill_set(&set2, &orig_set);
7751 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_MULTIPLICATIVE, &set2, options);
7752 LY_CHECK_GOTO(rc, cleanup);
7753
7754 /* eval */
7755 if (options & LYXP_SCNODE_ALL) {
7756 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007757 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007758 set_scnode_clear_ctx(set);
7759 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007760 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007761 LY_CHECK_GOTO(rc, cleanup);
7762 }
7763 }
7764
7765cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007766 lyxp_set_free_content(&orig_set);
7767 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007768 return rc;
7769}
7770
7771/**
7772 * @brief Evaluate AdditiveExpr. Logs directly on error.
7773 *
Michal Vaskod3678892020-05-21 10:06:58 +02007774 * [17] AdditiveExpr ::= MultiplicativeExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007775 * | AdditiveExpr '+' MultiplicativeExpr
7776 * | AdditiveExpr '-' MultiplicativeExpr
7777 *
7778 * @param[in] exp Parsed XPath expression.
7779 * @param[in] exp_idx Position in the expression @p exp.
7780 * @param[in] repeat How many times this expression is repeated.
7781 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7782 * @param[in] options XPath options.
7783 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7784 */
7785static LY_ERR
7786eval_additive_expr(struct lyxp_expr *exp, uint16_t *exp_idx, uint16_t repeat, struct lyxp_set *set, int options)
7787{
7788 LY_ERR rc;
7789 uint16_t this_op;
7790 struct lyxp_set orig_set, set2;
7791 uint16_t i;
7792
7793 assert(repeat);
7794
7795 set_init(&orig_set, set);
7796 set_init(&set2, set);
7797
7798 set_fill_set(&orig_set, set);
7799
7800 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_ADDITIVE, set, options);
7801 LY_CHECK_GOTO(rc, cleanup);
7802
7803 /* ('+' / '-' MultiplicativeExpr)* */
7804 for (i = 0; i < repeat; ++i) {
7805 this_op = *exp_idx;
7806
7807 assert(exp->tokens[*exp_idx] == LYXP_TOKEN_OPERATOR_MATH);
7808 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7809 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7810 ++(*exp_idx);
7811
7812 if (!set) {
7813 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_ADDITIVE, NULL, options);
7814 LY_CHECK_GOTO(rc, cleanup);
7815 continue;
7816 }
7817
7818 set_fill_set(&set2, &orig_set);
7819 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_ADDITIVE, &set2, options);
7820 LY_CHECK_GOTO(rc, cleanup);
7821
7822 /* eval */
7823 if (options & LYXP_SCNODE_ALL) {
7824 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007825 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007826 set_scnode_clear_ctx(set);
7827 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007828 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007829 LY_CHECK_GOTO(rc, cleanup);
7830 }
7831 }
7832
7833cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007834 lyxp_set_free_content(&orig_set);
7835 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007836 return rc;
7837}
7838
7839/**
7840 * @brief Evaluate RelationalExpr. Logs directly on error.
7841 *
Michal Vaskod3678892020-05-21 10:06:58 +02007842 * [16] RelationalExpr ::= AdditiveExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007843 * | RelationalExpr '<' AdditiveExpr
7844 * | RelationalExpr '>' AdditiveExpr
7845 * | RelationalExpr '<=' AdditiveExpr
7846 * | RelationalExpr '>=' AdditiveExpr
7847 *
7848 * @param[in] exp Parsed XPath expression.
7849 * @param[in] exp_idx Position in the expression @p exp.
7850 * @param[in] repeat How many times this expression is repeated.
7851 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7852 * @param[in] options XPath options.
7853 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7854 */
7855static LY_ERR
7856eval_relational_expr(struct lyxp_expr *exp, uint16_t *exp_idx, uint16_t repeat, struct lyxp_set *set, int options)
7857{
7858 LY_ERR rc;
7859 uint16_t this_op;
7860 struct lyxp_set orig_set, set2;
7861 uint16_t i;
7862
7863 assert(repeat);
7864
7865 set_init(&orig_set, set);
7866 set_init(&set2, set);
7867
7868 set_fill_set(&orig_set, set);
7869
7870 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_RELATIONAL, set, options);
7871 LY_CHECK_GOTO(rc, cleanup);
7872
7873 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
7874 for (i = 0; i < repeat; ++i) {
7875 this_op = *exp_idx;
7876
7877 assert(exp->tokens[*exp_idx] == LYXP_TOKEN_OPERATOR_COMP);
7878 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7879 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7880 ++(*exp_idx);
7881
7882 if (!set) {
7883 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_RELATIONAL, NULL, options);
7884 LY_CHECK_GOTO(rc, cleanup);
7885 continue;
7886 }
7887
7888 set_fill_set(&set2, &orig_set);
7889 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_RELATIONAL, &set2, options);
7890 LY_CHECK_GOTO(rc, cleanup);
7891
7892 /* eval */
7893 if (options & LYXP_SCNODE_ALL) {
7894 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007895 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007896 set_scnode_clear_ctx(set);
7897 } else {
7898 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options);
7899 LY_CHECK_GOTO(rc, cleanup);
7900 }
7901 }
7902
7903cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007904 lyxp_set_free_content(&orig_set);
7905 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007906 return rc;
7907}
7908
7909/**
7910 * @brief Evaluate EqualityExpr. Logs directly on error.
7911 *
Michal Vaskod3678892020-05-21 10:06:58 +02007912 * [15] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007913 * | EqualityExpr '!=' RelationalExpr
7914 *
7915 * @param[in] exp Parsed XPath expression.
7916 * @param[in] exp_idx Position in the expression @p exp.
7917 * @param[in] repeat How many times this expression is repeated.
7918 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7919 * @param[in] options XPath options.
7920 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7921 */
7922static LY_ERR
7923eval_equality_expr(struct lyxp_expr *exp, uint16_t *exp_idx, uint16_t repeat, struct lyxp_set *set, int options)
7924{
7925 LY_ERR rc;
7926 uint16_t this_op;
7927 struct lyxp_set orig_set, set2;
7928 uint16_t i;
7929
7930 assert(repeat);
7931
7932 set_init(&orig_set, set);
7933 set_init(&set2, set);
7934
7935 set_fill_set(&orig_set, set);
7936
7937 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_EQUALITY, set, options);
7938 LY_CHECK_GOTO(rc, cleanup);
7939
7940 /* ('=' / '!=' RelationalExpr)* */
7941 for (i = 0; i < repeat; ++i) {
7942 this_op = *exp_idx;
7943
7944 assert(exp->tokens[*exp_idx] == LYXP_TOKEN_OPERATOR_COMP);
7945 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7946 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7947 ++(*exp_idx);
7948
7949 if (!set) {
7950 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_EQUALITY, NULL, options);
7951 LY_CHECK_GOTO(rc, cleanup);
7952 continue;
7953 }
7954
7955 set_fill_set(&set2, &orig_set);
7956 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_EQUALITY, &set2, options);
7957 LY_CHECK_GOTO(rc, cleanup);
7958
7959 /* eval */
7960 if (options & LYXP_SCNODE_ALL) {
7961 warn_operands(set->ctx, set, &set2, 0, exp->expr, exp->tok_pos[this_op - 1]);
7962 warn_equality_value(exp, set, *exp_idx - 1, this_op - 1, *exp_idx - 1);
7963 warn_equality_value(exp, &set2, this_op - 1, this_op - 1, *exp_idx - 1);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007964 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007965 set_scnode_clear_ctx(set);
7966 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007967 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options);
7968 LY_CHECK_GOTO(rc, cleanup);
7969 }
7970 }
7971
7972cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007973 lyxp_set_free_content(&orig_set);
7974 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007975 return rc;
7976}
7977
7978/**
7979 * @brief Evaluate AndExpr. Logs directly on error.
7980 *
Michal Vaskod3678892020-05-21 10:06:58 +02007981 * [14] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007982 *
7983 * @param[in] exp Parsed XPath expression.
7984 * @param[in] exp_idx Position in the expression @p exp.
7985 * @param[in] repeat How many times this expression is repeated.
7986 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7987 * @param[in] options XPath options.
7988 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7989 */
7990static LY_ERR
7991eval_and_expr(struct lyxp_expr *exp, uint16_t *exp_idx, uint16_t repeat, struct lyxp_set *set, int options)
7992{
7993 LY_ERR rc;
7994 struct lyxp_set orig_set, set2;
7995 uint16_t i;
7996
7997 assert(repeat);
7998
7999 set_init(&orig_set, set);
8000 set_init(&set2, set);
8001
8002 set_fill_set(&orig_set, set);
8003
8004 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_AND, set, options);
8005 LY_CHECK_GOTO(rc, cleanup);
8006
8007 /* cast to boolean, we know that will be the final result */
8008 if (set && (options & LYXP_SCNODE_ALL)) {
8009 set_scnode_clear_ctx(set);
8010 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008011 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008012 }
8013
8014 /* ('and' EqualityExpr)* */
8015 for (i = 0; i < repeat; ++i) {
8016 assert(exp->tokens[*exp_idx] == LYXP_TOKEN_OPERATOR_LOG);
8017 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (!set || !set->val.bool ? "skipped" : "parsed"),
8018 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
8019 ++(*exp_idx);
8020
8021 /* lazy evaluation */
8022 if (!set || ((set->type == LYXP_SET_BOOLEAN) && !set->val.bool)) {
8023 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_AND, NULL, options);
8024 LY_CHECK_GOTO(rc, cleanup);
8025 continue;
8026 }
8027
8028 set_fill_set(&set2, &orig_set);
8029 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_AND, &set2, options);
8030 LY_CHECK_GOTO(rc, cleanup);
8031
8032 /* eval - just get boolean value actually */
8033 if (set->type == LYXP_SET_SCNODE_SET) {
8034 set_scnode_clear_ctx(&set2);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008035 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008036 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008037 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008038 set_fill_set(set, &set2);
8039 }
8040 }
8041
8042cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008043 lyxp_set_free_content(&orig_set);
8044 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008045 return rc;
8046}
8047
8048/**
8049 * @brief Evaluate OrExpr. Logs directly on error.
8050 *
Michal Vaskod3678892020-05-21 10:06:58 +02008051 * [13] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008052 *
8053 * @param[in] exp Parsed XPath expression.
8054 * @param[in] exp_idx Position in the expression @p exp.
8055 * @param[in] repeat How many times this expression is repeated.
8056 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8057 * @param[in] options XPath options.
8058 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8059 */
8060static LY_ERR
8061eval_or_expr(struct lyxp_expr *exp, uint16_t *exp_idx, uint16_t repeat, struct lyxp_set *set, int options)
8062{
8063 LY_ERR rc;
8064 struct lyxp_set orig_set, set2;
8065 uint16_t i;
8066
8067 assert(repeat);
8068
8069 set_init(&orig_set, set);
8070 set_init(&set2, set);
8071
8072 set_fill_set(&orig_set, set);
8073
8074 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_OR, set, options);
8075 LY_CHECK_GOTO(rc, cleanup);
8076
8077 /* cast to boolean, we know that will be the final result */
8078 if (set && (options & LYXP_SCNODE_ALL)) {
8079 set_scnode_clear_ctx(set);
8080 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008081 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008082 }
8083
8084 /* ('or' AndExpr)* */
8085 for (i = 0; i < repeat; ++i) {
8086 assert(exp->tokens[*exp_idx] == LYXP_TOKEN_OPERATOR_LOG);
8087 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (!set || set->val.bool ? "skipped" : "parsed"),
8088 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
8089 ++(*exp_idx);
8090
8091 /* lazy evaluation */
8092 if (!set || ((set->type == LYXP_SET_BOOLEAN) && set->val.bool)) {
8093 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_OR, NULL, options);
8094 LY_CHECK_GOTO(rc, cleanup);
8095 continue;
8096 }
8097
8098 set_fill_set(&set2, &orig_set);
8099 /* expr_type cound have been LYXP_EXPR_NONE in all these later calls (except for the first one),
8100 * but it does not matter */
8101 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_OR, &set2, options);
8102 LY_CHECK_GOTO(rc, cleanup);
8103
8104 /* eval - just get boolean value actually */
8105 if (set->type == LYXP_SET_SCNODE_SET) {
8106 set_scnode_clear_ctx(&set2);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008107 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008108 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008109 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008110 set_fill_set(set, &set2);
8111 }
8112 }
8113
8114cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008115 lyxp_set_free_content(&orig_set);
8116 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008117 return rc;
8118}
8119
8120/**
8121 * @brief Decide what expression is at the pointer @p exp_idx and evaluate it accordingly.
8122 *
8123 * @param[in] exp Parsed XPath expression.
8124 * @param[in] exp_idx Position in the expression @p exp.
8125 * @param[in] etype Expression type to evaluate.
8126 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8127 * @param[in] options XPath options.
8128 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8129 */
8130static LY_ERR
8131eval_expr_select(struct lyxp_expr *exp, uint16_t *exp_idx, enum lyxp_expr_type etype, struct lyxp_set *set, int options)
8132{
8133 uint16_t i, count;
8134 enum lyxp_expr_type next_etype;
8135 LY_ERR rc;
8136
8137 /* process operator repeats */
8138 if (!exp->repeat[*exp_idx]) {
8139 next_etype = LYXP_EXPR_NONE;
8140 } else {
8141 /* find etype repeat */
8142 for (i = 0; exp->repeat[*exp_idx][i] > etype; ++i);
8143
8144 /* select one-priority lower because etype expression called us */
8145 if (i) {
8146 next_etype = exp->repeat[*exp_idx][i - 1];
8147 /* count repeats for that expression */
8148 for (count = 0; i && exp->repeat[*exp_idx][i - 1] == next_etype; ++count, --i);
8149 } else {
8150 next_etype = LYXP_EXPR_NONE;
8151 }
8152 }
8153
8154 /* decide what expression are we parsing based on the repeat */
8155 switch (next_etype) {
8156 case LYXP_EXPR_OR:
8157 rc = eval_or_expr(exp, exp_idx, count, set, options);
8158 break;
8159 case LYXP_EXPR_AND:
8160 rc = eval_and_expr(exp, exp_idx, count, set, options);
8161 break;
8162 case LYXP_EXPR_EQUALITY:
8163 rc = eval_equality_expr(exp, exp_idx, count, set, options);
8164 break;
8165 case LYXP_EXPR_RELATIONAL:
8166 rc = eval_relational_expr(exp, exp_idx, count, set, options);
8167 break;
8168 case LYXP_EXPR_ADDITIVE:
8169 rc = eval_additive_expr(exp, exp_idx, count, set, options);
8170 break;
8171 case LYXP_EXPR_MULTIPLICATIVE:
8172 rc = eval_multiplicative_expr(exp, exp_idx, count, set, options);
8173 break;
8174 case LYXP_EXPR_UNARY:
8175 rc = eval_unary_expr(exp, exp_idx, count, set, options);
8176 break;
8177 case LYXP_EXPR_UNION:
8178 rc = eval_union_expr(exp, exp_idx, count, set, options);
8179 break;
8180 case LYXP_EXPR_NONE:
8181 rc = eval_path_expr(exp, exp_idx, set, options);
8182 break;
8183 default:
8184 LOGINT_RET(set->ctx);
8185 }
8186
8187 return rc;
8188}
8189
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008190/**
8191 * @brief Get root type.
8192 *
8193 * @param[in] ctx_node Context node.
8194 * @param[in] ctx_scnode Schema context node.
8195 * @param[in] options XPath options.
8196 * @return Root type.
8197 */
8198static enum lyxp_node_type
8199lyxp_get_root_type(const struct lyd_node *ctx_node, const struct lysc_node *ctx_scnode, int options)
8200{
8201 if (options & LYXP_SCNODE_ALL) {
8202 if (options & LYXP_SCNODE) {
8203 /* general root that can access everything */
8204 return LYXP_NODE_ROOT;
8205 } else if (!ctx_scnode || (ctx_scnode->flags & LYS_CONFIG_W)) {
8206 /* root context node can access only config data (because we said so, it is unspecified) */
8207 return LYXP_NODE_ROOT_CONFIG;
8208 } else {
8209 return LYXP_NODE_ROOT;
8210 }
8211 }
8212
8213 if (!ctx_node || (ctx_node->schema->flags & LYS_CONFIG_W)) {
8214 /* root context node can access only config data (because we said so, it is unspecified) */
8215 return LYXP_NODE_ROOT_CONFIG;
8216 }
8217
8218 return LYXP_NODE_ROOT;
8219}
8220
Michal Vasko03ff5a72019-09-11 13:49:33 +02008221LY_ERR
Michal Vaskoecd62de2019-11-13 12:35:11 +01008222lyxp_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 +01008223 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 +02008224{
Michal Vasko03ff5a72019-09-11 13:49:33 +02008225 uint16_t exp_idx = 0;
8226 LY_ERR rc;
8227
Michal Vaskoecd62de2019-11-13 12:35:11 +01008228 LY_CHECK_ARG_RET(NULL, exp, local_mod, set, LY_EINVAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008229
8230 /* prepare set for evaluation */
8231 exp_idx = 0;
8232 memset(set, 0, sizeof *set);
Michal Vaskod3678892020-05-21 10:06:58 +02008233 set->type = LYXP_SET_NODE_SET;
Michal Vasko9b368d32020-02-14 13:53:31 +01008234 set_insert_node(set, (struct lyd_node *)ctx_node, 0, ctx_node_type, 0);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008235 set->ctx = local_mod->ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008236 set->ctx_node = ctx_node;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008237 set->root_type = lyxp_get_root_type(ctx_node, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008238 set->local_mod = local_mod;
Michal Vaskof03ed032020-03-04 13:31:44 +01008239 set->tree = tree;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008240 set->format = format;
8241
8242 /* evaluate */
8243 rc = eval_expr_select(exp, &exp_idx, 0, set, options);
8244 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02008245 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008246 }
8247
Michal Vasko03ff5a72019-09-11 13:49:33 +02008248 return rc;
8249}
8250
8251#if 0
8252
8253/* full xml printing of set elements, not used currently */
8254
8255void
8256lyxp_set_print_xml(FILE *f, struct lyxp_set *set)
8257{
8258 uint32_t i;
8259 char *str_num;
8260 struct lyout out;
8261
8262 memset(&out, 0, sizeof out);
8263
8264 out.type = LYOUT_STREAM;
8265 out.method.f = f;
8266
8267 switch (set->type) {
8268 case LYXP_SET_EMPTY:
8269 ly_print(&out, "Empty XPath set\n\n");
8270 break;
8271 case LYXP_SET_BOOLEAN:
8272 ly_print(&out, "Boolean XPath set:\n");
8273 ly_print(&out, "%s\n\n", set->value.bool ? "true" : "false");
8274 break;
8275 case LYXP_SET_STRING:
8276 ly_print(&out, "String XPath set:\n");
8277 ly_print(&out, "\"%s\"\n\n", set->value.str);
8278 break;
8279 case LYXP_SET_NUMBER:
8280 ly_print(&out, "Number XPath set:\n");
8281
8282 if (isnan(set->value.num)) {
8283 str_num = strdup("NaN");
8284 } else if ((set->value.num == 0) || (set->value.num == -0.0f)) {
8285 str_num = strdup("0");
8286 } else if (isinf(set->value.num) && !signbit(set->value.num)) {
8287 str_num = strdup("Infinity");
8288 } else if (isinf(set->value.num) && signbit(set->value.num)) {
8289 str_num = strdup("-Infinity");
8290 } else if ((long long)set->value.num == set->value.num) {
8291 if (asprintf(&str_num, "%lld", (long long)set->value.num) == -1) {
8292 str_num = NULL;
8293 }
8294 } else {
8295 if (asprintf(&str_num, "%03.1Lf", set->value.num) == -1) {
8296 str_num = NULL;
8297 }
8298 }
8299 if (!str_num) {
8300 LOGMEM;
8301 return;
8302 }
8303 ly_print(&out, "%s\n\n", str_num);
8304 free(str_num);
8305 break;
8306 case LYXP_SET_NODE_SET:
8307 ly_print(&out, "Node XPath set:\n");
8308
8309 for (i = 0; i < set->used; ++i) {
8310 ly_print(&out, "%d. ", i + 1);
8311 switch (set->node_type[i]) {
8312 case LYXP_NODE_ROOT_ALL:
8313 ly_print(&out, "ROOT all\n\n");
8314 break;
8315 case LYXP_NODE_ROOT_CONFIG:
8316 ly_print(&out, "ROOT config\n\n");
8317 break;
8318 case LYXP_NODE_ROOT_STATE:
8319 ly_print(&out, "ROOT state\n\n");
8320 break;
8321 case LYXP_NODE_ROOT_NOTIF:
8322 ly_print(&out, "ROOT notification \"%s\"\n\n", set->value.nodes[i]->schema->name);
8323 break;
8324 case LYXP_NODE_ROOT_RPC:
8325 ly_print(&out, "ROOT rpc \"%s\"\n\n", set->value.nodes[i]->schema->name);
8326 break;
8327 case LYXP_NODE_ROOT_OUTPUT:
8328 ly_print(&out, "ROOT output \"%s\"\n\n", set->value.nodes[i]->schema->name);
8329 break;
8330 case LYXP_NODE_ELEM:
8331 ly_print(&out, "ELEM \"%s\"\n", set->value.nodes[i]->schema->name);
8332 xml_print_node(&out, 1, set->value.nodes[i], 1, LYP_FORMAT);
8333 ly_print(&out, "\n");
8334 break;
8335 case LYXP_NODE_TEXT:
8336 ly_print(&out, "TEXT \"%s\"\n\n", ((struct lyd_node_leaf_list *)set->value.nodes[i])->value_str);
8337 break;
8338 case LYXP_NODE_ATTR:
8339 ly_print(&out, "ATTR \"%s\" = \"%s\"\n\n", set->value.attrs[i]->name, set->value.attrs[i]->value);
8340 break;
8341 }
8342 }
8343 break;
8344 }
8345}
8346
8347#endif
8348
8349LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008350lyxp_set_cast(struct lyxp_set *set, enum lyxp_set_type target)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008351{
8352 long double num;
8353 char *str;
8354 LY_ERR rc;
8355
8356 if (!set || (set->type == target)) {
8357 return LY_SUCCESS;
8358 }
8359
8360 /* it's not possible to convert anything into a node set */
Michal Vaskod3678892020-05-21 10:06:58 +02008361 assert(target != LYXP_SET_NODE_SET);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008362
8363 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vaskod3678892020-05-21 10:06:58 +02008364 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008365 return LY_EINVAL;
8366 }
8367
8368 /* to STRING */
Michal Vaskod3678892020-05-21 10:06:58 +02008369 if ((target == LYXP_SET_STRING) || ((target == LYXP_SET_NUMBER) && (set->type == LYXP_SET_NODE_SET))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008370 switch (set->type) {
8371 case LYXP_SET_NUMBER:
8372 if (isnan(set->val.num)) {
8373 set->val.str = strdup("NaN");
8374 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8375 } else if ((set->val.num == 0) || (set->val.num == -0.0f)) {
8376 set->val.str = strdup("0");
8377 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8378 } else if (isinf(set->val.num) && !signbit(set->val.num)) {
8379 set->val.str = strdup("Infinity");
8380 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8381 } else if (isinf(set->val.num) && signbit(set->val.num)) {
8382 set->val.str = strdup("-Infinity");
8383 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8384 } else if ((long long)set->val.num == set->val.num) {
8385 if (asprintf(&str, "%lld", (long long)set->val.num) == -1) {
8386 LOGMEM_RET(set->ctx);
8387 }
8388 set->val.str = str;
8389 } else {
8390 if (asprintf(&str, "%03.1Lf", set->val.num) == -1) {
8391 LOGMEM_RET(set->ctx);
8392 }
8393 set->val.str = str;
8394 }
8395 break;
8396 case LYXP_SET_BOOLEAN:
8397 if (set->val.bool) {
8398 set->val.str = strdup("true");
8399 } else {
8400 set->val.str = strdup("false");
8401 }
8402 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM);
8403 break;
8404 case LYXP_SET_NODE_SET:
8405 assert(set->used);
8406
8407 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008408 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008409
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008410 rc = cast_node_set_to_string(set, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008411 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02008412 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008413 set->val.str = str;
8414 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008415 default:
8416 LOGINT_RET(set->ctx);
8417 }
8418 set->type = LYXP_SET_STRING;
8419 }
8420
8421 /* to NUMBER */
8422 if (target == LYXP_SET_NUMBER) {
8423 switch (set->type) {
8424 case LYXP_SET_STRING:
8425 num = cast_string_to_number(set->val.str);
Michal Vaskod3678892020-05-21 10:06:58 +02008426 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008427 set->val.num = num;
8428 break;
8429 case LYXP_SET_BOOLEAN:
8430 if (set->val.bool) {
8431 set->val.num = 1;
8432 } else {
8433 set->val.num = 0;
8434 }
8435 break;
8436 default:
8437 LOGINT_RET(set->ctx);
8438 }
8439 set->type = LYXP_SET_NUMBER;
8440 }
8441
8442 /* to BOOLEAN */
8443 if (target == LYXP_SET_BOOLEAN) {
8444 switch (set->type) {
8445 case LYXP_SET_NUMBER:
8446 if ((set->val.num == 0) || (set->val.num == -0.0f) || isnan(set->val.num)) {
8447 set->val.bool = 0;
8448 } else {
8449 set->val.bool = 1;
8450 }
8451 break;
8452 case LYXP_SET_STRING:
8453 if (set->val.str[0]) {
Michal Vaskod3678892020-05-21 10:06:58 +02008454 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008455 set->val.bool = 1;
8456 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02008457 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008458 set->val.bool = 0;
8459 }
8460 break;
8461 case LYXP_SET_NODE_SET:
Michal Vaskod3678892020-05-21 10:06:58 +02008462 if (set->used) {
8463 lyxp_set_free_content(set);
8464 set->val.bool = 1;
8465 } else {
8466 lyxp_set_free_content(set);
8467 set->val.bool = 0;
8468 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008469 break;
8470 default:
8471 LOGINT_RET(set->ctx);
8472 }
8473 set->type = LYXP_SET_BOOLEAN;
8474 }
8475
Michal Vasko03ff5a72019-09-11 13:49:33 +02008476 return LY_SUCCESS;
8477}
8478
8479LY_ERR
8480lyxp_atomize(struct lyxp_expr *exp, LYD_FORMAT format, const struct lys_module *local_mod, const struct lysc_node *ctx_scnode,
8481 enum lyxp_node_type ctx_scnode_type, struct lyxp_set *set, int options)
8482{
8483 struct ly_ctx *ctx;
8484 uint16_t exp_idx = 0;
8485
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008486 LY_CHECK_ARG_RET(NULL, exp, local_mod, set, LY_EINVAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008487
8488 ctx = local_mod->ctx;
8489
8490 /* prepare set for evaluation */
8491 exp_idx = 0;
8492 memset(set, 0, sizeof *set);
8493 set->type = LYXP_SET_SCNODE_SET;
Michal Vaskoecd62de2019-11-13 12:35:11 +01008494 lyxp_set_scnode_insert_node(set, ctx_scnode, ctx_scnode_type);
Michal Vasko5c4e5892019-11-14 12:31:38 +01008495 set->val.scnodes[0].in_ctx = -2;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008496 set->ctx = ctx;
8497 set->ctx_scnode = ctx_scnode;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008498 set->root_type = lyxp_get_root_type(NULL, ctx_scnode, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008499 set->local_mod = local_mod;
8500 set->format = format;
8501
8502 /* evaluate */
8503 return eval_expr_select(exp, &exp_idx, 0, set, options);
8504}