blob: 49c6a559f4a528825c8cc8f7acd0c4372bae79aa [file] [log] [blame]
Radek Krejcib1646a92018-11-02 16:08:26 +01001/**
2 * @file xpath.c
3 * @author Michal Vasko <mvasko@cesnet.cz>
4 * @brief YANG XPath evaluation functions
5 *
Michal Vasko61ac2f62020-05-25 12:39:51 +02006 * Copyright (c) 2015 - 2020 CESNET, z.s.p.o.
Radek Krejcib1646a92018-11-02 16:08:26 +01007 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
Michal Vasko03ff5a72019-09-11 13:49:33 +020014#define _GNU_SOURCE
15
16/* needed by libmath functions isfinite(), isinf(), isnan(), signbit(), ... */
17#define _ISOC99_SOURCE
Radek Krejcib1646a92018-11-02 16:08:26 +010018
Radek Krejci535ea9f2020-05-29 16:01:05 +020019#include "xpath.h"
Radek Krejcib1646a92018-11-02 16:08:26 +010020
Radek Krejci535ea9f2020-05-29 16:01:05 +020021#include <assert.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010022#include <ctype.h>
Radek Krejci535ea9f2020-05-29 16:01:05 +020023#include <errno.h>
24#include <limits.h>
25#include <math.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020026#include <stdint.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010027#include <stdio.h>
28#include <stdlib.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010029#include <string.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010030
Radek Krejci535ea9f2020-05-29 16:01:05 +020031#include "common.h"
32#include "context.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020033#include "dict.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020034#include "hash_table.h"
Michal Vasko03ff5a72019-09-11 13:49:33 +020035#include "plugins_types.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020036#include "printer.h"
37#include "printer_data.h"
38#include "tree.h"
39#include "tree_data_internal.h"
40#include "tree_schema_internal.h"
41#include "xml.h"
Michal Vasko03ff5a72019-09-11 13:49:33 +020042
Michal Vasko14676352020-05-29 11:35:55 +020043static LY_ERR reparse_or_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *exp_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +020044static LY_ERR eval_expr_select(struct lyxp_expr *exp, uint16_t *exp_idx, enum lyxp_expr_type etype, struct lyxp_set *set, int options);
45
46/**
47 * @brief Print the type of an XPath \p set.
48 *
49 * @param[in] set Set to use.
50 * @return Set type string.
51 */
52static const char *
53print_set_type(struct lyxp_set *set)
54{
55 switch (set->type) {
Michal Vasko03ff5a72019-09-11 13:49:33 +020056 case LYXP_SET_NODE_SET:
57 return "node set";
58 case LYXP_SET_SCNODE_SET:
59 return "schema node set";
60 case LYXP_SET_BOOLEAN:
61 return "boolean";
62 case LYXP_SET_NUMBER:
63 return "number";
64 case LYXP_SET_STRING:
65 return "string";
66 }
67
68 return NULL;
69}
70
71/**
72 * @brief Print an XPath token \p tok type.
73 *
74 * @param[in] tok Token to use.
75 * @return Token type string.
76 */
77static const char *
78print_token(enum lyxp_token tok)
79{
80 switch (tok) {
81 case LYXP_TOKEN_PAR1:
82 return "(";
83 case LYXP_TOKEN_PAR2:
84 return ")";
85 case LYXP_TOKEN_BRACK1:
86 return "[";
87 case LYXP_TOKEN_BRACK2:
88 return "]";
89 case LYXP_TOKEN_DOT:
90 return ".";
91 case LYXP_TOKEN_DDOT:
92 return "..";
93 case LYXP_TOKEN_AT:
94 return "@";
95 case LYXP_TOKEN_COMMA:
96 return ",";
97 case LYXP_TOKEN_NAMETEST:
98 return "NameTest";
99 case LYXP_TOKEN_NODETYPE:
100 return "NodeType";
101 case LYXP_TOKEN_FUNCNAME:
102 return "FunctionName";
103 case LYXP_TOKEN_OPERATOR_LOG:
104 return "Operator(Logic)";
105 case LYXP_TOKEN_OPERATOR_COMP:
106 return "Operator(Comparison)";
107 case LYXP_TOKEN_OPERATOR_MATH:
108 return "Operator(Math)";
109 case LYXP_TOKEN_OPERATOR_UNI:
110 return "Operator(Union)";
111 case LYXP_TOKEN_OPERATOR_PATH:
112 return "Operator(Path)";
Michal Vasko14676352020-05-29 11:35:55 +0200113 case LYXP_TOKEN_OPERATOR_RPATH:
114 return "Operator(Recursive Path)";
Michal Vasko03ff5a72019-09-11 13:49:33 +0200115 case LYXP_TOKEN_LITERAL:
116 return "Literal";
117 case LYXP_TOKEN_NUMBER:
118 return "Number";
119 default:
120 LOGINT(NULL);
121 return "";
122 }
123}
124
125/**
126 * @brief Print the whole expression \p exp to debug output.
127 *
128 * @param[in] exp Expression to use.
129 */
130static void
131print_expr_struct_debug(struct lyxp_expr *exp)
132{
133 uint16_t i, j;
134 char tmp[128];
135
136 if (!exp || (ly_log_level < LY_LLDBG)) {
137 return;
138 }
139
140 LOGDBG(LY_LDGXPATH, "expression \"%s\":", exp->expr);
141 for (i = 0; i < exp->used; ++i) {
142 sprintf(tmp, "\ttoken %s, in expression \"%.*s\"", print_token(exp->tokens[i]), exp->tok_len[i],
143 &exp->expr[exp->tok_pos[i]]);
144 if (exp->repeat[i]) {
145 sprintf(tmp + strlen(tmp), " (repeat %d", exp->repeat[i][0]);
146 for (j = 1; exp->repeat[i][j]; ++j) {
147 sprintf(tmp + strlen(tmp), ", %d", exp->repeat[i][j]);
148 }
149 strcat(tmp, ")");
150 }
151 LOGDBG(LY_LDGXPATH, tmp);
152 }
153}
154
155#ifndef NDEBUG
156
157/**
158 * @brief Print XPath set content to debug output.
159 *
160 * @param[in] set Set to print.
161 */
162static void
163print_set_debug(struct lyxp_set *set)
164{
165 uint32_t i;
166 char *str;
167 int dynamic;
168 struct lyxp_set_node *item;
169 struct lyxp_set_scnode *sitem;
170
171 if (ly_log_level < LY_LLDBG) {
172 return;
173 }
174
175 switch (set->type) {
176 case LYXP_SET_NODE_SET:
177 LOGDBG(LY_LDGXPATH, "set NODE SET:");
178 for (i = 0; i < set->used; ++i) {
179 item = &set->val.nodes[i];
180
181 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +0100182 case LYXP_NODE_NONE:
183 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): NONE", i + 1, item->pos);
184 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200185 case LYXP_NODE_ROOT:
186 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT", i + 1, item->pos);
187 break;
188 case LYXP_NODE_ROOT_CONFIG:
189 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT CONFIG", i + 1, item->pos);
190 break;
191 case LYXP_NODE_ELEM:
192 if ((item->node->schema->nodetype == LYS_LIST)
193 && (((struct lyd_node_inner *)item->node)->child->schema->nodetype == LYS_LEAF)) {
194 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (1st child val: %s)", i + 1, item->pos,
195 item->node->schema->name,
196 (str = (char *)lyd_value2str((struct lyd_node_term *)lyd_node_children(item->node), &dynamic)));
197 if (dynamic) {
198 free(str);
199 }
200 } else if (((struct lyd_node_inner *)item->node)->schema->nodetype == LYS_LEAFLIST) {
201 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (val: %s)", i + 1, item->pos,
202 item->node->schema->name,
203 (str = (char *)lyd_value2str((struct lyd_node_term *)item->node, &dynamic)));
204 if (dynamic) {
205 free(str);
206 }
207 } else {
208 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s", i + 1, item->pos, item->node->schema->name);
209 }
210 break;
211 case LYXP_NODE_TEXT:
212 if (item->node->schema->nodetype & LYS_ANYDATA) {
213 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): TEXT <%s>", i + 1, item->pos,
214 item->node->schema->nodetype == LYS_ANYXML ? "anyxml" : "anydata");
215 } else {
216 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): TEXT %s", i + 1, item->pos,
217 (str = (char *)lyd_value2str((struct lyd_node_term *)item->node, &dynamic)));
218 if (dynamic) {
219 free(str);
220 }
221 }
222 break;
Michal Vasko9f96a052020-03-10 09:41:45 +0100223 case LYXP_NODE_META:
224 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): META %s = %s", i + 1, item->pos, set->val.meta[i].meta->name,
225 set->val.meta[i].meta->value);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200226 break;
227 }
228 }
229 break;
230
231 case LYXP_SET_SCNODE_SET:
232 LOGDBG(LY_LDGXPATH, "set SCNODE SET:");
233 for (i = 0; i < set->used; ++i) {
234 sitem = &set->val.scnodes[i];
235
236 switch (sitem->type) {
237 case LYXP_NODE_ROOT:
238 LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT", i + 1, sitem->in_ctx);
239 break;
240 case LYXP_NODE_ROOT_CONFIG:
241 LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT CONFIG", i + 1, sitem->in_ctx);
242 break;
243 case LYXP_NODE_ELEM:
244 LOGDBG(LY_LDGXPATH, "\t%d (%u): ELEM %s", i + 1, sitem->in_ctx, sitem->scnode->name);
245 break;
246 default:
247 LOGINT(NULL);
248 break;
249 }
250 }
251 break;
252
Michal Vasko03ff5a72019-09-11 13:49:33 +0200253 case LYXP_SET_BOOLEAN:
254 LOGDBG(LY_LDGXPATH, "set BOOLEAN");
255 LOGDBG(LY_LDGXPATH, "\t%s", (set->val.bool ? "true" : "false"));
256 break;
257
258 case LYXP_SET_STRING:
259 LOGDBG(LY_LDGXPATH, "set STRING");
260 LOGDBG(LY_LDGXPATH, "\t%s", set->val.str);
261 break;
262
263 case LYXP_SET_NUMBER:
264 LOGDBG(LY_LDGXPATH, "set NUMBER");
265
266 if (isnan(set->val.num)) {
267 str = strdup("NaN");
268 } else if ((set->val.num == 0) || (set->val.num == -0.0f)) {
269 str = strdup("0");
270 } else if (isinf(set->val.num) && !signbit(set->val.num)) {
271 str = strdup("Infinity");
272 } else if (isinf(set->val.num) && signbit(set->val.num)) {
273 str = strdup("-Infinity");
274 } else if ((long long)set->val.num == set->val.num) {
275 if (asprintf(&str, "%lld", (long long)set->val.num) == -1) {
276 str = NULL;
277 }
278 } else {
279 if (asprintf(&str, "%03.1Lf", set->val.num) == -1) {
280 str = NULL;
281 }
282 }
283 LY_CHECK_ERR_RET(!str, LOGMEM(NULL), );
284
285 LOGDBG(LY_LDGXPATH, "\t%s", str);
286 free(str);
287 }
288}
289
290#endif
291
292/**
293 * @brief Realloc the string \p str.
294 *
295 * @param[in] ctx libyang context for logging.
296 * @param[in] needed How much free space is required.
297 * @param[in,out] str Pointer to the string to use.
298 * @param[in,out] used Used bytes in \p str.
299 * @param[in,out] size Allocated bytes in \p str.
300 * @return LY_ERR
301 */
302static LY_ERR
Michal Vasko52927e22020-03-16 17:26:14 +0100303cast_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 +0200304{
305 if (*size - *used < needed) {
306 do {
307 if ((UINT16_MAX - *size) < LYXP_STRING_CAST_SIZE_STEP) {
308 LOGERR(ctx, LY_EINVAL, "XPath string length limit (%u) reached.", UINT16_MAX);
309 return LY_EINVAL;
310 }
311 *size += LYXP_STRING_CAST_SIZE_STEP;
312 } while (*size - *used < needed);
313 *str = ly_realloc(*str, *size * sizeof(char));
314 LY_CHECK_ERR_RET(!(*str), LOGMEM(ctx), LY_EMEM);
315 }
316
317 return LY_SUCCESS;
318}
319
320/**
321 * @brief Cast nodes recursively to one string @p str.
322 *
323 * @param[in] node Node to cast.
324 * @param[in] fake_cont Whether to put the data into a "fake" container.
325 * @param[in] root_type Type of the XPath root.
326 * @param[in] indent Current indent.
327 * @param[in,out] str Resulting string.
328 * @param[in,out] used Used bytes in @p str.
329 * @param[in,out] size Allocated bytes in @p str.
330 * @return LY_ERR
331 */
332static LY_ERR
333cast_string_recursive(const struct lyd_node *node, int fake_cont, enum lyxp_node_type root_type, uint16_t indent, char **str,
334 uint16_t *used, uint16_t *size)
335{
336 char *buf, *line, *ptr;
337 const char *value_str;
338 int dynamic;
339 const struct lyd_node *child;
340 struct lyd_node_any *any;
341 LY_ERR rc;
342
343 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) {
344 return LY_SUCCESS;
345 }
346
347 if (fake_cont) {
348 rc = cast_string_realloc(LYD_NODE_CTX(node), 1, str, used, size);
349 LY_CHECK_RET(rc);
350 strcpy(*str + (*used - 1), "\n");
351 ++(*used);
352
353 ++indent;
354 }
355
356 switch (node->schema->nodetype) {
357 case LYS_CONTAINER:
358 case LYS_LIST:
359 case LYS_RPC:
360 case LYS_NOTIF:
361 rc = cast_string_realloc(LYD_NODE_CTX(node), 1, str, used, size);
362 LY_CHECK_RET(rc);
363 strcpy(*str + (*used - 1), "\n");
364 ++(*used);
365
366 for (child = lyd_node_children(node); child; child = child->next) {
367 rc = cast_string_recursive(child, 0, root_type, indent + 1, str, used, size);
368 LY_CHECK_RET(rc);
369 }
370
371 break;
372
373 case LYS_LEAF:
374 case LYS_LEAFLIST:
375 value_str = lyd_value2str(((struct lyd_node_term *)node), &dynamic);
376
377 /* print indent */
378 rc = cast_string_realloc(LYD_NODE_CTX(node), indent * 2 + strlen(value_str) + 1, str, used, size);
379 if (rc != LY_SUCCESS) {
380 if (dynamic) {
381 free((char *)value_str);
382 }
383 return rc;
384 }
385 memset(*str + (*used - 1), ' ', indent * 2);
386 *used += indent * 2;
387
388 /* print value */
389 if (*used == 1) {
390 sprintf(*str + (*used - 1), "%s", value_str);
391 *used += strlen(value_str);
392 } else {
393 sprintf(*str + (*used - 1), "%s\n", value_str);
394 *used += strlen(value_str) + 1;
395 }
396 if (dynamic) {
397 free((char *)value_str);
398 }
399
400 break;
401
402 case LYS_ANYXML:
403 case LYS_ANYDATA:
404 any = (struct lyd_node_any *)node;
405 if (!(void *)any->value.tree) {
406 /* no content */
407 buf = strdup("");
408 LY_CHECK_ERR_RET(!buf, LOGMEM(LYD_NODE_CTX(node)), LY_EMEM);
409 } else {
Radek Krejci241f6b52020-05-21 18:13:49 +0200410 struct ly_out *out;
Radek Krejcia5bba312020-01-09 15:41:20 +0100411
Michal Vasko03ff5a72019-09-11 13:49:33 +0200412 switch (any->value_type) {
413 case LYD_ANYDATA_STRING:
414 case LYD_ANYDATA_XML:
415 case LYD_ANYDATA_JSON:
416 buf = strdup(any->value.json);
417 LY_CHECK_ERR_RET(!buf, LOGMEM(LYD_NODE_CTX(node)), LY_EMEM);
418 break;
419 case LYD_ANYDATA_DATATREE:
Radek Krejci241f6b52020-05-21 18:13:49 +0200420 out = ly_out_new_memory(&buf, 0);
Radek Krejcia5bba312020-01-09 15:41:20 +0100421 rc = lyd_print(out, any->value.tree, LYD_XML, LYDP_WITHSIBLINGS);
Radek Krejci241f6b52020-05-21 18:13:49 +0200422 ly_out_free(out, NULL, 0);
Radek Krejcia5bba312020-01-09 15:41:20 +0100423 LY_CHECK_RET(rc < 0, -rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200424 break;
425 /* TODO case LYD_ANYDATA_LYB:
426 LOGERR(LYD_NODE_CTX(node), LY_EINVAL, "Cannot convert LYB anydata into string.");
427 return -1;*/
428 }
429 }
430
431 line = strtok_r(buf, "\n", &ptr);
432 do {
433 rc = cast_string_realloc(LYD_NODE_CTX(node), indent * 2 + strlen(line) + 1, str, used, size);
434 if (rc != LY_SUCCESS) {
435 free(buf);
436 return rc;
437 }
438 memset(*str + (*used - 1), ' ', indent * 2);
439 *used += indent * 2;
440
441 strcpy(*str + (*used - 1), line);
442 *used += strlen(line);
443
444 strcpy(*str + (*used - 1), "\n");
445 *used += 1;
446 } while ((line = strtok_r(NULL, "\n", &ptr)));
447
448 free(buf);
449 break;
450
451 default:
452 LOGINT_RET(LYD_NODE_CTX(node));
453 }
454
455 if (fake_cont) {
456 rc = cast_string_realloc(LYD_NODE_CTX(node), 1, str, used, size);
457 LY_CHECK_RET(rc);
458 strcpy(*str + (*used - 1), "\n");
459 ++(*used);
460
461 --indent;
462 }
463
464 return LY_SUCCESS;
465}
466
467/**
468 * @brief Cast an element into a string.
469 *
470 * @param[in] node Node to cast.
471 * @param[in] fake_cont Whether to put the data into a "fake" container.
472 * @param[in] root_type Type of the XPath root.
473 * @param[out] str Element cast to dynamically-allocated string.
474 * @return LY_ERR
475 */
476static LY_ERR
477cast_string_elem(struct lyd_node *node, int fake_cont, enum lyxp_node_type root_type, char **str)
478{
479 uint16_t used, size;
480 LY_ERR rc;
481
482 *str = malloc(LYXP_STRING_CAST_SIZE_START * sizeof(char));
483 LY_CHECK_ERR_RET(!*str, LOGMEM(LYD_NODE_CTX(node)), LY_EMEM);
484 (*str)[0] = '\0';
485 used = 1;
486 size = LYXP_STRING_CAST_SIZE_START;
487
488 rc = cast_string_recursive(node, fake_cont, root_type, 0, str, &used, &size);
489 if (rc != LY_SUCCESS) {
490 free(*str);
491 return rc;
492 }
493
494 if (size > used) {
495 *str = ly_realloc(*str, used * sizeof(char));
496 LY_CHECK_ERR_RET(!*str, LOGMEM(LYD_NODE_CTX(node)), LY_EMEM);
497 }
498 return LY_SUCCESS;
499}
500
501/**
502 * @brief Cast a LYXP_SET_NODE_SET set into a string.
503 * Context position aware.
504 *
505 * @param[in] set Set to cast.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200506 * @param[out] str Cast dynamically-allocated string.
507 * @return LY_ERR
508 */
509static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100510cast_node_set_to_string(struct lyxp_set *set, char **str)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200511{
Michal Vasko03ff5a72019-09-11 13:49:33 +0200512 int dynamic;
513
Michal Vasko03ff5a72019-09-11 13:49:33 +0200514 switch (set->val.nodes[0].type) {
Michal Vasko2caefc12019-11-14 16:07:56 +0100515 case LYXP_NODE_NONE:
516 /* invalid */
517 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200518 case LYXP_NODE_ROOT:
519 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100520 return cast_string_elem(set->val.nodes[0].node, 1, set->root_type, str);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200521 case LYXP_NODE_ELEM:
522 case LYXP_NODE_TEXT:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100523 return cast_string_elem(set->val.nodes[0].node, 0, set->root_type, str);
Michal Vasko9f96a052020-03-10 09:41:45 +0100524 case LYXP_NODE_META:
525 *str = (char *)lyd_meta2str(set->val.meta[0].meta, &dynamic);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200526 if (!dynamic) {
527 *str = strdup(*str);
528 if (!*str) {
529 LOGMEM_RET(set->ctx);
530 }
531 }
532 return LY_SUCCESS;
533 }
534
535 LOGINT_RET(set->ctx);
536}
537
538/**
539 * @brief Cast a string into an XPath number.
540 *
541 * @param[in] str String to use.
542 * @return Cast number.
543 */
544static long double
545cast_string_to_number(const char *str)
546{
547 long double num;
548 char *ptr;
549
550 errno = 0;
551 num = strtold(str, &ptr);
552 if (errno || *ptr) {
553 num = NAN;
554 }
555 return num;
556}
557
558/**
559 * @brief Callback for checking value equality.
560 *
561 * @param[in] val1_p First value.
562 * @param[in] val2_p Second value.
563 * @param[in] mod Whether hash table is being modified.
564 * @param[in] cb_data Callback data.
565 * @return 0 if not equal, non-zero if equal.
566 */
567static int
568set_values_equal_cb(void *val1_p, void *val2_p, int UNUSED(mod), void *UNUSED(cb_data))
569{
570 struct lyxp_set_hash_node *val1, *val2;
571
572 val1 = (struct lyxp_set_hash_node *)val1_p;
573 val2 = (struct lyxp_set_hash_node *)val2_p;
574
575 if ((val1->node == val2->node) && (val1->type == val2->type)) {
576 return 1;
577 }
578
579 return 0;
580}
581
582/**
583 * @brief Insert node and its hash into set.
584 *
585 * @param[in] set et to insert to.
586 * @param[in] node Node with hash.
587 * @param[in] type Node type.
588 */
589static void
590set_insert_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type)
591{
592 int r;
593 uint32_t i, hash;
594 struct lyxp_set_hash_node hnode;
595
596 if (!set->ht && (set->used >= LYD_HT_MIN_ITEMS)) {
597 /* create hash table and add all the nodes */
598 set->ht = lyht_new(1, sizeof(struct lyxp_set_hash_node), set_values_equal_cb, NULL, 1);
599 for (i = 0; i < set->used; ++i) {
600 hnode.node = set->val.nodes[i].node;
601 hnode.type = set->val.nodes[i].type;
602
603 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
604 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
605 hash = dict_hash_multi(hash, NULL, 0);
606
607 r = lyht_insert(set->ht, &hnode, hash, NULL);
608 assert(!r);
609 (void)r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200610
Michal Vasko9d6befd2019-12-11 14:56:56 +0100611 if (hnode.node == node) {
612 /* it was just added, do not add it twice */
613 node = NULL;
614 }
615 }
616 }
617
618 if (set->ht && node) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200619 /* add the new node into hash table */
620 hnode.node = node;
621 hnode.type = type;
622
623 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
624 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
625 hash = dict_hash_multi(hash, NULL, 0);
626
627 r = lyht_insert(set->ht, &hnode, hash, NULL);
628 assert(!r);
629 (void)r;
630 }
631}
632
633/**
634 * @brief Remove node and its hash from set.
635 *
636 * @param[in] set Set to remove from.
637 * @param[in] node Node to remove.
638 * @param[in] type Node type.
639 */
640static void
641set_remove_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type)
642{
643 int r;
644 struct lyxp_set_hash_node hnode;
645 uint32_t hash;
646
647 if (set->ht) {
648 hnode.node = node;
649 hnode.type = type;
650
651 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
652 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
653 hash = dict_hash_multi(hash, NULL, 0);
654
655 r = lyht_remove(set->ht, &hnode, hash);
656 assert(!r);
657 (void)r;
658
659 if (!set->ht->used) {
660 lyht_free(set->ht);
661 set->ht = NULL;
662 }
663 }
664}
665
666/**
667 * @brief Check whether node is in set based on its hash.
668 *
669 * @param[in] set Set to search in.
670 * @param[in] node Node to search for.
671 * @param[in] type Node type.
672 * @param[in] skip_idx Index in @p set to skip.
673 * @return LY_ERR
674 */
675static LY_ERR
676set_dup_node_hash_check(const struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type, int skip_idx)
677{
678 struct lyxp_set_hash_node hnode, *match_p;
679 uint32_t hash;
680
681 hnode.node = node;
682 hnode.type = type;
683
684 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
685 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
686 hash = dict_hash_multi(hash, NULL, 0);
687
688 if (!lyht_find(set->ht, &hnode, hash, (void **)&match_p)) {
689 if ((skip_idx > -1) && (set->val.nodes[skip_idx].node == match_p->node) && (set->val.nodes[skip_idx].type == match_p->type)) {
690 /* we found it on the index that should be skipped, find another */
691 hnode = *match_p;
692 if (lyht_find_next(set->ht, &hnode, hash, (void **)&match_p)) {
693 /* none other found */
694 return LY_SUCCESS;
695 }
696 }
697
698 return LY_EEXIST;
699 }
700
701 /* not found */
702 return LY_SUCCESS;
703}
704
Michal Vaskod3678892020-05-21 10:06:58 +0200705void
706lyxp_set_free_content(struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200707{
708 if (!set) {
709 return;
710 }
711
712 if (set->type == LYXP_SET_NODE_SET) {
713 free(set->val.nodes);
714 lyht_free(set->ht);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200715 } else if (set->type == LYXP_SET_SCNODE_SET) {
716 free(set->val.scnodes);
Michal Vaskod3678892020-05-21 10:06:58 +0200717 lyht_free(set->ht);
718 } else {
719 if (set->type == LYXP_SET_STRING) {
720 free(set->val.str);
721 }
722 set->type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200723 }
Michal Vaskod3678892020-05-21 10:06:58 +0200724
725 set->val.nodes = NULL;
726 set->used = 0;
727 set->size = 0;
728 set->ht = NULL;
729 set->ctx_pos = 0;
730 set->ctx_pos = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200731}
732
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100733/**
734 * @brief Free dynamically-allocated set.
735 *
736 * @param[in] set Set to free.
737 */
738static void
Michal Vasko03ff5a72019-09-11 13:49:33 +0200739lyxp_set_free(struct lyxp_set *set)
740{
741 if (!set) {
742 return;
743 }
744
Michal Vaskod3678892020-05-21 10:06:58 +0200745 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200746 free(set);
747}
748
749/**
750 * @brief Initialize set context.
751 *
752 * @param[in] new Set to initialize.
753 * @param[in] set Arbitrary initialized set.
754 */
755static void
756set_init(struct lyxp_set *new, struct lyxp_set *set)
757{
758 memset(new, 0, sizeof *new);
Michal Vasko02a77382019-09-12 11:47:35 +0200759 if (set) {
760 new->ctx = set->ctx;
761 new->ctx_node = set->ctx_node;
Michal Vasko588112f2019-12-10 14:51:53 +0100762 new->root_type = set->root_type;
Michal Vasko02a77382019-09-12 11:47:35 +0200763 new->local_mod = set->local_mod;
Michal Vaskof03ed032020-03-04 13:31:44 +0100764 new->tree = set->tree;
Michal Vasko02a77382019-09-12 11:47:35 +0200765 new->format = set->format;
766 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200767}
768
769/**
770 * @brief Create a deep copy of a set.
771 *
772 * @param[in] set Set to copy.
773 * @return Copy of @p set.
774 */
775static struct lyxp_set *
776set_copy(struct lyxp_set *set)
777{
778 struct lyxp_set *ret;
779 uint16_t i;
Michal Vaskoba716542019-12-16 10:01:58 +0100780 int idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200781
782 if (!set) {
783 return NULL;
784 }
785
786 ret = malloc(sizeof *ret);
787 LY_CHECK_ERR_RET(!ret, LOGMEM(set->ctx), NULL);
788 set_init(ret, set);
789
790 if (set->type == LYXP_SET_SCNODE_SET) {
791 ret->type = set->type;
792
793 for (i = 0; i < set->used; ++i) {
Michal Vaskoba716542019-12-16 10:01:58 +0100794 if ((set->val.scnodes[i].in_ctx == 1) || (set->val.scnodes[i].in_ctx == -2)) {
795 idx = lyxp_set_scnode_insert_node(ret, set->val.scnodes[i].scnode, set->val.scnodes[i].type);
Michal Vasko3f27c522020-01-06 08:37:49 +0100796 /* coverity seems to think scnodes can be NULL */
797 if ((idx == -1) || !ret->val.scnodes) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200798 lyxp_set_free(ret);
799 return NULL;
800 }
Michal Vaskoba716542019-12-16 10:01:58 +0100801 ret->val.scnodes[idx].in_ctx = set->val.scnodes[i].in_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200802 }
803 }
804 } else if (set->type == LYXP_SET_NODE_SET) {
805 ret->type = set->type;
806 ret->val.nodes = malloc(set->used * sizeof *ret->val.nodes);
807 LY_CHECK_ERR_RET(!ret->val.nodes, LOGMEM(set->ctx); free(ret), NULL);
808 memcpy(ret->val.nodes, set->val.nodes, set->used * sizeof *ret->val.nodes);
809
810 ret->used = ret->size = set->used;
811 ret->ctx_pos = set->ctx_pos;
812 ret->ctx_size = set->ctx_size;
813 ret->ht = lyht_dup(set->ht);
814 } else {
815 memcpy(ret, set, sizeof *ret);
816 if (set->type == LYXP_SET_STRING) {
817 ret->val.str = strdup(set->val.str);
818 LY_CHECK_ERR_RET(!ret->val.str, LOGMEM(set->ctx); free(ret), NULL);
819 }
820 }
821
822 return ret;
823}
824
825/**
826 * @brief Fill XPath set with a string. Any current data are disposed of.
827 *
828 * @param[in] set Set to fill.
829 * @param[in] string String to fill into \p set.
830 * @param[in] str_len Length of \p string. 0 is a valid value!
831 */
832static void
833set_fill_string(struct lyxp_set *set, const char *string, uint16_t str_len)
834{
Michal Vaskod3678892020-05-21 10:06:58 +0200835 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200836
837 set->type = LYXP_SET_STRING;
838 if ((str_len == 0) && (string[0] != '\0')) {
839 string = "";
840 }
841 set->val.str = strndup(string, str_len);
842}
843
844/**
845 * @brief Fill XPath set with a number. Any current data are disposed of.
846 *
847 * @param[in] set Set to fill.
848 * @param[in] number Number to fill into \p set.
849 */
850static void
851set_fill_number(struct lyxp_set *set, long double number)
852{
Michal Vaskod3678892020-05-21 10:06:58 +0200853 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200854
855 set->type = LYXP_SET_NUMBER;
856 set->val.num = number;
857}
858
859/**
860 * @brief Fill XPath set with a boolean. Any current data are disposed of.
861 *
862 * @param[in] set Set to fill.
863 * @param[in] boolean Boolean to fill into \p set.
864 */
865static void
866set_fill_boolean(struct lyxp_set *set, int boolean)
867{
Michal Vaskod3678892020-05-21 10:06:58 +0200868 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200869
870 set->type = LYXP_SET_BOOLEAN;
871 set->val.bool = boolean;
872}
873
874/**
875 * @brief Fill XPath set with the value from another set (deep assign).
876 * Any current data are disposed of.
877 *
878 * @param[in] trg Set to fill.
879 * @param[in] src Source set to copy into \p trg.
880 */
881static void
882set_fill_set(struct lyxp_set *trg, struct lyxp_set *src)
883{
884 if (!trg || !src) {
885 return;
886 }
887
888 if (trg->type == LYXP_SET_NODE_SET) {
889 free(trg->val.nodes);
890 } else if (trg->type == LYXP_SET_STRING) {
891 free(trg->val.str);
892 }
893 set_init(trg, src);
894
895 if (src->type == LYXP_SET_SCNODE_SET) {
896 trg->type = LYXP_SET_SCNODE_SET;
897 trg->used = src->used;
898 trg->size = src->used;
899
900 trg->val.scnodes = ly_realloc(trg->val.scnodes, trg->size * sizeof *trg->val.scnodes);
901 LY_CHECK_ERR_RET(!trg->val.scnodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), );
902 memcpy(trg->val.scnodes, src->val.scnodes, src->used * sizeof *src->val.scnodes);
903 } else if (src->type == LYXP_SET_BOOLEAN) {
904 set_fill_boolean(trg, src->val.bool);
905 } else if (src->type == LYXP_SET_NUMBER) {
906 set_fill_number(trg, src->val.num);
907 } else if (src->type == LYXP_SET_STRING) {
908 set_fill_string(trg, src->val.str, strlen(src->val.str));
909 } else {
910 if (trg->type == LYXP_SET_NODE_SET) {
911 free(trg->val.nodes);
912 } else if (trg->type == LYXP_SET_STRING) {
913 free(trg->val.str);
914 }
915
Michal Vaskod3678892020-05-21 10:06:58 +0200916 assert(src->type == LYXP_SET_NODE_SET);
917
918 trg->type = LYXP_SET_NODE_SET;
919 trg->used = src->used;
920 trg->size = src->used;
921 trg->ctx_pos = src->ctx_pos;
922 trg->ctx_size = src->ctx_size;
923
924 trg->val.nodes = malloc(trg->used * sizeof *trg->val.nodes);
925 LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), );
926 memcpy(trg->val.nodes, src->val.nodes, src->used * sizeof *src->val.nodes);
927 if (src->ht) {
928 trg->ht = lyht_dup(src->ht);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200929 } else {
Michal Vaskod3678892020-05-21 10:06:58 +0200930 trg->ht = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200931 }
932 }
933}
934
935/**
936 * @brief Clear context of all schema nodes.
937 *
938 * @param[in] set Set to clear.
939 */
940static void
941set_scnode_clear_ctx(struct lyxp_set *set)
942{
943 uint32_t i;
944
945 for (i = 0; i < set->used; ++i) {
946 if (set->val.scnodes[i].in_ctx == 1) {
947 set->val.scnodes[i].in_ctx = 0;
Michal Vasko5c4e5892019-11-14 12:31:38 +0100948 } else if (set->val.scnodes[i].in_ctx == -2) {
949 set->val.scnodes[i].in_ctx = -1;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200950 }
951 }
952}
953
954/**
955 * @brief Remove a node from a set. Removing last node changes
956 * set into LYXP_SET_EMPTY. Context position aware.
957 *
958 * @param[in] set Set to use.
959 * @param[in] idx Index from @p set of the node to be removed.
960 */
961static void
962set_remove_node(struct lyxp_set *set, uint32_t idx)
963{
964 assert(set && (set->type == LYXP_SET_NODE_SET));
965 assert(idx < set->used);
966
967 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
968
969 --set->used;
970 if (set->used) {
971 memmove(&set->val.nodes[idx], &set->val.nodes[idx + 1],
972 (set->used - idx) * sizeof *set->val.nodes);
973 } else {
Michal Vaskod3678892020-05-21 10:06:58 +0200974 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200975 }
976}
977
978/**
Michal Vasko2caefc12019-11-14 16:07:56 +0100979 * @brief Remove a node from a set by setting its type to LYXP_NODE_NONE.
Michal Vasko57eab132019-09-24 11:46:26 +0200980 *
981 * @param[in] set Set to use.
982 * @param[in] idx Index from @p set of the node to be removed.
983 */
984static void
Michal Vasko2caefc12019-11-14 16:07:56 +0100985set_remove_node_none(struct lyxp_set *set, uint32_t idx)
Michal Vasko57eab132019-09-24 11:46:26 +0200986{
987 assert(set && (set->type == LYXP_SET_NODE_SET));
988 assert(idx < set->used);
989
Michal Vasko2caefc12019-11-14 16:07:56 +0100990 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
991 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
992 }
993 set->val.nodes[idx].type = LYXP_NODE_NONE;
Michal Vasko57eab132019-09-24 11:46:26 +0200994}
995
996/**
Michal Vasko2caefc12019-11-14 16:07:56 +0100997 * @brief Remove all LYXP_NODE_NONE nodes from a set. Removing last node changes
Michal Vasko57eab132019-09-24 11:46:26 +0200998 * set into LYXP_SET_EMPTY. Context position aware.
999 *
1000 * @param[in] set Set to consolidate.
1001 */
1002static void
Michal Vasko2caefc12019-11-14 16:07:56 +01001003set_remove_nodes_none(struct lyxp_set *set)
Michal Vasko57eab132019-09-24 11:46:26 +02001004{
1005 uint16_t i, orig_used, end;
1006 int32_t start;
1007
Michal Vaskod3678892020-05-21 10:06:58 +02001008 assert(set);
Michal Vasko57eab132019-09-24 11:46:26 +02001009
1010 orig_used = set->used;
1011 set->used = 0;
1012 for (i = 0; i < orig_used;) {
1013 start = -1;
1014 do {
Michal Vasko2caefc12019-11-14 16:07:56 +01001015 if ((set->val.nodes[i].type != LYXP_NODE_NONE) && (start == -1)) {
Michal Vasko57eab132019-09-24 11:46:26 +02001016 start = i;
Michal Vasko2caefc12019-11-14 16:07:56 +01001017 } else if ((start > -1) && (set->val.nodes[i].type == LYXP_NODE_NONE)) {
Michal Vasko57eab132019-09-24 11:46:26 +02001018 end = i;
1019 ++i;
1020 break;
1021 }
1022
1023 ++i;
1024 if (i == orig_used) {
1025 end = i;
1026 }
1027 } while (i < orig_used);
1028
1029 if (start > -1) {
1030 /* move the whole chunk of valid nodes together */
1031 if (set->used != (unsigned)start) {
1032 memmove(&set->val.nodes[set->used], &set->val.nodes[start], (end - start) * sizeof *set->val.nodes);
1033 }
1034 set->used += end - start;
1035 }
1036 }
Michal Vasko57eab132019-09-24 11:46:26 +02001037}
1038
1039/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02001040 * @brief Check for duplicates in a node set.
1041 *
1042 * @param[in] set Set to check.
1043 * @param[in] node Node to look for in @p set.
1044 * @param[in] node_type Type of @p node.
1045 * @param[in] skip_idx Index from @p set to skip.
1046 * @return LY_ERR
1047 */
1048static LY_ERR
1049set_dup_node_check(const struct lyxp_set *set, const struct lyd_node *node, enum lyxp_node_type node_type, int skip_idx)
1050{
1051 uint32_t i;
1052
Michal Vasko2caefc12019-11-14 16:07:56 +01001053 if (set->ht && node) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001054 return set_dup_node_hash_check(set, (struct lyd_node *)node, node_type, skip_idx);
1055 }
1056
1057 for (i = 0; i < set->used; ++i) {
1058 if ((skip_idx > -1) && (i == (unsigned)skip_idx)) {
1059 continue;
1060 }
1061
1062 if ((set->val.nodes[i].node == node) && (set->val.nodes[i].type == node_type)) {
1063 return LY_EEXIST;
1064 }
1065 }
1066
1067 return LY_SUCCESS;
1068}
1069
Michal Vaskoecd62de2019-11-13 12:35:11 +01001070int
1071lyxp_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 +02001072{
1073 uint32_t i;
1074
1075 for (i = 0; i < set->used; ++i) {
1076 if ((skip_idx > -1) && (i == (unsigned)skip_idx)) {
1077 continue;
1078 }
1079
1080 if ((set->val.scnodes[i].scnode == node) && (set->val.scnodes[i].type == node_type)) {
1081 return i;
1082 }
1083 }
1084
1085 return -1;
1086}
1087
Michal Vaskoecd62de2019-11-13 12:35:11 +01001088void
1089lyxp_set_scnode_merge(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001090{
1091 uint32_t orig_used, i, j;
1092
Michal Vaskod3678892020-05-21 10:06:58 +02001093 assert((set1->type == LYXP_SET_SCNODE_SET) && (set2->type == LYXP_SET_SCNODE_SET));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001094
Michal Vaskod3678892020-05-21 10:06:58 +02001095 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001096 return;
1097 }
1098
Michal Vaskod3678892020-05-21 10:06:58 +02001099 if (!set1->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001100 memcpy(set1, set2, sizeof *set1);
1101 return;
1102 }
1103
1104 if (set1->used + set2->used > set1->size) {
1105 set1->size = set1->used + set2->used;
1106 set1->val.scnodes = ly_realloc(set1->val.scnodes, set1->size * sizeof *set1->val.scnodes);
1107 LY_CHECK_ERR_RET(!set1->val.scnodes, LOGMEM(set1->ctx), );
1108 }
1109
1110 orig_used = set1->used;
1111
1112 for (i = 0; i < set2->used; ++i) {
1113 for (j = 0; j < orig_used; ++j) {
1114 /* detect duplicities */
1115 if (set1->val.scnodes[j].scnode == set2->val.scnodes[i].scnode) {
1116 break;
1117 }
1118 }
1119
1120 if (j == orig_used) {
1121 memcpy(&set1->val.scnodes[set1->used], &set2->val.scnodes[i], sizeof *set2->val.scnodes);
1122 ++set1->used;
1123 }
1124 }
1125
Michal Vaskod3678892020-05-21 10:06:58 +02001126 lyxp_set_free_content(set2);
1127 set2->type = LYXP_SET_SCNODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001128}
1129
1130/**
1131 * @brief Insert a node into a set. Context position aware.
1132 *
1133 * @param[in] set Set to use.
1134 * @param[in] node Node to insert to @p set.
1135 * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting.
1136 * @param[in] node_type Node type of @p node.
1137 * @param[in] idx Index in @p set to insert into.
1138 */
1139static void
1140set_insert_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx)
1141{
Michal Vaskod3678892020-05-21 10:06:58 +02001142 assert(set && (set->type == LYXP_SET_NODE_SET));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001143
Michal Vaskod3678892020-05-21 10:06:58 +02001144 if (!set->size) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001145 /* first item */
1146 if (idx) {
1147 /* no real harm done, but it is a bug */
1148 LOGINT(set->ctx);
1149 idx = 0;
1150 }
1151 set->val.nodes = malloc(LYXP_SET_SIZE_START * sizeof *set->val.nodes);
1152 LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), );
1153 set->type = LYXP_SET_NODE_SET;
1154 set->used = 0;
1155 set->size = LYXP_SET_SIZE_START;
1156 set->ctx_pos = 1;
1157 set->ctx_size = 1;
1158 set->ht = NULL;
1159 } else {
1160 /* not an empty set */
1161 if (set->used == set->size) {
1162
1163 /* set is full */
1164 set->val.nodes = ly_realloc(set->val.nodes, (set->size + LYXP_SET_SIZE_STEP) * sizeof *set->val.nodes);
1165 LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), );
1166 set->size += LYXP_SET_SIZE_STEP;
1167 }
1168
1169 if (idx > set->used) {
1170 LOGINT(set->ctx);
1171 idx = set->used;
1172 }
1173
1174 /* make space for the new node */
1175 if (idx < set->used) {
1176 memmove(&set->val.nodes[idx + 1], &set->val.nodes[idx], (set->used - idx) * sizeof *set->val.nodes);
1177 }
1178 }
1179
1180 /* finally assign the value */
1181 set->val.nodes[idx].node = (struct lyd_node *)node;
1182 set->val.nodes[idx].type = node_type;
1183 set->val.nodes[idx].pos = pos;
1184 ++set->used;
1185
Michal Vasko2caefc12019-11-14 16:07:56 +01001186 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1187 set_insert_node_hash(set, (struct lyd_node *)node, node_type);
1188 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001189}
1190
Michal Vaskoecd62de2019-11-13 12:35:11 +01001191int
1192lyxp_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 +02001193{
1194 int ret;
1195
1196 assert(set->type == LYXP_SET_SCNODE_SET);
1197
Michal Vaskoecd62de2019-11-13 12:35:11 +01001198 ret = lyxp_set_scnode_dup_node_check(set, node, node_type, -1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001199 if (ret > -1) {
1200 set->val.scnodes[ret].in_ctx = 1;
1201 } else {
1202 if (set->used == set->size) {
1203 set->val.scnodes = ly_realloc(set->val.scnodes, (set->size + LYXP_SET_SIZE_STEP) * sizeof *set->val.scnodes);
1204 LY_CHECK_ERR_RET(!set->val.scnodes, LOGMEM(set->ctx), -1);
1205 set->size += LYXP_SET_SIZE_STEP;
1206 }
1207
1208 ret = set->used;
1209 set->val.scnodes[ret].scnode = (struct lysc_node *)node;
1210 set->val.scnodes[ret].type = node_type;
1211 set->val.scnodes[ret].in_ctx = 1;
1212 ++set->used;
1213 }
1214
1215 return ret;
1216}
1217
1218/**
1219 * @brief Replace a node in a set with another. Context position aware.
1220 *
1221 * @param[in] set Set to use.
1222 * @param[in] node Node to insert to @p set.
1223 * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting.
1224 * @param[in] node_type Node type of @p node.
1225 * @param[in] idx Index in @p set of the node to replace.
1226 */
1227static void
1228set_replace_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx)
1229{
1230 assert(set && (idx < set->used));
1231
Michal Vasko2caefc12019-11-14 16:07:56 +01001232 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1233 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1234 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001235 set->val.nodes[idx].node = (struct lyd_node *)node;
1236 set->val.nodes[idx].type = node_type;
1237 set->val.nodes[idx].pos = pos;
Michal Vasko2caefc12019-11-14 16:07:56 +01001238 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1239 set_insert_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1240 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001241}
1242
1243/**
1244 * @brief Set all nodes with ctx 1 to a new unique context value.
1245 *
1246 * @param[in] set Set to modify.
1247 * @return New context value.
1248 */
Michal Vasko5c4e5892019-11-14 12:31:38 +01001249static int32_t
Michal Vasko03ff5a72019-09-11 13:49:33 +02001250set_scnode_new_in_ctx(struct lyxp_set *set)
1251{
Michal Vasko5c4e5892019-11-14 12:31:38 +01001252 uint32_t i;
1253 int32_t ret_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001254
1255 assert(set->type == LYXP_SET_SCNODE_SET);
1256
1257 ret_ctx = 3;
1258retry:
1259 for (i = 0; i < set->used; ++i) {
1260 if (set->val.scnodes[i].in_ctx >= ret_ctx) {
1261 ret_ctx = set->val.scnodes[i].in_ctx + 1;
1262 goto retry;
1263 }
1264 }
1265 for (i = 0; i < set->used; ++i) {
1266 if (set->val.scnodes[i].in_ctx == 1) {
1267 set->val.scnodes[i].in_ctx = ret_ctx;
1268 }
1269 }
1270
1271 return ret_ctx;
1272}
1273
1274/**
1275 * @brief Get unique @p node position in the data.
1276 *
1277 * @param[in] node Node to find.
1278 * @param[in] node_type Node type of @p node.
1279 * @param[in] root Root node.
1280 * @param[in] root_type Type of the XPath @p root node.
1281 * @param[in] prev Node that we think is before @p node in DFS from @p root. Can optionally
1282 * be used to increase efficiency and start the DFS from this node.
1283 * @param[in] prev_pos Node @p prev position. Optional, but must be set if @p prev is set.
1284 * @return Node position.
1285 */
1286static uint32_t
1287get_node_pos(const struct lyd_node *node, enum lyxp_node_type node_type, const struct lyd_node *root,
1288 enum lyxp_node_type root_type, const struct lyd_node **prev, uint32_t *prev_pos)
1289{
1290 const struct lyd_node *next, *elem, *top_sibling;
1291 uint32_t pos = 1;
1292
1293 assert(prev && prev_pos && !root->prev->next);
1294
1295 if ((node_type == LYXP_NODE_ROOT) || (node_type == LYXP_NODE_ROOT_CONFIG)) {
1296 return 0;
1297 }
1298
1299 if (*prev) {
1300 /* start from the previous element instead from the root */
1301 elem = next = *prev;
1302 pos = *prev_pos;
1303 for (top_sibling = elem; top_sibling->parent; top_sibling = (struct lyd_node *)top_sibling->parent);
1304 goto dfs_search;
1305 }
1306
1307 for (top_sibling = root; top_sibling; top_sibling = top_sibling->next) {
1308 /* TREE DFS */
1309 LYD_TREE_DFS_BEGIN(top_sibling, next, elem) {
1310dfs_search:
1311 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (elem->schema->flags & LYS_CONFIG_R)) {
1312 goto skip_children;
1313 }
1314
1315 if (elem == node) {
1316 break;
1317 }
1318 ++pos;
1319
1320 /* TREE DFS END */
1321 /* select element for the next run - children first,
1322 * child exception for lyd_node_leaf and lyd_node_leaflist, but not the root */
1323 if (elem->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) {
1324 next = NULL;
1325 } else {
1326 next = lyd_node_children(elem);
1327 }
1328 if (!next) {
1329skip_children:
1330 /* no children */
1331 if (elem == top_sibling) {
1332 /* we are done, root has no children */
1333 elem = NULL;
1334 break;
1335 }
1336 /* try siblings */
1337 next = elem->next;
1338 }
1339 while (!next) {
1340 /* no siblings, go back through parents */
1341 if (elem->parent == top_sibling->parent) {
1342 /* we are done, no next element to process */
1343 elem = NULL;
1344 break;
1345 }
1346 /* parent is already processed, go to its sibling */
1347 elem = (struct lyd_node *)elem->parent;
1348 next = elem->next;
1349 }
1350 }
1351
1352 /* node found */
1353 if (elem) {
1354 break;
1355 }
1356 }
1357
1358 if (!elem) {
1359 if (!(*prev)) {
1360 /* we went from root and failed to find it, cannot be */
1361 LOGINT(node->schema->module->ctx);
1362 return 0;
1363 } else {
1364 *prev = NULL;
1365 *prev_pos = 0;
1366
1367 elem = next = top_sibling = root;
1368 pos = 1;
1369 goto dfs_search;
1370 }
1371 }
1372
1373 /* remember the last found node for next time */
1374 *prev = node;
1375 *prev_pos = pos;
1376
1377 return pos;
1378}
1379
1380/**
1381 * @brief Assign (fill) missing node positions.
1382 *
1383 * @param[in] set Set to fill positions in.
1384 * @param[in] root Context root node.
1385 * @param[in] root_type Context root type.
1386 * @return LY_ERR
1387 */
1388static LY_ERR
1389set_assign_pos(struct lyxp_set *set, const struct lyd_node *root, enum lyxp_node_type root_type)
1390{
1391 const struct lyd_node *prev = NULL, *tmp_node;
1392 uint32_t i, tmp_pos = 0;
1393
1394 for (i = 0; i < set->used; ++i) {
1395 if (!set->val.nodes[i].pos) {
1396 tmp_node = NULL;
1397 switch (set->val.nodes[i].type) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001398 case LYXP_NODE_META:
1399 tmp_node = set->val.meta[i].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001400 if (!tmp_node) {
1401 LOGINT_RET(root->schema->module->ctx);
1402 }
1403 /* fallthrough */
1404 case LYXP_NODE_ELEM:
1405 case LYXP_NODE_TEXT:
1406 if (!tmp_node) {
1407 tmp_node = set->val.nodes[i].node;
1408 }
1409 set->val.nodes[i].pos = get_node_pos(tmp_node, set->val.nodes[i].type, root, root_type, &prev, &tmp_pos);
1410 break;
1411 default:
1412 /* all roots have position 0 */
1413 break;
1414 }
1415 }
1416 }
1417
1418 return LY_SUCCESS;
1419}
1420
1421/**
Michal Vasko9f96a052020-03-10 09:41:45 +01001422 * @brief Get unique @p meta position in the parent metadata.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001423 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001424 * @param[in] meta Metadata to use.
1425 * @return Metadata position.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001426 */
1427static uint16_t
Michal Vasko9f96a052020-03-10 09:41:45 +01001428get_meta_pos(struct lyd_meta *meta)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001429{
1430 uint16_t pos = 0;
Michal Vasko9f96a052020-03-10 09:41:45 +01001431 struct lyd_meta *meta2;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001432
Michal Vasko9f96a052020-03-10 09:41:45 +01001433 for (meta2 = meta->parent->meta; meta2 && (meta2 != meta); meta2 = meta2->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001434 ++pos;
1435 }
1436
Michal Vasko9f96a052020-03-10 09:41:45 +01001437 assert(meta2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001438 return pos;
1439}
1440
1441/**
1442 * @brief Compare 2 nodes in respect to XPath document order.
1443 *
1444 * @param[in] item1 1st node.
1445 * @param[in] item2 2nd node.
1446 * @return If 1st > 2nd returns 1, 1st == 2nd returns 0, and 1st < 2nd returns -1.
1447 */
1448static int
1449set_sort_compare(struct lyxp_set_node *item1, struct lyxp_set_node *item2)
1450{
Michal Vasko9f96a052020-03-10 09:41:45 +01001451 uint32_t meta_pos1 = 0, meta_pos2 = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001452
1453 if (item1->pos < item2->pos) {
1454 return -1;
1455 }
1456
1457 if (item1->pos > item2->pos) {
1458 return 1;
1459 }
1460
1461 /* node positions are equal, the fun case */
1462
1463 /* 1st ELEM - == - 2nd TEXT, 1st TEXT - == - 2nd ELEM */
1464 /* special case since text nodes are actually saved as their parents */
1465 if ((item1->node == item2->node) && (item1->type != item2->type)) {
1466 if (item1->type == LYXP_NODE_ELEM) {
1467 assert(item2->type == LYXP_NODE_TEXT);
1468 return -1;
1469 } else {
1470 assert((item1->type == LYXP_NODE_TEXT) && (item2->type == LYXP_NODE_ELEM));
1471 return 1;
1472 }
1473 }
1474
Michal Vasko9f96a052020-03-10 09:41:45 +01001475 /* we need meta positions now */
1476 if (item1->type == LYXP_NODE_META) {
1477 meta_pos1 = get_meta_pos((struct lyd_meta *)item1->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001478 }
Michal Vasko9f96a052020-03-10 09:41:45 +01001479 if (item2->type == LYXP_NODE_META) {
1480 meta_pos2 = get_meta_pos((struct lyd_meta *)item2->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001481 }
1482
Michal Vasko9f96a052020-03-10 09:41:45 +01001483 /* 1st ROOT - 2nd ROOT, 1st ELEM - 2nd ELEM, 1st TEXT - 2nd TEXT, 1st META - =pos= - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001484 /* check for duplicates */
1485 if (item1->node == item2->node) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001486 assert((item1->type == item2->type) && ((item1->type != LYXP_NODE_META) || (meta_pos1 == meta_pos2)));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001487 return 0;
1488 }
1489
Michal Vasko9f96a052020-03-10 09:41:45 +01001490 /* 1st ELEM - 2nd TEXT, 1st ELEM - any pos - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001491 /* elem is always first, 2nd node is after it */
1492 if (item1->type == LYXP_NODE_ELEM) {
1493 assert(item2->type != LYXP_NODE_ELEM);
1494 return -1;
1495 }
1496
Michal Vasko9f96a052020-03-10 09:41:45 +01001497 /* 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 +02001498 /* 2nd is before 1st */
1499 if (((item1->type == LYXP_NODE_TEXT)
Michal Vasko9f96a052020-03-10 09:41:45 +01001500 && ((item2->type == LYXP_NODE_ELEM) || (item2->type == LYXP_NODE_META)))
1501 || ((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_ELEM))
1502 || (((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_META))
1503 && (meta_pos1 > meta_pos2))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001504 return 1;
1505 }
1506
Michal Vasko9f96a052020-03-10 09:41:45 +01001507 /* 1st META - any pos - 2nd TEXT, 1st META <pos< - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001508 /* 2nd is after 1st */
1509 return -1;
1510}
1511
1512/**
1513 * @brief Set cast for comparisons.
1514 *
1515 * @param[in] trg Target set to cast source into.
1516 * @param[in] src Source set.
1517 * @param[in] type Target set type.
1518 * @param[in] src_idx Source set node index.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001519 * @return LY_ERR
1520 */
1521static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001522set_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 +02001523{
1524 assert(src->type == LYXP_SET_NODE_SET);
1525
1526 set_init(trg, src);
1527
1528 /* insert node into target set */
1529 set_insert_node(trg, src->val.nodes[src_idx].node, src->val.nodes[src_idx].pos, src->val.nodes[src_idx].type, 0);
1530
1531 /* cast target set appropriately */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001532 return lyxp_set_cast(trg, type);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001533}
1534
1535#ifndef NDEBUG
1536
1537/**
1538 * @brief Bubble sort @p set into XPath document order.
1539 * Context position aware. Unused in the 'Release' build target.
1540 *
1541 * @param[in] set Set to sort.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001542 * @return How many times the whole set was traversed - 1 (if set was sorted, returns 0).
1543 */
1544static int
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001545set_sort(struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001546{
1547 uint32_t i, j;
1548 int ret = 0, cmp, inverted, change;
1549 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001550 struct lyxp_set_node item;
1551 struct lyxp_set_hash_node hnode;
1552 uint64_t hash;
1553
Michal Vasko3cf8fbf2020-05-27 15:21:21 +02001554 if ((set->type != LYXP_SET_NODE_SET) || (set->used < 2)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001555 return 0;
1556 }
1557
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001558 /* find first top-level node to be used as anchor for positions */
1559 for (root = set->ctx_node; root->parent; root = (const struct lyd_node *)root->parent);
1560 for (; root->prev->next; root = root->prev);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001561
1562 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001563 if (set_assign_pos(set, root, set->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001564 return -1;
1565 }
1566
1567 LOGDBG(LY_LDGXPATH, "SORT BEGIN");
1568 print_set_debug(set);
1569
1570 for (i = 0; i < set->used; ++i) {
1571 inverted = 0;
1572 change = 0;
1573
1574 for (j = 1; j < set->used - i; ++j) {
1575 /* compare node positions */
1576 if (inverted) {
1577 cmp = set_sort_compare(&set->val.nodes[j], &set->val.nodes[j - 1]);
1578 } else {
1579 cmp = set_sort_compare(&set->val.nodes[j - 1], &set->val.nodes[j]);
1580 }
1581
1582 /* swap if needed */
1583 if ((inverted && (cmp < 0)) || (!inverted && (cmp > 0))) {
1584 change = 1;
1585
1586 item = set->val.nodes[j - 1];
1587 set->val.nodes[j - 1] = set->val.nodes[j];
1588 set->val.nodes[j] = item;
1589 } else {
1590 /* whether node_pos1 should be smaller than node_pos2 or the other way around */
1591 inverted = !inverted;
1592 }
1593 }
1594
1595 ++ret;
1596
1597 if (!change) {
1598 break;
1599 }
1600 }
1601
1602 LOGDBG(LY_LDGXPATH, "SORT END %d", ret);
1603 print_set_debug(set);
1604
1605 /* check node hashes */
1606 if (set->used >= LYD_HT_MIN_ITEMS) {
1607 assert(set->ht);
1608 for (i = 0; i < set->used; ++i) {
1609 hnode.node = set->val.nodes[i].node;
1610 hnode.type = set->val.nodes[i].type;
1611
1612 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
1613 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
1614 hash = dict_hash_multi(hash, NULL, 0);
1615
1616 assert(!lyht_find(set->ht, &hnode, hash, NULL));
1617 }
1618 }
1619
1620 return ret - 1;
1621}
1622
1623/**
1624 * @brief Remove duplicate entries in a sorted node set.
1625 *
1626 * @param[in] set Sorted set to check.
1627 * @return LY_ERR (LY_EEXIST if some duplicates are found)
1628 */
1629static LY_ERR
1630set_sorted_dup_node_clean(struct lyxp_set *set)
1631{
1632 uint32_t i = 0;
1633 LY_ERR ret = LY_SUCCESS;
1634
1635 if (set->used > 1) {
1636 while (i < set->used - 1) {
1637 if ((set->val.nodes[i].node == set->val.nodes[i + 1].node)
1638 && (set->val.nodes[i].type == set->val.nodes[i + 1].type)) {
Michal Vasko2caefc12019-11-14 16:07:56 +01001639 set_remove_node_none(set, i + 1);
Michal Vasko57eab132019-09-24 11:46:26 +02001640 ret = LY_EEXIST;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001641 }
Michal Vasko57eab132019-09-24 11:46:26 +02001642 ++i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001643 }
1644 }
1645
Michal Vasko2caefc12019-11-14 16:07:56 +01001646 set_remove_nodes_none(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001647 return ret;
1648}
1649
1650#endif
1651
1652/**
1653 * @brief Merge 2 sorted sets into one.
1654 *
1655 * @param[in,out] trg Set to merge into. Duplicates are removed.
1656 * @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 +02001657 * @return LY_ERR
1658 */
1659static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001660set_sorted_merge(struct lyxp_set *trg, struct lyxp_set *src)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001661{
1662 uint32_t i, j, k, count, dup_count;
1663 int cmp;
1664 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001665
Michal Vaskod3678892020-05-21 10:06:58 +02001666 if ((trg->type != LYXP_SET_NODE_SET) || (src->type != LYXP_SET_NODE_SET)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001667 return LY_EINVAL;
1668 }
1669
Michal Vaskod3678892020-05-21 10:06:58 +02001670 if (!src->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001671 return LY_SUCCESS;
Michal Vaskod3678892020-05-21 10:06:58 +02001672 } else if (!trg->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001673 set_fill_set(trg, src);
Michal Vaskod3678892020-05-21 10:06:58 +02001674 lyxp_set_free_content(src);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001675 return LY_SUCCESS;
1676 }
1677
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001678 /* find first top-level node to be used as anchor for positions */
1679 for (root = trg->ctx_node; root->parent; root = (const struct lyd_node *)root->parent);
1680 for (; root->prev->next; root = root->prev);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001681
1682 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001683 if (set_assign_pos(trg, root, trg->root_type) || set_assign_pos(src, root, src->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001684 return LY_EINT;
1685 }
1686
1687#ifndef NDEBUG
1688 LOGDBG(LY_LDGXPATH, "MERGE target");
1689 print_set_debug(trg);
1690 LOGDBG(LY_LDGXPATH, "MERGE source");
1691 print_set_debug(src);
1692#endif
1693
1694 /* make memory for the merge (duplicates are not detected yet, so space
1695 * will likely be wasted on them, too bad) */
1696 if (trg->size - trg->used < src->used) {
1697 trg->size = trg->used + src->used;
1698
1699 trg->val.nodes = ly_realloc(trg->val.nodes, trg->size * sizeof *trg->val.nodes);
1700 LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx), LY_EMEM);
1701 }
1702
1703 i = 0;
1704 j = 0;
1705 count = 0;
1706 dup_count = 0;
1707 do {
1708 cmp = set_sort_compare(&src->val.nodes[i], &trg->val.nodes[j]);
1709 if (!cmp) {
1710 if (!count) {
1711 /* duplicate, just skip it */
1712 ++i;
1713 ++j;
1714 } else {
1715 /* we are copying something already, so let's copy the duplicate too,
1716 * we are hoping that afterwards there are some more nodes to
1717 * copy and this way we can copy them all together */
1718 ++count;
1719 ++dup_count;
1720 ++i;
1721 ++j;
1722 }
1723 } else if (cmp < 0) {
1724 /* inserting src node into trg, just remember it for now */
1725 ++count;
1726 ++i;
1727
1728 /* insert the hash now */
1729 set_insert_node_hash(trg, src->val.nodes[i - 1].node, src->val.nodes[i - 1].type);
1730 } else if (count) {
1731copy_nodes:
1732 /* time to actually copy the nodes, we have found the largest block of nodes */
1733 memmove(&trg->val.nodes[j + (count - dup_count)],
1734 &trg->val.nodes[j],
1735 (trg->used - j) * sizeof *trg->val.nodes);
1736 memcpy(&trg->val.nodes[j - dup_count], &src->val.nodes[i - count], count * sizeof *src->val.nodes);
1737
1738 trg->used += count - dup_count;
1739 /* do not change i, except the copying above, we are basically doing exactly what is in the else branch below */
1740 j += count - dup_count;
1741
1742 count = 0;
1743 dup_count = 0;
1744 } else {
1745 ++j;
1746 }
1747 } while ((i < src->used) && (j < trg->used));
1748
1749 if ((i < src->used) || count) {
1750 /* insert all the hashes first */
1751 for (k = i; k < src->used; ++k) {
1752 set_insert_node_hash(trg, src->val.nodes[k].node, src->val.nodes[k].type);
1753 }
1754
1755 /* loop ended, but we need to copy something at trg end */
1756 count += src->used - i;
1757 i = src->used;
1758 goto copy_nodes;
1759 }
1760
1761 /* we are inserting hashes before the actual node insert, which causes
1762 * situations when there were initially not enough items for a hash table,
1763 * but even after some were inserted, hash table was not created (during
1764 * insertion the number of items is not updated yet) */
1765 if (!trg->ht && (trg->used >= LYD_HT_MIN_ITEMS)) {
1766 set_insert_node_hash(trg, NULL, 0);
1767 }
1768
1769#ifndef NDEBUG
1770 LOGDBG(LY_LDGXPATH, "MERGE result");
1771 print_set_debug(trg);
1772#endif
1773
Michal Vaskod3678892020-05-21 10:06:58 +02001774 lyxp_set_free_content(src);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001775 return LY_SUCCESS;
1776}
1777
1778/*
1779 * (re)parse functions
1780 *
1781 * Parse functions parse the expression into
1782 * tokens (syntactic analysis).
1783 *
1784 * Reparse functions perform semantic analysis
1785 * (do not save the result, just a check) of
1786 * the expression and fill repeat indices.
1787 */
1788
Michal Vasko14676352020-05-29 11:35:55 +02001789LY_ERR
1790lyxp_check_token(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t exp_idx, enum lyxp_token want_tok)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001791{
1792 if (exp->used == exp_idx) {
Michal Vasko14676352020-05-29 11:35:55 +02001793 if (ctx) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001794 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOF);
1795 }
Michal Vasko14676352020-05-29 11:35:55 +02001796 return LY_EINCOMPLETE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001797 }
1798
1799 if (want_tok && (exp->tokens[exp_idx] != want_tok)) {
Michal Vasko14676352020-05-29 11:35:55 +02001800 if (ctx) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001801 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK,
1802 print_token(exp->tokens[exp_idx]), &exp->expr[exp->tok_pos[exp_idx]]);
1803 }
Michal Vasko14676352020-05-29 11:35:55 +02001804 return LY_ENOT;
1805 }
1806
1807 return LY_SUCCESS;
1808}
1809
1810/* just like lyxp_check_token() but tests for 2 tokens */
1811static LY_ERR
1812exp_check_token2(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t exp_idx, enum lyxp_token want_tok1,
1813 enum lyxp_token want_tok2)
1814{
1815 if (exp->used == exp_idx) {
1816 if (ctx) {
1817 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOF);
1818 }
1819 return LY_EINCOMPLETE;
1820 }
1821
1822 if ((exp->tokens[exp_idx] != want_tok1) && (exp->tokens[exp_idx] != want_tok2)) {
1823 if (ctx) {
1824 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK,
1825 print_token(exp->tokens[exp_idx]), &exp->expr[exp->tok_pos[exp_idx]]);
1826 }
1827 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001828 }
1829
1830 return LY_SUCCESS;
1831}
1832
1833/**
1834 * @brief Stack operation push on the repeat array.
1835 *
1836 * @param[in] exp Expression to use.
1837 * @param[in] exp_idx Position in the expresion \p exp.
1838 * @param[in] repeat_op_idx Index from \p exp of the operator token. This value is pushed.
1839 */
1840static void
1841exp_repeat_push(struct lyxp_expr *exp, uint16_t exp_idx, uint16_t repeat_op_idx)
1842{
1843 uint16_t i;
1844
1845 if (exp->repeat[exp_idx]) {
1846 for (i = 0; exp->repeat[exp_idx][i]; ++i);
1847 exp->repeat[exp_idx] = realloc(exp->repeat[exp_idx], (i + 2) * sizeof *exp->repeat[exp_idx]);
1848 LY_CHECK_ERR_RET(!exp->repeat[exp_idx], LOGMEM(NULL), );
1849 exp->repeat[exp_idx][i] = repeat_op_idx;
1850 exp->repeat[exp_idx][i + 1] = 0;
1851 } else {
1852 exp->repeat[exp_idx] = calloc(2, sizeof *exp->repeat[exp_idx]);
1853 LY_CHECK_ERR_RET(!exp->repeat[exp_idx], LOGMEM(NULL), );
1854 exp->repeat[exp_idx][0] = repeat_op_idx;
1855 }
1856}
1857
1858/**
1859 * @brief Reparse Predicate. Logs directly on error.
1860 *
1861 * [7] Predicate ::= '[' Expr ']'
1862 *
1863 * @param[in] ctx Context for logging.
1864 * @param[in] exp Parsed XPath expression.
1865 * @param[in] exp_idx Position in the expression @p exp.
1866 * @return LY_ERR
1867 */
1868static LY_ERR
Michal Vasko14676352020-05-29 11:35:55 +02001869reparse_predicate(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *exp_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001870{
1871 LY_ERR rc;
1872
Michal Vasko14676352020-05-29 11:35:55 +02001873 rc = lyxp_check_token(ctx, exp, *exp_idx, LYXP_TOKEN_BRACK1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001874 LY_CHECK_RET(rc);
1875 ++(*exp_idx);
1876
1877 rc = reparse_or_expr(ctx, exp, exp_idx);
1878 LY_CHECK_RET(rc);
1879
Michal Vasko14676352020-05-29 11:35:55 +02001880 rc = lyxp_check_token(ctx, exp, *exp_idx, LYXP_TOKEN_BRACK2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001881 LY_CHECK_RET(rc);
1882 ++(*exp_idx);
1883
1884 return LY_SUCCESS;
1885}
1886
1887/**
1888 * @brief Reparse RelativeLocationPath. Logs directly on error.
1889 *
1890 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
1891 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
1892 * [6] NodeTest ::= NameTest | NodeType '(' ')'
1893 *
1894 * @param[in] ctx Context for logging.
1895 * @param[in] exp Parsed XPath expression.
1896 * @param[in] exp_idx Position in the expression \p exp.
1897 * @return LY_ERR (LY_EINCOMPLETE on forward reference)
1898 */
1899static LY_ERR
Michal Vasko14676352020-05-29 11:35:55 +02001900reparse_relative_location_path(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *exp_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001901{
1902 LY_ERR rc;
1903
Michal Vasko14676352020-05-29 11:35:55 +02001904 rc = lyxp_check_token(ctx, exp, *exp_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001905 LY_CHECK_RET(rc);
1906
1907 goto step;
1908 do {
1909 /* '/' or '//' */
1910 ++(*exp_idx);
1911
Michal Vasko14676352020-05-29 11:35:55 +02001912 rc = lyxp_check_token(ctx, exp, *exp_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001913 LY_CHECK_RET(rc);
1914step:
1915 /* Step */
1916 switch (exp->tokens[*exp_idx]) {
1917 case LYXP_TOKEN_DOT:
1918 ++(*exp_idx);
1919 break;
1920
1921 case LYXP_TOKEN_DDOT:
1922 ++(*exp_idx);
1923 break;
1924
1925 case LYXP_TOKEN_AT:
1926 ++(*exp_idx);
1927
Michal Vasko14676352020-05-29 11:35:55 +02001928 rc = lyxp_check_token(ctx, exp, *exp_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001929 LY_CHECK_RET(rc);
1930 if ((exp->tokens[*exp_idx] != LYXP_TOKEN_NAMETEST) && (exp->tokens[*exp_idx] != LYXP_TOKEN_NODETYPE)) {
1931 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK,
1932 print_token(exp->tokens[*exp_idx]), &exp->expr[exp->tok_pos[*exp_idx]]);
1933 return LY_EVALID;
1934 }
1935 /* fall through */
1936 case LYXP_TOKEN_NAMETEST:
1937 ++(*exp_idx);
1938 goto reparse_predicate;
1939 break;
1940
1941 case LYXP_TOKEN_NODETYPE:
1942 ++(*exp_idx);
1943
1944 /* '(' */
Michal Vasko14676352020-05-29 11:35:55 +02001945 rc = lyxp_check_token(ctx, exp, *exp_idx, LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001946 LY_CHECK_RET(rc);
1947 ++(*exp_idx);
1948
1949 /* ')' */
Michal Vasko14676352020-05-29 11:35:55 +02001950 rc = lyxp_check_token(ctx, exp, *exp_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001951 LY_CHECK_RET(rc);
1952 ++(*exp_idx);
1953
1954reparse_predicate:
1955 /* Predicate* */
Michal Vasko14676352020-05-29 11:35:55 +02001956 while (!lyxp_check_token(NULL, exp, *exp_idx, LYXP_TOKEN_BRACK1)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001957 rc = reparse_predicate(ctx, exp, exp_idx);
1958 LY_CHECK_RET(rc);
1959 }
1960 break;
1961 default:
1962 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK,
1963 print_token(exp->tokens[*exp_idx]), &exp->expr[exp->tok_pos[*exp_idx]]);
1964 return LY_EVALID;
1965 }
Michal Vasko14676352020-05-29 11:35:55 +02001966 } while (!exp_check_token2(NULL, exp, *exp_idx, LYXP_TOKEN_OPERATOR_PATH, LYXP_TOKEN_OPERATOR_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001967
1968 return LY_SUCCESS;
1969}
1970
1971/**
1972 * @brief Reparse AbsoluteLocationPath. Logs directly on error.
1973 *
1974 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
1975 *
1976 * @param[in] ctx Context for logging.
1977 * @param[in] exp Parsed XPath expression.
1978 * @param[in] exp_idx Position in the expression \p exp.
1979 * @return LY_ERR
1980 */
1981static LY_ERR
Michal Vasko14676352020-05-29 11:35:55 +02001982reparse_absolute_location_path(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *exp_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001983{
1984 LY_ERR rc;
1985
Michal Vasko14676352020-05-29 11:35:55 +02001986 rc = exp_check_token2(ctx, exp, *exp_idx, LYXP_TOKEN_OPERATOR_PATH, LYXP_TOKEN_OPERATOR_RPATH);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001987 LY_CHECK_RET(rc);
1988
1989 /* '/' RelativeLocationPath? */
Michal Vasko14676352020-05-29 11:35:55 +02001990 if (exp->tokens[*exp_idx] == LYXP_TOKEN_OPERATOR_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001991 /* '/' */
1992 ++(*exp_idx);
1993
Michal Vasko14676352020-05-29 11:35:55 +02001994 if (lyxp_check_token(NULL, exp, *exp_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001995 return LY_SUCCESS;
1996 }
1997 switch (exp->tokens[*exp_idx]) {
1998 case LYXP_TOKEN_DOT:
1999 case LYXP_TOKEN_DDOT:
2000 case LYXP_TOKEN_AT:
2001 case LYXP_TOKEN_NAMETEST:
2002 case LYXP_TOKEN_NODETYPE:
2003 rc = reparse_relative_location_path(ctx, exp, exp_idx);
2004 LY_CHECK_RET(rc);
2005 /* fall through */
2006 default:
2007 break;
2008 }
2009
2010 /* '//' RelativeLocationPath */
2011 } else {
2012 /* '//' */
2013 ++(*exp_idx);
2014
2015 rc = reparse_relative_location_path(ctx, exp, exp_idx);
2016 LY_CHECK_RET(rc);
2017 }
2018
2019 return LY_SUCCESS;
2020}
2021
2022/**
2023 * @brief Reparse FunctionCall. Logs directly on error.
2024 *
2025 * [9] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
2026 *
2027 * @param[in] ctx Context for logging.
2028 * @param[in] exp Parsed XPath expression.
2029 * @param[in] exp_idx Position in the expression @p exp.
2030 * @return LY_ERR
2031 */
2032static LY_ERR
Michal Vasko14676352020-05-29 11:35:55 +02002033reparse_function_call(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *exp_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002034{
2035 int min_arg_count = -1, max_arg_count, arg_count;
2036 uint16_t func_exp_idx;
2037 LY_ERR rc;
2038
Michal Vasko14676352020-05-29 11:35:55 +02002039 rc = lyxp_check_token(ctx, exp, *exp_idx, LYXP_TOKEN_FUNCNAME);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002040 LY_CHECK_RET(rc);
2041 func_exp_idx = *exp_idx;
2042 switch (exp->tok_len[*exp_idx]) {
2043 case 3:
2044 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "not", 3)) {
2045 min_arg_count = 1;
2046 max_arg_count = 1;
2047 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "sum", 3)) {
2048 min_arg_count = 1;
2049 max_arg_count = 1;
2050 }
2051 break;
2052 case 4:
2053 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "lang", 4)) {
2054 min_arg_count = 1;
2055 max_arg_count = 1;
2056 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "last", 4)) {
2057 min_arg_count = 0;
2058 max_arg_count = 0;
2059 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "name", 4)) {
2060 min_arg_count = 0;
2061 max_arg_count = 1;
2062 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "true", 4)) {
2063 min_arg_count = 0;
2064 max_arg_count = 0;
2065 }
2066 break;
2067 case 5:
2068 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "count", 5)) {
2069 min_arg_count = 1;
2070 max_arg_count = 1;
2071 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "false", 5)) {
2072 min_arg_count = 0;
2073 max_arg_count = 0;
2074 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "floor", 5)) {
2075 min_arg_count = 1;
2076 max_arg_count = 1;
2077 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "round", 5)) {
2078 min_arg_count = 1;
2079 max_arg_count = 1;
2080 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "deref", 5)) {
2081 min_arg_count = 1;
2082 max_arg_count = 1;
2083 }
2084 break;
2085 case 6:
2086 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "concat", 6)) {
2087 min_arg_count = 2;
Michal Vaskobe2e3562019-10-15 15:35:35 +02002088 max_arg_count = INT_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002089 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "number", 6)) {
2090 min_arg_count = 0;
2091 max_arg_count = 1;
2092 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "string", 6)) {
2093 min_arg_count = 0;
2094 max_arg_count = 1;
2095 }
2096 break;
2097 case 7:
2098 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "boolean", 7)) {
2099 min_arg_count = 1;
2100 max_arg_count = 1;
2101 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "ceiling", 7)) {
2102 min_arg_count = 1;
2103 max_arg_count = 1;
2104 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "current", 7)) {
2105 min_arg_count = 0;
2106 max_arg_count = 0;
2107 }
2108 break;
2109 case 8:
2110 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "contains", 8)) {
2111 min_arg_count = 2;
2112 max_arg_count = 2;
2113 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "position", 8)) {
2114 min_arg_count = 0;
2115 max_arg_count = 0;
2116 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "re-match", 8)) {
2117 min_arg_count = 2;
2118 max_arg_count = 2;
2119 }
2120 break;
2121 case 9:
2122 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "substring", 9)) {
2123 min_arg_count = 2;
2124 max_arg_count = 3;
2125 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "translate", 9)) {
2126 min_arg_count = 3;
2127 max_arg_count = 3;
2128 }
2129 break;
2130 case 10:
2131 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "local-name", 10)) {
2132 min_arg_count = 0;
2133 max_arg_count = 1;
2134 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "enum-value", 10)) {
2135 min_arg_count = 1;
2136 max_arg_count = 1;
2137 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "bit-is-set", 10)) {
2138 min_arg_count = 2;
2139 max_arg_count = 2;
2140 }
2141 break;
2142 case 11:
2143 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "starts-with", 11)) {
2144 min_arg_count = 2;
2145 max_arg_count = 2;
2146 }
2147 break;
2148 case 12:
2149 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "derived-from", 12)) {
2150 min_arg_count = 2;
2151 max_arg_count = 2;
2152 }
2153 break;
2154 case 13:
2155 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "namespace-uri", 13)) {
2156 min_arg_count = 0;
2157 max_arg_count = 1;
2158 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "string-length", 13)) {
2159 min_arg_count = 0;
2160 max_arg_count = 1;
2161 }
2162 break;
2163 case 15:
2164 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "normalize-space", 15)) {
2165 min_arg_count = 0;
2166 max_arg_count = 1;
2167 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "substring-after", 15)) {
2168 min_arg_count = 2;
2169 max_arg_count = 2;
2170 }
2171 break;
2172 case 16:
2173 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "substring-before", 16)) {
2174 min_arg_count = 2;
2175 max_arg_count = 2;
2176 }
2177 break;
2178 case 20:
2179 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "derived-from-or-self", 20)) {
2180 min_arg_count = 2;
2181 max_arg_count = 2;
2182 }
2183 break;
2184 }
2185 if (min_arg_count == -1) {
2186 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INFUNC, exp->tok_len[*exp_idx], &exp->expr[exp->tok_pos[*exp_idx]]);
2187 return LY_EINVAL;
2188 }
2189 ++(*exp_idx);
2190
2191 /* '(' */
Michal Vasko14676352020-05-29 11:35:55 +02002192 rc = lyxp_check_token(ctx, exp, *exp_idx, LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002193 LY_CHECK_RET(rc);
2194 ++(*exp_idx);
2195
2196 /* ( Expr ( ',' Expr )* )? */
2197 arg_count = 0;
Michal Vasko14676352020-05-29 11:35:55 +02002198 rc = lyxp_check_token(ctx, exp, *exp_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002199 LY_CHECK_RET(rc);
2200 if (exp->tokens[*exp_idx] != LYXP_TOKEN_PAR2) {
2201 ++arg_count;
2202 rc = reparse_or_expr(ctx, exp, exp_idx);
2203 LY_CHECK_RET(rc);
2204 }
Michal Vasko14676352020-05-29 11:35:55 +02002205 while (!lyxp_check_token(NULL, exp, *exp_idx, LYXP_TOKEN_COMMA)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002206 ++(*exp_idx);
2207
2208 ++arg_count;
2209 rc = reparse_or_expr(ctx, exp, exp_idx);
2210 LY_CHECK_RET(rc);
2211 }
2212
2213 /* ')' */
Michal Vasko14676352020-05-29 11:35:55 +02002214 rc = lyxp_check_token(ctx, exp, *exp_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002215 LY_CHECK_RET(rc);
2216 ++(*exp_idx);
2217
2218 if ((arg_count < min_arg_count) || (arg_count > max_arg_count)) {
2219 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INARGCOUNT, arg_count, exp->tok_len[func_exp_idx],
2220 &exp->expr[exp->tok_pos[func_exp_idx]]);
2221 return LY_EVALID;
2222 }
2223
2224 return LY_SUCCESS;
2225}
2226
2227/**
2228 * @brief Reparse PathExpr. Logs directly on error.
2229 *
2230 * [10] PathExpr ::= LocationPath | PrimaryExpr Predicate*
2231 * | PrimaryExpr Predicate* '/' RelativeLocationPath
2232 * | PrimaryExpr Predicate* '//' RelativeLocationPath
2233 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
2234 * [8] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall
2235 *
2236 * @param[in] ctx Context for logging.
2237 * @param[in] exp Parsed XPath expression.
2238 * @param[in] exp_idx Position in the expression @p exp.
2239 * @return LY_ERR
2240 */
2241static LY_ERR
Michal Vasko14676352020-05-29 11:35:55 +02002242reparse_path_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *exp_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002243{
2244 LY_ERR rc;
2245
Michal Vasko14676352020-05-29 11:35:55 +02002246 if (lyxp_check_token(ctx, exp, *exp_idx, LYXP_TOKEN_NONE)) {
2247 return LY_EVALID;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002248 }
2249
2250 switch (exp->tokens[*exp_idx]) {
2251 case LYXP_TOKEN_PAR1:
2252 /* '(' Expr ')' Predicate* */
2253 ++(*exp_idx);
2254
2255 rc = reparse_or_expr(ctx, exp, exp_idx);
2256 LY_CHECK_RET(rc);
2257
Michal Vasko14676352020-05-29 11:35:55 +02002258 rc = lyxp_check_token(ctx, exp, *exp_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002259 LY_CHECK_RET(rc);
2260 ++(*exp_idx);
2261 goto predicate;
2262 break;
2263 case LYXP_TOKEN_DOT:
2264 case LYXP_TOKEN_DDOT:
2265 case LYXP_TOKEN_AT:
2266 case LYXP_TOKEN_NAMETEST:
2267 case LYXP_TOKEN_NODETYPE:
2268 /* RelativeLocationPath */
2269 rc = reparse_relative_location_path(ctx, exp, exp_idx);
2270 LY_CHECK_RET(rc);
2271 break;
2272 case LYXP_TOKEN_FUNCNAME:
2273 /* FunctionCall */
2274 rc = reparse_function_call(ctx, exp, exp_idx);
2275 LY_CHECK_RET(rc);
2276 goto predicate;
2277 break;
2278 case LYXP_TOKEN_OPERATOR_PATH:
Michal Vasko14676352020-05-29 11:35:55 +02002279 case LYXP_TOKEN_OPERATOR_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02002280 /* AbsoluteLocationPath */
2281 rc = reparse_absolute_location_path(ctx, exp, exp_idx);
2282 LY_CHECK_RET(rc);
2283 break;
2284 case LYXP_TOKEN_LITERAL:
2285 /* Literal */
2286 ++(*exp_idx);
2287 goto predicate;
2288 break;
2289 case LYXP_TOKEN_NUMBER:
2290 /* Number */
2291 ++(*exp_idx);
2292 goto predicate;
2293 break;
2294 default:
2295 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK,
2296 print_token(exp->tokens[*exp_idx]), &exp->expr[exp->tok_pos[*exp_idx]]);
2297 return LY_EVALID;
2298 }
2299
2300 return LY_SUCCESS;
2301
2302predicate:
2303 /* Predicate* */
Michal Vasko14676352020-05-29 11:35:55 +02002304 while (!lyxp_check_token(NULL, exp, *exp_idx, LYXP_TOKEN_BRACK1)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002305 rc = reparse_predicate(ctx, exp, exp_idx);
2306 LY_CHECK_RET(rc);
2307 }
2308
2309 /* ('/' or '//') RelativeLocationPath */
Michal Vasko14676352020-05-29 11:35:55 +02002310 if (!exp_check_token2(NULL, exp, *exp_idx, LYXP_TOKEN_OPERATOR_PATH, LYXP_TOKEN_OPERATOR_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002311
2312 /* '/' or '//' */
2313 ++(*exp_idx);
2314
2315 rc = reparse_relative_location_path(ctx, exp, exp_idx);
2316 LY_CHECK_RET(rc);
2317 }
2318
2319 return LY_SUCCESS;
2320}
2321
2322/**
2323 * @brief Reparse UnaryExpr. Logs directly on error.
2324 *
2325 * [17] UnaryExpr ::= UnionExpr | '-' UnaryExpr
2326 * [18] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
2327 *
2328 * @param[in] ctx Context for logging.
2329 * @param[in] exp Parsed XPath expression.
2330 * @param[in] exp_idx Position in the expression @p exp.
2331 * @return LY_ERR
2332 */
2333static LY_ERR
Michal Vasko14676352020-05-29 11:35:55 +02002334reparse_unary_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *exp_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002335{
2336 uint16_t prev_exp;
2337 LY_ERR rc;
2338
2339 /* ('-')* */
2340 prev_exp = *exp_idx;
Michal Vasko14676352020-05-29 11:35:55 +02002341 while (!lyxp_check_token(NULL, exp, *exp_idx, LYXP_TOKEN_OPERATOR_MATH)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002342 && (exp->expr[exp->tok_pos[*exp_idx]] == '-')) {
2343 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNARY);
2344 ++(*exp_idx);
2345 }
2346
2347 /* PathExpr */
2348 prev_exp = *exp_idx;
2349 rc = reparse_path_expr(ctx, exp, exp_idx);
2350 LY_CHECK_RET(rc);
2351
2352 /* ('|' PathExpr)* */
Michal Vasko14676352020-05-29 11:35:55 +02002353 while (!lyxp_check_token(NULL, exp, *exp_idx, LYXP_TOKEN_OPERATOR_UNI)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002354 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNION);
2355 ++(*exp_idx);
2356
2357 rc = reparse_path_expr(ctx, exp, exp_idx);
2358 LY_CHECK_RET(rc);
2359 }
2360
2361 return LY_SUCCESS;
2362}
2363
2364/**
2365 * @brief Reparse AdditiveExpr. Logs directly on error.
2366 *
2367 * [15] AdditiveExpr ::= MultiplicativeExpr
2368 * | AdditiveExpr '+' MultiplicativeExpr
2369 * | AdditiveExpr '-' MultiplicativeExpr
2370 * [16] MultiplicativeExpr ::= UnaryExpr
2371 * | MultiplicativeExpr '*' UnaryExpr
2372 * | MultiplicativeExpr 'div' UnaryExpr
2373 * | MultiplicativeExpr 'mod' UnaryExpr
2374 *
2375 * @param[in] ctx Context for logging.
2376 * @param[in] exp Parsed XPath expression.
2377 * @param[in] exp_idx Position in the expression @p exp.
2378 * @return LY_ERR
2379 */
2380static LY_ERR
Michal Vasko14676352020-05-29 11:35:55 +02002381reparse_additive_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *exp_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002382{
2383 uint16_t prev_add_exp, prev_mul_exp;
2384 LY_ERR rc;
2385
2386 prev_add_exp = *exp_idx;
2387 goto reparse_multiplicative_expr;
2388
2389 /* ('+' / '-' MultiplicativeExpr)* */
Michal Vasko14676352020-05-29 11:35:55 +02002390 while (!lyxp_check_token(NULL, exp, *exp_idx, LYXP_TOKEN_OPERATOR_MATH)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002391 && ((exp->expr[exp->tok_pos[*exp_idx]] == '+') || (exp->expr[exp->tok_pos[*exp_idx]] == '-'))) {
2392 exp_repeat_push(exp, prev_add_exp, LYXP_EXPR_ADDITIVE);
2393 ++(*exp_idx);
2394
2395reparse_multiplicative_expr:
2396 /* UnaryExpr */
2397 prev_mul_exp = *exp_idx;
2398 rc = reparse_unary_expr(ctx, exp, exp_idx);
2399 LY_CHECK_RET(rc);
2400
2401 /* ('*' / 'div' / 'mod' UnaryExpr)* */
Michal Vasko14676352020-05-29 11:35:55 +02002402 while (!lyxp_check_token(NULL, exp, *exp_idx, LYXP_TOKEN_OPERATOR_MATH)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002403 && ((exp->expr[exp->tok_pos[*exp_idx]] == '*') || (exp->tok_len[*exp_idx] == 3))) {
2404 exp_repeat_push(exp, prev_mul_exp, LYXP_EXPR_MULTIPLICATIVE);
2405 ++(*exp_idx);
2406
2407 rc = reparse_unary_expr(ctx, exp, exp_idx);
2408 LY_CHECK_RET(rc);
2409 }
2410 }
2411
2412 return LY_SUCCESS;
2413}
2414
2415/**
2416 * @brief Reparse EqualityExpr. Logs directly on error.
2417 *
2418 * [13] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
2419 * | EqualityExpr '!=' RelationalExpr
2420 * [14] RelationalExpr ::= AdditiveExpr
2421 * | RelationalExpr '<' AdditiveExpr
2422 * | RelationalExpr '>' AdditiveExpr
2423 * | RelationalExpr '<=' AdditiveExpr
2424 * | RelationalExpr '>=' AdditiveExpr
2425 *
2426 * @param[in] ctx Context for logging.
2427 * @param[in] exp Parsed XPath expression.
2428 * @param[in] exp_idx Position in the expression @p exp.
2429 * @return LY_ERR
2430 */
2431static LY_ERR
Michal Vasko14676352020-05-29 11:35:55 +02002432reparse_equality_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *exp_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002433{
2434 uint16_t prev_eq_exp, prev_rel_exp;
2435 LY_ERR rc;
2436
2437 prev_eq_exp = *exp_idx;
2438 goto reparse_additive_expr;
2439
2440 /* ('=' / '!=' RelationalExpr)* */
Michal Vasko14676352020-05-29 11:35:55 +02002441 while (!lyxp_check_token(NULL, exp, *exp_idx, LYXP_TOKEN_OPERATOR_COMP)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002442 && ((exp->expr[exp->tok_pos[*exp_idx]] == '=') || (exp->expr[exp->tok_pos[*exp_idx]] == '!'))) {
2443 exp_repeat_push(exp, prev_eq_exp, LYXP_EXPR_EQUALITY);
2444 ++(*exp_idx);
2445
2446reparse_additive_expr:
2447 /* AdditiveExpr */
2448 prev_rel_exp = *exp_idx;
2449 rc = reparse_additive_expr(ctx, exp, exp_idx);
2450 LY_CHECK_RET(rc);
2451
2452 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
Michal Vasko14676352020-05-29 11:35:55 +02002453 while (!lyxp_check_token(NULL, exp, *exp_idx, LYXP_TOKEN_OPERATOR_COMP)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002454 && ((exp->expr[exp->tok_pos[*exp_idx]] == '<') || (exp->expr[exp->tok_pos[*exp_idx]] == '>'))) {
2455 exp_repeat_push(exp, prev_rel_exp, LYXP_EXPR_RELATIONAL);
2456 ++(*exp_idx);
2457
2458 rc = reparse_additive_expr(ctx, exp, exp_idx);
2459 LY_CHECK_RET(rc);
2460 }
2461 }
2462
2463 return LY_SUCCESS;
2464}
2465
2466/**
2467 * @brief Reparse OrExpr. Logs directly on error.
2468 *
2469 * [11] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
2470 * [12] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
2471 *
2472 * @param[in] ctx Context for logging.
2473 * @param[in] exp Parsed XPath expression.
2474 * @param[in] exp_idx Position in the expression @p exp.
2475 * @return LY_ERR
2476 */
2477static LY_ERR
Michal Vasko14676352020-05-29 11:35:55 +02002478reparse_or_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *exp_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002479{
2480 uint16_t prev_or_exp, prev_and_exp;
2481 LY_ERR rc;
2482
2483 prev_or_exp = *exp_idx;
2484 goto reparse_equality_expr;
2485
2486 /* ('or' AndExpr)* */
Michal Vasko14676352020-05-29 11:35:55 +02002487 while (!lyxp_check_token(NULL, exp, *exp_idx, LYXP_TOKEN_OPERATOR_LOG) && (exp->tok_len[*exp_idx] == 2)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002488 exp_repeat_push(exp, prev_or_exp, LYXP_EXPR_OR);
2489 ++(*exp_idx);
2490
2491reparse_equality_expr:
2492 /* EqualityExpr */
2493 prev_and_exp = *exp_idx;
2494 rc = reparse_equality_expr(ctx, exp, exp_idx);
2495 LY_CHECK_RET(rc);
2496
2497 /* ('and' EqualityExpr)* */
Michal Vasko14676352020-05-29 11:35:55 +02002498 while (!lyxp_check_token(NULL, exp, *exp_idx, LYXP_TOKEN_OPERATOR_LOG) && (exp->tok_len[*exp_idx] == 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002499 exp_repeat_push(exp, prev_and_exp, LYXP_EXPR_AND);
2500 ++(*exp_idx);
2501
2502 rc = reparse_equality_expr(ctx, exp, exp_idx);
2503 LY_CHECK_RET(rc);
2504 }
2505 }
2506
2507 return LY_SUCCESS;
2508}
Radek Krejcib1646a92018-11-02 16:08:26 +01002509
2510/**
2511 * @brief Parse NCName.
2512 *
2513 * @param[in] ncname Name to parse.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002514 * @return Length of @p ncname valid bytes.
Radek Krejcib1646a92018-11-02 16:08:26 +01002515 */
Radek Krejcid4270262019-01-07 15:07:25 +01002516static long int
Radek Krejcib1646a92018-11-02 16:08:26 +01002517parse_ncname(const char *ncname)
2518{
2519 unsigned int uc;
Radek Krejcid4270262019-01-07 15:07:25 +01002520 size_t size;
2521 long int len = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002522
2523 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), 0);
2524 if (!is_xmlqnamestartchar(uc) || (uc == ':')) {
2525 return len;
2526 }
2527
2528 do {
2529 len += size;
Radek Krejci9a564c92019-01-07 14:53:57 +01002530 if (!*ncname) {
2531 break;
2532 }
Radek Krejcid4270262019-01-07 15:07:25 +01002533 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), -len);
Radek Krejcib1646a92018-11-02 16:08:26 +01002534 } while (is_xmlqnamechar(uc) && (uc != ':'));
2535
2536 return len;
2537}
2538
2539/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02002540 * @brief Add @p token into the expression @p exp.
Radek Krejcib1646a92018-11-02 16:08:26 +01002541 *
Michal Vasko03ff5a72019-09-11 13:49:33 +02002542 * @param[in] ctx Context for logging.
Radek Krejcib1646a92018-11-02 16:08:26 +01002543 * @param[in] exp Expression to use.
2544 * @param[in] token Token to add.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002545 * @param[in] tok_pos Token position in the XPath expression.
Radek Krejcib1646a92018-11-02 16:08:26 +01002546 * @param[in] tok_len Token length in the XPath expression.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002547 * @return LY_ERR
Radek Krejcib1646a92018-11-02 16:08:26 +01002548 */
2549static LY_ERR
Michal Vasko14676352020-05-29 11:35:55 +02002550exp_add_token(const struct ly_ctx *ctx, struct lyxp_expr *exp, enum lyxp_token token, uint16_t tok_pos, uint16_t tok_len)
Radek Krejcib1646a92018-11-02 16:08:26 +01002551{
2552 uint32_t prev;
2553
2554 if (exp->used == exp->size) {
2555 prev = exp->size;
2556 exp->size += LYXP_EXPR_SIZE_STEP;
2557 if (prev > exp->size) {
2558 LOGINT(ctx);
2559 return LY_EINT;
2560 }
2561
2562 exp->tokens = ly_realloc(exp->tokens, exp->size * sizeof *exp->tokens);
2563 LY_CHECK_ERR_RET(!exp->tokens, LOGMEM(ctx), LY_EMEM);
2564 exp->tok_pos = ly_realloc(exp->tok_pos, exp->size * sizeof *exp->tok_pos);
2565 LY_CHECK_ERR_RET(!exp->tok_pos, LOGMEM(ctx), LY_EMEM);
2566 exp->tok_len = ly_realloc(exp->tok_len, exp->size * sizeof *exp->tok_len);
2567 LY_CHECK_ERR_RET(!exp->tok_len, LOGMEM(ctx), LY_EMEM);
2568 }
2569
2570 exp->tokens[exp->used] = token;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002571 exp->tok_pos[exp->used] = tok_pos;
Radek Krejcib1646a92018-11-02 16:08:26 +01002572 exp->tok_len[exp->used] = tok_len;
2573 ++exp->used;
2574 return LY_SUCCESS;
2575}
2576
2577void
Michal Vasko14676352020-05-29 11:35:55 +02002578lyxp_expr_free(const struct ly_ctx *ctx, struct lyxp_expr *expr)
Radek Krejcib1646a92018-11-02 16:08:26 +01002579{
2580 uint16_t i;
2581
2582 if (!expr) {
2583 return;
2584 }
2585
2586 lydict_remove(ctx, expr->expr);
2587 free(expr->tokens);
2588 free(expr->tok_pos);
2589 free(expr->tok_len);
2590 if (expr->repeat) {
2591 for (i = 0; i < expr->used; ++i) {
2592 free(expr->repeat[i]);
2593 }
2594 }
2595 free(expr->repeat);
2596 free(expr);
2597}
2598
2599struct lyxp_expr *
Michal Vasko14676352020-05-29 11:35:55 +02002600lyxp_expr_parse(const struct ly_ctx *ctx, const char *expr)
Radek Krejcib1646a92018-11-02 16:08:26 +01002601{
2602 struct lyxp_expr *ret;
Radek Krejcid4270262019-01-07 15:07:25 +01002603 size_t parsed = 0, tok_len;
2604 long int ncname_len;
Radek Krejcib1646a92018-11-02 16:08:26 +01002605 enum lyxp_token tok_type;
2606 int prev_function_check = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002607 uint16_t exp_idx = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002608
2609 if (strlen(expr) > UINT16_MAX) {
2610 LOGERR(ctx, LY_EINVAL, "XPath expression cannot be longer than %ud characters.", UINT16_MAX);
2611 return NULL;
2612 }
2613
2614 /* init lyxp_expr structure */
2615 ret = calloc(1, sizeof *ret);
2616 LY_CHECK_ERR_GOTO(!ret, LOGMEM(ctx), error);
2617 ret->expr = lydict_insert(ctx, expr, strlen(expr));
2618 LY_CHECK_ERR_GOTO(!ret->expr, LOGMEM(ctx), error);
2619 ret->used = 0;
2620 ret->size = LYXP_EXPR_SIZE_START;
2621 ret->tokens = malloc(ret->size * sizeof *ret->tokens);
2622 LY_CHECK_ERR_GOTO(!ret->tokens, LOGMEM(ctx), error);
2623
2624 ret->tok_pos = malloc(ret->size * sizeof *ret->tok_pos);
2625 LY_CHECK_ERR_GOTO(!ret->tok_pos, LOGMEM(ctx), error);
2626
2627 ret->tok_len = malloc(ret->size * sizeof *ret->tok_len);
2628 LY_CHECK_ERR_GOTO(!ret->tok_len, LOGMEM(ctx), error);
2629
2630 while (is_xmlws(expr[parsed])) {
2631 ++parsed;
2632 }
2633
2634 do {
2635 if (expr[parsed] == '(') {
2636
2637 /* '(' */
2638 tok_len = 1;
2639 tok_type = LYXP_TOKEN_PAR1;
2640
2641 if (prev_function_check && ret->used && (ret->tokens[ret->used - 1] == LYXP_TOKEN_NAMETEST)) {
2642 /* it is a NodeType/FunctionName after all */
2643 if (((ret->tok_len[ret->used - 1] == 4)
2644 && (!strncmp(&expr[ret->tok_pos[ret->used - 1]], "node", 4)
2645 || !strncmp(&expr[ret->tok_pos[ret->used - 1]], "text", 4))) ||
2646 ((ret->tok_len[ret->used - 1] == 7)
2647 && !strncmp(&expr[ret->tok_pos[ret->used - 1]], "comment", 7))) {
2648 ret->tokens[ret->used - 1] = LYXP_TOKEN_NODETYPE;
2649 } else {
2650 ret->tokens[ret->used - 1] = LYXP_TOKEN_FUNCNAME;
2651 }
2652 prev_function_check = 0;
2653 }
2654
2655 } else if (expr[parsed] == ')') {
2656
2657 /* ')' */
2658 tok_len = 1;
2659 tok_type = LYXP_TOKEN_PAR2;
2660
2661 } else if (expr[parsed] == '[') {
2662
2663 /* '[' */
2664 tok_len = 1;
2665 tok_type = LYXP_TOKEN_BRACK1;
2666
2667 } else if (expr[parsed] == ']') {
2668
2669 /* ']' */
2670 tok_len = 1;
2671 tok_type = LYXP_TOKEN_BRACK2;
2672
2673 } else if (!strncmp(&expr[parsed], "..", 2)) {
2674
2675 /* '..' */
2676 tok_len = 2;
2677 tok_type = LYXP_TOKEN_DDOT;
2678
2679 } else if ((expr[parsed] == '.') && (!isdigit(expr[parsed + 1]))) {
2680
2681 /* '.' */
2682 tok_len = 1;
2683 tok_type = LYXP_TOKEN_DOT;
2684
2685 } else if (expr[parsed] == '@') {
2686
2687 /* '@' */
2688 tok_len = 1;
2689 tok_type = LYXP_TOKEN_AT;
2690
2691 } else if (expr[parsed] == ',') {
2692
2693 /* ',' */
2694 tok_len = 1;
2695 tok_type = LYXP_TOKEN_COMMA;
2696
2697 } else if (expr[parsed] == '\'') {
2698
2699 /* Literal with ' */
2700 for (tok_len = 1; (expr[parsed + tok_len] != '\0') && (expr[parsed + tok_len] != '\''); ++tok_len);
2701 LY_CHECK_ERR_GOTO(expr[parsed + tok_len] == '\0',
2702 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOE, expr[parsed], &expr[parsed]), error);
2703 ++tok_len;
2704 tok_type = LYXP_TOKEN_LITERAL;
2705
2706 } else if (expr[parsed] == '\"') {
2707
2708 /* Literal with " */
2709 for (tok_len = 1; (expr[parsed + tok_len] != '\0') && (expr[parsed + tok_len] != '\"'); ++tok_len);
2710 LY_CHECK_ERR_GOTO(expr[parsed + tok_len] == '\0',
2711 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOE, expr[parsed], &expr[parsed]), error);
2712 ++tok_len;
2713 tok_type = LYXP_TOKEN_LITERAL;
2714
2715 } else if ((expr[parsed] == '.') || (isdigit(expr[parsed]))) {
2716
2717 /* Number */
2718 for (tok_len = 0; isdigit(expr[parsed + tok_len]); ++tok_len);
2719 if (expr[parsed + tok_len] == '.') {
2720 ++tok_len;
2721 for (; isdigit(expr[parsed + tok_len]); ++tok_len);
2722 }
2723 tok_type = LYXP_TOKEN_NUMBER;
2724
2725 } else if (expr[parsed] == '/') {
2726
2727 /* Operator '/', '//' */
2728 if (!strncmp(&expr[parsed], "//", 2)) {
2729 tok_len = 2;
Michal Vasko14676352020-05-29 11:35:55 +02002730 tok_type = LYXP_TOKEN_OPERATOR_RPATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002731 } else {
2732 tok_len = 1;
Michal Vasko14676352020-05-29 11:35:55 +02002733 tok_type = LYXP_TOKEN_OPERATOR_PATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002734 }
Radek Krejcib1646a92018-11-02 16:08:26 +01002735
2736 } else if (!strncmp(&expr[parsed], "!=", 2) || !strncmp(&expr[parsed], "<=", 2)
2737 || !strncmp(&expr[parsed], ">=", 2)) {
2738
2739 /* Operator '!=', '<=', '>=' */
2740 tok_len = 2;
2741 tok_type = LYXP_TOKEN_OPERATOR_COMP;
2742
2743 } else if (expr[parsed] == '|') {
2744
2745 /* Operator '|' */
2746 tok_len = 1;
2747 tok_type = LYXP_TOKEN_OPERATOR_UNI;
2748
2749 } else if ((expr[parsed] == '+') || (expr[parsed] == '-')) {
2750
2751 /* Operator '+', '-' */
2752 tok_len = 1;
2753 tok_type = LYXP_TOKEN_OPERATOR_MATH;
2754
2755 } else if ((expr[parsed] == '=') || (expr[parsed] == '<') || (expr[parsed] == '>')) {
2756
2757 /* Operator '=', '<', '>' */
2758 tok_len = 1;
2759 tok_type = LYXP_TOKEN_OPERATOR_COMP;
2760
2761 } else if (ret->used && (ret->tokens[ret->used - 1] != LYXP_TOKEN_AT)
2762 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_PAR1)
2763 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_BRACK1)
2764 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_COMMA)
2765 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPERATOR_LOG)
2766 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPERATOR_COMP)
2767 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPERATOR_MATH)
2768 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPERATOR_UNI)
Michal Vasko14676352020-05-29 11:35:55 +02002769 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPERATOR_PATH)
2770 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPERATOR_RPATH)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002771
2772 /* Operator '*', 'or', 'and', 'mod', or 'div' */
2773 if (expr[parsed] == '*') {
2774 tok_len = 1;
2775 tok_type = LYXP_TOKEN_OPERATOR_MATH;
2776
2777 } else if (!strncmp(&expr[parsed], "or", 2)) {
2778 tok_len = 2;
2779 tok_type = LYXP_TOKEN_OPERATOR_LOG;
2780
2781 } else if (!strncmp(&expr[parsed], "and", 3)) {
2782 tok_len = 3;
2783 tok_type = LYXP_TOKEN_OPERATOR_LOG;
2784
2785 } else if (!strncmp(&expr[parsed], "mod", 3) || !strncmp(&expr[parsed], "div", 3)) {
2786 tok_len = 3;
2787 tok_type = LYXP_TOKEN_OPERATOR_MATH;
2788
2789 } else if (prev_function_check) {
Michal Vasko53078572019-05-24 08:50:15 +02002790 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "Invalid character 0x%x ('%c'), perhaps \"%.*s\" is supposed to be a function call.",
2791 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 +01002792 goto error;
2793 } else {
Radek Krejcid4270262019-01-07 15:07:25 +01002794 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INEXPR, parsed + 1, expr);
Radek Krejcib1646a92018-11-02 16:08:26 +01002795 goto error;
2796 }
2797 } else if (expr[parsed] == '*') {
2798
2799 /* NameTest '*' */
2800 tok_len = 1;
2801 tok_type = LYXP_TOKEN_NAMETEST;
2802
2803 } else {
2804
2805 /* NameTest (NCName ':' '*' | QName) or NodeType/FunctionName */
2806 ncname_len = parse_ncname(&expr[parsed]);
Radek Krejcid4270262019-01-07 15:07:25 +01002807 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 +01002808 tok_len = ncname_len;
2809
2810 if (expr[parsed + tok_len] == ':') {
2811 ++tok_len;
2812 if (expr[parsed + tok_len] == '*') {
2813 ++tok_len;
2814 } else {
2815 ncname_len = parse_ncname(&expr[parsed + tok_len]);
Radek Krejcid4270262019-01-07 15:07:25 +01002816 LY_CHECK_ERR_GOTO(ncname_len < 0, LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INEXPR, parsed - ncname_len + 1, expr), error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002817 tok_len += ncname_len;
2818 }
2819 /* remove old flag to prevent ambiguities */
2820 prev_function_check = 0;
2821 tok_type = LYXP_TOKEN_NAMETEST;
2822 } else {
2823 /* there is no prefix so it can still be NodeType/FunctionName, we can't finally decide now */
2824 prev_function_check = 1;
2825 tok_type = LYXP_TOKEN_NAMETEST;
2826 }
2827 }
2828
2829 /* store the token, move on to the next one */
2830 LY_CHECK_GOTO(exp_add_token(ctx, ret, tok_type, parsed, tok_len), error);
2831 parsed += tok_len;
2832 while (is_xmlws(expr[parsed])) {
2833 ++parsed;
2834 }
2835
2836 } while (expr[parsed]);
2837
2838 /* prealloc repeat */
2839 ret->repeat = calloc(ret->size, sizeof *ret->repeat);
2840 LY_CHECK_ERR_GOTO(!ret->repeat, LOGMEM(ctx), error);
2841
Michal Vasko03ff5a72019-09-11 13:49:33 +02002842 /* fill repeat */
2843 LY_CHECK_GOTO(reparse_or_expr(ctx, ret, &exp_idx), error);
2844 if (ret->used > exp_idx) {
2845 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK, "Unknown", &ret->expr[ret->tok_pos[exp_idx]]);
2846 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "Unparsed characters \"%s\" left at the end of an XPath expression.",
2847 &ret->expr[ret->tok_pos[exp_idx]]);
2848 goto error;
2849 }
2850
2851 print_expr_struct_debug(ret);
2852
Radek Krejcib1646a92018-11-02 16:08:26 +01002853 return ret;
2854
2855error:
2856 lyxp_expr_free(ctx, ret);
2857 return NULL;
2858}
2859
Michal Vasko03ff5a72019-09-11 13:49:33 +02002860/*
2861 * warn functions
2862 *
2863 * Warn functions check specific reasonable conditions for schema XPath
2864 * and print a warning if they are not satisfied.
2865 */
2866
2867/**
2868 * @brief Get the last-added schema node that is currently in the context.
2869 *
2870 * @param[in] set Set to search in.
2871 * @return Last-added schema context node, NULL if no node is in context.
2872 */
2873static struct lysc_node *
2874warn_get_scnode_in_ctx(struct lyxp_set *set)
2875{
2876 uint32_t i;
2877
2878 if (!set || (set->type != LYXP_SET_SCNODE_SET)) {
2879 return NULL;
2880 }
2881
2882 i = set->used;
2883 do {
2884 --i;
2885 if (set->val.scnodes[i].in_ctx == 1) {
2886 /* if there are more, simply return the first found (last added) */
2887 return set->val.scnodes[i].scnode;
2888 }
2889 } while (i);
2890
2891 return NULL;
2892}
2893
2894/**
2895 * @brief Test whether a type is numeric - integer type or decimal64.
2896 *
2897 * @param[in] type Type to test.
2898 * @return 1 if numeric, 0 otherwise.
2899 */
2900static int
2901warn_is_numeric_type(struct lysc_type *type)
2902{
2903 struct lysc_type_union *uni;
2904 int ret;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002905 LY_ARRAY_SIZE_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002906
2907 switch (type->basetype) {
2908 case LY_TYPE_DEC64:
2909 case LY_TYPE_INT8:
2910 case LY_TYPE_UINT8:
2911 case LY_TYPE_INT16:
2912 case LY_TYPE_UINT16:
2913 case LY_TYPE_INT32:
2914 case LY_TYPE_UINT32:
2915 case LY_TYPE_INT64:
2916 case LY_TYPE_UINT64:
2917 return 1;
2918 case LY_TYPE_UNION:
2919 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002920 LY_ARRAY_FOR(uni->types, u) {
2921 ret = warn_is_numeric_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002922 if (ret) {
2923 /* found a suitable type */
2924 return 1;
2925 }
2926 }
2927 /* did not find any suitable type */
2928 return 0;
2929 case LY_TYPE_LEAFREF:
2930 return warn_is_numeric_type(((struct lysc_type_leafref *)type)->realtype);
2931 default:
2932 return 0;
2933 }
2934}
2935
2936/**
2937 * @brief Test whether a type is string-like - no integers, decimal64 or binary.
2938 *
2939 * @param[in] type Type to test.
2940 * @return 1 if string, 0 otherwise.
2941 */
2942static int
2943warn_is_string_type(struct lysc_type *type)
2944{
2945 struct lysc_type_union *uni;
2946 int ret;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002947 LY_ARRAY_SIZE_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002948
2949 switch (type->basetype) {
2950 case LY_TYPE_BITS:
2951 case LY_TYPE_ENUM:
2952 case LY_TYPE_IDENT:
2953 case LY_TYPE_INST:
2954 case LY_TYPE_STRING:
2955 return 1;
2956 case LY_TYPE_UNION:
2957 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002958 LY_ARRAY_FOR(uni->types, u) {
2959 ret = warn_is_string_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002960 if (ret) {
2961 /* found a suitable type */
2962 return 1;
2963 }
2964 }
2965 /* did not find any suitable type */
2966 return 0;
2967 case LY_TYPE_LEAFREF:
2968 return warn_is_string_type(((struct lysc_type_leafref *)type)->realtype);
2969 default:
2970 return 0;
2971 }
2972}
2973
2974/**
2975 * @brief Test whether a type is one specific type.
2976 *
2977 * @param[in] type Type to test.
2978 * @param[in] base Expected type.
2979 * @return 1 if it is, 0 otherwise.
2980 */
2981static int
2982warn_is_specific_type(struct lysc_type *type, LY_DATA_TYPE base)
2983{
2984 struct lysc_type_union *uni;
2985 int ret;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002986 LY_ARRAY_SIZE_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002987
2988 if (type->basetype == base) {
2989 return 1;
2990 } else if (type->basetype == LY_TYPE_UNION) {
2991 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002992 LY_ARRAY_FOR(uni->types, u) {
2993 ret = warn_is_specific_type(uni->types[u], base);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002994 if (ret) {
2995 /* found a suitable type */
2996 return 1;
2997 }
2998 }
2999 /* did not find any suitable type */
3000 return 0;
3001 } else if (type->basetype == LY_TYPE_LEAFREF) {
3002 return warn_is_specific_type(((struct lysc_type_leafref *)type)->realtype, base);
3003 }
3004
3005 return 0;
3006}
3007
3008/**
3009 * @brief Get next type of a (union) type.
3010 *
3011 * @param[in] type Base type.
3012 * @param[in] prev_type Previously returned type.
3013 * @return Next type or NULL.
3014 */
3015static struct lysc_type *
3016warn_is_equal_type_next_type(struct lysc_type *type, struct lysc_type *prev_type)
3017{
3018 struct lysc_type_union *uni;
3019 int found = 0;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003020 LY_ARRAY_SIZE_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003021
3022 switch (type->basetype) {
3023 case LY_TYPE_UNION:
3024 uni = (struct lysc_type_union *)type;
3025 if (!prev_type) {
3026 return uni->types[0];
3027 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003028 LY_ARRAY_FOR(uni->types, u) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003029 if (found) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003030 return uni->types[u];
Michal Vasko03ff5a72019-09-11 13:49:33 +02003031 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003032 if (prev_type == uni->types[u]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003033 found = 1;
3034 }
3035 }
3036 return NULL;
3037 default:
3038 if (prev_type) {
3039 assert(type == prev_type);
3040 return NULL;
3041 } else {
3042 return type;
3043 }
3044 }
3045}
3046
3047/**
3048 * @brief Test whether 2 types have a common type.
3049 *
3050 * @param[in] type1 First type.
3051 * @param[in] type2 Second type.
3052 * @return 1 if they do, 0 otherwise.
3053 */
3054static int
3055warn_is_equal_type(struct lysc_type *type1, struct lysc_type *type2)
3056{
3057 struct lysc_type *t1, *rt1, *t2, *rt2;
3058
3059 t1 = NULL;
3060 while ((t1 = warn_is_equal_type_next_type(type1, t1))) {
3061 if (t1->basetype == LY_TYPE_LEAFREF) {
3062 rt1 = ((struct lysc_type_leafref *)t1)->realtype;
3063 } else {
3064 rt1 = t1;
3065 }
3066
3067 t2 = NULL;
3068 while ((t2 = warn_is_equal_type_next_type(type2, t2))) {
3069 if (t2->basetype == LY_TYPE_LEAFREF) {
3070 rt2 = ((struct lysc_type_leafref *)t2)->realtype;
3071 } else {
3072 rt2 = t2;
3073 }
3074
3075 if (rt2->basetype == rt1->basetype) {
3076 /* match found */
3077 return 1;
3078 }
3079 }
3080 }
3081
3082 return 0;
3083}
3084
3085/**
3086 * @brief Check both operands of comparison operators.
3087 *
3088 * @param[in] ctx Context for errors.
3089 * @param[in] set1 First operand set.
3090 * @param[in] set2 Second operand set.
3091 * @param[in] numbers_only Whether accept only numbers or other types are fine too (for '=' and '!=').
3092 * @param[in] expr Start of the expression to print with the warning.
3093 * @param[in] tok_pos Token position.
3094 */
3095static void
3096warn_operands(struct ly_ctx *ctx, struct lyxp_set *set1, struct lyxp_set *set2, int numbers_only, const char *expr, uint16_t tok_pos)
3097{
3098 struct lysc_node_leaf *node1, *node2;
3099 int leaves = 1, warning = 0;
3100
3101 node1 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set1);
3102 node2 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set2);
3103
3104 if (!node1 && !node2) {
3105 /* no node-sets involved, nothing to do */
3106 return;
3107 }
3108
3109 if (node1) {
3110 if (!(node1->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3111 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node1->nodetype), node1->name);
3112 warning = 1;
3113 leaves = 0;
3114 } else if (numbers_only && !warn_is_numeric_type(node1->type)) {
3115 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node1->name);
3116 warning = 1;
3117 }
3118 }
3119
3120 if (node2) {
3121 if (!(node2->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3122 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node2->nodetype), node2->name);
3123 warning = 1;
3124 leaves = 0;
3125 } else if (numbers_only && !warn_is_numeric_type(node2->type)) {
3126 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node2->name);
3127 warning = 1;
3128 }
3129 }
3130
3131 if (node1 && node2 && leaves && !numbers_only) {
3132 if ((warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type))
3133 || (!warn_is_numeric_type(node1->type) && warn_is_numeric_type(node2->type))
3134 || (!warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type)
3135 && !warn_is_equal_type(node1->type, node2->type))) {
3136 LOGWRN(ctx, "Incompatible types of operands \"%s\" and \"%s\" for comparison.", node1->name, node2->name);
3137 warning = 1;
3138 }
3139 }
3140
3141 if (warning) {
3142 LOGWRN(ctx, "Previous warning generated by XPath subexpression[%u] \"%.20s\".", tok_pos, expr + tok_pos);
3143 }
3144}
3145
3146/**
3147 * @brief Check that a value is valid for a leaf. If not applicable, does nothing.
3148 *
3149 * @param[in] exp Parsed XPath expression.
3150 * @param[in] set Set with the leaf/leaf-list.
3151 * @param[in] val_exp Index of the value (literal/number) in @p exp.
3152 * @param[in] equal_exp Index of the start of the equality expression in @p exp.
3153 * @param[in] last_equal_exp Index of the end of the equality expression in @p exp.
3154 */
3155static void
3156warn_equality_value(struct lyxp_expr *exp, struct lyxp_set *set, uint16_t val_exp, uint16_t equal_exp, uint16_t last_equal_exp)
3157{
3158 struct lysc_node *scnode;
3159 struct lysc_type *type;
3160 char *value;
3161 LY_ERR rc;
3162 struct ly_err_item *err = NULL;
3163
3164 if ((scnode = warn_get_scnode_in_ctx(set)) && (scnode->nodetype & (LYS_LEAF | LYS_LEAFLIST))
3165 && ((exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) || (exp->tokens[val_exp] == LYXP_TOKEN_NUMBER))) {
3166 /* check that the node can have the specified value */
3167 if (exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) {
3168 value = strndup(exp->expr + exp->tok_pos[val_exp] + 1, exp->tok_len[val_exp] - 2);
3169 } else {
3170 value = strndup(exp->expr + exp->tok_pos[val_exp], exp->tok_len[val_exp]);
3171 }
3172 if (!value) {
3173 LOGMEM(set->ctx);
3174 return;
3175 }
3176
3177 if ((((struct lysc_node_leaf *)scnode)->type->basetype == LY_TYPE_IDENT) && !strchr(value, ':')) {
3178 LOGWRN(set->ctx, "Identityref \"%s\" comparison with identity \"%s\" without prefix, consider adding"
3179 " a prefix or best using \"derived-from(-or-self)()\" functions.", scnode->name, value);
3180 LOGWRN(set->ctx, "Previous warning generated by XPath subexpression[%u] \"%.*s\".", exp->tok_pos[equal_exp],
3181 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
3182 exp->expr + exp->tok_pos[equal_exp]);
3183 }
3184
3185 type = ((struct lysc_node_leaf *)scnode)->type;
3186 if (type->basetype != LY_TYPE_IDENT) {
3187 rc = type->plugin->store(set->ctx, type, value, strlen(value), LY_TYPE_OPTS_SCHEMA,
3188 lys_resolve_prefix, (void *)type->dflt_mod, LYD_XML, NULL, NULL, NULL, NULL, &err);
3189
3190 if (err) {
3191 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type (%s).", value, err->msg);
3192 ly_err_free(err);
3193 } else if (rc != LY_SUCCESS) {
3194 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type.", value);
3195 }
3196 if (rc != LY_SUCCESS) {
3197 LOGWRN(set->ctx, "Previous warning generated by XPath subexpression[%u] \"%.*s\".", exp->tok_pos[equal_exp],
3198 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
3199 exp->expr + exp->tok_pos[equal_exp]);
3200 }
3201 }
3202 free(value);
3203 }
3204}
3205
3206/*
3207 * XPath functions
3208 */
3209
3210/**
3211 * @brief Execute the YANG 1.1 bit-is-set(node-set, string) function. Returns LYXP_SET_BOOLEAN
3212 * depending on whether the first node bit value from the second argument is set.
3213 *
3214 * @param[in] args Array of arguments.
3215 * @param[in] arg_count Count of elements in @p args.
3216 * @param[in,out] set Context and result set at the same time.
3217 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003218 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003219 */
3220static LY_ERR
3221xpath_bit_is_set(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3222{
3223 struct lyd_node_term *leaf;
3224 struct lysc_node_leaf *sleaf;
3225 struct lysc_type_bits *bits;
3226 LY_ERR rc = LY_SUCCESS;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003227 LY_ARRAY_SIZE_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003228
3229 if (options & LYXP_SCNODE_ALL) {
3230 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3231 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003232 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3233 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 +02003234 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_BITS)) {
3235 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"bits\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003236 }
3237
3238 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3239 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3240 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 +02003241 } else if (!warn_is_string_type(sleaf->type)) {
3242 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003243 }
3244 }
3245 set_scnode_clear_ctx(set);
3246 return rc;
3247 }
3248
Michal Vaskod3678892020-05-21 10:06:58 +02003249 if (args[0]->type != LYXP_SET_NODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003250 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)");
3251 return LY_EVALID;
3252 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003253 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003254 LY_CHECK_RET(rc);
3255
3256 set_fill_boolean(set, 0);
Michal Vaskod3678892020-05-21 10:06:58 +02003257 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003258 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
3259 if ((leaf->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST))
3260 && (((struct lysc_node_leaf *)leaf->schema)->type->basetype == LY_TYPE_BITS)) {
3261 bits = (struct lysc_type_bits *)((struct lysc_node_leaf *)leaf->schema)->type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003262 LY_ARRAY_FOR(bits->bits, u) {
3263 if (!strcmp(bits->bits[u].name, args[1]->val.str)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003264 set_fill_boolean(set, 1);
3265 break;
3266 }
3267 }
3268 }
3269 }
3270
3271 return LY_SUCCESS;
3272}
3273
3274/**
3275 * @brief Execute the XPath boolean(object) function. Returns LYXP_SET_BOOLEAN
3276 * with the argument converted to boolean.
3277 *
3278 * @param[in] args Array of arguments.
3279 * @param[in] arg_count Count of elements in @p args.
3280 * @param[in,out] set Context and result set at the same time.
3281 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003282 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003283 */
3284static LY_ERR
3285xpath_boolean(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3286{
3287 LY_ERR rc;
3288
3289 if (options & LYXP_SCNODE_ALL) {
3290 set_scnode_clear_ctx(set);
3291 return LY_SUCCESS;
3292 }
3293
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003294 rc = lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003295 LY_CHECK_RET(rc);
3296 set_fill_set(set, args[0]);
3297
3298 return LY_SUCCESS;
3299}
3300
3301/**
3302 * @brief Execute the XPath ceiling(number) function. Returns LYXP_SET_NUMBER
3303 * with the first argument rounded up to the nearest integer.
3304 *
3305 * @param[in] args Array of arguments.
3306 * @param[in] arg_count Count of elements in @p args.
3307 * @param[in,out] set Context and result set at the same time.
3308 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003309 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003310 */
3311static LY_ERR
3312xpath_ceiling(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3313{
3314 struct lysc_node_leaf *sleaf;
3315 LY_ERR rc = LY_SUCCESS;
3316
3317 if (options & LYXP_SCNODE_ALL) {
3318 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3319 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003320 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3321 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 +02003322 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
3323 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003324 }
3325 set_scnode_clear_ctx(set);
3326 return rc;
3327 }
3328
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003329 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003330 LY_CHECK_RET(rc);
3331 if ((long long)args[0]->val.num != args[0]->val.num) {
3332 set_fill_number(set, ((long long)args[0]->val.num) + 1);
3333 } else {
3334 set_fill_number(set, args[0]->val.num);
3335 }
3336
3337 return LY_SUCCESS;
3338}
3339
3340/**
3341 * @brief Execute the XPath concat(string, string, string*) function.
3342 * Returns LYXP_SET_STRING with the concatenation of all the arguments.
3343 *
3344 * @param[in] args Array of arguments.
3345 * @param[in] arg_count Count of elements in @p args.
3346 * @param[in,out] set Context and result set at the same time.
3347 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003348 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003349 */
3350static LY_ERR
3351xpath_concat(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
3352{
3353 uint16_t i;
3354 char *str = NULL;
3355 size_t used = 1;
3356 LY_ERR rc = LY_SUCCESS;
3357 struct lysc_node_leaf *sleaf;
3358
3359 if (options & LYXP_SCNODE_ALL) {
3360 for (i = 0; i < arg_count; ++i) {
3361 if ((args[i]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[i]))) {
3362 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3363 LOGWRN(set->ctx, "Argument #%u of %s is a %s node \"%s\".",
3364 i + 1, __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003365 } else if (!warn_is_string_type(sleaf->type)) {
3366 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 +02003367 }
3368 }
3369 }
3370 set_scnode_clear_ctx(set);
3371 return rc;
3372 }
3373
3374 for (i = 0; i < arg_count; ++i) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003375 rc = lyxp_set_cast(args[i], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003376 if (rc != LY_SUCCESS) {
3377 free(str);
3378 return rc;
3379 }
3380
3381 str = ly_realloc(str, (used + strlen(args[i]->val.str)) * sizeof(char));
3382 LY_CHECK_ERR_RET(!str, LOGMEM(set->ctx), LY_EMEM);
3383 strcpy(str + used - 1, args[i]->val.str);
3384 used += strlen(args[i]->val.str);
3385 }
3386
3387 /* free, kind of */
Michal Vaskod3678892020-05-21 10:06:58 +02003388 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003389 set->type = LYXP_SET_STRING;
3390 set->val.str = str;
3391
3392 return LY_SUCCESS;
3393}
3394
3395/**
3396 * @brief Execute the XPath contains(string, string) function.
3397 * Returns LYXP_SET_BOOLEAN whether the second argument can
3398 * be found in the first or not.
3399 *
3400 * @param[in] args Array of arguments.
3401 * @param[in] arg_count Count of elements in @p args.
3402 * @param[in,out] set Context and result set at the same time.
3403 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003404 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003405 */
3406static LY_ERR
3407xpath_contains(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3408{
3409 struct lysc_node_leaf *sleaf;
3410 LY_ERR rc = LY_SUCCESS;
3411
3412 if (options & LYXP_SCNODE_ALL) {
3413 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3414 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3415 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 +02003416 } else if (!warn_is_string_type(sleaf->type)) {
3417 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003418 }
3419 }
3420
3421 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3422 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3423 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 +02003424 } else if (!warn_is_string_type(sleaf->type)) {
3425 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003426 }
3427 }
3428 set_scnode_clear_ctx(set);
3429 return rc;
3430 }
3431
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003432 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003433 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003434 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003435 LY_CHECK_RET(rc);
3436
3437 if (strstr(args[0]->val.str, args[1]->val.str)) {
3438 set_fill_boolean(set, 1);
3439 } else {
3440 set_fill_boolean(set, 0);
3441 }
3442
3443 return LY_SUCCESS;
3444}
3445
3446/**
3447 * @brief Execute the XPath count(node-set) function. Returns LYXP_SET_NUMBER
3448 * with the size of the node-set from the argument.
3449 *
3450 * @param[in] args Array of arguments.
3451 * @param[in] arg_count Count of elements in @p args.
3452 * @param[in,out] set Context and result set at the same time.
3453 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003454 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003455 */
3456static LY_ERR
3457xpath_count(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3458{
3459 struct lysc_node *scnode = NULL;
3460 LY_ERR rc = LY_SUCCESS;
3461
3462 if (options & LYXP_SCNODE_ALL) {
3463 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(scnode = warn_get_scnode_in_ctx(args[0]))) {
3464 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003465 }
3466 set_scnode_clear_ctx(set);
3467 return rc;
3468 }
3469
Michal Vasko03ff5a72019-09-11 13:49:33 +02003470 if (args[0]->type != LYXP_SET_NODE_SET) {
3471 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "count(node-set)");
3472 return LY_EVALID;
3473 }
3474
3475 set_fill_number(set, args[0]->used);
3476 return LY_SUCCESS;
3477}
3478
3479/**
3480 * @brief Execute the XPath current() function. Returns LYXP_SET_NODE_SET
3481 * with the context with the intial node.
3482 *
3483 * @param[in] args Array of arguments.
3484 * @param[in] arg_count Count of elements in @p args.
3485 * @param[in,out] set Context and result set at the same time.
3486 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003487 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003488 */
3489static LY_ERR
3490xpath_current(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
3491{
3492 if (arg_count || args) {
3493 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGCOUNT, arg_count, "current()");
3494 return LY_EVALID;
3495 }
3496
3497 if (options & LYXP_SCNODE_ALL) {
3498 set_scnode_clear_ctx(set);
3499
Michal Vaskoecd62de2019-11-13 12:35:11 +01003500 lyxp_set_scnode_insert_node(set, set->ctx_scnode, LYXP_NODE_ELEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003501 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02003502 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003503
3504 /* position is filled later */
3505 set_insert_node(set, set->ctx_node, 0, LYXP_NODE_ELEM, 0);
3506 }
3507
3508 return LY_SUCCESS;
3509}
3510
3511/**
3512 * @brief Execute the YANG 1.1 deref(node-set) function. Returns LYXP_SET_NODE_SET with either
3513 * leafref or instance-identifier target node(s).
3514 *
3515 * @param[in] args Array of arguments.
3516 * @param[in] arg_count Count of elements in @p args.
3517 * @param[in,out] set Context and result set at the same time.
3518 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003519 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003520 */
3521static LY_ERR
3522xpath_deref(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3523{
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003524 struct lysc_ctx cctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003525 struct lyd_node_term *leaf;
Michal Vasko42e497c2020-01-06 08:38:25 +01003526 struct lysc_node_leaf *sleaf = NULL;
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003527 const struct lysc_node *target;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003528 const struct lyd_node *node;
3529 char *errmsg = NULL;
3530 const char *val;
3531 int dynamic;
3532 LY_ERR rc = LY_SUCCESS;
3533
3534 if (options & LYXP_SCNODE_ALL) {
3535 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3536 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003537 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3538 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 +02003539 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_LEAFREF) && !warn_is_specific_type(sleaf->type, LY_TYPE_INST)) {
3540 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"leafref\" nor \"instance-identifier\".",
3541 __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003542 }
3543 set_scnode_clear_ctx(set);
Michal Vasko42e497c2020-01-06 08:38:25 +01003544 if (sleaf && (sleaf->type->basetype == LY_TYPE_LEAFREF)) {
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003545 cctx.ctx = set->ctx;
3546 rc = lys_compile_leafref_validate(&cctx, (struct lysc_node *)sleaf, (struct lysc_type_leafref *)sleaf->type, &target);
3547 /* it was already validated, it must succeed */
3548 if (rc == LY_SUCCESS) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01003549 lyxp_set_scnode_insert_node(set, target, LYXP_NODE_ELEM);
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003550 }
3551 }
3552
Michal Vasko03ff5a72019-09-11 13:49:33 +02003553 return rc;
3554 }
3555
Michal Vaskod3678892020-05-21 10:06:58 +02003556 if (args[0]->type != LYXP_SET_NODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003557 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "deref(node-set)");
3558 return LY_EVALID;
3559 }
3560
Michal Vaskod3678892020-05-21 10:06:58 +02003561 lyxp_set_free_content(set);
3562 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003563 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
3564 sleaf = (struct lysc_node_leaf *)leaf->schema;
3565 if (sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
3566 if (sleaf->type->basetype == LY_TYPE_LEAFREF) {
3567 /* find leafref target */
3568 val = lyd_value2str(leaf, &dynamic);
3569 node = ly_type_find_leafref(set->ctx, sleaf->type, val, strlen(val), (struct lyd_node *)leaf,
Michal Vaskof03ed032020-03-04 13:31:44 +01003570 set->tree, &leaf->value, &errmsg);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003571 if (dynamic) {
3572 free((char *)val);
3573 }
3574 if (!node) {
3575 LOGERR(set->ctx, LY_EINVAL, errmsg);
3576 free(errmsg);
3577 return LY_EINVAL;
3578 }
3579
3580 /* insert it */
3581 set_insert_node(set, node, 0, LYXP_NODE_ELEM, 0);
3582 } else {
3583 assert(sleaf->type->basetype == LY_TYPE_INST);
Michal Vaskof03ed032020-03-04 13:31:44 +01003584 node = (struct lyd_node *)lyd_target(leaf->value.target, set->tree);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003585 if (!node) {
3586 val = lyd_value2str(leaf, &dynamic);
3587 LOGERR(set->ctx, LY_EVALID, "Invalid instance-identifier \"%s\" value - required instance not found.", val);
3588 if (dynamic) {
3589 free((char *)val);
3590 }
3591 return LY_EVALID;
3592 }
3593 }
3594 }
3595 }
3596
3597 return LY_SUCCESS;
3598}
3599
3600/**
3601 * @brief Execute the YANG 1.1 derived-from(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
3602 * on whether the first argument nodes contain a node of an identity derived from the second
3603 * argument identity.
3604 *
3605 * @param[in] args Array of arguments.
3606 * @param[in] arg_count Count of elements in @p args.
3607 * @param[in,out] set Context and result set at the same time.
3608 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003609 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003610 */
3611static LY_ERR
3612xpath_derived_from(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3613{
3614 uint16_t i;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003615 LY_ARRAY_SIZE_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003616 struct lyd_node_term *leaf;
3617 struct lysc_node_leaf *sleaf;
3618 struct lyd_value data = {0};
3619 struct ly_err_item *err = NULL;
3620 LY_ERR rc = LY_SUCCESS;
3621 int found;
3622
3623 if (options & LYXP_SCNODE_ALL) {
3624 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3625 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003626 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3627 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 +02003628 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_IDENT)) {
3629 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"identityref\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003630 }
3631
3632 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3633 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3634 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 +02003635 } else if (!warn_is_string_type(sleaf->type)) {
3636 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003637 }
3638 }
3639 set_scnode_clear_ctx(set);
3640 return rc;
3641 }
3642
Michal Vaskod3678892020-05-21 10:06:58 +02003643 if (args[0]->type != LYXP_SET_NODE_SET) {
3644 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
3645 "derived-from(node-set, string)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003646 return LY_EVALID;
3647 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003648 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003649 LY_CHECK_RET(rc);
3650
3651 set_fill_boolean(set, 0);
Michal Vaskod3678892020-05-21 10:06:58 +02003652 found = 0;
3653 for (i = 0; i < args[0]->used; ++i) {
3654 leaf = (struct lyd_node_term *)args[0]->val.nodes[i].node;
3655 sleaf = (struct lysc_node_leaf *)leaf->schema;
3656 if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_IDENT)) {
3657 /* store args[1] into ident */
3658 rc = sleaf->type->plugin->store(set->ctx, sleaf->type, args[1]->val.str, strlen(args[1]->val.str),
3659 LY_TYPE_OPTS_STORE, lys_resolve_prefix, (void *)sleaf->dflt_mod, set->format,
3660 (struct lyd_node *)leaf, set->tree, &data, NULL, &err);
3661 if (err) {
3662 ly_err_print(err);
3663 ly_err_free(err);
3664 }
3665 LY_CHECK_RET(rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003666
Michal Vaskod3678892020-05-21 10:06:58 +02003667 LY_ARRAY_FOR(data.ident->derived, u) {
3668 if (data.ident->derived[u] == leaf->value.ident) {
3669 set_fill_boolean(set, 1);
3670 found = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003671 break;
3672 }
3673 }
Michal Vaskod3678892020-05-21 10:06:58 +02003674 if (found) {
3675 break;
3676 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003677 }
3678 }
3679
3680 return LY_SUCCESS;
3681}
3682
3683/**
3684 * @brief Execute the YANG 1.1 derived-from-or-self(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
3685 * on whether the first argument nodes contain a node of an identity that either is or is derived from
3686 * the second argument identity.
3687 *
3688 * @param[in] args Array of arguments.
3689 * @param[in] arg_count Count of elements in @p args.
3690 * @param[in,out] set Context and result set at the same time.
3691 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003692 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003693 */
3694static LY_ERR
3695xpath_derived_from_or_self(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3696{
3697 uint16_t i;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003698 LY_ARRAY_SIZE_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003699 struct lyd_node_term *leaf;
3700 struct lysc_node_leaf *sleaf;
3701 struct lyd_value data = {0};
3702 struct ly_err_item *err = NULL;
3703 LY_ERR rc = LY_SUCCESS;
3704 int found;
3705
3706 if (options & LYXP_SCNODE_ALL) {
3707 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3708 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003709 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3710 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 +02003711 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_IDENT)) {
3712 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"identityref\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003713 }
3714
3715 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3716 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3717 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 +02003718 } else if (!warn_is_string_type(sleaf->type)) {
3719 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003720 }
3721 }
3722 set_scnode_clear_ctx(set);
3723 return rc;
3724 }
3725
Michal Vaskod3678892020-05-21 10:06:58 +02003726 if (args[0]->type != LYXP_SET_NODE_SET) {
3727 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
3728 "derived-from-or-self(node-set, string)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003729 return LY_EVALID;
3730 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003731 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003732 LY_CHECK_RET(rc);
3733
3734 set_fill_boolean(set, 0);
Michal Vaskod3678892020-05-21 10:06:58 +02003735 found = 0;
3736 for (i = 0; i < args[0]->used; ++i) {
3737 leaf = (struct lyd_node_term *)args[0]->val.nodes[i].node;
3738 sleaf = (struct lysc_node_leaf *)leaf->schema;
3739 if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_IDENT)) {
3740 /* store args[1] into ident */
3741 rc = sleaf->type->plugin->store(set->ctx, sleaf->type, args[1]->val.str, strlen(args[1]->val.str),
3742 LY_TYPE_OPTS_STORE, lys_resolve_prefix, (void *)sleaf->dflt_mod, set->format,
3743 (struct lyd_node *)leaf, set->tree, &data, NULL, &err);
3744 if (err) {
3745 ly_err_print(err);
3746 ly_err_free(err);
3747 }
3748 LY_CHECK_RET(rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003749
Michal Vaskod3678892020-05-21 10:06:58 +02003750 if (data.ident == leaf->value.ident) {
3751 set_fill_boolean(set, 1);
3752 break;
3753 }
3754 LY_ARRAY_FOR(data.ident->derived, u) {
3755 if (data.ident->derived[u] == leaf->value.ident) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003756 set_fill_boolean(set, 1);
Michal Vaskod3678892020-05-21 10:06:58 +02003757 found = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003758 break;
3759 }
Michal Vaskod3678892020-05-21 10:06:58 +02003760 }
3761 if (found) {
3762 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003763 }
3764 }
3765 }
3766
3767 return LY_SUCCESS;
3768}
3769
3770/**
3771 * @brief Execute the YANG 1.1 enum-value(node-set) function. Returns LYXP_SET_NUMBER
3772 * with the integer value of the first node's enum value, otherwise NaN.
3773 *
3774 * @param[in] args Array of arguments.
3775 * @param[in] arg_count Count of elements in @p args.
3776 * @param[in,out] set Context and result set at the same time.
3777 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003778 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003779 */
3780static LY_ERR
3781xpath_enum_value(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3782{
3783 struct lyd_node_term *leaf;
3784 struct lysc_node_leaf *sleaf;
3785 LY_ERR rc = LY_SUCCESS;
3786
3787 if (options & LYXP_SCNODE_ALL) {
3788 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3789 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003790 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3791 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 +02003792 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_ENUM)) {
3793 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"enumeration\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003794 }
3795 set_scnode_clear_ctx(set);
3796 return rc;
3797 }
3798
Michal Vaskod3678892020-05-21 10:06:58 +02003799 if (args[0]->type != LYXP_SET_NODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003800 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "enum-value(node-set)");
3801 return LY_EVALID;
3802 }
3803
3804 set_fill_number(set, NAN);
Michal Vaskod3678892020-05-21 10:06:58 +02003805 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003806 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
3807 sleaf = (struct lysc_node_leaf *)leaf->schema;
3808 if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_ENUM)) {
3809 set_fill_number(set, leaf->value.enum_item->value);
3810 }
3811 }
3812
3813 return LY_SUCCESS;
3814}
3815
3816/**
3817 * @brief Execute the XPath false() function. Returns LYXP_SET_BOOLEAN
3818 * with false value.
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
3827xpath_false(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3828{
3829 if (options & LYXP_SCNODE_ALL) {
3830 set_scnode_clear_ctx(set);
3831 return LY_SUCCESS;
3832 }
3833
3834 set_fill_boolean(set, 0);
3835 return LY_SUCCESS;
3836}
3837
3838/**
3839 * @brief Execute the XPath floor(number) function. Returns LYXP_SET_NUMBER
3840 * with the first argument floored (truncated).
3841 *
3842 * @param[in] args Array of arguments.
3843 * @param[in] arg_count Count of elements in @p args.
3844 * @param[in,out] set Context and result set at the same time.
3845 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003846 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003847 */
3848static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003849xpath_floor(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int UNUSED(options))
Michal Vasko03ff5a72019-09-11 13:49:33 +02003850{
3851 LY_ERR rc;
3852
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003853 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003854 LY_CHECK_RET(rc);
3855 if (isfinite(args[0]->val.num)) {
3856 set_fill_number(set, (long long)args[0]->val.num);
3857 }
3858
3859 return LY_SUCCESS;
3860}
3861
3862/**
3863 * @brief Execute the XPath lang(string) function. Returns LYXP_SET_BOOLEAN
3864 * whether the language of the text matches the one from the argument.
3865 *
3866 * @param[in] args Array of arguments.
3867 * @param[in] arg_count Count of elements in @p args.
3868 * @param[in,out] set Context and result set at the same time.
3869 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003870 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003871 */
3872static LY_ERR
3873xpath_lang(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3874{
3875 const struct lyd_node *node;
3876 struct lysc_node_leaf *sleaf;
Michal Vasko9f96a052020-03-10 09:41:45 +01003877 struct lyd_meta *meta = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003878 const char *val;
3879 int i, dynamic;
3880 LY_ERR rc = LY_SUCCESS;
3881
3882 if (options & LYXP_SCNODE_ALL) {
3883 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3884 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3885 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 +02003886 } else if (!warn_is_string_type(sleaf->type)) {
3887 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003888 }
3889 }
3890 set_scnode_clear_ctx(set);
3891 return rc;
3892 }
3893
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003894 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003895 LY_CHECK_RET(rc);
3896
Michal Vasko03ff5a72019-09-11 13:49:33 +02003897 if (set->type != LYXP_SET_NODE_SET) {
3898 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "lang(string)");
3899 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02003900 } else if (!set->used) {
3901 set_fill_boolean(set, 0);
3902 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003903 }
3904
3905 switch (set->val.nodes[0].type) {
3906 case LYXP_NODE_ELEM:
3907 case LYXP_NODE_TEXT:
3908 node = set->val.nodes[0].node;
3909 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01003910 case LYXP_NODE_META:
3911 node = set->val.meta[0].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003912 break;
3913 default:
3914 /* nothing to do with roots */
3915 set_fill_boolean(set, 0);
3916 return LY_SUCCESS;
3917 }
3918
Michal Vasko9f96a052020-03-10 09:41:45 +01003919 /* find lang metadata */
Michal Vasko03ff5a72019-09-11 13:49:33 +02003920 for (; node; node = (struct lyd_node *)node->parent) {
Michal Vasko9f96a052020-03-10 09:41:45 +01003921 for (meta = node->meta; meta; meta = meta->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003922 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01003923 if (meta->name && !strcmp(meta->name, "lang") && !strcmp(meta->annotation->module->name, "xml")) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003924 break;
3925 }
3926 }
3927
Michal Vasko9f96a052020-03-10 09:41:45 +01003928 if (meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003929 break;
3930 }
3931 }
3932
3933 /* compare languages */
Michal Vasko9f96a052020-03-10 09:41:45 +01003934 if (!meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003935 set_fill_boolean(set, 0);
3936 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01003937 val = lyd_meta2str(meta, &dynamic);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003938 for (i = 0; args[0]->val.str[i]; ++i) {
3939 if (tolower(args[0]->val.str[i]) != tolower(val[i])) {
3940 set_fill_boolean(set, 0);
3941 break;
3942 }
3943 }
3944 if (!args[0]->val.str[i]) {
3945 if (!val[i] || (val[i] == '-')) {
3946 set_fill_boolean(set, 1);
3947 } else {
3948 set_fill_boolean(set, 0);
3949 }
3950 }
3951 if (dynamic) {
3952 free((char *)val);
3953 }
3954 }
3955
3956 return LY_SUCCESS;
3957}
3958
3959/**
3960 * @brief Execute the XPath last() function. Returns LYXP_SET_NUMBER
3961 * with the context size.
3962 *
3963 * @param[in] args Array of arguments.
3964 * @param[in] arg_count Count of elements in @p args.
3965 * @param[in,out] set Context and result set at the same time.
3966 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003967 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003968 */
3969static LY_ERR
3970xpath_last(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3971{
3972 if (options & LYXP_SCNODE_ALL) {
3973 set_scnode_clear_ctx(set);
3974 return LY_SUCCESS;
3975 }
3976
Michal Vasko03ff5a72019-09-11 13:49:33 +02003977 if (set->type != LYXP_SET_NODE_SET) {
3978 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "last()");
3979 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02003980 } else if (!set->used) {
3981 set_fill_number(set, 0);
3982 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003983 }
3984
3985 set_fill_number(set, set->ctx_size);
3986 return LY_SUCCESS;
3987}
3988
3989/**
3990 * @brief Execute the XPath local-name(node-set?) function. Returns LYXP_SET_STRING
3991 * with the node name without namespace from the argument or the context.
3992 *
3993 * @param[in] args Array of arguments.
3994 * @param[in] arg_count Count of elements in @p args.
3995 * @param[in,out] set Context and result set at the same time.
3996 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003997 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003998 */
3999static LY_ERR
4000xpath_local_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
4001{
4002 struct lyxp_set_node *item;
4003 /* suppress unused variable warning */
4004 (void)options;
4005
4006 if (options & LYXP_SCNODE_ALL) {
4007 set_scnode_clear_ctx(set);
4008 return LY_SUCCESS;
4009 }
4010
4011 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004012 if (args[0]->type != LYXP_SET_NODE_SET) {
4013 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "local-name(node-set?)");
4014 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004015 } else if (!args[0]->used) {
4016 set_fill_string(set, "", 0);
4017 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004018 }
4019
4020 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004021 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004022
4023 item = &args[0]->val.nodes[0];
4024 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004025 if (set->type != LYXP_SET_NODE_SET) {
4026 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "local-name(node-set?)");
4027 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004028 } else if (!set->used) {
4029 set_fill_string(set, "", 0);
4030 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004031 }
4032
4033 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004034 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004035
4036 item = &set->val.nodes[0];
4037 }
4038
4039 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004040 case LYXP_NODE_NONE:
4041 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004042 case LYXP_NODE_ROOT:
4043 case LYXP_NODE_ROOT_CONFIG:
4044 case LYXP_NODE_TEXT:
4045 set_fill_string(set, "", 0);
4046 break;
4047 case LYXP_NODE_ELEM:
4048 set_fill_string(set, item->node->schema->name, strlen(item->node->schema->name));
4049 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004050 case LYXP_NODE_META:
4051 set_fill_string(set, ((struct lyd_meta *)item->node)->name, strlen(((struct lyd_meta *)item->node)->name));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004052 break;
4053 }
4054
4055 return LY_SUCCESS;
4056}
4057
4058/**
4059 * @brief Execute the XPath name(node-set?) function. Returns LYXP_SET_STRING
4060 * with the node name fully qualified (with namespace) from the argument or the context.
4061 *
4062 * @param[in] args Array of arguments.
4063 * @param[in] arg_count Count of elements in @p args.
4064 * @param[in,out] set Context and result set at the same time.
4065 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004066 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004067 */
4068static LY_ERR
4069xpath_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
4070{
4071 struct lyxp_set_node *item;
4072 struct lys_module *mod;
4073 char *str;
4074 const char *name;
4075 int rc;
4076
4077 if (options & LYXP_SCNODE_ALL) {
4078 set_scnode_clear_ctx(set);
4079 return LY_SUCCESS;
4080 }
4081
4082 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004083 if (args[0]->type != LYXP_SET_NODE_SET) {
4084 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "name(node-set?)");
4085 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004086 } else if (!args[0]->used) {
4087 set_fill_string(set, "", 0);
4088 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004089 }
4090
4091 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004092 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004093
4094 item = &args[0]->val.nodes[0];
4095 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004096 if (set->type != LYXP_SET_NODE_SET) {
4097 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "name(node-set?)");
4098 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004099 } else if (!set->used) {
4100 set_fill_string(set, "", 0);
4101 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004102 }
4103
4104 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004105 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004106
4107 item = &set->val.nodes[0];
4108 }
4109
4110 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004111 case LYXP_NODE_NONE:
4112 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004113 case LYXP_NODE_ROOT:
4114 case LYXP_NODE_ROOT_CONFIG:
4115 case LYXP_NODE_TEXT:
4116 mod = NULL;
4117 name = NULL;
4118 break;
4119 case LYXP_NODE_ELEM:
4120 mod = item->node->schema->module;
4121 name = item->node->schema->name;
4122 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004123 case LYXP_NODE_META:
4124 mod = ((struct lyd_meta *)item->node)->annotation->module;
4125 name = ((struct lyd_meta *)item->node)->name;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004126 break;
4127 }
4128
4129 if (mod && name) {
4130 switch (set->format) {
Michal Vasko52927e22020-03-16 17:26:14 +01004131 case LYD_SCHEMA:
Michal Vasko03ff5a72019-09-11 13:49:33 +02004132 rc = asprintf(&str, "%s:%s", lys_prefix_find_module(set->local_mod, mod), name);
4133 break;
4134 case LYD_JSON:
4135 rc = asprintf(&str, "%s:%s", mod->name, name);
4136 break;
Michal Vasko52927e22020-03-16 17:26:14 +01004137 case LYD_XML:
Michal Vasko9409ef62019-09-12 11:47:17 +02004138 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004139 }
4140 LY_CHECK_ERR_RET(rc == -1, LOGMEM(set->ctx), LY_EMEM);
4141 set_fill_string(set, str, strlen(str));
4142 free(str);
4143 } else {
4144 set_fill_string(set, "", 0);
4145 }
4146
4147 return LY_SUCCESS;
4148}
4149
4150/**
4151 * @brief Execute the XPath namespace-uri(node-set?) function. Returns LYXP_SET_STRING
4152 * with the namespace of the node from the argument or the context.
4153 *
4154 * @param[in] args Array of arguments.
4155 * @param[in] arg_count Count of elements in @p args.
4156 * @param[in,out] set Context and result set at the same time.
4157 * @param[in] options XPath options.
4158 * @return LY_ERR (LY_EINVAL for wrong arguments on schema)
4159 */
4160static LY_ERR
4161xpath_namespace_uri(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
4162{
4163 struct lyxp_set_node *item;
4164 struct lys_module *mod;
4165 /* suppress unused variable warning */
4166 (void)options;
4167
4168 if (options & LYXP_SCNODE_ALL) {
4169 set_scnode_clear_ctx(set);
4170 return LY_SUCCESS;
4171 }
4172
4173 if (arg_count) {
Michal Vaskod3678892020-05-21 10:06:58 +02004174 if (args[0]->type != LYXP_SET_NODE_SET) {
4175 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
4176 "namespace-uri(node-set?)");
4177 return LY_EVALID;
4178 } else if (!args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004179 set_fill_string(set, "", 0);
4180 return LY_SUCCESS;
4181 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004182
4183 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004184 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004185
4186 item = &args[0]->val.nodes[0];
4187 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004188 if (set->type != LYXP_SET_NODE_SET) {
4189 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "namespace-uri(node-set?)");
4190 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004191 } else if (!set->used) {
4192 set_fill_string(set, "", 0);
4193 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004194 }
4195
4196 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004197 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004198
4199 item = &set->val.nodes[0];
4200 }
4201
4202 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004203 case LYXP_NODE_NONE:
4204 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004205 case LYXP_NODE_ROOT:
4206 case LYXP_NODE_ROOT_CONFIG:
4207 case LYXP_NODE_TEXT:
4208 set_fill_string(set, "", 0);
4209 break;
4210 case LYXP_NODE_ELEM:
Michal Vasko9f96a052020-03-10 09:41:45 +01004211 case LYXP_NODE_META:
Michal Vasko03ff5a72019-09-11 13:49:33 +02004212 if (item->type == LYXP_NODE_ELEM) {
4213 mod = item->node->schema->module;
Michal Vasko9f96a052020-03-10 09:41:45 +01004214 } else { /* LYXP_NODE_META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004215 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004216 mod = ((struct lyd_meta *)item->node)->annotation->module;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004217 }
4218
4219 set_fill_string(set, mod->ns, strlen(mod->ns));
4220 break;
4221 }
4222
4223 return LY_SUCCESS;
4224}
4225
4226/**
4227 * @brief Execute the XPath node() function (node type). Returns LYXP_SET_NODE_SET
4228 * with only nodes from the context. In practice it either leaves the context
4229 * as it is or returns an empty node set.
4230 *
4231 * @param[in] args Array of arguments.
4232 * @param[in] arg_count Count of elements in @p args.
4233 * @param[in,out] set Context and result set at the same time.
4234 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004235 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004236 */
4237static LY_ERR
4238xpath_node(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4239{
4240 if (options & LYXP_SCNODE_ALL) {
4241 set_scnode_clear_ctx(set);
4242 return LY_SUCCESS;
4243 }
4244
4245 if (set->type != LYXP_SET_NODE_SET) {
Michal Vaskod3678892020-05-21 10:06:58 +02004246 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004247 }
4248 return LY_SUCCESS;
4249}
4250
4251/**
4252 * @brief Execute the XPath normalize-space(string?) function. Returns LYXP_SET_STRING
4253 * with normalized value (no leading, trailing, double white spaces) of the node
4254 * from the argument or the context.
4255 *
4256 * @param[in] args Array of arguments.
4257 * @param[in] arg_count Count of elements in @p args.
4258 * @param[in,out] set Context and result set at the same time.
4259 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004260 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004261 */
4262static LY_ERR
4263xpath_normalize_space(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
4264{
4265 uint16_t i, new_used;
4266 char *new;
4267 int have_spaces = 0, space_before = 0;
4268 struct lysc_node_leaf *sleaf;
4269 LY_ERR rc = LY_SUCCESS;
4270
4271 if (options & LYXP_SCNODE_ALL) {
4272 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4273 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4274 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 +02004275 } else if (!warn_is_string_type(sleaf->type)) {
4276 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004277 }
4278 }
4279 set_scnode_clear_ctx(set);
4280 return rc;
4281 }
4282
4283 if (arg_count) {
4284 set_fill_set(set, args[0]);
4285 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004286 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004287 LY_CHECK_RET(rc);
4288
4289 /* is there any normalization necessary? */
4290 for (i = 0; set->val.str[i]; ++i) {
4291 if (is_xmlws(set->val.str[i])) {
4292 if ((i == 0) || space_before || (!set->val.str[i + 1])) {
4293 have_spaces = 1;
4294 break;
4295 }
4296 space_before = 1;
4297 } else {
4298 space_before = 0;
4299 }
4300 }
4301
4302 /* yep, there is */
4303 if (have_spaces) {
4304 /* it's enough, at least one character will go, makes space for ending '\0' */
4305 new = malloc(strlen(set->val.str) * sizeof(char));
4306 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4307 new_used = 0;
4308
4309 space_before = 0;
4310 for (i = 0; set->val.str[i]; ++i) {
4311 if (is_xmlws(set->val.str[i])) {
4312 if ((i == 0) || space_before) {
4313 space_before = 1;
4314 continue;
4315 } else {
4316 space_before = 1;
4317 }
4318 } else {
4319 space_before = 0;
4320 }
4321
4322 new[new_used] = (space_before ? ' ' : set->val.str[i]);
4323 ++new_used;
4324 }
4325
4326 /* at worst there is one trailing space now */
4327 if (new_used && is_xmlws(new[new_used - 1])) {
4328 --new_used;
4329 }
4330
4331 new = ly_realloc(new, (new_used + 1) * sizeof(char));
4332 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4333 new[new_used] = '\0';
4334
4335 free(set->val.str);
4336 set->val.str = new;
4337 }
4338
4339 return LY_SUCCESS;
4340}
4341
4342/**
4343 * @brief Execute the XPath not(boolean) function. Returns LYXP_SET_BOOLEAN
4344 * with the argument converted to boolean and logically inverted.
4345 *
4346 * @param[in] args Array of arguments.
4347 * @param[in] arg_count Count of elements in @p args.
4348 * @param[in,out] set Context and result set at the same time.
4349 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004350 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004351 */
4352static LY_ERR
4353xpath_not(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4354{
4355 if (options & LYXP_SCNODE_ALL) {
4356 set_scnode_clear_ctx(set);
4357 return LY_SUCCESS;
4358 }
4359
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004360 lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004361 if (args[0]->val.bool) {
4362 set_fill_boolean(set, 0);
4363 } else {
4364 set_fill_boolean(set, 1);
4365 }
4366
4367 return LY_SUCCESS;
4368}
4369
4370/**
4371 * @brief Execute the XPath number(object?) function. Returns LYXP_SET_NUMBER
4372 * with the number representation of either the argument or the context.
4373 *
4374 * @param[in] args Array of arguments.
4375 * @param[in] arg_count Count of elements in @p args.
4376 * @param[in,out] set Context and result set at the same time.
4377 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004378 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004379 */
4380static LY_ERR
4381xpath_number(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
4382{
4383 LY_ERR rc;
4384
4385 if (options & LYXP_SCNODE_ALL) {
4386 set_scnode_clear_ctx(set);
4387 return LY_SUCCESS;
4388 }
4389
4390 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004391 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004392 LY_CHECK_RET(rc);
4393 set_fill_set(set, args[0]);
4394 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004395 rc = lyxp_set_cast(set, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004396 LY_CHECK_RET(rc);
4397 }
4398
4399 return LY_SUCCESS;
4400}
4401
4402/**
4403 * @brief Execute the XPath position() function. Returns LYXP_SET_NUMBER
4404 * with the context position.
4405 *
4406 * @param[in] args Array of arguments.
4407 * @param[in] arg_count Count of elements in @p args.
4408 * @param[in,out] set Context and result set at the same time.
4409 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004410 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004411 */
4412static LY_ERR
4413xpath_position(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4414{
4415 if (options & LYXP_SCNODE_ALL) {
4416 set_scnode_clear_ctx(set);
4417 return LY_SUCCESS;
4418 }
4419
Michal Vasko03ff5a72019-09-11 13:49:33 +02004420 if (set->type != LYXP_SET_NODE_SET) {
4421 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "position()");
4422 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004423 } else if (!set->used) {
4424 set_fill_number(set, 0);
4425 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004426 }
4427
4428 set_fill_number(set, set->ctx_pos);
4429
4430 /* UNUSED in 'Release' build type */
4431 (void)options;
4432 return LY_SUCCESS;
4433}
4434
4435/**
4436 * @brief Execute the YANG 1.1 re-match(string, string) function. Returns LYXP_SET_BOOLEAN
4437 * depending on whether the second argument regex matches the first argument string. For details refer to
4438 * YANG 1.1 RFC section 10.2.1.
4439 *
4440 * @param[in] args Array of arguments.
4441 * @param[in] arg_count Count of elements in @p args.
4442 * @param[in,out] set Context and result set at the same time.
4443 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004444 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004445 */
4446static LY_ERR
4447xpath_re_match(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4448{
4449 struct lysc_pattern **patterns = NULL, **pattern;
4450 struct lysc_node_leaf *sleaf;
4451 char *path;
4452 LY_ERR rc = LY_SUCCESS;
4453 struct ly_err_item *err;
4454
4455 if (options & LYXP_SCNODE_ALL) {
4456 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4457 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4458 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 +02004459 } else if (!warn_is_string_type(sleaf->type)) {
4460 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004461 }
4462 }
4463
4464 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4465 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4466 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 +02004467 } else if (!warn_is_string_type(sleaf->type)) {
4468 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004469 }
4470 }
4471 set_scnode_clear_ctx(set);
4472 return rc;
4473 }
4474
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004475 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004476 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004477 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004478 LY_CHECK_RET(rc);
4479
4480 LY_ARRAY_NEW_RET(set->ctx, patterns, pattern, LY_EMEM);
4481 *pattern = malloc(sizeof **pattern);
4482 path = lyd_path(set->ctx_node, LYD_PATH_LOG, NULL, 0);
4483 rc = lys_compile_type_pattern_check(set->ctx, path, args[1]->val.str, &(*pattern)->code);
4484 free(path);
4485 if (rc != LY_SUCCESS) {
4486 LY_ARRAY_FREE(patterns);
4487 return rc;
4488 }
4489
4490 rc = ly_type_validate_patterns(patterns, args[0]->val.str, strlen(args[0]->val.str), &err);
4491 pcre2_code_free((*pattern)->code);
4492 free(*pattern);
4493 LY_ARRAY_FREE(patterns);
4494 if (rc && (rc != LY_EVALID)) {
4495 ly_err_print(err);
4496 ly_err_free(err);
4497 return rc;
4498 }
4499
4500 if (rc == LY_EVALID) {
4501 ly_err_free(err);
4502 set_fill_boolean(set, 0);
4503 } else {
4504 set_fill_boolean(set, 1);
4505 }
4506
4507 return LY_SUCCESS;
4508}
4509
4510/**
4511 * @brief Execute the XPath round(number) function. Returns LYXP_SET_NUMBER
4512 * with the rounded first argument. For details refer to
4513 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-round.
4514 *
4515 * @param[in] args Array of arguments.
4516 * @param[in] arg_count Count of elements in @p args.
4517 * @param[in,out] set Context and result set at the same time.
4518 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004519 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004520 */
4521static LY_ERR
4522xpath_round(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4523{
4524 struct lysc_node_leaf *sleaf;
4525 LY_ERR rc = LY_SUCCESS;
4526
4527 if (options & LYXP_SCNODE_ALL) {
4528 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4529 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004530 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4531 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 +02004532 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
4533 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004534 }
4535 set_scnode_clear_ctx(set);
4536 return rc;
4537 }
4538
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004539 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004540 LY_CHECK_RET(rc);
4541
4542 /* cover only the cases where floor can't be used */
4543 if ((args[0]->val.num == -0.0f) || ((args[0]->val.num < 0) && (args[0]->val.num >= -0.5))) {
4544 set_fill_number(set, -0.0f);
4545 } else {
4546 args[0]->val.num += 0.5;
4547 rc = xpath_floor(args, 1, args[0], options);
4548 LY_CHECK_RET(rc);
4549 set_fill_number(set, args[0]->val.num);
4550 }
4551
4552 return LY_SUCCESS;
4553}
4554
4555/**
4556 * @brief Execute the XPath starts-with(string, string) function.
4557 * Returns LYXP_SET_BOOLEAN whether the second argument is
4558 * the prefix of the first or not.
4559 *
4560 * @param[in] args Array of arguments.
4561 * @param[in] arg_count Count of elements in @p args.
4562 * @param[in,out] set Context and result set at the same time.
4563 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004564 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004565 */
4566static LY_ERR
4567xpath_starts_with(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4568{
4569 struct lysc_node_leaf *sleaf;
4570 LY_ERR rc = LY_SUCCESS;
4571
4572 if (options & LYXP_SCNODE_ALL) {
4573 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4574 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4575 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 +02004576 } else if (!warn_is_string_type(sleaf->type)) {
4577 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004578 }
4579 }
4580
4581 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4582 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4583 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 +02004584 } else if (!warn_is_string_type(sleaf->type)) {
4585 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004586 }
4587 }
4588 set_scnode_clear_ctx(set);
4589 return rc;
4590 }
4591
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004592 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004593 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004594 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004595 LY_CHECK_RET(rc);
4596
4597 if (strncmp(args[0]->val.str, args[1]->val.str, strlen(args[1]->val.str))) {
4598 set_fill_boolean(set, 0);
4599 } else {
4600 set_fill_boolean(set, 1);
4601 }
4602
4603 return LY_SUCCESS;
4604}
4605
4606/**
4607 * @brief Execute the XPath string(object?) function. Returns LYXP_SET_STRING
4608 * with the string representation of either the argument or the context.
4609 *
4610 * @param[in] args Array of arguments.
4611 * @param[in] arg_count Count of elements in @p args.
4612 * @param[in,out] set Context and result set at the same time.
4613 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004614 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004615 */
4616static LY_ERR
4617xpath_string(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
4618{
4619 LY_ERR rc;
4620
4621 if (options & LYXP_SCNODE_ALL) {
4622 set_scnode_clear_ctx(set);
4623 return LY_SUCCESS;
4624 }
4625
4626 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004627 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004628 LY_CHECK_RET(rc);
4629 set_fill_set(set, args[0]);
4630 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004631 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004632 LY_CHECK_RET(rc);
4633 }
4634
4635 return LY_SUCCESS;
4636}
4637
4638/**
4639 * @brief Execute the XPath string-length(string?) function. Returns LYXP_SET_NUMBER
4640 * with the length of the string in either the argument or the context.
4641 *
4642 * @param[in] args Array of arguments.
4643 * @param[in] arg_count Count of elements in @p args.
4644 * @param[in,out] set Context and result set at the same time.
4645 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004646 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004647 */
4648static LY_ERR
4649xpath_string_length(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
4650{
4651 struct lysc_node_leaf *sleaf;
4652 LY_ERR rc = LY_SUCCESS;
4653
4654 if (options & LYXP_SCNODE_ALL) {
4655 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4656 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4657 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 +02004658 } else if (!warn_is_string_type(sleaf->type)) {
4659 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004660 }
4661 }
4662 if (!arg_count && (set->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set))) {
4663 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4664 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 +02004665 } else if (!warn_is_string_type(sleaf->type)) {
4666 LOGWRN(set->ctx, "Argument #0 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004667 }
4668 }
4669 set_scnode_clear_ctx(set);
4670 return rc;
4671 }
4672
4673 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004674 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004675 LY_CHECK_RET(rc);
4676 set_fill_number(set, strlen(args[0]->val.str));
4677 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004678 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004679 LY_CHECK_RET(rc);
4680 set_fill_number(set, strlen(set->val.str));
4681 }
4682
4683 return LY_SUCCESS;
4684}
4685
4686/**
4687 * @brief Execute the XPath substring(string, number, number?) function.
4688 * Returns LYXP_SET_STRING substring of the first argument starting
4689 * on the second argument index ending on the third argument index,
4690 * indexed from 1. For exact definition refer to
4691 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-substring.
4692 *
4693 * @param[in] args Array of arguments.
4694 * @param[in] arg_count Count of elements in @p args.
4695 * @param[in,out] set Context and result set at the same time.
4696 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004697 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004698 */
4699static LY_ERR
4700xpath_substring(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
4701{
4702 int start, len;
4703 uint16_t str_start, str_len, pos;
4704 struct lysc_node_leaf *sleaf;
4705 LY_ERR rc = LY_SUCCESS;
4706
4707 if (options & LYXP_SCNODE_ALL) {
4708 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4709 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4710 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 +02004711 } else if (!warn_is_string_type(sleaf->type)) {
4712 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004713 }
4714 }
4715
4716 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4717 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4718 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 +02004719 } else if (!warn_is_numeric_type(sleaf->type)) {
4720 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004721 }
4722 }
4723
4724 if ((arg_count == 3) && (args[2]->type == LYXP_SET_SCNODE_SET)
4725 && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
4726 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4727 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 +02004728 } else if (!warn_is_numeric_type(sleaf->type)) {
4729 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004730 }
4731 }
4732 set_scnode_clear_ctx(set);
4733 return rc;
4734 }
4735
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004736 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004737 LY_CHECK_RET(rc);
4738
4739 /* start */
4740 if (xpath_round(&args[1], 1, args[1], options)) {
4741 return -1;
4742 }
4743 if (isfinite(args[1]->val.num)) {
4744 start = args[1]->val.num - 1;
4745 } else if (isinf(args[1]->val.num) && signbit(args[1]->val.num)) {
4746 start = INT_MIN;
4747 } else {
4748 start = INT_MAX;
4749 }
4750
4751 /* len */
4752 if (arg_count == 3) {
4753 rc = xpath_round(&args[2], 1, args[2], options);
4754 LY_CHECK_RET(rc);
4755 if (isfinite(args[2]->val.num)) {
4756 len = args[2]->val.num;
4757 } else if (isnan(args[2]->val.num) || signbit(args[2]->val.num)) {
4758 len = 0;
4759 } else {
4760 len = INT_MAX;
4761 }
4762 } else {
4763 len = INT_MAX;
4764 }
4765
4766 /* find matching character positions */
4767 str_start = 0;
4768 str_len = 0;
4769 for (pos = 0; args[0]->val.str[pos]; ++pos) {
4770 if (pos < start) {
4771 ++str_start;
4772 } else if (pos < start + len) {
4773 ++str_len;
4774 } else {
4775 break;
4776 }
4777 }
4778
4779 set_fill_string(set, args[0]->val.str + str_start, str_len);
4780 return LY_SUCCESS;
4781}
4782
4783/**
4784 * @brief Execute the XPath substring-after(string, string) function.
4785 * Returns LYXP_SET_STRING with the string succeeding the occurance
4786 * of the second argument in the first or an empty string.
4787 *
4788 * @param[in] args Array of arguments.
4789 * @param[in] arg_count Count of elements in @p args.
4790 * @param[in,out] set Context and result set at the same time.
4791 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004792 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004793 */
4794static LY_ERR
4795xpath_substring_after(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4796{
4797 char *ptr;
4798 struct lysc_node_leaf *sleaf;
4799 LY_ERR rc = LY_SUCCESS;
4800
4801 if (options & LYXP_SCNODE_ALL) {
4802 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4803 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4804 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 +02004805 } else if (!warn_is_string_type(sleaf->type)) {
4806 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004807 }
4808 }
4809
4810 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4811 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4812 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 +02004813 } else if (!warn_is_string_type(sleaf->type)) {
4814 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004815 }
4816 }
4817 set_scnode_clear_ctx(set);
4818 return rc;
4819 }
4820
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004821 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004822 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004823 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004824 LY_CHECK_RET(rc);
4825
4826 ptr = strstr(args[0]->val.str, args[1]->val.str);
4827 if (ptr) {
4828 set_fill_string(set, ptr + strlen(args[1]->val.str), strlen(ptr + strlen(args[1]->val.str)));
4829 } else {
4830 set_fill_string(set, "", 0);
4831 }
4832
4833 return LY_SUCCESS;
4834}
4835
4836/**
4837 * @brief Execute the XPath substring-before(string, string) function.
4838 * Returns LYXP_SET_STRING with the string preceding the occurance
4839 * of the second argument in the first or an empty string.
4840 *
4841 * @param[in] args Array of arguments.
4842 * @param[in] arg_count Count of elements in @p args.
4843 * @param[in,out] set Context and result set at the same time.
4844 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004845 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004846 */
4847static LY_ERR
4848xpath_substring_before(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4849{
4850 char *ptr;
4851 struct lysc_node_leaf *sleaf;
4852 LY_ERR rc = LY_SUCCESS;
4853
4854 if (options & LYXP_SCNODE_ALL) {
4855 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4856 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4857 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 +02004858 } else if (!warn_is_string_type(sleaf->type)) {
4859 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004860 }
4861 }
4862
4863 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4864 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4865 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 +02004866 } else if (!warn_is_string_type(sleaf->type)) {
4867 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004868 }
4869 }
4870 set_scnode_clear_ctx(set);
4871 return rc;
4872 }
4873
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004874 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004875 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004876 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004877 LY_CHECK_RET(rc);
4878
4879 ptr = strstr(args[0]->val.str, args[1]->val.str);
4880 if (ptr) {
4881 set_fill_string(set, args[0]->val.str, ptr - args[0]->val.str);
4882 } else {
4883 set_fill_string(set, "", 0);
4884 }
4885
4886 return LY_SUCCESS;
4887}
4888
4889/**
4890 * @brief Execute the XPath sum(node-set) function. Returns LYXP_SET_NUMBER
4891 * with the sum of all the nodes in the context.
4892 *
4893 * @param[in] args Array of arguments.
4894 * @param[in] arg_count Count of elements in @p args.
4895 * @param[in,out] set Context and result set at the same time.
4896 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004897 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004898 */
4899static LY_ERR
4900xpath_sum(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4901{
4902 long double num;
4903 char *str;
4904 uint16_t i;
4905 struct lyxp_set set_item;
4906 struct lysc_node_leaf *sleaf;
4907 LY_ERR rc = LY_SUCCESS;
4908
4909 if (options & LYXP_SCNODE_ALL) {
4910 if (args[0]->type == LYXP_SET_SCNODE_SET) {
4911 for (i = 0; i < args[0]->used; ++i) {
4912 if (args[0]->val.scnodes[i].in_ctx == 1) {
4913 sleaf = (struct lysc_node_leaf *)args[0]->val.scnodes[i].scnode;
4914 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4915 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__,
4916 lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004917 } else if (!warn_is_numeric_type(sleaf->type)) {
4918 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004919 }
4920 }
4921 }
4922 }
4923 set_scnode_clear_ctx(set);
4924 return rc;
4925 }
4926
4927 set_fill_number(set, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004928
4929 if (args[0]->type != LYXP_SET_NODE_SET) {
4930 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "sum(node-set)");
4931 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004932 } else if (!args[0]->used) {
4933 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004934 }
4935
Michal Vasko5c4e5892019-11-14 12:31:38 +01004936 set_init(&set_item, set);
4937
Michal Vasko03ff5a72019-09-11 13:49:33 +02004938 set_item.type = LYXP_SET_NODE_SET;
4939 set_item.val.nodes = malloc(sizeof *set_item.val.nodes);
4940 LY_CHECK_ERR_RET(!set_item.val.nodes, LOGMEM(set->ctx), LY_EMEM);
4941
4942 set_item.used = 1;
4943 set_item.size = 1;
4944
4945 for (i = 0; i < args[0]->used; ++i) {
4946 set_item.val.nodes[0] = args[0]->val.nodes[i];
4947
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004948 rc = cast_node_set_to_string(&set_item, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004949 LY_CHECK_RET(rc);
4950 num = cast_string_to_number(str);
4951 free(str);
4952 set->val.num += num;
4953 }
4954
4955 free(set_item.val.nodes);
4956
4957 return LY_SUCCESS;
4958}
4959
4960/**
4961 * @brief Execute the XPath text() function (node type). Returns LYXP_SET_NODE_SET
4962 * with the text content of the nodes in the context.
4963 *
4964 * @param[in] args Array of arguments.
4965 * @param[in] arg_count Count of elements in @p args.
4966 * @param[in,out] set Context and result set at the same time.
4967 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004968 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004969 */
4970static LY_ERR
4971xpath_text(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4972{
4973 uint32_t i;
4974
4975 if (options & LYXP_SCNODE_ALL) {
4976 set_scnode_clear_ctx(set);
4977 return LY_SUCCESS;
4978 }
4979
Michal Vasko03ff5a72019-09-11 13:49:33 +02004980 if (set->type != LYXP_SET_NODE_SET) {
4981 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "text()");
4982 return LY_EVALID;
4983 }
4984
4985 for (i = 0; i < set->used;) {
4986 switch (set->val.nodes[i].type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004987 case LYXP_NODE_NONE:
4988 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004989 case LYXP_NODE_ELEM:
Michal Vasko03ff5a72019-09-11 13:49:33 +02004990 if (set->val.nodes[i].node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
4991 set->val.nodes[i].type = LYXP_NODE_TEXT;
4992 ++i;
4993 break;
4994 }
4995 /* fall through */
4996 case LYXP_NODE_ROOT:
4997 case LYXP_NODE_ROOT_CONFIG:
4998 case LYXP_NODE_TEXT:
Michal Vasko9f96a052020-03-10 09:41:45 +01004999 case LYXP_NODE_META:
Michal Vasko03ff5a72019-09-11 13:49:33 +02005000 set_remove_node(set, i);
5001 break;
5002 }
5003 }
5004
5005 return LY_SUCCESS;
5006}
5007
5008/**
5009 * @brief Execute the XPath translate(string, string, string) function.
5010 * Returns LYXP_SET_STRING with the first argument with the characters
5011 * from the second argument replaced by those on the corresponding
5012 * positions in the third argument.
5013 *
5014 * @param[in] args Array of arguments.
5015 * @param[in] arg_count Count of elements in @p args.
5016 * @param[in,out] set Context and result set at the same time.
5017 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005018 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005019 */
5020static LY_ERR
5021xpath_translate(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
5022{
5023 uint16_t i, j, new_used;
5024 char *new;
5025 int found, have_removed;
5026 struct lysc_node_leaf *sleaf;
5027 LY_ERR rc = LY_SUCCESS;
5028
5029 if (options & LYXP_SCNODE_ALL) {
5030 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5031 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5032 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 +02005033 } else if (!warn_is_string_type(sleaf->type)) {
5034 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005035 }
5036 }
5037
5038 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5039 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5040 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 +02005041 } else if (!warn_is_string_type(sleaf->type)) {
5042 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005043 }
5044 }
5045
5046 if ((args[2]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
5047 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5048 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 +02005049 } else if (!warn_is_string_type(sleaf->type)) {
5050 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005051 }
5052 }
5053 set_scnode_clear_ctx(set);
5054 return rc;
5055 }
5056
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005057 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005058 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005059 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005060 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005061 rc = lyxp_set_cast(args[2], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005062 LY_CHECK_RET(rc);
5063
5064 new = malloc((strlen(args[0]->val.str) + 1) * sizeof(char));
5065 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5066 new_used = 0;
5067
5068 have_removed = 0;
5069 for (i = 0; args[0]->val.str[i]; ++i) {
5070 found = 0;
5071
5072 for (j = 0; args[1]->val.str[j]; ++j) {
5073 if (args[0]->val.str[i] == args[1]->val.str[j]) {
5074 /* removing this char */
5075 if (j >= strlen(args[2]->val.str)) {
5076 have_removed = 1;
5077 found = 1;
5078 break;
5079 }
5080 /* replacing this char */
5081 new[new_used] = args[2]->val.str[j];
5082 ++new_used;
5083 found = 1;
5084 break;
5085 }
5086 }
5087
5088 /* copying this char */
5089 if (!found) {
5090 new[new_used] = args[0]->val.str[i];
5091 ++new_used;
5092 }
5093 }
5094
5095 if (have_removed) {
5096 new = ly_realloc(new, (new_used + 1) * sizeof(char));
5097 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5098 }
5099 new[new_used] = '\0';
5100
Michal Vaskod3678892020-05-21 10:06:58 +02005101 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005102 set->type = LYXP_SET_STRING;
5103 set->val.str = new;
5104
5105 return LY_SUCCESS;
5106}
5107
5108/**
5109 * @brief Execute the XPath true() function. Returns LYXP_SET_BOOLEAN
5110 * with true value.
5111 *
5112 * @param[in] args Array of arguments.
5113 * @param[in] arg_count Count of elements in @p args.
5114 * @param[in,out] set Context and result set at the same time.
5115 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005116 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005117 */
5118static LY_ERR
5119xpath_true(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
5120{
5121 if (options & LYXP_SCNODE_ALL) {
5122 set_scnode_clear_ctx(set);
5123 return LY_SUCCESS;
5124 }
5125
5126 set_fill_boolean(set, 1);
5127 return LY_SUCCESS;
5128}
5129
5130/*
5131 * moveto functions
5132 *
5133 * They and only they actually change the context (set).
5134 */
5135
5136/**
Michal Vasko6346ece2019-09-24 13:12:53 +02005137 * @brief Skip prefix and return corresponding model if there is a prefix. Logs directly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005138 *
Michal Vasko2104e9f2020-03-06 08:23:25 +01005139 * XPath @p set is expected to be a (sc)node set!
5140 *
Michal Vasko6346ece2019-09-24 13:12:53 +02005141 * @param[in,out] qname Qualified node name. If includes prefix, it is skipped.
5142 * @param[in,out] qname_len Length of @p qname, is updated accordingly.
5143 * @param[in] set Set with XPath context.
5144 * @param[out] moveto_mod Expected module of a matching node.
5145 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005146 */
Michal Vasko6346ece2019-09-24 13:12:53 +02005147static LY_ERR
5148moveto_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 +02005149{
Michal Vasko6346ece2019-09-24 13:12:53 +02005150 const struct lys_module *mod;
5151 const char *ptr;
5152 int pref_len;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005153 char *str;
5154
Michal Vasko2104e9f2020-03-06 08:23:25 +01005155 assert((set->type == LYXP_SET_NODE_SET) || (set->type == LYXP_SET_SCNODE_SET));
5156
Michal Vasko6346ece2019-09-24 13:12:53 +02005157 if ((ptr = ly_strnchr(*qname, ':', *qname_len))) {
5158 /* specific module */
5159 pref_len = ptr - *qname;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005160
Michal Vasko6346ece2019-09-24 13:12:53 +02005161 switch (set->format) {
Michal Vasko52927e22020-03-16 17:26:14 +01005162 case LYD_SCHEMA:
Michal Vasko6346ece2019-09-24 13:12:53 +02005163 /* schema, search all local module imports */
5164 mod = lys_module_find_prefix(set->local_mod, *qname, pref_len);
5165 break;
5166 case LYD_JSON:
5167 /* JSON data, search in context */
5168 str = strndup(*qname, pref_len);
Michal Vasko97e76fd2020-05-27 15:22:01 +02005169 mod = ly_ctx_get_module_implemented(set->ctx, str);
Michal Vasko6346ece2019-09-24 13:12:53 +02005170 free(str);
5171 break;
Michal Vasko52927e22020-03-16 17:26:14 +01005172 case LYD_XML:
Michal Vasko6346ece2019-09-24 13:12:53 +02005173 LOGINT_RET(set->ctx);
5174 }
5175
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005176 /* Check for errors and non-implemented modules, as they are not valid */
5177 if (!mod || !mod->implemented) {
Michal Vasko2104e9f2020-03-06 08:23:25 +01005178 if (set->type == LYXP_SET_SCNODE_SET) {
5179 LOGVAL(set->ctx, LY_VLOG_LYSC, set->ctx_scnode, LY_VCODE_XP_INMOD, pref_len, *qname);
5180 } else {
5181 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INMOD, pref_len, *qname);
5182 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005183 return LY_EVALID;
5184 }
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005185
Michal Vasko6346ece2019-09-24 13:12:53 +02005186 *qname += pref_len + 1;
5187 *qname_len -= pref_len + 1;
5188 } else if (((*qname)[0] == '*') && (*qname_len == 1)) {
5189 /* all modules - special case */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005190 mod = NULL;
Michal Vasko6346ece2019-09-24 13:12:53 +02005191 } else {
5192 /* local module */
5193 mod = set->local_mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005194 }
5195
Michal Vasko6346ece2019-09-24 13:12:53 +02005196 *moveto_mod = mod;
5197 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005198}
5199
5200/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02005201 * @brief Move context @p set to the root. Handles absolute path.
5202 * Result is LYXP_SET_NODE_SET.
5203 *
5204 * @param[in,out] set Set to use.
5205 * @param[in] options Xpath options.
5206 */
5207static void
5208moveto_root(struct lyxp_set *set, int options)
5209{
Michal Vasko03ff5a72019-09-11 13:49:33 +02005210 if (!set) {
5211 return;
5212 }
5213
5214 if (options & LYXP_SCNODE_ALL) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005215 set_scnode_clear_ctx(set);
Michal Vaskoecd62de2019-11-13 12:35:11 +01005216 lyxp_set_scnode_insert_node(set, NULL, set->root_type);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005217 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005218 set->type = LYXP_SET_NODE_SET;
5219 set->used = 0;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005220 set_insert_node(set, NULL, 0, set->root_type, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005221 }
5222}
5223
5224/**
Michal Vaskoa1424542019-11-14 16:08:52 +01005225 * @brief Check whether a node has some unresolved "when".
5226 *
5227 * @param[in] node Node to check.
5228 * @return LY_ERR value (LY_EINCOMPLETE if there are some unresolved "when")
5229 */
5230static LY_ERR
5231moveto_when_check(const struct lyd_node *node)
5232{
5233 const struct lysc_node *schema;
5234
5235 if (!node) {
5236 return LY_SUCCESS;
5237 }
5238
5239 schema = node->schema;
5240 do {
5241 if (schema->when && !(node->flags & LYD_WHEN_TRUE)) {
5242 return LY_EINCOMPLETE;
5243 }
5244 schema = schema->parent;
5245 } while (schema && (schema->nodetype & (LYS_CASE | LYS_CHOICE)));
5246
5247 return LY_SUCCESS;
5248}
5249
5250/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02005251 * @brief Check @p node as a part of NameTest processing.
5252 *
5253 * @param[in] node Node to check.
5254 * @param[in] root_type XPath root node type.
Michal Vaskod3678892020-05-21 10:06:58 +02005255 * @param[in] node_name Node name in the dictionary to move to, NULL for any node.
5256 * @param[in] moveto_mod Expected module of the node, NULL for any.
Michal Vasko6346ece2019-09-24 13:12:53 +02005257 * @return LY_ERR (LY_ENOT if node does not match, LY_EINCOMPLETE on unresolved when,
5258 * LY_EINVAL if netither node nor any children match)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005259 */
5260static LY_ERR
5261moveto_node_check(const struct lyd_node *node, enum lyxp_node_type root_type, const char *node_name,
5262 const struct lys_module *moveto_mod)
5263{
5264 /* module check */
5265 if (moveto_mod && (node->schema->module != moveto_mod)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005266 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005267 }
5268
Michal Vasko5c4e5892019-11-14 12:31:38 +01005269 /* context check */
5270 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005271 return LY_EINVAL;
5272 }
5273
5274 /* name check */
Michal Vasko61ac2f62020-05-25 12:39:51 +02005275 if (node_name && strcmp(node_name, "*") && (node->schema->name != node_name)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005276 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005277 }
5278
Michal Vaskoa1424542019-11-14 16:08:52 +01005279 /* when check */
5280 if (moveto_when_check(node)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005281 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01005282 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005283
5284 /* match */
5285 return LY_SUCCESS;
5286}
5287
5288/**
5289 * @brief Check @p node as a part of schema NameTest processing.
5290 *
5291 * @param[in] node Schema node to check.
5292 * @param[in] root_type XPath root node type.
Michal Vaskod3678892020-05-21 10:06:58 +02005293 * @param[in] node_name Node name in the dictionary to move to, NULL for any nodes.
5294 * @param[in] moveto_mod Expected module of the node, NULL for any.
Michal Vasko6346ece2019-09-24 13:12:53 +02005295 * @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 +02005296 */
5297static LY_ERR
5298moveto_scnode_check(const struct lysc_node *node, enum lyxp_node_type root_type, const char *node_name,
Michal Vaskocafad9d2019-11-07 15:20:03 +01005299 const struct lys_module *moveto_mod)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005300{
Michal Vasko03ff5a72019-09-11 13:49:33 +02005301 /* module check */
Michal Vaskod3678892020-05-21 10:06:58 +02005302 if (moveto_mod && (node->module != moveto_mod)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005303 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005304 }
5305
5306 /* context check */
5307 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (node->flags & LYS_CONFIG_R)) {
5308 return LY_EINVAL;
5309 }
5310
5311 /* name check */
Michal Vasko61ac2f62020-05-25 12:39:51 +02005312 if (node_name && strcmp(node_name, "*") && (node->name != node_name)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005313 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005314 }
5315
5316 /* match */
5317 return LY_SUCCESS;
5318}
5319
5320/**
Michal Vaskod3678892020-05-21 10:06:58 +02005321 * @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 +02005322 *
5323 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005324 * @param[in] mod Matching node module, NULL for any.
5325 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005326 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5327 */
5328static LY_ERR
Michal Vaskod3678892020-05-21 10:06:58 +02005329moveto_node(struct lyxp_set *set, const struct lys_module *mod, const char *ncname)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005330{
Michal Vaskof03ed032020-03-04 13:31:44 +01005331 uint32_t i;
Michal Vasko6346ece2019-09-24 13:12:53 +02005332 int replaced;
Michal Vaskod3678892020-05-21 10:06:58 +02005333 const struct lyd_node *siblings, *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005334 LY_ERR rc;
5335
Michal Vaskod3678892020-05-21 10:06:58 +02005336 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005337 return LY_SUCCESS;
5338 }
5339
5340 if (set->type != LYXP_SET_NODE_SET) {
5341 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
5342 return LY_EVALID;
5343 }
5344
Michal Vasko03ff5a72019-09-11 13:49:33 +02005345 for (i = 0; i < set->used; ) {
5346 replaced = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02005347 siblings = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005348
5349 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 +01005350 assert(!set->val.nodes[i].node);
Michal Vaskod3678892020-05-21 10:06:58 +02005351
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005352 /* search in all the trees */
Michal Vaskod3678892020-05-21 10:06:58 +02005353 siblings = set->tree;
Michal Vasko5c4e5892019-11-14 12:31:38 +01005354 } else if (!(set->val.nodes[i].node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA))) {
Michal Vaskod3678892020-05-21 10:06:58 +02005355 /* search in children */
5356 siblings = lyd_node_children(set->val.nodes[i].node);
5357 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005358
Michal Vaskod3678892020-05-21 10:06:58 +02005359 for (sub = siblings; sub; sub = sub->next) {
5360 rc = moveto_node_check(sub, set->root_type, ncname, mod);
5361 if (rc == LY_SUCCESS) {
5362 if (!replaced) {
5363 set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i);
5364 replaced = 1;
5365 } else {
5366 set_insert_node(set, sub, 0, LYXP_NODE_ELEM, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005367 }
Michal Vaskod3678892020-05-21 10:06:58 +02005368 ++i;
5369 } else if (rc == LY_EINCOMPLETE) {
5370 return rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005371 }
5372 }
5373
5374 if (!replaced) {
5375 /* no match */
5376 set_remove_node(set, i);
5377 }
5378 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005379
5380 return LY_SUCCESS;
5381}
5382
5383/**
Michal Vaskod3678892020-05-21 10:06:58 +02005384 * @brief Move context @p set to a node using hashes. Result is LYXP_SET_NODE_SET (or LYXP_SET_EMPTY).
5385 * Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005386 *
5387 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005388 * @param[in] scnode Matching node schema.
5389 * @param[in] list_inst If @p scnode is ::LYS_LIST, the matching list instance. Ignored otherwise.
5390 * @param[in] llist_val If @p scnode is ::LYS_LEAFLIST, the matching leaf-list value. Ignored otherwise.
5391 * @param[in] llist_val_len Length of @p llist_val.
5392 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5393 */
5394static LY_ERR
5395moveto_node_hash(struct lyxp_set *set, const struct lysc_node *scnode, const struct lyd_node *list_inst,
5396 const char *llist_val, uint16_t llist_val_len)
5397{
5398 uint32_t i;
5399 int replaced;
5400 const struct lyd_node *siblings;
5401 struct lyd_node *sub;
5402
5403 assert(scnode && ((scnode->nodetype != LYS_LIST) || list_inst) && ((scnode->nodetype != LYS_LEAFLIST) || llist_val));
5404
5405 if (!set) {
5406 return LY_SUCCESS;
5407 }
5408
5409 if (set->type != LYXP_SET_NODE_SET) {
5410 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
5411 return LY_EVALID;
5412 }
5413
5414 /* context check for all the nodes since we have the schema node */
5415 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) {
5416 lyxp_set_free_content(set);
5417 return LY_SUCCESS;
5418 }
5419
5420 for (i = 0; i < set->used; ) {
5421 replaced = 0;
5422 siblings = NULL;
5423
5424 if ((set->val.nodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.nodes[i].type == LYXP_NODE_ROOT)) {
5425 assert(!set->val.nodes[i].node);
5426
5427 /* search in all the trees */
5428 siblings = set->tree;
5429 } else if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
5430 /* search in children */
5431 siblings = lyd_node_children(set->val.nodes[i].node);
5432 }
5433
5434 /* find the node using hashes */
5435 if (scnode->nodetype == LYS_LIST) {
5436 lyd_find_sibling_first(siblings, list_inst, &sub);
5437 } else {
5438 lyd_find_sibling_val(siblings, scnode, llist_val, llist_val_len, &sub);
5439 }
5440
5441 /* when check */
5442 if (sub && moveto_when_check(sub)) {
5443 return LY_EINCOMPLETE;
5444 }
5445
5446 if (sub) {
5447 /* pos filled later */
5448 if (!replaced) {
5449 set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i);
5450 replaced = 1;
5451 } else {
5452 set_insert_node(set, sub, 0, LYXP_NODE_ELEM, i);
5453 }
5454 ++i;
5455 }
5456
5457 if (!replaced) {
5458 /* no match */
5459 set_remove_node(set, i);
5460 }
5461 }
5462
5463 return LY_SUCCESS;
5464}
5465
5466/**
5467 * @brief Move context @p set to a schema node. Result is LYXP_SET_SCNODE_SET (or LYXP_SET_EMPTY).
5468 *
5469 * @param[in,out] set Set to use.
5470 * @param[in] mod Matching node module, NULL for any.
5471 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005472 * @param[in] options XPath options.
5473 * @return LY_ERR
5474 */
5475static LY_ERR
Michal Vaskod3678892020-05-21 10:06:58 +02005476moveto_scnode(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, int options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005477{
Michal Vaskocafad9d2019-11-07 15:20:03 +01005478 int i, orig_used, idx, temp_ctx = 0, getnext_opts;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005479 uint32_t mod_idx;
Michal Vasko519fd602020-05-26 12:17:39 +02005480 const struct lysc_node *iter, *start_parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005481
Michal Vaskod3678892020-05-21 10:06:58 +02005482 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005483 return LY_SUCCESS;
5484 }
5485
5486 if (set->type != LYXP_SET_SCNODE_SET) {
Michal Vaskof6e51882019-12-16 09:59:45 +01005487 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 +02005488 return LY_EVALID;
5489 }
5490
Michal Vaskocafad9d2019-11-07 15:20:03 +01005491 /* getnext opts */
5492 getnext_opts = LYS_GETNEXT_NOSTATECHECK;
5493 if (options & LYXP_SCNODE_OUTPUT) {
5494 getnext_opts |= LYS_GETNEXT_OUTPUT;
5495 }
5496
Michal Vasko03ff5a72019-09-11 13:49:33 +02005497 orig_used = set->used;
5498 for (i = 0; i < orig_used; ++i) {
5499 if (set->val.scnodes[i].in_ctx != 1) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01005500 if (set->val.scnodes[i].in_ctx != -2) {
5501 continue;
5502 }
5503
5504 /* remember context node */
5505 set->val.scnodes[i].in_ctx = -1;
Michal Vaskoec4df482019-12-16 10:02:18 +01005506 } else {
5507 set->val.scnodes[i].in_ctx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005508 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005509
5510 start_parent = set->val.scnodes[i].scnode;
5511
5512 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 +02005513 /* 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 +02005514 * so use it directly (root node itself is useless in this case) */
5515 mod_idx = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02005516 while (mod || (mod = (struct lys_module *)ly_ctx_get_module_iter(set->ctx, &mod_idx))) {
Michal Vasko519fd602020-05-26 12:17:39 +02005517 iter = NULL;
Michal Vasko509de4d2019-12-10 14:51:30 +01005518 /* module may not be implemented */
Michal Vasko519fd602020-05-26 12:17:39 +02005519 while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) {
5520 if (!moveto_scnode_check(iter, set->root_type, ncname, mod)) {
5521 idx = lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005522 /* we need to prevent these nodes from being considered in this moveto */
5523 if ((idx < orig_used) && (idx > i)) {
5524 set->val.scnodes[idx].in_ctx = 2;
5525 temp_ctx = 1;
5526 }
5527 }
5528 }
5529
5530 if (!mod_idx) {
Michal Vaskod3678892020-05-21 10:06:58 +02005531 /* mod was specified, we are not going through the whole context */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005532 break;
5533 }
5534 /* next iteration */
Michal Vaskod3678892020-05-21 10:06:58 +02005535 mod = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005536 }
5537
Michal Vasko519fd602020-05-26 12:17:39 +02005538 } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
5539 iter = NULL;
5540 while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) {
5541 if (!moveto_scnode_check(iter, set->root_type, ncname, (mod ? mod : set->local_mod))) {
5542 idx = lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005543 if ((idx < orig_used) && (idx > i)) {
5544 set->val.scnodes[idx].in_ctx = 2;
5545 temp_ctx = 1;
5546 }
5547 }
5548 }
5549 }
5550 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005551
5552 /* correct temporary in_ctx values */
5553 if (temp_ctx) {
5554 for (i = 0; i < orig_used; ++i) {
5555 if (set->val.scnodes[i].in_ctx == 2) {
5556 set->val.scnodes[i].in_ctx = 1;
5557 }
5558 }
5559 }
5560
5561 return LY_SUCCESS;
5562}
5563
5564/**
Michal Vaskod3678892020-05-21 10:06:58 +02005565 * @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 +02005566 * Context position aware.
5567 *
5568 * @param[in] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005569 * @param[in] mod Matching node module, NULL for any.
5570 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005571 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5572 */
5573static LY_ERR
Michal Vaskod3678892020-05-21 10:06:58 +02005574moveto_node_alldesc(struct lyxp_set *set, const struct lys_module *mod, const char *ncname)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005575{
5576 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005577 const struct lyd_node *next, *elem, *start;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005578 struct lyxp_set ret_set;
5579 LY_ERR rc;
5580
Michal Vaskod3678892020-05-21 10:06:58 +02005581 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005582 return LY_SUCCESS;
5583 }
5584
5585 if (set->type != LYXP_SET_NODE_SET) {
5586 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
5587 return LY_EVALID;
5588 }
5589
Michal Vasko9f96a052020-03-10 09:41:45 +01005590 /* 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 +02005591 rc = moveto_node(set, NULL, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005592 LY_CHECK_RET(rc);
5593
Michal Vasko6346ece2019-09-24 13:12:53 +02005594 /* this loop traverses all the nodes in the set and adds/keeps only those that match qname */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005595 set_init(&ret_set, set);
5596 for (i = 0; i < set->used; ++i) {
5597
5598 /* TREE DFS */
5599 start = set->val.nodes[i].node;
5600 for (elem = next = start; elem; elem = next) {
Michal Vaskod3678892020-05-21 10:06:58 +02005601 rc = moveto_node_check(elem, set->root_type, ncname, mod);
Michal Vasko6346ece2019-09-24 13:12:53 +02005602 if (!rc) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005603 /* add matching node into result set */
5604 set_insert_node(&ret_set, elem, 0, LYXP_NODE_ELEM, ret_set.used);
5605 if (set_dup_node_check(set, elem, LYXP_NODE_ELEM, i)) {
5606 /* the node is a duplicate, we'll process it later in the set */
5607 goto skip_children;
5608 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005609 } else if (rc == LY_EINCOMPLETE) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005610 return rc;
5611 } else if (rc == LY_EINVAL) {
5612 goto skip_children;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005613 }
5614
5615 /* TREE DFS NEXT ELEM */
5616 /* select element for the next run - children first */
Michal Vaskod3678892020-05-21 10:06:58 +02005617 next = lyd_node_children(elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005618 if (!next) {
5619skip_children:
5620 /* no children, so try siblings, but only if it's not the start,
5621 * that is considered to be the root and it's siblings are not traversed */
5622 if (elem != start) {
5623 next = elem->next;
5624 } else {
5625 break;
5626 }
5627 }
5628 while (!next) {
5629 /* no siblings, go back through the parents */
5630 if ((struct lyd_node *)elem->parent == start) {
5631 /* we are done, no next element to process */
5632 break;
5633 }
5634 /* parent is already processed, go to its sibling */
5635 elem = (struct lyd_node *)elem->parent;
5636 next = elem->next;
5637 }
5638 }
5639 }
5640
5641 /* make the temporary set the current one */
5642 ret_set.ctx_pos = set->ctx_pos;
5643 ret_set.ctx_size = set->ctx_size;
Michal Vaskod3678892020-05-21 10:06:58 +02005644 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005645 memcpy(set, &ret_set, sizeof *set);
5646
5647 return LY_SUCCESS;
5648}
5649
5650/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005651 * @brief Move context @p set to a schema node and all its descendants. Result is LYXP_SET_NODE_SET.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005652 *
5653 * @param[in] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005654 * @param[in] mod Matching node module, NULL for any.
5655 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005656 * @param[in] options XPath options.
5657 * @return LY_ERR
5658 */
5659static LY_ERR
Michal Vaskod3678892020-05-21 10:06:58 +02005660moveto_scnode_alldesc(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, int options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005661{
Michal Vasko6346ece2019-09-24 13:12:53 +02005662 int i, orig_used, idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005663 const struct lysc_node *next, *elem, *start;
Michal Vasko6346ece2019-09-24 13:12:53 +02005664 LY_ERR rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005665
Michal Vaskod3678892020-05-21 10:06:58 +02005666 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005667 return LY_SUCCESS;
5668 }
5669
5670 if (set->type != LYXP_SET_SCNODE_SET) {
Michal Vaskof6e51882019-12-16 09:59:45 +01005671 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 +02005672 return LY_EVALID;
5673 }
5674
Michal Vasko03ff5a72019-09-11 13:49:33 +02005675 orig_used = set->used;
5676 for (i = 0; i < orig_used; ++i) {
5677 if (set->val.scnodes[i].in_ctx != 1) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01005678 if (set->val.scnodes[i].in_ctx != -2) {
5679 continue;
5680 }
5681
5682 /* remember context node */
5683 set->val.scnodes[i].in_ctx = -1;
Michal Vaskoec4df482019-12-16 10:02:18 +01005684 } else {
5685 set->val.scnodes[i].in_ctx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005686 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005687
5688 /* TREE DFS */
5689 start = set->val.scnodes[i].scnode;
5690 for (elem = next = start; elem; elem = next) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005691 if ((elem == start) || (elem->nodetype & (LYS_CHOICE | LYS_CASE))) {
5692 /* schema-only nodes, skip root */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005693 goto next_iter;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005694 }
5695
Michal Vaskod3678892020-05-21 10:06:58 +02005696 rc = moveto_scnode_check(elem, set->root_type, ncname, mod);
Michal Vasko6346ece2019-09-24 13:12:53 +02005697 if (!rc) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01005698 if ((idx = lyxp_set_scnode_dup_node_check(set, elem, LYXP_NODE_ELEM, i)) > -1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005699 set->val.scnodes[idx].in_ctx = 1;
5700 if (idx > i) {
5701 /* we will process it later in the set */
5702 goto skip_children;
5703 }
5704 } else {
Michal Vaskoecd62de2019-11-13 12:35:11 +01005705 lyxp_set_scnode_insert_node(set, elem, LYXP_NODE_ELEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005706 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005707 } else if (rc == LY_EINVAL) {
5708 goto skip_children;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005709 }
5710
5711next_iter:
5712 /* TREE DFS NEXT ELEM */
5713 /* select element for the next run - children first */
5714 next = lysc_node_children(elem, options & LYXP_SCNODE_OUTPUT ? LYS_CONFIG_R : LYS_CONFIG_W);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005715 if (!next) {
5716skip_children:
5717 /* no children, so try siblings, but only if it's not the start,
5718 * that is considered to be the root and it's siblings are not traversed */
5719 if (elem != start) {
5720 next = elem->next;
5721 } else {
5722 break;
5723 }
5724 }
5725 while (!next) {
5726 /* no siblings, go back through the parents */
5727 if (elem->parent == start) {
5728 /* we are done, no next element to process */
5729 break;
5730 }
5731 /* parent is already processed, go to its sibling */
5732 elem = elem->parent;
5733 next = elem->next;
5734 }
5735 }
5736 }
5737
5738 return LY_SUCCESS;
5739}
5740
5741/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005742 * @brief Move context @p set to an attribute. Result is LYXP_SET_NODE_SET.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005743 * Indirectly context position aware.
5744 *
5745 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005746 * @param[in] mod Matching metadata module, NULL for any.
5747 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005748 * @return LY_ERR
5749 */
5750static LY_ERR
Michal Vaskod3678892020-05-21 10:06:58 +02005751moveto_attr(struct lyxp_set *set, const struct lys_module *mod, const char *ncname)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005752{
5753 uint32_t i;
Michal Vaskod3678892020-05-21 10:06:58 +02005754 int replaced;
Michal Vasko9f96a052020-03-10 09:41:45 +01005755 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005756
Michal Vaskod3678892020-05-21 10:06:58 +02005757 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005758 return LY_SUCCESS;
5759 }
5760
5761 if (set->type != LYXP_SET_NODE_SET) {
5762 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
5763 return LY_EVALID;
5764 }
5765
Michal Vasko03ff5a72019-09-11 13:49:33 +02005766 for (i = 0; i < set->used; ) {
5767 replaced = 0;
5768
5769 /* only attributes of an elem (not dummy) can be in the result, skip all the rest;
5770 * our attributes are always qualified */
Michal Vasko5c4e5892019-11-14 12:31:38 +01005771 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01005772 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005773
5774 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02005775 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005776 continue;
5777 }
5778
Michal Vaskod3678892020-05-21 10:06:58 +02005779 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005780 /* match */
5781 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01005782 set->val.meta[i].meta = sub;
5783 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005784 /* pos does not change */
5785 replaced = 1;
5786 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01005787 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 +02005788 }
5789 ++i;
5790 }
5791 }
5792 }
5793
5794 if (!replaced) {
5795 /* no match */
5796 set_remove_node(set, i);
5797 }
5798 }
5799
5800 return LY_SUCCESS;
5801}
5802
5803/**
5804 * @brief Move context @p set1 to union with @p set2. @p set2 is emptied afterwards.
Michal Vasko61ac2f62020-05-25 12:39:51 +02005805 * Result is LYXP_SET_NODE_SET. Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005806 *
5807 * @param[in,out] set1 Set to use for the result.
5808 * @param[in] set2 Set that is copied to @p set1.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005809 * @return LY_ERR
5810 */
5811static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005812moveto_union(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005813{
5814 LY_ERR rc;
5815
Michal Vaskod3678892020-05-21 10:06:58 +02005816 if ((set1->type != LYXP_SET_NODE_SET) || (set2->type != LYXP_SET_NODE_SET)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005817 LOGVAL(set1->ctx, LY_VLOG_LYD, set1->ctx_node, LY_VCODE_XP_INOP_2, "union", print_set_type(set1), print_set_type(set2));
5818 return LY_EVALID;
5819 }
5820
5821 /* set2 is empty or both set1 and set2 */
Michal Vaskod3678892020-05-21 10:06:58 +02005822 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005823 return LY_SUCCESS;
5824 }
5825
Michal Vaskod3678892020-05-21 10:06:58 +02005826 if (!set1->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005827 memcpy(set1, set2, sizeof *set1);
5828 /* dynamic memory belongs to set1 now, do not free */
Michal Vaskod3678892020-05-21 10:06:58 +02005829 memset(set2, 0, sizeof *set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005830 return LY_SUCCESS;
5831 }
5832
5833 /* we assume sets are sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005834 assert(!set_sort(set1) && !set_sort(set2));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005835
5836 /* sort, remove duplicates */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005837 rc = set_sorted_merge(set1, set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005838 LY_CHECK_RET(rc);
5839
5840 /* final set must be sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005841 assert(!set_sort(set1));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005842
5843 return LY_SUCCESS;
5844}
5845
5846/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005847 * @brief Move context @p set to an attribute in any of the descendants. Result is LYXP_SET_NODE_SET.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005848 * Context position aware.
5849 *
5850 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005851 * @param[in] mod Matching metadata module, NULL for any.
5852 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005853 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5854 */
5855static int
Michal Vaskod3678892020-05-21 10:06:58 +02005856moveto_attr_alldesc(struct lyxp_set *set, const struct lys_module *mod, const char *ncname)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005857{
5858 uint32_t i;
Michal Vaskod3678892020-05-21 10:06:58 +02005859 int replaced;
Michal Vasko9f96a052020-03-10 09:41:45 +01005860 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005861 struct lyxp_set *set_all_desc = NULL;
5862 LY_ERR rc;
5863
Michal Vaskod3678892020-05-21 10:06:58 +02005864 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005865 return LY_SUCCESS;
5866 }
5867
5868 if (set->type != LYXP_SET_NODE_SET) {
5869 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
5870 return LY_EVALID;
5871 }
5872
Michal Vasko03ff5a72019-09-11 13:49:33 +02005873 /* can be optimized similarly to moveto_node_alldesc() and save considerable amount of memory,
5874 * but it likely won't be used much, so it's a waste of time */
5875 /* copy the context */
5876 set_all_desc = set_copy(set);
5877 /* get all descendant nodes (the original context nodes are removed) */
Michal Vaskod3678892020-05-21 10:06:58 +02005878 rc = moveto_node_alldesc(set_all_desc, NULL, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005879 if (rc != LY_SUCCESS) {
5880 lyxp_set_free(set_all_desc);
5881 return rc;
5882 }
5883 /* prepend the original context nodes */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005884 rc = moveto_union(set, set_all_desc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005885 if (rc != LY_SUCCESS) {
5886 lyxp_set_free(set_all_desc);
5887 return rc;
5888 }
5889 lyxp_set_free(set_all_desc);
5890
Michal Vasko03ff5a72019-09-11 13:49:33 +02005891 for (i = 0; i < set->used; ) {
5892 replaced = 0;
5893
5894 /* only attributes of an elem can be in the result, skip all the rest,
5895 * we have all attributes qualified in lyd tree */
5896 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01005897 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005898 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02005899 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005900 continue;
5901 }
5902
Michal Vaskod3678892020-05-21 10:06:58 +02005903 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005904 /* match */
5905 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01005906 set->val.meta[i].meta = sub;
5907 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005908 /* pos does not change */
5909 replaced = 1;
5910 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01005911 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 +02005912 }
5913 ++i;
5914 }
5915 }
5916 }
5917
5918 if (!replaced) {
5919 /* no match */
5920 set_remove_node(set, i);
5921 }
5922 }
5923
5924 return LY_SUCCESS;
5925}
5926
5927/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005928 * @brief Move context @p set to self and al chilren, recursively. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET.
5929 * Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005930 *
5931 * @param[in] parent Current parent.
5932 * @param[in] parent_pos Position of @p parent.
5933 * @param[in] parent_type Node type of @p parent.
5934 * @param[in,out] to_set Set to use.
5935 * @param[in] dup_check_set Set for checking duplicities.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005936 * @param[in] options XPath options.
5937 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5938 */
5939static LY_ERR
5940moveto_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 +01005941 struct lyxp_set *to_set, const struct lyxp_set *dup_check_set, int options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005942{
Michal Vasko61ac2f62020-05-25 12:39:51 +02005943 const struct lyd_node *iter, *first;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005944 LY_ERR rc;
5945
5946 switch (parent_type) {
5947 case LYXP_NODE_ROOT:
5948 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko61ac2f62020-05-25 12:39:51 +02005949 assert(!parent);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005950
Michal Vasko61ac2f62020-05-25 12:39:51 +02005951 /* add all top-level nodes as elements */
5952 first = to_set->tree;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005953 break;
5954 case LYXP_NODE_ELEM:
Michal Vasko61ac2f62020-05-25 12:39:51 +02005955 /* add just the text node of this term element node */
5956 if (parent->schema->nodetype & (LYD_NODE_TERM | LYD_NODE_ANY)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005957 if (!set_dup_node_check(dup_check_set, parent, LYXP_NODE_TEXT, -1)) {
5958 set_insert_node(to_set, parent, parent_pos, LYXP_NODE_TEXT, to_set->used);
5959 }
Michal Vasko61ac2f62020-05-25 12:39:51 +02005960 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005961 }
Michal Vasko61ac2f62020-05-25 12:39:51 +02005962
5963 /* add all the children of this node */
5964 first = lyd_node_children(parent);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005965 break;
5966 default:
5967 LOGINT_RET(parent->schema->module->ctx);
5968 }
5969
Michal Vasko61ac2f62020-05-25 12:39:51 +02005970 /* add all top-level nodes as elements */
5971 LY_LIST_FOR(first, iter) {
5972 /* context check */
5973 if ((parent_type == LYXP_NODE_ROOT_CONFIG) && (iter->schema->flags & LYS_CONFIG_R)) {
5974 continue;
5975 }
5976
5977 /* when check */
5978 if (moveto_when_check(iter)) {
5979 return LY_EINCOMPLETE;
5980 }
5981
5982 if (!set_dup_node_check(dup_check_set, iter, LYXP_NODE_ELEM, -1)) {
5983 set_insert_node(to_set, iter, 0, LYXP_NODE_ELEM, to_set->used);
5984
5985 /* also add all the children of this node, recursively */
5986 rc = moveto_self_add_children_r(iter, 0, LYXP_NODE_ELEM, to_set, dup_check_set, options);
5987 LY_CHECK_RET(rc);
5988 }
5989 }
5990
Michal Vasko03ff5a72019-09-11 13:49:33 +02005991 return LY_SUCCESS;
5992}
5993
5994/**
5995 * @brief Move context @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET
5996 * (or LYXP_SET_EMPTY). Context position aware.
5997 *
5998 * @param[in,out] set Set to use.
5999 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6000 * @param[in] options XPath options.
6001 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6002 */
6003static LY_ERR
6004moveto_self(struct lyxp_set *set, int all_desc, int options)
6005{
6006 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006007 struct lyxp_set ret_set;
6008 LY_ERR rc;
6009
Michal Vaskod3678892020-05-21 10:06:58 +02006010 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006011 return LY_SUCCESS;
6012 }
6013
6014 if (set->type != LYXP_SET_NODE_SET) {
6015 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
6016 return LY_EVALID;
6017 }
6018
6019 /* nothing to do */
6020 if (!all_desc) {
6021 return LY_SUCCESS;
6022 }
6023
Michal Vasko03ff5a72019-09-11 13:49:33 +02006024 /* add all the children, they get added recursively */
6025 set_init(&ret_set, set);
6026 for (i = 0; i < set->used; ++i) {
6027 /* copy the current node to tmp */
6028 set_insert_node(&ret_set, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, ret_set.used);
6029
6030 /* do not touch attributes and text nodes */
Michal Vasko9f96a052020-03-10 09:41:45 +01006031 if ((set->val.nodes[i].type == LYXP_NODE_TEXT) || (set->val.nodes[i].type == LYXP_NODE_META)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006032 continue;
6033 }
6034
Michal Vasko03ff5a72019-09-11 13:49:33 +02006035 /* add all the children */
6036 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 +01006037 set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006038 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006039 lyxp_set_free_content(&ret_set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006040 return rc;
6041 }
6042 }
6043
6044 /* use the temporary set as the current one */
6045 ret_set.ctx_pos = set->ctx_pos;
6046 ret_set.ctx_size = set->ctx_size;
Michal Vaskod3678892020-05-21 10:06:58 +02006047 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006048 memcpy(set, &ret_set, sizeof *set);
6049
6050 return LY_SUCCESS;
6051}
6052
6053/**
6054 * @brief Move context schema @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_SCNODE_SET
6055 * (or LYXP_SET_EMPTY).
6056 *
6057 * @param[in,out] set Set to use.
6058 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6059 * @param[in] options XPath options.
6060 * @return LY_ERR
6061 */
6062static LY_ERR
6063moveto_scnode_self(struct lyxp_set *set, int all_desc, int options)
6064{
Michal Vasko519fd602020-05-26 12:17:39 +02006065 int getnext_opts;
6066 uint32_t i, mod_idx;
6067 const struct lysc_node *iter, *start_parent;
6068 const struct lys_module *mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006069
Michal Vaskod3678892020-05-21 10:06:58 +02006070 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006071 return LY_SUCCESS;
6072 }
6073
6074 if (set->type != LYXP_SET_SCNODE_SET) {
Michal Vaskof6e51882019-12-16 09:59:45 +01006075 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 +02006076 return LY_EVALID;
6077 }
6078
6079 /* nothing to do */
6080 if (!all_desc) {
6081 return LY_SUCCESS;
6082 }
6083
Michal Vasko519fd602020-05-26 12:17:39 +02006084 /* getnext opts */
6085 getnext_opts = LYS_GETNEXT_NOSTATECHECK;
6086 if (options & LYXP_SCNODE_OUTPUT) {
6087 getnext_opts |= LYS_GETNEXT_OUTPUT;
6088 }
6089
6090 /* add all the children, recursively as they are being added into the same set */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006091 for (i = 0; i < set->used; ++i) {
6092 if (set->val.scnodes[i].in_ctx != 1) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006093 if (set->val.scnodes[i].in_ctx != -2) {
6094 continue;
6095 }
6096
Michal Vasko519fd602020-05-26 12:17:39 +02006097 /* remember context node */
6098 set->val.scnodes[i].in_ctx = -1;
6099 } else {
6100 set->val.scnodes[i].in_ctx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006101 }
6102
Michal Vasko519fd602020-05-26 12:17:39 +02006103 start_parent = set->val.scnodes[i].scnode;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006104
Michal Vasko519fd602020-05-26 12:17:39 +02006105 if ((set->val.scnodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.scnodes[i].type == LYXP_NODE_ROOT)) {
6106 /* it can actually be in any module, it's all <running> */
6107 mod_idx = 0;
6108 while ((mod = (struct lys_module *)ly_ctx_get_module_iter(set->ctx, &mod_idx))) {
6109 iter = NULL;
6110 /* module may not be implemented */
6111 while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) {
6112 /* context check */
6113 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) {
6114 continue;
6115 }
6116
6117 lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM);
6118 /* throw away the insert index, we want to consider that node again, recursively */
6119 }
6120 }
6121
6122 } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
6123 iter = NULL;
6124 while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006125 /* context check */
Michal Vasko519fd602020-05-26 12:17:39 +02006126 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006127 continue;
6128 }
6129
Michal Vasko519fd602020-05-26 12:17:39 +02006130 lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006131 }
6132 }
6133 }
6134
6135 return LY_SUCCESS;
6136}
6137
6138/**
6139 * @brief Move context @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_NODE_SET
6140 * (or LYXP_SET_EMPTY). Context position aware.
6141 *
6142 * @param[in] set Set to use.
6143 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6144 * @param[in] options XPath options.
6145 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6146 */
6147static LY_ERR
6148moveto_parent(struct lyxp_set *set, int all_desc, int options)
6149{
6150 LY_ERR rc;
6151 uint32_t i;
6152 struct lyd_node *node, *new_node;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006153 enum lyxp_node_type new_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006154
Michal Vaskod3678892020-05-21 10:06:58 +02006155 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006156 return LY_SUCCESS;
6157 }
6158
6159 if (set->type != LYXP_SET_NODE_SET) {
6160 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
6161 return LY_EVALID;
6162 }
6163
6164 if (all_desc) {
6165 /* <path>//.. == <path>//./.. */
6166 rc = moveto_self(set, 1, options);
6167 LY_CHECK_RET(rc);
6168 }
6169
Michal Vasko57eab132019-09-24 11:46:26 +02006170 for (i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006171 node = set->val.nodes[i].node;
6172
6173 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
6174 new_node = (struct lyd_node *)node->parent;
6175 } else if (set->val.nodes[i].type == LYXP_NODE_TEXT) {
6176 new_node = node;
Michal Vasko9f96a052020-03-10 09:41:45 +01006177 } else if (set->val.nodes[i].type == LYXP_NODE_META) {
6178 new_node = set->val.meta[i].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006179 if (!new_node) {
6180 LOGINT_RET(set->ctx);
6181 }
6182 } else {
6183 /* root does not have a parent */
Michal Vasko2caefc12019-11-14 16:07:56 +01006184 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006185 continue;
6186 }
6187
Michal Vaskoa1424542019-11-14 16:08:52 +01006188 /* when check */
6189 if (moveto_when_check(new_node)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006190 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01006191 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006192
6193 /* node already there can also be the root */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006194 if (!new_node) {
6195 new_type = set->root_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006196
6197 /* node has a standard parent (it can equal the root, it's not the root yet since they are fake) */
6198 } else {
6199 new_type = LYXP_NODE_ELEM;
6200 }
6201
Michal Vasko03ff5a72019-09-11 13:49:33 +02006202 if (set_dup_node_check(set, new_node, new_type, -1)) {
Michal Vasko2caefc12019-11-14 16:07:56 +01006203 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006204 } else {
6205 set_replace_node(set, new_node, 0, new_type, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006206 }
6207 }
6208
Michal Vasko2caefc12019-11-14 16:07:56 +01006209 set_remove_nodes_none(set);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006210 assert(!set_sort(set) && !set_sorted_dup_node_clean(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006211
6212 return LY_SUCCESS;
6213}
6214
6215/**
6216 * @brief Move context schema @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_SCNODE_SET
6217 * (or LYXP_SET_EMPTY).
6218 *
6219 * @param[in] set Set to use.
6220 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6221 * @param[in] options XPath options.
6222 * @return LY_ERR
6223 */
6224static LY_ERR
6225moveto_scnode_parent(struct lyxp_set *set, int all_desc, int options)
6226{
6227 int idx, i, orig_used, temp_ctx = 0;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006228 const struct lysc_node *node, *new_node;
6229 enum lyxp_node_type new_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006230 LY_ERR rc;
6231
Michal Vaskod3678892020-05-21 10:06:58 +02006232 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006233 return LY_SUCCESS;
6234 }
6235
6236 if (set->type != LYXP_SET_SCNODE_SET) {
Michal Vaskof6e51882019-12-16 09:59:45 +01006237 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 +02006238 return LY_EVALID;
6239 }
6240
6241 if (all_desc) {
6242 /* <path>//.. == <path>//./.. */
6243 rc = moveto_scnode_self(set, 1, options);
6244 LY_CHECK_RET(rc);
6245 }
6246
Michal Vasko03ff5a72019-09-11 13:49:33 +02006247 orig_used = set->used;
6248 for (i = 0; i < orig_used; ++i) {
6249 if (set->val.scnodes[i].in_ctx != 1) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006250 if (set->val.scnodes[i].in_ctx != -2) {
6251 continue;
6252 }
6253
6254 /* remember context node */
6255 set->val.scnodes[i].in_ctx = -1;
Michal Vaskoec4df482019-12-16 10:02:18 +01006256 } else {
6257 set->val.scnodes[i].in_ctx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006258 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006259
6260 node = set->val.scnodes[i].scnode;
6261
6262 if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
Michal Vaskod3678892020-05-21 10:06:58 +02006263 new_node = lysc_data_parent(node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006264 } else {
6265 /* root does not have a parent */
6266 continue;
6267 }
6268
Michal Vasko03ff5a72019-09-11 13:49:33 +02006269 /* node has no parent */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006270 if (!new_node) {
6271 new_type = set->root_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006272
6273 /* node has a standard parent (it can equal the root, it's not the root yet since they are fake) */
6274 } else {
6275 new_type = LYXP_NODE_ELEM;
6276 }
6277
Michal Vaskoecd62de2019-11-13 12:35:11 +01006278 idx = lyxp_set_scnode_insert_node(set, new_node, new_type);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006279 if ((idx < orig_used) && (idx > i)) {
6280 set->val.scnodes[idx].in_ctx = 2;
6281 temp_ctx = 1;
6282 }
6283 }
6284
6285 if (temp_ctx) {
6286 for (i = 0; i < orig_used; ++i) {
6287 if (set->val.scnodes[i].in_ctx == 2) {
6288 set->val.scnodes[i].in_ctx = 1;
6289 }
6290 }
6291 }
6292
6293 return LY_SUCCESS;
6294}
6295
6296/**
6297 * @brief Move context @p set to the result of a comparison. Handles '=', '!=', '<=', '<', '>=', or '>'.
6298 * Result is LYXP_SET_BOOLEAN. Indirectly context position aware.
6299 *
6300 * @param[in,out] set1 Set to use for the result.
6301 * @param[in] set2 Set acting as the second operand for @p op.
6302 * @param[in] op Comparison operator to process.
6303 * @param[in] options XPath options.
6304 * @return LY_ERR
6305 */
6306static LY_ERR
6307moveto_op_comp(struct lyxp_set *set1, struct lyxp_set *set2, const char *op, int options)
6308{
6309 /*
6310 * NODE SET + NODE SET = NODE SET + STRING /(1 NODE SET) 2 STRING
6311 * NODE SET + STRING = STRING + STRING /1 STRING (2 STRING)
6312 * NODE SET + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6313 * NODE SET + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6314 * STRING + NODE SET = STRING + STRING /(1 STRING) 2 STRING
6315 * NUMBER + NODE SET = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6316 * BOOLEAN + NODE SET = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6317 *
6318 * '=' or '!='
6319 * BOOLEAN + BOOLEAN
6320 * BOOLEAN + STRING = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6321 * BOOLEAN + NUMBER = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6322 * STRING + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6323 * NUMBER + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6324 * NUMBER + NUMBER
6325 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6326 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6327 * STRING + STRING
6328 *
6329 * '<=', '<', '>=', '>'
6330 * NUMBER + NUMBER
6331 * BOOLEAN + BOOLEAN = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6332 * BOOLEAN + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6333 * BOOLEAN + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6334 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6335 * STRING + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6336 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6337 * NUMBER + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6338 * STRING + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6339 */
6340 struct lyxp_set iter1, iter2;
6341 int result;
6342 int64_t i;
6343 LY_ERR rc;
6344
Michal Vaskod3678892020-05-21 10:06:58 +02006345 iter1.type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006346
6347 /* iterative evaluation with node-sets */
6348 if ((set1->type == LYXP_SET_NODE_SET) || (set2->type == LYXP_SET_NODE_SET)) {
6349 if (set1->type == LYXP_SET_NODE_SET) {
6350 for (i = 0; i < set1->used; ++i) {
6351 switch (set2->type) {
6352 case LYXP_SET_NUMBER:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006353 rc = set_comp_cast(&iter1, set1, LYXP_SET_NUMBER, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006354 break;
6355 case LYXP_SET_BOOLEAN:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006356 rc = set_comp_cast(&iter1, set1, LYXP_SET_BOOLEAN, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006357 break;
6358 default:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006359 rc = set_comp_cast(&iter1, set1, LYXP_SET_STRING, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006360 break;
6361 }
6362 LY_CHECK_RET(rc);
6363
6364 rc = moveto_op_comp(&iter1, set2, op, options);
6365 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006366 lyxp_set_free_content(&iter1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006367 return rc;
6368 }
6369
6370 /* lazy evaluation until true */
6371 if (iter1.val.bool) {
6372 set_fill_boolean(set1, 1);
6373 return LY_SUCCESS;
6374 }
6375 }
6376 } else {
6377 for (i = 0; i < set2->used; ++i) {
6378 switch (set1->type) {
6379 case LYXP_SET_NUMBER:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006380 rc = set_comp_cast(&iter2, set2, LYXP_SET_NUMBER, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006381 break;
6382 case LYXP_SET_BOOLEAN:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006383 rc = set_comp_cast(&iter2, set2, LYXP_SET_BOOLEAN, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006384 break;
6385 default:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006386 rc = set_comp_cast(&iter2, set2, LYXP_SET_STRING, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006387 break;
6388 }
6389 LY_CHECK_RET(rc);
6390
6391 set_fill_set(&iter1, set1);
6392
6393 rc = moveto_op_comp(&iter1, &iter2, op, options);
6394 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006395 lyxp_set_free_content(&iter1);
6396 lyxp_set_free_content(&iter2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006397 return rc;
6398 }
Michal Vaskod3678892020-05-21 10:06:58 +02006399 lyxp_set_free_content(&iter2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006400
6401 /* lazy evaluation until true */
6402 if (iter1.val.bool) {
6403 set_fill_boolean(set1, 1);
6404 return LY_SUCCESS;
6405 }
6406 }
6407 }
6408
6409 /* false for all nodes */
6410 set_fill_boolean(set1, 0);
6411 return LY_SUCCESS;
6412 }
6413
6414 /* first convert properly */
6415 if ((op[0] == '=') || (op[0] == '!')) {
6416 if ((set1->type == LYXP_SET_BOOLEAN) || (set2->type == LYXP_SET_BOOLEAN)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006417 lyxp_set_cast(set1, LYXP_SET_BOOLEAN);
6418 lyxp_set_cast(set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006419 } else if ((set1->type == LYXP_SET_NUMBER) || (set2->type == LYXP_SET_NUMBER)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006420 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006421 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006422 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006423 LY_CHECK_RET(rc);
6424 } /* else we have 2 strings */
6425 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006426 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006427 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006428 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006429 LY_CHECK_RET(rc);
6430 }
6431
6432 assert(set1->type == set2->type);
6433
6434 /* compute result */
6435 if (op[0] == '=') {
6436 if (set1->type == LYXP_SET_BOOLEAN) {
6437 result = (set1->val.bool == set2->val.bool);
6438 } else if (set1->type == LYXP_SET_NUMBER) {
6439 result = (set1->val.num == set2->val.num);
6440 } else {
6441 assert(set1->type == LYXP_SET_STRING);
Michal Vaskoac6c72f2019-11-14 16:09:34 +01006442 result = !strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006443 }
6444 } else if (op[0] == '!') {
6445 if (set1->type == LYXP_SET_BOOLEAN) {
6446 result = (set1->val.bool != set2->val.bool);
6447 } else if (set1->type == LYXP_SET_NUMBER) {
6448 result = (set1->val.num != set2->val.num);
6449 } else {
6450 assert(set1->type == LYXP_SET_STRING);
Michal Vaskoac6c72f2019-11-14 16:09:34 +01006451 result = !strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006452 }
6453 } else {
6454 assert(set1->type == LYXP_SET_NUMBER);
6455 if (op[0] == '<') {
6456 if (op[1] == '=') {
6457 result = (set1->val.num <= set2->val.num);
6458 } else {
6459 result = (set1->val.num < set2->val.num);
6460 }
6461 } else {
6462 if (op[1] == '=') {
6463 result = (set1->val.num >= set2->val.num);
6464 } else {
6465 result = (set1->val.num > set2->val.num);
6466 }
6467 }
6468 }
6469
6470 /* assign result */
6471 if (result) {
6472 set_fill_boolean(set1, 1);
6473 } else {
6474 set_fill_boolean(set1, 0);
6475 }
6476
6477 return LY_SUCCESS;
6478}
6479
6480/**
6481 * @brief Move context @p set to the result of a basic operation. Handles '+', '-', unary '-', '*', 'div',
6482 * or 'mod'. Result is LYXP_SET_NUMBER. Indirectly context position aware.
6483 *
6484 * @param[in,out] set1 Set to use for the result.
6485 * @param[in] set2 Set acting as the second operand for @p op.
6486 * @param[in] op Operator to process.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006487 * @return LY_ERR
6488 */
6489static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006490moveto_op_math(struct lyxp_set *set1, struct lyxp_set *set2, const char *op)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006491{
6492 LY_ERR rc;
6493
6494 /* unary '-' */
6495 if (!set2 && (op[0] == '-')) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006496 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006497 LY_CHECK_RET(rc);
6498 set1->val.num *= -1;
6499 lyxp_set_free(set2);
6500 return LY_SUCCESS;
6501 }
6502
6503 assert(set1 && set2);
6504
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006505 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006506 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006507 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006508 LY_CHECK_RET(rc);
6509
6510 switch (op[0]) {
6511 /* '+' */
6512 case '+':
6513 set1->val.num += set2->val.num;
6514 break;
6515
6516 /* '-' */
6517 case '-':
6518 set1->val.num -= set2->val.num;
6519 break;
6520
6521 /* '*' */
6522 case '*':
6523 set1->val.num *= set2->val.num;
6524 break;
6525
6526 /* 'div' */
6527 case 'd':
6528 set1->val.num /= set2->val.num;
6529 break;
6530
6531 /* 'mod' */
6532 case 'm':
6533 set1->val.num = ((long long)set1->val.num) % ((long long)set2->val.num);
6534 break;
6535
6536 default:
6537 LOGINT_RET(set1->ctx);
6538 }
6539
6540 return LY_SUCCESS;
6541}
6542
6543/*
6544 * eval functions
6545 *
6546 * They execute a parsed XPath expression on some data subtree.
6547 */
6548
6549/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02006550 * @brief Evaluate Predicate. Logs directly on error.
6551 *
Michal Vaskod3678892020-05-21 10:06:58 +02006552 * [9] Predicate ::= '[' Expr ']'
Michal Vasko03ff5a72019-09-11 13:49:33 +02006553 *
6554 * @param[in] exp Parsed XPath expression.
6555 * @param[in] exp_idx Position in the expression @p exp.
6556 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
6557 * @param[in] options XPath options.
6558 * @param[in] parent_pos_pred Whether parent predicate was a positional one.
6559 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6560 */
6561static LY_ERR
6562eval_predicate(struct lyxp_expr *exp, uint16_t *exp_idx, struct lyxp_set *set, int options, int parent_pos_pred)
6563{
6564 LY_ERR rc;
Michal Vasko57eab132019-09-24 11:46:26 +02006565 uint16_t i, orig_exp;
Michal Vasko5c4e5892019-11-14 12:31:38 +01006566 uint32_t orig_pos, orig_size;
6567 int32_t pred_in_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006568 struct lyxp_set set2;
6569 struct lyd_node *orig_parent;
6570
6571 /* '[' */
6572 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
6573 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
6574 ++(*exp_idx);
6575
6576 if (!set) {
6577only_parse:
6578 rc = eval_expr_select(exp, exp_idx, 0, NULL, options);
6579 LY_CHECK_RET(rc);
6580 } else if (set->type == LYXP_SET_NODE_SET) {
6581 /* 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 +01006582 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006583
6584 /* empty set, nothing to evaluate */
6585 if (!set->used) {
6586 goto only_parse;
6587 }
6588
6589 orig_exp = *exp_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006590 orig_pos = 0;
6591 orig_size = set->used;
6592 orig_parent = NULL;
Michal Vasko39dbf352020-05-21 10:08:59 +02006593 for (i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006594 set_init(&set2, set);
6595 set_insert_node(&set2, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, 0);
6596 /* remember the node context position for position() and context size for last(),
6597 * predicates should always be evaluated with respect to the child axis (since we do
6598 * not support explicit axes) so we assign positions based on their parents */
6599 if (parent_pos_pred && ((struct lyd_node *)set->val.nodes[i].node->parent != orig_parent)) {
6600 orig_parent = (struct lyd_node *)set->val.nodes[i].node->parent;
6601 orig_pos = 1;
6602 } else {
6603 ++orig_pos;
6604 }
6605
6606 set2.ctx_pos = orig_pos;
6607 set2.ctx_size = orig_size;
6608 *exp_idx = orig_exp;
6609
6610 rc = eval_expr_select(exp, exp_idx, 0, &set2, options);
6611 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006612 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006613 return rc;
6614 }
6615
6616 /* number is a position */
6617 if (set2.type == LYXP_SET_NUMBER) {
6618 if ((long long)set2.val.num == orig_pos) {
6619 set2.val.num = 1;
6620 } else {
6621 set2.val.num = 0;
6622 }
6623 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006624 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006625
6626 /* predicate satisfied or not? */
Michal Vasko57eab132019-09-24 11:46:26 +02006627 if (!set2.val.bool) {
Michal Vasko2caefc12019-11-14 16:07:56 +01006628 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006629 }
6630 }
Michal Vasko2caefc12019-11-14 16:07:56 +01006631 set_remove_nodes_none(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006632
6633 } else if (set->type == LYXP_SET_SCNODE_SET) {
6634 for (i = 0; i < set->used; ++i) {
6635 if (set->val.scnodes[i].in_ctx == 1) {
6636 /* there is a currently-valid node */
6637 break;
6638 }
6639 }
6640 /* empty set, nothing to evaluate */
6641 if (i == set->used) {
6642 goto only_parse;
6643 }
6644
6645 orig_exp = *exp_idx;
6646
Michal Vasko03ff5a72019-09-11 13:49:33 +02006647 /* set special in_ctx to all the valid snodes */
6648 pred_in_ctx = set_scnode_new_in_ctx(set);
6649
6650 /* use the valid snodes one-by-one */
6651 for (i = 0; i < set->used; ++i) {
6652 if (set->val.scnodes[i].in_ctx != pred_in_ctx) {
6653 continue;
6654 }
6655 set->val.scnodes[i].in_ctx = 1;
6656
6657 *exp_idx = orig_exp;
6658
6659 rc = eval_expr_select(exp, exp_idx, 0, set, options);
6660 LY_CHECK_RET(rc);
6661
6662 set->val.scnodes[i].in_ctx = pred_in_ctx;
6663 }
6664
6665 /* restore the state as it was before the predicate */
6666 for (i = 0; i < set->used; ++i) {
6667 if (set->val.scnodes[i].in_ctx == 1) {
6668 set->val.scnodes[i].in_ctx = 0;
6669 } else if (set->val.scnodes[i].in_ctx == pred_in_ctx) {
6670 set->val.scnodes[i].in_ctx = 1;
6671 }
6672 }
6673
6674 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02006675 set2.type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006676 set_fill_set(&set2, set);
6677
6678 rc = eval_expr_select(exp, exp_idx, 0, &set2, options);
6679 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006680 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006681 return rc;
6682 }
6683
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006684 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006685 if (!set2.val.bool) {
Michal Vaskod3678892020-05-21 10:06:58 +02006686 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006687 }
Michal Vaskod3678892020-05-21 10:06:58 +02006688 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006689 }
6690
6691 /* ']' */
6692 assert(exp->tokens[*exp_idx] == LYXP_TOKEN_BRACK2);
6693 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
6694 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
6695 ++(*exp_idx);
6696
6697 return LY_SUCCESS;
6698}
6699
6700/**
Michal Vaskod3678892020-05-21 10:06:58 +02006701 * @brief Evaluate Literal. Logs directly on error.
6702 *
6703 * @param[in] exp Parsed XPath expression.
6704 * @param[in] exp_idx Position in the expression @p exp.
6705 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
6706 */
6707static void
6708eval_literal(struct lyxp_expr *exp, uint16_t *exp_idx, struct lyxp_set *set)
6709{
6710 if (set) {
6711 if (exp->tok_len[*exp_idx] == 2) {
6712 set_fill_string(set, "", 0);
6713 } else {
6714 set_fill_string(set, &exp->expr[exp->tok_pos[*exp_idx] + 1], exp->tok_len[*exp_idx] - 2);
6715 }
6716 }
6717 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
6718 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
6719 ++(*exp_idx);
6720}
6721
6722/**
6723 * @brief Check and parse a predicate following a leaf-list to extract its value.
6724 * On success all the used tokens are skipped.
6725 *
6726 * @param[in] exp Parsed XPath expression.
6727 * @param[in] exp_idx Position in the expression @p exp.
6728 * @param[out] val Leaf-list value on success.
6729 * @param[out] val_len Length of @p val.
6730 * @return LY_ERR
6731 */
6732static LY_ERR
6733eval_name_test_parse_leaflist_predicate(struct lyxp_expr *exp, uint16_t *exp_idx, const char **val, uint16_t *val_len)
6734{
6735 uint16_t cur_idx;
6736
6737 cur_idx = *exp_idx;
6738
6739 /* '[' */
6740 if ((exp->used == cur_idx) || (exp->tokens[cur_idx] != LYXP_TOKEN_BRACK1)) {
6741 return LY_EINVAL;
6742 }
6743 ++cur_idx;
6744
6745 /* '.' */
6746 if ((exp->used == cur_idx) || (exp->tokens[cur_idx] != LYXP_TOKEN_DOT)) {
6747 return LY_EINVAL;
6748 }
6749 ++cur_idx;
6750
6751 /* '=' */
6752 if ((exp->used == cur_idx) || (exp->tokens[cur_idx] != LYXP_TOKEN_OPERATOR_COMP)
6753 || (exp->expr[exp->tok_pos[cur_idx]] != '=')) {
6754 return LY_EINVAL;
6755 }
6756 ++cur_idx;
6757
6758 /* value */
6759 if ((exp->used == cur_idx) || (exp->tokens[cur_idx] != LYXP_TOKEN_LITERAL)) {
6760 return LY_EINVAL;
6761 }
6762 ++cur_idx;
6763
6764 /* ']' */
6765 if ((exp->used == cur_idx) || (exp->tokens[cur_idx] != LYXP_TOKEN_BRACK2)) {
6766 return LY_EINVAL;
6767 }
6768 ++cur_idx;
6769
6770 /* success */
6771 *val = &exp->expr[exp->tok_pos[cur_idx - 2] + 1];
6772 *val_len = exp->tok_len[cur_idx - 2] - 2;
6773 *exp_idx = cur_idx;
6774 return LY_SUCCESS;
6775}
6776
6777/**
6778 * @brief Check and parse a predicate following a list to make sure all the tokens are as expected (all key values defined).
6779 * On success all the used tokens are skipped.
6780 *
6781 * @param[in] exp Parsed XPath expression.
6782 * @param[in] exp_idx Position in the expression @p exp.
6783 * @param[in] slist Schema node of the list.
6784 * @param[out] val Leaf-list value on success.
6785 * @param[out] val_len Length of @p val.
6786 * @return LY_ERR
6787 */
6788static LY_ERR
6789eval_name_test_parse_list_predicate(struct lyxp_expr *exp, uint16_t *exp_idx, const struct lysc_node *slist,
6790 const char **keys, uint16_t *keys_len)
6791{
6792 uint16_t key_count, i, cur_idx;
6793 const struct lysc_node *key;
6794
6795 if (slist->flags & LYS_KEYLESS) {
6796 /* invalid */
6797 return LY_EINVAL;
6798 }
6799
6800 /* get key count */
6801 key_count = 0;
6802 for (key = lysc_node_children(slist, 0); key && (key->flags & LYS_KEY); key = key->next) {
6803 ++key_count;
6804 }
6805
6806 /* briefly check the predicate for each key */
6807 cur_idx = *exp_idx;
6808 for (i = 0; i < key_count; ++i) {
6809 /* '[' */
6810 if ((exp->used == cur_idx) || (exp->tokens[cur_idx] != LYXP_TOKEN_BRACK1)) {
6811 return LY_EINVAL;
6812 }
6813 ++cur_idx;
6814
6815 /* key-name */
6816 if ((exp->used == cur_idx) || (exp->tokens[cur_idx] != LYXP_TOKEN_NAMETEST)) {
6817 return LY_EINVAL;
6818 }
6819 ++cur_idx;
6820
6821 /* '=' */
6822 if ((exp->used == cur_idx) || (exp->tokens[cur_idx] != LYXP_TOKEN_OPERATOR_COMP)
6823 || (exp->expr[exp->tok_pos[cur_idx]] != '=')) {
6824 return LY_EINVAL;
6825 }
6826 ++cur_idx;
6827
6828 /* key-value */
6829 if ((exp->used == cur_idx) || (exp->tokens[cur_idx] != LYXP_TOKEN_LITERAL)) {
6830 return LY_EINVAL;
6831 }
6832 ++cur_idx;
6833
6834 /* ']' */
6835 if ((exp->used == cur_idx) || (exp->tokens[cur_idx] != LYXP_TOKEN_BRACK2)) {
6836 return LY_EINVAL;
6837 }
6838 ++cur_idx;
6839 }
6840
6841 /* success */
6842 *keys = &exp->expr[exp->tok_pos[*exp_idx]];
6843 *keys_len = (exp->expr + exp->tok_pos[cur_idx - 1] + exp->tok_len[cur_idx - 1]) - *keys;
6844 *exp_idx = cur_idx;
6845 return LY_SUCCESS;
6846}
6847
6848/**
6849 * @brief Evaluate NameTest and any following Predicates. Logs directly on error.
6850 *
6851 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
6852 * [6] NodeTest ::= NameTest | NodeType '(' ')'
6853 * [7] NameTest ::= '*' | NCName ':' '*' | QName
6854 *
6855 * @param[in] exp Parsed XPath expression.
6856 * @param[in] exp_idx Position in the expression @p exp.
6857 * @param[in] attr_axis Whether to search attributes or standard nodes.
6858 * @param[in] all_desc Whether to search all the descendants or children only.
6859 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
6860 * @param[in] options XPath options.
6861 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6862 */
6863static LY_ERR
6864eval_name_test_with_predicate(struct lyxp_expr *exp, uint16_t *exp_idx, int attr_axis, int all_desc, struct lyxp_set *set,
6865 int options)
6866{
6867 int i;
6868 char *path;
6869 const char *ncname = NULL, *key_val = NULL;
6870 uint16_t ncname_len, key_val_len, prev_exp_idx;
6871 const struct lys_module *moveto_mod;
6872 const struct lysc_node *scnode = NULL, *tmp;
6873 struct lyd_node *list_inst = NULL;
6874 LY_ERR rc = LY_SUCCESS;
6875
6876 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
6877 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
6878 ++(*exp_idx);
6879
6880 if (!set) {
6881 goto moveto;
6882 }
6883
6884 ncname = &exp->expr[exp->tok_pos[*exp_idx - 1]];
6885 ncname_len = exp->tok_len[*exp_idx - 1];
6886
6887 /* parse (and skip) module name */
6888 rc = moveto_resolve_model(&ncname, &ncname_len, set, &moveto_mod);
6889 LY_CHECK_GOTO(rc, cleanup);
6890
6891 if (moveto_mod && !attr_axis && !all_desc && (set->type == LYXP_SET_NODE_SET)) {
6892 /* find the matching schema node in some parent in the context */
6893 for (i = 0; i < (signed)set->used; ++i) {
6894 if (set->val.nodes[i].type == set->root_type) {
6895 tmp = lys_find_child(NULL, moveto_mod, ncname, ncname_len, 0, 0);
6896 } else if ((set->val.nodes[i].type == LYXP_NODE_ELEM)
6897 && (!scnode || (lysc_data_parent(scnode) != set->val.nodes[i].node->schema))) {
6898 /* do not repeat the same search */
6899 tmp = lys_find_child(set->val.nodes[i].node->schema, moveto_mod, ncname, ncname_len, 0, 0);
6900 }
6901
6902 /* additional context check */
6903 if (tmp && (set->root_type == LYXP_NODE_ROOT_CONFIG) && (tmp->flags & LYS_CONFIG_R)) {
6904 tmp = NULL;
6905 }
6906
6907 if (tmp) {
6908 if (scnode) {
6909 /* we found a schema node with the same name but at different level, give up, too complicated */
6910 scnode = NULL;
6911 break;
6912 } else {
6913 /* remember the found schema node and continue to make sure it can be used */
6914 scnode = tmp;
6915 }
6916 tmp = NULL;
6917 }
6918 }
6919
6920 if (scnode && (scnode->nodetype == LYS_LIST)) {
6921 /* make sure all the tokens are right */
6922 prev_exp_idx = *exp_idx;
6923 if (eval_name_test_parse_list_predicate(exp, exp_idx, scnode, &key_val, &key_val_len)) {
6924 scnode = NULL;
6925 }
6926
6927 /* try to create a list instance */
6928 if (scnode && lyd_create_list(scnode, key_val, key_val_len, set->format, 0, &list_inst)) {
6929 /* for whatever reason the list failed to be created, just use standard name comparison and
6930 * parse predicate normally */
6931 *exp_idx = prev_exp_idx;
6932 scnode = NULL;
6933 }
6934 } else if (scnode && (scnode->nodetype == LYS_LEAFLIST)) {
6935 /* make sure we know the leaf-list value */
6936 if (eval_name_test_parse_leaflist_predicate(exp, exp_idx, &key_val, &key_val_len)) {
6937 /* value could not be recognized, use standard name comparison */
6938 scnode = NULL;
6939 }
6940 }
6941 }
6942
6943 if (!scnode) {
6944 /* we are not able to match based on a schema node */
6945 if (!moveto_mod) {
6946 /* '*', all nodes match */
6947 ncname = NULL;
6948 } else {
6949 /* insert name into dictionary for efficient comparison */
6950 ncname = lydict_insert(set->ctx, ncname, ncname_len);
6951 }
6952 }
6953
6954moveto:
6955 /* move to the attribute(s), data node(s), or schema node(s) */
6956 if (attr_axis) {
6957 if (set && (options & LYXP_SCNODE_ALL)) {
6958 set_scnode_clear_ctx(set);
6959 } else {
6960 if (all_desc) {
6961 rc = moveto_attr_alldesc(set, moveto_mod, ncname);
6962 } else {
6963 rc = moveto_attr(set, moveto_mod, ncname);
6964 }
6965 LY_CHECK_GOTO(rc, cleanup);
6966 }
6967 } else {
6968 if (set && (options & LYXP_SCNODE_ALL)) {
6969 if (all_desc) {
6970 rc = moveto_scnode_alldesc(set, moveto_mod, ncname, options);
6971 } else {
6972 rc = moveto_scnode(set, moveto_mod, ncname, options);
6973 }
6974 LY_CHECK_GOTO(rc, cleanup);
6975
6976 for (i = set->used - 1; i > -1; --i) {
6977 if (set->val.scnodes[i].in_ctx > 0) {
6978 break;
6979 }
6980 }
6981 if (i == -1) {
6982 path = lysc_path(set->ctx_scnode, LYSC_PATH_LOG, NULL, 0);
6983 LOGWRN(set->ctx, "Schema node \"%.*s\" not found (%.*s) with context node \"%s\".",
6984 exp->tok_len[*exp_idx], &exp->expr[exp->tok_pos[*exp_idx]],
6985 exp->tok_pos[*exp_idx] + exp->tok_len[*exp_idx], exp->expr, path);
6986 free(path);
6987 }
6988 } else {
6989 if (all_desc) {
6990 rc = moveto_node_alldesc(set, moveto_mod, ncname);
6991 } else {
6992 if (scnode) {
6993 /* we can find the nodes using hashes */
6994 rc = moveto_node_hash(set, scnode, list_inst, key_val, key_val_len);
6995 } else {
6996 rc = moveto_node(set, moveto_mod, ncname);
6997 }
6998 }
6999 LY_CHECK_GOTO(rc, cleanup);
7000 }
7001 }
7002
7003 /* Predicate* */
Michal Vasko14676352020-05-29 11:35:55 +02007004 while (!lyxp_check_token(NULL, exp, *exp_idx, LYXP_TOKEN_BRACK1)) {
Michal Vaskod3678892020-05-21 10:06:58 +02007005 rc = eval_predicate(exp, exp_idx, set, options, 1);
7006 LY_CHECK_RET(rc);
7007 }
7008
7009cleanup:
Michal Vaskodb51a8d2020-05-27 15:22:29 +02007010 if (set) {
7011 lydict_remove(set->ctx, ncname);
7012 }
Michal Vaskod3678892020-05-21 10:06:58 +02007013 lyd_free_tree(list_inst);
7014 return rc;
7015}
7016
7017/**
7018 * @brief Evaluate NodeType and any following Predicates. Logs directly on error.
7019 *
7020 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
7021 * [6] NodeTest ::= NameTest | NodeType '(' ')'
7022 * [8] NodeType ::= 'text' | 'node'
7023 *
7024 * @param[in] exp Parsed XPath expression.
7025 * @param[in] exp_idx Position in the expression @p exp.
7026 * @param[in] attr_axis Whether to search attributes or standard nodes.
7027 * @param[in] all_desc Whether to search all the descendants or children only.
7028 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7029 * @param[in] options XPath options.
7030 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7031 */
7032static LY_ERR
7033eval_node_type_with_predicate(struct lyxp_expr *exp, uint16_t *exp_idx, int attr_axis, int all_desc,
7034 struct lyxp_set *set, int options)
7035{
7036 LY_ERR rc;
7037
7038 /* TODO */
7039 (void)attr_axis;
7040 (void)all_desc;
7041
7042 if (set) {
7043 assert(exp->tok_len[*exp_idx] == 4);
7044 if (set->type == LYXP_SET_SCNODE_SET) {
7045 set_scnode_clear_ctx(set);
7046 /* just for the debug message below */
7047 set = NULL;
7048 } else {
7049 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "node", 4)) {
7050 rc = xpath_node(NULL, 0, set, options);
7051 } else {
7052 assert(!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "text", 4));
7053 rc = xpath_text(NULL, 0, set, options);
7054 }
7055 LY_CHECK_RET(rc);
7056 }
7057 }
7058 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7059 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7060 ++(*exp_idx);
7061
7062 /* '(' */
7063 assert(exp->tokens[*exp_idx] == LYXP_TOKEN_PAR1);
7064 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7065 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7066 ++(*exp_idx);
7067
7068 /* ')' */
7069 assert(exp->tokens[*exp_idx] == LYXP_TOKEN_PAR2);
7070 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7071 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7072 ++(*exp_idx);
7073
7074 /* Predicate* */
Michal Vasko14676352020-05-29 11:35:55 +02007075 while (!lyxp_check_token(NULL, exp, *exp_idx, LYXP_TOKEN_BRACK1)) {
Michal Vaskod3678892020-05-21 10:06:58 +02007076 rc = eval_predicate(exp, exp_idx, set, options, 1);
7077 LY_CHECK_RET(rc);
7078 }
7079
7080 return LY_SUCCESS;
7081}
7082
7083/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02007084 * @brief Evaluate RelativeLocationPath. Logs directly on error.
7085 *
7086 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
7087 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
Michal Vaskod3678892020-05-21 10:06:58 +02007088 * [6] NodeTest ::= NameTest | NodeType '(' ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007089 *
7090 * @param[in] exp Parsed XPath expression.
7091 * @param[in] exp_idx Position in the expression @p exp.
7092 * @param[in] all_desc Whether to search all the descendants or children only.
7093 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7094 * @param[in] options XPath options.
7095 * @return LY_ERR (YL_EINCOMPLETE on unresolved when)
7096 */
7097static LY_ERR
7098eval_relative_location_path(struct lyxp_expr *exp, uint16_t *exp_idx, int all_desc, struct lyxp_set *set, int options)
7099{
7100 int attr_axis;
7101 LY_ERR rc;
7102
7103 goto step;
7104 do {
7105 /* evaluate '/' or '//' */
7106 if (exp->tok_len[*exp_idx] == 1) {
7107 all_desc = 0;
7108 } else {
7109 assert(exp->tok_len[*exp_idx] == 2);
7110 all_desc = 1;
7111 }
7112 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7113 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7114 ++(*exp_idx);
7115
7116step:
Michal Vaskod3678892020-05-21 10:06:58 +02007117 /* evaluate abbreviated axis '@'? if any */
7118 if (exp->tokens[*exp_idx] == LYXP_TOKEN_AT) {
7119 attr_axis = 1;
7120
7121 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7122 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7123 ++(*exp_idx);
7124 } else {
7125 attr_axis = 0;
7126 }
7127
Michal Vasko03ff5a72019-09-11 13:49:33 +02007128 /* Step */
Michal Vasko03ff5a72019-09-11 13:49:33 +02007129 switch (exp->tokens[*exp_idx]) {
7130 case LYXP_TOKEN_DOT:
7131 /* evaluate '.' */
7132 if (set && (options & LYXP_SCNODE_ALL)) {
7133 rc = moveto_scnode_self(set, all_desc, options);
7134 } else {
7135 rc = moveto_self(set, all_desc, options);
7136 }
7137 LY_CHECK_RET(rc);
7138 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7139 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7140 ++(*exp_idx);
7141 break;
7142
7143 case LYXP_TOKEN_DDOT:
7144 /* evaluate '..' */
7145 if (set && (options & LYXP_SCNODE_ALL)) {
7146 rc = moveto_scnode_parent(set, all_desc, options);
7147 } else {
7148 rc = moveto_parent(set, all_desc, options);
7149 }
7150 LY_CHECK_RET(rc);
7151 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7152 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7153 ++(*exp_idx);
7154 break;
7155
Michal Vasko03ff5a72019-09-11 13:49:33 +02007156 case LYXP_TOKEN_NAMETEST:
Michal Vaskod3678892020-05-21 10:06:58 +02007157 /* evaluate NameTest Predicate* */
7158 rc = eval_name_test_with_predicate(exp, exp_idx, attr_axis, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007159 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02007160 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007161
Michal Vaskod3678892020-05-21 10:06:58 +02007162 case LYXP_TOKEN_NODETYPE:
7163 /* evaluate NodeType Predicate* */
7164 rc = eval_node_type_with_predicate(exp, exp_idx, attr_axis, all_desc, set, options);
7165 LY_CHECK_RET(rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007166 break;
7167
7168 default:
Michal Vasko02a77382019-09-12 11:47:35 +02007169 LOGINT_RET(set ? set->ctx : NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007170 }
Michal Vasko14676352020-05-29 11:35:55 +02007171 } while (!exp_check_token2(NULL, exp, *exp_idx, LYXP_TOKEN_OPERATOR_PATH, LYXP_TOKEN_OPERATOR_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007172
7173 return LY_SUCCESS;
7174}
7175
7176/**
7177 * @brief Evaluate AbsoluteLocationPath. Logs directly on error.
7178 *
7179 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
7180 *
7181 * @param[in] exp Parsed XPath expression.
7182 * @param[in] exp_idx Position in the expression @p exp.
7183 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7184 * @param[in] options XPath options.
7185 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7186 */
7187static LY_ERR
7188eval_absolute_location_path(struct lyxp_expr *exp, uint16_t *exp_idx, struct lyxp_set *set, int options)
7189{
7190 int all_desc;
7191 LY_ERR rc;
7192
7193 if (set) {
7194 /* no matter what tokens follow, we need to be at the root */
7195 moveto_root(set, options);
7196 }
7197
7198 /* '/' RelativeLocationPath? */
7199 if (exp->tok_len[*exp_idx] == 1) {
7200 /* evaluate '/' - deferred */
7201 all_desc = 0;
7202 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7203 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7204 ++(*exp_idx);
7205
Michal Vasko14676352020-05-29 11:35:55 +02007206 if (lyxp_check_token(NULL, exp, *exp_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007207 return LY_SUCCESS;
7208 }
7209 switch (exp->tokens[*exp_idx]) {
7210 case LYXP_TOKEN_DOT:
7211 case LYXP_TOKEN_DDOT:
7212 case LYXP_TOKEN_AT:
7213 case LYXP_TOKEN_NAMETEST:
7214 case LYXP_TOKEN_NODETYPE:
7215 rc = eval_relative_location_path(exp, exp_idx, all_desc, set, options);
7216 LY_CHECK_RET(rc);
7217 break;
7218 default:
7219 break;
7220 }
7221
7222 /* '//' RelativeLocationPath */
7223 } else {
7224 /* evaluate '//' - deferred so as not to waste memory by remembering all the nodes */
7225 all_desc = 1;
7226 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7227 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7228 ++(*exp_idx);
7229
7230 rc = eval_relative_location_path(exp, exp_idx, all_desc, set, options);
7231 LY_CHECK_RET(rc);
7232 }
7233
7234 return LY_SUCCESS;
7235}
7236
7237/**
7238 * @brief Evaluate FunctionCall. Logs directly on error.
7239 *
Michal Vaskod3678892020-05-21 10:06:58 +02007240 * [11] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007241 *
7242 * @param[in] exp Parsed XPath expression.
7243 * @param[in] exp_idx Position in the expression @p exp.
7244 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7245 * @param[in] options XPath options.
7246 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7247 */
7248static LY_ERR
7249eval_function_call(struct lyxp_expr *exp, uint16_t *exp_idx, struct lyxp_set *set, int options)
7250{
7251 LY_ERR rc;
7252 LY_ERR (*xpath_func)(struct lyxp_set **, uint16_t, struct lyxp_set *, int) = NULL;
Michal Vasko0cbf54f2019-12-16 10:01:06 +01007253 uint16_t arg_count = 0, i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007254 struct lyxp_set **args = NULL, **args_aux;
7255
7256 if (set) {
7257 /* FunctionName */
7258 switch (exp->tok_len[*exp_idx]) {
7259 case 3:
7260 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "not", 3)) {
7261 xpath_func = &xpath_not;
7262 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "sum", 3)) {
7263 xpath_func = &xpath_sum;
7264 }
7265 break;
7266 case 4:
7267 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "lang", 4)) {
7268 xpath_func = &xpath_lang;
7269 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "last", 4)) {
7270 xpath_func = &xpath_last;
7271 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "name", 4)) {
7272 xpath_func = &xpath_name;
7273 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "true", 4)) {
7274 xpath_func = &xpath_true;
7275 }
7276 break;
7277 case 5:
7278 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "count", 5)) {
7279 xpath_func = &xpath_count;
7280 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "false", 5)) {
7281 xpath_func = &xpath_false;
7282 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "floor", 5)) {
7283 xpath_func = &xpath_floor;
7284 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "round", 5)) {
7285 xpath_func = &xpath_round;
7286 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "deref", 5)) {
7287 xpath_func = &xpath_deref;
7288 }
7289 break;
7290 case 6:
7291 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "concat", 6)) {
7292 xpath_func = &xpath_concat;
7293 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "number", 6)) {
7294 xpath_func = &xpath_number;
7295 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "string", 6)) {
7296 xpath_func = &xpath_string;
7297 }
7298 break;
7299 case 7:
7300 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "boolean", 7)) {
7301 xpath_func = &xpath_boolean;
7302 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "ceiling", 7)) {
7303 xpath_func = &xpath_ceiling;
7304 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "current", 7)) {
7305 xpath_func = &xpath_current;
7306 }
7307 break;
7308 case 8:
7309 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "contains", 8)) {
7310 xpath_func = &xpath_contains;
7311 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "position", 8)) {
7312 xpath_func = &xpath_position;
7313 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "re-match", 8)) {
7314 xpath_func = &xpath_re_match;
7315 }
7316 break;
7317 case 9:
7318 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "substring", 9)) {
7319 xpath_func = &xpath_substring;
7320 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "translate", 9)) {
7321 xpath_func = &xpath_translate;
7322 }
7323 break;
7324 case 10:
7325 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "local-name", 10)) {
7326 xpath_func = &xpath_local_name;
7327 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "enum-value", 10)) {
7328 xpath_func = &xpath_enum_value;
7329 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "bit-is-set", 10)) {
7330 xpath_func = &xpath_bit_is_set;
7331 }
7332 break;
7333 case 11:
7334 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "starts-with", 11)) {
7335 xpath_func = &xpath_starts_with;
7336 }
7337 break;
7338 case 12:
7339 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "derived-from", 12)) {
7340 xpath_func = &xpath_derived_from;
7341 }
7342 break;
7343 case 13:
7344 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "namespace-uri", 13)) {
7345 xpath_func = &xpath_namespace_uri;
7346 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "string-length", 13)) {
7347 xpath_func = &xpath_string_length;
7348 }
7349 break;
7350 case 15:
7351 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "normalize-space", 15)) {
7352 xpath_func = &xpath_normalize_space;
7353 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "substring-after", 15)) {
7354 xpath_func = &xpath_substring_after;
7355 }
7356 break;
7357 case 16:
7358 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "substring-before", 16)) {
7359 xpath_func = &xpath_substring_before;
7360 }
7361 break;
7362 case 20:
7363 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "derived-from-or-self", 20)) {
7364 xpath_func = &xpath_derived_from_or_self;
7365 }
7366 break;
7367 }
7368
7369 if (!xpath_func) {
7370 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*exp_idx]]);
7371 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]]);
7372 return LY_EVALID;
7373 }
7374 }
7375
7376 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7377 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7378 ++(*exp_idx);
7379
7380 /* '(' */
7381 assert(exp->tokens[*exp_idx] == LYXP_TOKEN_PAR1);
7382 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7383 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7384 ++(*exp_idx);
7385
7386 /* ( Expr ( ',' Expr )* )? */
7387 if (exp->tokens[*exp_idx] != LYXP_TOKEN_PAR2) {
7388 if (set) {
7389 args = malloc(sizeof *args);
7390 LY_CHECK_ERR_GOTO(!args, LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7391 arg_count = 1;
7392 args[0] = set_copy(set);
7393 if (!args[0]) {
7394 rc = LY_EMEM;
7395 goto cleanup;
7396 }
7397
7398 rc = eval_expr_select(exp, exp_idx, 0, args[0], 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 }
Michal Vasko14676352020-05-29 11:35:55 +02007405 while (!lyxp_check_token(NULL, exp, *exp_idx, LYXP_TOKEN_COMMA)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007406 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7407 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7408 ++(*exp_idx);
7409
7410 if (set) {
7411 ++arg_count;
7412 args_aux = realloc(args, arg_count * sizeof *args);
7413 LY_CHECK_ERR_GOTO(!args_aux, arg_count--; LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7414 args = args_aux;
7415 args[arg_count - 1] = set_copy(set);
7416 if (!args[arg_count - 1]) {
7417 rc = LY_EMEM;
7418 goto cleanup;
7419 }
7420
7421 rc = eval_expr_select(exp, exp_idx, 0, args[arg_count - 1], options);
7422 LY_CHECK_GOTO(rc, cleanup);
7423 } else {
7424 rc = eval_expr_select(exp, exp_idx, 0, NULL, options);
7425 LY_CHECK_GOTO(rc, cleanup);
7426 }
7427 }
7428
7429 /* ')' */
7430 assert(exp->tokens[*exp_idx] == LYXP_TOKEN_PAR2);
7431 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7432 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7433 ++(*exp_idx);
7434
7435 if (set) {
7436 /* evaluate function */
7437 rc = xpath_func(args, arg_count, set, options);
7438
7439 if (options & LYXP_SCNODE_ALL) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007440 /* merge all nodes from arg evaluations */
7441 for (i = 0; i < arg_count; ++i) {
7442 set_scnode_clear_ctx(args[i]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007443 lyxp_set_scnode_merge(set, args[i]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007444 }
7445 }
7446 } else {
7447 rc = LY_SUCCESS;
7448 }
7449
7450cleanup:
7451 for (i = 0; i < arg_count; ++i) {
7452 lyxp_set_free(args[i]);
7453 }
7454 free(args);
7455
7456 return rc;
7457}
7458
7459/**
7460 * @brief Evaluate Number. Logs directly on error.
7461 *
7462 * @param[in] ctx Context for errors.
7463 * @param[in] exp Parsed XPath expression.
7464 * @param[in] exp_idx Position in the expression @p exp.
7465 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7466 * @return LY_ERR
7467 */
7468static LY_ERR
7469eval_number(struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *exp_idx, struct lyxp_set *set)
7470{
7471 long double num;
7472 char *endptr;
7473
7474 if (set) {
7475 errno = 0;
7476 num = strtold(&exp->expr[exp->tok_pos[*exp_idx]], &endptr);
7477 if (errno) {
7478 LOGVAL(ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*exp_idx]]);
7479 LOGVAL(ctx, LY_VLOG_LYD, set->ctx_node, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double (%s).",
7480 exp->tok_len[*exp_idx], &exp->expr[exp->tok_pos[*exp_idx]], strerror(errno));
7481 return LY_EVALID;
7482 } else if (endptr - &exp->expr[exp->tok_pos[*exp_idx]] != exp->tok_len[*exp_idx]) {
7483 LOGVAL(ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*exp_idx]]);
7484 LOGVAL(ctx, LY_VLOG_LYD, set->ctx_node, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double.",
7485 exp->tok_len[*exp_idx], &exp->expr[exp->tok_pos[*exp_idx]]);
7486 return LY_EVALID;
7487 }
7488
7489 set_fill_number(set, num);
7490 }
7491
7492 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7493 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7494 ++(*exp_idx);
7495 return LY_SUCCESS;
7496}
7497
7498/**
7499 * @brief Evaluate PathExpr. Logs directly on error.
7500 *
Michal Vaskod3678892020-05-21 10:06:58 +02007501 * [12] PathExpr ::= LocationPath | PrimaryExpr Predicate*
Michal Vasko03ff5a72019-09-11 13:49:33 +02007502 * | PrimaryExpr Predicate* '/' RelativeLocationPath
7503 * | PrimaryExpr Predicate* '//' RelativeLocationPath
7504 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
Michal Vaskod3678892020-05-21 10:06:58 +02007505 * [10] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall
Michal Vasko03ff5a72019-09-11 13:49:33 +02007506 *
7507 * @param[in] exp Parsed XPath expression.
7508 * @param[in] exp_idx Position in the expression @p exp.
7509 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7510 * @param[in] options XPath options.
7511 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7512 */
7513static LY_ERR
7514eval_path_expr(struct lyxp_expr *exp, uint16_t *exp_idx, struct lyxp_set *set, int options)
7515{
7516 int all_desc, parent_pos_pred;
7517 LY_ERR rc;
7518
7519 switch (exp->tokens[*exp_idx]) {
7520 case LYXP_TOKEN_PAR1:
7521 /* '(' Expr ')' */
7522
7523 /* '(' */
7524 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7525 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7526 ++(*exp_idx);
7527
7528 /* Expr */
7529 rc = eval_expr_select(exp, exp_idx, 0, set, options);
7530 LY_CHECK_RET(rc);
7531
7532 /* ')' */
7533 assert(exp->tokens[*exp_idx] == LYXP_TOKEN_PAR2);
7534 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7535 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7536 ++(*exp_idx);
7537
7538 parent_pos_pred = 0;
7539 goto predicate;
7540
7541 case LYXP_TOKEN_DOT:
7542 case LYXP_TOKEN_DDOT:
7543 case LYXP_TOKEN_AT:
7544 case LYXP_TOKEN_NAMETEST:
7545 case LYXP_TOKEN_NODETYPE:
7546 /* RelativeLocationPath */
7547 rc = eval_relative_location_path(exp, exp_idx, 0, set, options);
7548 LY_CHECK_RET(rc);
7549 break;
7550
7551 case LYXP_TOKEN_FUNCNAME:
7552 /* FunctionCall */
7553 if (!set) {
7554 rc = eval_function_call(exp, exp_idx, NULL, options);
7555 } else {
7556 rc = eval_function_call(exp, exp_idx, set, options);
7557 }
7558 LY_CHECK_RET(rc);
7559
7560 parent_pos_pred = 1;
7561 goto predicate;
7562
7563 case LYXP_TOKEN_OPERATOR_PATH:
Michal Vasko14676352020-05-29 11:35:55 +02007564 case LYXP_TOKEN_OPERATOR_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02007565 /* AbsoluteLocationPath */
7566 rc = eval_absolute_location_path(exp, exp_idx, set, options);
7567 LY_CHECK_RET(rc);
7568 break;
7569
7570 case LYXP_TOKEN_LITERAL:
7571 /* Literal */
7572 if (!set || (options & LYXP_SCNODE_ALL)) {
7573 if (set) {
7574 set_scnode_clear_ctx(set);
7575 }
7576 eval_literal(exp, exp_idx, NULL);
7577 } else {
7578 eval_literal(exp, exp_idx, set);
7579 }
7580
7581 parent_pos_pred = 1;
7582 goto predicate;
7583
7584 case LYXP_TOKEN_NUMBER:
7585 /* Number */
7586 if (!set || (options & LYXP_SCNODE_ALL)) {
7587 if (set) {
7588 set_scnode_clear_ctx(set);
7589 }
Michal Vasko02a77382019-09-12 11:47:35 +02007590 rc = eval_number(NULL, exp, exp_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007591 } else {
7592 rc = eval_number(set->ctx, exp, exp_idx, set);
7593 }
7594 LY_CHECK_RET(rc);
7595
7596 parent_pos_pred = 1;
7597 goto predicate;
7598
7599 default:
7600 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INTOK, print_token(exp->tokens[*exp_idx]),
7601 &exp->expr[exp->tok_pos[*exp_idx]]);
7602 return LY_EVALID;
7603 }
7604
7605 return LY_SUCCESS;
7606
7607predicate:
7608 /* Predicate* */
Michal Vasko14676352020-05-29 11:35:55 +02007609 while (!lyxp_check_token(NULL, exp, *exp_idx, LYXP_TOKEN_BRACK1)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007610 rc = eval_predicate(exp, exp_idx, set, options, parent_pos_pred);
7611 LY_CHECK_RET(rc);
7612 }
7613
7614 /* ('/' or '//') RelativeLocationPath */
Michal Vasko14676352020-05-29 11:35:55 +02007615 if (!exp_check_token2(NULL, exp, *exp_idx, LYXP_TOKEN_OPERATOR_PATH, LYXP_TOKEN_OPERATOR_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007616
7617 /* evaluate '/' or '//' */
Michal Vasko14676352020-05-29 11:35:55 +02007618 if (exp->tokens[*exp_idx] == LYXP_TOKEN_OPERATOR_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007619 all_desc = 0;
7620 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007621 all_desc = 1;
7622 }
7623
7624 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7625 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7626 ++(*exp_idx);
7627
7628 rc = eval_relative_location_path(exp, exp_idx, all_desc, set, options);
7629 LY_CHECK_RET(rc);
7630 }
7631
7632 return LY_SUCCESS;
7633}
7634
7635/**
7636 * @brief Evaluate UnionExpr. Logs directly on error.
7637 *
Michal Vaskod3678892020-05-21 10:06:58 +02007638 * [20] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007639 *
7640 * @param[in] exp Parsed XPath expression.
7641 * @param[in] exp_idx Position in the expression @p exp.
7642 * @param[in] repeat How many times this expression is repeated.
7643 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7644 * @param[in] options XPath options.
7645 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7646 */
7647static LY_ERR
7648eval_union_expr(struct lyxp_expr *exp, uint16_t *exp_idx, uint16_t repeat, struct lyxp_set *set, int options)
7649{
7650 LY_ERR rc = LY_SUCCESS;
7651 struct lyxp_set orig_set, set2;
7652 uint16_t i;
7653
7654 assert(repeat);
7655
7656 set_init(&orig_set, set);
7657 set_init(&set2, set);
7658
7659 set_fill_set(&orig_set, set);
7660
7661 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_UNION, set, options);
7662 LY_CHECK_GOTO(rc, cleanup);
7663
7664 /* ('|' PathExpr)* */
7665 for (i = 0; i < repeat; ++i) {
7666 assert(exp->tokens[*exp_idx] == LYXP_TOKEN_OPERATOR_UNI);
7667 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7668 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7669 ++(*exp_idx);
7670
7671 if (!set) {
7672 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_UNION, NULL, options);
7673 LY_CHECK_GOTO(rc, cleanup);
7674 continue;
7675 }
7676
7677 set_fill_set(&set2, &orig_set);
7678 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_UNION, &set2, options);
7679 LY_CHECK_GOTO(rc, cleanup);
7680
7681 /* eval */
7682 if (options & LYXP_SCNODE_ALL) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01007683 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007684 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007685 rc = moveto_union(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007686 LY_CHECK_GOTO(rc, cleanup);
7687 }
7688 }
7689
7690cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007691 lyxp_set_free_content(&orig_set);
7692 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007693 return rc;
7694}
7695
7696/**
7697 * @brief Evaluate UnaryExpr. Logs directly on error.
7698 *
Michal Vaskod3678892020-05-21 10:06:58 +02007699 * [19] UnaryExpr ::= UnionExpr | '-' UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007700 *
7701 * @param[in] exp Parsed XPath expression.
7702 * @param[in] exp_idx Position in the expression @p exp.
7703 * @param[in] repeat How many times this expression is repeated.
7704 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7705 * @param[in] options XPath options.
7706 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7707 */
7708static LY_ERR
7709eval_unary_expr(struct lyxp_expr *exp, uint16_t *exp_idx, uint16_t repeat, struct lyxp_set *set, int options)
7710{
7711 LY_ERR rc;
7712 uint16_t this_op, i;
7713
7714 assert(repeat);
7715
7716 /* ('-')+ */
7717 this_op = *exp_idx;
7718 for (i = 0; i < repeat; ++i) {
Michal Vasko14676352020-05-29 11:35:55 +02007719 assert(!lyxp_check_token(NULL, exp, *exp_idx, LYXP_TOKEN_OPERATOR_MATH) && (exp->expr[exp->tok_pos[*exp_idx]] == '-'));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007720
7721 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7722 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7723 ++(*exp_idx);
7724 }
7725
7726 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_UNARY, set, options);
7727 LY_CHECK_RET(rc);
7728
7729 if (set && (repeat % 2)) {
7730 if (options & LYXP_SCNODE_ALL) {
7731 warn_operands(set->ctx, set, NULL, 1, exp->expr, exp->tok_pos[this_op]);
7732 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007733 rc = moveto_op_math(set, NULL, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007734 LY_CHECK_RET(rc);
7735 }
7736 }
7737
7738 return LY_SUCCESS;
7739}
7740
7741/**
7742 * @brief Evaluate MultiplicativeExpr. Logs directly on error.
7743 *
Michal Vaskod3678892020-05-21 10:06:58 +02007744 * [18] MultiplicativeExpr ::= UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007745 * | MultiplicativeExpr '*' UnaryExpr
7746 * | MultiplicativeExpr 'div' UnaryExpr
7747 * | MultiplicativeExpr 'mod' UnaryExpr
7748 *
7749 * @param[in] exp Parsed XPath expression.
7750 * @param[in] exp_idx Position in the expression @p exp.
7751 * @param[in] repeat How many times this expression is repeated.
7752 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7753 * @param[in] options XPath options.
7754 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7755 */
7756static LY_ERR
7757eval_multiplicative_expr(struct lyxp_expr *exp, uint16_t *exp_idx, uint16_t repeat, struct lyxp_set *set, int options)
7758{
7759 LY_ERR rc;
7760 uint16_t this_op;
7761 struct lyxp_set orig_set, set2;
7762 uint16_t i;
7763
7764 assert(repeat);
7765
7766 set_init(&orig_set, set);
7767 set_init(&set2, set);
7768
7769 set_fill_set(&orig_set, set);
7770
7771 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_MULTIPLICATIVE, set, options);
7772 LY_CHECK_GOTO(rc, cleanup);
7773
7774 /* ('*' / 'div' / 'mod' UnaryExpr)* */
7775 for (i = 0; i < repeat; ++i) {
7776 this_op = *exp_idx;
7777
7778 assert(exp->tokens[*exp_idx] == LYXP_TOKEN_OPERATOR_MATH);
7779 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7780 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7781 ++(*exp_idx);
7782
7783 if (!set) {
7784 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_MULTIPLICATIVE, NULL, options);
7785 LY_CHECK_GOTO(rc, cleanup);
7786 continue;
7787 }
7788
7789 set_fill_set(&set2, &orig_set);
7790 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_MULTIPLICATIVE, &set2, options);
7791 LY_CHECK_GOTO(rc, cleanup);
7792
7793 /* eval */
7794 if (options & LYXP_SCNODE_ALL) {
7795 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007796 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007797 set_scnode_clear_ctx(set);
7798 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007799 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007800 LY_CHECK_GOTO(rc, cleanup);
7801 }
7802 }
7803
7804cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007805 lyxp_set_free_content(&orig_set);
7806 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007807 return rc;
7808}
7809
7810/**
7811 * @brief Evaluate AdditiveExpr. Logs directly on error.
7812 *
Michal Vaskod3678892020-05-21 10:06:58 +02007813 * [17] AdditiveExpr ::= MultiplicativeExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007814 * | AdditiveExpr '+' MultiplicativeExpr
7815 * | AdditiveExpr '-' MultiplicativeExpr
7816 *
7817 * @param[in] exp Parsed XPath expression.
7818 * @param[in] exp_idx Position in the expression @p exp.
7819 * @param[in] repeat How many times this expression is repeated.
7820 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7821 * @param[in] options XPath options.
7822 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7823 */
7824static LY_ERR
7825eval_additive_expr(struct lyxp_expr *exp, uint16_t *exp_idx, uint16_t repeat, struct lyxp_set *set, int options)
7826{
7827 LY_ERR rc;
7828 uint16_t this_op;
7829 struct lyxp_set orig_set, set2;
7830 uint16_t i;
7831
7832 assert(repeat);
7833
7834 set_init(&orig_set, set);
7835 set_init(&set2, set);
7836
7837 set_fill_set(&orig_set, set);
7838
7839 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_ADDITIVE, set, options);
7840 LY_CHECK_GOTO(rc, cleanup);
7841
7842 /* ('+' / '-' MultiplicativeExpr)* */
7843 for (i = 0; i < repeat; ++i) {
7844 this_op = *exp_idx;
7845
7846 assert(exp->tokens[*exp_idx] == LYXP_TOKEN_OPERATOR_MATH);
7847 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7848 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7849 ++(*exp_idx);
7850
7851 if (!set) {
7852 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_ADDITIVE, NULL, options);
7853 LY_CHECK_GOTO(rc, cleanup);
7854 continue;
7855 }
7856
7857 set_fill_set(&set2, &orig_set);
7858 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_ADDITIVE, &set2, options);
7859 LY_CHECK_GOTO(rc, cleanup);
7860
7861 /* eval */
7862 if (options & LYXP_SCNODE_ALL) {
7863 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007864 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007865 set_scnode_clear_ctx(set);
7866 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007867 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007868 LY_CHECK_GOTO(rc, cleanup);
7869 }
7870 }
7871
7872cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007873 lyxp_set_free_content(&orig_set);
7874 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007875 return rc;
7876}
7877
7878/**
7879 * @brief Evaluate RelationalExpr. Logs directly on error.
7880 *
Michal Vaskod3678892020-05-21 10:06:58 +02007881 * [16] RelationalExpr ::= AdditiveExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007882 * | RelationalExpr '<' AdditiveExpr
7883 * | RelationalExpr '>' AdditiveExpr
7884 * | RelationalExpr '<=' AdditiveExpr
7885 * | RelationalExpr '>=' AdditiveExpr
7886 *
7887 * @param[in] exp Parsed XPath expression.
7888 * @param[in] exp_idx Position in the expression @p exp.
7889 * @param[in] repeat How many times this expression is repeated.
7890 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7891 * @param[in] options XPath options.
7892 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7893 */
7894static LY_ERR
7895eval_relational_expr(struct lyxp_expr *exp, uint16_t *exp_idx, uint16_t repeat, struct lyxp_set *set, int options)
7896{
7897 LY_ERR rc;
7898 uint16_t this_op;
7899 struct lyxp_set orig_set, set2;
7900 uint16_t i;
7901
7902 assert(repeat);
7903
7904 set_init(&orig_set, set);
7905 set_init(&set2, set);
7906
7907 set_fill_set(&orig_set, set);
7908
7909 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_RELATIONAL, set, options);
7910 LY_CHECK_GOTO(rc, cleanup);
7911
7912 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
7913 for (i = 0; i < repeat; ++i) {
7914 this_op = *exp_idx;
7915
7916 assert(exp->tokens[*exp_idx] == LYXP_TOKEN_OPERATOR_COMP);
7917 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7918 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7919 ++(*exp_idx);
7920
7921 if (!set) {
7922 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_RELATIONAL, NULL, options);
7923 LY_CHECK_GOTO(rc, cleanup);
7924 continue;
7925 }
7926
7927 set_fill_set(&set2, &orig_set);
7928 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_RELATIONAL, &set2, options);
7929 LY_CHECK_GOTO(rc, cleanup);
7930
7931 /* eval */
7932 if (options & LYXP_SCNODE_ALL) {
7933 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007934 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007935 set_scnode_clear_ctx(set);
7936 } else {
7937 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options);
7938 LY_CHECK_GOTO(rc, cleanup);
7939 }
7940 }
7941
7942cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007943 lyxp_set_free_content(&orig_set);
7944 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007945 return rc;
7946}
7947
7948/**
7949 * @brief Evaluate EqualityExpr. Logs directly on error.
7950 *
Michal Vaskod3678892020-05-21 10:06:58 +02007951 * [15] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007952 * | EqualityExpr '!=' RelationalExpr
7953 *
7954 * @param[in] exp Parsed XPath expression.
7955 * @param[in] exp_idx Position in the expression @p exp.
7956 * @param[in] repeat How many times this expression is repeated.
7957 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7958 * @param[in] options XPath options.
7959 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7960 */
7961static LY_ERR
7962eval_equality_expr(struct lyxp_expr *exp, uint16_t *exp_idx, uint16_t repeat, struct lyxp_set *set, int options)
7963{
7964 LY_ERR rc;
7965 uint16_t this_op;
7966 struct lyxp_set orig_set, set2;
7967 uint16_t i;
7968
7969 assert(repeat);
7970
7971 set_init(&orig_set, set);
7972 set_init(&set2, set);
7973
7974 set_fill_set(&orig_set, set);
7975
7976 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_EQUALITY, set, options);
7977 LY_CHECK_GOTO(rc, cleanup);
7978
7979 /* ('=' / '!=' RelationalExpr)* */
7980 for (i = 0; i < repeat; ++i) {
7981 this_op = *exp_idx;
7982
7983 assert(exp->tokens[*exp_idx] == LYXP_TOKEN_OPERATOR_COMP);
7984 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7985 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7986 ++(*exp_idx);
7987
7988 if (!set) {
7989 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_EQUALITY, NULL, options);
7990 LY_CHECK_GOTO(rc, cleanup);
7991 continue;
7992 }
7993
7994 set_fill_set(&set2, &orig_set);
7995 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_EQUALITY, &set2, options);
7996 LY_CHECK_GOTO(rc, cleanup);
7997
7998 /* eval */
7999 if (options & LYXP_SCNODE_ALL) {
8000 warn_operands(set->ctx, set, &set2, 0, exp->expr, exp->tok_pos[this_op - 1]);
8001 warn_equality_value(exp, set, *exp_idx - 1, this_op - 1, *exp_idx - 1);
8002 warn_equality_value(exp, &set2, this_op - 1, this_op - 1, *exp_idx - 1);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008003 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008004 set_scnode_clear_ctx(set);
8005 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008006 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options);
8007 LY_CHECK_GOTO(rc, cleanup);
8008 }
8009 }
8010
8011cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008012 lyxp_set_free_content(&orig_set);
8013 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008014 return rc;
8015}
8016
8017/**
8018 * @brief Evaluate AndExpr. Logs directly on error.
8019 *
Michal Vaskod3678892020-05-21 10:06:58 +02008020 * [14] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008021 *
8022 * @param[in] exp Parsed XPath expression.
8023 * @param[in] exp_idx Position in the expression @p exp.
8024 * @param[in] repeat How many times this expression is repeated.
8025 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8026 * @param[in] options XPath options.
8027 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8028 */
8029static LY_ERR
8030eval_and_expr(struct lyxp_expr *exp, uint16_t *exp_idx, uint16_t repeat, struct lyxp_set *set, int options)
8031{
8032 LY_ERR rc;
8033 struct lyxp_set orig_set, set2;
8034 uint16_t i;
8035
8036 assert(repeat);
8037
8038 set_init(&orig_set, set);
8039 set_init(&set2, set);
8040
8041 set_fill_set(&orig_set, set);
8042
8043 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_AND, set, options);
8044 LY_CHECK_GOTO(rc, cleanup);
8045
8046 /* cast to boolean, we know that will be the final result */
8047 if (set && (options & LYXP_SCNODE_ALL)) {
8048 set_scnode_clear_ctx(set);
8049 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008050 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008051 }
8052
8053 /* ('and' EqualityExpr)* */
8054 for (i = 0; i < repeat; ++i) {
8055 assert(exp->tokens[*exp_idx] == LYXP_TOKEN_OPERATOR_LOG);
8056 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (!set || !set->val.bool ? "skipped" : "parsed"),
8057 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
8058 ++(*exp_idx);
8059
8060 /* lazy evaluation */
8061 if (!set || ((set->type == LYXP_SET_BOOLEAN) && !set->val.bool)) {
8062 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_AND, NULL, options);
8063 LY_CHECK_GOTO(rc, cleanup);
8064 continue;
8065 }
8066
8067 set_fill_set(&set2, &orig_set);
8068 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_AND, &set2, options);
8069 LY_CHECK_GOTO(rc, cleanup);
8070
8071 /* eval - just get boolean value actually */
8072 if (set->type == LYXP_SET_SCNODE_SET) {
8073 set_scnode_clear_ctx(&set2);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008074 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008075 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008076 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008077 set_fill_set(set, &set2);
8078 }
8079 }
8080
8081cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008082 lyxp_set_free_content(&orig_set);
8083 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008084 return rc;
8085}
8086
8087/**
8088 * @brief Evaluate OrExpr. Logs directly on error.
8089 *
Michal Vaskod3678892020-05-21 10:06:58 +02008090 * [13] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008091 *
8092 * @param[in] exp Parsed XPath expression.
8093 * @param[in] exp_idx Position in the expression @p exp.
8094 * @param[in] repeat How many times this expression is repeated.
8095 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8096 * @param[in] options XPath options.
8097 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8098 */
8099static LY_ERR
8100eval_or_expr(struct lyxp_expr *exp, uint16_t *exp_idx, uint16_t repeat, struct lyxp_set *set, int options)
8101{
8102 LY_ERR rc;
8103 struct lyxp_set orig_set, set2;
8104 uint16_t i;
8105
8106 assert(repeat);
8107
8108 set_init(&orig_set, set);
8109 set_init(&set2, set);
8110
8111 set_fill_set(&orig_set, set);
8112
8113 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_OR, set, options);
8114 LY_CHECK_GOTO(rc, cleanup);
8115
8116 /* cast to boolean, we know that will be the final result */
8117 if (set && (options & LYXP_SCNODE_ALL)) {
8118 set_scnode_clear_ctx(set);
8119 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008120 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008121 }
8122
8123 /* ('or' AndExpr)* */
8124 for (i = 0; i < repeat; ++i) {
8125 assert(exp->tokens[*exp_idx] == LYXP_TOKEN_OPERATOR_LOG);
8126 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (!set || set->val.bool ? "skipped" : "parsed"),
8127 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
8128 ++(*exp_idx);
8129
8130 /* lazy evaluation */
8131 if (!set || ((set->type == LYXP_SET_BOOLEAN) && set->val.bool)) {
8132 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_OR, NULL, options);
8133 LY_CHECK_GOTO(rc, cleanup);
8134 continue;
8135 }
8136
8137 set_fill_set(&set2, &orig_set);
8138 /* expr_type cound have been LYXP_EXPR_NONE in all these later calls (except for the first one),
8139 * but it does not matter */
8140 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_OR, &set2, options);
8141 LY_CHECK_GOTO(rc, cleanup);
8142
8143 /* eval - just get boolean value actually */
8144 if (set->type == LYXP_SET_SCNODE_SET) {
8145 set_scnode_clear_ctx(&set2);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008146 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008147 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008148 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008149 set_fill_set(set, &set2);
8150 }
8151 }
8152
8153cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008154 lyxp_set_free_content(&orig_set);
8155 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008156 return rc;
8157}
8158
8159/**
8160 * @brief Decide what expression is at the pointer @p exp_idx and evaluate it accordingly.
8161 *
8162 * @param[in] exp Parsed XPath expression.
8163 * @param[in] exp_idx Position in the expression @p exp.
8164 * @param[in] etype Expression type to evaluate.
8165 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8166 * @param[in] options XPath options.
8167 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8168 */
8169static LY_ERR
8170eval_expr_select(struct lyxp_expr *exp, uint16_t *exp_idx, enum lyxp_expr_type etype, struct lyxp_set *set, int options)
8171{
8172 uint16_t i, count;
8173 enum lyxp_expr_type next_etype;
8174 LY_ERR rc;
8175
8176 /* process operator repeats */
8177 if (!exp->repeat[*exp_idx]) {
8178 next_etype = LYXP_EXPR_NONE;
8179 } else {
8180 /* find etype repeat */
8181 for (i = 0; exp->repeat[*exp_idx][i] > etype; ++i);
8182
8183 /* select one-priority lower because etype expression called us */
8184 if (i) {
8185 next_etype = exp->repeat[*exp_idx][i - 1];
8186 /* count repeats for that expression */
8187 for (count = 0; i && exp->repeat[*exp_idx][i - 1] == next_etype; ++count, --i);
8188 } else {
8189 next_etype = LYXP_EXPR_NONE;
8190 }
8191 }
8192
8193 /* decide what expression are we parsing based on the repeat */
8194 switch (next_etype) {
8195 case LYXP_EXPR_OR:
8196 rc = eval_or_expr(exp, exp_idx, count, set, options);
8197 break;
8198 case LYXP_EXPR_AND:
8199 rc = eval_and_expr(exp, exp_idx, count, set, options);
8200 break;
8201 case LYXP_EXPR_EQUALITY:
8202 rc = eval_equality_expr(exp, exp_idx, count, set, options);
8203 break;
8204 case LYXP_EXPR_RELATIONAL:
8205 rc = eval_relational_expr(exp, exp_idx, count, set, options);
8206 break;
8207 case LYXP_EXPR_ADDITIVE:
8208 rc = eval_additive_expr(exp, exp_idx, count, set, options);
8209 break;
8210 case LYXP_EXPR_MULTIPLICATIVE:
8211 rc = eval_multiplicative_expr(exp, exp_idx, count, set, options);
8212 break;
8213 case LYXP_EXPR_UNARY:
8214 rc = eval_unary_expr(exp, exp_idx, count, set, options);
8215 break;
8216 case LYXP_EXPR_UNION:
8217 rc = eval_union_expr(exp, exp_idx, count, set, options);
8218 break;
8219 case LYXP_EXPR_NONE:
8220 rc = eval_path_expr(exp, exp_idx, set, options);
8221 break;
8222 default:
8223 LOGINT_RET(set->ctx);
8224 }
8225
8226 return rc;
8227}
8228
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008229/**
8230 * @brief Get root type.
8231 *
8232 * @param[in] ctx_node Context node.
8233 * @param[in] ctx_scnode Schema context node.
8234 * @param[in] options XPath options.
8235 * @return Root type.
8236 */
8237static enum lyxp_node_type
8238lyxp_get_root_type(const struct lyd_node *ctx_node, const struct lysc_node *ctx_scnode, int options)
8239{
8240 if (options & LYXP_SCNODE_ALL) {
8241 if (options & LYXP_SCNODE) {
8242 /* general root that can access everything */
8243 return LYXP_NODE_ROOT;
8244 } else if (!ctx_scnode || (ctx_scnode->flags & LYS_CONFIG_W)) {
8245 /* root context node can access only config data (because we said so, it is unspecified) */
8246 return LYXP_NODE_ROOT_CONFIG;
8247 } else {
8248 return LYXP_NODE_ROOT;
8249 }
8250 }
8251
8252 if (!ctx_node || (ctx_node->schema->flags & LYS_CONFIG_W)) {
8253 /* root context node can access only config data (because we said so, it is unspecified) */
8254 return LYXP_NODE_ROOT_CONFIG;
8255 }
8256
8257 return LYXP_NODE_ROOT;
8258}
8259
Michal Vasko03ff5a72019-09-11 13:49:33 +02008260LY_ERR
Michal Vaskoecd62de2019-11-13 12:35:11 +01008261lyxp_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 +01008262 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 +02008263{
Michal Vasko03ff5a72019-09-11 13:49:33 +02008264 uint16_t exp_idx = 0;
8265 LY_ERR rc;
8266
Michal Vaskoecd62de2019-11-13 12:35:11 +01008267 LY_CHECK_ARG_RET(NULL, exp, local_mod, set, LY_EINVAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008268
8269 /* prepare set for evaluation */
8270 exp_idx = 0;
8271 memset(set, 0, sizeof *set);
Michal Vaskod3678892020-05-21 10:06:58 +02008272 set->type = LYXP_SET_NODE_SET;
Michal Vasko9b368d32020-02-14 13:53:31 +01008273 set_insert_node(set, (struct lyd_node *)ctx_node, 0, ctx_node_type, 0);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008274 set->ctx = local_mod->ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008275 set->ctx_node = ctx_node;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008276 set->root_type = lyxp_get_root_type(ctx_node, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008277 set->local_mod = local_mod;
Michal Vaskof03ed032020-03-04 13:31:44 +01008278 set->tree = tree;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008279 set->format = format;
8280
8281 /* evaluate */
8282 rc = eval_expr_select(exp, &exp_idx, 0, set, options);
8283 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02008284 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008285 }
8286
Michal Vasko03ff5a72019-09-11 13:49:33 +02008287 return rc;
8288}
8289
8290#if 0
8291
8292/* full xml printing of set elements, not used currently */
8293
8294void
8295lyxp_set_print_xml(FILE *f, struct lyxp_set *set)
8296{
8297 uint32_t i;
8298 char *str_num;
8299 struct lyout out;
8300
8301 memset(&out, 0, sizeof out);
8302
8303 out.type = LYOUT_STREAM;
8304 out.method.f = f;
8305
8306 switch (set->type) {
8307 case LYXP_SET_EMPTY:
8308 ly_print(&out, "Empty XPath set\n\n");
8309 break;
8310 case LYXP_SET_BOOLEAN:
8311 ly_print(&out, "Boolean XPath set:\n");
8312 ly_print(&out, "%s\n\n", set->value.bool ? "true" : "false");
8313 break;
8314 case LYXP_SET_STRING:
8315 ly_print(&out, "String XPath set:\n");
8316 ly_print(&out, "\"%s\"\n\n", set->value.str);
8317 break;
8318 case LYXP_SET_NUMBER:
8319 ly_print(&out, "Number XPath set:\n");
8320
8321 if (isnan(set->value.num)) {
8322 str_num = strdup("NaN");
8323 } else if ((set->value.num == 0) || (set->value.num == -0.0f)) {
8324 str_num = strdup("0");
8325 } else if (isinf(set->value.num) && !signbit(set->value.num)) {
8326 str_num = strdup("Infinity");
8327 } else if (isinf(set->value.num) && signbit(set->value.num)) {
8328 str_num = strdup("-Infinity");
8329 } else if ((long long)set->value.num == set->value.num) {
8330 if (asprintf(&str_num, "%lld", (long long)set->value.num) == -1) {
8331 str_num = NULL;
8332 }
8333 } else {
8334 if (asprintf(&str_num, "%03.1Lf", set->value.num) == -1) {
8335 str_num = NULL;
8336 }
8337 }
8338 if (!str_num) {
8339 LOGMEM;
8340 return;
8341 }
8342 ly_print(&out, "%s\n\n", str_num);
8343 free(str_num);
8344 break;
8345 case LYXP_SET_NODE_SET:
8346 ly_print(&out, "Node XPath set:\n");
8347
8348 for (i = 0; i < set->used; ++i) {
8349 ly_print(&out, "%d. ", i + 1);
8350 switch (set->node_type[i]) {
8351 case LYXP_NODE_ROOT_ALL:
8352 ly_print(&out, "ROOT all\n\n");
8353 break;
8354 case LYXP_NODE_ROOT_CONFIG:
8355 ly_print(&out, "ROOT config\n\n");
8356 break;
8357 case LYXP_NODE_ROOT_STATE:
8358 ly_print(&out, "ROOT state\n\n");
8359 break;
8360 case LYXP_NODE_ROOT_NOTIF:
8361 ly_print(&out, "ROOT notification \"%s\"\n\n", set->value.nodes[i]->schema->name);
8362 break;
8363 case LYXP_NODE_ROOT_RPC:
8364 ly_print(&out, "ROOT rpc \"%s\"\n\n", set->value.nodes[i]->schema->name);
8365 break;
8366 case LYXP_NODE_ROOT_OUTPUT:
8367 ly_print(&out, "ROOT output \"%s\"\n\n", set->value.nodes[i]->schema->name);
8368 break;
8369 case LYXP_NODE_ELEM:
8370 ly_print(&out, "ELEM \"%s\"\n", set->value.nodes[i]->schema->name);
8371 xml_print_node(&out, 1, set->value.nodes[i], 1, LYP_FORMAT);
8372 ly_print(&out, "\n");
8373 break;
8374 case LYXP_NODE_TEXT:
8375 ly_print(&out, "TEXT \"%s\"\n\n", ((struct lyd_node_leaf_list *)set->value.nodes[i])->value_str);
8376 break;
8377 case LYXP_NODE_ATTR:
8378 ly_print(&out, "ATTR \"%s\" = \"%s\"\n\n", set->value.attrs[i]->name, set->value.attrs[i]->value);
8379 break;
8380 }
8381 }
8382 break;
8383 }
8384}
8385
8386#endif
8387
8388LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008389lyxp_set_cast(struct lyxp_set *set, enum lyxp_set_type target)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008390{
8391 long double num;
8392 char *str;
8393 LY_ERR rc;
8394
8395 if (!set || (set->type == target)) {
8396 return LY_SUCCESS;
8397 }
8398
8399 /* it's not possible to convert anything into a node set */
Michal Vaskod3678892020-05-21 10:06:58 +02008400 assert(target != LYXP_SET_NODE_SET);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008401
8402 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vaskod3678892020-05-21 10:06:58 +02008403 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008404 return LY_EINVAL;
8405 }
8406
8407 /* to STRING */
Michal Vaskod3678892020-05-21 10:06:58 +02008408 if ((target == LYXP_SET_STRING) || ((target == LYXP_SET_NUMBER) && (set->type == LYXP_SET_NODE_SET))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008409 switch (set->type) {
8410 case LYXP_SET_NUMBER:
8411 if (isnan(set->val.num)) {
8412 set->val.str = strdup("NaN");
8413 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8414 } else if ((set->val.num == 0) || (set->val.num == -0.0f)) {
8415 set->val.str = strdup("0");
8416 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8417 } else if (isinf(set->val.num) && !signbit(set->val.num)) {
8418 set->val.str = strdup("Infinity");
8419 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8420 } else if (isinf(set->val.num) && signbit(set->val.num)) {
8421 set->val.str = strdup("-Infinity");
8422 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8423 } else if ((long long)set->val.num == set->val.num) {
8424 if (asprintf(&str, "%lld", (long long)set->val.num) == -1) {
8425 LOGMEM_RET(set->ctx);
8426 }
8427 set->val.str = str;
8428 } else {
8429 if (asprintf(&str, "%03.1Lf", set->val.num) == -1) {
8430 LOGMEM_RET(set->ctx);
8431 }
8432 set->val.str = str;
8433 }
8434 break;
8435 case LYXP_SET_BOOLEAN:
8436 if (set->val.bool) {
8437 set->val.str = strdup("true");
8438 } else {
8439 set->val.str = strdup("false");
8440 }
8441 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM);
8442 break;
8443 case LYXP_SET_NODE_SET:
8444 assert(set->used);
8445
8446 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008447 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008448
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008449 rc = cast_node_set_to_string(set, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008450 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02008451 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008452 set->val.str = str;
8453 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008454 default:
8455 LOGINT_RET(set->ctx);
8456 }
8457 set->type = LYXP_SET_STRING;
8458 }
8459
8460 /* to NUMBER */
8461 if (target == LYXP_SET_NUMBER) {
8462 switch (set->type) {
8463 case LYXP_SET_STRING:
8464 num = cast_string_to_number(set->val.str);
Michal Vaskod3678892020-05-21 10:06:58 +02008465 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008466 set->val.num = num;
8467 break;
8468 case LYXP_SET_BOOLEAN:
8469 if (set->val.bool) {
8470 set->val.num = 1;
8471 } else {
8472 set->val.num = 0;
8473 }
8474 break;
8475 default:
8476 LOGINT_RET(set->ctx);
8477 }
8478 set->type = LYXP_SET_NUMBER;
8479 }
8480
8481 /* to BOOLEAN */
8482 if (target == LYXP_SET_BOOLEAN) {
8483 switch (set->type) {
8484 case LYXP_SET_NUMBER:
8485 if ((set->val.num == 0) || (set->val.num == -0.0f) || isnan(set->val.num)) {
8486 set->val.bool = 0;
8487 } else {
8488 set->val.bool = 1;
8489 }
8490 break;
8491 case LYXP_SET_STRING:
8492 if (set->val.str[0]) {
Michal Vaskod3678892020-05-21 10:06:58 +02008493 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008494 set->val.bool = 1;
8495 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02008496 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008497 set->val.bool = 0;
8498 }
8499 break;
8500 case LYXP_SET_NODE_SET:
Michal Vaskod3678892020-05-21 10:06:58 +02008501 if (set->used) {
8502 lyxp_set_free_content(set);
8503 set->val.bool = 1;
8504 } else {
8505 lyxp_set_free_content(set);
8506 set->val.bool = 0;
8507 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008508 break;
8509 default:
8510 LOGINT_RET(set->ctx);
8511 }
8512 set->type = LYXP_SET_BOOLEAN;
8513 }
8514
Michal Vasko03ff5a72019-09-11 13:49:33 +02008515 return LY_SUCCESS;
8516}
8517
8518LY_ERR
8519lyxp_atomize(struct lyxp_expr *exp, LYD_FORMAT format, const struct lys_module *local_mod, const struct lysc_node *ctx_scnode,
8520 enum lyxp_node_type ctx_scnode_type, struct lyxp_set *set, int options)
8521{
8522 struct ly_ctx *ctx;
8523 uint16_t exp_idx = 0;
8524
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008525 LY_CHECK_ARG_RET(NULL, exp, local_mod, set, LY_EINVAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008526
8527 ctx = local_mod->ctx;
8528
8529 /* prepare set for evaluation */
8530 exp_idx = 0;
8531 memset(set, 0, sizeof *set);
8532 set->type = LYXP_SET_SCNODE_SET;
Michal Vaskoecd62de2019-11-13 12:35:11 +01008533 lyxp_set_scnode_insert_node(set, ctx_scnode, ctx_scnode_type);
Michal Vasko5c4e5892019-11-14 12:31:38 +01008534 set->val.scnodes[0].in_ctx = -2;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008535 set->ctx = ctx;
8536 set->ctx_scnode = ctx_scnode;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008537 set->root_type = lyxp_get_root_type(NULL, ctx_scnode, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008538 set->local_mod = local_mod;
8539 set->format = format;
8540
8541 /* evaluate */
8542 return eval_expr_select(exp, &exp_idx, 0, set, options);
8543}