blob: f8333d1132091676c0a819d55f0291ff188c0c2c [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 Vasko03ff5a72019-09-11 13:49:33 +02006 * Copyright (c) 2015 - 2019 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 Vaskod3678892020-05-21 10:06:58 +02005253 if (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 Vaskod3678892020-05-21 10:06:58 +02005290 if (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 Vaskod3678892020-05-21 10:06:58 +02005630 * @brief Move context @p set to a schema node and all its descendants. Result is LYXP_SET_NODE_SET (or LYXP_SET_EMPTY).
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 Vaskod3678892020-05-21 10:06:58 +02005721 * @brief Move context @p set to an attribute.
5722 * Result is LYXP_SET_NODE_SET (or LYXP_SET_EMPTY).
Michal Vasko03ff5a72019-09-11 13:49:33 +02005723 * Indirectly context position aware.
5724 *
5725 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005726 * @param[in] mod Matching metadata module, NULL for any.
5727 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005728 * @return LY_ERR
5729 */
5730static LY_ERR
Michal Vaskod3678892020-05-21 10:06:58 +02005731moveto_attr(struct lyxp_set *set, const struct lys_module *mod, const char *ncname)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005732{
5733 uint32_t i;
Michal Vaskod3678892020-05-21 10:06:58 +02005734 int replaced;
Michal Vasko9f96a052020-03-10 09:41:45 +01005735 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005736
Michal Vaskod3678892020-05-21 10:06:58 +02005737 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005738 return LY_SUCCESS;
5739 }
5740
5741 if (set->type != LYXP_SET_NODE_SET) {
5742 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
5743 return LY_EVALID;
5744 }
5745
Michal Vasko03ff5a72019-09-11 13:49:33 +02005746 for (i = 0; i < set->used; ) {
5747 replaced = 0;
5748
5749 /* only attributes of an elem (not dummy) can be in the result, skip all the rest;
5750 * our attributes are always qualified */
Michal Vasko5c4e5892019-11-14 12:31:38 +01005751 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01005752 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005753
5754 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02005755 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005756 continue;
5757 }
5758
Michal Vaskod3678892020-05-21 10:06:58 +02005759 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005760 /* match */
5761 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01005762 set->val.meta[i].meta = sub;
5763 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005764 /* pos does not change */
5765 replaced = 1;
5766 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01005767 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 +02005768 }
5769 ++i;
5770 }
5771 }
5772 }
5773
5774 if (!replaced) {
5775 /* no match */
5776 set_remove_node(set, i);
5777 }
5778 }
5779
5780 return LY_SUCCESS;
5781}
5782
5783/**
5784 * @brief Move context @p set1 to union with @p set2. @p set2 is emptied afterwards.
5785 * Result is LYXP_SET_NODE_SET (or LYXP_SET_EMPTY). Context position aware.
5786 *
5787 * @param[in,out] set1 Set to use for the result.
5788 * @param[in] set2 Set that is copied to @p set1.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005789 * @return LY_ERR
5790 */
5791static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005792moveto_union(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005793{
5794 LY_ERR rc;
5795
Michal Vaskod3678892020-05-21 10:06:58 +02005796 if ((set1->type != LYXP_SET_NODE_SET) || (set2->type != LYXP_SET_NODE_SET)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005797 LOGVAL(set1->ctx, LY_VLOG_LYD, set1->ctx_node, LY_VCODE_XP_INOP_2, "union", print_set_type(set1), print_set_type(set2));
5798 return LY_EVALID;
5799 }
5800
5801 /* set2 is empty or both set1 and set2 */
Michal Vaskod3678892020-05-21 10:06:58 +02005802 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005803 return LY_SUCCESS;
5804 }
5805
Michal Vaskod3678892020-05-21 10:06:58 +02005806 if (!set1->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005807 memcpy(set1, set2, sizeof *set1);
5808 /* dynamic memory belongs to set1 now, do not free */
Michal Vaskod3678892020-05-21 10:06:58 +02005809 memset(set2, 0, sizeof *set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005810 return LY_SUCCESS;
5811 }
5812
5813 /* we assume sets are sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005814 assert(!set_sort(set1) && !set_sort(set2));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005815
5816 /* sort, remove duplicates */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005817 rc = set_sorted_merge(set1, set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005818 LY_CHECK_RET(rc);
5819
5820 /* final set must be sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005821 assert(!set_sort(set1));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005822
5823 return LY_SUCCESS;
5824}
5825
5826/**
Michal Vaskod3678892020-05-21 10:06:58 +02005827 * @brief Move context @p set to an attribute in any of the descendants.
5828 * Result is LYXP_SET_NODE_SET (or LYXP_SET_EMPTY).
Michal Vasko03ff5a72019-09-11 13:49:33 +02005829 * Context position aware.
5830 *
5831 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005832 * @param[in] mod Matching metadata module, NULL for any.
5833 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005834 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5835 */
5836static int
Michal Vaskod3678892020-05-21 10:06:58 +02005837moveto_attr_alldesc(struct lyxp_set *set, const struct lys_module *mod, const char *ncname)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005838{
5839 uint32_t i;
Michal Vaskod3678892020-05-21 10:06:58 +02005840 int replaced;
Michal Vasko9f96a052020-03-10 09:41:45 +01005841 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005842 struct lyxp_set *set_all_desc = NULL;
5843 LY_ERR rc;
5844
Michal Vaskod3678892020-05-21 10:06:58 +02005845 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005846 return LY_SUCCESS;
5847 }
5848
5849 if (set->type != LYXP_SET_NODE_SET) {
5850 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
5851 return LY_EVALID;
5852 }
5853
Michal Vasko03ff5a72019-09-11 13:49:33 +02005854 /* can be optimized similarly to moveto_node_alldesc() and save considerable amount of memory,
5855 * but it likely won't be used much, so it's a waste of time */
5856 /* copy the context */
5857 set_all_desc = set_copy(set);
5858 /* get all descendant nodes (the original context nodes are removed) */
Michal Vaskod3678892020-05-21 10:06:58 +02005859 rc = moveto_node_alldesc(set_all_desc, NULL, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005860 if (rc != LY_SUCCESS) {
5861 lyxp_set_free(set_all_desc);
5862 return rc;
5863 }
5864 /* prepend the original context nodes */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005865 rc = moveto_union(set, set_all_desc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005866 if (rc != LY_SUCCESS) {
5867 lyxp_set_free(set_all_desc);
5868 return rc;
5869 }
5870 lyxp_set_free(set_all_desc);
5871
Michal Vasko03ff5a72019-09-11 13:49:33 +02005872 for (i = 0; i < set->used; ) {
5873 replaced = 0;
5874
5875 /* only attributes of an elem can be in the result, skip all the rest,
5876 * we have all attributes qualified in lyd tree */
5877 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01005878 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005879 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02005880 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005881 continue;
5882 }
5883
Michal Vaskod3678892020-05-21 10:06:58 +02005884 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005885 /* match */
5886 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01005887 set->val.meta[i].meta = sub;
5888 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005889 /* pos does not change */
5890 replaced = 1;
5891 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01005892 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 +02005893 }
5894 ++i;
5895 }
5896 }
5897 }
5898
5899 if (!replaced) {
5900 /* no match */
5901 set_remove_node(set, i);
5902 }
5903 }
5904
5905 return LY_SUCCESS;
5906}
5907
5908/**
5909 * @brief Move context @p set to self and al chilren, recursively. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET
5910 * (or LYXP_SET_EMPTY). Context position aware.
5911 *
5912 * @param[in] parent Current parent.
5913 * @param[in] parent_pos Position of @p parent.
5914 * @param[in] parent_type Node type of @p parent.
5915 * @param[in,out] to_set Set to use.
5916 * @param[in] dup_check_set Set for checking duplicities.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005917 * @param[in] options XPath options.
5918 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5919 */
5920static LY_ERR
5921moveto_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 +01005922 struct lyxp_set *to_set, const struct lyxp_set *dup_check_set, int options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005923{
5924 const struct lyd_node *sub;
5925 LY_ERR rc;
5926
5927 switch (parent_type) {
5928 case LYXP_NODE_ROOT:
5929 case LYXP_NODE_ROOT_CONFIG:
5930 /* add the same node but as an element */
5931 if (!set_dup_node_check(dup_check_set, parent, LYXP_NODE_ELEM, -1)) {
5932 set_insert_node(to_set, parent, 0, LYXP_NODE_ELEM, to_set->used);
5933
5934 /* skip anydata/anyxml and dummy nodes */
Michal Vasko5c4e5892019-11-14 12:31:38 +01005935 if (!(parent->schema->nodetype & LYS_ANYDATA)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005936 /* also add all the children of this node, recursively */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005937 rc = moveto_self_add_children_r(parent, 0, LYXP_NODE_ELEM, to_set, dup_check_set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005938 LY_CHECK_RET(rc);
5939 }
5940 }
5941 break;
5942 case LYXP_NODE_ELEM:
5943 /* add all the children ... */
5944 if (!(parent->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA))) {
5945 for (sub = lyd_node_children(parent); sub; sub = sub->next) {
5946 /* context check */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005947 if ((to_set->root_type == LYXP_NODE_ROOT_CONFIG) && (sub->schema->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005948 continue;
5949 }
5950
Michal Vaskoa1424542019-11-14 16:08:52 +01005951 /* when check */
5952 if (moveto_when_check(sub)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005953 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01005954 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005955
5956 if (!set_dup_node_check(dup_check_set, sub, LYXP_NODE_ELEM, -1)) {
5957 set_insert_node(to_set, sub, 0, LYXP_NODE_ELEM, to_set->used);
5958
Michal Vasko5c4e5892019-11-14 12:31:38 +01005959 /* skip anydata/anyxml nodes */
5960 if (sub->schema->nodetype & LYS_ANYDATA) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005961 continue;
5962 }
5963
5964 /* also add all the children of this node, recursively */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005965 rc = moveto_self_add_children_r(sub, 0, LYXP_NODE_ELEM, to_set, dup_check_set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005966 LY_CHECK_RET(rc);
5967 }
5968 }
5969
5970 /* ... or add their text node, ... */
5971 } else {
5972 if (!set_dup_node_check(dup_check_set, parent, LYXP_NODE_TEXT, -1)) {
5973 set_insert_node(to_set, parent, parent_pos, LYXP_NODE_TEXT, to_set->used);
5974 }
5975 }
5976 break;
5977 default:
5978 LOGINT_RET(parent->schema->module->ctx);
5979 }
5980
5981 return LY_SUCCESS;
5982}
5983
5984/**
5985 * @brief Move context @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET
5986 * (or LYXP_SET_EMPTY). Context position aware.
5987 *
5988 * @param[in,out] set Set to use.
5989 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
5990 * @param[in] options XPath options.
5991 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5992 */
5993static LY_ERR
5994moveto_self(struct lyxp_set *set, int all_desc, int options)
5995{
5996 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005997 struct lyxp_set ret_set;
5998 LY_ERR rc;
5999
Michal Vaskod3678892020-05-21 10:06:58 +02006000 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006001 return LY_SUCCESS;
6002 }
6003
6004 if (set->type != LYXP_SET_NODE_SET) {
6005 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
6006 return LY_EVALID;
6007 }
6008
6009 /* nothing to do */
6010 if (!all_desc) {
6011 return LY_SUCCESS;
6012 }
6013
Michal Vasko03ff5a72019-09-11 13:49:33 +02006014 /* add all the children, they get added recursively */
6015 set_init(&ret_set, set);
6016 for (i = 0; i < set->used; ++i) {
6017 /* copy the current node to tmp */
6018 set_insert_node(&ret_set, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, ret_set.used);
6019
6020 /* do not touch attributes and text nodes */
Michal Vasko9f96a052020-03-10 09:41:45 +01006021 if ((set->val.nodes[i].type == LYXP_NODE_TEXT) || (set->val.nodes[i].type == LYXP_NODE_META)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006022 continue;
6023 }
6024
Michal Vasko5c4e5892019-11-14 12:31:38 +01006025 /* skip anydata/anyxml nodes */
6026 if (set->val.nodes[i].node->schema->nodetype & LYS_ANYDATA) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006027 continue;
6028 }
6029
6030 /* add all the children */
6031 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 +01006032 set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006033 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006034 lyxp_set_free_content(&ret_set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006035 return rc;
6036 }
6037 }
6038
6039 /* use the temporary set as the current one */
6040 ret_set.ctx_pos = set->ctx_pos;
6041 ret_set.ctx_size = set->ctx_size;
Michal Vaskod3678892020-05-21 10:06:58 +02006042 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006043 memcpy(set, &ret_set, sizeof *set);
6044
6045 return LY_SUCCESS;
6046}
6047
6048/**
6049 * @brief Move context schema @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_SCNODE_SET
6050 * (or LYXP_SET_EMPTY).
6051 *
6052 * @param[in,out] set Set to use.
6053 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6054 * @param[in] options XPath options.
6055 * @return LY_ERR
6056 */
6057static LY_ERR
6058moveto_scnode_self(struct lyxp_set *set, int all_desc, int options)
6059{
6060 const struct lysc_node *sub;
6061 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006062
Michal Vaskod3678892020-05-21 10:06:58 +02006063 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006064 return LY_SUCCESS;
6065 }
6066
6067 if (set->type != LYXP_SET_SCNODE_SET) {
Michal Vaskof6e51882019-12-16 09:59:45 +01006068 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 +02006069 return LY_EVALID;
6070 }
6071
6072 /* nothing to do */
6073 if (!all_desc) {
6074 return LY_SUCCESS;
6075 }
6076
Michal Vasko03ff5a72019-09-11 13:49:33 +02006077 /* add all the children, they get added recursively */
6078 for (i = 0; i < set->used; ++i) {
6079 if (set->val.scnodes[i].in_ctx != 1) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006080 if (set->val.scnodes[i].in_ctx != -2) {
6081 continue;
6082 }
6083
6084 /* remember context node (it was traversed again so it changes to a normal node) */
6085 set->val.scnodes[i].in_ctx = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006086 }
6087
6088 /* add all the children */
6089 if (set->val.scnodes[i].scnode->nodetype & (LYS_LIST | LYS_CONTAINER)) {
6090 sub = NULL;
6091 while ((sub = lys_getnext(sub, set->val.scnodes[i].scnode, NULL, LYS_GETNEXT_NOSTATECHECK))) {
6092 /* RPC input/output check */
6093 if (options & LYXP_SCNODE_OUTPUT) {
6094 if (sub->parent->nodetype == LYS_INPUT) {
6095 continue;
6096 }
6097 } else {
6098 if (sub->parent->nodetype == LYS_OUTPUT) {
6099 continue;
6100 }
6101 }
6102
6103 /* context check */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006104 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (sub->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006105 continue;
6106 }
6107
Michal Vaskoecd62de2019-11-13 12:35:11 +01006108 lyxp_set_scnode_insert_node(set, sub, LYXP_NODE_ELEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006109 /* throw away the insert index, we want to consider that node again, recursively */
6110 }
6111 }
6112 }
6113
6114 return LY_SUCCESS;
6115}
6116
6117/**
6118 * @brief Move context @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_NODE_SET
6119 * (or LYXP_SET_EMPTY). Context position aware.
6120 *
6121 * @param[in] set Set to use.
6122 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6123 * @param[in] options XPath options.
6124 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6125 */
6126static LY_ERR
6127moveto_parent(struct lyxp_set *set, int all_desc, int options)
6128{
6129 LY_ERR rc;
6130 uint32_t i;
6131 struct lyd_node *node, *new_node;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006132 enum lyxp_node_type new_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006133
Michal Vaskod3678892020-05-21 10:06:58 +02006134 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006135 return LY_SUCCESS;
6136 }
6137
6138 if (set->type != LYXP_SET_NODE_SET) {
6139 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
6140 return LY_EVALID;
6141 }
6142
6143 if (all_desc) {
6144 /* <path>//.. == <path>//./.. */
6145 rc = moveto_self(set, 1, options);
6146 LY_CHECK_RET(rc);
6147 }
6148
Michal Vasko57eab132019-09-24 11:46:26 +02006149 for (i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006150 node = set->val.nodes[i].node;
6151
6152 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
6153 new_node = (struct lyd_node *)node->parent;
6154 } else if (set->val.nodes[i].type == LYXP_NODE_TEXT) {
6155 new_node = node;
Michal Vasko9f96a052020-03-10 09:41:45 +01006156 } else if (set->val.nodes[i].type == LYXP_NODE_META) {
6157 new_node = set->val.meta[i].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006158 if (!new_node) {
6159 LOGINT_RET(set->ctx);
6160 }
6161 } else {
6162 /* root does not have a parent */
Michal Vasko2caefc12019-11-14 16:07:56 +01006163 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006164 continue;
6165 }
6166
Michal Vaskoa1424542019-11-14 16:08:52 +01006167 /* when check */
6168 if (moveto_when_check(new_node)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006169 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01006170 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006171
6172 /* node already there can also be the root */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006173 if (!new_node) {
6174 new_type = set->root_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006175
6176 /* node has a standard parent (it can equal the root, it's not the root yet since they are fake) */
6177 } else {
6178 new_type = LYXP_NODE_ELEM;
6179 }
6180
Michal Vasko03ff5a72019-09-11 13:49:33 +02006181 if (set_dup_node_check(set, new_node, new_type, -1)) {
Michal Vasko2caefc12019-11-14 16:07:56 +01006182 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006183 } else {
6184 set_replace_node(set, new_node, 0, new_type, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006185 }
6186 }
6187
Michal Vasko2caefc12019-11-14 16:07:56 +01006188 set_remove_nodes_none(set);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006189 assert(!set_sort(set) && !set_sorted_dup_node_clean(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006190
6191 return LY_SUCCESS;
6192}
6193
6194/**
6195 * @brief Move context schema @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_SCNODE_SET
6196 * (or LYXP_SET_EMPTY).
6197 *
6198 * @param[in] set Set to use.
6199 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6200 * @param[in] options XPath options.
6201 * @return LY_ERR
6202 */
6203static LY_ERR
6204moveto_scnode_parent(struct lyxp_set *set, int all_desc, int options)
6205{
6206 int idx, i, orig_used, temp_ctx = 0;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006207 const struct lysc_node *node, *new_node;
6208 enum lyxp_node_type new_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006209 LY_ERR rc;
6210
Michal Vaskod3678892020-05-21 10:06:58 +02006211 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006212 return LY_SUCCESS;
6213 }
6214
6215 if (set->type != LYXP_SET_SCNODE_SET) {
Michal Vaskof6e51882019-12-16 09:59:45 +01006216 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 +02006217 return LY_EVALID;
6218 }
6219
6220 if (all_desc) {
6221 /* <path>//.. == <path>//./.. */
6222 rc = moveto_scnode_self(set, 1, options);
6223 LY_CHECK_RET(rc);
6224 }
6225
Michal Vasko03ff5a72019-09-11 13:49:33 +02006226 orig_used = set->used;
6227 for (i = 0; i < orig_used; ++i) {
6228 if (set->val.scnodes[i].in_ctx != 1) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006229 if (set->val.scnodes[i].in_ctx != -2) {
6230 continue;
6231 }
6232
6233 /* remember context node */
6234 set->val.scnodes[i].in_ctx = -1;
Michal Vaskoec4df482019-12-16 10:02:18 +01006235 } else {
6236 set->val.scnodes[i].in_ctx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006237 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006238
6239 node = set->val.scnodes[i].scnode;
6240
6241 if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
Michal Vaskod3678892020-05-21 10:06:58 +02006242 new_node = lysc_data_parent(node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006243 } else {
6244 /* root does not have a parent */
6245 continue;
6246 }
6247
Michal Vasko03ff5a72019-09-11 13:49:33 +02006248 /* node has no parent */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006249 if (!new_node) {
6250 new_type = set->root_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006251
6252 /* node has a standard parent (it can equal the root, it's not the root yet since they are fake) */
6253 } else {
6254 new_type = LYXP_NODE_ELEM;
6255 }
6256
Michal Vaskoecd62de2019-11-13 12:35:11 +01006257 idx = lyxp_set_scnode_insert_node(set, new_node, new_type);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006258 if ((idx < orig_used) && (idx > i)) {
6259 set->val.scnodes[idx].in_ctx = 2;
6260 temp_ctx = 1;
6261 }
6262 }
6263
6264 if (temp_ctx) {
6265 for (i = 0; i < orig_used; ++i) {
6266 if (set->val.scnodes[i].in_ctx == 2) {
6267 set->val.scnodes[i].in_ctx = 1;
6268 }
6269 }
6270 }
6271
6272 return LY_SUCCESS;
6273}
6274
6275/**
6276 * @brief Move context @p set to the result of a comparison. Handles '=', '!=', '<=', '<', '>=', or '>'.
6277 * Result is LYXP_SET_BOOLEAN. Indirectly context position aware.
6278 *
6279 * @param[in,out] set1 Set to use for the result.
6280 * @param[in] set2 Set acting as the second operand for @p op.
6281 * @param[in] op Comparison operator to process.
6282 * @param[in] options XPath options.
6283 * @return LY_ERR
6284 */
6285static LY_ERR
6286moveto_op_comp(struct lyxp_set *set1, struct lyxp_set *set2, const char *op, int options)
6287{
6288 /*
6289 * NODE SET + NODE SET = NODE SET + STRING /(1 NODE SET) 2 STRING
6290 * NODE SET + STRING = STRING + STRING /1 STRING (2 STRING)
6291 * NODE SET + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6292 * NODE SET + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6293 * STRING + NODE SET = STRING + STRING /(1 STRING) 2 STRING
6294 * NUMBER + NODE SET = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6295 * BOOLEAN + NODE SET = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6296 *
6297 * '=' or '!='
6298 * BOOLEAN + BOOLEAN
6299 * BOOLEAN + STRING = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6300 * BOOLEAN + NUMBER = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6301 * STRING + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6302 * NUMBER + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6303 * NUMBER + NUMBER
6304 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6305 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6306 * STRING + STRING
6307 *
6308 * '<=', '<', '>=', '>'
6309 * NUMBER + NUMBER
6310 * BOOLEAN + BOOLEAN = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6311 * BOOLEAN + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6312 * BOOLEAN + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6313 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6314 * STRING + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6315 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6316 * NUMBER + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6317 * STRING + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6318 */
6319 struct lyxp_set iter1, iter2;
6320 int result;
6321 int64_t i;
6322 LY_ERR rc;
6323
Michal Vaskod3678892020-05-21 10:06:58 +02006324 iter1.type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006325
6326 /* iterative evaluation with node-sets */
6327 if ((set1->type == LYXP_SET_NODE_SET) || (set2->type == LYXP_SET_NODE_SET)) {
6328 if (set1->type == LYXP_SET_NODE_SET) {
6329 for (i = 0; i < set1->used; ++i) {
6330 switch (set2->type) {
6331 case LYXP_SET_NUMBER:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006332 rc = set_comp_cast(&iter1, set1, LYXP_SET_NUMBER, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006333 break;
6334 case LYXP_SET_BOOLEAN:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006335 rc = set_comp_cast(&iter1, set1, LYXP_SET_BOOLEAN, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006336 break;
6337 default:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006338 rc = set_comp_cast(&iter1, set1, LYXP_SET_STRING, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006339 break;
6340 }
6341 LY_CHECK_RET(rc);
6342
6343 rc = moveto_op_comp(&iter1, set2, op, options);
6344 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006345 lyxp_set_free_content(&iter1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006346 return rc;
6347 }
6348
6349 /* lazy evaluation until true */
6350 if (iter1.val.bool) {
6351 set_fill_boolean(set1, 1);
6352 return LY_SUCCESS;
6353 }
6354 }
6355 } else {
6356 for (i = 0; i < set2->used; ++i) {
6357 switch (set1->type) {
6358 case LYXP_SET_NUMBER:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006359 rc = set_comp_cast(&iter2, set2, LYXP_SET_NUMBER, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006360 break;
6361 case LYXP_SET_BOOLEAN:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006362 rc = set_comp_cast(&iter2, set2, LYXP_SET_BOOLEAN, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006363 break;
6364 default:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006365 rc = set_comp_cast(&iter2, set2, LYXP_SET_STRING, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006366 break;
6367 }
6368 LY_CHECK_RET(rc);
6369
6370 set_fill_set(&iter1, set1);
6371
6372 rc = moveto_op_comp(&iter1, &iter2, op, options);
6373 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006374 lyxp_set_free_content(&iter1);
6375 lyxp_set_free_content(&iter2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006376 return rc;
6377 }
Michal Vaskod3678892020-05-21 10:06:58 +02006378 lyxp_set_free_content(&iter2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006379
6380 /* lazy evaluation until true */
6381 if (iter1.val.bool) {
6382 set_fill_boolean(set1, 1);
6383 return LY_SUCCESS;
6384 }
6385 }
6386 }
6387
6388 /* false for all nodes */
6389 set_fill_boolean(set1, 0);
6390 return LY_SUCCESS;
6391 }
6392
6393 /* first convert properly */
6394 if ((op[0] == '=') || (op[0] == '!')) {
6395 if ((set1->type == LYXP_SET_BOOLEAN) || (set2->type == LYXP_SET_BOOLEAN)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006396 lyxp_set_cast(set1, LYXP_SET_BOOLEAN);
6397 lyxp_set_cast(set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006398 } else if ((set1->type == LYXP_SET_NUMBER) || (set2->type == LYXP_SET_NUMBER)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006399 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006400 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006401 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006402 LY_CHECK_RET(rc);
6403 } /* else we have 2 strings */
6404 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006405 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006406 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006407 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006408 LY_CHECK_RET(rc);
6409 }
6410
6411 assert(set1->type == set2->type);
6412
6413 /* compute result */
6414 if (op[0] == '=') {
6415 if (set1->type == LYXP_SET_BOOLEAN) {
6416 result = (set1->val.bool == set2->val.bool);
6417 } else if (set1->type == LYXP_SET_NUMBER) {
6418 result = (set1->val.num == set2->val.num);
6419 } else {
6420 assert(set1->type == LYXP_SET_STRING);
Michal Vaskoac6c72f2019-11-14 16:09:34 +01006421 result = !strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006422 }
6423 } else if (op[0] == '!') {
6424 if (set1->type == LYXP_SET_BOOLEAN) {
6425 result = (set1->val.bool != set2->val.bool);
6426 } else if (set1->type == LYXP_SET_NUMBER) {
6427 result = (set1->val.num != set2->val.num);
6428 } else {
6429 assert(set1->type == LYXP_SET_STRING);
Michal Vaskoac6c72f2019-11-14 16:09:34 +01006430 result = !strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006431 }
6432 } else {
6433 assert(set1->type == LYXP_SET_NUMBER);
6434 if (op[0] == '<') {
6435 if (op[1] == '=') {
6436 result = (set1->val.num <= set2->val.num);
6437 } else {
6438 result = (set1->val.num < set2->val.num);
6439 }
6440 } else {
6441 if (op[1] == '=') {
6442 result = (set1->val.num >= set2->val.num);
6443 } else {
6444 result = (set1->val.num > set2->val.num);
6445 }
6446 }
6447 }
6448
6449 /* assign result */
6450 if (result) {
6451 set_fill_boolean(set1, 1);
6452 } else {
6453 set_fill_boolean(set1, 0);
6454 }
6455
6456 return LY_SUCCESS;
6457}
6458
6459/**
6460 * @brief Move context @p set to the result of a basic operation. Handles '+', '-', unary '-', '*', 'div',
6461 * or 'mod'. Result is LYXP_SET_NUMBER. Indirectly context position aware.
6462 *
6463 * @param[in,out] set1 Set to use for the result.
6464 * @param[in] set2 Set acting as the second operand for @p op.
6465 * @param[in] op Operator to process.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006466 * @return LY_ERR
6467 */
6468static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006469moveto_op_math(struct lyxp_set *set1, struct lyxp_set *set2, const char *op)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006470{
6471 LY_ERR rc;
6472
6473 /* unary '-' */
6474 if (!set2 && (op[0] == '-')) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006475 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006476 LY_CHECK_RET(rc);
6477 set1->val.num *= -1;
6478 lyxp_set_free(set2);
6479 return LY_SUCCESS;
6480 }
6481
6482 assert(set1 && set2);
6483
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006484 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006485 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006486 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006487 LY_CHECK_RET(rc);
6488
6489 switch (op[0]) {
6490 /* '+' */
6491 case '+':
6492 set1->val.num += set2->val.num;
6493 break;
6494
6495 /* '-' */
6496 case '-':
6497 set1->val.num -= set2->val.num;
6498 break;
6499
6500 /* '*' */
6501 case '*':
6502 set1->val.num *= set2->val.num;
6503 break;
6504
6505 /* 'div' */
6506 case 'd':
6507 set1->val.num /= set2->val.num;
6508 break;
6509
6510 /* 'mod' */
6511 case 'm':
6512 set1->val.num = ((long long)set1->val.num) % ((long long)set2->val.num);
6513 break;
6514
6515 default:
6516 LOGINT_RET(set1->ctx);
6517 }
6518
6519 return LY_SUCCESS;
6520}
6521
6522/*
6523 * eval functions
6524 *
6525 * They execute a parsed XPath expression on some data subtree.
6526 */
6527
6528/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02006529 * @brief Evaluate Predicate. Logs directly on error.
6530 *
Michal Vaskod3678892020-05-21 10:06:58 +02006531 * [9] Predicate ::= '[' Expr ']'
Michal Vasko03ff5a72019-09-11 13:49:33 +02006532 *
6533 * @param[in] exp Parsed XPath expression.
6534 * @param[in] exp_idx Position in the expression @p exp.
6535 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
6536 * @param[in] options XPath options.
6537 * @param[in] parent_pos_pred Whether parent predicate was a positional one.
6538 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6539 */
6540static LY_ERR
6541eval_predicate(struct lyxp_expr *exp, uint16_t *exp_idx, struct lyxp_set *set, int options, int parent_pos_pred)
6542{
6543 LY_ERR rc;
Michal Vasko57eab132019-09-24 11:46:26 +02006544 uint16_t i, orig_exp;
Michal Vasko5c4e5892019-11-14 12:31:38 +01006545 uint32_t orig_pos, orig_size;
6546 int32_t pred_in_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006547 struct lyxp_set set2;
6548 struct lyd_node *orig_parent;
6549
6550 /* '[' */
6551 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
6552 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
6553 ++(*exp_idx);
6554
6555 if (!set) {
6556only_parse:
6557 rc = eval_expr_select(exp, exp_idx, 0, NULL, options);
6558 LY_CHECK_RET(rc);
6559 } else if (set->type == LYXP_SET_NODE_SET) {
6560 /* 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 +01006561 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006562
6563 /* empty set, nothing to evaluate */
6564 if (!set->used) {
6565 goto only_parse;
6566 }
6567
6568 orig_exp = *exp_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006569 orig_pos = 0;
6570 orig_size = set->used;
6571 orig_parent = NULL;
Michal Vasko39dbf352020-05-21 10:08:59 +02006572 for (i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006573 set_init(&set2, set);
6574 set_insert_node(&set2, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, 0);
6575 /* remember the node context position for position() and context size for last(),
6576 * predicates should always be evaluated with respect to the child axis (since we do
6577 * not support explicit axes) so we assign positions based on their parents */
6578 if (parent_pos_pred && ((struct lyd_node *)set->val.nodes[i].node->parent != orig_parent)) {
6579 orig_parent = (struct lyd_node *)set->val.nodes[i].node->parent;
6580 orig_pos = 1;
6581 } else {
6582 ++orig_pos;
6583 }
6584
6585 set2.ctx_pos = orig_pos;
6586 set2.ctx_size = orig_size;
6587 *exp_idx = orig_exp;
6588
6589 rc = eval_expr_select(exp, exp_idx, 0, &set2, options);
6590 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006591 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006592 return rc;
6593 }
6594
6595 /* number is a position */
6596 if (set2.type == LYXP_SET_NUMBER) {
6597 if ((long long)set2.val.num == orig_pos) {
6598 set2.val.num = 1;
6599 } else {
6600 set2.val.num = 0;
6601 }
6602 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006603 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006604
6605 /* predicate satisfied or not? */
Michal Vasko57eab132019-09-24 11:46:26 +02006606 if (!set2.val.bool) {
Michal Vasko2caefc12019-11-14 16:07:56 +01006607 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006608 }
6609 }
Michal Vasko2caefc12019-11-14 16:07:56 +01006610 set_remove_nodes_none(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006611
6612 } else if (set->type == LYXP_SET_SCNODE_SET) {
6613 for (i = 0; i < set->used; ++i) {
6614 if (set->val.scnodes[i].in_ctx == 1) {
6615 /* there is a currently-valid node */
6616 break;
6617 }
6618 }
6619 /* empty set, nothing to evaluate */
6620 if (i == set->used) {
6621 goto only_parse;
6622 }
6623
6624 orig_exp = *exp_idx;
6625
Michal Vasko03ff5a72019-09-11 13:49:33 +02006626 /* set special in_ctx to all the valid snodes */
6627 pred_in_ctx = set_scnode_new_in_ctx(set);
6628
6629 /* use the valid snodes one-by-one */
6630 for (i = 0; i < set->used; ++i) {
6631 if (set->val.scnodes[i].in_ctx != pred_in_ctx) {
6632 continue;
6633 }
6634 set->val.scnodes[i].in_ctx = 1;
6635
6636 *exp_idx = orig_exp;
6637
6638 rc = eval_expr_select(exp, exp_idx, 0, set, options);
6639 LY_CHECK_RET(rc);
6640
6641 set->val.scnodes[i].in_ctx = pred_in_ctx;
6642 }
6643
6644 /* restore the state as it was before the predicate */
6645 for (i = 0; i < set->used; ++i) {
6646 if (set->val.scnodes[i].in_ctx == 1) {
6647 set->val.scnodes[i].in_ctx = 0;
6648 } else if (set->val.scnodes[i].in_ctx == pred_in_ctx) {
6649 set->val.scnodes[i].in_ctx = 1;
6650 }
6651 }
6652
6653 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02006654 set2.type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006655 set_fill_set(&set2, set);
6656
6657 rc = eval_expr_select(exp, exp_idx, 0, &set2, options);
6658 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006659 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006660 return rc;
6661 }
6662
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006663 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006664 if (!set2.val.bool) {
Michal Vaskod3678892020-05-21 10:06:58 +02006665 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006666 }
Michal Vaskod3678892020-05-21 10:06:58 +02006667 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006668 }
6669
6670 /* ']' */
6671 assert(exp->tokens[*exp_idx] == LYXP_TOKEN_BRACK2);
6672 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
6673 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
6674 ++(*exp_idx);
6675
6676 return LY_SUCCESS;
6677}
6678
6679/**
Michal Vaskod3678892020-05-21 10:06:58 +02006680 * @brief Evaluate Literal. Logs directly on error.
6681 *
6682 * @param[in] exp Parsed XPath expression.
6683 * @param[in] exp_idx Position in the expression @p exp.
6684 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
6685 */
6686static void
6687eval_literal(struct lyxp_expr *exp, uint16_t *exp_idx, struct lyxp_set *set)
6688{
6689 if (set) {
6690 if (exp->tok_len[*exp_idx] == 2) {
6691 set_fill_string(set, "", 0);
6692 } else {
6693 set_fill_string(set, &exp->expr[exp->tok_pos[*exp_idx] + 1], exp->tok_len[*exp_idx] - 2);
6694 }
6695 }
6696 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
6697 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
6698 ++(*exp_idx);
6699}
6700
6701/**
6702 * @brief Check and parse a predicate following a leaf-list to extract its value.
6703 * On success all the used tokens are skipped.
6704 *
6705 * @param[in] exp Parsed XPath expression.
6706 * @param[in] exp_idx Position in the expression @p exp.
6707 * @param[out] val Leaf-list value on success.
6708 * @param[out] val_len Length of @p val.
6709 * @return LY_ERR
6710 */
6711static LY_ERR
6712eval_name_test_parse_leaflist_predicate(struct lyxp_expr *exp, uint16_t *exp_idx, const char **val, uint16_t *val_len)
6713{
6714 uint16_t cur_idx;
6715
6716 cur_idx = *exp_idx;
6717
6718 /* '[' */
6719 if ((exp->used == cur_idx) || (exp->tokens[cur_idx] != LYXP_TOKEN_BRACK1)) {
6720 return LY_EINVAL;
6721 }
6722 ++cur_idx;
6723
6724 /* '.' */
6725 if ((exp->used == cur_idx) || (exp->tokens[cur_idx] != LYXP_TOKEN_DOT)) {
6726 return LY_EINVAL;
6727 }
6728 ++cur_idx;
6729
6730 /* '=' */
6731 if ((exp->used == cur_idx) || (exp->tokens[cur_idx] != LYXP_TOKEN_OPERATOR_COMP)
6732 || (exp->expr[exp->tok_pos[cur_idx]] != '=')) {
6733 return LY_EINVAL;
6734 }
6735 ++cur_idx;
6736
6737 /* value */
6738 if ((exp->used == cur_idx) || (exp->tokens[cur_idx] != LYXP_TOKEN_LITERAL)) {
6739 return LY_EINVAL;
6740 }
6741 ++cur_idx;
6742
6743 /* ']' */
6744 if ((exp->used == cur_idx) || (exp->tokens[cur_idx] != LYXP_TOKEN_BRACK2)) {
6745 return LY_EINVAL;
6746 }
6747 ++cur_idx;
6748
6749 /* success */
6750 *val = &exp->expr[exp->tok_pos[cur_idx - 2] + 1];
6751 *val_len = exp->tok_len[cur_idx - 2] - 2;
6752 *exp_idx = cur_idx;
6753 return LY_SUCCESS;
6754}
6755
6756/**
6757 * @brief Check and parse a predicate following a list to make sure all the tokens are as expected (all key values defined).
6758 * On success all the used tokens are skipped.
6759 *
6760 * @param[in] exp Parsed XPath expression.
6761 * @param[in] exp_idx Position in the expression @p exp.
6762 * @param[in] slist Schema node of the list.
6763 * @param[out] val Leaf-list value on success.
6764 * @param[out] val_len Length of @p val.
6765 * @return LY_ERR
6766 */
6767static LY_ERR
6768eval_name_test_parse_list_predicate(struct lyxp_expr *exp, uint16_t *exp_idx, const struct lysc_node *slist,
6769 const char **keys, uint16_t *keys_len)
6770{
6771 uint16_t key_count, i, cur_idx;
6772 const struct lysc_node *key;
6773
6774 if (slist->flags & LYS_KEYLESS) {
6775 /* invalid */
6776 return LY_EINVAL;
6777 }
6778
6779 /* get key count */
6780 key_count = 0;
6781 for (key = lysc_node_children(slist, 0); key && (key->flags & LYS_KEY); key = key->next) {
6782 ++key_count;
6783 }
6784
6785 /* briefly check the predicate for each key */
6786 cur_idx = *exp_idx;
6787 for (i = 0; i < key_count; ++i) {
6788 /* '[' */
6789 if ((exp->used == cur_idx) || (exp->tokens[cur_idx] != LYXP_TOKEN_BRACK1)) {
6790 return LY_EINVAL;
6791 }
6792 ++cur_idx;
6793
6794 /* key-name */
6795 if ((exp->used == cur_idx) || (exp->tokens[cur_idx] != LYXP_TOKEN_NAMETEST)) {
6796 return LY_EINVAL;
6797 }
6798 ++cur_idx;
6799
6800 /* '=' */
6801 if ((exp->used == cur_idx) || (exp->tokens[cur_idx] != LYXP_TOKEN_OPERATOR_COMP)
6802 || (exp->expr[exp->tok_pos[cur_idx]] != '=')) {
6803 return LY_EINVAL;
6804 }
6805 ++cur_idx;
6806
6807 /* key-value */
6808 if ((exp->used == cur_idx) || (exp->tokens[cur_idx] != LYXP_TOKEN_LITERAL)) {
6809 return LY_EINVAL;
6810 }
6811 ++cur_idx;
6812
6813 /* ']' */
6814 if ((exp->used == cur_idx) || (exp->tokens[cur_idx] != LYXP_TOKEN_BRACK2)) {
6815 return LY_EINVAL;
6816 }
6817 ++cur_idx;
6818 }
6819
6820 /* success */
6821 *keys = &exp->expr[exp->tok_pos[*exp_idx]];
6822 *keys_len = (exp->expr + exp->tok_pos[cur_idx - 1] + exp->tok_len[cur_idx - 1]) - *keys;
6823 *exp_idx = cur_idx;
6824 return LY_SUCCESS;
6825}
6826
6827/**
6828 * @brief Evaluate NameTest and any following Predicates. Logs directly on error.
6829 *
6830 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
6831 * [6] NodeTest ::= NameTest | NodeType '(' ')'
6832 * [7] NameTest ::= '*' | NCName ':' '*' | QName
6833 *
6834 * @param[in] exp Parsed XPath expression.
6835 * @param[in] exp_idx Position in the expression @p exp.
6836 * @param[in] attr_axis Whether to search attributes or standard nodes.
6837 * @param[in] all_desc Whether to search all the descendants or children only.
6838 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
6839 * @param[in] options XPath options.
6840 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6841 */
6842static LY_ERR
6843eval_name_test_with_predicate(struct lyxp_expr *exp, uint16_t *exp_idx, int attr_axis, int all_desc, struct lyxp_set *set,
6844 int options)
6845{
6846 int i;
6847 char *path;
6848 const char *ncname = NULL, *key_val = NULL;
6849 uint16_t ncname_len, key_val_len, prev_exp_idx;
6850 const struct lys_module *moveto_mod;
6851 const struct lysc_node *scnode = NULL, *tmp;
6852 struct lyd_node *list_inst = NULL;
6853 LY_ERR rc = LY_SUCCESS;
6854
6855 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
6856 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
6857 ++(*exp_idx);
6858
6859 if (!set) {
6860 goto moveto;
6861 }
6862
6863 ncname = &exp->expr[exp->tok_pos[*exp_idx - 1]];
6864 ncname_len = exp->tok_len[*exp_idx - 1];
6865
6866 /* parse (and skip) module name */
6867 rc = moveto_resolve_model(&ncname, &ncname_len, set, &moveto_mod);
6868 LY_CHECK_GOTO(rc, cleanup);
6869
6870 if (moveto_mod && !attr_axis && !all_desc && (set->type == LYXP_SET_NODE_SET)) {
6871 /* find the matching schema node in some parent in the context */
6872 for (i = 0; i < (signed)set->used; ++i) {
6873 if (set->val.nodes[i].type == set->root_type) {
6874 tmp = lys_find_child(NULL, moveto_mod, ncname, ncname_len, 0, 0);
6875 } else if ((set->val.nodes[i].type == LYXP_NODE_ELEM)
6876 && (!scnode || (lysc_data_parent(scnode) != set->val.nodes[i].node->schema))) {
6877 /* do not repeat the same search */
6878 tmp = lys_find_child(set->val.nodes[i].node->schema, moveto_mod, ncname, ncname_len, 0, 0);
6879 }
6880
6881 /* additional context check */
6882 if (tmp && (set->root_type == LYXP_NODE_ROOT_CONFIG) && (tmp->flags & LYS_CONFIG_R)) {
6883 tmp = NULL;
6884 }
6885
6886 if (tmp) {
6887 if (scnode) {
6888 /* we found a schema node with the same name but at different level, give up, too complicated */
6889 scnode = NULL;
6890 break;
6891 } else {
6892 /* remember the found schema node and continue to make sure it can be used */
6893 scnode = tmp;
6894 }
6895 tmp = NULL;
6896 }
6897 }
6898
6899 if (scnode && (scnode->nodetype == LYS_LIST)) {
6900 /* make sure all the tokens are right */
6901 prev_exp_idx = *exp_idx;
6902 if (eval_name_test_parse_list_predicate(exp, exp_idx, scnode, &key_val, &key_val_len)) {
6903 scnode = NULL;
6904 }
6905
6906 /* try to create a list instance */
6907 if (scnode && lyd_create_list(scnode, key_val, key_val_len, set->format, 0, &list_inst)) {
6908 /* for whatever reason the list failed to be created, just use standard name comparison and
6909 * parse predicate normally */
6910 *exp_idx = prev_exp_idx;
6911 scnode = NULL;
6912 }
6913 } else if (scnode && (scnode->nodetype == LYS_LEAFLIST)) {
6914 /* make sure we know the leaf-list value */
6915 if (eval_name_test_parse_leaflist_predicate(exp, exp_idx, &key_val, &key_val_len)) {
6916 /* value could not be recognized, use standard name comparison */
6917 scnode = NULL;
6918 }
6919 }
6920 }
6921
6922 if (!scnode) {
6923 /* we are not able to match based on a schema node */
6924 if (!moveto_mod) {
6925 /* '*', all nodes match */
6926 ncname = NULL;
6927 } else {
6928 /* insert name into dictionary for efficient comparison */
6929 ncname = lydict_insert(set->ctx, ncname, ncname_len);
6930 }
6931 }
6932
6933moveto:
6934 /* move to the attribute(s), data node(s), or schema node(s) */
6935 if (attr_axis) {
6936 if (set && (options & LYXP_SCNODE_ALL)) {
6937 set_scnode_clear_ctx(set);
6938 } else {
6939 if (all_desc) {
6940 rc = moveto_attr_alldesc(set, moveto_mod, ncname);
6941 } else {
6942 rc = moveto_attr(set, moveto_mod, ncname);
6943 }
6944 LY_CHECK_GOTO(rc, cleanup);
6945 }
6946 } else {
6947 if (set && (options & LYXP_SCNODE_ALL)) {
6948 if (all_desc) {
6949 rc = moveto_scnode_alldesc(set, moveto_mod, ncname, options);
6950 } else {
6951 rc = moveto_scnode(set, moveto_mod, ncname, options);
6952 }
6953 LY_CHECK_GOTO(rc, cleanup);
6954
6955 for (i = set->used - 1; i > -1; --i) {
6956 if (set->val.scnodes[i].in_ctx > 0) {
6957 break;
6958 }
6959 }
6960 if (i == -1) {
6961 path = lysc_path(set->ctx_scnode, LYSC_PATH_LOG, NULL, 0);
6962 LOGWRN(set->ctx, "Schema node \"%.*s\" not found (%.*s) with context node \"%s\".",
6963 exp->tok_len[*exp_idx], &exp->expr[exp->tok_pos[*exp_idx]],
6964 exp->tok_pos[*exp_idx] + exp->tok_len[*exp_idx], exp->expr, path);
6965 free(path);
6966 }
6967 } else {
6968 if (all_desc) {
6969 rc = moveto_node_alldesc(set, moveto_mod, ncname);
6970 } else {
6971 if (scnode) {
6972 /* we can find the nodes using hashes */
6973 rc = moveto_node_hash(set, scnode, list_inst, key_val, key_val_len);
6974 } else {
6975 rc = moveto_node(set, moveto_mod, ncname);
6976 }
6977 }
6978 LY_CHECK_GOTO(rc, cleanup);
6979 }
6980 }
6981
6982 /* Predicate* */
6983 while ((exp->used > *exp_idx) && (exp->tokens[*exp_idx] == LYXP_TOKEN_BRACK1)) {
6984 rc = eval_predicate(exp, exp_idx, set, options, 1);
6985 LY_CHECK_RET(rc);
6986 }
6987
6988cleanup:
6989 lydict_remove(set->ctx, ncname);
6990 lyd_free_tree(list_inst);
6991 return rc;
6992}
6993
6994/**
6995 * @brief Evaluate NodeType and any following Predicates. Logs directly on error.
6996 *
6997 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
6998 * [6] NodeTest ::= NameTest | NodeType '(' ')'
6999 * [8] NodeType ::= 'text' | 'node'
7000 *
7001 * @param[in] exp Parsed XPath expression.
7002 * @param[in] exp_idx Position in the expression @p exp.
7003 * @param[in] attr_axis Whether to search attributes or standard nodes.
7004 * @param[in] all_desc Whether to search all the descendants or children only.
7005 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7006 * @param[in] options XPath options.
7007 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7008 */
7009static LY_ERR
7010eval_node_type_with_predicate(struct lyxp_expr *exp, uint16_t *exp_idx, int attr_axis, int all_desc,
7011 struct lyxp_set *set, int options)
7012{
7013 LY_ERR rc;
7014
7015 /* TODO */
7016 (void)attr_axis;
7017 (void)all_desc;
7018
7019 if (set) {
7020 assert(exp->tok_len[*exp_idx] == 4);
7021 if (set->type == LYXP_SET_SCNODE_SET) {
7022 set_scnode_clear_ctx(set);
7023 /* just for the debug message below */
7024 set = NULL;
7025 } else {
7026 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "node", 4)) {
7027 rc = xpath_node(NULL, 0, set, options);
7028 } else {
7029 assert(!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "text", 4));
7030 rc = xpath_text(NULL, 0, set, options);
7031 }
7032 LY_CHECK_RET(rc);
7033 }
7034 }
7035 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7036 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7037 ++(*exp_idx);
7038
7039 /* '(' */
7040 assert(exp->tokens[*exp_idx] == LYXP_TOKEN_PAR1);
7041 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7042 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7043 ++(*exp_idx);
7044
7045 /* ')' */
7046 assert(exp->tokens[*exp_idx] == LYXP_TOKEN_PAR2);
7047 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7048 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7049 ++(*exp_idx);
7050
7051 /* Predicate* */
7052 while ((exp->used > *exp_idx) && (exp->tokens[*exp_idx] == LYXP_TOKEN_BRACK1)) {
7053 rc = eval_predicate(exp, exp_idx, set, options, 1);
7054 LY_CHECK_RET(rc);
7055 }
7056
7057 return LY_SUCCESS;
7058}
7059
7060/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02007061 * @brief Evaluate RelativeLocationPath. Logs directly on error.
7062 *
7063 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
7064 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
Michal Vaskod3678892020-05-21 10:06:58 +02007065 * [6] NodeTest ::= NameTest | NodeType '(' ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007066 *
7067 * @param[in] exp Parsed XPath expression.
7068 * @param[in] exp_idx Position in the expression @p exp.
7069 * @param[in] all_desc Whether to search all the descendants or children only.
7070 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7071 * @param[in] options XPath options.
7072 * @return LY_ERR (YL_EINCOMPLETE on unresolved when)
7073 */
7074static LY_ERR
7075eval_relative_location_path(struct lyxp_expr *exp, uint16_t *exp_idx, int all_desc, struct lyxp_set *set, int options)
7076{
7077 int attr_axis;
7078 LY_ERR rc;
7079
7080 goto step;
7081 do {
7082 /* evaluate '/' or '//' */
7083 if (exp->tok_len[*exp_idx] == 1) {
7084 all_desc = 0;
7085 } else {
7086 assert(exp->tok_len[*exp_idx] == 2);
7087 all_desc = 1;
7088 }
7089 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7090 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7091 ++(*exp_idx);
7092
7093step:
Michal Vaskod3678892020-05-21 10:06:58 +02007094 /* evaluate abbreviated axis '@'? if any */
7095 if (exp->tokens[*exp_idx] == LYXP_TOKEN_AT) {
7096 attr_axis = 1;
7097
7098 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7099 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7100 ++(*exp_idx);
7101 } else {
7102 attr_axis = 0;
7103 }
7104
Michal Vasko03ff5a72019-09-11 13:49:33 +02007105 /* Step */
Michal Vasko03ff5a72019-09-11 13:49:33 +02007106 switch (exp->tokens[*exp_idx]) {
7107 case LYXP_TOKEN_DOT:
7108 /* evaluate '.' */
7109 if (set && (options & LYXP_SCNODE_ALL)) {
7110 rc = moveto_scnode_self(set, all_desc, options);
7111 } else {
7112 rc = moveto_self(set, all_desc, options);
7113 }
7114 LY_CHECK_RET(rc);
7115 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7116 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7117 ++(*exp_idx);
7118 break;
7119
7120 case LYXP_TOKEN_DDOT:
7121 /* evaluate '..' */
7122 if (set && (options & LYXP_SCNODE_ALL)) {
7123 rc = moveto_scnode_parent(set, all_desc, options);
7124 } else {
7125 rc = moveto_parent(set, all_desc, options);
7126 }
7127 LY_CHECK_RET(rc);
7128 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7129 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7130 ++(*exp_idx);
7131 break;
7132
Michal Vasko03ff5a72019-09-11 13:49:33 +02007133 case LYXP_TOKEN_NAMETEST:
Michal Vaskod3678892020-05-21 10:06:58 +02007134 /* evaluate NameTest Predicate* */
7135 rc = eval_name_test_with_predicate(exp, exp_idx, attr_axis, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007136 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02007137 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007138
Michal Vaskod3678892020-05-21 10:06:58 +02007139 case LYXP_TOKEN_NODETYPE:
7140 /* evaluate NodeType Predicate* */
7141 rc = eval_node_type_with_predicate(exp, exp_idx, attr_axis, all_desc, set, options);
7142 LY_CHECK_RET(rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007143 break;
7144
7145 default:
Michal Vasko02a77382019-09-12 11:47:35 +02007146 LOGINT_RET(set ? set->ctx : NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007147 }
7148 } while ((exp->used > *exp_idx) && (exp->tokens[*exp_idx] == LYXP_TOKEN_OPERATOR_PATH));
7149
7150 return LY_SUCCESS;
7151}
7152
7153/**
7154 * @brief Evaluate AbsoluteLocationPath. Logs directly on error.
7155 *
7156 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
7157 *
7158 * @param[in] exp Parsed XPath expression.
7159 * @param[in] exp_idx Position in the expression @p exp.
7160 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7161 * @param[in] options XPath options.
7162 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7163 */
7164static LY_ERR
7165eval_absolute_location_path(struct lyxp_expr *exp, uint16_t *exp_idx, struct lyxp_set *set, int options)
7166{
7167 int all_desc;
7168 LY_ERR rc;
7169
7170 if (set) {
7171 /* no matter what tokens follow, we need to be at the root */
7172 moveto_root(set, options);
7173 }
7174
7175 /* '/' RelativeLocationPath? */
7176 if (exp->tok_len[*exp_idx] == 1) {
7177 /* evaluate '/' - deferred */
7178 all_desc = 0;
7179 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7180 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7181 ++(*exp_idx);
7182
Michal Vasko4b9e1052019-09-13 11:25:37 +02007183 if (exp_check_token(set ? set->ctx : NULL, exp, *exp_idx, LYXP_TOKEN_NONE, 0)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007184 return LY_SUCCESS;
7185 }
7186 switch (exp->tokens[*exp_idx]) {
7187 case LYXP_TOKEN_DOT:
7188 case LYXP_TOKEN_DDOT:
7189 case LYXP_TOKEN_AT:
7190 case LYXP_TOKEN_NAMETEST:
7191 case LYXP_TOKEN_NODETYPE:
7192 rc = eval_relative_location_path(exp, exp_idx, all_desc, set, options);
7193 LY_CHECK_RET(rc);
7194 break;
7195 default:
7196 break;
7197 }
7198
7199 /* '//' RelativeLocationPath */
7200 } else {
7201 /* evaluate '//' - deferred so as not to waste memory by remembering all the nodes */
7202 all_desc = 1;
7203 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7204 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7205 ++(*exp_idx);
7206
7207 rc = eval_relative_location_path(exp, exp_idx, all_desc, set, options);
7208 LY_CHECK_RET(rc);
7209 }
7210
7211 return LY_SUCCESS;
7212}
7213
7214/**
7215 * @brief Evaluate FunctionCall. Logs directly on error.
7216 *
Michal Vaskod3678892020-05-21 10:06:58 +02007217 * [11] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007218 *
7219 * @param[in] exp Parsed XPath expression.
7220 * @param[in] exp_idx Position in the expression @p exp.
7221 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7222 * @param[in] options XPath options.
7223 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7224 */
7225static LY_ERR
7226eval_function_call(struct lyxp_expr *exp, uint16_t *exp_idx, struct lyxp_set *set, int options)
7227{
7228 LY_ERR rc;
7229 LY_ERR (*xpath_func)(struct lyxp_set **, uint16_t, struct lyxp_set *, int) = NULL;
Michal Vasko0cbf54f2019-12-16 10:01:06 +01007230 uint16_t arg_count = 0, i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007231 struct lyxp_set **args = NULL, **args_aux;
7232
7233 if (set) {
7234 /* FunctionName */
7235 switch (exp->tok_len[*exp_idx]) {
7236 case 3:
7237 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "not", 3)) {
7238 xpath_func = &xpath_not;
7239 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "sum", 3)) {
7240 xpath_func = &xpath_sum;
7241 }
7242 break;
7243 case 4:
7244 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "lang", 4)) {
7245 xpath_func = &xpath_lang;
7246 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "last", 4)) {
7247 xpath_func = &xpath_last;
7248 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "name", 4)) {
7249 xpath_func = &xpath_name;
7250 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "true", 4)) {
7251 xpath_func = &xpath_true;
7252 }
7253 break;
7254 case 5:
7255 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "count", 5)) {
7256 xpath_func = &xpath_count;
7257 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "false", 5)) {
7258 xpath_func = &xpath_false;
7259 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "floor", 5)) {
7260 xpath_func = &xpath_floor;
7261 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "round", 5)) {
7262 xpath_func = &xpath_round;
7263 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "deref", 5)) {
7264 xpath_func = &xpath_deref;
7265 }
7266 break;
7267 case 6:
7268 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "concat", 6)) {
7269 xpath_func = &xpath_concat;
7270 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "number", 6)) {
7271 xpath_func = &xpath_number;
7272 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "string", 6)) {
7273 xpath_func = &xpath_string;
7274 }
7275 break;
7276 case 7:
7277 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "boolean", 7)) {
7278 xpath_func = &xpath_boolean;
7279 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "ceiling", 7)) {
7280 xpath_func = &xpath_ceiling;
7281 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "current", 7)) {
7282 xpath_func = &xpath_current;
7283 }
7284 break;
7285 case 8:
7286 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "contains", 8)) {
7287 xpath_func = &xpath_contains;
7288 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "position", 8)) {
7289 xpath_func = &xpath_position;
7290 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "re-match", 8)) {
7291 xpath_func = &xpath_re_match;
7292 }
7293 break;
7294 case 9:
7295 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "substring", 9)) {
7296 xpath_func = &xpath_substring;
7297 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "translate", 9)) {
7298 xpath_func = &xpath_translate;
7299 }
7300 break;
7301 case 10:
7302 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "local-name", 10)) {
7303 xpath_func = &xpath_local_name;
7304 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "enum-value", 10)) {
7305 xpath_func = &xpath_enum_value;
7306 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "bit-is-set", 10)) {
7307 xpath_func = &xpath_bit_is_set;
7308 }
7309 break;
7310 case 11:
7311 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "starts-with", 11)) {
7312 xpath_func = &xpath_starts_with;
7313 }
7314 break;
7315 case 12:
7316 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "derived-from", 12)) {
7317 xpath_func = &xpath_derived_from;
7318 }
7319 break;
7320 case 13:
7321 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "namespace-uri", 13)) {
7322 xpath_func = &xpath_namespace_uri;
7323 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "string-length", 13)) {
7324 xpath_func = &xpath_string_length;
7325 }
7326 break;
7327 case 15:
7328 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "normalize-space", 15)) {
7329 xpath_func = &xpath_normalize_space;
7330 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "substring-after", 15)) {
7331 xpath_func = &xpath_substring_after;
7332 }
7333 break;
7334 case 16:
7335 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "substring-before", 16)) {
7336 xpath_func = &xpath_substring_before;
7337 }
7338 break;
7339 case 20:
7340 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "derived-from-or-self", 20)) {
7341 xpath_func = &xpath_derived_from_or_self;
7342 }
7343 break;
7344 }
7345
7346 if (!xpath_func) {
7347 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*exp_idx]]);
7348 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]]);
7349 return LY_EVALID;
7350 }
7351 }
7352
7353 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7354 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7355 ++(*exp_idx);
7356
7357 /* '(' */
7358 assert(exp->tokens[*exp_idx] == LYXP_TOKEN_PAR1);
7359 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7360 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7361 ++(*exp_idx);
7362
7363 /* ( Expr ( ',' Expr )* )? */
7364 if (exp->tokens[*exp_idx] != LYXP_TOKEN_PAR2) {
7365 if (set) {
7366 args = malloc(sizeof *args);
7367 LY_CHECK_ERR_GOTO(!args, LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7368 arg_count = 1;
7369 args[0] = set_copy(set);
7370 if (!args[0]) {
7371 rc = LY_EMEM;
7372 goto cleanup;
7373 }
7374
7375 rc = eval_expr_select(exp, exp_idx, 0, args[0], options);
7376 LY_CHECK_GOTO(rc, cleanup);
7377 } else {
7378 rc = eval_expr_select(exp, exp_idx, 0, NULL, options);
7379 LY_CHECK_GOTO(rc, cleanup);
7380 }
7381 }
7382 while ((exp->used > *exp_idx) && (exp->tokens[*exp_idx] == LYXP_TOKEN_COMMA)) {
7383 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7384 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7385 ++(*exp_idx);
7386
7387 if (set) {
7388 ++arg_count;
7389 args_aux = realloc(args, arg_count * sizeof *args);
7390 LY_CHECK_ERR_GOTO(!args_aux, arg_count--; LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7391 args = args_aux;
7392 args[arg_count - 1] = set_copy(set);
7393 if (!args[arg_count - 1]) {
7394 rc = LY_EMEM;
7395 goto cleanup;
7396 }
7397
7398 rc = eval_expr_select(exp, exp_idx, 0, args[arg_count - 1], options);
7399 LY_CHECK_GOTO(rc, cleanup);
7400 } else {
7401 rc = eval_expr_select(exp, exp_idx, 0, NULL, options);
7402 LY_CHECK_GOTO(rc, cleanup);
7403 }
7404 }
7405
7406 /* ')' */
7407 assert(exp->tokens[*exp_idx] == LYXP_TOKEN_PAR2);
7408 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7409 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7410 ++(*exp_idx);
7411
7412 if (set) {
7413 /* evaluate function */
7414 rc = xpath_func(args, arg_count, set, options);
7415
7416 if (options & LYXP_SCNODE_ALL) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007417 /* merge all nodes from arg evaluations */
7418 for (i = 0; i < arg_count; ++i) {
7419 set_scnode_clear_ctx(args[i]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007420 lyxp_set_scnode_merge(set, args[i]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007421 }
7422 }
7423 } else {
7424 rc = LY_SUCCESS;
7425 }
7426
7427cleanup:
7428 for (i = 0; i < arg_count; ++i) {
7429 lyxp_set_free(args[i]);
7430 }
7431 free(args);
7432
7433 return rc;
7434}
7435
7436/**
7437 * @brief Evaluate Number. Logs directly on error.
7438 *
7439 * @param[in] ctx Context for errors.
7440 * @param[in] exp Parsed XPath expression.
7441 * @param[in] exp_idx Position in the expression @p exp.
7442 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7443 * @return LY_ERR
7444 */
7445static LY_ERR
7446eval_number(struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *exp_idx, struct lyxp_set *set)
7447{
7448 long double num;
7449 char *endptr;
7450
7451 if (set) {
7452 errno = 0;
7453 num = strtold(&exp->expr[exp->tok_pos[*exp_idx]], &endptr);
7454 if (errno) {
7455 LOGVAL(ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*exp_idx]]);
7456 LOGVAL(ctx, LY_VLOG_LYD, set->ctx_node, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double (%s).",
7457 exp->tok_len[*exp_idx], &exp->expr[exp->tok_pos[*exp_idx]], strerror(errno));
7458 return LY_EVALID;
7459 } else if (endptr - &exp->expr[exp->tok_pos[*exp_idx]] != exp->tok_len[*exp_idx]) {
7460 LOGVAL(ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*exp_idx]]);
7461 LOGVAL(ctx, LY_VLOG_LYD, set->ctx_node, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double.",
7462 exp->tok_len[*exp_idx], &exp->expr[exp->tok_pos[*exp_idx]]);
7463 return LY_EVALID;
7464 }
7465
7466 set_fill_number(set, num);
7467 }
7468
7469 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7470 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7471 ++(*exp_idx);
7472 return LY_SUCCESS;
7473}
7474
7475/**
7476 * @brief Evaluate PathExpr. Logs directly on error.
7477 *
Michal Vaskod3678892020-05-21 10:06:58 +02007478 * [12] PathExpr ::= LocationPath | PrimaryExpr Predicate*
Michal Vasko03ff5a72019-09-11 13:49:33 +02007479 * | PrimaryExpr Predicate* '/' RelativeLocationPath
7480 * | PrimaryExpr Predicate* '//' RelativeLocationPath
7481 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
Michal Vaskod3678892020-05-21 10:06:58 +02007482 * [10] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall
Michal Vasko03ff5a72019-09-11 13:49:33 +02007483 *
7484 * @param[in] exp Parsed XPath expression.
7485 * @param[in] exp_idx Position in the expression @p exp.
7486 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7487 * @param[in] options XPath options.
7488 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7489 */
7490static LY_ERR
7491eval_path_expr(struct lyxp_expr *exp, uint16_t *exp_idx, struct lyxp_set *set, int options)
7492{
7493 int all_desc, parent_pos_pred;
7494 LY_ERR rc;
7495
7496 switch (exp->tokens[*exp_idx]) {
7497 case LYXP_TOKEN_PAR1:
7498 /* '(' Expr ')' */
7499
7500 /* '(' */
7501 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7502 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7503 ++(*exp_idx);
7504
7505 /* Expr */
7506 rc = eval_expr_select(exp, exp_idx, 0, set, options);
7507 LY_CHECK_RET(rc);
7508
7509 /* ')' */
7510 assert(exp->tokens[*exp_idx] == LYXP_TOKEN_PAR2);
7511 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7512 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7513 ++(*exp_idx);
7514
7515 parent_pos_pred = 0;
7516 goto predicate;
7517
7518 case LYXP_TOKEN_DOT:
7519 case LYXP_TOKEN_DDOT:
7520 case LYXP_TOKEN_AT:
7521 case LYXP_TOKEN_NAMETEST:
7522 case LYXP_TOKEN_NODETYPE:
7523 /* RelativeLocationPath */
7524 rc = eval_relative_location_path(exp, exp_idx, 0, set, options);
7525 LY_CHECK_RET(rc);
7526 break;
7527
7528 case LYXP_TOKEN_FUNCNAME:
7529 /* FunctionCall */
7530 if (!set) {
7531 rc = eval_function_call(exp, exp_idx, NULL, options);
7532 } else {
7533 rc = eval_function_call(exp, exp_idx, set, options);
7534 }
7535 LY_CHECK_RET(rc);
7536
7537 parent_pos_pred = 1;
7538 goto predicate;
7539
7540 case LYXP_TOKEN_OPERATOR_PATH:
7541 /* AbsoluteLocationPath */
7542 rc = eval_absolute_location_path(exp, exp_idx, set, options);
7543 LY_CHECK_RET(rc);
7544 break;
7545
7546 case LYXP_TOKEN_LITERAL:
7547 /* Literal */
7548 if (!set || (options & LYXP_SCNODE_ALL)) {
7549 if (set) {
7550 set_scnode_clear_ctx(set);
7551 }
7552 eval_literal(exp, exp_idx, NULL);
7553 } else {
7554 eval_literal(exp, exp_idx, set);
7555 }
7556
7557 parent_pos_pred = 1;
7558 goto predicate;
7559
7560 case LYXP_TOKEN_NUMBER:
7561 /* Number */
7562 if (!set || (options & LYXP_SCNODE_ALL)) {
7563 if (set) {
7564 set_scnode_clear_ctx(set);
7565 }
Michal Vasko02a77382019-09-12 11:47:35 +02007566 rc = eval_number(NULL, exp, exp_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007567 } else {
7568 rc = eval_number(set->ctx, exp, exp_idx, set);
7569 }
7570 LY_CHECK_RET(rc);
7571
7572 parent_pos_pred = 1;
7573 goto predicate;
7574
7575 default:
7576 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INTOK, print_token(exp->tokens[*exp_idx]),
7577 &exp->expr[exp->tok_pos[*exp_idx]]);
7578 return LY_EVALID;
7579 }
7580
7581 return LY_SUCCESS;
7582
7583predicate:
7584 /* Predicate* */
7585 while ((exp->used > *exp_idx) && (exp->tokens[*exp_idx] == LYXP_TOKEN_BRACK1)) {
7586 rc = eval_predicate(exp, exp_idx, set, options, parent_pos_pred);
7587 LY_CHECK_RET(rc);
7588 }
7589
7590 /* ('/' or '//') RelativeLocationPath */
7591 if ((exp->used > *exp_idx) && (exp->tokens[*exp_idx] == LYXP_TOKEN_OPERATOR_PATH)) {
7592
7593 /* evaluate '/' or '//' */
7594 if (exp->tok_len[*exp_idx] == 1) {
7595 all_desc = 0;
7596 } else {
7597 assert(exp->tok_len[*exp_idx] == 2);
7598 all_desc = 1;
7599 }
7600
7601 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7602 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7603 ++(*exp_idx);
7604
7605 rc = eval_relative_location_path(exp, exp_idx, all_desc, set, options);
7606 LY_CHECK_RET(rc);
7607 }
7608
7609 return LY_SUCCESS;
7610}
7611
7612/**
7613 * @brief Evaluate UnionExpr. Logs directly on error.
7614 *
Michal Vaskod3678892020-05-21 10:06:58 +02007615 * [20] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007616 *
7617 * @param[in] exp Parsed XPath expression.
7618 * @param[in] exp_idx Position in the expression @p exp.
7619 * @param[in] repeat How many times this expression is repeated.
7620 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7621 * @param[in] options XPath options.
7622 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7623 */
7624static LY_ERR
7625eval_union_expr(struct lyxp_expr *exp, uint16_t *exp_idx, uint16_t repeat, struct lyxp_set *set, int options)
7626{
7627 LY_ERR rc = LY_SUCCESS;
7628 struct lyxp_set orig_set, set2;
7629 uint16_t i;
7630
7631 assert(repeat);
7632
7633 set_init(&orig_set, set);
7634 set_init(&set2, set);
7635
7636 set_fill_set(&orig_set, set);
7637
7638 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_UNION, set, options);
7639 LY_CHECK_GOTO(rc, cleanup);
7640
7641 /* ('|' PathExpr)* */
7642 for (i = 0; i < repeat; ++i) {
7643 assert(exp->tokens[*exp_idx] == LYXP_TOKEN_OPERATOR_UNI);
7644 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7645 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7646 ++(*exp_idx);
7647
7648 if (!set) {
7649 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_UNION, NULL, options);
7650 LY_CHECK_GOTO(rc, cleanup);
7651 continue;
7652 }
7653
7654 set_fill_set(&set2, &orig_set);
7655 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_UNION, &set2, options);
7656 LY_CHECK_GOTO(rc, cleanup);
7657
7658 /* eval */
7659 if (options & LYXP_SCNODE_ALL) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01007660 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007661 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007662 rc = moveto_union(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007663 LY_CHECK_GOTO(rc, cleanup);
7664 }
7665 }
7666
7667cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007668 lyxp_set_free_content(&orig_set);
7669 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007670 return rc;
7671}
7672
7673/**
7674 * @brief Evaluate UnaryExpr. Logs directly on error.
7675 *
Michal Vaskod3678892020-05-21 10:06:58 +02007676 * [19] UnaryExpr ::= UnionExpr | '-' UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007677 *
7678 * @param[in] exp Parsed XPath expression.
7679 * @param[in] exp_idx Position in the expression @p exp.
7680 * @param[in] repeat How many times this expression is repeated.
7681 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7682 * @param[in] options XPath options.
7683 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7684 */
7685static LY_ERR
7686eval_unary_expr(struct lyxp_expr *exp, uint16_t *exp_idx, uint16_t repeat, struct lyxp_set *set, int options)
7687{
7688 LY_ERR rc;
7689 uint16_t this_op, i;
7690
7691 assert(repeat);
7692
7693 /* ('-')+ */
7694 this_op = *exp_idx;
7695 for (i = 0; i < repeat; ++i) {
7696 assert(!exp_check_token(set->ctx, exp, *exp_idx, LYXP_TOKEN_OPERATOR_MATH, 0) && (exp->expr[exp->tok_pos[*exp_idx]] == '-'));
7697
7698 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7699 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7700 ++(*exp_idx);
7701 }
7702
7703 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_UNARY, set, options);
7704 LY_CHECK_RET(rc);
7705
7706 if (set && (repeat % 2)) {
7707 if (options & LYXP_SCNODE_ALL) {
7708 warn_operands(set->ctx, set, NULL, 1, exp->expr, exp->tok_pos[this_op]);
7709 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007710 rc = moveto_op_math(set, NULL, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007711 LY_CHECK_RET(rc);
7712 }
7713 }
7714
7715 return LY_SUCCESS;
7716}
7717
7718/**
7719 * @brief Evaluate MultiplicativeExpr. Logs directly on error.
7720 *
Michal Vaskod3678892020-05-21 10:06:58 +02007721 * [18] MultiplicativeExpr ::= UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007722 * | MultiplicativeExpr '*' UnaryExpr
7723 * | MultiplicativeExpr 'div' UnaryExpr
7724 * | MultiplicativeExpr 'mod' UnaryExpr
7725 *
7726 * @param[in] exp Parsed XPath expression.
7727 * @param[in] exp_idx Position in the expression @p exp.
7728 * @param[in] repeat How many times this expression is repeated.
7729 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7730 * @param[in] options XPath options.
7731 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7732 */
7733static LY_ERR
7734eval_multiplicative_expr(struct lyxp_expr *exp, uint16_t *exp_idx, uint16_t repeat, struct lyxp_set *set, int options)
7735{
7736 LY_ERR rc;
7737 uint16_t this_op;
7738 struct lyxp_set orig_set, set2;
7739 uint16_t i;
7740
7741 assert(repeat);
7742
7743 set_init(&orig_set, set);
7744 set_init(&set2, set);
7745
7746 set_fill_set(&orig_set, set);
7747
7748 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_MULTIPLICATIVE, set, options);
7749 LY_CHECK_GOTO(rc, cleanup);
7750
7751 /* ('*' / 'div' / 'mod' UnaryExpr)* */
7752 for (i = 0; i < repeat; ++i) {
7753 this_op = *exp_idx;
7754
7755 assert(exp->tokens[*exp_idx] == LYXP_TOKEN_OPERATOR_MATH);
7756 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7757 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7758 ++(*exp_idx);
7759
7760 if (!set) {
7761 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_MULTIPLICATIVE, NULL, options);
7762 LY_CHECK_GOTO(rc, cleanup);
7763 continue;
7764 }
7765
7766 set_fill_set(&set2, &orig_set);
7767 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_MULTIPLICATIVE, &set2, options);
7768 LY_CHECK_GOTO(rc, cleanup);
7769
7770 /* eval */
7771 if (options & LYXP_SCNODE_ALL) {
7772 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007773 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007774 set_scnode_clear_ctx(set);
7775 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007776 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007777 LY_CHECK_GOTO(rc, cleanup);
7778 }
7779 }
7780
7781cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007782 lyxp_set_free_content(&orig_set);
7783 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007784 return rc;
7785}
7786
7787/**
7788 * @brief Evaluate AdditiveExpr. Logs directly on error.
7789 *
Michal Vaskod3678892020-05-21 10:06:58 +02007790 * [17] AdditiveExpr ::= MultiplicativeExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007791 * | AdditiveExpr '+' MultiplicativeExpr
7792 * | AdditiveExpr '-' MultiplicativeExpr
7793 *
7794 * @param[in] exp Parsed XPath expression.
7795 * @param[in] exp_idx Position in the expression @p exp.
7796 * @param[in] repeat How many times this expression is repeated.
7797 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7798 * @param[in] options XPath options.
7799 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7800 */
7801static LY_ERR
7802eval_additive_expr(struct lyxp_expr *exp, uint16_t *exp_idx, uint16_t repeat, struct lyxp_set *set, int options)
7803{
7804 LY_ERR rc;
7805 uint16_t this_op;
7806 struct lyxp_set orig_set, set2;
7807 uint16_t i;
7808
7809 assert(repeat);
7810
7811 set_init(&orig_set, set);
7812 set_init(&set2, set);
7813
7814 set_fill_set(&orig_set, set);
7815
7816 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_ADDITIVE, set, options);
7817 LY_CHECK_GOTO(rc, cleanup);
7818
7819 /* ('+' / '-' MultiplicativeExpr)* */
7820 for (i = 0; i < repeat; ++i) {
7821 this_op = *exp_idx;
7822
7823 assert(exp->tokens[*exp_idx] == LYXP_TOKEN_OPERATOR_MATH);
7824 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7825 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7826 ++(*exp_idx);
7827
7828 if (!set) {
7829 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_ADDITIVE, NULL, options);
7830 LY_CHECK_GOTO(rc, cleanup);
7831 continue;
7832 }
7833
7834 set_fill_set(&set2, &orig_set);
7835 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_ADDITIVE, &set2, options);
7836 LY_CHECK_GOTO(rc, cleanup);
7837
7838 /* eval */
7839 if (options & LYXP_SCNODE_ALL) {
7840 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007841 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007842 set_scnode_clear_ctx(set);
7843 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007844 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007845 LY_CHECK_GOTO(rc, cleanup);
7846 }
7847 }
7848
7849cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007850 lyxp_set_free_content(&orig_set);
7851 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007852 return rc;
7853}
7854
7855/**
7856 * @brief Evaluate RelationalExpr. Logs directly on error.
7857 *
Michal Vaskod3678892020-05-21 10:06:58 +02007858 * [16] RelationalExpr ::= AdditiveExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007859 * | RelationalExpr '<' AdditiveExpr
7860 * | RelationalExpr '>' AdditiveExpr
7861 * | RelationalExpr '<=' AdditiveExpr
7862 * | RelationalExpr '>=' AdditiveExpr
7863 *
7864 * @param[in] exp Parsed XPath expression.
7865 * @param[in] exp_idx Position in the expression @p exp.
7866 * @param[in] repeat How many times this expression is repeated.
7867 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7868 * @param[in] options XPath options.
7869 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7870 */
7871static LY_ERR
7872eval_relational_expr(struct lyxp_expr *exp, uint16_t *exp_idx, uint16_t repeat, struct lyxp_set *set, int options)
7873{
7874 LY_ERR rc;
7875 uint16_t this_op;
7876 struct lyxp_set orig_set, set2;
7877 uint16_t i;
7878
7879 assert(repeat);
7880
7881 set_init(&orig_set, set);
7882 set_init(&set2, set);
7883
7884 set_fill_set(&orig_set, set);
7885
7886 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_RELATIONAL, set, options);
7887 LY_CHECK_GOTO(rc, cleanup);
7888
7889 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
7890 for (i = 0; i < repeat; ++i) {
7891 this_op = *exp_idx;
7892
7893 assert(exp->tokens[*exp_idx] == LYXP_TOKEN_OPERATOR_COMP);
7894 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7895 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7896 ++(*exp_idx);
7897
7898 if (!set) {
7899 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_RELATIONAL, NULL, options);
7900 LY_CHECK_GOTO(rc, cleanup);
7901 continue;
7902 }
7903
7904 set_fill_set(&set2, &orig_set);
7905 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_RELATIONAL, &set2, options);
7906 LY_CHECK_GOTO(rc, cleanup);
7907
7908 /* eval */
7909 if (options & LYXP_SCNODE_ALL) {
7910 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007911 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007912 set_scnode_clear_ctx(set);
7913 } else {
7914 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options);
7915 LY_CHECK_GOTO(rc, cleanup);
7916 }
7917 }
7918
7919cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007920 lyxp_set_free_content(&orig_set);
7921 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007922 return rc;
7923}
7924
7925/**
7926 * @brief Evaluate EqualityExpr. Logs directly on error.
7927 *
Michal Vaskod3678892020-05-21 10:06:58 +02007928 * [15] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007929 * | EqualityExpr '!=' RelationalExpr
7930 *
7931 * @param[in] exp Parsed XPath expression.
7932 * @param[in] exp_idx Position in the expression @p exp.
7933 * @param[in] repeat How many times this expression is repeated.
7934 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7935 * @param[in] options XPath options.
7936 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7937 */
7938static LY_ERR
7939eval_equality_expr(struct lyxp_expr *exp, uint16_t *exp_idx, uint16_t repeat, struct lyxp_set *set, int options)
7940{
7941 LY_ERR rc;
7942 uint16_t this_op;
7943 struct lyxp_set orig_set, set2;
7944 uint16_t i;
7945
7946 assert(repeat);
7947
7948 set_init(&orig_set, set);
7949 set_init(&set2, set);
7950
7951 set_fill_set(&orig_set, set);
7952
7953 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_EQUALITY, set, options);
7954 LY_CHECK_GOTO(rc, cleanup);
7955
7956 /* ('=' / '!=' RelationalExpr)* */
7957 for (i = 0; i < repeat; ++i) {
7958 this_op = *exp_idx;
7959
7960 assert(exp->tokens[*exp_idx] == LYXP_TOKEN_OPERATOR_COMP);
7961 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7962 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7963 ++(*exp_idx);
7964
7965 if (!set) {
7966 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_EQUALITY, NULL, options);
7967 LY_CHECK_GOTO(rc, cleanup);
7968 continue;
7969 }
7970
7971 set_fill_set(&set2, &orig_set);
7972 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_EQUALITY, &set2, options);
7973 LY_CHECK_GOTO(rc, cleanup);
7974
7975 /* eval */
7976 if (options & LYXP_SCNODE_ALL) {
7977 warn_operands(set->ctx, set, &set2, 0, exp->expr, exp->tok_pos[this_op - 1]);
7978 warn_equality_value(exp, set, *exp_idx - 1, this_op - 1, *exp_idx - 1);
7979 warn_equality_value(exp, &set2, this_op - 1, this_op - 1, *exp_idx - 1);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007980 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007981 set_scnode_clear_ctx(set);
7982 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007983 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options);
7984 LY_CHECK_GOTO(rc, cleanup);
7985 }
7986 }
7987
7988cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007989 lyxp_set_free_content(&orig_set);
7990 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007991 return rc;
7992}
7993
7994/**
7995 * @brief Evaluate AndExpr. Logs directly on error.
7996 *
Michal Vaskod3678892020-05-21 10:06:58 +02007997 * [14] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007998 *
7999 * @param[in] exp Parsed XPath expression.
8000 * @param[in] exp_idx Position in the expression @p exp.
8001 * @param[in] repeat How many times this expression is repeated.
8002 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8003 * @param[in] options XPath options.
8004 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8005 */
8006static LY_ERR
8007eval_and_expr(struct lyxp_expr *exp, uint16_t *exp_idx, uint16_t repeat, struct lyxp_set *set, int options)
8008{
8009 LY_ERR rc;
8010 struct lyxp_set orig_set, set2;
8011 uint16_t i;
8012
8013 assert(repeat);
8014
8015 set_init(&orig_set, set);
8016 set_init(&set2, set);
8017
8018 set_fill_set(&orig_set, set);
8019
8020 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_AND, set, options);
8021 LY_CHECK_GOTO(rc, cleanup);
8022
8023 /* cast to boolean, we know that will be the final result */
8024 if (set && (options & LYXP_SCNODE_ALL)) {
8025 set_scnode_clear_ctx(set);
8026 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008027 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008028 }
8029
8030 /* ('and' EqualityExpr)* */
8031 for (i = 0; i < repeat; ++i) {
8032 assert(exp->tokens[*exp_idx] == LYXP_TOKEN_OPERATOR_LOG);
8033 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (!set || !set->val.bool ? "skipped" : "parsed"),
8034 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
8035 ++(*exp_idx);
8036
8037 /* lazy evaluation */
8038 if (!set || ((set->type == LYXP_SET_BOOLEAN) && !set->val.bool)) {
8039 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_AND, NULL, options);
8040 LY_CHECK_GOTO(rc, cleanup);
8041 continue;
8042 }
8043
8044 set_fill_set(&set2, &orig_set);
8045 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_AND, &set2, options);
8046 LY_CHECK_GOTO(rc, cleanup);
8047
8048 /* eval - just get boolean value actually */
8049 if (set->type == LYXP_SET_SCNODE_SET) {
8050 set_scnode_clear_ctx(&set2);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008051 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008052 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008053 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008054 set_fill_set(set, &set2);
8055 }
8056 }
8057
8058cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008059 lyxp_set_free_content(&orig_set);
8060 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008061 return rc;
8062}
8063
8064/**
8065 * @brief Evaluate OrExpr. Logs directly on error.
8066 *
Michal Vaskod3678892020-05-21 10:06:58 +02008067 * [13] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008068 *
8069 * @param[in] exp Parsed XPath expression.
8070 * @param[in] exp_idx Position in the expression @p exp.
8071 * @param[in] repeat How many times this expression is repeated.
8072 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8073 * @param[in] options XPath options.
8074 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8075 */
8076static LY_ERR
8077eval_or_expr(struct lyxp_expr *exp, uint16_t *exp_idx, uint16_t repeat, struct lyxp_set *set, int options)
8078{
8079 LY_ERR rc;
8080 struct lyxp_set orig_set, set2;
8081 uint16_t i;
8082
8083 assert(repeat);
8084
8085 set_init(&orig_set, set);
8086 set_init(&set2, set);
8087
8088 set_fill_set(&orig_set, set);
8089
8090 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_OR, set, options);
8091 LY_CHECK_GOTO(rc, cleanup);
8092
8093 /* cast to boolean, we know that will be the final result */
8094 if (set && (options & LYXP_SCNODE_ALL)) {
8095 set_scnode_clear_ctx(set);
8096 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008097 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008098 }
8099
8100 /* ('or' AndExpr)* */
8101 for (i = 0; i < repeat; ++i) {
8102 assert(exp->tokens[*exp_idx] == LYXP_TOKEN_OPERATOR_LOG);
8103 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (!set || set->val.bool ? "skipped" : "parsed"),
8104 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
8105 ++(*exp_idx);
8106
8107 /* lazy evaluation */
8108 if (!set || ((set->type == LYXP_SET_BOOLEAN) && set->val.bool)) {
8109 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_OR, NULL, options);
8110 LY_CHECK_GOTO(rc, cleanup);
8111 continue;
8112 }
8113
8114 set_fill_set(&set2, &orig_set);
8115 /* expr_type cound have been LYXP_EXPR_NONE in all these later calls (except for the first one),
8116 * but it does not matter */
8117 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_OR, &set2, options);
8118 LY_CHECK_GOTO(rc, cleanup);
8119
8120 /* eval - just get boolean value actually */
8121 if (set->type == LYXP_SET_SCNODE_SET) {
8122 set_scnode_clear_ctx(&set2);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008123 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008124 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008125 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008126 set_fill_set(set, &set2);
8127 }
8128 }
8129
8130cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008131 lyxp_set_free_content(&orig_set);
8132 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008133 return rc;
8134}
8135
8136/**
8137 * @brief Decide what expression is at the pointer @p exp_idx and evaluate it accordingly.
8138 *
8139 * @param[in] exp Parsed XPath expression.
8140 * @param[in] exp_idx Position in the expression @p exp.
8141 * @param[in] etype Expression type to evaluate.
8142 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8143 * @param[in] options XPath options.
8144 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8145 */
8146static LY_ERR
8147eval_expr_select(struct lyxp_expr *exp, uint16_t *exp_idx, enum lyxp_expr_type etype, struct lyxp_set *set, int options)
8148{
8149 uint16_t i, count;
8150 enum lyxp_expr_type next_etype;
8151 LY_ERR rc;
8152
8153 /* process operator repeats */
8154 if (!exp->repeat[*exp_idx]) {
8155 next_etype = LYXP_EXPR_NONE;
8156 } else {
8157 /* find etype repeat */
8158 for (i = 0; exp->repeat[*exp_idx][i] > etype; ++i);
8159
8160 /* select one-priority lower because etype expression called us */
8161 if (i) {
8162 next_etype = exp->repeat[*exp_idx][i - 1];
8163 /* count repeats for that expression */
8164 for (count = 0; i && exp->repeat[*exp_idx][i - 1] == next_etype; ++count, --i);
8165 } else {
8166 next_etype = LYXP_EXPR_NONE;
8167 }
8168 }
8169
8170 /* decide what expression are we parsing based on the repeat */
8171 switch (next_etype) {
8172 case LYXP_EXPR_OR:
8173 rc = eval_or_expr(exp, exp_idx, count, set, options);
8174 break;
8175 case LYXP_EXPR_AND:
8176 rc = eval_and_expr(exp, exp_idx, count, set, options);
8177 break;
8178 case LYXP_EXPR_EQUALITY:
8179 rc = eval_equality_expr(exp, exp_idx, count, set, options);
8180 break;
8181 case LYXP_EXPR_RELATIONAL:
8182 rc = eval_relational_expr(exp, exp_idx, count, set, options);
8183 break;
8184 case LYXP_EXPR_ADDITIVE:
8185 rc = eval_additive_expr(exp, exp_idx, count, set, options);
8186 break;
8187 case LYXP_EXPR_MULTIPLICATIVE:
8188 rc = eval_multiplicative_expr(exp, exp_idx, count, set, options);
8189 break;
8190 case LYXP_EXPR_UNARY:
8191 rc = eval_unary_expr(exp, exp_idx, count, set, options);
8192 break;
8193 case LYXP_EXPR_UNION:
8194 rc = eval_union_expr(exp, exp_idx, count, set, options);
8195 break;
8196 case LYXP_EXPR_NONE:
8197 rc = eval_path_expr(exp, exp_idx, set, options);
8198 break;
8199 default:
8200 LOGINT_RET(set->ctx);
8201 }
8202
8203 return rc;
8204}
8205
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008206/**
8207 * @brief Get root type.
8208 *
8209 * @param[in] ctx_node Context node.
8210 * @param[in] ctx_scnode Schema context node.
8211 * @param[in] options XPath options.
8212 * @return Root type.
8213 */
8214static enum lyxp_node_type
8215lyxp_get_root_type(const struct lyd_node *ctx_node, const struct lysc_node *ctx_scnode, int options)
8216{
8217 if (options & LYXP_SCNODE_ALL) {
8218 if (options & LYXP_SCNODE) {
8219 /* general root that can access everything */
8220 return LYXP_NODE_ROOT;
8221 } else if (!ctx_scnode || (ctx_scnode->flags & LYS_CONFIG_W)) {
8222 /* root context node can access only config data (because we said so, it is unspecified) */
8223 return LYXP_NODE_ROOT_CONFIG;
8224 } else {
8225 return LYXP_NODE_ROOT;
8226 }
8227 }
8228
8229 if (!ctx_node || (ctx_node->schema->flags & LYS_CONFIG_W)) {
8230 /* root context node can access only config data (because we said so, it is unspecified) */
8231 return LYXP_NODE_ROOT_CONFIG;
8232 }
8233
8234 return LYXP_NODE_ROOT;
8235}
8236
Michal Vasko03ff5a72019-09-11 13:49:33 +02008237LY_ERR
Michal Vaskoecd62de2019-11-13 12:35:11 +01008238lyxp_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 +01008239 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 +02008240{
Michal Vasko03ff5a72019-09-11 13:49:33 +02008241 uint16_t exp_idx = 0;
8242 LY_ERR rc;
8243
Michal Vaskoecd62de2019-11-13 12:35:11 +01008244 LY_CHECK_ARG_RET(NULL, exp, local_mod, set, LY_EINVAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008245
8246 /* prepare set for evaluation */
8247 exp_idx = 0;
8248 memset(set, 0, sizeof *set);
Michal Vaskod3678892020-05-21 10:06:58 +02008249 set->type = LYXP_SET_NODE_SET;
Michal Vasko9b368d32020-02-14 13:53:31 +01008250 set_insert_node(set, (struct lyd_node *)ctx_node, 0, ctx_node_type, 0);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008251 set->ctx = local_mod->ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008252 set->ctx_node = ctx_node;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008253 set->root_type = lyxp_get_root_type(ctx_node, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008254 set->local_mod = local_mod;
Michal Vaskof03ed032020-03-04 13:31:44 +01008255 set->tree = tree;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008256 set->format = format;
8257
8258 /* evaluate */
8259 rc = eval_expr_select(exp, &exp_idx, 0, set, options);
8260 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02008261 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008262 }
8263
Michal Vasko03ff5a72019-09-11 13:49:33 +02008264 return rc;
8265}
8266
8267#if 0
8268
8269/* full xml printing of set elements, not used currently */
8270
8271void
8272lyxp_set_print_xml(FILE *f, struct lyxp_set *set)
8273{
8274 uint32_t i;
8275 char *str_num;
8276 struct lyout out;
8277
8278 memset(&out, 0, sizeof out);
8279
8280 out.type = LYOUT_STREAM;
8281 out.method.f = f;
8282
8283 switch (set->type) {
8284 case LYXP_SET_EMPTY:
8285 ly_print(&out, "Empty XPath set\n\n");
8286 break;
8287 case LYXP_SET_BOOLEAN:
8288 ly_print(&out, "Boolean XPath set:\n");
8289 ly_print(&out, "%s\n\n", set->value.bool ? "true" : "false");
8290 break;
8291 case LYXP_SET_STRING:
8292 ly_print(&out, "String XPath set:\n");
8293 ly_print(&out, "\"%s\"\n\n", set->value.str);
8294 break;
8295 case LYXP_SET_NUMBER:
8296 ly_print(&out, "Number XPath set:\n");
8297
8298 if (isnan(set->value.num)) {
8299 str_num = strdup("NaN");
8300 } else if ((set->value.num == 0) || (set->value.num == -0.0f)) {
8301 str_num = strdup("0");
8302 } else if (isinf(set->value.num) && !signbit(set->value.num)) {
8303 str_num = strdup("Infinity");
8304 } else if (isinf(set->value.num) && signbit(set->value.num)) {
8305 str_num = strdup("-Infinity");
8306 } else if ((long long)set->value.num == set->value.num) {
8307 if (asprintf(&str_num, "%lld", (long long)set->value.num) == -1) {
8308 str_num = NULL;
8309 }
8310 } else {
8311 if (asprintf(&str_num, "%03.1Lf", set->value.num) == -1) {
8312 str_num = NULL;
8313 }
8314 }
8315 if (!str_num) {
8316 LOGMEM;
8317 return;
8318 }
8319 ly_print(&out, "%s\n\n", str_num);
8320 free(str_num);
8321 break;
8322 case LYXP_SET_NODE_SET:
8323 ly_print(&out, "Node XPath set:\n");
8324
8325 for (i = 0; i < set->used; ++i) {
8326 ly_print(&out, "%d. ", i + 1);
8327 switch (set->node_type[i]) {
8328 case LYXP_NODE_ROOT_ALL:
8329 ly_print(&out, "ROOT all\n\n");
8330 break;
8331 case LYXP_NODE_ROOT_CONFIG:
8332 ly_print(&out, "ROOT config\n\n");
8333 break;
8334 case LYXP_NODE_ROOT_STATE:
8335 ly_print(&out, "ROOT state\n\n");
8336 break;
8337 case LYXP_NODE_ROOT_NOTIF:
8338 ly_print(&out, "ROOT notification \"%s\"\n\n", set->value.nodes[i]->schema->name);
8339 break;
8340 case LYXP_NODE_ROOT_RPC:
8341 ly_print(&out, "ROOT rpc \"%s\"\n\n", set->value.nodes[i]->schema->name);
8342 break;
8343 case LYXP_NODE_ROOT_OUTPUT:
8344 ly_print(&out, "ROOT output \"%s\"\n\n", set->value.nodes[i]->schema->name);
8345 break;
8346 case LYXP_NODE_ELEM:
8347 ly_print(&out, "ELEM \"%s\"\n", set->value.nodes[i]->schema->name);
8348 xml_print_node(&out, 1, set->value.nodes[i], 1, LYP_FORMAT);
8349 ly_print(&out, "\n");
8350 break;
8351 case LYXP_NODE_TEXT:
8352 ly_print(&out, "TEXT \"%s\"\n\n", ((struct lyd_node_leaf_list *)set->value.nodes[i])->value_str);
8353 break;
8354 case LYXP_NODE_ATTR:
8355 ly_print(&out, "ATTR \"%s\" = \"%s\"\n\n", set->value.attrs[i]->name, set->value.attrs[i]->value);
8356 break;
8357 }
8358 }
8359 break;
8360 }
8361}
8362
8363#endif
8364
8365LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008366lyxp_set_cast(struct lyxp_set *set, enum lyxp_set_type target)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008367{
8368 long double num;
8369 char *str;
8370 LY_ERR rc;
8371
8372 if (!set || (set->type == target)) {
8373 return LY_SUCCESS;
8374 }
8375
8376 /* it's not possible to convert anything into a node set */
Michal Vaskod3678892020-05-21 10:06:58 +02008377 assert(target != LYXP_SET_NODE_SET);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008378
8379 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vaskod3678892020-05-21 10:06:58 +02008380 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008381 return LY_EINVAL;
8382 }
8383
8384 /* to STRING */
Michal Vaskod3678892020-05-21 10:06:58 +02008385 if ((target == LYXP_SET_STRING) || ((target == LYXP_SET_NUMBER) && (set->type == LYXP_SET_NODE_SET))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008386 switch (set->type) {
8387 case LYXP_SET_NUMBER:
8388 if (isnan(set->val.num)) {
8389 set->val.str = strdup("NaN");
8390 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8391 } else if ((set->val.num == 0) || (set->val.num == -0.0f)) {
8392 set->val.str = strdup("0");
8393 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8394 } else if (isinf(set->val.num) && !signbit(set->val.num)) {
8395 set->val.str = strdup("Infinity");
8396 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8397 } else if (isinf(set->val.num) && signbit(set->val.num)) {
8398 set->val.str = strdup("-Infinity");
8399 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8400 } else if ((long long)set->val.num == set->val.num) {
8401 if (asprintf(&str, "%lld", (long long)set->val.num) == -1) {
8402 LOGMEM_RET(set->ctx);
8403 }
8404 set->val.str = str;
8405 } else {
8406 if (asprintf(&str, "%03.1Lf", set->val.num) == -1) {
8407 LOGMEM_RET(set->ctx);
8408 }
8409 set->val.str = str;
8410 }
8411 break;
8412 case LYXP_SET_BOOLEAN:
8413 if (set->val.bool) {
8414 set->val.str = strdup("true");
8415 } else {
8416 set->val.str = strdup("false");
8417 }
8418 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM);
8419 break;
8420 case LYXP_SET_NODE_SET:
8421 assert(set->used);
8422
8423 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008424 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008425
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008426 rc = cast_node_set_to_string(set, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008427 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02008428 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008429 set->val.str = str;
8430 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008431 default:
8432 LOGINT_RET(set->ctx);
8433 }
8434 set->type = LYXP_SET_STRING;
8435 }
8436
8437 /* to NUMBER */
8438 if (target == LYXP_SET_NUMBER) {
8439 switch (set->type) {
8440 case LYXP_SET_STRING:
8441 num = cast_string_to_number(set->val.str);
Michal Vaskod3678892020-05-21 10:06:58 +02008442 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008443 set->val.num = num;
8444 break;
8445 case LYXP_SET_BOOLEAN:
8446 if (set->val.bool) {
8447 set->val.num = 1;
8448 } else {
8449 set->val.num = 0;
8450 }
8451 break;
8452 default:
8453 LOGINT_RET(set->ctx);
8454 }
8455 set->type = LYXP_SET_NUMBER;
8456 }
8457
8458 /* to BOOLEAN */
8459 if (target == LYXP_SET_BOOLEAN) {
8460 switch (set->type) {
8461 case LYXP_SET_NUMBER:
8462 if ((set->val.num == 0) || (set->val.num == -0.0f) || isnan(set->val.num)) {
8463 set->val.bool = 0;
8464 } else {
8465 set->val.bool = 1;
8466 }
8467 break;
8468 case LYXP_SET_STRING:
8469 if (set->val.str[0]) {
Michal Vaskod3678892020-05-21 10:06:58 +02008470 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008471 set->val.bool = 1;
8472 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02008473 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008474 set->val.bool = 0;
8475 }
8476 break;
8477 case LYXP_SET_NODE_SET:
Michal Vaskod3678892020-05-21 10:06:58 +02008478 if (set->used) {
8479 lyxp_set_free_content(set);
8480 set->val.bool = 1;
8481 } else {
8482 lyxp_set_free_content(set);
8483 set->val.bool = 0;
8484 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008485 break;
8486 default:
8487 LOGINT_RET(set->ctx);
8488 }
8489 set->type = LYXP_SET_BOOLEAN;
8490 }
8491
Michal Vasko03ff5a72019-09-11 13:49:33 +02008492 return LY_SUCCESS;
8493}
8494
8495LY_ERR
8496lyxp_atomize(struct lyxp_expr *exp, LYD_FORMAT format, const struct lys_module *local_mod, const struct lysc_node *ctx_scnode,
8497 enum lyxp_node_type ctx_scnode_type, struct lyxp_set *set, int options)
8498{
8499 struct ly_ctx *ctx;
8500 uint16_t exp_idx = 0;
8501
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008502 LY_CHECK_ARG_RET(NULL, exp, local_mod, set, LY_EINVAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008503
8504 ctx = local_mod->ctx;
8505
8506 /* prepare set for evaluation */
8507 exp_idx = 0;
8508 memset(set, 0, sizeof *set);
8509 set->type = LYXP_SET_SCNODE_SET;
Michal Vaskoecd62de2019-11-13 12:35:11 +01008510 lyxp_set_scnode_insert_node(set, ctx_scnode, ctx_scnode_type);
Michal Vasko5c4e5892019-11-14 12:31:38 +01008511 set->val.scnodes[0].in_ctx = -2;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008512 set->ctx = ctx;
8513 set->ctx_scnode = ctx_scnode;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008514 set->root_type = lyxp_get_root_type(NULL, ctx_scnode, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008515 set->local_mod = local_mod;
8516 set->format = format;
8517
8518 /* evaluate */
8519 return eval_expr_select(exp, &exp_idx, 0, set, options);
8520}