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