blob: a60170f8ca4f0bbb8965d01ee9962b1c0ddf0aa0 [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 Vasko14676352020-05-29 11:35:55 +020038static LY_ERR reparse_or_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *exp_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +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)";
Michal Vasko14676352020-05-29 11:35:55 +0200108 case LYXP_TOKEN_OPERATOR_RPATH:
109 return "Operator(Recursive Path)";
Michal Vasko03ff5a72019-09-11 13:49:33 +0200110 case LYXP_TOKEN_LITERAL:
111 return "Literal";
112 case LYXP_TOKEN_NUMBER:
113 return "Number";
114 default:
115 LOGINT(NULL);
116 return "";
117 }
118}
119
120/**
121 * @brief Print the whole expression \p exp to debug output.
122 *
123 * @param[in] exp Expression to use.
124 */
125static void
126print_expr_struct_debug(struct lyxp_expr *exp)
127{
128 uint16_t i, j;
129 char tmp[128];
130
131 if (!exp || (ly_log_level < LY_LLDBG)) {
132 return;
133 }
134
135 LOGDBG(LY_LDGXPATH, "expression \"%s\":", exp->expr);
136 for (i = 0; i < exp->used; ++i) {
137 sprintf(tmp, "\ttoken %s, in expression \"%.*s\"", print_token(exp->tokens[i]), exp->tok_len[i],
138 &exp->expr[exp->tok_pos[i]]);
139 if (exp->repeat[i]) {
140 sprintf(tmp + strlen(tmp), " (repeat %d", exp->repeat[i][0]);
141 for (j = 1; exp->repeat[i][j]; ++j) {
142 sprintf(tmp + strlen(tmp), ", %d", exp->repeat[i][j]);
143 }
144 strcat(tmp, ")");
145 }
146 LOGDBG(LY_LDGXPATH, tmp);
147 }
148}
149
150#ifndef NDEBUG
151
152/**
153 * @brief Print XPath set content to debug output.
154 *
155 * @param[in] set Set to print.
156 */
157static void
158print_set_debug(struct lyxp_set *set)
159{
160 uint32_t i;
161 char *str;
162 int dynamic;
163 struct lyxp_set_node *item;
164 struct lyxp_set_scnode *sitem;
165
166 if (ly_log_level < LY_LLDBG) {
167 return;
168 }
169
170 switch (set->type) {
171 case LYXP_SET_NODE_SET:
172 LOGDBG(LY_LDGXPATH, "set NODE SET:");
173 for (i = 0; i < set->used; ++i) {
174 item = &set->val.nodes[i];
175
176 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +0100177 case LYXP_NODE_NONE:
178 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): NONE", i + 1, item->pos);
179 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200180 case LYXP_NODE_ROOT:
181 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT", i + 1, item->pos);
182 break;
183 case LYXP_NODE_ROOT_CONFIG:
184 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT CONFIG", i + 1, item->pos);
185 break;
186 case LYXP_NODE_ELEM:
187 if ((item->node->schema->nodetype == LYS_LIST)
188 && (((struct lyd_node_inner *)item->node)->child->schema->nodetype == LYS_LEAF)) {
189 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (1st child val: %s)", i + 1, item->pos,
190 item->node->schema->name,
191 (str = (char *)lyd_value2str((struct lyd_node_term *)lyd_node_children(item->node), &dynamic)));
192 if (dynamic) {
193 free(str);
194 }
195 } else if (((struct lyd_node_inner *)item->node)->schema->nodetype == LYS_LEAFLIST) {
196 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (val: %s)", i + 1, item->pos,
197 item->node->schema->name,
198 (str = (char *)lyd_value2str((struct lyd_node_term *)item->node, &dynamic)));
199 if (dynamic) {
200 free(str);
201 }
202 } else {
203 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s", i + 1, item->pos, item->node->schema->name);
204 }
205 break;
206 case LYXP_NODE_TEXT:
207 if (item->node->schema->nodetype & LYS_ANYDATA) {
208 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): TEXT <%s>", i + 1, item->pos,
209 item->node->schema->nodetype == LYS_ANYXML ? "anyxml" : "anydata");
210 } else {
211 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): TEXT %s", i + 1, item->pos,
212 (str = (char *)lyd_value2str((struct lyd_node_term *)item->node, &dynamic)));
213 if (dynamic) {
214 free(str);
215 }
216 }
217 break;
Michal Vasko9f96a052020-03-10 09:41:45 +0100218 case LYXP_NODE_META:
219 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): META %s = %s", i + 1, item->pos, set->val.meta[i].meta->name,
220 set->val.meta[i].meta->value);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200221 break;
222 }
223 }
224 break;
225
226 case LYXP_SET_SCNODE_SET:
227 LOGDBG(LY_LDGXPATH, "set SCNODE SET:");
228 for (i = 0; i < set->used; ++i) {
229 sitem = &set->val.scnodes[i];
230
231 switch (sitem->type) {
232 case LYXP_NODE_ROOT:
233 LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT", i + 1, sitem->in_ctx);
234 break;
235 case LYXP_NODE_ROOT_CONFIG:
236 LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT CONFIG", i + 1, sitem->in_ctx);
237 break;
238 case LYXP_NODE_ELEM:
239 LOGDBG(LY_LDGXPATH, "\t%d (%u): ELEM %s", i + 1, sitem->in_ctx, sitem->scnode->name);
240 break;
241 default:
242 LOGINT(NULL);
243 break;
244 }
245 }
246 break;
247
Michal Vasko03ff5a72019-09-11 13:49:33 +0200248 case LYXP_SET_BOOLEAN:
249 LOGDBG(LY_LDGXPATH, "set BOOLEAN");
250 LOGDBG(LY_LDGXPATH, "\t%s", (set->val.bool ? "true" : "false"));
251 break;
252
253 case LYXP_SET_STRING:
254 LOGDBG(LY_LDGXPATH, "set STRING");
255 LOGDBG(LY_LDGXPATH, "\t%s", set->val.str);
256 break;
257
258 case LYXP_SET_NUMBER:
259 LOGDBG(LY_LDGXPATH, "set NUMBER");
260
261 if (isnan(set->val.num)) {
262 str = strdup("NaN");
263 } else if ((set->val.num == 0) || (set->val.num == -0.0f)) {
264 str = strdup("0");
265 } else if (isinf(set->val.num) && !signbit(set->val.num)) {
266 str = strdup("Infinity");
267 } else if (isinf(set->val.num) && signbit(set->val.num)) {
268 str = strdup("-Infinity");
269 } else if ((long long)set->val.num == set->val.num) {
270 if (asprintf(&str, "%lld", (long long)set->val.num) == -1) {
271 str = NULL;
272 }
273 } else {
274 if (asprintf(&str, "%03.1Lf", set->val.num) == -1) {
275 str = NULL;
276 }
277 }
278 LY_CHECK_ERR_RET(!str, LOGMEM(NULL), );
279
280 LOGDBG(LY_LDGXPATH, "\t%s", str);
281 free(str);
282 }
283}
284
285#endif
286
287/**
288 * @brief Realloc the string \p str.
289 *
290 * @param[in] ctx libyang context for logging.
291 * @param[in] needed How much free space is required.
292 * @param[in,out] str Pointer to the string to use.
293 * @param[in,out] used Used bytes in \p str.
294 * @param[in,out] size Allocated bytes in \p str.
295 * @return LY_ERR
296 */
297static LY_ERR
Michal Vasko52927e22020-03-16 17:26:14 +0100298cast_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 +0200299{
300 if (*size - *used < needed) {
301 do {
302 if ((UINT16_MAX - *size) < LYXP_STRING_CAST_SIZE_STEP) {
303 LOGERR(ctx, LY_EINVAL, "XPath string length limit (%u) reached.", UINT16_MAX);
304 return LY_EINVAL;
305 }
306 *size += LYXP_STRING_CAST_SIZE_STEP;
307 } while (*size - *used < needed);
308 *str = ly_realloc(*str, *size * sizeof(char));
309 LY_CHECK_ERR_RET(!(*str), LOGMEM(ctx), LY_EMEM);
310 }
311
312 return LY_SUCCESS;
313}
314
315/**
316 * @brief Cast nodes recursively to one string @p str.
317 *
318 * @param[in] node Node to cast.
319 * @param[in] fake_cont Whether to put the data into a "fake" container.
320 * @param[in] root_type Type of the XPath root.
321 * @param[in] indent Current indent.
322 * @param[in,out] str Resulting string.
323 * @param[in,out] used Used bytes in @p str.
324 * @param[in,out] size Allocated bytes in @p str.
325 * @return LY_ERR
326 */
327static LY_ERR
328cast_string_recursive(const struct lyd_node *node, int fake_cont, enum lyxp_node_type root_type, uint16_t indent, char **str,
329 uint16_t *used, uint16_t *size)
330{
331 char *buf, *line, *ptr;
332 const char *value_str;
333 int dynamic;
334 const struct lyd_node *child;
335 struct lyd_node_any *any;
336 LY_ERR rc;
337
338 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) {
339 return LY_SUCCESS;
340 }
341
342 if (fake_cont) {
343 rc = cast_string_realloc(LYD_NODE_CTX(node), 1, str, used, size);
344 LY_CHECK_RET(rc);
345 strcpy(*str + (*used - 1), "\n");
346 ++(*used);
347
348 ++indent;
349 }
350
351 switch (node->schema->nodetype) {
352 case LYS_CONTAINER:
353 case LYS_LIST:
354 case LYS_RPC:
355 case LYS_NOTIF:
356 rc = cast_string_realloc(LYD_NODE_CTX(node), 1, str, used, size);
357 LY_CHECK_RET(rc);
358 strcpy(*str + (*used - 1), "\n");
359 ++(*used);
360
361 for (child = lyd_node_children(node); child; child = child->next) {
362 rc = cast_string_recursive(child, 0, root_type, indent + 1, str, used, size);
363 LY_CHECK_RET(rc);
364 }
365
366 break;
367
368 case LYS_LEAF:
369 case LYS_LEAFLIST:
370 value_str = lyd_value2str(((struct lyd_node_term *)node), &dynamic);
371
372 /* print indent */
373 rc = cast_string_realloc(LYD_NODE_CTX(node), indent * 2 + strlen(value_str) + 1, str, used, size);
374 if (rc != LY_SUCCESS) {
375 if (dynamic) {
376 free((char *)value_str);
377 }
378 return rc;
379 }
380 memset(*str + (*used - 1), ' ', indent * 2);
381 *used += indent * 2;
382
383 /* print value */
384 if (*used == 1) {
385 sprintf(*str + (*used - 1), "%s", value_str);
386 *used += strlen(value_str);
387 } else {
388 sprintf(*str + (*used - 1), "%s\n", value_str);
389 *used += strlen(value_str) + 1;
390 }
391 if (dynamic) {
392 free((char *)value_str);
393 }
394
395 break;
396
397 case LYS_ANYXML:
398 case LYS_ANYDATA:
399 any = (struct lyd_node_any *)node;
400 if (!(void *)any->value.tree) {
401 /* no content */
402 buf = strdup("");
403 LY_CHECK_ERR_RET(!buf, LOGMEM(LYD_NODE_CTX(node)), LY_EMEM);
404 } else {
Radek Krejci241f6b52020-05-21 18:13:49 +0200405 struct ly_out *out;
Radek Krejcia5bba312020-01-09 15:41:20 +0100406
Michal Vasko03ff5a72019-09-11 13:49:33 +0200407 switch (any->value_type) {
408 case LYD_ANYDATA_STRING:
409 case LYD_ANYDATA_XML:
410 case LYD_ANYDATA_JSON:
411 buf = strdup(any->value.json);
412 LY_CHECK_ERR_RET(!buf, LOGMEM(LYD_NODE_CTX(node)), LY_EMEM);
413 break;
414 case LYD_ANYDATA_DATATREE:
Radek Krejci241f6b52020-05-21 18:13:49 +0200415 out = ly_out_new_memory(&buf, 0);
Radek Krejcia5bba312020-01-09 15:41:20 +0100416 rc = lyd_print(out, any->value.tree, LYD_XML, LYDP_WITHSIBLINGS);
Radek Krejci241f6b52020-05-21 18:13:49 +0200417 ly_out_free(out, NULL, 0);
Radek Krejcia5bba312020-01-09 15:41:20 +0100418 LY_CHECK_RET(rc < 0, -rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200419 break;
420 /* TODO case LYD_ANYDATA_LYB:
421 LOGERR(LYD_NODE_CTX(node), LY_EINVAL, "Cannot convert LYB anydata into string.");
422 return -1;*/
423 }
424 }
425
426 line = strtok_r(buf, "\n", &ptr);
427 do {
428 rc = cast_string_realloc(LYD_NODE_CTX(node), indent * 2 + strlen(line) + 1, str, used, size);
429 if (rc != LY_SUCCESS) {
430 free(buf);
431 return rc;
432 }
433 memset(*str + (*used - 1), ' ', indent * 2);
434 *used += indent * 2;
435
436 strcpy(*str + (*used - 1), line);
437 *used += strlen(line);
438
439 strcpy(*str + (*used - 1), "\n");
440 *used += 1;
441 } while ((line = strtok_r(NULL, "\n", &ptr)));
442
443 free(buf);
444 break;
445
446 default:
447 LOGINT_RET(LYD_NODE_CTX(node));
448 }
449
450 if (fake_cont) {
451 rc = cast_string_realloc(LYD_NODE_CTX(node), 1, str, used, size);
452 LY_CHECK_RET(rc);
453 strcpy(*str + (*used - 1), "\n");
454 ++(*used);
455
456 --indent;
457 }
458
459 return LY_SUCCESS;
460}
461
462/**
463 * @brief Cast an element into a string.
464 *
465 * @param[in] node Node to cast.
466 * @param[in] fake_cont Whether to put the data into a "fake" container.
467 * @param[in] root_type Type of the XPath root.
468 * @param[out] str Element cast to dynamically-allocated string.
469 * @return LY_ERR
470 */
471static LY_ERR
472cast_string_elem(struct lyd_node *node, int fake_cont, enum lyxp_node_type root_type, char **str)
473{
474 uint16_t used, size;
475 LY_ERR rc;
476
477 *str = malloc(LYXP_STRING_CAST_SIZE_START * sizeof(char));
478 LY_CHECK_ERR_RET(!*str, LOGMEM(LYD_NODE_CTX(node)), LY_EMEM);
479 (*str)[0] = '\0';
480 used = 1;
481 size = LYXP_STRING_CAST_SIZE_START;
482
483 rc = cast_string_recursive(node, fake_cont, root_type, 0, str, &used, &size);
484 if (rc != LY_SUCCESS) {
485 free(*str);
486 return rc;
487 }
488
489 if (size > used) {
490 *str = ly_realloc(*str, used * sizeof(char));
491 LY_CHECK_ERR_RET(!*str, LOGMEM(LYD_NODE_CTX(node)), LY_EMEM);
492 }
493 return LY_SUCCESS;
494}
495
496/**
497 * @brief Cast a LYXP_SET_NODE_SET set into a string.
498 * Context position aware.
499 *
500 * @param[in] set Set to cast.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200501 * @param[out] str Cast dynamically-allocated string.
502 * @return LY_ERR
503 */
504static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100505cast_node_set_to_string(struct lyxp_set *set, char **str)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200506{
Michal Vasko03ff5a72019-09-11 13:49:33 +0200507 int dynamic;
508
Michal Vasko03ff5a72019-09-11 13:49:33 +0200509 switch (set->val.nodes[0].type) {
Michal Vasko2caefc12019-11-14 16:07:56 +0100510 case LYXP_NODE_NONE:
511 /* invalid */
512 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200513 case LYXP_NODE_ROOT:
514 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100515 return cast_string_elem(set->val.nodes[0].node, 1, set->root_type, str);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200516 case LYXP_NODE_ELEM:
517 case LYXP_NODE_TEXT:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100518 return cast_string_elem(set->val.nodes[0].node, 0, set->root_type, str);
Michal Vasko9f96a052020-03-10 09:41:45 +0100519 case LYXP_NODE_META:
520 *str = (char *)lyd_meta2str(set->val.meta[0].meta, &dynamic);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200521 if (!dynamic) {
522 *str = strdup(*str);
523 if (!*str) {
524 LOGMEM_RET(set->ctx);
525 }
526 }
527 return LY_SUCCESS;
528 }
529
530 LOGINT_RET(set->ctx);
531}
532
533/**
534 * @brief Cast a string into an XPath number.
535 *
536 * @param[in] str String to use.
537 * @return Cast number.
538 */
539static long double
540cast_string_to_number(const char *str)
541{
542 long double num;
543 char *ptr;
544
545 errno = 0;
546 num = strtold(str, &ptr);
547 if (errno || *ptr) {
548 num = NAN;
549 }
550 return num;
551}
552
553/**
554 * @brief Callback for checking value equality.
555 *
556 * @param[in] val1_p First value.
557 * @param[in] val2_p Second value.
558 * @param[in] mod Whether hash table is being modified.
559 * @param[in] cb_data Callback data.
560 * @return 0 if not equal, non-zero if equal.
561 */
562static int
563set_values_equal_cb(void *val1_p, void *val2_p, int UNUSED(mod), void *UNUSED(cb_data))
564{
565 struct lyxp_set_hash_node *val1, *val2;
566
567 val1 = (struct lyxp_set_hash_node *)val1_p;
568 val2 = (struct lyxp_set_hash_node *)val2_p;
569
570 if ((val1->node == val2->node) && (val1->type == val2->type)) {
571 return 1;
572 }
573
574 return 0;
575}
576
577/**
578 * @brief Insert node and its hash into set.
579 *
580 * @param[in] set et to insert to.
581 * @param[in] node Node with hash.
582 * @param[in] type Node type.
583 */
584static void
585set_insert_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type)
586{
587 int r;
588 uint32_t i, hash;
589 struct lyxp_set_hash_node hnode;
590
591 if (!set->ht && (set->used >= LYD_HT_MIN_ITEMS)) {
592 /* create hash table and add all the nodes */
593 set->ht = lyht_new(1, sizeof(struct lyxp_set_hash_node), set_values_equal_cb, NULL, 1);
594 for (i = 0; i < set->used; ++i) {
595 hnode.node = set->val.nodes[i].node;
596 hnode.type = set->val.nodes[i].type;
597
598 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
599 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
600 hash = dict_hash_multi(hash, NULL, 0);
601
602 r = lyht_insert(set->ht, &hnode, hash, NULL);
603 assert(!r);
604 (void)r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200605
Michal Vasko9d6befd2019-12-11 14:56:56 +0100606 if (hnode.node == node) {
607 /* it was just added, do not add it twice */
608 node = NULL;
609 }
610 }
611 }
612
613 if (set->ht && node) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200614 /* add the new node into hash table */
615 hnode.node = node;
616 hnode.type = type;
617
618 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
619 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
620 hash = dict_hash_multi(hash, NULL, 0);
621
622 r = lyht_insert(set->ht, &hnode, hash, NULL);
623 assert(!r);
624 (void)r;
625 }
626}
627
628/**
629 * @brief Remove node and its hash from set.
630 *
631 * @param[in] set Set to remove from.
632 * @param[in] node Node to remove.
633 * @param[in] type Node type.
634 */
635static void
636set_remove_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type)
637{
638 int r;
639 struct lyxp_set_hash_node hnode;
640 uint32_t hash;
641
642 if (set->ht) {
643 hnode.node = node;
644 hnode.type = type;
645
646 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
647 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
648 hash = dict_hash_multi(hash, NULL, 0);
649
650 r = lyht_remove(set->ht, &hnode, hash);
651 assert(!r);
652 (void)r;
653
654 if (!set->ht->used) {
655 lyht_free(set->ht);
656 set->ht = NULL;
657 }
658 }
659}
660
661/**
662 * @brief Check whether node is in set based on its hash.
663 *
664 * @param[in] set Set to search in.
665 * @param[in] node Node to search for.
666 * @param[in] type Node type.
667 * @param[in] skip_idx Index in @p set to skip.
668 * @return LY_ERR
669 */
670static LY_ERR
671set_dup_node_hash_check(const struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type, int skip_idx)
672{
673 struct lyxp_set_hash_node hnode, *match_p;
674 uint32_t hash;
675
676 hnode.node = node;
677 hnode.type = type;
678
679 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
680 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
681 hash = dict_hash_multi(hash, NULL, 0);
682
683 if (!lyht_find(set->ht, &hnode, hash, (void **)&match_p)) {
684 if ((skip_idx > -1) && (set->val.nodes[skip_idx].node == match_p->node) && (set->val.nodes[skip_idx].type == match_p->type)) {
685 /* we found it on the index that should be skipped, find another */
686 hnode = *match_p;
687 if (lyht_find_next(set->ht, &hnode, hash, (void **)&match_p)) {
688 /* none other found */
689 return LY_SUCCESS;
690 }
691 }
692
693 return LY_EEXIST;
694 }
695
696 /* not found */
697 return LY_SUCCESS;
698}
699
Michal Vaskod3678892020-05-21 10:06:58 +0200700void
701lyxp_set_free_content(struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200702{
703 if (!set) {
704 return;
705 }
706
707 if (set->type == LYXP_SET_NODE_SET) {
708 free(set->val.nodes);
709 lyht_free(set->ht);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200710 } else if (set->type == LYXP_SET_SCNODE_SET) {
711 free(set->val.scnodes);
Michal Vaskod3678892020-05-21 10:06:58 +0200712 lyht_free(set->ht);
713 } else {
714 if (set->type == LYXP_SET_STRING) {
715 free(set->val.str);
716 }
717 set->type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200718 }
Michal Vaskod3678892020-05-21 10:06:58 +0200719
720 set->val.nodes = NULL;
721 set->used = 0;
722 set->size = 0;
723 set->ht = NULL;
724 set->ctx_pos = 0;
725 set->ctx_pos = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200726}
727
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100728/**
729 * @brief Free dynamically-allocated set.
730 *
731 * @param[in] set Set to free.
732 */
733static void
Michal Vasko03ff5a72019-09-11 13:49:33 +0200734lyxp_set_free(struct lyxp_set *set)
735{
736 if (!set) {
737 return;
738 }
739
Michal Vaskod3678892020-05-21 10:06:58 +0200740 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200741 free(set);
742}
743
744/**
745 * @brief Initialize set context.
746 *
747 * @param[in] new Set to initialize.
748 * @param[in] set Arbitrary initialized set.
749 */
750static void
751set_init(struct lyxp_set *new, struct lyxp_set *set)
752{
753 memset(new, 0, sizeof *new);
Michal Vasko02a77382019-09-12 11:47:35 +0200754 if (set) {
755 new->ctx = set->ctx;
756 new->ctx_node = set->ctx_node;
Michal Vasko588112f2019-12-10 14:51:53 +0100757 new->root_type = set->root_type;
Michal Vasko02a77382019-09-12 11:47:35 +0200758 new->local_mod = set->local_mod;
Michal Vaskof03ed032020-03-04 13:31:44 +0100759 new->tree = set->tree;
Michal Vasko02a77382019-09-12 11:47:35 +0200760 new->format = set->format;
761 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200762}
763
764/**
765 * @brief Create a deep copy of a set.
766 *
767 * @param[in] set Set to copy.
768 * @return Copy of @p set.
769 */
770static struct lyxp_set *
771set_copy(struct lyxp_set *set)
772{
773 struct lyxp_set *ret;
774 uint16_t i;
Michal Vaskoba716542019-12-16 10:01:58 +0100775 int idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200776
777 if (!set) {
778 return NULL;
779 }
780
781 ret = malloc(sizeof *ret);
782 LY_CHECK_ERR_RET(!ret, LOGMEM(set->ctx), NULL);
783 set_init(ret, set);
784
785 if (set->type == LYXP_SET_SCNODE_SET) {
786 ret->type = set->type;
787
788 for (i = 0; i < set->used; ++i) {
Michal Vaskoba716542019-12-16 10:01:58 +0100789 if ((set->val.scnodes[i].in_ctx == 1) || (set->val.scnodes[i].in_ctx == -2)) {
790 idx = lyxp_set_scnode_insert_node(ret, set->val.scnodes[i].scnode, set->val.scnodes[i].type);
Michal Vasko3f27c522020-01-06 08:37:49 +0100791 /* coverity seems to think scnodes can be NULL */
792 if ((idx == -1) || !ret->val.scnodes) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200793 lyxp_set_free(ret);
794 return NULL;
795 }
Michal Vaskoba716542019-12-16 10:01:58 +0100796 ret->val.scnodes[idx].in_ctx = set->val.scnodes[i].in_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200797 }
798 }
799 } else if (set->type == LYXP_SET_NODE_SET) {
800 ret->type = set->type;
801 ret->val.nodes = malloc(set->used * sizeof *ret->val.nodes);
802 LY_CHECK_ERR_RET(!ret->val.nodes, LOGMEM(set->ctx); free(ret), NULL);
803 memcpy(ret->val.nodes, set->val.nodes, set->used * sizeof *ret->val.nodes);
804
805 ret->used = ret->size = set->used;
806 ret->ctx_pos = set->ctx_pos;
807 ret->ctx_size = set->ctx_size;
808 ret->ht = lyht_dup(set->ht);
809 } else {
810 memcpy(ret, set, sizeof *ret);
811 if (set->type == LYXP_SET_STRING) {
812 ret->val.str = strdup(set->val.str);
813 LY_CHECK_ERR_RET(!ret->val.str, LOGMEM(set->ctx); free(ret), NULL);
814 }
815 }
816
817 return ret;
818}
819
820/**
821 * @brief Fill XPath set with a string. Any current data are disposed of.
822 *
823 * @param[in] set Set to fill.
824 * @param[in] string String to fill into \p set.
825 * @param[in] str_len Length of \p string. 0 is a valid value!
826 */
827static void
828set_fill_string(struct lyxp_set *set, const char *string, uint16_t str_len)
829{
Michal Vaskod3678892020-05-21 10:06:58 +0200830 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200831
832 set->type = LYXP_SET_STRING;
833 if ((str_len == 0) && (string[0] != '\0')) {
834 string = "";
835 }
836 set->val.str = strndup(string, str_len);
837}
838
839/**
840 * @brief Fill XPath set with a number. Any current data are disposed of.
841 *
842 * @param[in] set Set to fill.
843 * @param[in] number Number to fill into \p set.
844 */
845static void
846set_fill_number(struct lyxp_set *set, long double number)
847{
Michal Vaskod3678892020-05-21 10:06:58 +0200848 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200849
850 set->type = LYXP_SET_NUMBER;
851 set->val.num = number;
852}
853
854/**
855 * @brief Fill XPath set with a boolean. Any current data are disposed of.
856 *
857 * @param[in] set Set to fill.
858 * @param[in] boolean Boolean to fill into \p set.
859 */
860static void
861set_fill_boolean(struct lyxp_set *set, int boolean)
862{
Michal Vaskod3678892020-05-21 10:06:58 +0200863 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200864
865 set->type = LYXP_SET_BOOLEAN;
866 set->val.bool = boolean;
867}
868
869/**
870 * @brief Fill XPath set with the value from another set (deep assign).
871 * Any current data are disposed of.
872 *
873 * @param[in] trg Set to fill.
874 * @param[in] src Source set to copy into \p trg.
875 */
876static void
877set_fill_set(struct lyxp_set *trg, struct lyxp_set *src)
878{
879 if (!trg || !src) {
880 return;
881 }
882
883 if (trg->type == LYXP_SET_NODE_SET) {
884 free(trg->val.nodes);
885 } else if (trg->type == LYXP_SET_STRING) {
886 free(trg->val.str);
887 }
888 set_init(trg, src);
889
890 if (src->type == LYXP_SET_SCNODE_SET) {
891 trg->type = LYXP_SET_SCNODE_SET;
892 trg->used = src->used;
893 trg->size = src->used;
894
895 trg->val.scnodes = ly_realloc(trg->val.scnodes, trg->size * sizeof *trg->val.scnodes);
896 LY_CHECK_ERR_RET(!trg->val.scnodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), );
897 memcpy(trg->val.scnodes, src->val.scnodes, src->used * sizeof *src->val.scnodes);
898 } else if (src->type == LYXP_SET_BOOLEAN) {
899 set_fill_boolean(trg, src->val.bool);
900 } else if (src->type == LYXP_SET_NUMBER) {
901 set_fill_number(trg, src->val.num);
902 } else if (src->type == LYXP_SET_STRING) {
903 set_fill_string(trg, src->val.str, strlen(src->val.str));
904 } else {
905 if (trg->type == LYXP_SET_NODE_SET) {
906 free(trg->val.nodes);
907 } else if (trg->type == LYXP_SET_STRING) {
908 free(trg->val.str);
909 }
910
Michal Vaskod3678892020-05-21 10:06:58 +0200911 assert(src->type == LYXP_SET_NODE_SET);
912
913 trg->type = LYXP_SET_NODE_SET;
914 trg->used = src->used;
915 trg->size = src->used;
916 trg->ctx_pos = src->ctx_pos;
917 trg->ctx_size = src->ctx_size;
918
919 trg->val.nodes = malloc(trg->used * sizeof *trg->val.nodes);
920 LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), );
921 memcpy(trg->val.nodes, src->val.nodes, src->used * sizeof *src->val.nodes);
922 if (src->ht) {
923 trg->ht = lyht_dup(src->ht);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200924 } else {
Michal Vaskod3678892020-05-21 10:06:58 +0200925 trg->ht = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200926 }
927 }
928}
929
930/**
931 * @brief Clear context of all schema nodes.
932 *
933 * @param[in] set Set to clear.
934 */
935static void
936set_scnode_clear_ctx(struct lyxp_set *set)
937{
938 uint32_t i;
939
940 for (i = 0; i < set->used; ++i) {
941 if (set->val.scnodes[i].in_ctx == 1) {
942 set->val.scnodes[i].in_ctx = 0;
Michal Vasko5c4e5892019-11-14 12:31:38 +0100943 } else if (set->val.scnodes[i].in_ctx == -2) {
944 set->val.scnodes[i].in_ctx = -1;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200945 }
946 }
947}
948
949/**
950 * @brief Remove a node from a set. Removing last node changes
951 * set into LYXP_SET_EMPTY. Context position aware.
952 *
953 * @param[in] set Set to use.
954 * @param[in] idx Index from @p set of the node to be removed.
955 */
956static void
957set_remove_node(struct lyxp_set *set, uint32_t idx)
958{
959 assert(set && (set->type == LYXP_SET_NODE_SET));
960 assert(idx < set->used);
961
962 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
963
964 --set->used;
965 if (set->used) {
966 memmove(&set->val.nodes[idx], &set->val.nodes[idx + 1],
967 (set->used - idx) * sizeof *set->val.nodes);
968 } else {
Michal Vaskod3678892020-05-21 10:06:58 +0200969 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200970 }
971}
972
973/**
Michal Vasko2caefc12019-11-14 16:07:56 +0100974 * @brief Remove a node from a set by setting its type to LYXP_NODE_NONE.
Michal Vasko57eab132019-09-24 11:46:26 +0200975 *
976 * @param[in] set Set to use.
977 * @param[in] idx Index from @p set of the node to be removed.
978 */
979static void
Michal Vasko2caefc12019-11-14 16:07:56 +0100980set_remove_node_none(struct lyxp_set *set, uint32_t idx)
Michal Vasko57eab132019-09-24 11:46:26 +0200981{
982 assert(set && (set->type == LYXP_SET_NODE_SET));
983 assert(idx < set->used);
984
Michal Vasko2caefc12019-11-14 16:07:56 +0100985 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
986 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
987 }
988 set->val.nodes[idx].type = LYXP_NODE_NONE;
Michal Vasko57eab132019-09-24 11:46:26 +0200989}
990
991/**
Michal Vasko2caefc12019-11-14 16:07:56 +0100992 * @brief Remove all LYXP_NODE_NONE nodes from a set. Removing last node changes
Michal Vasko57eab132019-09-24 11:46:26 +0200993 * set into LYXP_SET_EMPTY. Context position aware.
994 *
995 * @param[in] set Set to consolidate.
996 */
997static void
Michal Vasko2caefc12019-11-14 16:07:56 +0100998set_remove_nodes_none(struct lyxp_set *set)
Michal Vasko57eab132019-09-24 11:46:26 +0200999{
1000 uint16_t i, orig_used, end;
1001 int32_t start;
1002
Michal Vaskod3678892020-05-21 10:06:58 +02001003 assert(set);
Michal Vasko57eab132019-09-24 11:46:26 +02001004
1005 orig_used = set->used;
1006 set->used = 0;
1007 for (i = 0; i < orig_used;) {
1008 start = -1;
1009 do {
Michal Vasko2caefc12019-11-14 16:07:56 +01001010 if ((set->val.nodes[i].type != LYXP_NODE_NONE) && (start == -1)) {
Michal Vasko57eab132019-09-24 11:46:26 +02001011 start = i;
Michal Vasko2caefc12019-11-14 16:07:56 +01001012 } else if ((start > -1) && (set->val.nodes[i].type == LYXP_NODE_NONE)) {
Michal Vasko57eab132019-09-24 11:46:26 +02001013 end = i;
1014 ++i;
1015 break;
1016 }
1017
1018 ++i;
1019 if (i == orig_used) {
1020 end = i;
1021 }
1022 } while (i < orig_used);
1023
1024 if (start > -1) {
1025 /* move the whole chunk of valid nodes together */
1026 if (set->used != (unsigned)start) {
1027 memmove(&set->val.nodes[set->used], &set->val.nodes[start], (end - start) * sizeof *set->val.nodes);
1028 }
1029 set->used += end - start;
1030 }
1031 }
Michal Vasko57eab132019-09-24 11:46:26 +02001032}
1033
1034/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02001035 * @brief Check for duplicates in a node set.
1036 *
1037 * @param[in] set Set to check.
1038 * @param[in] node Node to look for in @p set.
1039 * @param[in] node_type Type of @p node.
1040 * @param[in] skip_idx Index from @p set to skip.
1041 * @return LY_ERR
1042 */
1043static LY_ERR
1044set_dup_node_check(const struct lyxp_set *set, const struct lyd_node *node, enum lyxp_node_type node_type, int skip_idx)
1045{
1046 uint32_t i;
1047
Michal Vasko2caefc12019-11-14 16:07:56 +01001048 if (set->ht && node) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001049 return set_dup_node_hash_check(set, (struct lyd_node *)node, node_type, skip_idx);
1050 }
1051
1052 for (i = 0; i < set->used; ++i) {
1053 if ((skip_idx > -1) && (i == (unsigned)skip_idx)) {
1054 continue;
1055 }
1056
1057 if ((set->val.nodes[i].node == node) && (set->val.nodes[i].type == node_type)) {
1058 return LY_EEXIST;
1059 }
1060 }
1061
1062 return LY_SUCCESS;
1063}
1064
Michal Vaskoecd62de2019-11-13 12:35:11 +01001065int
1066lyxp_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 +02001067{
1068 uint32_t i;
1069
1070 for (i = 0; i < set->used; ++i) {
1071 if ((skip_idx > -1) && (i == (unsigned)skip_idx)) {
1072 continue;
1073 }
1074
1075 if ((set->val.scnodes[i].scnode == node) && (set->val.scnodes[i].type == node_type)) {
1076 return i;
1077 }
1078 }
1079
1080 return -1;
1081}
1082
Michal Vaskoecd62de2019-11-13 12:35:11 +01001083void
1084lyxp_set_scnode_merge(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001085{
1086 uint32_t orig_used, i, j;
1087
Michal Vaskod3678892020-05-21 10:06:58 +02001088 assert((set1->type == LYXP_SET_SCNODE_SET) && (set2->type == LYXP_SET_SCNODE_SET));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001089
Michal Vaskod3678892020-05-21 10:06:58 +02001090 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001091 return;
1092 }
1093
Michal Vaskod3678892020-05-21 10:06:58 +02001094 if (!set1->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001095 memcpy(set1, set2, sizeof *set1);
1096 return;
1097 }
1098
1099 if (set1->used + set2->used > set1->size) {
1100 set1->size = set1->used + set2->used;
1101 set1->val.scnodes = ly_realloc(set1->val.scnodes, set1->size * sizeof *set1->val.scnodes);
1102 LY_CHECK_ERR_RET(!set1->val.scnodes, LOGMEM(set1->ctx), );
1103 }
1104
1105 orig_used = set1->used;
1106
1107 for (i = 0; i < set2->used; ++i) {
1108 for (j = 0; j < orig_used; ++j) {
1109 /* detect duplicities */
1110 if (set1->val.scnodes[j].scnode == set2->val.scnodes[i].scnode) {
1111 break;
1112 }
1113 }
1114
1115 if (j == orig_used) {
1116 memcpy(&set1->val.scnodes[set1->used], &set2->val.scnodes[i], sizeof *set2->val.scnodes);
1117 ++set1->used;
1118 }
1119 }
1120
Michal Vaskod3678892020-05-21 10:06:58 +02001121 lyxp_set_free_content(set2);
1122 set2->type = LYXP_SET_SCNODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001123}
1124
1125/**
1126 * @brief Insert a node into a set. Context position aware.
1127 *
1128 * @param[in] set Set to use.
1129 * @param[in] node Node to insert to @p set.
1130 * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting.
1131 * @param[in] node_type Node type of @p node.
1132 * @param[in] idx Index in @p set to insert into.
1133 */
1134static void
1135set_insert_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx)
1136{
Michal Vaskod3678892020-05-21 10:06:58 +02001137 assert(set && (set->type == LYXP_SET_NODE_SET));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001138
Michal Vaskod3678892020-05-21 10:06:58 +02001139 if (!set->size) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001140 /* first item */
1141 if (idx) {
1142 /* no real harm done, but it is a bug */
1143 LOGINT(set->ctx);
1144 idx = 0;
1145 }
1146 set->val.nodes = malloc(LYXP_SET_SIZE_START * sizeof *set->val.nodes);
1147 LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), );
1148 set->type = LYXP_SET_NODE_SET;
1149 set->used = 0;
1150 set->size = LYXP_SET_SIZE_START;
1151 set->ctx_pos = 1;
1152 set->ctx_size = 1;
1153 set->ht = NULL;
1154 } else {
1155 /* not an empty set */
1156 if (set->used == set->size) {
1157
1158 /* set is full */
1159 set->val.nodes = ly_realloc(set->val.nodes, (set->size + LYXP_SET_SIZE_STEP) * sizeof *set->val.nodes);
1160 LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), );
1161 set->size += LYXP_SET_SIZE_STEP;
1162 }
1163
1164 if (idx > set->used) {
1165 LOGINT(set->ctx);
1166 idx = set->used;
1167 }
1168
1169 /* make space for the new node */
1170 if (idx < set->used) {
1171 memmove(&set->val.nodes[idx + 1], &set->val.nodes[idx], (set->used - idx) * sizeof *set->val.nodes);
1172 }
1173 }
1174
1175 /* finally assign the value */
1176 set->val.nodes[idx].node = (struct lyd_node *)node;
1177 set->val.nodes[idx].type = node_type;
1178 set->val.nodes[idx].pos = pos;
1179 ++set->used;
1180
Michal Vasko2caefc12019-11-14 16:07:56 +01001181 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1182 set_insert_node_hash(set, (struct lyd_node *)node, node_type);
1183 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001184}
1185
Michal Vaskoecd62de2019-11-13 12:35:11 +01001186int
1187lyxp_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 +02001188{
1189 int ret;
1190
1191 assert(set->type == LYXP_SET_SCNODE_SET);
1192
Michal Vaskoecd62de2019-11-13 12:35:11 +01001193 ret = lyxp_set_scnode_dup_node_check(set, node, node_type, -1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001194 if (ret > -1) {
1195 set->val.scnodes[ret].in_ctx = 1;
1196 } else {
1197 if (set->used == set->size) {
1198 set->val.scnodes = ly_realloc(set->val.scnodes, (set->size + LYXP_SET_SIZE_STEP) * sizeof *set->val.scnodes);
1199 LY_CHECK_ERR_RET(!set->val.scnodes, LOGMEM(set->ctx), -1);
1200 set->size += LYXP_SET_SIZE_STEP;
1201 }
1202
1203 ret = set->used;
1204 set->val.scnodes[ret].scnode = (struct lysc_node *)node;
1205 set->val.scnodes[ret].type = node_type;
1206 set->val.scnodes[ret].in_ctx = 1;
1207 ++set->used;
1208 }
1209
1210 return ret;
1211}
1212
1213/**
1214 * @brief Replace a node in a set with another. Context position aware.
1215 *
1216 * @param[in] set Set to use.
1217 * @param[in] node Node to insert to @p set.
1218 * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting.
1219 * @param[in] node_type Node type of @p node.
1220 * @param[in] idx Index in @p set of the node to replace.
1221 */
1222static void
1223set_replace_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx)
1224{
1225 assert(set && (idx < set->used));
1226
Michal Vasko2caefc12019-11-14 16:07:56 +01001227 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1228 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1229 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001230 set->val.nodes[idx].node = (struct lyd_node *)node;
1231 set->val.nodes[idx].type = node_type;
1232 set->val.nodes[idx].pos = pos;
Michal Vasko2caefc12019-11-14 16:07:56 +01001233 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1234 set_insert_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1235 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001236}
1237
1238/**
1239 * @brief Set all nodes with ctx 1 to a new unique context value.
1240 *
1241 * @param[in] set Set to modify.
1242 * @return New context value.
1243 */
Michal Vasko5c4e5892019-11-14 12:31:38 +01001244static int32_t
Michal Vasko03ff5a72019-09-11 13:49:33 +02001245set_scnode_new_in_ctx(struct lyxp_set *set)
1246{
Michal Vasko5c4e5892019-11-14 12:31:38 +01001247 uint32_t i;
1248 int32_t ret_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001249
1250 assert(set->type == LYXP_SET_SCNODE_SET);
1251
1252 ret_ctx = 3;
1253retry:
1254 for (i = 0; i < set->used; ++i) {
1255 if (set->val.scnodes[i].in_ctx >= ret_ctx) {
1256 ret_ctx = set->val.scnodes[i].in_ctx + 1;
1257 goto retry;
1258 }
1259 }
1260 for (i = 0; i < set->used; ++i) {
1261 if (set->val.scnodes[i].in_ctx == 1) {
1262 set->val.scnodes[i].in_ctx = ret_ctx;
1263 }
1264 }
1265
1266 return ret_ctx;
1267}
1268
1269/**
1270 * @brief Get unique @p node position in the data.
1271 *
1272 * @param[in] node Node to find.
1273 * @param[in] node_type Node type of @p node.
1274 * @param[in] root Root node.
1275 * @param[in] root_type Type of the XPath @p root node.
1276 * @param[in] prev Node that we think is before @p node in DFS from @p root. Can optionally
1277 * be used to increase efficiency and start the DFS from this node.
1278 * @param[in] prev_pos Node @p prev position. Optional, but must be set if @p prev is set.
1279 * @return Node position.
1280 */
1281static uint32_t
1282get_node_pos(const struct lyd_node *node, enum lyxp_node_type node_type, const struct lyd_node *root,
1283 enum lyxp_node_type root_type, const struct lyd_node **prev, uint32_t *prev_pos)
1284{
1285 const struct lyd_node *next, *elem, *top_sibling;
1286 uint32_t pos = 1;
1287
1288 assert(prev && prev_pos && !root->prev->next);
1289
1290 if ((node_type == LYXP_NODE_ROOT) || (node_type == LYXP_NODE_ROOT_CONFIG)) {
1291 return 0;
1292 }
1293
1294 if (*prev) {
1295 /* start from the previous element instead from the root */
1296 elem = next = *prev;
1297 pos = *prev_pos;
1298 for (top_sibling = elem; top_sibling->parent; top_sibling = (struct lyd_node *)top_sibling->parent);
1299 goto dfs_search;
1300 }
1301
1302 for (top_sibling = root; top_sibling; top_sibling = top_sibling->next) {
1303 /* TREE DFS */
1304 LYD_TREE_DFS_BEGIN(top_sibling, next, elem) {
1305dfs_search:
1306 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (elem->schema->flags & LYS_CONFIG_R)) {
1307 goto skip_children;
1308 }
1309
1310 if (elem == node) {
1311 break;
1312 }
1313 ++pos;
1314
1315 /* TREE DFS END */
1316 /* select element for the next run - children first,
1317 * child exception for lyd_node_leaf and lyd_node_leaflist, but not the root */
1318 if (elem->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) {
1319 next = NULL;
1320 } else {
1321 next = lyd_node_children(elem);
1322 }
1323 if (!next) {
1324skip_children:
1325 /* no children */
1326 if (elem == top_sibling) {
1327 /* we are done, root has no children */
1328 elem = NULL;
1329 break;
1330 }
1331 /* try siblings */
1332 next = elem->next;
1333 }
1334 while (!next) {
1335 /* no siblings, go back through parents */
1336 if (elem->parent == top_sibling->parent) {
1337 /* we are done, no next element to process */
1338 elem = NULL;
1339 break;
1340 }
1341 /* parent is already processed, go to its sibling */
1342 elem = (struct lyd_node *)elem->parent;
1343 next = elem->next;
1344 }
1345 }
1346
1347 /* node found */
1348 if (elem) {
1349 break;
1350 }
1351 }
1352
1353 if (!elem) {
1354 if (!(*prev)) {
1355 /* we went from root and failed to find it, cannot be */
1356 LOGINT(node->schema->module->ctx);
1357 return 0;
1358 } else {
1359 *prev = NULL;
1360 *prev_pos = 0;
1361
1362 elem = next = top_sibling = root;
1363 pos = 1;
1364 goto dfs_search;
1365 }
1366 }
1367
1368 /* remember the last found node for next time */
1369 *prev = node;
1370 *prev_pos = pos;
1371
1372 return pos;
1373}
1374
1375/**
1376 * @brief Assign (fill) missing node positions.
1377 *
1378 * @param[in] set Set to fill positions in.
1379 * @param[in] root Context root node.
1380 * @param[in] root_type Context root type.
1381 * @return LY_ERR
1382 */
1383static LY_ERR
1384set_assign_pos(struct lyxp_set *set, const struct lyd_node *root, enum lyxp_node_type root_type)
1385{
1386 const struct lyd_node *prev = NULL, *tmp_node;
1387 uint32_t i, tmp_pos = 0;
1388
1389 for (i = 0; i < set->used; ++i) {
1390 if (!set->val.nodes[i].pos) {
1391 tmp_node = NULL;
1392 switch (set->val.nodes[i].type) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001393 case LYXP_NODE_META:
1394 tmp_node = set->val.meta[i].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001395 if (!tmp_node) {
1396 LOGINT_RET(root->schema->module->ctx);
1397 }
1398 /* fallthrough */
1399 case LYXP_NODE_ELEM:
1400 case LYXP_NODE_TEXT:
1401 if (!tmp_node) {
1402 tmp_node = set->val.nodes[i].node;
1403 }
1404 set->val.nodes[i].pos = get_node_pos(tmp_node, set->val.nodes[i].type, root, root_type, &prev, &tmp_pos);
1405 break;
1406 default:
1407 /* all roots have position 0 */
1408 break;
1409 }
1410 }
1411 }
1412
1413 return LY_SUCCESS;
1414}
1415
1416/**
Michal Vasko9f96a052020-03-10 09:41:45 +01001417 * @brief Get unique @p meta position in the parent metadata.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001418 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001419 * @param[in] meta Metadata to use.
1420 * @return Metadata position.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001421 */
1422static uint16_t
Michal Vasko9f96a052020-03-10 09:41:45 +01001423get_meta_pos(struct lyd_meta *meta)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001424{
1425 uint16_t pos = 0;
Michal Vasko9f96a052020-03-10 09:41:45 +01001426 struct lyd_meta *meta2;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001427
Michal Vasko9f96a052020-03-10 09:41:45 +01001428 for (meta2 = meta->parent->meta; meta2 && (meta2 != meta); meta2 = meta2->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001429 ++pos;
1430 }
1431
Michal Vasko9f96a052020-03-10 09:41:45 +01001432 assert(meta2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001433 return pos;
1434}
1435
1436/**
1437 * @brief Compare 2 nodes in respect to XPath document order.
1438 *
1439 * @param[in] item1 1st node.
1440 * @param[in] item2 2nd node.
1441 * @return If 1st > 2nd returns 1, 1st == 2nd returns 0, and 1st < 2nd returns -1.
1442 */
1443static int
1444set_sort_compare(struct lyxp_set_node *item1, struct lyxp_set_node *item2)
1445{
Michal Vasko9f96a052020-03-10 09:41:45 +01001446 uint32_t meta_pos1 = 0, meta_pos2 = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001447
1448 if (item1->pos < item2->pos) {
1449 return -1;
1450 }
1451
1452 if (item1->pos > item2->pos) {
1453 return 1;
1454 }
1455
1456 /* node positions are equal, the fun case */
1457
1458 /* 1st ELEM - == - 2nd TEXT, 1st TEXT - == - 2nd ELEM */
1459 /* special case since text nodes are actually saved as their parents */
1460 if ((item1->node == item2->node) && (item1->type != item2->type)) {
1461 if (item1->type == LYXP_NODE_ELEM) {
1462 assert(item2->type == LYXP_NODE_TEXT);
1463 return -1;
1464 } else {
1465 assert((item1->type == LYXP_NODE_TEXT) && (item2->type == LYXP_NODE_ELEM));
1466 return 1;
1467 }
1468 }
1469
Michal Vasko9f96a052020-03-10 09:41:45 +01001470 /* we need meta positions now */
1471 if (item1->type == LYXP_NODE_META) {
1472 meta_pos1 = get_meta_pos((struct lyd_meta *)item1->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001473 }
Michal Vasko9f96a052020-03-10 09:41:45 +01001474 if (item2->type == LYXP_NODE_META) {
1475 meta_pos2 = get_meta_pos((struct lyd_meta *)item2->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001476 }
1477
Michal Vasko9f96a052020-03-10 09:41:45 +01001478 /* 1st ROOT - 2nd ROOT, 1st ELEM - 2nd ELEM, 1st TEXT - 2nd TEXT, 1st META - =pos= - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001479 /* check for duplicates */
1480 if (item1->node == item2->node) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001481 assert((item1->type == item2->type) && ((item1->type != LYXP_NODE_META) || (meta_pos1 == meta_pos2)));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001482 return 0;
1483 }
1484
Michal Vasko9f96a052020-03-10 09:41:45 +01001485 /* 1st ELEM - 2nd TEXT, 1st ELEM - any pos - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001486 /* elem is always first, 2nd node is after it */
1487 if (item1->type == LYXP_NODE_ELEM) {
1488 assert(item2->type != LYXP_NODE_ELEM);
1489 return -1;
1490 }
1491
Michal Vasko9f96a052020-03-10 09:41:45 +01001492 /* 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 +02001493 /* 2nd is before 1st */
1494 if (((item1->type == LYXP_NODE_TEXT)
Michal Vasko9f96a052020-03-10 09:41:45 +01001495 && ((item2->type == LYXP_NODE_ELEM) || (item2->type == LYXP_NODE_META)))
1496 || ((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_ELEM))
1497 || (((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_META))
1498 && (meta_pos1 > meta_pos2))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001499 return 1;
1500 }
1501
Michal Vasko9f96a052020-03-10 09:41:45 +01001502 /* 1st META - any pos - 2nd TEXT, 1st META <pos< - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001503 /* 2nd is after 1st */
1504 return -1;
1505}
1506
1507/**
1508 * @brief Set cast for comparisons.
1509 *
1510 * @param[in] trg Target set to cast source into.
1511 * @param[in] src Source set.
1512 * @param[in] type Target set type.
1513 * @param[in] src_idx Source set node index.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001514 * @return LY_ERR
1515 */
1516static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001517set_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 +02001518{
1519 assert(src->type == LYXP_SET_NODE_SET);
1520
1521 set_init(trg, src);
1522
1523 /* insert node into target set */
1524 set_insert_node(trg, src->val.nodes[src_idx].node, src->val.nodes[src_idx].pos, src->val.nodes[src_idx].type, 0);
1525
1526 /* cast target set appropriately */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001527 return lyxp_set_cast(trg, type);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001528}
1529
1530#ifndef NDEBUG
1531
1532/**
1533 * @brief Bubble sort @p set into XPath document order.
1534 * Context position aware. Unused in the 'Release' build target.
1535 *
1536 * @param[in] set Set to sort.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001537 * @return How many times the whole set was traversed - 1 (if set was sorted, returns 0).
1538 */
1539static int
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001540set_sort(struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001541{
1542 uint32_t i, j;
1543 int ret = 0, cmp, inverted, change;
1544 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001545 struct lyxp_set_node item;
1546 struct lyxp_set_hash_node hnode;
1547 uint64_t hash;
1548
Michal Vasko3cf8fbf2020-05-27 15:21:21 +02001549 if ((set->type != LYXP_SET_NODE_SET) || (set->used < 2)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001550 return 0;
1551 }
1552
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001553 /* find first top-level node to be used as anchor for positions */
1554 for (root = set->ctx_node; root->parent; root = (const struct lyd_node *)root->parent);
1555 for (; root->prev->next; root = root->prev);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001556
1557 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001558 if (set_assign_pos(set, root, set->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001559 return -1;
1560 }
1561
1562 LOGDBG(LY_LDGXPATH, "SORT BEGIN");
1563 print_set_debug(set);
1564
1565 for (i = 0; i < set->used; ++i) {
1566 inverted = 0;
1567 change = 0;
1568
1569 for (j = 1; j < set->used - i; ++j) {
1570 /* compare node positions */
1571 if (inverted) {
1572 cmp = set_sort_compare(&set->val.nodes[j], &set->val.nodes[j - 1]);
1573 } else {
1574 cmp = set_sort_compare(&set->val.nodes[j - 1], &set->val.nodes[j]);
1575 }
1576
1577 /* swap if needed */
1578 if ((inverted && (cmp < 0)) || (!inverted && (cmp > 0))) {
1579 change = 1;
1580
1581 item = set->val.nodes[j - 1];
1582 set->val.nodes[j - 1] = set->val.nodes[j];
1583 set->val.nodes[j] = item;
1584 } else {
1585 /* whether node_pos1 should be smaller than node_pos2 or the other way around */
1586 inverted = !inverted;
1587 }
1588 }
1589
1590 ++ret;
1591
1592 if (!change) {
1593 break;
1594 }
1595 }
1596
1597 LOGDBG(LY_LDGXPATH, "SORT END %d", ret);
1598 print_set_debug(set);
1599
1600 /* check node hashes */
1601 if (set->used >= LYD_HT_MIN_ITEMS) {
1602 assert(set->ht);
1603 for (i = 0; i < set->used; ++i) {
1604 hnode.node = set->val.nodes[i].node;
1605 hnode.type = set->val.nodes[i].type;
1606
1607 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
1608 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
1609 hash = dict_hash_multi(hash, NULL, 0);
1610
1611 assert(!lyht_find(set->ht, &hnode, hash, NULL));
1612 }
1613 }
1614
1615 return ret - 1;
1616}
1617
1618/**
1619 * @brief Remove duplicate entries in a sorted node set.
1620 *
1621 * @param[in] set Sorted set to check.
1622 * @return LY_ERR (LY_EEXIST if some duplicates are found)
1623 */
1624static LY_ERR
1625set_sorted_dup_node_clean(struct lyxp_set *set)
1626{
1627 uint32_t i = 0;
1628 LY_ERR ret = LY_SUCCESS;
1629
1630 if (set->used > 1) {
1631 while (i < set->used - 1) {
1632 if ((set->val.nodes[i].node == set->val.nodes[i + 1].node)
1633 && (set->val.nodes[i].type == set->val.nodes[i + 1].type)) {
Michal Vasko2caefc12019-11-14 16:07:56 +01001634 set_remove_node_none(set, i + 1);
Michal Vasko57eab132019-09-24 11:46:26 +02001635 ret = LY_EEXIST;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001636 }
Michal Vasko57eab132019-09-24 11:46:26 +02001637 ++i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001638 }
1639 }
1640
Michal Vasko2caefc12019-11-14 16:07:56 +01001641 set_remove_nodes_none(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001642 return ret;
1643}
1644
1645#endif
1646
1647/**
1648 * @brief Merge 2 sorted sets into one.
1649 *
1650 * @param[in,out] trg Set to merge into. Duplicates are removed.
1651 * @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 +02001652 * @return LY_ERR
1653 */
1654static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001655set_sorted_merge(struct lyxp_set *trg, struct lyxp_set *src)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001656{
1657 uint32_t i, j, k, count, dup_count;
1658 int cmp;
1659 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001660
Michal Vaskod3678892020-05-21 10:06:58 +02001661 if ((trg->type != LYXP_SET_NODE_SET) || (src->type != LYXP_SET_NODE_SET)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001662 return LY_EINVAL;
1663 }
1664
Michal Vaskod3678892020-05-21 10:06:58 +02001665 if (!src->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001666 return LY_SUCCESS;
Michal Vaskod3678892020-05-21 10:06:58 +02001667 } else if (!trg->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001668 set_fill_set(trg, src);
Michal Vaskod3678892020-05-21 10:06:58 +02001669 lyxp_set_free_content(src);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001670 return LY_SUCCESS;
1671 }
1672
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001673 /* find first top-level node to be used as anchor for positions */
1674 for (root = trg->ctx_node; root->parent; root = (const struct lyd_node *)root->parent);
1675 for (; root->prev->next; root = root->prev);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001676
1677 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001678 if (set_assign_pos(trg, root, trg->root_type) || set_assign_pos(src, root, src->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001679 return LY_EINT;
1680 }
1681
1682#ifndef NDEBUG
1683 LOGDBG(LY_LDGXPATH, "MERGE target");
1684 print_set_debug(trg);
1685 LOGDBG(LY_LDGXPATH, "MERGE source");
1686 print_set_debug(src);
1687#endif
1688
1689 /* make memory for the merge (duplicates are not detected yet, so space
1690 * will likely be wasted on them, too bad) */
1691 if (trg->size - trg->used < src->used) {
1692 trg->size = trg->used + src->used;
1693
1694 trg->val.nodes = ly_realloc(trg->val.nodes, trg->size * sizeof *trg->val.nodes);
1695 LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx), LY_EMEM);
1696 }
1697
1698 i = 0;
1699 j = 0;
1700 count = 0;
1701 dup_count = 0;
1702 do {
1703 cmp = set_sort_compare(&src->val.nodes[i], &trg->val.nodes[j]);
1704 if (!cmp) {
1705 if (!count) {
1706 /* duplicate, just skip it */
1707 ++i;
1708 ++j;
1709 } else {
1710 /* we are copying something already, so let's copy the duplicate too,
1711 * we are hoping that afterwards there are some more nodes to
1712 * copy and this way we can copy them all together */
1713 ++count;
1714 ++dup_count;
1715 ++i;
1716 ++j;
1717 }
1718 } else if (cmp < 0) {
1719 /* inserting src node into trg, just remember it for now */
1720 ++count;
1721 ++i;
1722
1723 /* insert the hash now */
1724 set_insert_node_hash(trg, src->val.nodes[i - 1].node, src->val.nodes[i - 1].type);
1725 } else if (count) {
1726copy_nodes:
1727 /* time to actually copy the nodes, we have found the largest block of nodes */
1728 memmove(&trg->val.nodes[j + (count - dup_count)],
1729 &trg->val.nodes[j],
1730 (trg->used - j) * sizeof *trg->val.nodes);
1731 memcpy(&trg->val.nodes[j - dup_count], &src->val.nodes[i - count], count * sizeof *src->val.nodes);
1732
1733 trg->used += count - dup_count;
1734 /* do not change i, except the copying above, we are basically doing exactly what is in the else branch below */
1735 j += count - dup_count;
1736
1737 count = 0;
1738 dup_count = 0;
1739 } else {
1740 ++j;
1741 }
1742 } while ((i < src->used) && (j < trg->used));
1743
1744 if ((i < src->used) || count) {
1745 /* insert all the hashes first */
1746 for (k = i; k < src->used; ++k) {
1747 set_insert_node_hash(trg, src->val.nodes[k].node, src->val.nodes[k].type);
1748 }
1749
1750 /* loop ended, but we need to copy something at trg end */
1751 count += src->used - i;
1752 i = src->used;
1753 goto copy_nodes;
1754 }
1755
1756 /* we are inserting hashes before the actual node insert, which causes
1757 * situations when there were initially not enough items for a hash table,
1758 * but even after some were inserted, hash table was not created (during
1759 * insertion the number of items is not updated yet) */
1760 if (!trg->ht && (trg->used >= LYD_HT_MIN_ITEMS)) {
1761 set_insert_node_hash(trg, NULL, 0);
1762 }
1763
1764#ifndef NDEBUG
1765 LOGDBG(LY_LDGXPATH, "MERGE result");
1766 print_set_debug(trg);
1767#endif
1768
Michal Vaskod3678892020-05-21 10:06:58 +02001769 lyxp_set_free_content(src);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001770 return LY_SUCCESS;
1771}
1772
1773/*
1774 * (re)parse functions
1775 *
1776 * Parse functions parse the expression into
1777 * tokens (syntactic analysis).
1778 *
1779 * Reparse functions perform semantic analysis
1780 * (do not save the result, just a check) of
1781 * the expression and fill repeat indices.
1782 */
1783
Michal Vasko14676352020-05-29 11:35:55 +02001784LY_ERR
1785lyxp_check_token(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t exp_idx, enum lyxp_token want_tok)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001786{
1787 if (exp->used == exp_idx) {
Michal Vasko14676352020-05-29 11:35:55 +02001788 if (ctx) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001789 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOF);
1790 }
Michal Vasko14676352020-05-29 11:35:55 +02001791 return LY_EINCOMPLETE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001792 }
1793
1794 if (want_tok && (exp->tokens[exp_idx] != want_tok)) {
Michal Vasko14676352020-05-29 11:35:55 +02001795 if (ctx) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001796 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK,
1797 print_token(exp->tokens[exp_idx]), &exp->expr[exp->tok_pos[exp_idx]]);
1798 }
Michal Vasko14676352020-05-29 11:35:55 +02001799 return LY_ENOT;
1800 }
1801
1802 return LY_SUCCESS;
1803}
1804
1805/* just like lyxp_check_token() but tests for 2 tokens */
1806static LY_ERR
1807exp_check_token2(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t exp_idx, enum lyxp_token want_tok1,
1808 enum lyxp_token want_tok2)
1809{
1810 if (exp->used == exp_idx) {
1811 if (ctx) {
1812 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOF);
1813 }
1814 return LY_EINCOMPLETE;
1815 }
1816
1817 if ((exp->tokens[exp_idx] != want_tok1) && (exp->tokens[exp_idx] != want_tok2)) {
1818 if (ctx) {
1819 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK,
1820 print_token(exp->tokens[exp_idx]), &exp->expr[exp->tok_pos[exp_idx]]);
1821 }
1822 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001823 }
1824
1825 return LY_SUCCESS;
1826}
1827
1828/**
1829 * @brief Stack operation push on the repeat array.
1830 *
1831 * @param[in] exp Expression to use.
1832 * @param[in] exp_idx Position in the expresion \p exp.
1833 * @param[in] repeat_op_idx Index from \p exp of the operator token. This value is pushed.
1834 */
1835static void
1836exp_repeat_push(struct lyxp_expr *exp, uint16_t exp_idx, uint16_t repeat_op_idx)
1837{
1838 uint16_t i;
1839
1840 if (exp->repeat[exp_idx]) {
1841 for (i = 0; exp->repeat[exp_idx][i]; ++i);
1842 exp->repeat[exp_idx] = realloc(exp->repeat[exp_idx], (i + 2) * sizeof *exp->repeat[exp_idx]);
1843 LY_CHECK_ERR_RET(!exp->repeat[exp_idx], LOGMEM(NULL), );
1844 exp->repeat[exp_idx][i] = repeat_op_idx;
1845 exp->repeat[exp_idx][i + 1] = 0;
1846 } else {
1847 exp->repeat[exp_idx] = calloc(2, sizeof *exp->repeat[exp_idx]);
1848 LY_CHECK_ERR_RET(!exp->repeat[exp_idx], LOGMEM(NULL), );
1849 exp->repeat[exp_idx][0] = repeat_op_idx;
1850 }
1851}
1852
1853/**
1854 * @brief Reparse Predicate. Logs directly on error.
1855 *
1856 * [7] Predicate ::= '[' Expr ']'
1857 *
1858 * @param[in] ctx Context for logging.
1859 * @param[in] exp Parsed XPath expression.
1860 * @param[in] exp_idx Position in the expression @p exp.
1861 * @return LY_ERR
1862 */
1863static LY_ERR
Michal Vasko14676352020-05-29 11:35:55 +02001864reparse_predicate(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *exp_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001865{
1866 LY_ERR rc;
1867
Michal Vasko14676352020-05-29 11:35:55 +02001868 rc = lyxp_check_token(ctx, exp, *exp_idx, LYXP_TOKEN_BRACK1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001869 LY_CHECK_RET(rc);
1870 ++(*exp_idx);
1871
1872 rc = reparse_or_expr(ctx, exp, exp_idx);
1873 LY_CHECK_RET(rc);
1874
Michal Vasko14676352020-05-29 11:35:55 +02001875 rc = lyxp_check_token(ctx, exp, *exp_idx, LYXP_TOKEN_BRACK2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001876 LY_CHECK_RET(rc);
1877 ++(*exp_idx);
1878
1879 return LY_SUCCESS;
1880}
1881
1882/**
1883 * @brief Reparse RelativeLocationPath. Logs directly on error.
1884 *
1885 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
1886 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
1887 * [6] NodeTest ::= NameTest | NodeType '(' ')'
1888 *
1889 * @param[in] ctx Context for logging.
1890 * @param[in] exp Parsed XPath expression.
1891 * @param[in] exp_idx Position in the expression \p exp.
1892 * @return LY_ERR (LY_EINCOMPLETE on forward reference)
1893 */
1894static LY_ERR
Michal Vasko14676352020-05-29 11:35:55 +02001895reparse_relative_location_path(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *exp_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001896{
1897 LY_ERR rc;
1898
Michal Vasko14676352020-05-29 11:35:55 +02001899 rc = lyxp_check_token(ctx, exp, *exp_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001900 LY_CHECK_RET(rc);
1901
1902 goto step;
1903 do {
1904 /* '/' or '//' */
1905 ++(*exp_idx);
1906
Michal Vasko14676352020-05-29 11:35:55 +02001907 rc = lyxp_check_token(ctx, exp, *exp_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001908 LY_CHECK_RET(rc);
1909step:
1910 /* Step */
1911 switch (exp->tokens[*exp_idx]) {
1912 case LYXP_TOKEN_DOT:
1913 ++(*exp_idx);
1914 break;
1915
1916 case LYXP_TOKEN_DDOT:
1917 ++(*exp_idx);
1918 break;
1919
1920 case LYXP_TOKEN_AT:
1921 ++(*exp_idx);
1922
Michal Vasko14676352020-05-29 11:35:55 +02001923 rc = lyxp_check_token(ctx, exp, *exp_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001924 LY_CHECK_RET(rc);
1925 if ((exp->tokens[*exp_idx] != LYXP_TOKEN_NAMETEST) && (exp->tokens[*exp_idx] != LYXP_TOKEN_NODETYPE)) {
1926 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK,
1927 print_token(exp->tokens[*exp_idx]), &exp->expr[exp->tok_pos[*exp_idx]]);
1928 return LY_EVALID;
1929 }
1930 /* fall through */
1931 case LYXP_TOKEN_NAMETEST:
1932 ++(*exp_idx);
1933 goto reparse_predicate;
1934 break;
1935
1936 case LYXP_TOKEN_NODETYPE:
1937 ++(*exp_idx);
1938
1939 /* '(' */
Michal Vasko14676352020-05-29 11:35:55 +02001940 rc = lyxp_check_token(ctx, exp, *exp_idx, LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001941 LY_CHECK_RET(rc);
1942 ++(*exp_idx);
1943
1944 /* ')' */
Michal Vasko14676352020-05-29 11:35:55 +02001945 rc = lyxp_check_token(ctx, exp, *exp_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001946 LY_CHECK_RET(rc);
1947 ++(*exp_idx);
1948
1949reparse_predicate:
1950 /* Predicate* */
Michal Vasko14676352020-05-29 11:35:55 +02001951 while (!lyxp_check_token(NULL, exp, *exp_idx, LYXP_TOKEN_BRACK1)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001952 rc = reparse_predicate(ctx, exp, exp_idx);
1953 LY_CHECK_RET(rc);
1954 }
1955 break;
1956 default:
1957 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK,
1958 print_token(exp->tokens[*exp_idx]), &exp->expr[exp->tok_pos[*exp_idx]]);
1959 return LY_EVALID;
1960 }
Michal Vasko14676352020-05-29 11:35:55 +02001961 } while (!exp_check_token2(NULL, exp, *exp_idx, LYXP_TOKEN_OPERATOR_PATH, LYXP_TOKEN_OPERATOR_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001962
1963 return LY_SUCCESS;
1964}
1965
1966/**
1967 * @brief Reparse AbsoluteLocationPath. Logs directly on error.
1968 *
1969 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
1970 *
1971 * @param[in] ctx Context for logging.
1972 * @param[in] exp Parsed XPath expression.
1973 * @param[in] exp_idx Position in the expression \p exp.
1974 * @return LY_ERR
1975 */
1976static LY_ERR
Michal Vasko14676352020-05-29 11:35:55 +02001977reparse_absolute_location_path(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *exp_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001978{
1979 LY_ERR rc;
1980
Michal Vasko14676352020-05-29 11:35:55 +02001981 rc = exp_check_token2(ctx, exp, *exp_idx, LYXP_TOKEN_OPERATOR_PATH, LYXP_TOKEN_OPERATOR_RPATH);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001982 LY_CHECK_RET(rc);
1983
1984 /* '/' RelativeLocationPath? */
Michal Vasko14676352020-05-29 11:35:55 +02001985 if (exp->tokens[*exp_idx] == LYXP_TOKEN_OPERATOR_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001986 /* '/' */
1987 ++(*exp_idx);
1988
Michal Vasko14676352020-05-29 11:35:55 +02001989 if (lyxp_check_token(NULL, exp, *exp_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001990 return LY_SUCCESS;
1991 }
1992 switch (exp->tokens[*exp_idx]) {
1993 case LYXP_TOKEN_DOT:
1994 case LYXP_TOKEN_DDOT:
1995 case LYXP_TOKEN_AT:
1996 case LYXP_TOKEN_NAMETEST:
1997 case LYXP_TOKEN_NODETYPE:
1998 rc = reparse_relative_location_path(ctx, exp, exp_idx);
1999 LY_CHECK_RET(rc);
2000 /* fall through */
2001 default:
2002 break;
2003 }
2004
2005 /* '//' RelativeLocationPath */
2006 } else {
2007 /* '//' */
2008 ++(*exp_idx);
2009
2010 rc = reparse_relative_location_path(ctx, exp, exp_idx);
2011 LY_CHECK_RET(rc);
2012 }
2013
2014 return LY_SUCCESS;
2015}
2016
2017/**
2018 * @brief Reparse FunctionCall. Logs directly on error.
2019 *
2020 * [9] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
2021 *
2022 * @param[in] ctx Context for logging.
2023 * @param[in] exp Parsed XPath expression.
2024 * @param[in] exp_idx Position in the expression @p exp.
2025 * @return LY_ERR
2026 */
2027static LY_ERR
Michal Vasko14676352020-05-29 11:35:55 +02002028reparse_function_call(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *exp_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002029{
2030 int min_arg_count = -1, max_arg_count, arg_count;
2031 uint16_t func_exp_idx;
2032 LY_ERR rc;
2033
Michal Vasko14676352020-05-29 11:35:55 +02002034 rc = lyxp_check_token(ctx, exp, *exp_idx, LYXP_TOKEN_FUNCNAME);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002035 LY_CHECK_RET(rc);
2036 func_exp_idx = *exp_idx;
2037 switch (exp->tok_len[*exp_idx]) {
2038 case 3:
2039 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "not", 3)) {
2040 min_arg_count = 1;
2041 max_arg_count = 1;
2042 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "sum", 3)) {
2043 min_arg_count = 1;
2044 max_arg_count = 1;
2045 }
2046 break;
2047 case 4:
2048 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "lang", 4)) {
2049 min_arg_count = 1;
2050 max_arg_count = 1;
2051 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "last", 4)) {
2052 min_arg_count = 0;
2053 max_arg_count = 0;
2054 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "name", 4)) {
2055 min_arg_count = 0;
2056 max_arg_count = 1;
2057 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "true", 4)) {
2058 min_arg_count = 0;
2059 max_arg_count = 0;
2060 }
2061 break;
2062 case 5:
2063 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "count", 5)) {
2064 min_arg_count = 1;
2065 max_arg_count = 1;
2066 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "false", 5)) {
2067 min_arg_count = 0;
2068 max_arg_count = 0;
2069 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "floor", 5)) {
2070 min_arg_count = 1;
2071 max_arg_count = 1;
2072 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "round", 5)) {
2073 min_arg_count = 1;
2074 max_arg_count = 1;
2075 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "deref", 5)) {
2076 min_arg_count = 1;
2077 max_arg_count = 1;
2078 }
2079 break;
2080 case 6:
2081 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "concat", 6)) {
2082 min_arg_count = 2;
Michal Vaskobe2e3562019-10-15 15:35:35 +02002083 max_arg_count = INT_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002084 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "number", 6)) {
2085 min_arg_count = 0;
2086 max_arg_count = 1;
2087 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "string", 6)) {
2088 min_arg_count = 0;
2089 max_arg_count = 1;
2090 }
2091 break;
2092 case 7:
2093 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "boolean", 7)) {
2094 min_arg_count = 1;
2095 max_arg_count = 1;
2096 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "ceiling", 7)) {
2097 min_arg_count = 1;
2098 max_arg_count = 1;
2099 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "current", 7)) {
2100 min_arg_count = 0;
2101 max_arg_count = 0;
2102 }
2103 break;
2104 case 8:
2105 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "contains", 8)) {
2106 min_arg_count = 2;
2107 max_arg_count = 2;
2108 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "position", 8)) {
2109 min_arg_count = 0;
2110 max_arg_count = 0;
2111 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "re-match", 8)) {
2112 min_arg_count = 2;
2113 max_arg_count = 2;
2114 }
2115 break;
2116 case 9:
2117 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "substring", 9)) {
2118 min_arg_count = 2;
2119 max_arg_count = 3;
2120 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "translate", 9)) {
2121 min_arg_count = 3;
2122 max_arg_count = 3;
2123 }
2124 break;
2125 case 10:
2126 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "local-name", 10)) {
2127 min_arg_count = 0;
2128 max_arg_count = 1;
2129 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "enum-value", 10)) {
2130 min_arg_count = 1;
2131 max_arg_count = 1;
2132 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "bit-is-set", 10)) {
2133 min_arg_count = 2;
2134 max_arg_count = 2;
2135 }
2136 break;
2137 case 11:
2138 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "starts-with", 11)) {
2139 min_arg_count = 2;
2140 max_arg_count = 2;
2141 }
2142 break;
2143 case 12:
2144 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "derived-from", 12)) {
2145 min_arg_count = 2;
2146 max_arg_count = 2;
2147 }
2148 break;
2149 case 13:
2150 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "namespace-uri", 13)) {
2151 min_arg_count = 0;
2152 max_arg_count = 1;
2153 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "string-length", 13)) {
2154 min_arg_count = 0;
2155 max_arg_count = 1;
2156 }
2157 break;
2158 case 15:
2159 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "normalize-space", 15)) {
2160 min_arg_count = 0;
2161 max_arg_count = 1;
2162 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "substring-after", 15)) {
2163 min_arg_count = 2;
2164 max_arg_count = 2;
2165 }
2166 break;
2167 case 16:
2168 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "substring-before", 16)) {
2169 min_arg_count = 2;
2170 max_arg_count = 2;
2171 }
2172 break;
2173 case 20:
2174 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "derived-from-or-self", 20)) {
2175 min_arg_count = 2;
2176 max_arg_count = 2;
2177 }
2178 break;
2179 }
2180 if (min_arg_count == -1) {
2181 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INFUNC, exp->tok_len[*exp_idx], &exp->expr[exp->tok_pos[*exp_idx]]);
2182 return LY_EINVAL;
2183 }
2184 ++(*exp_idx);
2185
2186 /* '(' */
Michal Vasko14676352020-05-29 11:35:55 +02002187 rc = lyxp_check_token(ctx, exp, *exp_idx, LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002188 LY_CHECK_RET(rc);
2189 ++(*exp_idx);
2190
2191 /* ( Expr ( ',' Expr )* )? */
2192 arg_count = 0;
Michal Vasko14676352020-05-29 11:35:55 +02002193 rc = lyxp_check_token(ctx, exp, *exp_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002194 LY_CHECK_RET(rc);
2195 if (exp->tokens[*exp_idx] != LYXP_TOKEN_PAR2) {
2196 ++arg_count;
2197 rc = reparse_or_expr(ctx, exp, exp_idx);
2198 LY_CHECK_RET(rc);
2199 }
Michal Vasko14676352020-05-29 11:35:55 +02002200 while (!lyxp_check_token(NULL, exp, *exp_idx, LYXP_TOKEN_COMMA)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002201 ++(*exp_idx);
2202
2203 ++arg_count;
2204 rc = reparse_or_expr(ctx, exp, exp_idx);
2205 LY_CHECK_RET(rc);
2206 }
2207
2208 /* ')' */
Michal Vasko14676352020-05-29 11:35:55 +02002209 rc = lyxp_check_token(ctx, exp, *exp_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002210 LY_CHECK_RET(rc);
2211 ++(*exp_idx);
2212
2213 if ((arg_count < min_arg_count) || (arg_count > max_arg_count)) {
2214 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INARGCOUNT, arg_count, exp->tok_len[func_exp_idx],
2215 &exp->expr[exp->tok_pos[func_exp_idx]]);
2216 return LY_EVALID;
2217 }
2218
2219 return LY_SUCCESS;
2220}
2221
2222/**
2223 * @brief Reparse PathExpr. Logs directly on error.
2224 *
2225 * [10] PathExpr ::= LocationPath | PrimaryExpr Predicate*
2226 * | PrimaryExpr Predicate* '/' RelativeLocationPath
2227 * | PrimaryExpr Predicate* '//' RelativeLocationPath
2228 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
2229 * [8] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall
2230 *
2231 * @param[in] ctx Context for logging.
2232 * @param[in] exp Parsed XPath expression.
2233 * @param[in] exp_idx Position in the expression @p exp.
2234 * @return LY_ERR
2235 */
2236static LY_ERR
Michal Vasko14676352020-05-29 11:35:55 +02002237reparse_path_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *exp_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002238{
2239 LY_ERR rc;
2240
Michal Vasko14676352020-05-29 11:35:55 +02002241 if (lyxp_check_token(ctx, exp, *exp_idx, LYXP_TOKEN_NONE)) {
2242 return LY_EVALID;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002243 }
2244
2245 switch (exp->tokens[*exp_idx]) {
2246 case LYXP_TOKEN_PAR1:
2247 /* '(' Expr ')' Predicate* */
2248 ++(*exp_idx);
2249
2250 rc = reparse_or_expr(ctx, exp, exp_idx);
2251 LY_CHECK_RET(rc);
2252
Michal Vasko14676352020-05-29 11:35:55 +02002253 rc = lyxp_check_token(ctx, exp, *exp_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002254 LY_CHECK_RET(rc);
2255 ++(*exp_idx);
2256 goto predicate;
2257 break;
2258 case LYXP_TOKEN_DOT:
2259 case LYXP_TOKEN_DDOT:
2260 case LYXP_TOKEN_AT:
2261 case LYXP_TOKEN_NAMETEST:
2262 case LYXP_TOKEN_NODETYPE:
2263 /* RelativeLocationPath */
2264 rc = reparse_relative_location_path(ctx, exp, exp_idx);
2265 LY_CHECK_RET(rc);
2266 break;
2267 case LYXP_TOKEN_FUNCNAME:
2268 /* FunctionCall */
2269 rc = reparse_function_call(ctx, exp, exp_idx);
2270 LY_CHECK_RET(rc);
2271 goto predicate;
2272 break;
2273 case LYXP_TOKEN_OPERATOR_PATH:
Michal Vasko14676352020-05-29 11:35:55 +02002274 case LYXP_TOKEN_OPERATOR_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02002275 /* AbsoluteLocationPath */
2276 rc = reparse_absolute_location_path(ctx, exp, exp_idx);
2277 LY_CHECK_RET(rc);
2278 break;
2279 case LYXP_TOKEN_LITERAL:
2280 /* Literal */
2281 ++(*exp_idx);
2282 goto predicate;
2283 break;
2284 case LYXP_TOKEN_NUMBER:
2285 /* Number */
2286 ++(*exp_idx);
2287 goto predicate;
2288 break;
2289 default:
2290 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK,
2291 print_token(exp->tokens[*exp_idx]), &exp->expr[exp->tok_pos[*exp_idx]]);
2292 return LY_EVALID;
2293 }
2294
2295 return LY_SUCCESS;
2296
2297predicate:
2298 /* Predicate* */
Michal Vasko14676352020-05-29 11:35:55 +02002299 while (!lyxp_check_token(NULL, exp, *exp_idx, LYXP_TOKEN_BRACK1)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002300 rc = reparse_predicate(ctx, exp, exp_idx);
2301 LY_CHECK_RET(rc);
2302 }
2303
2304 /* ('/' or '//') RelativeLocationPath */
Michal Vasko14676352020-05-29 11:35:55 +02002305 if (!exp_check_token2(NULL, exp, *exp_idx, LYXP_TOKEN_OPERATOR_PATH, LYXP_TOKEN_OPERATOR_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002306
2307 /* '/' or '//' */
2308 ++(*exp_idx);
2309
2310 rc = reparse_relative_location_path(ctx, exp, exp_idx);
2311 LY_CHECK_RET(rc);
2312 }
2313
2314 return LY_SUCCESS;
2315}
2316
2317/**
2318 * @brief Reparse UnaryExpr. Logs directly on error.
2319 *
2320 * [17] UnaryExpr ::= UnionExpr | '-' UnaryExpr
2321 * [18] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
2322 *
2323 * @param[in] ctx Context for logging.
2324 * @param[in] exp Parsed XPath expression.
2325 * @param[in] exp_idx Position in the expression @p exp.
2326 * @return LY_ERR
2327 */
2328static LY_ERR
Michal Vasko14676352020-05-29 11:35:55 +02002329reparse_unary_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *exp_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002330{
2331 uint16_t prev_exp;
2332 LY_ERR rc;
2333
2334 /* ('-')* */
2335 prev_exp = *exp_idx;
Michal Vasko14676352020-05-29 11:35:55 +02002336 while (!lyxp_check_token(NULL, exp, *exp_idx, LYXP_TOKEN_OPERATOR_MATH)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002337 && (exp->expr[exp->tok_pos[*exp_idx]] == '-')) {
2338 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNARY);
2339 ++(*exp_idx);
2340 }
2341
2342 /* PathExpr */
2343 prev_exp = *exp_idx;
2344 rc = reparse_path_expr(ctx, exp, exp_idx);
2345 LY_CHECK_RET(rc);
2346
2347 /* ('|' PathExpr)* */
Michal Vasko14676352020-05-29 11:35:55 +02002348 while (!lyxp_check_token(NULL, exp, *exp_idx, LYXP_TOKEN_OPERATOR_UNI)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002349 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNION);
2350 ++(*exp_idx);
2351
2352 rc = reparse_path_expr(ctx, exp, exp_idx);
2353 LY_CHECK_RET(rc);
2354 }
2355
2356 return LY_SUCCESS;
2357}
2358
2359/**
2360 * @brief Reparse AdditiveExpr. Logs directly on error.
2361 *
2362 * [15] AdditiveExpr ::= MultiplicativeExpr
2363 * | AdditiveExpr '+' MultiplicativeExpr
2364 * | AdditiveExpr '-' MultiplicativeExpr
2365 * [16] MultiplicativeExpr ::= UnaryExpr
2366 * | MultiplicativeExpr '*' UnaryExpr
2367 * | MultiplicativeExpr 'div' UnaryExpr
2368 * | MultiplicativeExpr 'mod' UnaryExpr
2369 *
2370 * @param[in] ctx Context for logging.
2371 * @param[in] exp Parsed XPath expression.
2372 * @param[in] exp_idx Position in the expression @p exp.
2373 * @return LY_ERR
2374 */
2375static LY_ERR
Michal Vasko14676352020-05-29 11:35:55 +02002376reparse_additive_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *exp_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002377{
2378 uint16_t prev_add_exp, prev_mul_exp;
2379 LY_ERR rc;
2380
2381 prev_add_exp = *exp_idx;
2382 goto reparse_multiplicative_expr;
2383
2384 /* ('+' / '-' MultiplicativeExpr)* */
Michal Vasko14676352020-05-29 11:35:55 +02002385 while (!lyxp_check_token(NULL, exp, *exp_idx, LYXP_TOKEN_OPERATOR_MATH)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002386 && ((exp->expr[exp->tok_pos[*exp_idx]] == '+') || (exp->expr[exp->tok_pos[*exp_idx]] == '-'))) {
2387 exp_repeat_push(exp, prev_add_exp, LYXP_EXPR_ADDITIVE);
2388 ++(*exp_idx);
2389
2390reparse_multiplicative_expr:
2391 /* UnaryExpr */
2392 prev_mul_exp = *exp_idx;
2393 rc = reparse_unary_expr(ctx, exp, exp_idx);
2394 LY_CHECK_RET(rc);
2395
2396 /* ('*' / 'div' / 'mod' UnaryExpr)* */
Michal Vasko14676352020-05-29 11:35:55 +02002397 while (!lyxp_check_token(NULL, exp, *exp_idx, LYXP_TOKEN_OPERATOR_MATH)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002398 && ((exp->expr[exp->tok_pos[*exp_idx]] == '*') || (exp->tok_len[*exp_idx] == 3))) {
2399 exp_repeat_push(exp, prev_mul_exp, LYXP_EXPR_MULTIPLICATIVE);
2400 ++(*exp_idx);
2401
2402 rc = reparse_unary_expr(ctx, exp, exp_idx);
2403 LY_CHECK_RET(rc);
2404 }
2405 }
2406
2407 return LY_SUCCESS;
2408}
2409
2410/**
2411 * @brief Reparse EqualityExpr. Logs directly on error.
2412 *
2413 * [13] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
2414 * | EqualityExpr '!=' RelationalExpr
2415 * [14] RelationalExpr ::= AdditiveExpr
2416 * | RelationalExpr '<' AdditiveExpr
2417 * | RelationalExpr '>' AdditiveExpr
2418 * | RelationalExpr '<=' AdditiveExpr
2419 * | RelationalExpr '>=' AdditiveExpr
2420 *
2421 * @param[in] ctx Context for logging.
2422 * @param[in] exp Parsed XPath expression.
2423 * @param[in] exp_idx Position in the expression @p exp.
2424 * @return LY_ERR
2425 */
2426static LY_ERR
Michal Vasko14676352020-05-29 11:35:55 +02002427reparse_equality_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *exp_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002428{
2429 uint16_t prev_eq_exp, prev_rel_exp;
2430 LY_ERR rc;
2431
2432 prev_eq_exp = *exp_idx;
2433 goto reparse_additive_expr;
2434
2435 /* ('=' / '!=' RelationalExpr)* */
Michal Vasko14676352020-05-29 11:35:55 +02002436 while (!lyxp_check_token(NULL, exp, *exp_idx, LYXP_TOKEN_OPERATOR_COMP)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002437 && ((exp->expr[exp->tok_pos[*exp_idx]] == '=') || (exp->expr[exp->tok_pos[*exp_idx]] == '!'))) {
2438 exp_repeat_push(exp, prev_eq_exp, LYXP_EXPR_EQUALITY);
2439 ++(*exp_idx);
2440
2441reparse_additive_expr:
2442 /* AdditiveExpr */
2443 prev_rel_exp = *exp_idx;
2444 rc = reparse_additive_expr(ctx, exp, exp_idx);
2445 LY_CHECK_RET(rc);
2446
2447 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
Michal Vasko14676352020-05-29 11:35:55 +02002448 while (!lyxp_check_token(NULL, exp, *exp_idx, LYXP_TOKEN_OPERATOR_COMP)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002449 && ((exp->expr[exp->tok_pos[*exp_idx]] == '<') || (exp->expr[exp->tok_pos[*exp_idx]] == '>'))) {
2450 exp_repeat_push(exp, prev_rel_exp, LYXP_EXPR_RELATIONAL);
2451 ++(*exp_idx);
2452
2453 rc = reparse_additive_expr(ctx, exp, exp_idx);
2454 LY_CHECK_RET(rc);
2455 }
2456 }
2457
2458 return LY_SUCCESS;
2459}
2460
2461/**
2462 * @brief Reparse OrExpr. Logs directly on error.
2463 *
2464 * [11] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
2465 * [12] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
2466 *
2467 * @param[in] ctx Context for logging.
2468 * @param[in] exp Parsed XPath expression.
2469 * @param[in] exp_idx Position in the expression @p exp.
2470 * @return LY_ERR
2471 */
2472static LY_ERR
Michal Vasko14676352020-05-29 11:35:55 +02002473reparse_or_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *exp_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002474{
2475 uint16_t prev_or_exp, prev_and_exp;
2476 LY_ERR rc;
2477
2478 prev_or_exp = *exp_idx;
2479 goto reparse_equality_expr;
2480
2481 /* ('or' AndExpr)* */
Michal Vasko14676352020-05-29 11:35:55 +02002482 while (!lyxp_check_token(NULL, exp, *exp_idx, LYXP_TOKEN_OPERATOR_LOG) && (exp->tok_len[*exp_idx] == 2)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002483 exp_repeat_push(exp, prev_or_exp, LYXP_EXPR_OR);
2484 ++(*exp_idx);
2485
2486reparse_equality_expr:
2487 /* EqualityExpr */
2488 prev_and_exp = *exp_idx;
2489 rc = reparse_equality_expr(ctx, exp, exp_idx);
2490 LY_CHECK_RET(rc);
2491
2492 /* ('and' EqualityExpr)* */
Michal Vasko14676352020-05-29 11:35:55 +02002493 while (!lyxp_check_token(NULL, exp, *exp_idx, LYXP_TOKEN_OPERATOR_LOG) && (exp->tok_len[*exp_idx] == 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002494 exp_repeat_push(exp, prev_and_exp, LYXP_EXPR_AND);
2495 ++(*exp_idx);
2496
2497 rc = reparse_equality_expr(ctx, exp, exp_idx);
2498 LY_CHECK_RET(rc);
2499 }
2500 }
2501
2502 return LY_SUCCESS;
2503}
Radek Krejcib1646a92018-11-02 16:08:26 +01002504
2505/**
2506 * @brief Parse NCName.
2507 *
2508 * @param[in] ncname Name to parse.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002509 * @return Length of @p ncname valid bytes.
Radek Krejcib1646a92018-11-02 16:08:26 +01002510 */
Radek Krejcid4270262019-01-07 15:07:25 +01002511static long int
Radek Krejcib1646a92018-11-02 16:08:26 +01002512parse_ncname(const char *ncname)
2513{
2514 unsigned int uc;
Radek Krejcid4270262019-01-07 15:07:25 +01002515 size_t size;
2516 long int len = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002517
2518 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), 0);
2519 if (!is_xmlqnamestartchar(uc) || (uc == ':')) {
2520 return len;
2521 }
2522
2523 do {
2524 len += size;
Radek Krejci9a564c92019-01-07 14:53:57 +01002525 if (!*ncname) {
2526 break;
2527 }
Radek Krejcid4270262019-01-07 15:07:25 +01002528 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), -len);
Radek Krejcib1646a92018-11-02 16:08:26 +01002529 } while (is_xmlqnamechar(uc) && (uc != ':'));
2530
2531 return len;
2532}
2533
2534/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02002535 * @brief Add @p token into the expression @p exp.
Radek Krejcib1646a92018-11-02 16:08:26 +01002536 *
Michal Vasko03ff5a72019-09-11 13:49:33 +02002537 * @param[in] ctx Context for logging.
Radek Krejcib1646a92018-11-02 16:08:26 +01002538 * @param[in] exp Expression to use.
2539 * @param[in] token Token to add.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002540 * @param[in] tok_pos Token position in the XPath expression.
Radek Krejcib1646a92018-11-02 16:08:26 +01002541 * @param[in] tok_len Token length in the XPath expression.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002542 * @return LY_ERR
Radek Krejcib1646a92018-11-02 16:08:26 +01002543 */
2544static LY_ERR
Michal Vasko14676352020-05-29 11:35:55 +02002545exp_add_token(const struct ly_ctx *ctx, struct lyxp_expr *exp, enum lyxp_token token, uint16_t tok_pos, uint16_t tok_len)
Radek Krejcib1646a92018-11-02 16:08:26 +01002546{
2547 uint32_t prev;
2548
2549 if (exp->used == exp->size) {
2550 prev = exp->size;
2551 exp->size += LYXP_EXPR_SIZE_STEP;
2552 if (prev > exp->size) {
2553 LOGINT(ctx);
2554 return LY_EINT;
2555 }
2556
2557 exp->tokens = ly_realloc(exp->tokens, exp->size * sizeof *exp->tokens);
2558 LY_CHECK_ERR_RET(!exp->tokens, LOGMEM(ctx), LY_EMEM);
2559 exp->tok_pos = ly_realloc(exp->tok_pos, exp->size * sizeof *exp->tok_pos);
2560 LY_CHECK_ERR_RET(!exp->tok_pos, LOGMEM(ctx), LY_EMEM);
2561 exp->tok_len = ly_realloc(exp->tok_len, exp->size * sizeof *exp->tok_len);
2562 LY_CHECK_ERR_RET(!exp->tok_len, LOGMEM(ctx), LY_EMEM);
2563 }
2564
2565 exp->tokens[exp->used] = token;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002566 exp->tok_pos[exp->used] = tok_pos;
Radek Krejcib1646a92018-11-02 16:08:26 +01002567 exp->tok_len[exp->used] = tok_len;
2568 ++exp->used;
2569 return LY_SUCCESS;
2570}
2571
2572void
Michal Vasko14676352020-05-29 11:35:55 +02002573lyxp_expr_free(const struct ly_ctx *ctx, struct lyxp_expr *expr)
Radek Krejcib1646a92018-11-02 16:08:26 +01002574{
2575 uint16_t i;
2576
2577 if (!expr) {
2578 return;
2579 }
2580
2581 lydict_remove(ctx, expr->expr);
2582 free(expr->tokens);
2583 free(expr->tok_pos);
2584 free(expr->tok_len);
2585 if (expr->repeat) {
2586 for (i = 0; i < expr->used; ++i) {
2587 free(expr->repeat[i]);
2588 }
2589 }
2590 free(expr->repeat);
2591 free(expr);
2592}
2593
2594struct lyxp_expr *
Michal Vasko14676352020-05-29 11:35:55 +02002595lyxp_expr_parse(const struct ly_ctx *ctx, const char *expr)
Radek Krejcib1646a92018-11-02 16:08:26 +01002596{
2597 struct lyxp_expr *ret;
Radek Krejcid4270262019-01-07 15:07:25 +01002598 size_t parsed = 0, tok_len;
2599 long int ncname_len;
Radek Krejcib1646a92018-11-02 16:08:26 +01002600 enum lyxp_token tok_type;
2601 int prev_function_check = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002602 uint16_t exp_idx = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002603
2604 if (strlen(expr) > UINT16_MAX) {
2605 LOGERR(ctx, LY_EINVAL, "XPath expression cannot be longer than %ud characters.", UINT16_MAX);
2606 return NULL;
2607 }
2608
2609 /* init lyxp_expr structure */
2610 ret = calloc(1, sizeof *ret);
2611 LY_CHECK_ERR_GOTO(!ret, LOGMEM(ctx), error);
2612 ret->expr = lydict_insert(ctx, expr, strlen(expr));
2613 LY_CHECK_ERR_GOTO(!ret->expr, LOGMEM(ctx), error);
2614 ret->used = 0;
2615 ret->size = LYXP_EXPR_SIZE_START;
2616 ret->tokens = malloc(ret->size * sizeof *ret->tokens);
2617 LY_CHECK_ERR_GOTO(!ret->tokens, LOGMEM(ctx), error);
2618
2619 ret->tok_pos = malloc(ret->size * sizeof *ret->tok_pos);
2620 LY_CHECK_ERR_GOTO(!ret->tok_pos, LOGMEM(ctx), error);
2621
2622 ret->tok_len = malloc(ret->size * sizeof *ret->tok_len);
2623 LY_CHECK_ERR_GOTO(!ret->tok_len, LOGMEM(ctx), error);
2624
2625 while (is_xmlws(expr[parsed])) {
2626 ++parsed;
2627 }
2628
2629 do {
2630 if (expr[parsed] == '(') {
2631
2632 /* '(' */
2633 tok_len = 1;
2634 tok_type = LYXP_TOKEN_PAR1;
2635
2636 if (prev_function_check && ret->used && (ret->tokens[ret->used - 1] == LYXP_TOKEN_NAMETEST)) {
2637 /* it is a NodeType/FunctionName after all */
2638 if (((ret->tok_len[ret->used - 1] == 4)
2639 && (!strncmp(&expr[ret->tok_pos[ret->used - 1]], "node", 4)
2640 || !strncmp(&expr[ret->tok_pos[ret->used - 1]], "text", 4))) ||
2641 ((ret->tok_len[ret->used - 1] == 7)
2642 && !strncmp(&expr[ret->tok_pos[ret->used - 1]], "comment", 7))) {
2643 ret->tokens[ret->used - 1] = LYXP_TOKEN_NODETYPE;
2644 } else {
2645 ret->tokens[ret->used - 1] = LYXP_TOKEN_FUNCNAME;
2646 }
2647 prev_function_check = 0;
2648 }
2649
2650 } else if (expr[parsed] == ')') {
2651
2652 /* ')' */
2653 tok_len = 1;
2654 tok_type = LYXP_TOKEN_PAR2;
2655
2656 } else if (expr[parsed] == '[') {
2657
2658 /* '[' */
2659 tok_len = 1;
2660 tok_type = LYXP_TOKEN_BRACK1;
2661
2662 } else if (expr[parsed] == ']') {
2663
2664 /* ']' */
2665 tok_len = 1;
2666 tok_type = LYXP_TOKEN_BRACK2;
2667
2668 } else if (!strncmp(&expr[parsed], "..", 2)) {
2669
2670 /* '..' */
2671 tok_len = 2;
2672 tok_type = LYXP_TOKEN_DDOT;
2673
2674 } else if ((expr[parsed] == '.') && (!isdigit(expr[parsed + 1]))) {
2675
2676 /* '.' */
2677 tok_len = 1;
2678 tok_type = LYXP_TOKEN_DOT;
2679
2680 } else if (expr[parsed] == '@') {
2681
2682 /* '@' */
2683 tok_len = 1;
2684 tok_type = LYXP_TOKEN_AT;
2685
2686 } else if (expr[parsed] == ',') {
2687
2688 /* ',' */
2689 tok_len = 1;
2690 tok_type = LYXP_TOKEN_COMMA;
2691
2692 } else if (expr[parsed] == '\'') {
2693
2694 /* Literal with ' */
2695 for (tok_len = 1; (expr[parsed + tok_len] != '\0') && (expr[parsed + tok_len] != '\''); ++tok_len);
2696 LY_CHECK_ERR_GOTO(expr[parsed + tok_len] == '\0',
2697 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOE, expr[parsed], &expr[parsed]), error);
2698 ++tok_len;
2699 tok_type = LYXP_TOKEN_LITERAL;
2700
2701 } else if (expr[parsed] == '\"') {
2702
2703 /* Literal with " */
2704 for (tok_len = 1; (expr[parsed + tok_len] != '\0') && (expr[parsed + tok_len] != '\"'); ++tok_len);
2705 LY_CHECK_ERR_GOTO(expr[parsed + tok_len] == '\0',
2706 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOE, expr[parsed], &expr[parsed]), error);
2707 ++tok_len;
2708 tok_type = LYXP_TOKEN_LITERAL;
2709
2710 } else if ((expr[parsed] == '.') || (isdigit(expr[parsed]))) {
2711
2712 /* Number */
2713 for (tok_len = 0; isdigit(expr[parsed + tok_len]); ++tok_len);
2714 if (expr[parsed + tok_len] == '.') {
2715 ++tok_len;
2716 for (; isdigit(expr[parsed + tok_len]); ++tok_len);
2717 }
2718 tok_type = LYXP_TOKEN_NUMBER;
2719
2720 } else if (expr[parsed] == '/') {
2721
2722 /* Operator '/', '//' */
2723 if (!strncmp(&expr[parsed], "//", 2)) {
2724 tok_len = 2;
Michal Vasko14676352020-05-29 11:35:55 +02002725 tok_type = LYXP_TOKEN_OPERATOR_RPATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002726 } else {
2727 tok_len = 1;
Michal Vasko14676352020-05-29 11:35:55 +02002728 tok_type = LYXP_TOKEN_OPERATOR_PATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002729 }
Radek Krejcib1646a92018-11-02 16:08:26 +01002730
2731 } else if (!strncmp(&expr[parsed], "!=", 2) || !strncmp(&expr[parsed], "<=", 2)
2732 || !strncmp(&expr[parsed], ">=", 2)) {
2733
2734 /* Operator '!=', '<=', '>=' */
2735 tok_len = 2;
2736 tok_type = LYXP_TOKEN_OPERATOR_COMP;
2737
2738 } else if (expr[parsed] == '|') {
2739
2740 /* Operator '|' */
2741 tok_len = 1;
2742 tok_type = LYXP_TOKEN_OPERATOR_UNI;
2743
2744 } else if ((expr[parsed] == '+') || (expr[parsed] == '-')) {
2745
2746 /* Operator '+', '-' */
2747 tok_len = 1;
2748 tok_type = LYXP_TOKEN_OPERATOR_MATH;
2749
2750 } else if ((expr[parsed] == '=') || (expr[parsed] == '<') || (expr[parsed] == '>')) {
2751
2752 /* Operator '=', '<', '>' */
2753 tok_len = 1;
2754 tok_type = LYXP_TOKEN_OPERATOR_COMP;
2755
2756 } else if (ret->used && (ret->tokens[ret->used - 1] != LYXP_TOKEN_AT)
2757 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_PAR1)
2758 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_BRACK1)
2759 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_COMMA)
2760 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPERATOR_LOG)
2761 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPERATOR_COMP)
2762 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPERATOR_MATH)
2763 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPERATOR_UNI)
Michal Vasko14676352020-05-29 11:35:55 +02002764 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPERATOR_PATH)
2765 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPERATOR_RPATH)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002766
2767 /* Operator '*', 'or', 'and', 'mod', or 'div' */
2768 if (expr[parsed] == '*') {
2769 tok_len = 1;
2770 tok_type = LYXP_TOKEN_OPERATOR_MATH;
2771
2772 } else if (!strncmp(&expr[parsed], "or", 2)) {
2773 tok_len = 2;
2774 tok_type = LYXP_TOKEN_OPERATOR_LOG;
2775
2776 } else if (!strncmp(&expr[parsed], "and", 3)) {
2777 tok_len = 3;
2778 tok_type = LYXP_TOKEN_OPERATOR_LOG;
2779
2780 } else if (!strncmp(&expr[parsed], "mod", 3) || !strncmp(&expr[parsed], "div", 3)) {
2781 tok_len = 3;
2782 tok_type = LYXP_TOKEN_OPERATOR_MATH;
2783
2784 } else if (prev_function_check) {
Michal Vasko53078572019-05-24 08:50:15 +02002785 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "Invalid character 0x%x ('%c'), perhaps \"%.*s\" is supposed to be a function call.",
2786 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 +01002787 goto error;
2788 } else {
Radek Krejcid4270262019-01-07 15:07:25 +01002789 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INEXPR, parsed + 1, expr);
Radek Krejcib1646a92018-11-02 16:08:26 +01002790 goto error;
2791 }
2792 } else if (expr[parsed] == '*') {
2793
2794 /* NameTest '*' */
2795 tok_len = 1;
2796 tok_type = LYXP_TOKEN_NAMETEST;
2797
2798 } else {
2799
2800 /* NameTest (NCName ':' '*' | QName) or NodeType/FunctionName */
2801 ncname_len = parse_ncname(&expr[parsed]);
Radek Krejcid4270262019-01-07 15:07:25 +01002802 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 +01002803 tok_len = ncname_len;
2804
2805 if (expr[parsed + tok_len] == ':') {
2806 ++tok_len;
2807 if (expr[parsed + tok_len] == '*') {
2808 ++tok_len;
2809 } else {
2810 ncname_len = parse_ncname(&expr[parsed + tok_len]);
Radek Krejcid4270262019-01-07 15:07:25 +01002811 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 +01002812 tok_len += ncname_len;
2813 }
2814 /* remove old flag to prevent ambiguities */
2815 prev_function_check = 0;
2816 tok_type = LYXP_TOKEN_NAMETEST;
2817 } else {
2818 /* there is no prefix so it can still be NodeType/FunctionName, we can't finally decide now */
2819 prev_function_check = 1;
2820 tok_type = LYXP_TOKEN_NAMETEST;
2821 }
2822 }
2823
2824 /* store the token, move on to the next one */
2825 LY_CHECK_GOTO(exp_add_token(ctx, ret, tok_type, parsed, tok_len), error);
2826 parsed += tok_len;
2827 while (is_xmlws(expr[parsed])) {
2828 ++parsed;
2829 }
2830
2831 } while (expr[parsed]);
2832
2833 /* prealloc repeat */
2834 ret->repeat = calloc(ret->size, sizeof *ret->repeat);
2835 LY_CHECK_ERR_GOTO(!ret->repeat, LOGMEM(ctx), error);
2836
Michal Vasko03ff5a72019-09-11 13:49:33 +02002837 /* fill repeat */
2838 LY_CHECK_GOTO(reparse_or_expr(ctx, ret, &exp_idx), error);
2839 if (ret->used > exp_idx) {
2840 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK, "Unknown", &ret->expr[ret->tok_pos[exp_idx]]);
2841 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "Unparsed characters \"%s\" left at the end of an XPath expression.",
2842 &ret->expr[ret->tok_pos[exp_idx]]);
2843 goto error;
2844 }
2845
2846 print_expr_struct_debug(ret);
2847
Radek Krejcib1646a92018-11-02 16:08:26 +01002848 return ret;
2849
2850error:
2851 lyxp_expr_free(ctx, ret);
2852 return NULL;
2853}
2854
Michal Vasko03ff5a72019-09-11 13:49:33 +02002855/*
2856 * warn functions
2857 *
2858 * Warn functions check specific reasonable conditions for schema XPath
2859 * and print a warning if they are not satisfied.
2860 */
2861
2862/**
2863 * @brief Get the last-added schema node that is currently in the context.
2864 *
2865 * @param[in] set Set to search in.
2866 * @return Last-added schema context node, NULL if no node is in context.
2867 */
2868static struct lysc_node *
2869warn_get_scnode_in_ctx(struct lyxp_set *set)
2870{
2871 uint32_t i;
2872
2873 if (!set || (set->type != LYXP_SET_SCNODE_SET)) {
2874 return NULL;
2875 }
2876
2877 i = set->used;
2878 do {
2879 --i;
2880 if (set->val.scnodes[i].in_ctx == 1) {
2881 /* if there are more, simply return the first found (last added) */
2882 return set->val.scnodes[i].scnode;
2883 }
2884 } while (i);
2885
2886 return NULL;
2887}
2888
2889/**
2890 * @brief Test whether a type is numeric - integer type or decimal64.
2891 *
2892 * @param[in] type Type to test.
2893 * @return 1 if numeric, 0 otherwise.
2894 */
2895static int
2896warn_is_numeric_type(struct lysc_type *type)
2897{
2898 struct lysc_type_union *uni;
2899 int ret;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002900 LY_ARRAY_SIZE_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002901
2902 switch (type->basetype) {
2903 case LY_TYPE_DEC64:
2904 case LY_TYPE_INT8:
2905 case LY_TYPE_UINT8:
2906 case LY_TYPE_INT16:
2907 case LY_TYPE_UINT16:
2908 case LY_TYPE_INT32:
2909 case LY_TYPE_UINT32:
2910 case LY_TYPE_INT64:
2911 case LY_TYPE_UINT64:
2912 return 1;
2913 case LY_TYPE_UNION:
2914 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002915 LY_ARRAY_FOR(uni->types, u) {
2916 ret = warn_is_numeric_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002917 if (ret) {
2918 /* found a suitable type */
2919 return 1;
2920 }
2921 }
2922 /* did not find any suitable type */
2923 return 0;
2924 case LY_TYPE_LEAFREF:
2925 return warn_is_numeric_type(((struct lysc_type_leafref *)type)->realtype);
2926 default:
2927 return 0;
2928 }
2929}
2930
2931/**
2932 * @brief Test whether a type is string-like - no integers, decimal64 or binary.
2933 *
2934 * @param[in] type Type to test.
2935 * @return 1 if string, 0 otherwise.
2936 */
2937static int
2938warn_is_string_type(struct lysc_type *type)
2939{
2940 struct lysc_type_union *uni;
2941 int ret;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002942 LY_ARRAY_SIZE_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002943
2944 switch (type->basetype) {
2945 case LY_TYPE_BITS:
2946 case LY_TYPE_ENUM:
2947 case LY_TYPE_IDENT:
2948 case LY_TYPE_INST:
2949 case LY_TYPE_STRING:
2950 return 1;
2951 case LY_TYPE_UNION:
2952 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002953 LY_ARRAY_FOR(uni->types, u) {
2954 ret = warn_is_string_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002955 if (ret) {
2956 /* found a suitable type */
2957 return 1;
2958 }
2959 }
2960 /* did not find any suitable type */
2961 return 0;
2962 case LY_TYPE_LEAFREF:
2963 return warn_is_string_type(((struct lysc_type_leafref *)type)->realtype);
2964 default:
2965 return 0;
2966 }
2967}
2968
2969/**
2970 * @brief Test whether a type is one specific type.
2971 *
2972 * @param[in] type Type to test.
2973 * @param[in] base Expected type.
2974 * @return 1 if it is, 0 otherwise.
2975 */
2976static int
2977warn_is_specific_type(struct lysc_type *type, LY_DATA_TYPE base)
2978{
2979 struct lysc_type_union *uni;
2980 int ret;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002981 LY_ARRAY_SIZE_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002982
2983 if (type->basetype == base) {
2984 return 1;
2985 } else if (type->basetype == LY_TYPE_UNION) {
2986 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002987 LY_ARRAY_FOR(uni->types, u) {
2988 ret = warn_is_specific_type(uni->types[u], base);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002989 if (ret) {
2990 /* found a suitable type */
2991 return 1;
2992 }
2993 }
2994 /* did not find any suitable type */
2995 return 0;
2996 } else if (type->basetype == LY_TYPE_LEAFREF) {
2997 return warn_is_specific_type(((struct lysc_type_leafref *)type)->realtype, base);
2998 }
2999
3000 return 0;
3001}
3002
3003/**
3004 * @brief Get next type of a (union) type.
3005 *
3006 * @param[in] type Base type.
3007 * @param[in] prev_type Previously returned type.
3008 * @return Next type or NULL.
3009 */
3010static struct lysc_type *
3011warn_is_equal_type_next_type(struct lysc_type *type, struct lysc_type *prev_type)
3012{
3013 struct lysc_type_union *uni;
3014 int found = 0;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003015 LY_ARRAY_SIZE_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003016
3017 switch (type->basetype) {
3018 case LY_TYPE_UNION:
3019 uni = (struct lysc_type_union *)type;
3020 if (!prev_type) {
3021 return uni->types[0];
3022 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003023 LY_ARRAY_FOR(uni->types, u) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003024 if (found) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003025 return uni->types[u];
Michal Vasko03ff5a72019-09-11 13:49:33 +02003026 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003027 if (prev_type == uni->types[u]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003028 found = 1;
3029 }
3030 }
3031 return NULL;
3032 default:
3033 if (prev_type) {
3034 assert(type == prev_type);
3035 return NULL;
3036 } else {
3037 return type;
3038 }
3039 }
3040}
3041
3042/**
3043 * @brief Test whether 2 types have a common type.
3044 *
3045 * @param[in] type1 First type.
3046 * @param[in] type2 Second type.
3047 * @return 1 if they do, 0 otherwise.
3048 */
3049static int
3050warn_is_equal_type(struct lysc_type *type1, struct lysc_type *type2)
3051{
3052 struct lysc_type *t1, *rt1, *t2, *rt2;
3053
3054 t1 = NULL;
3055 while ((t1 = warn_is_equal_type_next_type(type1, t1))) {
3056 if (t1->basetype == LY_TYPE_LEAFREF) {
3057 rt1 = ((struct lysc_type_leafref *)t1)->realtype;
3058 } else {
3059 rt1 = t1;
3060 }
3061
3062 t2 = NULL;
3063 while ((t2 = warn_is_equal_type_next_type(type2, t2))) {
3064 if (t2->basetype == LY_TYPE_LEAFREF) {
3065 rt2 = ((struct lysc_type_leafref *)t2)->realtype;
3066 } else {
3067 rt2 = t2;
3068 }
3069
3070 if (rt2->basetype == rt1->basetype) {
3071 /* match found */
3072 return 1;
3073 }
3074 }
3075 }
3076
3077 return 0;
3078}
3079
3080/**
3081 * @brief Check both operands of comparison operators.
3082 *
3083 * @param[in] ctx Context for errors.
3084 * @param[in] set1 First operand set.
3085 * @param[in] set2 Second operand set.
3086 * @param[in] numbers_only Whether accept only numbers or other types are fine too (for '=' and '!=').
3087 * @param[in] expr Start of the expression to print with the warning.
3088 * @param[in] tok_pos Token position.
3089 */
3090static void
3091warn_operands(struct ly_ctx *ctx, struct lyxp_set *set1, struct lyxp_set *set2, int numbers_only, const char *expr, uint16_t tok_pos)
3092{
3093 struct lysc_node_leaf *node1, *node2;
3094 int leaves = 1, warning = 0;
3095
3096 node1 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set1);
3097 node2 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set2);
3098
3099 if (!node1 && !node2) {
3100 /* no node-sets involved, nothing to do */
3101 return;
3102 }
3103
3104 if (node1) {
3105 if (!(node1->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3106 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node1->nodetype), node1->name);
3107 warning = 1;
3108 leaves = 0;
3109 } else if (numbers_only && !warn_is_numeric_type(node1->type)) {
3110 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node1->name);
3111 warning = 1;
3112 }
3113 }
3114
3115 if (node2) {
3116 if (!(node2->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3117 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node2->nodetype), node2->name);
3118 warning = 1;
3119 leaves = 0;
3120 } else if (numbers_only && !warn_is_numeric_type(node2->type)) {
3121 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node2->name);
3122 warning = 1;
3123 }
3124 }
3125
3126 if (node1 && node2 && leaves && !numbers_only) {
3127 if ((warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type))
3128 || (!warn_is_numeric_type(node1->type) && warn_is_numeric_type(node2->type))
3129 || (!warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type)
3130 && !warn_is_equal_type(node1->type, node2->type))) {
3131 LOGWRN(ctx, "Incompatible types of operands \"%s\" and \"%s\" for comparison.", node1->name, node2->name);
3132 warning = 1;
3133 }
3134 }
3135
3136 if (warning) {
3137 LOGWRN(ctx, "Previous warning generated by XPath subexpression[%u] \"%.20s\".", tok_pos, expr + tok_pos);
3138 }
3139}
3140
3141/**
3142 * @brief Check that a value is valid for a leaf. If not applicable, does nothing.
3143 *
3144 * @param[in] exp Parsed XPath expression.
3145 * @param[in] set Set with the leaf/leaf-list.
3146 * @param[in] val_exp Index of the value (literal/number) in @p exp.
3147 * @param[in] equal_exp Index of the start of the equality expression in @p exp.
3148 * @param[in] last_equal_exp Index of the end of the equality expression in @p exp.
3149 */
3150static void
3151warn_equality_value(struct lyxp_expr *exp, struct lyxp_set *set, uint16_t val_exp, uint16_t equal_exp, uint16_t last_equal_exp)
3152{
3153 struct lysc_node *scnode;
3154 struct lysc_type *type;
3155 char *value;
3156 LY_ERR rc;
3157 struct ly_err_item *err = NULL;
3158
3159 if ((scnode = warn_get_scnode_in_ctx(set)) && (scnode->nodetype & (LYS_LEAF | LYS_LEAFLIST))
3160 && ((exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) || (exp->tokens[val_exp] == LYXP_TOKEN_NUMBER))) {
3161 /* check that the node can have the specified value */
3162 if (exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) {
3163 value = strndup(exp->expr + exp->tok_pos[val_exp] + 1, exp->tok_len[val_exp] - 2);
3164 } else {
3165 value = strndup(exp->expr + exp->tok_pos[val_exp], exp->tok_len[val_exp]);
3166 }
3167 if (!value) {
3168 LOGMEM(set->ctx);
3169 return;
3170 }
3171
3172 if ((((struct lysc_node_leaf *)scnode)->type->basetype == LY_TYPE_IDENT) && !strchr(value, ':')) {
3173 LOGWRN(set->ctx, "Identityref \"%s\" comparison with identity \"%s\" without prefix, consider adding"
3174 " a prefix or best using \"derived-from(-or-self)()\" functions.", scnode->name, value);
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 type = ((struct lysc_node_leaf *)scnode)->type;
3181 if (type->basetype != LY_TYPE_IDENT) {
3182 rc = type->plugin->store(set->ctx, type, value, strlen(value), LY_TYPE_OPTS_SCHEMA,
3183 lys_resolve_prefix, (void *)type->dflt_mod, LYD_XML, NULL, NULL, NULL, NULL, &err);
3184
3185 if (err) {
3186 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type (%s).", value, err->msg);
3187 ly_err_free(err);
3188 } else if (rc != LY_SUCCESS) {
3189 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type.", value);
3190 }
3191 if (rc != LY_SUCCESS) {
3192 LOGWRN(set->ctx, "Previous warning generated by XPath subexpression[%u] \"%.*s\".", exp->tok_pos[equal_exp],
3193 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
3194 exp->expr + exp->tok_pos[equal_exp]);
3195 }
3196 }
3197 free(value);
3198 }
3199}
3200
3201/*
3202 * XPath functions
3203 */
3204
3205/**
3206 * @brief Execute the YANG 1.1 bit-is-set(node-set, string) function. Returns LYXP_SET_BOOLEAN
3207 * depending on whether the first node bit value from the second argument is set.
3208 *
3209 * @param[in] args Array of arguments.
3210 * @param[in] arg_count Count of elements in @p args.
3211 * @param[in,out] set Context and result set at the same time.
3212 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003213 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003214 */
3215static LY_ERR
3216xpath_bit_is_set(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3217{
3218 struct lyd_node_term *leaf;
3219 struct lysc_node_leaf *sleaf;
3220 struct lysc_type_bits *bits;
3221 LY_ERR rc = LY_SUCCESS;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003222 LY_ARRAY_SIZE_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003223
3224 if (options & LYXP_SCNODE_ALL) {
3225 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3226 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003227 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3228 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 +02003229 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_BITS)) {
3230 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"bits\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003231 }
3232
3233 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3234 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3235 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 +02003236 } else if (!warn_is_string_type(sleaf->type)) {
3237 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003238 }
3239 }
3240 set_scnode_clear_ctx(set);
3241 return rc;
3242 }
3243
Michal Vaskod3678892020-05-21 10:06:58 +02003244 if (args[0]->type != LYXP_SET_NODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003245 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)");
3246 return LY_EVALID;
3247 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003248 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003249 LY_CHECK_RET(rc);
3250
3251 set_fill_boolean(set, 0);
Michal Vaskod3678892020-05-21 10:06:58 +02003252 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003253 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
3254 if ((leaf->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST))
3255 && (((struct lysc_node_leaf *)leaf->schema)->type->basetype == LY_TYPE_BITS)) {
3256 bits = (struct lysc_type_bits *)((struct lysc_node_leaf *)leaf->schema)->type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003257 LY_ARRAY_FOR(bits->bits, u) {
3258 if (!strcmp(bits->bits[u].name, args[1]->val.str)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003259 set_fill_boolean(set, 1);
3260 break;
3261 }
3262 }
3263 }
3264 }
3265
3266 return LY_SUCCESS;
3267}
3268
3269/**
3270 * @brief Execute the XPath boolean(object) function. Returns LYXP_SET_BOOLEAN
3271 * with the argument converted to boolean.
3272 *
3273 * @param[in] args Array of arguments.
3274 * @param[in] arg_count Count of elements in @p args.
3275 * @param[in,out] set Context and result set at the same time.
3276 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003277 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003278 */
3279static LY_ERR
3280xpath_boolean(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3281{
3282 LY_ERR rc;
3283
3284 if (options & LYXP_SCNODE_ALL) {
3285 set_scnode_clear_ctx(set);
3286 return LY_SUCCESS;
3287 }
3288
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003289 rc = lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003290 LY_CHECK_RET(rc);
3291 set_fill_set(set, args[0]);
3292
3293 return LY_SUCCESS;
3294}
3295
3296/**
3297 * @brief Execute the XPath ceiling(number) function. Returns LYXP_SET_NUMBER
3298 * with the first argument rounded up to the nearest integer.
3299 *
3300 * @param[in] args Array of arguments.
3301 * @param[in] arg_count Count of elements in @p args.
3302 * @param[in,out] set Context and result set at the same time.
3303 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003304 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003305 */
3306static LY_ERR
3307xpath_ceiling(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3308{
3309 struct lysc_node_leaf *sleaf;
3310 LY_ERR rc = LY_SUCCESS;
3311
3312 if (options & LYXP_SCNODE_ALL) {
3313 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3314 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003315 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3316 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 +02003317 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
3318 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003319 }
3320 set_scnode_clear_ctx(set);
3321 return rc;
3322 }
3323
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003324 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003325 LY_CHECK_RET(rc);
3326 if ((long long)args[0]->val.num != args[0]->val.num) {
3327 set_fill_number(set, ((long long)args[0]->val.num) + 1);
3328 } else {
3329 set_fill_number(set, args[0]->val.num);
3330 }
3331
3332 return LY_SUCCESS;
3333}
3334
3335/**
3336 * @brief Execute the XPath concat(string, string, string*) function.
3337 * Returns LYXP_SET_STRING with the concatenation of all the arguments.
3338 *
3339 * @param[in] args Array of arguments.
3340 * @param[in] arg_count Count of elements in @p args.
3341 * @param[in,out] set Context and result set at the same time.
3342 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003343 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003344 */
3345static LY_ERR
3346xpath_concat(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
3347{
3348 uint16_t i;
3349 char *str = NULL;
3350 size_t used = 1;
3351 LY_ERR rc = LY_SUCCESS;
3352 struct lysc_node_leaf *sleaf;
3353
3354 if (options & LYXP_SCNODE_ALL) {
3355 for (i = 0; i < arg_count; ++i) {
3356 if ((args[i]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[i]))) {
3357 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3358 LOGWRN(set->ctx, "Argument #%u of %s is a %s node \"%s\".",
3359 i + 1, __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003360 } else if (!warn_is_string_type(sleaf->type)) {
3361 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 +02003362 }
3363 }
3364 }
3365 set_scnode_clear_ctx(set);
3366 return rc;
3367 }
3368
3369 for (i = 0; i < arg_count; ++i) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003370 rc = lyxp_set_cast(args[i], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003371 if (rc != LY_SUCCESS) {
3372 free(str);
3373 return rc;
3374 }
3375
3376 str = ly_realloc(str, (used + strlen(args[i]->val.str)) * sizeof(char));
3377 LY_CHECK_ERR_RET(!str, LOGMEM(set->ctx), LY_EMEM);
3378 strcpy(str + used - 1, args[i]->val.str);
3379 used += strlen(args[i]->val.str);
3380 }
3381
3382 /* free, kind of */
Michal Vaskod3678892020-05-21 10:06:58 +02003383 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003384 set->type = LYXP_SET_STRING;
3385 set->val.str = str;
3386
3387 return LY_SUCCESS;
3388}
3389
3390/**
3391 * @brief Execute the XPath contains(string, string) function.
3392 * Returns LYXP_SET_BOOLEAN whether the second argument can
3393 * be found in the first or not.
3394 *
3395 * @param[in] args Array of arguments.
3396 * @param[in] arg_count Count of elements in @p args.
3397 * @param[in,out] set Context and result set at the same time.
3398 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003399 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003400 */
3401static LY_ERR
3402xpath_contains(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3403{
3404 struct lysc_node_leaf *sleaf;
3405 LY_ERR rc = LY_SUCCESS;
3406
3407 if (options & LYXP_SCNODE_ALL) {
3408 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3409 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3410 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 +02003411 } else if (!warn_is_string_type(sleaf->type)) {
3412 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003413 }
3414 }
3415
3416 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3417 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3418 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 +02003419 } else if (!warn_is_string_type(sleaf->type)) {
3420 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003421 }
3422 }
3423 set_scnode_clear_ctx(set);
3424 return rc;
3425 }
3426
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003427 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003428 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003429 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003430 LY_CHECK_RET(rc);
3431
3432 if (strstr(args[0]->val.str, args[1]->val.str)) {
3433 set_fill_boolean(set, 1);
3434 } else {
3435 set_fill_boolean(set, 0);
3436 }
3437
3438 return LY_SUCCESS;
3439}
3440
3441/**
3442 * @brief Execute the XPath count(node-set) function. Returns LYXP_SET_NUMBER
3443 * with the size of the node-set from the argument.
3444 *
3445 * @param[in] args Array of arguments.
3446 * @param[in] arg_count Count of elements in @p args.
3447 * @param[in,out] set Context and result set at the same time.
3448 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003449 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003450 */
3451static LY_ERR
3452xpath_count(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3453{
3454 struct lysc_node *scnode = NULL;
3455 LY_ERR rc = LY_SUCCESS;
3456
3457 if (options & LYXP_SCNODE_ALL) {
3458 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(scnode = warn_get_scnode_in_ctx(args[0]))) {
3459 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003460 }
3461 set_scnode_clear_ctx(set);
3462 return rc;
3463 }
3464
Michal Vasko03ff5a72019-09-11 13:49:33 +02003465 if (args[0]->type != LYXP_SET_NODE_SET) {
3466 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "count(node-set)");
3467 return LY_EVALID;
3468 }
3469
3470 set_fill_number(set, args[0]->used);
3471 return LY_SUCCESS;
3472}
3473
3474/**
3475 * @brief Execute the XPath current() function. Returns LYXP_SET_NODE_SET
3476 * with the context with the intial node.
3477 *
3478 * @param[in] args Array of arguments.
3479 * @param[in] arg_count Count of elements in @p args.
3480 * @param[in,out] set Context and result set at the same time.
3481 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003482 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003483 */
3484static LY_ERR
3485xpath_current(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
3486{
3487 if (arg_count || args) {
3488 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGCOUNT, arg_count, "current()");
3489 return LY_EVALID;
3490 }
3491
3492 if (options & LYXP_SCNODE_ALL) {
3493 set_scnode_clear_ctx(set);
3494
Michal Vaskoecd62de2019-11-13 12:35:11 +01003495 lyxp_set_scnode_insert_node(set, set->ctx_scnode, LYXP_NODE_ELEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003496 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02003497 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003498
3499 /* position is filled later */
3500 set_insert_node(set, set->ctx_node, 0, LYXP_NODE_ELEM, 0);
3501 }
3502
3503 return LY_SUCCESS;
3504}
3505
3506/**
3507 * @brief Execute the YANG 1.1 deref(node-set) function. Returns LYXP_SET_NODE_SET with either
3508 * leafref or instance-identifier target node(s).
3509 *
3510 * @param[in] args Array of arguments.
3511 * @param[in] arg_count Count of elements in @p args.
3512 * @param[in,out] set Context and result set at the same time.
3513 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003514 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003515 */
3516static LY_ERR
3517xpath_deref(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3518{
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003519 struct lysc_ctx cctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003520 struct lyd_node_term *leaf;
Michal Vasko42e497c2020-01-06 08:38:25 +01003521 struct lysc_node_leaf *sleaf = NULL;
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003522 const struct lysc_node *target;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003523 const struct lyd_node *node;
3524 char *errmsg = NULL;
3525 const char *val;
3526 int dynamic;
3527 LY_ERR rc = LY_SUCCESS;
3528
3529 if (options & LYXP_SCNODE_ALL) {
3530 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3531 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003532 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3533 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 +02003534 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_LEAFREF) && !warn_is_specific_type(sleaf->type, LY_TYPE_INST)) {
3535 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"leafref\" nor \"instance-identifier\".",
3536 __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003537 }
3538 set_scnode_clear_ctx(set);
Michal Vasko42e497c2020-01-06 08:38:25 +01003539 if (sleaf && (sleaf->type->basetype == LY_TYPE_LEAFREF)) {
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003540 cctx.ctx = set->ctx;
3541 rc = lys_compile_leafref_validate(&cctx, (struct lysc_node *)sleaf, (struct lysc_type_leafref *)sleaf->type, &target);
3542 /* it was already validated, it must succeed */
3543 if (rc == LY_SUCCESS) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01003544 lyxp_set_scnode_insert_node(set, target, LYXP_NODE_ELEM);
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003545 }
3546 }
3547
Michal Vasko03ff5a72019-09-11 13:49:33 +02003548 return rc;
3549 }
3550
Michal Vaskod3678892020-05-21 10:06:58 +02003551 if (args[0]->type != LYXP_SET_NODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003552 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "deref(node-set)");
3553 return LY_EVALID;
3554 }
3555
Michal Vaskod3678892020-05-21 10:06:58 +02003556 lyxp_set_free_content(set);
3557 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003558 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
3559 sleaf = (struct lysc_node_leaf *)leaf->schema;
3560 if (sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
3561 if (sleaf->type->basetype == LY_TYPE_LEAFREF) {
3562 /* find leafref target */
3563 val = lyd_value2str(leaf, &dynamic);
3564 node = ly_type_find_leafref(set->ctx, sleaf->type, val, strlen(val), (struct lyd_node *)leaf,
Michal Vaskof03ed032020-03-04 13:31:44 +01003565 set->tree, &leaf->value, &errmsg);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003566 if (dynamic) {
3567 free((char *)val);
3568 }
3569 if (!node) {
3570 LOGERR(set->ctx, LY_EINVAL, errmsg);
3571 free(errmsg);
3572 return LY_EINVAL;
3573 }
3574
3575 /* insert it */
3576 set_insert_node(set, node, 0, LYXP_NODE_ELEM, 0);
3577 } else {
3578 assert(sleaf->type->basetype == LY_TYPE_INST);
Michal Vaskof03ed032020-03-04 13:31:44 +01003579 node = (struct lyd_node *)lyd_target(leaf->value.target, set->tree);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003580 if (!node) {
3581 val = lyd_value2str(leaf, &dynamic);
3582 LOGERR(set->ctx, LY_EVALID, "Invalid instance-identifier \"%s\" value - required instance not found.", val);
3583 if (dynamic) {
3584 free((char *)val);
3585 }
3586 return LY_EVALID;
3587 }
3588 }
3589 }
3590 }
3591
3592 return LY_SUCCESS;
3593}
3594
3595/**
3596 * @brief Execute the YANG 1.1 derived-from(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
3597 * on whether the first argument nodes contain a node of an identity derived from the second
3598 * argument identity.
3599 *
3600 * @param[in] args Array of arguments.
3601 * @param[in] arg_count Count of elements in @p args.
3602 * @param[in,out] set Context and result set at the same time.
3603 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003604 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003605 */
3606static LY_ERR
3607xpath_derived_from(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3608{
3609 uint16_t i;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003610 LY_ARRAY_SIZE_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003611 struct lyd_node_term *leaf;
3612 struct lysc_node_leaf *sleaf;
3613 struct lyd_value data = {0};
3614 struct ly_err_item *err = NULL;
3615 LY_ERR rc = LY_SUCCESS;
3616 int found;
3617
3618 if (options & LYXP_SCNODE_ALL) {
3619 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3620 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003621 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3622 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 +02003623 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_IDENT)) {
3624 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"identityref\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003625 }
3626
3627 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3628 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3629 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 +02003630 } else if (!warn_is_string_type(sleaf->type)) {
3631 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003632 }
3633 }
3634 set_scnode_clear_ctx(set);
3635 return rc;
3636 }
3637
Michal Vaskod3678892020-05-21 10:06:58 +02003638 if (args[0]->type != LYXP_SET_NODE_SET) {
3639 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
3640 "derived-from(node-set, string)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003641 return LY_EVALID;
3642 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003643 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003644 LY_CHECK_RET(rc);
3645
3646 set_fill_boolean(set, 0);
Michal Vaskod3678892020-05-21 10:06:58 +02003647 found = 0;
3648 for (i = 0; i < args[0]->used; ++i) {
3649 leaf = (struct lyd_node_term *)args[0]->val.nodes[i].node;
3650 sleaf = (struct lysc_node_leaf *)leaf->schema;
3651 if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_IDENT)) {
3652 /* store args[1] into ident */
3653 rc = sleaf->type->plugin->store(set->ctx, sleaf->type, args[1]->val.str, strlen(args[1]->val.str),
3654 LY_TYPE_OPTS_STORE, lys_resolve_prefix, (void *)sleaf->dflt_mod, set->format,
3655 (struct lyd_node *)leaf, set->tree, &data, NULL, &err);
3656 if (err) {
3657 ly_err_print(err);
3658 ly_err_free(err);
3659 }
3660 LY_CHECK_RET(rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003661
Michal Vaskod3678892020-05-21 10:06:58 +02003662 LY_ARRAY_FOR(data.ident->derived, u) {
3663 if (data.ident->derived[u] == leaf->value.ident) {
3664 set_fill_boolean(set, 1);
3665 found = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003666 break;
3667 }
3668 }
Michal Vaskod3678892020-05-21 10:06:58 +02003669 if (found) {
3670 break;
3671 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003672 }
3673 }
3674
3675 return LY_SUCCESS;
3676}
3677
3678/**
3679 * @brief Execute the YANG 1.1 derived-from-or-self(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
3680 * on whether the first argument nodes contain a node of an identity that either is or is derived from
3681 * the second argument identity.
3682 *
3683 * @param[in] args Array of arguments.
3684 * @param[in] arg_count Count of elements in @p args.
3685 * @param[in,out] set Context and result set at the same time.
3686 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003687 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003688 */
3689static LY_ERR
3690xpath_derived_from_or_self(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3691{
3692 uint16_t i;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003693 LY_ARRAY_SIZE_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003694 struct lyd_node_term *leaf;
3695 struct lysc_node_leaf *sleaf;
3696 struct lyd_value data = {0};
3697 struct ly_err_item *err = NULL;
3698 LY_ERR rc = LY_SUCCESS;
3699 int found;
3700
3701 if (options & LYXP_SCNODE_ALL) {
3702 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3703 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003704 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3705 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 +02003706 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_IDENT)) {
3707 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"identityref\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003708 }
3709
3710 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3711 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3712 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 +02003713 } else if (!warn_is_string_type(sleaf->type)) {
3714 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003715 }
3716 }
3717 set_scnode_clear_ctx(set);
3718 return rc;
3719 }
3720
Michal Vaskod3678892020-05-21 10:06:58 +02003721 if (args[0]->type != LYXP_SET_NODE_SET) {
3722 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
3723 "derived-from-or-self(node-set, string)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003724 return LY_EVALID;
3725 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003726 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003727 LY_CHECK_RET(rc);
3728
3729 set_fill_boolean(set, 0);
Michal Vaskod3678892020-05-21 10:06:58 +02003730 found = 0;
3731 for (i = 0; i < args[0]->used; ++i) {
3732 leaf = (struct lyd_node_term *)args[0]->val.nodes[i].node;
3733 sleaf = (struct lysc_node_leaf *)leaf->schema;
3734 if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_IDENT)) {
3735 /* store args[1] into ident */
3736 rc = sleaf->type->plugin->store(set->ctx, sleaf->type, args[1]->val.str, strlen(args[1]->val.str),
3737 LY_TYPE_OPTS_STORE, lys_resolve_prefix, (void *)sleaf->dflt_mod, set->format,
3738 (struct lyd_node *)leaf, set->tree, &data, NULL, &err);
3739 if (err) {
3740 ly_err_print(err);
3741 ly_err_free(err);
3742 }
3743 LY_CHECK_RET(rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003744
Michal Vaskod3678892020-05-21 10:06:58 +02003745 if (data.ident == leaf->value.ident) {
3746 set_fill_boolean(set, 1);
3747 break;
3748 }
3749 LY_ARRAY_FOR(data.ident->derived, u) {
3750 if (data.ident->derived[u] == leaf->value.ident) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003751 set_fill_boolean(set, 1);
Michal Vaskod3678892020-05-21 10:06:58 +02003752 found = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003753 break;
3754 }
Michal Vaskod3678892020-05-21 10:06:58 +02003755 }
3756 if (found) {
3757 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003758 }
3759 }
3760 }
3761
3762 return LY_SUCCESS;
3763}
3764
3765/**
3766 * @brief Execute the YANG 1.1 enum-value(node-set) function. Returns LYXP_SET_NUMBER
3767 * with the integer value of the first node's enum value, otherwise NaN.
3768 *
3769 * @param[in] args Array of arguments.
3770 * @param[in] arg_count Count of elements in @p args.
3771 * @param[in,out] set Context and result set at the same time.
3772 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003773 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003774 */
3775static LY_ERR
3776xpath_enum_value(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3777{
3778 struct lyd_node_term *leaf;
3779 struct lysc_node_leaf *sleaf;
3780 LY_ERR rc = LY_SUCCESS;
3781
3782 if (options & LYXP_SCNODE_ALL) {
3783 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3784 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003785 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3786 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 +02003787 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_ENUM)) {
3788 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"enumeration\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003789 }
3790 set_scnode_clear_ctx(set);
3791 return rc;
3792 }
3793
Michal Vaskod3678892020-05-21 10:06:58 +02003794 if (args[0]->type != LYXP_SET_NODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003795 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "enum-value(node-set)");
3796 return LY_EVALID;
3797 }
3798
3799 set_fill_number(set, NAN);
Michal Vaskod3678892020-05-21 10:06:58 +02003800 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003801 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
3802 sleaf = (struct lysc_node_leaf *)leaf->schema;
3803 if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_ENUM)) {
3804 set_fill_number(set, leaf->value.enum_item->value);
3805 }
3806 }
3807
3808 return LY_SUCCESS;
3809}
3810
3811/**
3812 * @brief Execute the XPath false() function. Returns LYXP_SET_BOOLEAN
3813 * with false value.
3814 *
3815 * @param[in] args Array of arguments.
3816 * @param[in] arg_count Count of elements in @p args.
3817 * @param[in,out] set Context and result set at the same time.
3818 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003819 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003820 */
3821static LY_ERR
3822xpath_false(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3823{
3824 if (options & LYXP_SCNODE_ALL) {
3825 set_scnode_clear_ctx(set);
3826 return LY_SUCCESS;
3827 }
3828
3829 set_fill_boolean(set, 0);
3830 return LY_SUCCESS;
3831}
3832
3833/**
3834 * @brief Execute the XPath floor(number) function. Returns LYXP_SET_NUMBER
3835 * with the first argument floored (truncated).
3836 *
3837 * @param[in] args Array of arguments.
3838 * @param[in] arg_count Count of elements in @p args.
3839 * @param[in,out] set Context and result set at the same time.
3840 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003841 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003842 */
3843static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003844xpath_floor(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int UNUSED(options))
Michal Vasko03ff5a72019-09-11 13:49:33 +02003845{
3846 LY_ERR rc;
3847
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003848 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003849 LY_CHECK_RET(rc);
3850 if (isfinite(args[0]->val.num)) {
3851 set_fill_number(set, (long long)args[0]->val.num);
3852 }
3853
3854 return LY_SUCCESS;
3855}
3856
3857/**
3858 * @brief Execute the XPath lang(string) function. Returns LYXP_SET_BOOLEAN
3859 * whether the language of the text matches the one from the argument.
3860 *
3861 * @param[in] args Array of arguments.
3862 * @param[in] arg_count Count of elements in @p args.
3863 * @param[in,out] set Context and result set at the same time.
3864 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003865 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003866 */
3867static LY_ERR
3868xpath_lang(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3869{
3870 const struct lyd_node *node;
3871 struct lysc_node_leaf *sleaf;
Michal Vasko9f96a052020-03-10 09:41:45 +01003872 struct lyd_meta *meta = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003873 const char *val;
3874 int i, dynamic;
3875 LY_ERR rc = LY_SUCCESS;
3876
3877 if (options & LYXP_SCNODE_ALL) {
3878 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3879 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3880 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 +02003881 } else if (!warn_is_string_type(sleaf->type)) {
3882 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003883 }
3884 }
3885 set_scnode_clear_ctx(set);
3886 return rc;
3887 }
3888
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003889 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003890 LY_CHECK_RET(rc);
3891
Michal Vasko03ff5a72019-09-11 13:49:33 +02003892 if (set->type != LYXP_SET_NODE_SET) {
3893 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "lang(string)");
3894 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02003895 } else if (!set->used) {
3896 set_fill_boolean(set, 0);
3897 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003898 }
3899
3900 switch (set->val.nodes[0].type) {
3901 case LYXP_NODE_ELEM:
3902 case LYXP_NODE_TEXT:
3903 node = set->val.nodes[0].node;
3904 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01003905 case LYXP_NODE_META:
3906 node = set->val.meta[0].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003907 break;
3908 default:
3909 /* nothing to do with roots */
3910 set_fill_boolean(set, 0);
3911 return LY_SUCCESS;
3912 }
3913
Michal Vasko9f96a052020-03-10 09:41:45 +01003914 /* find lang metadata */
Michal Vasko03ff5a72019-09-11 13:49:33 +02003915 for (; node; node = (struct lyd_node *)node->parent) {
Michal Vasko9f96a052020-03-10 09:41:45 +01003916 for (meta = node->meta; meta; meta = meta->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003917 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01003918 if (meta->name && !strcmp(meta->name, "lang") && !strcmp(meta->annotation->module->name, "xml")) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003919 break;
3920 }
3921 }
3922
Michal Vasko9f96a052020-03-10 09:41:45 +01003923 if (meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003924 break;
3925 }
3926 }
3927
3928 /* compare languages */
Michal Vasko9f96a052020-03-10 09:41:45 +01003929 if (!meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003930 set_fill_boolean(set, 0);
3931 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01003932 val = lyd_meta2str(meta, &dynamic);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003933 for (i = 0; args[0]->val.str[i]; ++i) {
3934 if (tolower(args[0]->val.str[i]) != tolower(val[i])) {
3935 set_fill_boolean(set, 0);
3936 break;
3937 }
3938 }
3939 if (!args[0]->val.str[i]) {
3940 if (!val[i] || (val[i] == '-')) {
3941 set_fill_boolean(set, 1);
3942 } else {
3943 set_fill_boolean(set, 0);
3944 }
3945 }
3946 if (dynamic) {
3947 free((char *)val);
3948 }
3949 }
3950
3951 return LY_SUCCESS;
3952}
3953
3954/**
3955 * @brief Execute the XPath last() function. Returns LYXP_SET_NUMBER
3956 * with the context size.
3957 *
3958 * @param[in] args Array of arguments.
3959 * @param[in] arg_count Count of elements in @p args.
3960 * @param[in,out] set Context and result set at the same time.
3961 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003962 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003963 */
3964static LY_ERR
3965xpath_last(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3966{
3967 if (options & LYXP_SCNODE_ALL) {
3968 set_scnode_clear_ctx(set);
3969 return LY_SUCCESS;
3970 }
3971
Michal Vasko03ff5a72019-09-11 13:49:33 +02003972 if (set->type != LYXP_SET_NODE_SET) {
3973 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "last()");
3974 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02003975 } else if (!set->used) {
3976 set_fill_number(set, 0);
3977 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003978 }
3979
3980 set_fill_number(set, set->ctx_size);
3981 return LY_SUCCESS;
3982}
3983
3984/**
3985 * @brief Execute the XPath local-name(node-set?) function. Returns LYXP_SET_STRING
3986 * with the node name without namespace from the argument or the context.
3987 *
3988 * @param[in] args Array of arguments.
3989 * @param[in] arg_count Count of elements in @p args.
3990 * @param[in,out] set Context and result set at the same time.
3991 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003992 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003993 */
3994static LY_ERR
3995xpath_local_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
3996{
3997 struct lyxp_set_node *item;
3998 /* suppress unused variable warning */
3999 (void)options;
4000
4001 if (options & LYXP_SCNODE_ALL) {
4002 set_scnode_clear_ctx(set);
4003 return LY_SUCCESS;
4004 }
4005
4006 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004007 if (args[0]->type != LYXP_SET_NODE_SET) {
4008 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "local-name(node-set?)");
4009 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004010 } else if (!args[0]->used) {
4011 set_fill_string(set, "", 0);
4012 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004013 }
4014
4015 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004016 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004017
4018 item = &args[0]->val.nodes[0];
4019 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004020 if (set->type != LYXP_SET_NODE_SET) {
4021 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "local-name(node-set?)");
4022 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004023 } else if (!set->used) {
4024 set_fill_string(set, "", 0);
4025 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004026 }
4027
4028 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004029 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004030
4031 item = &set->val.nodes[0];
4032 }
4033
4034 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004035 case LYXP_NODE_NONE:
4036 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004037 case LYXP_NODE_ROOT:
4038 case LYXP_NODE_ROOT_CONFIG:
4039 case LYXP_NODE_TEXT:
4040 set_fill_string(set, "", 0);
4041 break;
4042 case LYXP_NODE_ELEM:
4043 set_fill_string(set, item->node->schema->name, strlen(item->node->schema->name));
4044 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004045 case LYXP_NODE_META:
4046 set_fill_string(set, ((struct lyd_meta *)item->node)->name, strlen(((struct lyd_meta *)item->node)->name));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004047 break;
4048 }
4049
4050 return LY_SUCCESS;
4051}
4052
4053/**
4054 * @brief Execute the XPath name(node-set?) function. Returns LYXP_SET_STRING
4055 * with the node name fully qualified (with namespace) from the argument or the context.
4056 *
4057 * @param[in] args Array of arguments.
4058 * @param[in] arg_count Count of elements in @p args.
4059 * @param[in,out] set Context and result set at the same time.
4060 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004061 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004062 */
4063static LY_ERR
4064xpath_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
4065{
4066 struct lyxp_set_node *item;
4067 struct lys_module *mod;
4068 char *str;
4069 const char *name;
4070 int rc;
4071
4072 if (options & LYXP_SCNODE_ALL) {
4073 set_scnode_clear_ctx(set);
4074 return LY_SUCCESS;
4075 }
4076
4077 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004078 if (args[0]->type != LYXP_SET_NODE_SET) {
4079 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "name(node-set?)");
4080 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004081 } else if (!args[0]->used) {
4082 set_fill_string(set, "", 0);
4083 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004084 }
4085
4086 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004087 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004088
4089 item = &args[0]->val.nodes[0];
4090 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004091 if (set->type != LYXP_SET_NODE_SET) {
4092 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "name(node-set?)");
4093 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004094 } else if (!set->used) {
4095 set_fill_string(set, "", 0);
4096 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004097 }
4098
4099 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004100 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004101
4102 item = &set->val.nodes[0];
4103 }
4104
4105 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004106 case LYXP_NODE_NONE:
4107 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004108 case LYXP_NODE_ROOT:
4109 case LYXP_NODE_ROOT_CONFIG:
4110 case LYXP_NODE_TEXT:
4111 mod = NULL;
4112 name = NULL;
4113 break;
4114 case LYXP_NODE_ELEM:
4115 mod = item->node->schema->module;
4116 name = item->node->schema->name;
4117 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004118 case LYXP_NODE_META:
4119 mod = ((struct lyd_meta *)item->node)->annotation->module;
4120 name = ((struct lyd_meta *)item->node)->name;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004121 break;
4122 }
4123
4124 if (mod && name) {
4125 switch (set->format) {
Michal Vasko52927e22020-03-16 17:26:14 +01004126 case LYD_SCHEMA:
Michal Vasko03ff5a72019-09-11 13:49:33 +02004127 rc = asprintf(&str, "%s:%s", lys_prefix_find_module(set->local_mod, mod), name);
4128 break;
4129 case LYD_JSON:
4130 rc = asprintf(&str, "%s:%s", mod->name, name);
4131 break;
Michal Vasko52927e22020-03-16 17:26:14 +01004132 case LYD_XML:
Michal Vasko9409ef62019-09-12 11:47:17 +02004133 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004134 }
4135 LY_CHECK_ERR_RET(rc == -1, LOGMEM(set->ctx), LY_EMEM);
4136 set_fill_string(set, str, strlen(str));
4137 free(str);
4138 } else {
4139 set_fill_string(set, "", 0);
4140 }
4141
4142 return LY_SUCCESS;
4143}
4144
4145/**
4146 * @brief Execute the XPath namespace-uri(node-set?) function. Returns LYXP_SET_STRING
4147 * with the namespace of the node from the argument or the context.
4148 *
4149 * @param[in] args Array of arguments.
4150 * @param[in] arg_count Count of elements in @p args.
4151 * @param[in,out] set Context and result set at the same time.
4152 * @param[in] options XPath options.
4153 * @return LY_ERR (LY_EINVAL for wrong arguments on schema)
4154 */
4155static LY_ERR
4156xpath_namespace_uri(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
4157{
4158 struct lyxp_set_node *item;
4159 struct lys_module *mod;
4160 /* suppress unused variable warning */
4161 (void)options;
4162
4163 if (options & LYXP_SCNODE_ALL) {
4164 set_scnode_clear_ctx(set);
4165 return LY_SUCCESS;
4166 }
4167
4168 if (arg_count) {
Michal Vaskod3678892020-05-21 10:06:58 +02004169 if (args[0]->type != LYXP_SET_NODE_SET) {
4170 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
4171 "namespace-uri(node-set?)");
4172 return LY_EVALID;
4173 } else if (!args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004174 set_fill_string(set, "", 0);
4175 return LY_SUCCESS;
4176 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004177
4178 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004179 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004180
4181 item = &args[0]->val.nodes[0];
4182 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004183 if (set->type != LYXP_SET_NODE_SET) {
4184 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "namespace-uri(node-set?)");
4185 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004186 } else if (!set->used) {
4187 set_fill_string(set, "", 0);
4188 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004189 }
4190
4191 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004192 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004193
4194 item = &set->val.nodes[0];
4195 }
4196
4197 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004198 case LYXP_NODE_NONE:
4199 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004200 case LYXP_NODE_ROOT:
4201 case LYXP_NODE_ROOT_CONFIG:
4202 case LYXP_NODE_TEXT:
4203 set_fill_string(set, "", 0);
4204 break;
4205 case LYXP_NODE_ELEM:
Michal Vasko9f96a052020-03-10 09:41:45 +01004206 case LYXP_NODE_META:
Michal Vasko03ff5a72019-09-11 13:49:33 +02004207 if (item->type == LYXP_NODE_ELEM) {
4208 mod = item->node->schema->module;
Michal Vasko9f96a052020-03-10 09:41:45 +01004209 } else { /* LYXP_NODE_META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004210 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004211 mod = ((struct lyd_meta *)item->node)->annotation->module;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004212 }
4213
4214 set_fill_string(set, mod->ns, strlen(mod->ns));
4215 break;
4216 }
4217
4218 return LY_SUCCESS;
4219}
4220
4221/**
4222 * @brief Execute the XPath node() function (node type). Returns LYXP_SET_NODE_SET
4223 * with only nodes from the context. In practice it either leaves the context
4224 * as it is or returns an empty node set.
4225 *
4226 * @param[in] args Array of arguments.
4227 * @param[in] arg_count Count of elements in @p args.
4228 * @param[in,out] set Context and result set at the same time.
4229 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004230 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004231 */
4232static LY_ERR
4233xpath_node(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4234{
4235 if (options & LYXP_SCNODE_ALL) {
4236 set_scnode_clear_ctx(set);
4237 return LY_SUCCESS;
4238 }
4239
4240 if (set->type != LYXP_SET_NODE_SET) {
Michal Vaskod3678892020-05-21 10:06:58 +02004241 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004242 }
4243 return LY_SUCCESS;
4244}
4245
4246/**
4247 * @brief Execute the XPath normalize-space(string?) function. Returns LYXP_SET_STRING
4248 * with normalized value (no leading, trailing, double white spaces) of the node
4249 * from the argument or the context.
4250 *
4251 * @param[in] args Array of arguments.
4252 * @param[in] arg_count Count of elements in @p args.
4253 * @param[in,out] set Context and result set at the same time.
4254 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004255 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004256 */
4257static LY_ERR
4258xpath_normalize_space(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
4259{
4260 uint16_t i, new_used;
4261 char *new;
4262 int have_spaces = 0, space_before = 0;
4263 struct lysc_node_leaf *sleaf;
4264 LY_ERR rc = LY_SUCCESS;
4265
4266 if (options & LYXP_SCNODE_ALL) {
4267 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4268 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4269 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 +02004270 } else if (!warn_is_string_type(sleaf->type)) {
4271 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004272 }
4273 }
4274 set_scnode_clear_ctx(set);
4275 return rc;
4276 }
4277
4278 if (arg_count) {
4279 set_fill_set(set, args[0]);
4280 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004281 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004282 LY_CHECK_RET(rc);
4283
4284 /* is there any normalization necessary? */
4285 for (i = 0; set->val.str[i]; ++i) {
4286 if (is_xmlws(set->val.str[i])) {
4287 if ((i == 0) || space_before || (!set->val.str[i + 1])) {
4288 have_spaces = 1;
4289 break;
4290 }
4291 space_before = 1;
4292 } else {
4293 space_before = 0;
4294 }
4295 }
4296
4297 /* yep, there is */
4298 if (have_spaces) {
4299 /* it's enough, at least one character will go, makes space for ending '\0' */
4300 new = malloc(strlen(set->val.str) * sizeof(char));
4301 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4302 new_used = 0;
4303
4304 space_before = 0;
4305 for (i = 0; set->val.str[i]; ++i) {
4306 if (is_xmlws(set->val.str[i])) {
4307 if ((i == 0) || space_before) {
4308 space_before = 1;
4309 continue;
4310 } else {
4311 space_before = 1;
4312 }
4313 } else {
4314 space_before = 0;
4315 }
4316
4317 new[new_used] = (space_before ? ' ' : set->val.str[i]);
4318 ++new_used;
4319 }
4320
4321 /* at worst there is one trailing space now */
4322 if (new_used && is_xmlws(new[new_used - 1])) {
4323 --new_used;
4324 }
4325
4326 new = ly_realloc(new, (new_used + 1) * sizeof(char));
4327 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4328 new[new_used] = '\0';
4329
4330 free(set->val.str);
4331 set->val.str = new;
4332 }
4333
4334 return LY_SUCCESS;
4335}
4336
4337/**
4338 * @brief Execute the XPath not(boolean) function. Returns LYXP_SET_BOOLEAN
4339 * with the argument converted to boolean and logically inverted.
4340 *
4341 * @param[in] args Array of arguments.
4342 * @param[in] arg_count Count of elements in @p args.
4343 * @param[in,out] set Context and result set at the same time.
4344 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004345 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004346 */
4347static LY_ERR
4348xpath_not(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4349{
4350 if (options & LYXP_SCNODE_ALL) {
4351 set_scnode_clear_ctx(set);
4352 return LY_SUCCESS;
4353 }
4354
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004355 lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004356 if (args[0]->val.bool) {
4357 set_fill_boolean(set, 0);
4358 } else {
4359 set_fill_boolean(set, 1);
4360 }
4361
4362 return LY_SUCCESS;
4363}
4364
4365/**
4366 * @brief Execute the XPath number(object?) function. Returns LYXP_SET_NUMBER
4367 * with the number representation of either the argument or the context.
4368 *
4369 * @param[in] args Array of arguments.
4370 * @param[in] arg_count Count of elements in @p args.
4371 * @param[in,out] set Context and result set at the same time.
4372 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004373 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004374 */
4375static LY_ERR
4376xpath_number(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
4377{
4378 LY_ERR rc;
4379
4380 if (options & LYXP_SCNODE_ALL) {
4381 set_scnode_clear_ctx(set);
4382 return LY_SUCCESS;
4383 }
4384
4385 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004386 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004387 LY_CHECK_RET(rc);
4388 set_fill_set(set, args[0]);
4389 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004390 rc = lyxp_set_cast(set, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004391 LY_CHECK_RET(rc);
4392 }
4393
4394 return LY_SUCCESS;
4395}
4396
4397/**
4398 * @brief Execute the XPath position() function. Returns LYXP_SET_NUMBER
4399 * with the context position.
4400 *
4401 * @param[in] args Array of arguments.
4402 * @param[in] arg_count Count of elements in @p args.
4403 * @param[in,out] set Context and result set at the same time.
4404 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004405 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004406 */
4407static LY_ERR
4408xpath_position(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4409{
4410 if (options & LYXP_SCNODE_ALL) {
4411 set_scnode_clear_ctx(set);
4412 return LY_SUCCESS;
4413 }
4414
Michal Vasko03ff5a72019-09-11 13:49:33 +02004415 if (set->type != LYXP_SET_NODE_SET) {
4416 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "position()");
4417 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004418 } else if (!set->used) {
4419 set_fill_number(set, 0);
4420 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004421 }
4422
4423 set_fill_number(set, set->ctx_pos);
4424
4425 /* UNUSED in 'Release' build type */
4426 (void)options;
4427 return LY_SUCCESS;
4428}
4429
4430/**
4431 * @brief Execute the YANG 1.1 re-match(string, string) function. Returns LYXP_SET_BOOLEAN
4432 * depending on whether the second argument regex matches the first argument string. For details refer to
4433 * YANG 1.1 RFC section 10.2.1.
4434 *
4435 * @param[in] args Array of arguments.
4436 * @param[in] arg_count Count of elements in @p args.
4437 * @param[in,out] set Context and result set at the same time.
4438 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004439 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004440 */
4441static LY_ERR
4442xpath_re_match(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4443{
4444 struct lysc_pattern **patterns = NULL, **pattern;
4445 struct lysc_node_leaf *sleaf;
4446 char *path;
4447 LY_ERR rc = LY_SUCCESS;
4448 struct ly_err_item *err;
4449
4450 if (options & LYXP_SCNODE_ALL) {
4451 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4452 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4453 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 +02004454 } else if (!warn_is_string_type(sleaf->type)) {
4455 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004456 }
4457 }
4458
4459 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4460 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4461 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 +02004462 } else if (!warn_is_string_type(sleaf->type)) {
4463 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004464 }
4465 }
4466 set_scnode_clear_ctx(set);
4467 return rc;
4468 }
4469
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004470 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004471 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004472 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004473 LY_CHECK_RET(rc);
4474
4475 LY_ARRAY_NEW_RET(set->ctx, patterns, pattern, LY_EMEM);
4476 *pattern = malloc(sizeof **pattern);
4477 path = lyd_path(set->ctx_node, LYD_PATH_LOG, NULL, 0);
4478 rc = lys_compile_type_pattern_check(set->ctx, path, args[1]->val.str, &(*pattern)->code);
4479 free(path);
4480 if (rc != LY_SUCCESS) {
4481 LY_ARRAY_FREE(patterns);
4482 return rc;
4483 }
4484
4485 rc = ly_type_validate_patterns(patterns, args[0]->val.str, strlen(args[0]->val.str), &err);
4486 pcre2_code_free((*pattern)->code);
4487 free(*pattern);
4488 LY_ARRAY_FREE(patterns);
4489 if (rc && (rc != LY_EVALID)) {
4490 ly_err_print(err);
4491 ly_err_free(err);
4492 return rc;
4493 }
4494
4495 if (rc == LY_EVALID) {
4496 ly_err_free(err);
4497 set_fill_boolean(set, 0);
4498 } else {
4499 set_fill_boolean(set, 1);
4500 }
4501
4502 return LY_SUCCESS;
4503}
4504
4505/**
4506 * @brief Execute the XPath round(number) function. Returns LYXP_SET_NUMBER
4507 * with the rounded first argument. For details refer to
4508 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-round.
4509 *
4510 * @param[in] args Array of arguments.
4511 * @param[in] arg_count Count of elements in @p args.
4512 * @param[in,out] set Context and result set at the same time.
4513 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004514 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004515 */
4516static LY_ERR
4517xpath_round(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4518{
4519 struct lysc_node_leaf *sleaf;
4520 LY_ERR rc = LY_SUCCESS;
4521
4522 if (options & LYXP_SCNODE_ALL) {
4523 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4524 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004525 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4526 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 +02004527 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
4528 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004529 }
4530 set_scnode_clear_ctx(set);
4531 return rc;
4532 }
4533
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004534 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004535 LY_CHECK_RET(rc);
4536
4537 /* cover only the cases where floor can't be used */
4538 if ((args[0]->val.num == -0.0f) || ((args[0]->val.num < 0) && (args[0]->val.num >= -0.5))) {
4539 set_fill_number(set, -0.0f);
4540 } else {
4541 args[0]->val.num += 0.5;
4542 rc = xpath_floor(args, 1, args[0], options);
4543 LY_CHECK_RET(rc);
4544 set_fill_number(set, args[0]->val.num);
4545 }
4546
4547 return LY_SUCCESS;
4548}
4549
4550/**
4551 * @brief Execute the XPath starts-with(string, string) function.
4552 * Returns LYXP_SET_BOOLEAN whether the second argument is
4553 * the prefix of the first or not.
4554 *
4555 * @param[in] args Array of arguments.
4556 * @param[in] arg_count Count of elements in @p args.
4557 * @param[in,out] set Context and result set at the same time.
4558 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004559 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004560 */
4561static LY_ERR
4562xpath_starts_with(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4563{
4564 struct lysc_node_leaf *sleaf;
4565 LY_ERR rc = LY_SUCCESS;
4566
4567 if (options & LYXP_SCNODE_ALL) {
4568 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4569 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4570 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 +02004571 } else if (!warn_is_string_type(sleaf->type)) {
4572 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004573 }
4574 }
4575
4576 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4577 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4578 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 +02004579 } else if (!warn_is_string_type(sleaf->type)) {
4580 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004581 }
4582 }
4583 set_scnode_clear_ctx(set);
4584 return rc;
4585 }
4586
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004587 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004588 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004589 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004590 LY_CHECK_RET(rc);
4591
4592 if (strncmp(args[0]->val.str, args[1]->val.str, strlen(args[1]->val.str))) {
4593 set_fill_boolean(set, 0);
4594 } else {
4595 set_fill_boolean(set, 1);
4596 }
4597
4598 return LY_SUCCESS;
4599}
4600
4601/**
4602 * @brief Execute the XPath string(object?) function. Returns LYXP_SET_STRING
4603 * with the string representation of either the argument or the context.
4604 *
4605 * @param[in] args Array of arguments.
4606 * @param[in] arg_count Count of elements in @p args.
4607 * @param[in,out] set Context and result set at the same time.
4608 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004609 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004610 */
4611static LY_ERR
4612xpath_string(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
4613{
4614 LY_ERR rc;
4615
4616 if (options & LYXP_SCNODE_ALL) {
4617 set_scnode_clear_ctx(set);
4618 return LY_SUCCESS;
4619 }
4620
4621 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004622 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004623 LY_CHECK_RET(rc);
4624 set_fill_set(set, args[0]);
4625 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004626 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004627 LY_CHECK_RET(rc);
4628 }
4629
4630 return LY_SUCCESS;
4631}
4632
4633/**
4634 * @brief Execute the XPath string-length(string?) function. Returns LYXP_SET_NUMBER
4635 * with the length of the string in either the argument or the context.
4636 *
4637 * @param[in] args Array of arguments.
4638 * @param[in] arg_count Count of elements in @p args.
4639 * @param[in,out] set Context and result set at the same time.
4640 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004641 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004642 */
4643static LY_ERR
4644xpath_string_length(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
4645{
4646 struct lysc_node_leaf *sleaf;
4647 LY_ERR rc = LY_SUCCESS;
4648
4649 if (options & LYXP_SCNODE_ALL) {
4650 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4651 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4652 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 +02004653 } else if (!warn_is_string_type(sleaf->type)) {
4654 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004655 }
4656 }
4657 if (!arg_count && (set->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set))) {
4658 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4659 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 +02004660 } else if (!warn_is_string_type(sleaf->type)) {
4661 LOGWRN(set->ctx, "Argument #0 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004662 }
4663 }
4664 set_scnode_clear_ctx(set);
4665 return rc;
4666 }
4667
4668 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004669 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004670 LY_CHECK_RET(rc);
4671 set_fill_number(set, strlen(args[0]->val.str));
4672 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004673 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004674 LY_CHECK_RET(rc);
4675 set_fill_number(set, strlen(set->val.str));
4676 }
4677
4678 return LY_SUCCESS;
4679}
4680
4681/**
4682 * @brief Execute the XPath substring(string, number, number?) function.
4683 * Returns LYXP_SET_STRING substring of the first argument starting
4684 * on the second argument index ending on the third argument index,
4685 * indexed from 1. For exact definition refer to
4686 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-substring.
4687 *
4688 * @param[in] args Array of arguments.
4689 * @param[in] arg_count Count of elements in @p args.
4690 * @param[in,out] set Context and result set at the same time.
4691 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004692 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004693 */
4694static LY_ERR
4695xpath_substring(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
4696{
4697 int start, len;
4698 uint16_t str_start, str_len, pos;
4699 struct lysc_node_leaf *sleaf;
4700 LY_ERR rc = LY_SUCCESS;
4701
4702 if (options & LYXP_SCNODE_ALL) {
4703 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4704 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4705 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 +02004706 } else if (!warn_is_string_type(sleaf->type)) {
4707 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004708 }
4709 }
4710
4711 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4712 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4713 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 +02004714 } else if (!warn_is_numeric_type(sleaf->type)) {
4715 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004716 }
4717 }
4718
4719 if ((arg_count == 3) && (args[2]->type == LYXP_SET_SCNODE_SET)
4720 && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
4721 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4722 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 +02004723 } else if (!warn_is_numeric_type(sleaf->type)) {
4724 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004725 }
4726 }
4727 set_scnode_clear_ctx(set);
4728 return rc;
4729 }
4730
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004731 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004732 LY_CHECK_RET(rc);
4733
4734 /* start */
4735 if (xpath_round(&args[1], 1, args[1], options)) {
4736 return -1;
4737 }
4738 if (isfinite(args[1]->val.num)) {
4739 start = args[1]->val.num - 1;
4740 } else if (isinf(args[1]->val.num) && signbit(args[1]->val.num)) {
4741 start = INT_MIN;
4742 } else {
4743 start = INT_MAX;
4744 }
4745
4746 /* len */
4747 if (arg_count == 3) {
4748 rc = xpath_round(&args[2], 1, args[2], options);
4749 LY_CHECK_RET(rc);
4750 if (isfinite(args[2]->val.num)) {
4751 len = args[2]->val.num;
4752 } else if (isnan(args[2]->val.num) || signbit(args[2]->val.num)) {
4753 len = 0;
4754 } else {
4755 len = INT_MAX;
4756 }
4757 } else {
4758 len = INT_MAX;
4759 }
4760
4761 /* find matching character positions */
4762 str_start = 0;
4763 str_len = 0;
4764 for (pos = 0; args[0]->val.str[pos]; ++pos) {
4765 if (pos < start) {
4766 ++str_start;
4767 } else if (pos < start + len) {
4768 ++str_len;
4769 } else {
4770 break;
4771 }
4772 }
4773
4774 set_fill_string(set, args[0]->val.str + str_start, str_len);
4775 return LY_SUCCESS;
4776}
4777
4778/**
4779 * @brief Execute the XPath substring-after(string, string) function.
4780 * Returns LYXP_SET_STRING with the string succeeding the occurance
4781 * of the second argument in the first or an empty string.
4782 *
4783 * @param[in] args Array of arguments.
4784 * @param[in] arg_count Count of elements in @p args.
4785 * @param[in,out] set Context and result set at the same time.
4786 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004787 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004788 */
4789static LY_ERR
4790xpath_substring_after(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4791{
4792 char *ptr;
4793 struct lysc_node_leaf *sleaf;
4794 LY_ERR rc = LY_SUCCESS;
4795
4796 if (options & LYXP_SCNODE_ALL) {
4797 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4798 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4799 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 +02004800 } else if (!warn_is_string_type(sleaf->type)) {
4801 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004802 }
4803 }
4804
4805 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4806 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4807 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 +02004808 } else if (!warn_is_string_type(sleaf->type)) {
4809 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004810 }
4811 }
4812 set_scnode_clear_ctx(set);
4813 return rc;
4814 }
4815
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004816 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004817 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004818 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004819 LY_CHECK_RET(rc);
4820
4821 ptr = strstr(args[0]->val.str, args[1]->val.str);
4822 if (ptr) {
4823 set_fill_string(set, ptr + strlen(args[1]->val.str), strlen(ptr + strlen(args[1]->val.str)));
4824 } else {
4825 set_fill_string(set, "", 0);
4826 }
4827
4828 return LY_SUCCESS;
4829}
4830
4831/**
4832 * @brief Execute the XPath substring-before(string, string) function.
4833 * Returns LYXP_SET_STRING with the string preceding the occurance
4834 * of the second argument in the first or an empty string.
4835 *
4836 * @param[in] args Array of arguments.
4837 * @param[in] arg_count Count of elements in @p args.
4838 * @param[in,out] set Context and result set at the same time.
4839 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004840 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004841 */
4842static LY_ERR
4843xpath_substring_before(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4844{
4845 char *ptr;
4846 struct lysc_node_leaf *sleaf;
4847 LY_ERR rc = LY_SUCCESS;
4848
4849 if (options & LYXP_SCNODE_ALL) {
4850 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4851 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4852 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 +02004853 } else if (!warn_is_string_type(sleaf->type)) {
4854 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004855 }
4856 }
4857
4858 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4859 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4860 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 +02004861 } else if (!warn_is_string_type(sleaf->type)) {
4862 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004863 }
4864 }
4865 set_scnode_clear_ctx(set);
4866 return rc;
4867 }
4868
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004869 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004870 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004871 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004872 LY_CHECK_RET(rc);
4873
4874 ptr = strstr(args[0]->val.str, args[1]->val.str);
4875 if (ptr) {
4876 set_fill_string(set, args[0]->val.str, ptr - args[0]->val.str);
4877 } else {
4878 set_fill_string(set, "", 0);
4879 }
4880
4881 return LY_SUCCESS;
4882}
4883
4884/**
4885 * @brief Execute the XPath sum(node-set) function. Returns LYXP_SET_NUMBER
4886 * with the sum of all the nodes in the context.
4887 *
4888 * @param[in] args Array of arguments.
4889 * @param[in] arg_count Count of elements in @p args.
4890 * @param[in,out] set Context and result set at the same time.
4891 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004892 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004893 */
4894static LY_ERR
4895xpath_sum(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4896{
4897 long double num;
4898 char *str;
4899 uint16_t i;
4900 struct lyxp_set set_item;
4901 struct lysc_node_leaf *sleaf;
4902 LY_ERR rc = LY_SUCCESS;
4903
4904 if (options & LYXP_SCNODE_ALL) {
4905 if (args[0]->type == LYXP_SET_SCNODE_SET) {
4906 for (i = 0; i < args[0]->used; ++i) {
4907 if (args[0]->val.scnodes[i].in_ctx == 1) {
4908 sleaf = (struct lysc_node_leaf *)args[0]->val.scnodes[i].scnode;
4909 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4910 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__,
4911 lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004912 } else if (!warn_is_numeric_type(sleaf->type)) {
4913 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004914 }
4915 }
4916 }
4917 }
4918 set_scnode_clear_ctx(set);
4919 return rc;
4920 }
4921
4922 set_fill_number(set, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004923
4924 if (args[0]->type != LYXP_SET_NODE_SET) {
4925 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "sum(node-set)");
4926 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004927 } else if (!args[0]->used) {
4928 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004929 }
4930
Michal Vasko5c4e5892019-11-14 12:31:38 +01004931 set_init(&set_item, set);
4932
Michal Vasko03ff5a72019-09-11 13:49:33 +02004933 set_item.type = LYXP_SET_NODE_SET;
4934 set_item.val.nodes = malloc(sizeof *set_item.val.nodes);
4935 LY_CHECK_ERR_RET(!set_item.val.nodes, LOGMEM(set->ctx), LY_EMEM);
4936
4937 set_item.used = 1;
4938 set_item.size = 1;
4939
4940 for (i = 0; i < args[0]->used; ++i) {
4941 set_item.val.nodes[0] = args[0]->val.nodes[i];
4942
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004943 rc = cast_node_set_to_string(&set_item, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004944 LY_CHECK_RET(rc);
4945 num = cast_string_to_number(str);
4946 free(str);
4947 set->val.num += num;
4948 }
4949
4950 free(set_item.val.nodes);
4951
4952 return LY_SUCCESS;
4953}
4954
4955/**
4956 * @brief Execute the XPath text() function (node type). Returns LYXP_SET_NODE_SET
4957 * with the text content of the nodes in the context.
4958 *
4959 * @param[in] args Array of arguments.
4960 * @param[in] arg_count Count of elements in @p args.
4961 * @param[in,out] set Context and result set at the same time.
4962 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004963 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004964 */
4965static LY_ERR
4966xpath_text(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4967{
4968 uint32_t i;
4969
4970 if (options & LYXP_SCNODE_ALL) {
4971 set_scnode_clear_ctx(set);
4972 return LY_SUCCESS;
4973 }
4974
Michal Vasko03ff5a72019-09-11 13:49:33 +02004975 if (set->type != LYXP_SET_NODE_SET) {
4976 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "text()");
4977 return LY_EVALID;
4978 }
4979
4980 for (i = 0; i < set->used;) {
4981 switch (set->val.nodes[i].type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004982 case LYXP_NODE_NONE:
4983 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004984 case LYXP_NODE_ELEM:
Michal Vasko03ff5a72019-09-11 13:49:33 +02004985 if (set->val.nodes[i].node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
4986 set->val.nodes[i].type = LYXP_NODE_TEXT;
4987 ++i;
4988 break;
4989 }
4990 /* fall through */
4991 case LYXP_NODE_ROOT:
4992 case LYXP_NODE_ROOT_CONFIG:
4993 case LYXP_NODE_TEXT:
Michal Vasko9f96a052020-03-10 09:41:45 +01004994 case LYXP_NODE_META:
Michal Vasko03ff5a72019-09-11 13:49:33 +02004995 set_remove_node(set, i);
4996 break;
4997 }
4998 }
4999
5000 return LY_SUCCESS;
5001}
5002
5003/**
5004 * @brief Execute the XPath translate(string, string, string) function.
5005 * Returns LYXP_SET_STRING with the first argument with the characters
5006 * from the second argument replaced by those on the corresponding
5007 * positions in the third argument.
5008 *
5009 * @param[in] args Array of arguments.
5010 * @param[in] arg_count Count of elements in @p args.
5011 * @param[in,out] set Context and result set at the same time.
5012 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005013 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005014 */
5015static LY_ERR
5016xpath_translate(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
5017{
5018 uint16_t i, j, new_used;
5019 char *new;
5020 int found, have_removed;
5021 struct lysc_node_leaf *sleaf;
5022 LY_ERR rc = LY_SUCCESS;
5023
5024 if (options & LYXP_SCNODE_ALL) {
5025 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5026 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5027 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 +02005028 } else if (!warn_is_string_type(sleaf->type)) {
5029 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005030 }
5031 }
5032
5033 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5034 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5035 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 +02005036 } else if (!warn_is_string_type(sleaf->type)) {
5037 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005038 }
5039 }
5040
5041 if ((args[2]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
5042 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5043 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 +02005044 } else if (!warn_is_string_type(sleaf->type)) {
5045 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005046 }
5047 }
5048 set_scnode_clear_ctx(set);
5049 return rc;
5050 }
5051
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005052 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005053 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005054 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005055 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005056 rc = lyxp_set_cast(args[2], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005057 LY_CHECK_RET(rc);
5058
5059 new = malloc((strlen(args[0]->val.str) + 1) * sizeof(char));
5060 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5061 new_used = 0;
5062
5063 have_removed = 0;
5064 for (i = 0; args[0]->val.str[i]; ++i) {
5065 found = 0;
5066
5067 for (j = 0; args[1]->val.str[j]; ++j) {
5068 if (args[0]->val.str[i] == args[1]->val.str[j]) {
5069 /* removing this char */
5070 if (j >= strlen(args[2]->val.str)) {
5071 have_removed = 1;
5072 found = 1;
5073 break;
5074 }
5075 /* replacing this char */
5076 new[new_used] = args[2]->val.str[j];
5077 ++new_used;
5078 found = 1;
5079 break;
5080 }
5081 }
5082
5083 /* copying this char */
5084 if (!found) {
5085 new[new_used] = args[0]->val.str[i];
5086 ++new_used;
5087 }
5088 }
5089
5090 if (have_removed) {
5091 new = ly_realloc(new, (new_used + 1) * sizeof(char));
5092 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5093 }
5094 new[new_used] = '\0';
5095
Michal Vaskod3678892020-05-21 10:06:58 +02005096 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005097 set->type = LYXP_SET_STRING;
5098 set->val.str = new;
5099
5100 return LY_SUCCESS;
5101}
5102
5103/**
5104 * @brief Execute the XPath true() function. Returns LYXP_SET_BOOLEAN
5105 * with true value.
5106 *
5107 * @param[in] args Array of arguments.
5108 * @param[in] arg_count Count of elements in @p args.
5109 * @param[in,out] set Context and result set at the same time.
5110 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005111 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005112 */
5113static LY_ERR
5114xpath_true(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
5115{
5116 if (options & LYXP_SCNODE_ALL) {
5117 set_scnode_clear_ctx(set);
5118 return LY_SUCCESS;
5119 }
5120
5121 set_fill_boolean(set, 1);
5122 return LY_SUCCESS;
5123}
5124
5125/*
5126 * moveto functions
5127 *
5128 * They and only they actually change the context (set).
5129 */
5130
5131/**
Michal Vasko6346ece2019-09-24 13:12:53 +02005132 * @brief Skip prefix and return corresponding model if there is a prefix. Logs directly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005133 *
Michal Vasko2104e9f2020-03-06 08:23:25 +01005134 * XPath @p set is expected to be a (sc)node set!
5135 *
Michal Vasko6346ece2019-09-24 13:12:53 +02005136 * @param[in,out] qname Qualified node name. If includes prefix, it is skipped.
5137 * @param[in,out] qname_len Length of @p qname, is updated accordingly.
5138 * @param[in] set Set with XPath context.
5139 * @param[out] moveto_mod Expected module of a matching node.
5140 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005141 */
Michal Vasko6346ece2019-09-24 13:12:53 +02005142static LY_ERR
5143moveto_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 +02005144{
Michal Vasko6346ece2019-09-24 13:12:53 +02005145 const struct lys_module *mod;
5146 const char *ptr;
5147 int pref_len;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005148 char *str;
5149
Michal Vasko2104e9f2020-03-06 08:23:25 +01005150 assert((set->type == LYXP_SET_NODE_SET) || (set->type == LYXP_SET_SCNODE_SET));
5151
Michal Vasko6346ece2019-09-24 13:12:53 +02005152 if ((ptr = ly_strnchr(*qname, ':', *qname_len))) {
5153 /* specific module */
5154 pref_len = ptr - *qname;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005155
Michal Vasko6346ece2019-09-24 13:12:53 +02005156 switch (set->format) {
Michal Vasko52927e22020-03-16 17:26:14 +01005157 case LYD_SCHEMA:
Michal Vasko6346ece2019-09-24 13:12:53 +02005158 /* schema, search all local module imports */
5159 mod = lys_module_find_prefix(set->local_mod, *qname, pref_len);
5160 break;
5161 case LYD_JSON:
5162 /* JSON data, search in context */
5163 str = strndup(*qname, pref_len);
Michal Vasko97e76fd2020-05-27 15:22:01 +02005164 mod = ly_ctx_get_module_implemented(set->ctx, str);
Michal Vasko6346ece2019-09-24 13:12:53 +02005165 free(str);
5166 break;
Michal Vasko52927e22020-03-16 17:26:14 +01005167 case LYD_XML:
Michal Vasko6346ece2019-09-24 13:12:53 +02005168 LOGINT_RET(set->ctx);
5169 }
5170
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005171 /* Check for errors and non-implemented modules, as they are not valid */
5172 if (!mod || !mod->implemented) {
Michal Vasko2104e9f2020-03-06 08:23:25 +01005173 if (set->type == LYXP_SET_SCNODE_SET) {
5174 LOGVAL(set->ctx, LY_VLOG_LYSC, set->ctx_scnode, LY_VCODE_XP_INMOD, pref_len, *qname);
5175 } else {
5176 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INMOD, pref_len, *qname);
5177 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005178 return LY_EVALID;
5179 }
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005180
Michal Vasko6346ece2019-09-24 13:12:53 +02005181 *qname += pref_len + 1;
5182 *qname_len -= pref_len + 1;
5183 } else if (((*qname)[0] == '*') && (*qname_len == 1)) {
5184 /* all modules - special case */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005185 mod = NULL;
Michal Vasko6346ece2019-09-24 13:12:53 +02005186 } else {
5187 /* local module */
5188 mod = set->local_mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005189 }
5190
Michal Vasko6346ece2019-09-24 13:12:53 +02005191 *moveto_mod = mod;
5192 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005193}
5194
5195/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02005196 * @brief Move context @p set to the root. Handles absolute path.
5197 * Result is LYXP_SET_NODE_SET.
5198 *
5199 * @param[in,out] set Set to use.
5200 * @param[in] options Xpath options.
5201 */
5202static void
5203moveto_root(struct lyxp_set *set, int options)
5204{
Michal Vasko03ff5a72019-09-11 13:49:33 +02005205 if (!set) {
5206 return;
5207 }
5208
5209 if (options & LYXP_SCNODE_ALL) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005210 set_scnode_clear_ctx(set);
Michal Vaskoecd62de2019-11-13 12:35:11 +01005211 lyxp_set_scnode_insert_node(set, NULL, set->root_type);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005212 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005213 set->type = LYXP_SET_NODE_SET;
5214 set->used = 0;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005215 set_insert_node(set, NULL, 0, set->root_type, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005216 }
5217}
5218
5219/**
Michal Vaskoa1424542019-11-14 16:08:52 +01005220 * @brief Check whether a node has some unresolved "when".
5221 *
5222 * @param[in] node Node to check.
5223 * @return LY_ERR value (LY_EINCOMPLETE if there are some unresolved "when")
5224 */
5225static LY_ERR
5226moveto_when_check(const struct lyd_node *node)
5227{
5228 const struct lysc_node *schema;
5229
5230 if (!node) {
5231 return LY_SUCCESS;
5232 }
5233
5234 schema = node->schema;
5235 do {
5236 if (schema->when && !(node->flags & LYD_WHEN_TRUE)) {
5237 return LY_EINCOMPLETE;
5238 }
5239 schema = schema->parent;
5240 } while (schema && (schema->nodetype & (LYS_CASE | LYS_CHOICE)));
5241
5242 return LY_SUCCESS;
5243}
5244
5245/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02005246 * @brief Check @p node as a part of NameTest processing.
5247 *
5248 * @param[in] node Node to check.
5249 * @param[in] root_type XPath root node type.
Michal Vaskod3678892020-05-21 10:06:58 +02005250 * @param[in] node_name Node name in the dictionary to move to, NULL for any node.
5251 * @param[in] moveto_mod Expected module of the node, NULL for any.
Michal Vasko6346ece2019-09-24 13:12:53 +02005252 * @return LY_ERR (LY_ENOT if node does not match, LY_EINCOMPLETE on unresolved when,
5253 * LY_EINVAL if netither node nor any children match)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005254 */
5255static LY_ERR
5256moveto_node_check(const struct lyd_node *node, enum lyxp_node_type root_type, const char *node_name,
5257 const struct lys_module *moveto_mod)
5258{
5259 /* module check */
5260 if (moveto_mod && (node->schema->module != moveto_mod)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005261 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005262 }
5263
Michal Vasko5c4e5892019-11-14 12:31:38 +01005264 /* context check */
5265 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005266 return LY_EINVAL;
5267 }
5268
5269 /* name check */
Michal Vasko61ac2f62020-05-25 12:39:51 +02005270 if (node_name && strcmp(node_name, "*") && (node->schema->name != node_name)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005271 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005272 }
5273
Michal Vaskoa1424542019-11-14 16:08:52 +01005274 /* when check */
5275 if (moveto_when_check(node)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005276 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01005277 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005278
5279 /* match */
5280 return LY_SUCCESS;
5281}
5282
5283/**
5284 * @brief Check @p node as a part of schema NameTest processing.
5285 *
5286 * @param[in] node Schema node to check.
5287 * @param[in] root_type XPath root node type.
Michal Vaskod3678892020-05-21 10:06:58 +02005288 * @param[in] node_name Node name in the dictionary to move to, NULL for any nodes.
5289 * @param[in] moveto_mod Expected module of the node, NULL for any.
Michal Vasko6346ece2019-09-24 13:12:53 +02005290 * @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 +02005291 */
5292static LY_ERR
5293moveto_scnode_check(const struct lysc_node *node, enum lyxp_node_type root_type, const char *node_name,
Michal Vaskocafad9d2019-11-07 15:20:03 +01005294 const struct lys_module *moveto_mod)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005295{
Michal Vasko03ff5a72019-09-11 13:49:33 +02005296 /* module check */
Michal Vaskod3678892020-05-21 10:06:58 +02005297 if (moveto_mod && (node->module != moveto_mod)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005298 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005299 }
5300
5301 /* context check */
5302 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (node->flags & LYS_CONFIG_R)) {
5303 return LY_EINVAL;
5304 }
5305
5306 /* name check */
Michal Vasko61ac2f62020-05-25 12:39:51 +02005307 if (node_name && strcmp(node_name, "*") && (node->name != node_name)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005308 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005309 }
5310
5311 /* match */
5312 return LY_SUCCESS;
5313}
5314
5315/**
Michal Vaskod3678892020-05-21 10:06:58 +02005316 * @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 +02005317 *
5318 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005319 * @param[in] mod Matching node module, NULL for any.
5320 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005321 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5322 */
5323static LY_ERR
Michal Vaskod3678892020-05-21 10:06:58 +02005324moveto_node(struct lyxp_set *set, const struct lys_module *mod, const char *ncname)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005325{
Michal Vaskof03ed032020-03-04 13:31:44 +01005326 uint32_t i;
Michal Vasko6346ece2019-09-24 13:12:53 +02005327 int replaced;
Michal Vaskod3678892020-05-21 10:06:58 +02005328 const struct lyd_node *siblings, *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005329 LY_ERR rc;
5330
Michal Vaskod3678892020-05-21 10:06:58 +02005331 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005332 return LY_SUCCESS;
5333 }
5334
5335 if (set->type != LYXP_SET_NODE_SET) {
5336 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
5337 return LY_EVALID;
5338 }
5339
Michal Vasko03ff5a72019-09-11 13:49:33 +02005340 for (i = 0; i < set->used; ) {
5341 replaced = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02005342 siblings = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005343
5344 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 +01005345 assert(!set->val.nodes[i].node);
Michal Vaskod3678892020-05-21 10:06:58 +02005346
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005347 /* search in all the trees */
Michal Vaskod3678892020-05-21 10:06:58 +02005348 siblings = set->tree;
Michal Vasko5c4e5892019-11-14 12:31:38 +01005349 } else if (!(set->val.nodes[i].node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA))) {
Michal Vaskod3678892020-05-21 10:06:58 +02005350 /* search in children */
5351 siblings = lyd_node_children(set->val.nodes[i].node);
5352 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005353
Michal Vaskod3678892020-05-21 10:06:58 +02005354 for (sub = siblings; sub; sub = sub->next) {
5355 rc = moveto_node_check(sub, set->root_type, ncname, mod);
5356 if (rc == LY_SUCCESS) {
5357 if (!replaced) {
5358 set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i);
5359 replaced = 1;
5360 } else {
5361 set_insert_node(set, sub, 0, LYXP_NODE_ELEM, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005362 }
Michal Vaskod3678892020-05-21 10:06:58 +02005363 ++i;
5364 } else if (rc == LY_EINCOMPLETE) {
5365 return rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005366 }
5367 }
5368
5369 if (!replaced) {
5370 /* no match */
5371 set_remove_node(set, i);
5372 }
5373 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005374
5375 return LY_SUCCESS;
5376}
5377
5378/**
Michal Vaskod3678892020-05-21 10:06:58 +02005379 * @brief Move context @p set to a node using hashes. Result is LYXP_SET_NODE_SET (or LYXP_SET_EMPTY).
5380 * Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005381 *
5382 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005383 * @param[in] scnode Matching node schema.
5384 * @param[in] list_inst If @p scnode is ::LYS_LIST, the matching list instance. Ignored otherwise.
5385 * @param[in] llist_val If @p scnode is ::LYS_LEAFLIST, the matching leaf-list value. Ignored otherwise.
5386 * @param[in] llist_val_len Length of @p llist_val.
5387 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5388 */
5389static LY_ERR
5390moveto_node_hash(struct lyxp_set *set, const struct lysc_node *scnode, const struct lyd_node *list_inst,
5391 const char *llist_val, uint16_t llist_val_len)
5392{
5393 uint32_t i;
5394 int replaced;
5395 const struct lyd_node *siblings;
5396 struct lyd_node *sub;
5397
5398 assert(scnode && ((scnode->nodetype != LYS_LIST) || list_inst) && ((scnode->nodetype != LYS_LEAFLIST) || llist_val));
5399
5400 if (!set) {
5401 return LY_SUCCESS;
5402 }
5403
5404 if (set->type != LYXP_SET_NODE_SET) {
5405 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
5406 return LY_EVALID;
5407 }
5408
5409 /* context check for all the nodes since we have the schema node */
5410 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) {
5411 lyxp_set_free_content(set);
5412 return LY_SUCCESS;
5413 }
5414
5415 for (i = 0; i < set->used; ) {
5416 replaced = 0;
5417 siblings = NULL;
5418
5419 if ((set->val.nodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.nodes[i].type == LYXP_NODE_ROOT)) {
5420 assert(!set->val.nodes[i].node);
5421
5422 /* search in all the trees */
5423 siblings = set->tree;
5424 } else if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
5425 /* search in children */
5426 siblings = lyd_node_children(set->val.nodes[i].node);
5427 }
5428
5429 /* find the node using hashes */
5430 if (scnode->nodetype == LYS_LIST) {
5431 lyd_find_sibling_first(siblings, list_inst, &sub);
5432 } else {
5433 lyd_find_sibling_val(siblings, scnode, llist_val, llist_val_len, &sub);
5434 }
5435
5436 /* when check */
5437 if (sub && moveto_when_check(sub)) {
5438 return LY_EINCOMPLETE;
5439 }
5440
5441 if (sub) {
5442 /* pos filled later */
5443 if (!replaced) {
5444 set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i);
5445 replaced = 1;
5446 } else {
5447 set_insert_node(set, sub, 0, LYXP_NODE_ELEM, i);
5448 }
5449 ++i;
5450 }
5451
5452 if (!replaced) {
5453 /* no match */
5454 set_remove_node(set, i);
5455 }
5456 }
5457
5458 return LY_SUCCESS;
5459}
5460
5461/**
5462 * @brief Move context @p set to a schema node. Result is LYXP_SET_SCNODE_SET (or LYXP_SET_EMPTY).
5463 *
5464 * @param[in,out] set Set to use.
5465 * @param[in] mod Matching node module, NULL for any.
5466 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005467 * @param[in] options XPath options.
5468 * @return LY_ERR
5469 */
5470static LY_ERR
Michal Vaskod3678892020-05-21 10:06:58 +02005471moveto_scnode(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, int options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005472{
Michal Vaskocafad9d2019-11-07 15:20:03 +01005473 int i, orig_used, idx, temp_ctx = 0, getnext_opts;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005474 uint32_t mod_idx;
Michal Vasko519fd602020-05-26 12:17:39 +02005475 const struct lysc_node *iter, *start_parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005476
Michal Vaskod3678892020-05-21 10:06:58 +02005477 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005478 return LY_SUCCESS;
5479 }
5480
5481 if (set->type != LYXP_SET_SCNODE_SET) {
Michal Vaskof6e51882019-12-16 09:59:45 +01005482 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 +02005483 return LY_EVALID;
5484 }
5485
Michal Vaskocafad9d2019-11-07 15:20:03 +01005486 /* getnext opts */
5487 getnext_opts = LYS_GETNEXT_NOSTATECHECK;
5488 if (options & LYXP_SCNODE_OUTPUT) {
5489 getnext_opts |= LYS_GETNEXT_OUTPUT;
5490 }
5491
Michal Vasko03ff5a72019-09-11 13:49:33 +02005492 orig_used = set->used;
5493 for (i = 0; i < orig_used; ++i) {
5494 if (set->val.scnodes[i].in_ctx != 1) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01005495 if (set->val.scnodes[i].in_ctx != -2) {
5496 continue;
5497 }
5498
5499 /* remember context node */
5500 set->val.scnodes[i].in_ctx = -1;
Michal Vaskoec4df482019-12-16 10:02:18 +01005501 } else {
5502 set->val.scnodes[i].in_ctx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005503 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005504
5505 start_parent = set->val.scnodes[i].scnode;
5506
5507 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 +02005508 /* 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 +02005509 * so use it directly (root node itself is useless in this case) */
5510 mod_idx = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02005511 while (mod || (mod = (struct lys_module *)ly_ctx_get_module_iter(set->ctx, &mod_idx))) {
Michal Vasko519fd602020-05-26 12:17:39 +02005512 iter = NULL;
Michal Vasko509de4d2019-12-10 14:51:30 +01005513 /* module may not be implemented */
Michal Vasko519fd602020-05-26 12:17:39 +02005514 while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) {
5515 if (!moveto_scnode_check(iter, set->root_type, ncname, mod)) {
5516 idx = lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005517 /* we need to prevent these nodes from being considered in this moveto */
5518 if ((idx < orig_used) && (idx > i)) {
5519 set->val.scnodes[idx].in_ctx = 2;
5520 temp_ctx = 1;
5521 }
5522 }
5523 }
5524
5525 if (!mod_idx) {
Michal Vaskod3678892020-05-21 10:06:58 +02005526 /* mod was specified, we are not going through the whole context */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005527 break;
5528 }
5529 /* next iteration */
Michal Vaskod3678892020-05-21 10:06:58 +02005530 mod = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005531 }
5532
Michal Vasko519fd602020-05-26 12:17:39 +02005533 } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
5534 iter = NULL;
5535 while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) {
5536 if (!moveto_scnode_check(iter, set->root_type, ncname, (mod ? mod : set->local_mod))) {
5537 idx = lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005538 if ((idx < orig_used) && (idx > i)) {
5539 set->val.scnodes[idx].in_ctx = 2;
5540 temp_ctx = 1;
5541 }
5542 }
5543 }
5544 }
5545 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005546
5547 /* correct temporary in_ctx values */
5548 if (temp_ctx) {
5549 for (i = 0; i < orig_used; ++i) {
5550 if (set->val.scnodes[i].in_ctx == 2) {
5551 set->val.scnodes[i].in_ctx = 1;
5552 }
5553 }
5554 }
5555
5556 return LY_SUCCESS;
5557}
5558
5559/**
Michal Vaskod3678892020-05-21 10:06:58 +02005560 * @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 +02005561 * Context position aware.
5562 *
5563 * @param[in] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005564 * @param[in] mod Matching node module, NULL for any.
5565 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005566 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5567 */
5568static LY_ERR
Michal Vaskod3678892020-05-21 10:06:58 +02005569moveto_node_alldesc(struct lyxp_set *set, const struct lys_module *mod, const char *ncname)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005570{
5571 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005572 const struct lyd_node *next, *elem, *start;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005573 struct lyxp_set ret_set;
5574 LY_ERR rc;
5575
Michal Vaskod3678892020-05-21 10:06:58 +02005576 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005577 return LY_SUCCESS;
5578 }
5579
5580 if (set->type != LYXP_SET_NODE_SET) {
5581 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
5582 return LY_EVALID;
5583 }
5584
Michal Vasko9f96a052020-03-10 09:41:45 +01005585 /* 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 +02005586 rc = moveto_node(set, NULL, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005587 LY_CHECK_RET(rc);
5588
Michal Vasko6346ece2019-09-24 13:12:53 +02005589 /* this loop traverses all the nodes in the set and adds/keeps only those that match qname */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005590 set_init(&ret_set, set);
5591 for (i = 0; i < set->used; ++i) {
5592
5593 /* TREE DFS */
5594 start = set->val.nodes[i].node;
5595 for (elem = next = start; elem; elem = next) {
Michal Vaskod3678892020-05-21 10:06:58 +02005596 rc = moveto_node_check(elem, set->root_type, ncname, mod);
Michal Vasko6346ece2019-09-24 13:12:53 +02005597 if (!rc) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005598 /* add matching node into result set */
5599 set_insert_node(&ret_set, elem, 0, LYXP_NODE_ELEM, ret_set.used);
5600 if (set_dup_node_check(set, elem, LYXP_NODE_ELEM, i)) {
5601 /* the node is a duplicate, we'll process it later in the set */
5602 goto skip_children;
5603 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005604 } else if (rc == LY_EINCOMPLETE) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005605 return rc;
5606 } else if (rc == LY_EINVAL) {
5607 goto skip_children;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005608 }
5609
5610 /* TREE DFS NEXT ELEM */
5611 /* select element for the next run - children first */
Michal Vaskod3678892020-05-21 10:06:58 +02005612 next = lyd_node_children(elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005613 if (!next) {
5614skip_children:
5615 /* no children, so try siblings, but only if it's not the start,
5616 * that is considered to be the root and it's siblings are not traversed */
5617 if (elem != start) {
5618 next = elem->next;
5619 } else {
5620 break;
5621 }
5622 }
5623 while (!next) {
5624 /* no siblings, go back through the parents */
5625 if ((struct lyd_node *)elem->parent == start) {
5626 /* we are done, no next element to process */
5627 break;
5628 }
5629 /* parent is already processed, go to its sibling */
5630 elem = (struct lyd_node *)elem->parent;
5631 next = elem->next;
5632 }
5633 }
5634 }
5635
5636 /* make the temporary set the current one */
5637 ret_set.ctx_pos = set->ctx_pos;
5638 ret_set.ctx_size = set->ctx_size;
Michal Vaskod3678892020-05-21 10:06:58 +02005639 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005640 memcpy(set, &ret_set, sizeof *set);
5641
5642 return LY_SUCCESS;
5643}
5644
5645/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005646 * @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 +02005647 *
5648 * @param[in] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005649 * @param[in] mod Matching node module, NULL for any.
5650 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005651 * @param[in] options XPath options.
5652 * @return LY_ERR
5653 */
5654static LY_ERR
Michal Vaskod3678892020-05-21 10:06:58 +02005655moveto_scnode_alldesc(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, int options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005656{
Michal Vasko6346ece2019-09-24 13:12:53 +02005657 int i, orig_used, idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005658 const struct lysc_node *next, *elem, *start;
Michal Vasko6346ece2019-09-24 13:12:53 +02005659 LY_ERR rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005660
Michal Vaskod3678892020-05-21 10:06:58 +02005661 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005662 return LY_SUCCESS;
5663 }
5664
5665 if (set->type != LYXP_SET_SCNODE_SET) {
Michal Vaskof6e51882019-12-16 09:59:45 +01005666 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 +02005667 return LY_EVALID;
5668 }
5669
Michal Vasko03ff5a72019-09-11 13:49:33 +02005670 orig_used = set->used;
5671 for (i = 0; i < orig_used; ++i) {
5672 if (set->val.scnodes[i].in_ctx != 1) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01005673 if (set->val.scnodes[i].in_ctx != -2) {
5674 continue;
5675 }
5676
5677 /* remember context node */
5678 set->val.scnodes[i].in_ctx = -1;
Michal Vaskoec4df482019-12-16 10:02:18 +01005679 } else {
5680 set->val.scnodes[i].in_ctx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005681 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005682
5683 /* TREE DFS */
5684 start = set->val.scnodes[i].scnode;
5685 for (elem = next = start; elem; elem = next) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005686 if ((elem == start) || (elem->nodetype & (LYS_CHOICE | LYS_CASE))) {
5687 /* schema-only nodes, skip root */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005688 goto next_iter;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005689 }
5690
Michal Vaskod3678892020-05-21 10:06:58 +02005691 rc = moveto_scnode_check(elem, set->root_type, ncname, mod);
Michal Vasko6346ece2019-09-24 13:12:53 +02005692 if (!rc) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01005693 if ((idx = lyxp_set_scnode_dup_node_check(set, elem, LYXP_NODE_ELEM, i)) > -1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005694 set->val.scnodes[idx].in_ctx = 1;
5695 if (idx > i) {
5696 /* we will process it later in the set */
5697 goto skip_children;
5698 }
5699 } else {
Michal Vaskoecd62de2019-11-13 12:35:11 +01005700 lyxp_set_scnode_insert_node(set, elem, LYXP_NODE_ELEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005701 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005702 } else if (rc == LY_EINVAL) {
5703 goto skip_children;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005704 }
5705
5706next_iter:
5707 /* TREE DFS NEXT ELEM */
5708 /* select element for the next run - children first */
5709 next = lysc_node_children(elem, options & LYXP_SCNODE_OUTPUT ? LYS_CONFIG_R : LYS_CONFIG_W);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005710 if (!next) {
5711skip_children:
5712 /* no children, so try siblings, but only if it's not the start,
5713 * that is considered to be the root and it's siblings are not traversed */
5714 if (elem != start) {
5715 next = elem->next;
5716 } else {
5717 break;
5718 }
5719 }
5720 while (!next) {
5721 /* no siblings, go back through the parents */
5722 if (elem->parent == start) {
5723 /* we are done, no next element to process */
5724 break;
5725 }
5726 /* parent is already processed, go to its sibling */
5727 elem = elem->parent;
5728 next = elem->next;
5729 }
5730 }
5731 }
5732
5733 return LY_SUCCESS;
5734}
5735
5736/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005737 * @brief Move context @p set to an attribute. Result is LYXP_SET_NODE_SET.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005738 * Indirectly context position aware.
5739 *
5740 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005741 * @param[in] mod Matching metadata module, NULL for any.
5742 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005743 * @return LY_ERR
5744 */
5745static LY_ERR
Michal Vaskod3678892020-05-21 10:06:58 +02005746moveto_attr(struct lyxp_set *set, const struct lys_module *mod, const char *ncname)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005747{
5748 uint32_t i;
Michal Vaskod3678892020-05-21 10:06:58 +02005749 int replaced;
Michal Vasko9f96a052020-03-10 09:41:45 +01005750 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005751
Michal Vaskod3678892020-05-21 10:06:58 +02005752 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005753 return LY_SUCCESS;
5754 }
5755
5756 if (set->type != LYXP_SET_NODE_SET) {
5757 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
5758 return LY_EVALID;
5759 }
5760
Michal Vasko03ff5a72019-09-11 13:49:33 +02005761 for (i = 0; i < set->used; ) {
5762 replaced = 0;
5763
5764 /* only attributes of an elem (not dummy) can be in the result, skip all the rest;
5765 * our attributes are always qualified */
Michal Vasko5c4e5892019-11-14 12:31:38 +01005766 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01005767 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005768
5769 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02005770 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005771 continue;
5772 }
5773
Michal Vaskod3678892020-05-21 10:06:58 +02005774 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005775 /* match */
5776 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01005777 set->val.meta[i].meta = sub;
5778 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005779 /* pos does not change */
5780 replaced = 1;
5781 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01005782 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 +02005783 }
5784 ++i;
5785 }
5786 }
5787 }
5788
5789 if (!replaced) {
5790 /* no match */
5791 set_remove_node(set, i);
5792 }
5793 }
5794
5795 return LY_SUCCESS;
5796}
5797
5798/**
5799 * @brief Move context @p set1 to union with @p set2. @p set2 is emptied afterwards.
Michal Vasko61ac2f62020-05-25 12:39:51 +02005800 * Result is LYXP_SET_NODE_SET. Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005801 *
5802 * @param[in,out] set1 Set to use for the result.
5803 * @param[in] set2 Set that is copied to @p set1.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005804 * @return LY_ERR
5805 */
5806static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005807moveto_union(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005808{
5809 LY_ERR rc;
5810
Michal Vaskod3678892020-05-21 10:06:58 +02005811 if ((set1->type != LYXP_SET_NODE_SET) || (set2->type != LYXP_SET_NODE_SET)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005812 LOGVAL(set1->ctx, LY_VLOG_LYD, set1->ctx_node, LY_VCODE_XP_INOP_2, "union", print_set_type(set1), print_set_type(set2));
5813 return LY_EVALID;
5814 }
5815
5816 /* set2 is empty or both set1 and set2 */
Michal Vaskod3678892020-05-21 10:06:58 +02005817 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005818 return LY_SUCCESS;
5819 }
5820
Michal Vaskod3678892020-05-21 10:06:58 +02005821 if (!set1->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005822 memcpy(set1, set2, sizeof *set1);
5823 /* dynamic memory belongs to set1 now, do not free */
Michal Vaskod3678892020-05-21 10:06:58 +02005824 memset(set2, 0, sizeof *set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005825 return LY_SUCCESS;
5826 }
5827
5828 /* we assume sets are sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005829 assert(!set_sort(set1) && !set_sort(set2));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005830
5831 /* sort, remove duplicates */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005832 rc = set_sorted_merge(set1, set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005833 LY_CHECK_RET(rc);
5834
5835 /* final set must be sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005836 assert(!set_sort(set1));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005837
5838 return LY_SUCCESS;
5839}
5840
5841/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005842 * @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 +02005843 * Context position aware.
5844 *
5845 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005846 * @param[in] mod Matching metadata module, NULL for any.
5847 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005848 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5849 */
5850static int
Michal Vaskod3678892020-05-21 10:06:58 +02005851moveto_attr_alldesc(struct lyxp_set *set, const struct lys_module *mod, const char *ncname)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005852{
5853 uint32_t i;
Michal Vaskod3678892020-05-21 10:06:58 +02005854 int replaced;
Michal Vasko9f96a052020-03-10 09:41:45 +01005855 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005856 struct lyxp_set *set_all_desc = NULL;
5857 LY_ERR rc;
5858
Michal Vaskod3678892020-05-21 10:06:58 +02005859 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005860 return LY_SUCCESS;
5861 }
5862
5863 if (set->type != LYXP_SET_NODE_SET) {
5864 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
5865 return LY_EVALID;
5866 }
5867
Michal Vasko03ff5a72019-09-11 13:49:33 +02005868 /* can be optimized similarly to moveto_node_alldesc() and save considerable amount of memory,
5869 * but it likely won't be used much, so it's a waste of time */
5870 /* copy the context */
5871 set_all_desc = set_copy(set);
5872 /* get all descendant nodes (the original context nodes are removed) */
Michal Vaskod3678892020-05-21 10:06:58 +02005873 rc = moveto_node_alldesc(set_all_desc, NULL, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005874 if (rc != LY_SUCCESS) {
5875 lyxp_set_free(set_all_desc);
5876 return rc;
5877 }
5878 /* prepend the original context nodes */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005879 rc = moveto_union(set, set_all_desc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005880 if (rc != LY_SUCCESS) {
5881 lyxp_set_free(set_all_desc);
5882 return rc;
5883 }
5884 lyxp_set_free(set_all_desc);
5885
Michal Vasko03ff5a72019-09-11 13:49:33 +02005886 for (i = 0; i < set->used; ) {
5887 replaced = 0;
5888
5889 /* only attributes of an elem can be in the result, skip all the rest,
5890 * we have all attributes qualified in lyd tree */
5891 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01005892 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005893 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02005894 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005895 continue;
5896 }
5897
Michal Vaskod3678892020-05-21 10:06:58 +02005898 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005899 /* match */
5900 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01005901 set->val.meta[i].meta = sub;
5902 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005903 /* pos does not change */
5904 replaced = 1;
5905 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01005906 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 +02005907 }
5908 ++i;
5909 }
5910 }
5911 }
5912
5913 if (!replaced) {
5914 /* no match */
5915 set_remove_node(set, i);
5916 }
5917 }
5918
5919 return LY_SUCCESS;
5920}
5921
5922/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005923 * @brief Move context @p set to self and al chilren, recursively. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET.
5924 * Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005925 *
5926 * @param[in] parent Current parent.
5927 * @param[in] parent_pos Position of @p parent.
5928 * @param[in] parent_type Node type of @p parent.
5929 * @param[in,out] to_set Set to use.
5930 * @param[in] dup_check_set Set for checking duplicities.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005931 * @param[in] options XPath options.
5932 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5933 */
5934static LY_ERR
5935moveto_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 +01005936 struct lyxp_set *to_set, const struct lyxp_set *dup_check_set, int options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005937{
Michal Vasko61ac2f62020-05-25 12:39:51 +02005938 const struct lyd_node *iter, *first;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005939 LY_ERR rc;
5940
5941 switch (parent_type) {
5942 case LYXP_NODE_ROOT:
5943 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko61ac2f62020-05-25 12:39:51 +02005944 assert(!parent);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005945
Michal Vasko61ac2f62020-05-25 12:39:51 +02005946 /* add all top-level nodes as elements */
5947 first = to_set->tree;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005948 break;
5949 case LYXP_NODE_ELEM:
Michal Vasko61ac2f62020-05-25 12:39:51 +02005950 /* add just the text node of this term element node */
5951 if (parent->schema->nodetype & (LYD_NODE_TERM | LYD_NODE_ANY)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005952 if (!set_dup_node_check(dup_check_set, parent, LYXP_NODE_TEXT, -1)) {
5953 set_insert_node(to_set, parent, parent_pos, LYXP_NODE_TEXT, to_set->used);
5954 }
Michal Vasko61ac2f62020-05-25 12:39:51 +02005955 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005956 }
Michal Vasko61ac2f62020-05-25 12:39:51 +02005957
5958 /* add all the children of this node */
5959 first = lyd_node_children(parent);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005960 break;
5961 default:
5962 LOGINT_RET(parent->schema->module->ctx);
5963 }
5964
Michal Vasko61ac2f62020-05-25 12:39:51 +02005965 /* add all top-level nodes as elements */
5966 LY_LIST_FOR(first, iter) {
5967 /* context check */
5968 if ((parent_type == LYXP_NODE_ROOT_CONFIG) && (iter->schema->flags & LYS_CONFIG_R)) {
5969 continue;
5970 }
5971
5972 /* when check */
5973 if (moveto_when_check(iter)) {
5974 return LY_EINCOMPLETE;
5975 }
5976
5977 if (!set_dup_node_check(dup_check_set, iter, LYXP_NODE_ELEM, -1)) {
5978 set_insert_node(to_set, iter, 0, LYXP_NODE_ELEM, to_set->used);
5979
5980 /* also add all the children of this node, recursively */
5981 rc = moveto_self_add_children_r(iter, 0, LYXP_NODE_ELEM, to_set, dup_check_set, options);
5982 LY_CHECK_RET(rc);
5983 }
5984 }
5985
Michal Vasko03ff5a72019-09-11 13:49:33 +02005986 return LY_SUCCESS;
5987}
5988
5989/**
5990 * @brief Move context @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET
5991 * (or LYXP_SET_EMPTY). Context position aware.
5992 *
5993 * @param[in,out] set Set to use.
5994 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
5995 * @param[in] options XPath options.
5996 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5997 */
5998static LY_ERR
5999moveto_self(struct lyxp_set *set, int all_desc, int options)
6000{
6001 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006002 struct lyxp_set ret_set;
6003 LY_ERR rc;
6004
Michal Vaskod3678892020-05-21 10:06:58 +02006005 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006006 return LY_SUCCESS;
6007 }
6008
6009 if (set->type != LYXP_SET_NODE_SET) {
6010 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
6011 return LY_EVALID;
6012 }
6013
6014 /* nothing to do */
6015 if (!all_desc) {
6016 return LY_SUCCESS;
6017 }
6018
Michal Vasko03ff5a72019-09-11 13:49:33 +02006019 /* add all the children, they get added recursively */
6020 set_init(&ret_set, set);
6021 for (i = 0; i < set->used; ++i) {
6022 /* copy the current node to tmp */
6023 set_insert_node(&ret_set, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, ret_set.used);
6024
6025 /* do not touch attributes and text nodes */
Michal Vasko9f96a052020-03-10 09:41:45 +01006026 if ((set->val.nodes[i].type == LYXP_NODE_TEXT) || (set->val.nodes[i].type == LYXP_NODE_META)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006027 continue;
6028 }
6029
Michal Vasko03ff5a72019-09-11 13:49:33 +02006030 /* add all the children */
6031 rc = moveto_self_add_children_r(set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, &ret_set,
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006032 set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006033 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006034 lyxp_set_free_content(&ret_set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006035 return rc;
6036 }
6037 }
6038
6039 /* use the temporary set as the current one */
6040 ret_set.ctx_pos = set->ctx_pos;
6041 ret_set.ctx_size = set->ctx_size;
Michal Vaskod3678892020-05-21 10:06:58 +02006042 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006043 memcpy(set, &ret_set, sizeof *set);
6044
6045 return LY_SUCCESS;
6046}
6047
6048/**
6049 * @brief Move context schema @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_SCNODE_SET
6050 * (or LYXP_SET_EMPTY).
6051 *
6052 * @param[in,out] set Set to use.
6053 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6054 * @param[in] options XPath options.
6055 * @return LY_ERR
6056 */
6057static LY_ERR
6058moveto_scnode_self(struct lyxp_set *set, int all_desc, int options)
6059{
Michal Vasko519fd602020-05-26 12:17:39 +02006060 int getnext_opts;
6061 uint32_t i, mod_idx;
6062 const struct lysc_node *iter, *start_parent;
6063 const struct lys_module *mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006064
Michal Vaskod3678892020-05-21 10:06:58 +02006065 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006066 return LY_SUCCESS;
6067 }
6068
6069 if (set->type != LYXP_SET_SCNODE_SET) {
Michal Vaskof6e51882019-12-16 09:59:45 +01006070 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 +02006071 return LY_EVALID;
6072 }
6073
6074 /* nothing to do */
6075 if (!all_desc) {
6076 return LY_SUCCESS;
6077 }
6078
Michal Vasko519fd602020-05-26 12:17:39 +02006079 /* getnext opts */
6080 getnext_opts = LYS_GETNEXT_NOSTATECHECK;
6081 if (options & LYXP_SCNODE_OUTPUT) {
6082 getnext_opts |= LYS_GETNEXT_OUTPUT;
6083 }
6084
6085 /* add all the children, recursively as they are being added into the same set */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006086 for (i = 0; i < set->used; ++i) {
6087 if (set->val.scnodes[i].in_ctx != 1) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006088 if (set->val.scnodes[i].in_ctx != -2) {
6089 continue;
6090 }
6091
Michal Vasko519fd602020-05-26 12:17:39 +02006092 /* remember context node */
6093 set->val.scnodes[i].in_ctx = -1;
6094 } else {
6095 set->val.scnodes[i].in_ctx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006096 }
6097
Michal Vasko519fd602020-05-26 12:17:39 +02006098 start_parent = set->val.scnodes[i].scnode;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006099
Michal Vasko519fd602020-05-26 12:17:39 +02006100 if ((set->val.scnodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.scnodes[i].type == LYXP_NODE_ROOT)) {
6101 /* it can actually be in any module, it's all <running> */
6102 mod_idx = 0;
6103 while ((mod = (struct lys_module *)ly_ctx_get_module_iter(set->ctx, &mod_idx))) {
6104 iter = NULL;
6105 /* module may not be implemented */
6106 while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) {
6107 /* context check */
6108 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) {
6109 continue;
6110 }
6111
6112 lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM);
6113 /* throw away the insert index, we want to consider that node again, recursively */
6114 }
6115 }
6116
6117 } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
6118 iter = NULL;
6119 while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006120 /* context check */
Michal Vasko519fd602020-05-26 12:17:39 +02006121 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006122 continue;
6123 }
6124
Michal Vasko519fd602020-05-26 12:17:39 +02006125 lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006126 }
6127 }
6128 }
6129
6130 return LY_SUCCESS;
6131}
6132
6133/**
6134 * @brief Move context @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_NODE_SET
6135 * (or LYXP_SET_EMPTY). Context position aware.
6136 *
6137 * @param[in] set Set to use.
6138 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6139 * @param[in] options XPath options.
6140 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6141 */
6142static LY_ERR
6143moveto_parent(struct lyxp_set *set, int all_desc, int options)
6144{
6145 LY_ERR rc;
6146 uint32_t i;
6147 struct lyd_node *node, *new_node;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006148 enum lyxp_node_type new_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006149
Michal Vaskod3678892020-05-21 10:06:58 +02006150 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006151 return LY_SUCCESS;
6152 }
6153
6154 if (set->type != LYXP_SET_NODE_SET) {
6155 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
6156 return LY_EVALID;
6157 }
6158
6159 if (all_desc) {
6160 /* <path>//.. == <path>//./.. */
6161 rc = moveto_self(set, 1, options);
6162 LY_CHECK_RET(rc);
6163 }
6164
Michal Vasko57eab132019-09-24 11:46:26 +02006165 for (i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006166 node = set->val.nodes[i].node;
6167
6168 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
6169 new_node = (struct lyd_node *)node->parent;
6170 } else if (set->val.nodes[i].type == LYXP_NODE_TEXT) {
6171 new_node = node;
Michal Vasko9f96a052020-03-10 09:41:45 +01006172 } else if (set->val.nodes[i].type == LYXP_NODE_META) {
6173 new_node = set->val.meta[i].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006174 if (!new_node) {
6175 LOGINT_RET(set->ctx);
6176 }
6177 } else {
6178 /* root does not have a parent */
Michal Vasko2caefc12019-11-14 16:07:56 +01006179 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006180 continue;
6181 }
6182
Michal Vaskoa1424542019-11-14 16:08:52 +01006183 /* when check */
6184 if (moveto_when_check(new_node)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006185 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01006186 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006187
6188 /* node already there can also be the root */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006189 if (!new_node) {
6190 new_type = set->root_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006191
6192 /* node has a standard parent (it can equal the root, it's not the root yet since they are fake) */
6193 } else {
6194 new_type = LYXP_NODE_ELEM;
6195 }
6196
Michal Vasko03ff5a72019-09-11 13:49:33 +02006197 if (set_dup_node_check(set, new_node, new_type, -1)) {
Michal Vasko2caefc12019-11-14 16:07:56 +01006198 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006199 } else {
6200 set_replace_node(set, new_node, 0, new_type, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006201 }
6202 }
6203
Michal Vasko2caefc12019-11-14 16:07:56 +01006204 set_remove_nodes_none(set);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006205 assert(!set_sort(set) && !set_sorted_dup_node_clean(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006206
6207 return LY_SUCCESS;
6208}
6209
6210/**
6211 * @brief Move context schema @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_SCNODE_SET
6212 * (or LYXP_SET_EMPTY).
6213 *
6214 * @param[in] set Set to use.
6215 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6216 * @param[in] options XPath options.
6217 * @return LY_ERR
6218 */
6219static LY_ERR
6220moveto_scnode_parent(struct lyxp_set *set, int all_desc, int options)
6221{
6222 int idx, i, orig_used, temp_ctx = 0;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006223 const struct lysc_node *node, *new_node;
6224 enum lyxp_node_type new_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006225 LY_ERR rc;
6226
Michal Vaskod3678892020-05-21 10:06:58 +02006227 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006228 return LY_SUCCESS;
6229 }
6230
6231 if (set->type != LYXP_SET_SCNODE_SET) {
Michal Vaskof6e51882019-12-16 09:59:45 +01006232 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 +02006233 return LY_EVALID;
6234 }
6235
6236 if (all_desc) {
6237 /* <path>//.. == <path>//./.. */
6238 rc = moveto_scnode_self(set, 1, options);
6239 LY_CHECK_RET(rc);
6240 }
6241
Michal Vasko03ff5a72019-09-11 13:49:33 +02006242 orig_used = set->used;
6243 for (i = 0; i < orig_used; ++i) {
6244 if (set->val.scnodes[i].in_ctx != 1) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006245 if (set->val.scnodes[i].in_ctx != -2) {
6246 continue;
6247 }
6248
6249 /* remember context node */
6250 set->val.scnodes[i].in_ctx = -1;
Michal Vaskoec4df482019-12-16 10:02:18 +01006251 } else {
6252 set->val.scnodes[i].in_ctx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006253 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006254
6255 node = set->val.scnodes[i].scnode;
6256
6257 if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
Michal Vaskod3678892020-05-21 10:06:58 +02006258 new_node = lysc_data_parent(node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006259 } else {
6260 /* root does not have a parent */
6261 continue;
6262 }
6263
Michal Vasko03ff5a72019-09-11 13:49:33 +02006264 /* node has no parent */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006265 if (!new_node) {
6266 new_type = set->root_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006267
6268 /* node has a standard parent (it can equal the root, it's not the root yet since they are fake) */
6269 } else {
6270 new_type = LYXP_NODE_ELEM;
6271 }
6272
Michal Vaskoecd62de2019-11-13 12:35:11 +01006273 idx = lyxp_set_scnode_insert_node(set, new_node, new_type);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006274 if ((idx < orig_used) && (idx > i)) {
6275 set->val.scnodes[idx].in_ctx = 2;
6276 temp_ctx = 1;
6277 }
6278 }
6279
6280 if (temp_ctx) {
6281 for (i = 0; i < orig_used; ++i) {
6282 if (set->val.scnodes[i].in_ctx == 2) {
6283 set->val.scnodes[i].in_ctx = 1;
6284 }
6285 }
6286 }
6287
6288 return LY_SUCCESS;
6289}
6290
6291/**
6292 * @brief Move context @p set to the result of a comparison. Handles '=', '!=', '<=', '<', '>=', or '>'.
6293 * Result is LYXP_SET_BOOLEAN. Indirectly context position aware.
6294 *
6295 * @param[in,out] set1 Set to use for the result.
6296 * @param[in] set2 Set acting as the second operand for @p op.
6297 * @param[in] op Comparison operator to process.
6298 * @param[in] options XPath options.
6299 * @return LY_ERR
6300 */
6301static LY_ERR
6302moveto_op_comp(struct lyxp_set *set1, struct lyxp_set *set2, const char *op, int options)
6303{
6304 /*
6305 * NODE SET + NODE SET = NODE SET + STRING /(1 NODE SET) 2 STRING
6306 * NODE SET + STRING = STRING + STRING /1 STRING (2 STRING)
6307 * NODE SET + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6308 * NODE SET + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6309 * STRING + NODE SET = STRING + STRING /(1 STRING) 2 STRING
6310 * NUMBER + NODE SET = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6311 * BOOLEAN + NODE SET = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6312 *
6313 * '=' or '!='
6314 * BOOLEAN + BOOLEAN
6315 * BOOLEAN + STRING = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6316 * BOOLEAN + NUMBER = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6317 * STRING + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6318 * NUMBER + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6319 * NUMBER + NUMBER
6320 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6321 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6322 * STRING + STRING
6323 *
6324 * '<=', '<', '>=', '>'
6325 * NUMBER + NUMBER
6326 * BOOLEAN + BOOLEAN = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6327 * BOOLEAN + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6328 * BOOLEAN + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6329 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6330 * STRING + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6331 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6332 * NUMBER + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6333 * STRING + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6334 */
6335 struct lyxp_set iter1, iter2;
6336 int result;
6337 int64_t i;
6338 LY_ERR rc;
6339
Michal Vaskod3678892020-05-21 10:06:58 +02006340 iter1.type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006341
6342 /* iterative evaluation with node-sets */
6343 if ((set1->type == LYXP_SET_NODE_SET) || (set2->type == LYXP_SET_NODE_SET)) {
6344 if (set1->type == LYXP_SET_NODE_SET) {
6345 for (i = 0; i < set1->used; ++i) {
6346 switch (set2->type) {
6347 case LYXP_SET_NUMBER:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006348 rc = set_comp_cast(&iter1, set1, LYXP_SET_NUMBER, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006349 break;
6350 case LYXP_SET_BOOLEAN:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006351 rc = set_comp_cast(&iter1, set1, LYXP_SET_BOOLEAN, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006352 break;
6353 default:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006354 rc = set_comp_cast(&iter1, set1, LYXP_SET_STRING, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006355 break;
6356 }
6357 LY_CHECK_RET(rc);
6358
6359 rc = moveto_op_comp(&iter1, set2, op, options);
6360 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006361 lyxp_set_free_content(&iter1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006362 return rc;
6363 }
6364
6365 /* lazy evaluation until true */
6366 if (iter1.val.bool) {
6367 set_fill_boolean(set1, 1);
6368 return LY_SUCCESS;
6369 }
6370 }
6371 } else {
6372 for (i = 0; i < set2->used; ++i) {
6373 switch (set1->type) {
6374 case LYXP_SET_NUMBER:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006375 rc = set_comp_cast(&iter2, set2, LYXP_SET_NUMBER, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006376 break;
6377 case LYXP_SET_BOOLEAN:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006378 rc = set_comp_cast(&iter2, set2, LYXP_SET_BOOLEAN, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006379 break;
6380 default:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006381 rc = set_comp_cast(&iter2, set2, LYXP_SET_STRING, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006382 break;
6383 }
6384 LY_CHECK_RET(rc);
6385
6386 set_fill_set(&iter1, set1);
6387
6388 rc = moveto_op_comp(&iter1, &iter2, op, options);
6389 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006390 lyxp_set_free_content(&iter1);
6391 lyxp_set_free_content(&iter2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006392 return rc;
6393 }
Michal Vaskod3678892020-05-21 10:06:58 +02006394 lyxp_set_free_content(&iter2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006395
6396 /* lazy evaluation until true */
6397 if (iter1.val.bool) {
6398 set_fill_boolean(set1, 1);
6399 return LY_SUCCESS;
6400 }
6401 }
6402 }
6403
6404 /* false for all nodes */
6405 set_fill_boolean(set1, 0);
6406 return LY_SUCCESS;
6407 }
6408
6409 /* first convert properly */
6410 if ((op[0] == '=') || (op[0] == '!')) {
6411 if ((set1->type == LYXP_SET_BOOLEAN) || (set2->type == LYXP_SET_BOOLEAN)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006412 lyxp_set_cast(set1, LYXP_SET_BOOLEAN);
6413 lyxp_set_cast(set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006414 } else if ((set1->type == LYXP_SET_NUMBER) || (set2->type == LYXP_SET_NUMBER)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006415 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006416 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006417 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006418 LY_CHECK_RET(rc);
6419 } /* else we have 2 strings */
6420 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006421 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006422 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006423 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006424 LY_CHECK_RET(rc);
6425 }
6426
6427 assert(set1->type == set2->type);
6428
6429 /* compute result */
6430 if (op[0] == '=') {
6431 if (set1->type == LYXP_SET_BOOLEAN) {
6432 result = (set1->val.bool == set2->val.bool);
6433 } else if (set1->type == LYXP_SET_NUMBER) {
6434 result = (set1->val.num == set2->val.num);
6435 } else {
6436 assert(set1->type == LYXP_SET_STRING);
Michal Vaskoac6c72f2019-11-14 16:09:34 +01006437 result = !strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006438 }
6439 } else if (op[0] == '!') {
6440 if (set1->type == LYXP_SET_BOOLEAN) {
6441 result = (set1->val.bool != set2->val.bool);
6442 } else if (set1->type == LYXP_SET_NUMBER) {
6443 result = (set1->val.num != set2->val.num);
6444 } else {
6445 assert(set1->type == LYXP_SET_STRING);
Michal Vaskoac6c72f2019-11-14 16:09:34 +01006446 result = !strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006447 }
6448 } else {
6449 assert(set1->type == LYXP_SET_NUMBER);
6450 if (op[0] == '<') {
6451 if (op[1] == '=') {
6452 result = (set1->val.num <= set2->val.num);
6453 } else {
6454 result = (set1->val.num < set2->val.num);
6455 }
6456 } else {
6457 if (op[1] == '=') {
6458 result = (set1->val.num >= set2->val.num);
6459 } else {
6460 result = (set1->val.num > set2->val.num);
6461 }
6462 }
6463 }
6464
6465 /* assign result */
6466 if (result) {
6467 set_fill_boolean(set1, 1);
6468 } else {
6469 set_fill_boolean(set1, 0);
6470 }
6471
6472 return LY_SUCCESS;
6473}
6474
6475/**
6476 * @brief Move context @p set to the result of a basic operation. Handles '+', '-', unary '-', '*', 'div',
6477 * or 'mod'. Result is LYXP_SET_NUMBER. Indirectly context position aware.
6478 *
6479 * @param[in,out] set1 Set to use for the result.
6480 * @param[in] set2 Set acting as the second operand for @p op.
6481 * @param[in] op Operator to process.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006482 * @return LY_ERR
6483 */
6484static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006485moveto_op_math(struct lyxp_set *set1, struct lyxp_set *set2, const char *op)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006486{
6487 LY_ERR rc;
6488
6489 /* unary '-' */
6490 if (!set2 && (op[0] == '-')) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006491 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006492 LY_CHECK_RET(rc);
6493 set1->val.num *= -1;
6494 lyxp_set_free(set2);
6495 return LY_SUCCESS;
6496 }
6497
6498 assert(set1 && set2);
6499
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006500 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006501 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006502 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006503 LY_CHECK_RET(rc);
6504
6505 switch (op[0]) {
6506 /* '+' */
6507 case '+':
6508 set1->val.num += set2->val.num;
6509 break;
6510
6511 /* '-' */
6512 case '-':
6513 set1->val.num -= set2->val.num;
6514 break;
6515
6516 /* '*' */
6517 case '*':
6518 set1->val.num *= set2->val.num;
6519 break;
6520
6521 /* 'div' */
6522 case 'd':
6523 set1->val.num /= set2->val.num;
6524 break;
6525
6526 /* 'mod' */
6527 case 'm':
6528 set1->val.num = ((long long)set1->val.num) % ((long long)set2->val.num);
6529 break;
6530
6531 default:
6532 LOGINT_RET(set1->ctx);
6533 }
6534
6535 return LY_SUCCESS;
6536}
6537
6538/*
6539 * eval functions
6540 *
6541 * They execute a parsed XPath expression on some data subtree.
6542 */
6543
6544/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02006545 * @brief Evaluate Predicate. Logs directly on error.
6546 *
Michal Vaskod3678892020-05-21 10:06:58 +02006547 * [9] Predicate ::= '[' Expr ']'
Michal Vasko03ff5a72019-09-11 13:49:33 +02006548 *
6549 * @param[in] exp Parsed XPath expression.
6550 * @param[in] exp_idx Position in the expression @p exp.
6551 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
6552 * @param[in] options XPath options.
6553 * @param[in] parent_pos_pred Whether parent predicate was a positional one.
6554 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6555 */
6556static LY_ERR
6557eval_predicate(struct lyxp_expr *exp, uint16_t *exp_idx, struct lyxp_set *set, int options, int parent_pos_pred)
6558{
6559 LY_ERR rc;
Michal Vasko57eab132019-09-24 11:46:26 +02006560 uint16_t i, orig_exp;
Michal Vasko5c4e5892019-11-14 12:31:38 +01006561 uint32_t orig_pos, orig_size;
6562 int32_t pred_in_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006563 struct lyxp_set set2;
6564 struct lyd_node *orig_parent;
6565
6566 /* '[' */
6567 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
6568 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
6569 ++(*exp_idx);
6570
6571 if (!set) {
6572only_parse:
6573 rc = eval_expr_select(exp, exp_idx, 0, NULL, options);
6574 LY_CHECK_RET(rc);
6575 } else if (set->type == LYXP_SET_NODE_SET) {
6576 /* 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 +01006577 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006578
6579 /* empty set, nothing to evaluate */
6580 if (!set->used) {
6581 goto only_parse;
6582 }
6583
6584 orig_exp = *exp_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006585 orig_pos = 0;
6586 orig_size = set->used;
6587 orig_parent = NULL;
Michal Vasko39dbf352020-05-21 10:08:59 +02006588 for (i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006589 set_init(&set2, set);
6590 set_insert_node(&set2, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, 0);
6591 /* remember the node context position for position() and context size for last(),
6592 * predicates should always be evaluated with respect to the child axis (since we do
6593 * not support explicit axes) so we assign positions based on their parents */
6594 if (parent_pos_pred && ((struct lyd_node *)set->val.nodes[i].node->parent != orig_parent)) {
6595 orig_parent = (struct lyd_node *)set->val.nodes[i].node->parent;
6596 orig_pos = 1;
6597 } else {
6598 ++orig_pos;
6599 }
6600
6601 set2.ctx_pos = orig_pos;
6602 set2.ctx_size = orig_size;
6603 *exp_idx = orig_exp;
6604
6605 rc = eval_expr_select(exp, exp_idx, 0, &set2, options);
6606 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006607 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006608 return rc;
6609 }
6610
6611 /* number is a position */
6612 if (set2.type == LYXP_SET_NUMBER) {
6613 if ((long long)set2.val.num == orig_pos) {
6614 set2.val.num = 1;
6615 } else {
6616 set2.val.num = 0;
6617 }
6618 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006619 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006620
6621 /* predicate satisfied or not? */
Michal Vasko57eab132019-09-24 11:46:26 +02006622 if (!set2.val.bool) {
Michal Vasko2caefc12019-11-14 16:07:56 +01006623 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006624 }
6625 }
Michal Vasko2caefc12019-11-14 16:07:56 +01006626 set_remove_nodes_none(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006627
6628 } else if (set->type == LYXP_SET_SCNODE_SET) {
6629 for (i = 0; i < set->used; ++i) {
6630 if (set->val.scnodes[i].in_ctx == 1) {
6631 /* there is a currently-valid node */
6632 break;
6633 }
6634 }
6635 /* empty set, nothing to evaluate */
6636 if (i == set->used) {
6637 goto only_parse;
6638 }
6639
6640 orig_exp = *exp_idx;
6641
Michal Vasko03ff5a72019-09-11 13:49:33 +02006642 /* set special in_ctx to all the valid snodes */
6643 pred_in_ctx = set_scnode_new_in_ctx(set);
6644
6645 /* use the valid snodes one-by-one */
6646 for (i = 0; i < set->used; ++i) {
6647 if (set->val.scnodes[i].in_ctx != pred_in_ctx) {
6648 continue;
6649 }
6650 set->val.scnodes[i].in_ctx = 1;
6651
6652 *exp_idx = orig_exp;
6653
6654 rc = eval_expr_select(exp, exp_idx, 0, set, options);
6655 LY_CHECK_RET(rc);
6656
6657 set->val.scnodes[i].in_ctx = pred_in_ctx;
6658 }
6659
6660 /* restore the state as it was before the predicate */
6661 for (i = 0; i < set->used; ++i) {
6662 if (set->val.scnodes[i].in_ctx == 1) {
6663 set->val.scnodes[i].in_ctx = 0;
6664 } else if (set->val.scnodes[i].in_ctx == pred_in_ctx) {
6665 set->val.scnodes[i].in_ctx = 1;
6666 }
6667 }
6668
6669 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02006670 set2.type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006671 set_fill_set(&set2, set);
6672
6673 rc = eval_expr_select(exp, exp_idx, 0, &set2, options);
6674 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006675 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006676 return rc;
6677 }
6678
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006679 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006680 if (!set2.val.bool) {
Michal Vaskod3678892020-05-21 10:06:58 +02006681 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006682 }
Michal Vaskod3678892020-05-21 10:06:58 +02006683 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006684 }
6685
6686 /* ']' */
6687 assert(exp->tokens[*exp_idx] == LYXP_TOKEN_BRACK2);
6688 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
6689 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
6690 ++(*exp_idx);
6691
6692 return LY_SUCCESS;
6693}
6694
6695/**
Michal Vaskod3678892020-05-21 10:06:58 +02006696 * @brief Evaluate Literal. Logs directly on error.
6697 *
6698 * @param[in] exp Parsed XPath expression.
6699 * @param[in] exp_idx Position in the expression @p exp.
6700 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
6701 */
6702static void
6703eval_literal(struct lyxp_expr *exp, uint16_t *exp_idx, struct lyxp_set *set)
6704{
6705 if (set) {
6706 if (exp->tok_len[*exp_idx] == 2) {
6707 set_fill_string(set, "", 0);
6708 } else {
6709 set_fill_string(set, &exp->expr[exp->tok_pos[*exp_idx] + 1], exp->tok_len[*exp_idx] - 2);
6710 }
6711 }
6712 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
6713 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
6714 ++(*exp_idx);
6715}
6716
6717/**
6718 * @brief Check and parse a predicate following a leaf-list to extract its value.
6719 * On success all the used tokens are skipped.
6720 *
6721 * @param[in] exp Parsed XPath expression.
6722 * @param[in] exp_idx Position in the expression @p exp.
6723 * @param[out] val Leaf-list value on success.
6724 * @param[out] val_len Length of @p val.
6725 * @return LY_ERR
6726 */
6727static LY_ERR
6728eval_name_test_parse_leaflist_predicate(struct lyxp_expr *exp, uint16_t *exp_idx, const char **val, uint16_t *val_len)
6729{
6730 uint16_t cur_idx;
6731
6732 cur_idx = *exp_idx;
6733
6734 /* '[' */
6735 if ((exp->used == cur_idx) || (exp->tokens[cur_idx] != LYXP_TOKEN_BRACK1)) {
6736 return LY_EINVAL;
6737 }
6738 ++cur_idx;
6739
6740 /* '.' */
6741 if ((exp->used == cur_idx) || (exp->tokens[cur_idx] != LYXP_TOKEN_DOT)) {
6742 return LY_EINVAL;
6743 }
6744 ++cur_idx;
6745
6746 /* '=' */
6747 if ((exp->used == cur_idx) || (exp->tokens[cur_idx] != LYXP_TOKEN_OPERATOR_COMP)
6748 || (exp->expr[exp->tok_pos[cur_idx]] != '=')) {
6749 return LY_EINVAL;
6750 }
6751 ++cur_idx;
6752
6753 /* value */
6754 if ((exp->used == cur_idx) || (exp->tokens[cur_idx] != LYXP_TOKEN_LITERAL)) {
6755 return LY_EINVAL;
6756 }
6757 ++cur_idx;
6758
6759 /* ']' */
6760 if ((exp->used == cur_idx) || (exp->tokens[cur_idx] != LYXP_TOKEN_BRACK2)) {
6761 return LY_EINVAL;
6762 }
6763 ++cur_idx;
6764
6765 /* success */
6766 *val = &exp->expr[exp->tok_pos[cur_idx - 2] + 1];
6767 *val_len = exp->tok_len[cur_idx - 2] - 2;
6768 *exp_idx = cur_idx;
6769 return LY_SUCCESS;
6770}
6771
6772/**
6773 * @brief Check and parse a predicate following a list to make sure all the tokens are as expected (all key values defined).
6774 * On success all the used tokens are skipped.
6775 *
6776 * @param[in] exp Parsed XPath expression.
6777 * @param[in] exp_idx Position in the expression @p exp.
6778 * @param[in] slist Schema node of the list.
6779 * @param[out] val Leaf-list value on success.
6780 * @param[out] val_len Length of @p val.
6781 * @return LY_ERR
6782 */
6783static LY_ERR
6784eval_name_test_parse_list_predicate(struct lyxp_expr *exp, uint16_t *exp_idx, const struct lysc_node *slist,
6785 const char **keys, uint16_t *keys_len)
6786{
6787 uint16_t key_count, i, cur_idx;
6788 const struct lysc_node *key;
6789
6790 if (slist->flags & LYS_KEYLESS) {
6791 /* invalid */
6792 return LY_EINVAL;
6793 }
6794
6795 /* get key count */
6796 key_count = 0;
6797 for (key = lysc_node_children(slist, 0); key && (key->flags & LYS_KEY); key = key->next) {
6798 ++key_count;
6799 }
6800
6801 /* briefly check the predicate for each key */
6802 cur_idx = *exp_idx;
6803 for (i = 0; i < key_count; ++i) {
6804 /* '[' */
6805 if ((exp->used == cur_idx) || (exp->tokens[cur_idx] != LYXP_TOKEN_BRACK1)) {
6806 return LY_EINVAL;
6807 }
6808 ++cur_idx;
6809
6810 /* key-name */
6811 if ((exp->used == cur_idx) || (exp->tokens[cur_idx] != LYXP_TOKEN_NAMETEST)) {
6812 return LY_EINVAL;
6813 }
6814 ++cur_idx;
6815
6816 /* '=' */
6817 if ((exp->used == cur_idx) || (exp->tokens[cur_idx] != LYXP_TOKEN_OPERATOR_COMP)
6818 || (exp->expr[exp->tok_pos[cur_idx]] != '=')) {
6819 return LY_EINVAL;
6820 }
6821 ++cur_idx;
6822
6823 /* key-value */
6824 if ((exp->used == cur_idx) || (exp->tokens[cur_idx] != LYXP_TOKEN_LITERAL)) {
6825 return LY_EINVAL;
6826 }
6827 ++cur_idx;
6828
6829 /* ']' */
6830 if ((exp->used == cur_idx) || (exp->tokens[cur_idx] != LYXP_TOKEN_BRACK2)) {
6831 return LY_EINVAL;
6832 }
6833 ++cur_idx;
6834 }
6835
6836 /* success */
6837 *keys = &exp->expr[exp->tok_pos[*exp_idx]];
6838 *keys_len = (exp->expr + exp->tok_pos[cur_idx - 1] + exp->tok_len[cur_idx - 1]) - *keys;
6839 *exp_idx = cur_idx;
6840 return LY_SUCCESS;
6841}
6842
6843/**
6844 * @brief Evaluate NameTest and any following Predicates. Logs directly on error.
6845 *
6846 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
6847 * [6] NodeTest ::= NameTest | NodeType '(' ')'
6848 * [7] NameTest ::= '*' | NCName ':' '*' | QName
6849 *
6850 * @param[in] exp Parsed XPath expression.
6851 * @param[in] exp_idx Position in the expression @p exp.
6852 * @param[in] attr_axis Whether to search attributes or standard nodes.
6853 * @param[in] all_desc Whether to search all the descendants or children only.
6854 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
6855 * @param[in] options XPath options.
6856 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6857 */
6858static LY_ERR
6859eval_name_test_with_predicate(struct lyxp_expr *exp, uint16_t *exp_idx, int attr_axis, int all_desc, struct lyxp_set *set,
6860 int options)
6861{
6862 int i;
6863 char *path;
6864 const char *ncname = NULL, *key_val = NULL;
6865 uint16_t ncname_len, key_val_len, prev_exp_idx;
6866 const struct lys_module *moveto_mod;
6867 const struct lysc_node *scnode = NULL, *tmp;
6868 struct lyd_node *list_inst = NULL;
6869 LY_ERR rc = LY_SUCCESS;
6870
6871 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
6872 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
6873 ++(*exp_idx);
6874
6875 if (!set) {
6876 goto moveto;
6877 }
6878
6879 ncname = &exp->expr[exp->tok_pos[*exp_idx - 1]];
6880 ncname_len = exp->tok_len[*exp_idx - 1];
6881
6882 /* parse (and skip) module name */
6883 rc = moveto_resolve_model(&ncname, &ncname_len, set, &moveto_mod);
6884 LY_CHECK_GOTO(rc, cleanup);
6885
6886 if (moveto_mod && !attr_axis && !all_desc && (set->type == LYXP_SET_NODE_SET)) {
6887 /* find the matching schema node in some parent in the context */
6888 for (i = 0; i < (signed)set->used; ++i) {
6889 if (set->val.nodes[i].type == set->root_type) {
6890 tmp = lys_find_child(NULL, moveto_mod, ncname, ncname_len, 0, 0);
6891 } else if ((set->val.nodes[i].type == LYXP_NODE_ELEM)
6892 && (!scnode || (lysc_data_parent(scnode) != set->val.nodes[i].node->schema))) {
6893 /* do not repeat the same search */
6894 tmp = lys_find_child(set->val.nodes[i].node->schema, moveto_mod, ncname, ncname_len, 0, 0);
6895 }
6896
6897 /* additional context check */
6898 if (tmp && (set->root_type == LYXP_NODE_ROOT_CONFIG) && (tmp->flags & LYS_CONFIG_R)) {
6899 tmp = NULL;
6900 }
6901
6902 if (tmp) {
6903 if (scnode) {
6904 /* we found a schema node with the same name but at different level, give up, too complicated */
6905 scnode = NULL;
6906 break;
6907 } else {
6908 /* remember the found schema node and continue to make sure it can be used */
6909 scnode = tmp;
6910 }
6911 tmp = NULL;
6912 }
6913 }
6914
6915 if (scnode && (scnode->nodetype == LYS_LIST)) {
6916 /* make sure all the tokens are right */
6917 prev_exp_idx = *exp_idx;
6918 if (eval_name_test_parse_list_predicate(exp, exp_idx, scnode, &key_val, &key_val_len)) {
6919 scnode = NULL;
6920 }
6921
6922 /* try to create a list instance */
6923 if (scnode && lyd_create_list(scnode, key_val, key_val_len, set->format, 0, &list_inst)) {
6924 /* for whatever reason the list failed to be created, just use standard name comparison and
6925 * parse predicate normally */
6926 *exp_idx = prev_exp_idx;
6927 scnode = NULL;
6928 }
6929 } else if (scnode && (scnode->nodetype == LYS_LEAFLIST)) {
6930 /* make sure we know the leaf-list value */
6931 if (eval_name_test_parse_leaflist_predicate(exp, exp_idx, &key_val, &key_val_len)) {
6932 /* value could not be recognized, use standard name comparison */
6933 scnode = NULL;
6934 }
6935 }
6936 }
6937
6938 if (!scnode) {
6939 /* we are not able to match based on a schema node */
6940 if (!moveto_mod) {
6941 /* '*', all nodes match */
6942 ncname = NULL;
6943 } else {
6944 /* insert name into dictionary for efficient comparison */
6945 ncname = lydict_insert(set->ctx, ncname, ncname_len);
6946 }
6947 }
6948
6949moveto:
6950 /* move to the attribute(s), data node(s), or schema node(s) */
6951 if (attr_axis) {
6952 if (set && (options & LYXP_SCNODE_ALL)) {
6953 set_scnode_clear_ctx(set);
6954 } else {
6955 if (all_desc) {
6956 rc = moveto_attr_alldesc(set, moveto_mod, ncname);
6957 } else {
6958 rc = moveto_attr(set, moveto_mod, ncname);
6959 }
6960 LY_CHECK_GOTO(rc, cleanup);
6961 }
6962 } else {
6963 if (set && (options & LYXP_SCNODE_ALL)) {
6964 if (all_desc) {
6965 rc = moveto_scnode_alldesc(set, moveto_mod, ncname, options);
6966 } else {
6967 rc = moveto_scnode(set, moveto_mod, ncname, options);
6968 }
6969 LY_CHECK_GOTO(rc, cleanup);
6970
6971 for (i = set->used - 1; i > -1; --i) {
6972 if (set->val.scnodes[i].in_ctx > 0) {
6973 break;
6974 }
6975 }
6976 if (i == -1) {
6977 path = lysc_path(set->ctx_scnode, LYSC_PATH_LOG, NULL, 0);
6978 LOGWRN(set->ctx, "Schema node \"%.*s\" not found (%.*s) with context node \"%s\".",
6979 exp->tok_len[*exp_idx], &exp->expr[exp->tok_pos[*exp_idx]],
6980 exp->tok_pos[*exp_idx] + exp->tok_len[*exp_idx], exp->expr, path);
6981 free(path);
6982 }
6983 } else {
6984 if (all_desc) {
6985 rc = moveto_node_alldesc(set, moveto_mod, ncname);
6986 } else {
6987 if (scnode) {
6988 /* we can find the nodes using hashes */
6989 rc = moveto_node_hash(set, scnode, list_inst, key_val, key_val_len);
6990 } else {
6991 rc = moveto_node(set, moveto_mod, ncname);
6992 }
6993 }
6994 LY_CHECK_GOTO(rc, cleanup);
6995 }
6996 }
6997
6998 /* Predicate* */
Michal Vasko14676352020-05-29 11:35:55 +02006999 while (!lyxp_check_token(NULL, exp, *exp_idx, LYXP_TOKEN_BRACK1)) {
Michal Vaskod3678892020-05-21 10:06:58 +02007000 rc = eval_predicate(exp, exp_idx, set, options, 1);
7001 LY_CHECK_RET(rc);
7002 }
7003
7004cleanup:
Michal Vaskodb51a8d2020-05-27 15:22:29 +02007005 if (set) {
7006 lydict_remove(set->ctx, ncname);
7007 }
Michal Vaskod3678892020-05-21 10:06:58 +02007008 lyd_free_tree(list_inst);
7009 return rc;
7010}
7011
7012/**
7013 * @brief Evaluate NodeType and any following Predicates. Logs directly on error.
7014 *
7015 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
7016 * [6] NodeTest ::= NameTest | NodeType '(' ')'
7017 * [8] NodeType ::= 'text' | 'node'
7018 *
7019 * @param[in] exp Parsed XPath expression.
7020 * @param[in] exp_idx Position in the expression @p exp.
7021 * @param[in] attr_axis Whether to search attributes or standard nodes.
7022 * @param[in] all_desc Whether to search all the descendants or children only.
7023 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7024 * @param[in] options XPath options.
7025 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7026 */
7027static LY_ERR
7028eval_node_type_with_predicate(struct lyxp_expr *exp, uint16_t *exp_idx, int attr_axis, int all_desc,
7029 struct lyxp_set *set, int options)
7030{
7031 LY_ERR rc;
7032
7033 /* TODO */
7034 (void)attr_axis;
7035 (void)all_desc;
7036
7037 if (set) {
7038 assert(exp->tok_len[*exp_idx] == 4);
7039 if (set->type == LYXP_SET_SCNODE_SET) {
7040 set_scnode_clear_ctx(set);
7041 /* just for the debug message below */
7042 set = NULL;
7043 } else {
7044 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "node", 4)) {
7045 rc = xpath_node(NULL, 0, set, options);
7046 } else {
7047 assert(!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "text", 4));
7048 rc = xpath_text(NULL, 0, set, options);
7049 }
7050 LY_CHECK_RET(rc);
7051 }
7052 }
7053 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7054 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7055 ++(*exp_idx);
7056
7057 /* '(' */
7058 assert(exp->tokens[*exp_idx] == LYXP_TOKEN_PAR1);
7059 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7060 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7061 ++(*exp_idx);
7062
7063 /* ')' */
7064 assert(exp->tokens[*exp_idx] == LYXP_TOKEN_PAR2);
7065 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7066 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7067 ++(*exp_idx);
7068
7069 /* Predicate* */
Michal Vasko14676352020-05-29 11:35:55 +02007070 while (!lyxp_check_token(NULL, exp, *exp_idx, LYXP_TOKEN_BRACK1)) {
Michal Vaskod3678892020-05-21 10:06:58 +02007071 rc = eval_predicate(exp, exp_idx, set, options, 1);
7072 LY_CHECK_RET(rc);
7073 }
7074
7075 return LY_SUCCESS;
7076}
7077
7078/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02007079 * @brief Evaluate RelativeLocationPath. Logs directly on error.
7080 *
7081 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
7082 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
Michal Vaskod3678892020-05-21 10:06:58 +02007083 * [6] NodeTest ::= NameTest | NodeType '(' ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007084 *
7085 * @param[in] exp Parsed XPath expression.
7086 * @param[in] exp_idx Position in the expression @p exp.
7087 * @param[in] all_desc Whether to search all the descendants or children only.
7088 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7089 * @param[in] options XPath options.
7090 * @return LY_ERR (YL_EINCOMPLETE on unresolved when)
7091 */
7092static LY_ERR
7093eval_relative_location_path(struct lyxp_expr *exp, uint16_t *exp_idx, int all_desc, struct lyxp_set *set, int options)
7094{
7095 int attr_axis;
7096 LY_ERR rc;
7097
7098 goto step;
7099 do {
7100 /* evaluate '/' or '//' */
7101 if (exp->tok_len[*exp_idx] == 1) {
7102 all_desc = 0;
7103 } else {
7104 assert(exp->tok_len[*exp_idx] == 2);
7105 all_desc = 1;
7106 }
7107 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7108 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7109 ++(*exp_idx);
7110
7111step:
Michal Vaskod3678892020-05-21 10:06:58 +02007112 /* evaluate abbreviated axis '@'? if any */
7113 if (exp->tokens[*exp_idx] == LYXP_TOKEN_AT) {
7114 attr_axis = 1;
7115
7116 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7117 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7118 ++(*exp_idx);
7119 } else {
7120 attr_axis = 0;
7121 }
7122
Michal Vasko03ff5a72019-09-11 13:49:33 +02007123 /* Step */
Michal Vasko03ff5a72019-09-11 13:49:33 +02007124 switch (exp->tokens[*exp_idx]) {
7125 case LYXP_TOKEN_DOT:
7126 /* evaluate '.' */
7127 if (set && (options & LYXP_SCNODE_ALL)) {
7128 rc = moveto_scnode_self(set, all_desc, options);
7129 } else {
7130 rc = moveto_self(set, all_desc, options);
7131 }
7132 LY_CHECK_RET(rc);
7133 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7134 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7135 ++(*exp_idx);
7136 break;
7137
7138 case LYXP_TOKEN_DDOT:
7139 /* evaluate '..' */
7140 if (set && (options & LYXP_SCNODE_ALL)) {
7141 rc = moveto_scnode_parent(set, all_desc, options);
7142 } else {
7143 rc = moveto_parent(set, all_desc, options);
7144 }
7145 LY_CHECK_RET(rc);
7146 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7147 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7148 ++(*exp_idx);
7149 break;
7150
Michal Vasko03ff5a72019-09-11 13:49:33 +02007151 case LYXP_TOKEN_NAMETEST:
Michal Vaskod3678892020-05-21 10:06:58 +02007152 /* evaluate NameTest Predicate* */
7153 rc = eval_name_test_with_predicate(exp, exp_idx, attr_axis, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007154 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02007155 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007156
Michal Vaskod3678892020-05-21 10:06:58 +02007157 case LYXP_TOKEN_NODETYPE:
7158 /* evaluate NodeType Predicate* */
7159 rc = eval_node_type_with_predicate(exp, exp_idx, attr_axis, all_desc, set, options);
7160 LY_CHECK_RET(rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007161 break;
7162
7163 default:
Michal Vasko02a77382019-09-12 11:47:35 +02007164 LOGINT_RET(set ? set->ctx : NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007165 }
Michal Vasko14676352020-05-29 11:35:55 +02007166 } while (!exp_check_token2(NULL, exp, *exp_idx, LYXP_TOKEN_OPERATOR_PATH, LYXP_TOKEN_OPERATOR_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007167
7168 return LY_SUCCESS;
7169}
7170
7171/**
7172 * @brief Evaluate AbsoluteLocationPath. Logs directly on error.
7173 *
7174 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
7175 *
7176 * @param[in] exp Parsed XPath expression.
7177 * @param[in] exp_idx Position in the expression @p exp.
7178 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7179 * @param[in] options XPath options.
7180 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7181 */
7182static LY_ERR
7183eval_absolute_location_path(struct lyxp_expr *exp, uint16_t *exp_idx, struct lyxp_set *set, int options)
7184{
7185 int all_desc;
7186 LY_ERR rc;
7187
7188 if (set) {
7189 /* no matter what tokens follow, we need to be at the root */
7190 moveto_root(set, options);
7191 }
7192
7193 /* '/' RelativeLocationPath? */
7194 if (exp->tok_len[*exp_idx] == 1) {
7195 /* evaluate '/' - deferred */
7196 all_desc = 0;
7197 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7198 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7199 ++(*exp_idx);
7200
Michal Vasko14676352020-05-29 11:35:55 +02007201 if (lyxp_check_token(NULL, exp, *exp_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007202 return LY_SUCCESS;
7203 }
7204 switch (exp->tokens[*exp_idx]) {
7205 case LYXP_TOKEN_DOT:
7206 case LYXP_TOKEN_DDOT:
7207 case LYXP_TOKEN_AT:
7208 case LYXP_TOKEN_NAMETEST:
7209 case LYXP_TOKEN_NODETYPE:
7210 rc = eval_relative_location_path(exp, exp_idx, all_desc, set, options);
7211 LY_CHECK_RET(rc);
7212 break;
7213 default:
7214 break;
7215 }
7216
7217 /* '//' RelativeLocationPath */
7218 } else {
7219 /* evaluate '//' - deferred so as not to waste memory by remembering all the nodes */
7220 all_desc = 1;
7221 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7222 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7223 ++(*exp_idx);
7224
7225 rc = eval_relative_location_path(exp, exp_idx, all_desc, set, options);
7226 LY_CHECK_RET(rc);
7227 }
7228
7229 return LY_SUCCESS;
7230}
7231
7232/**
7233 * @brief Evaluate FunctionCall. Logs directly on error.
7234 *
Michal Vaskod3678892020-05-21 10:06:58 +02007235 * [11] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007236 *
7237 * @param[in] exp Parsed XPath expression.
7238 * @param[in] exp_idx Position in the expression @p exp.
7239 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7240 * @param[in] options XPath options.
7241 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7242 */
7243static LY_ERR
7244eval_function_call(struct lyxp_expr *exp, uint16_t *exp_idx, struct lyxp_set *set, int options)
7245{
7246 LY_ERR rc;
7247 LY_ERR (*xpath_func)(struct lyxp_set **, uint16_t, struct lyxp_set *, int) = NULL;
Michal Vasko0cbf54f2019-12-16 10:01:06 +01007248 uint16_t arg_count = 0, i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007249 struct lyxp_set **args = NULL, **args_aux;
7250
7251 if (set) {
7252 /* FunctionName */
7253 switch (exp->tok_len[*exp_idx]) {
7254 case 3:
7255 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "not", 3)) {
7256 xpath_func = &xpath_not;
7257 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "sum", 3)) {
7258 xpath_func = &xpath_sum;
7259 }
7260 break;
7261 case 4:
7262 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "lang", 4)) {
7263 xpath_func = &xpath_lang;
7264 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "last", 4)) {
7265 xpath_func = &xpath_last;
7266 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "name", 4)) {
7267 xpath_func = &xpath_name;
7268 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "true", 4)) {
7269 xpath_func = &xpath_true;
7270 }
7271 break;
7272 case 5:
7273 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "count", 5)) {
7274 xpath_func = &xpath_count;
7275 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "false", 5)) {
7276 xpath_func = &xpath_false;
7277 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "floor", 5)) {
7278 xpath_func = &xpath_floor;
7279 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "round", 5)) {
7280 xpath_func = &xpath_round;
7281 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "deref", 5)) {
7282 xpath_func = &xpath_deref;
7283 }
7284 break;
7285 case 6:
7286 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "concat", 6)) {
7287 xpath_func = &xpath_concat;
7288 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "number", 6)) {
7289 xpath_func = &xpath_number;
7290 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "string", 6)) {
7291 xpath_func = &xpath_string;
7292 }
7293 break;
7294 case 7:
7295 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "boolean", 7)) {
7296 xpath_func = &xpath_boolean;
7297 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "ceiling", 7)) {
7298 xpath_func = &xpath_ceiling;
7299 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "current", 7)) {
7300 xpath_func = &xpath_current;
7301 }
7302 break;
7303 case 8:
7304 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "contains", 8)) {
7305 xpath_func = &xpath_contains;
7306 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "position", 8)) {
7307 xpath_func = &xpath_position;
7308 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "re-match", 8)) {
7309 xpath_func = &xpath_re_match;
7310 }
7311 break;
7312 case 9:
7313 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "substring", 9)) {
7314 xpath_func = &xpath_substring;
7315 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "translate", 9)) {
7316 xpath_func = &xpath_translate;
7317 }
7318 break;
7319 case 10:
7320 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "local-name", 10)) {
7321 xpath_func = &xpath_local_name;
7322 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "enum-value", 10)) {
7323 xpath_func = &xpath_enum_value;
7324 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "bit-is-set", 10)) {
7325 xpath_func = &xpath_bit_is_set;
7326 }
7327 break;
7328 case 11:
7329 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "starts-with", 11)) {
7330 xpath_func = &xpath_starts_with;
7331 }
7332 break;
7333 case 12:
7334 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "derived-from", 12)) {
7335 xpath_func = &xpath_derived_from;
7336 }
7337 break;
7338 case 13:
7339 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "namespace-uri", 13)) {
7340 xpath_func = &xpath_namespace_uri;
7341 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "string-length", 13)) {
7342 xpath_func = &xpath_string_length;
7343 }
7344 break;
7345 case 15:
7346 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "normalize-space", 15)) {
7347 xpath_func = &xpath_normalize_space;
7348 } else if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "substring-after", 15)) {
7349 xpath_func = &xpath_substring_after;
7350 }
7351 break;
7352 case 16:
7353 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "substring-before", 16)) {
7354 xpath_func = &xpath_substring_before;
7355 }
7356 break;
7357 case 20:
7358 if (!strncmp(&exp->expr[exp->tok_pos[*exp_idx]], "derived-from-or-self", 20)) {
7359 xpath_func = &xpath_derived_from_or_self;
7360 }
7361 break;
7362 }
7363
7364 if (!xpath_func) {
7365 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*exp_idx]]);
7366 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]]);
7367 return LY_EVALID;
7368 }
7369 }
7370
7371 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7372 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7373 ++(*exp_idx);
7374
7375 /* '(' */
7376 assert(exp->tokens[*exp_idx] == LYXP_TOKEN_PAR1);
7377 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7378 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7379 ++(*exp_idx);
7380
7381 /* ( Expr ( ',' Expr )* )? */
7382 if (exp->tokens[*exp_idx] != LYXP_TOKEN_PAR2) {
7383 if (set) {
7384 args = malloc(sizeof *args);
7385 LY_CHECK_ERR_GOTO(!args, LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7386 arg_count = 1;
7387 args[0] = set_copy(set);
7388 if (!args[0]) {
7389 rc = LY_EMEM;
7390 goto cleanup;
7391 }
7392
7393 rc = eval_expr_select(exp, exp_idx, 0, args[0], options);
7394 LY_CHECK_GOTO(rc, cleanup);
7395 } else {
7396 rc = eval_expr_select(exp, exp_idx, 0, NULL, options);
7397 LY_CHECK_GOTO(rc, cleanup);
7398 }
7399 }
Michal Vasko14676352020-05-29 11:35:55 +02007400 while (!lyxp_check_token(NULL, exp, *exp_idx, LYXP_TOKEN_COMMA)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007401 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7402 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7403 ++(*exp_idx);
7404
7405 if (set) {
7406 ++arg_count;
7407 args_aux = realloc(args, arg_count * sizeof *args);
7408 LY_CHECK_ERR_GOTO(!args_aux, arg_count--; LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7409 args = args_aux;
7410 args[arg_count - 1] = set_copy(set);
7411 if (!args[arg_count - 1]) {
7412 rc = LY_EMEM;
7413 goto cleanup;
7414 }
7415
7416 rc = eval_expr_select(exp, exp_idx, 0, args[arg_count - 1], options);
7417 LY_CHECK_GOTO(rc, cleanup);
7418 } else {
7419 rc = eval_expr_select(exp, exp_idx, 0, NULL, options);
7420 LY_CHECK_GOTO(rc, cleanup);
7421 }
7422 }
7423
7424 /* ')' */
7425 assert(exp->tokens[*exp_idx] == LYXP_TOKEN_PAR2);
7426 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7427 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7428 ++(*exp_idx);
7429
7430 if (set) {
7431 /* evaluate function */
7432 rc = xpath_func(args, arg_count, set, options);
7433
7434 if (options & LYXP_SCNODE_ALL) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007435 /* merge all nodes from arg evaluations */
7436 for (i = 0; i < arg_count; ++i) {
7437 set_scnode_clear_ctx(args[i]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007438 lyxp_set_scnode_merge(set, args[i]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007439 }
7440 }
7441 } else {
7442 rc = LY_SUCCESS;
7443 }
7444
7445cleanup:
7446 for (i = 0; i < arg_count; ++i) {
7447 lyxp_set_free(args[i]);
7448 }
7449 free(args);
7450
7451 return rc;
7452}
7453
7454/**
7455 * @brief Evaluate Number. Logs directly on error.
7456 *
7457 * @param[in] ctx Context for errors.
7458 * @param[in] exp Parsed XPath expression.
7459 * @param[in] exp_idx Position in the expression @p exp.
7460 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7461 * @return LY_ERR
7462 */
7463static LY_ERR
7464eval_number(struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *exp_idx, struct lyxp_set *set)
7465{
7466 long double num;
7467 char *endptr;
7468
7469 if (set) {
7470 errno = 0;
7471 num = strtold(&exp->expr[exp->tok_pos[*exp_idx]], &endptr);
7472 if (errno) {
7473 LOGVAL(ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*exp_idx]]);
7474 LOGVAL(ctx, LY_VLOG_LYD, set->ctx_node, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double (%s).",
7475 exp->tok_len[*exp_idx], &exp->expr[exp->tok_pos[*exp_idx]], strerror(errno));
7476 return LY_EVALID;
7477 } else if (endptr - &exp->expr[exp->tok_pos[*exp_idx]] != exp->tok_len[*exp_idx]) {
7478 LOGVAL(ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*exp_idx]]);
7479 LOGVAL(ctx, LY_VLOG_LYD, set->ctx_node, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double.",
7480 exp->tok_len[*exp_idx], &exp->expr[exp->tok_pos[*exp_idx]]);
7481 return LY_EVALID;
7482 }
7483
7484 set_fill_number(set, num);
7485 }
7486
7487 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7488 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7489 ++(*exp_idx);
7490 return LY_SUCCESS;
7491}
7492
7493/**
7494 * @brief Evaluate PathExpr. Logs directly on error.
7495 *
Michal Vaskod3678892020-05-21 10:06:58 +02007496 * [12] PathExpr ::= LocationPath | PrimaryExpr Predicate*
Michal Vasko03ff5a72019-09-11 13:49:33 +02007497 * | PrimaryExpr Predicate* '/' RelativeLocationPath
7498 * | PrimaryExpr Predicate* '//' RelativeLocationPath
7499 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
Michal Vaskod3678892020-05-21 10:06:58 +02007500 * [10] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall
Michal Vasko03ff5a72019-09-11 13:49:33 +02007501 *
7502 * @param[in] exp Parsed XPath expression.
7503 * @param[in] exp_idx Position in the expression @p exp.
7504 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7505 * @param[in] options XPath options.
7506 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7507 */
7508static LY_ERR
7509eval_path_expr(struct lyxp_expr *exp, uint16_t *exp_idx, struct lyxp_set *set, int options)
7510{
7511 int all_desc, parent_pos_pred;
7512 LY_ERR rc;
7513
7514 switch (exp->tokens[*exp_idx]) {
7515 case LYXP_TOKEN_PAR1:
7516 /* '(' Expr ')' */
7517
7518 /* '(' */
7519 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7520 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7521 ++(*exp_idx);
7522
7523 /* Expr */
7524 rc = eval_expr_select(exp, exp_idx, 0, set, options);
7525 LY_CHECK_RET(rc);
7526
7527 /* ')' */
7528 assert(exp->tokens[*exp_idx] == LYXP_TOKEN_PAR2);
7529 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7530 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7531 ++(*exp_idx);
7532
7533 parent_pos_pred = 0;
7534 goto predicate;
7535
7536 case LYXP_TOKEN_DOT:
7537 case LYXP_TOKEN_DDOT:
7538 case LYXP_TOKEN_AT:
7539 case LYXP_TOKEN_NAMETEST:
7540 case LYXP_TOKEN_NODETYPE:
7541 /* RelativeLocationPath */
7542 rc = eval_relative_location_path(exp, exp_idx, 0, set, options);
7543 LY_CHECK_RET(rc);
7544 break;
7545
7546 case LYXP_TOKEN_FUNCNAME:
7547 /* FunctionCall */
7548 if (!set) {
7549 rc = eval_function_call(exp, exp_idx, NULL, options);
7550 } else {
7551 rc = eval_function_call(exp, exp_idx, set, options);
7552 }
7553 LY_CHECK_RET(rc);
7554
7555 parent_pos_pred = 1;
7556 goto predicate;
7557
7558 case LYXP_TOKEN_OPERATOR_PATH:
Michal Vasko14676352020-05-29 11:35:55 +02007559 case LYXP_TOKEN_OPERATOR_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02007560 /* AbsoluteLocationPath */
7561 rc = eval_absolute_location_path(exp, exp_idx, set, options);
7562 LY_CHECK_RET(rc);
7563 break;
7564
7565 case LYXP_TOKEN_LITERAL:
7566 /* Literal */
7567 if (!set || (options & LYXP_SCNODE_ALL)) {
7568 if (set) {
7569 set_scnode_clear_ctx(set);
7570 }
7571 eval_literal(exp, exp_idx, NULL);
7572 } else {
7573 eval_literal(exp, exp_idx, set);
7574 }
7575
7576 parent_pos_pred = 1;
7577 goto predicate;
7578
7579 case LYXP_TOKEN_NUMBER:
7580 /* Number */
7581 if (!set || (options & LYXP_SCNODE_ALL)) {
7582 if (set) {
7583 set_scnode_clear_ctx(set);
7584 }
Michal Vasko02a77382019-09-12 11:47:35 +02007585 rc = eval_number(NULL, exp, exp_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007586 } else {
7587 rc = eval_number(set->ctx, exp, exp_idx, set);
7588 }
7589 LY_CHECK_RET(rc);
7590
7591 parent_pos_pred = 1;
7592 goto predicate;
7593
7594 default:
7595 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INTOK, print_token(exp->tokens[*exp_idx]),
7596 &exp->expr[exp->tok_pos[*exp_idx]]);
7597 return LY_EVALID;
7598 }
7599
7600 return LY_SUCCESS;
7601
7602predicate:
7603 /* Predicate* */
Michal Vasko14676352020-05-29 11:35:55 +02007604 while (!lyxp_check_token(NULL, exp, *exp_idx, LYXP_TOKEN_BRACK1)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007605 rc = eval_predicate(exp, exp_idx, set, options, parent_pos_pred);
7606 LY_CHECK_RET(rc);
7607 }
7608
7609 /* ('/' or '//') RelativeLocationPath */
Michal Vasko14676352020-05-29 11:35:55 +02007610 if (!exp_check_token2(NULL, exp, *exp_idx, LYXP_TOKEN_OPERATOR_PATH, LYXP_TOKEN_OPERATOR_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007611
7612 /* evaluate '/' or '//' */
Michal Vasko14676352020-05-29 11:35:55 +02007613 if (exp->tokens[*exp_idx] == LYXP_TOKEN_OPERATOR_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007614 all_desc = 0;
7615 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007616 all_desc = 1;
7617 }
7618
7619 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7620 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7621 ++(*exp_idx);
7622
7623 rc = eval_relative_location_path(exp, exp_idx, all_desc, set, options);
7624 LY_CHECK_RET(rc);
7625 }
7626
7627 return LY_SUCCESS;
7628}
7629
7630/**
7631 * @brief Evaluate UnionExpr. Logs directly on error.
7632 *
Michal Vaskod3678892020-05-21 10:06:58 +02007633 * [20] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007634 *
7635 * @param[in] exp Parsed XPath expression.
7636 * @param[in] exp_idx Position in the expression @p exp.
7637 * @param[in] repeat How many times this expression is repeated.
7638 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7639 * @param[in] options XPath options.
7640 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7641 */
7642static LY_ERR
7643eval_union_expr(struct lyxp_expr *exp, uint16_t *exp_idx, uint16_t repeat, struct lyxp_set *set, int options)
7644{
7645 LY_ERR rc = LY_SUCCESS;
7646 struct lyxp_set orig_set, set2;
7647 uint16_t i;
7648
7649 assert(repeat);
7650
7651 set_init(&orig_set, set);
7652 set_init(&set2, set);
7653
7654 set_fill_set(&orig_set, set);
7655
7656 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_UNION, set, options);
7657 LY_CHECK_GOTO(rc, cleanup);
7658
7659 /* ('|' PathExpr)* */
7660 for (i = 0; i < repeat; ++i) {
7661 assert(exp->tokens[*exp_idx] == LYXP_TOKEN_OPERATOR_UNI);
7662 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7663 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7664 ++(*exp_idx);
7665
7666 if (!set) {
7667 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_UNION, NULL, options);
7668 LY_CHECK_GOTO(rc, cleanup);
7669 continue;
7670 }
7671
7672 set_fill_set(&set2, &orig_set);
7673 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_UNION, &set2, options);
7674 LY_CHECK_GOTO(rc, cleanup);
7675
7676 /* eval */
7677 if (options & LYXP_SCNODE_ALL) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01007678 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007679 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007680 rc = moveto_union(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007681 LY_CHECK_GOTO(rc, cleanup);
7682 }
7683 }
7684
7685cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007686 lyxp_set_free_content(&orig_set);
7687 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007688 return rc;
7689}
7690
7691/**
7692 * @brief Evaluate UnaryExpr. Logs directly on error.
7693 *
Michal Vaskod3678892020-05-21 10:06:58 +02007694 * [19] UnaryExpr ::= UnionExpr | '-' UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007695 *
7696 * @param[in] exp Parsed XPath expression.
7697 * @param[in] exp_idx Position in the expression @p exp.
7698 * @param[in] repeat How many times this expression is repeated.
7699 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7700 * @param[in] options XPath options.
7701 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7702 */
7703static LY_ERR
7704eval_unary_expr(struct lyxp_expr *exp, uint16_t *exp_idx, uint16_t repeat, struct lyxp_set *set, int options)
7705{
7706 LY_ERR rc;
7707 uint16_t this_op, i;
7708
7709 assert(repeat);
7710
7711 /* ('-')+ */
7712 this_op = *exp_idx;
7713 for (i = 0; i < repeat; ++i) {
Michal Vasko14676352020-05-29 11:35:55 +02007714 assert(!lyxp_check_token(NULL, exp, *exp_idx, LYXP_TOKEN_OPERATOR_MATH) && (exp->expr[exp->tok_pos[*exp_idx]] == '-'));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007715
7716 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7717 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7718 ++(*exp_idx);
7719 }
7720
7721 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_UNARY, set, options);
7722 LY_CHECK_RET(rc);
7723
7724 if (set && (repeat % 2)) {
7725 if (options & LYXP_SCNODE_ALL) {
7726 warn_operands(set->ctx, set, NULL, 1, exp->expr, exp->tok_pos[this_op]);
7727 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007728 rc = moveto_op_math(set, NULL, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007729 LY_CHECK_RET(rc);
7730 }
7731 }
7732
7733 return LY_SUCCESS;
7734}
7735
7736/**
7737 * @brief Evaluate MultiplicativeExpr. Logs directly on error.
7738 *
Michal Vaskod3678892020-05-21 10:06:58 +02007739 * [18] MultiplicativeExpr ::= UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007740 * | MultiplicativeExpr '*' UnaryExpr
7741 * | MultiplicativeExpr 'div' UnaryExpr
7742 * | MultiplicativeExpr 'mod' UnaryExpr
7743 *
7744 * @param[in] exp Parsed XPath expression.
7745 * @param[in] exp_idx Position in the expression @p exp.
7746 * @param[in] repeat How many times this expression is repeated.
7747 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7748 * @param[in] options XPath options.
7749 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7750 */
7751static LY_ERR
7752eval_multiplicative_expr(struct lyxp_expr *exp, uint16_t *exp_idx, uint16_t repeat, struct lyxp_set *set, int options)
7753{
7754 LY_ERR rc;
7755 uint16_t this_op;
7756 struct lyxp_set orig_set, set2;
7757 uint16_t i;
7758
7759 assert(repeat);
7760
7761 set_init(&orig_set, set);
7762 set_init(&set2, set);
7763
7764 set_fill_set(&orig_set, set);
7765
7766 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_MULTIPLICATIVE, set, options);
7767 LY_CHECK_GOTO(rc, cleanup);
7768
7769 /* ('*' / 'div' / 'mod' UnaryExpr)* */
7770 for (i = 0; i < repeat; ++i) {
7771 this_op = *exp_idx;
7772
7773 assert(exp->tokens[*exp_idx] == LYXP_TOKEN_OPERATOR_MATH);
7774 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7775 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7776 ++(*exp_idx);
7777
7778 if (!set) {
7779 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_MULTIPLICATIVE, NULL, options);
7780 LY_CHECK_GOTO(rc, cleanup);
7781 continue;
7782 }
7783
7784 set_fill_set(&set2, &orig_set);
7785 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_MULTIPLICATIVE, &set2, options);
7786 LY_CHECK_GOTO(rc, cleanup);
7787
7788 /* eval */
7789 if (options & LYXP_SCNODE_ALL) {
7790 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007791 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007792 set_scnode_clear_ctx(set);
7793 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007794 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007795 LY_CHECK_GOTO(rc, cleanup);
7796 }
7797 }
7798
7799cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007800 lyxp_set_free_content(&orig_set);
7801 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007802 return rc;
7803}
7804
7805/**
7806 * @brief Evaluate AdditiveExpr. Logs directly on error.
7807 *
Michal Vaskod3678892020-05-21 10:06:58 +02007808 * [17] AdditiveExpr ::= MultiplicativeExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007809 * | AdditiveExpr '+' MultiplicativeExpr
7810 * | AdditiveExpr '-' MultiplicativeExpr
7811 *
7812 * @param[in] exp Parsed XPath expression.
7813 * @param[in] exp_idx Position in the expression @p exp.
7814 * @param[in] repeat How many times this expression is repeated.
7815 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7816 * @param[in] options XPath options.
7817 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7818 */
7819static LY_ERR
7820eval_additive_expr(struct lyxp_expr *exp, uint16_t *exp_idx, uint16_t repeat, struct lyxp_set *set, int options)
7821{
7822 LY_ERR rc;
7823 uint16_t this_op;
7824 struct lyxp_set orig_set, set2;
7825 uint16_t i;
7826
7827 assert(repeat);
7828
7829 set_init(&orig_set, set);
7830 set_init(&set2, set);
7831
7832 set_fill_set(&orig_set, set);
7833
7834 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_ADDITIVE, set, options);
7835 LY_CHECK_GOTO(rc, cleanup);
7836
7837 /* ('+' / '-' MultiplicativeExpr)* */
7838 for (i = 0; i < repeat; ++i) {
7839 this_op = *exp_idx;
7840
7841 assert(exp->tokens[*exp_idx] == LYXP_TOKEN_OPERATOR_MATH);
7842 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7843 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7844 ++(*exp_idx);
7845
7846 if (!set) {
7847 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_ADDITIVE, NULL, options);
7848 LY_CHECK_GOTO(rc, cleanup);
7849 continue;
7850 }
7851
7852 set_fill_set(&set2, &orig_set);
7853 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_ADDITIVE, &set2, options);
7854 LY_CHECK_GOTO(rc, cleanup);
7855
7856 /* eval */
7857 if (options & LYXP_SCNODE_ALL) {
7858 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007859 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007860 set_scnode_clear_ctx(set);
7861 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007862 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007863 LY_CHECK_GOTO(rc, cleanup);
7864 }
7865 }
7866
7867cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007868 lyxp_set_free_content(&orig_set);
7869 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007870 return rc;
7871}
7872
7873/**
7874 * @brief Evaluate RelationalExpr. Logs directly on error.
7875 *
Michal Vaskod3678892020-05-21 10:06:58 +02007876 * [16] RelationalExpr ::= AdditiveExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007877 * | RelationalExpr '<' AdditiveExpr
7878 * | RelationalExpr '>' AdditiveExpr
7879 * | RelationalExpr '<=' AdditiveExpr
7880 * | RelationalExpr '>=' AdditiveExpr
7881 *
7882 * @param[in] exp Parsed XPath expression.
7883 * @param[in] exp_idx Position in the expression @p exp.
7884 * @param[in] repeat How many times this expression is repeated.
7885 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7886 * @param[in] options XPath options.
7887 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7888 */
7889static LY_ERR
7890eval_relational_expr(struct lyxp_expr *exp, uint16_t *exp_idx, uint16_t repeat, struct lyxp_set *set, int options)
7891{
7892 LY_ERR rc;
7893 uint16_t this_op;
7894 struct lyxp_set orig_set, set2;
7895 uint16_t i;
7896
7897 assert(repeat);
7898
7899 set_init(&orig_set, set);
7900 set_init(&set2, set);
7901
7902 set_fill_set(&orig_set, set);
7903
7904 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_RELATIONAL, set, options);
7905 LY_CHECK_GOTO(rc, cleanup);
7906
7907 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
7908 for (i = 0; i < repeat; ++i) {
7909 this_op = *exp_idx;
7910
7911 assert(exp->tokens[*exp_idx] == LYXP_TOKEN_OPERATOR_COMP);
7912 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7913 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7914 ++(*exp_idx);
7915
7916 if (!set) {
7917 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_RELATIONAL, NULL, options);
7918 LY_CHECK_GOTO(rc, cleanup);
7919 continue;
7920 }
7921
7922 set_fill_set(&set2, &orig_set);
7923 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_RELATIONAL, &set2, options);
7924 LY_CHECK_GOTO(rc, cleanup);
7925
7926 /* eval */
7927 if (options & LYXP_SCNODE_ALL) {
7928 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007929 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007930 set_scnode_clear_ctx(set);
7931 } else {
7932 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options);
7933 LY_CHECK_GOTO(rc, cleanup);
7934 }
7935 }
7936
7937cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007938 lyxp_set_free_content(&orig_set);
7939 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007940 return rc;
7941}
7942
7943/**
7944 * @brief Evaluate EqualityExpr. Logs directly on error.
7945 *
Michal Vaskod3678892020-05-21 10:06:58 +02007946 * [15] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007947 * | EqualityExpr '!=' RelationalExpr
7948 *
7949 * @param[in] exp Parsed XPath expression.
7950 * @param[in] exp_idx Position in the expression @p exp.
7951 * @param[in] repeat How many times this expression is repeated.
7952 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7953 * @param[in] options XPath options.
7954 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7955 */
7956static LY_ERR
7957eval_equality_expr(struct lyxp_expr *exp, uint16_t *exp_idx, uint16_t repeat, struct lyxp_set *set, int options)
7958{
7959 LY_ERR rc;
7960 uint16_t this_op;
7961 struct lyxp_set orig_set, set2;
7962 uint16_t i;
7963
7964 assert(repeat);
7965
7966 set_init(&orig_set, set);
7967 set_init(&set2, set);
7968
7969 set_fill_set(&orig_set, set);
7970
7971 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_EQUALITY, set, options);
7972 LY_CHECK_GOTO(rc, cleanup);
7973
7974 /* ('=' / '!=' RelationalExpr)* */
7975 for (i = 0; i < repeat; ++i) {
7976 this_op = *exp_idx;
7977
7978 assert(exp->tokens[*exp_idx] == LYXP_TOKEN_OPERATOR_COMP);
7979 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
7980 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
7981 ++(*exp_idx);
7982
7983 if (!set) {
7984 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_EQUALITY, NULL, options);
7985 LY_CHECK_GOTO(rc, cleanup);
7986 continue;
7987 }
7988
7989 set_fill_set(&set2, &orig_set);
7990 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_EQUALITY, &set2, options);
7991 LY_CHECK_GOTO(rc, cleanup);
7992
7993 /* eval */
7994 if (options & LYXP_SCNODE_ALL) {
7995 warn_operands(set->ctx, set, &set2, 0, exp->expr, exp->tok_pos[this_op - 1]);
7996 warn_equality_value(exp, set, *exp_idx - 1, this_op - 1, *exp_idx - 1);
7997 warn_equality_value(exp, &set2, this_op - 1, this_op - 1, *exp_idx - 1);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007998 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007999 set_scnode_clear_ctx(set);
8000 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008001 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options);
8002 LY_CHECK_GOTO(rc, cleanup);
8003 }
8004 }
8005
8006cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008007 lyxp_set_free_content(&orig_set);
8008 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008009 return rc;
8010}
8011
8012/**
8013 * @brief Evaluate AndExpr. Logs directly on error.
8014 *
Michal Vaskod3678892020-05-21 10:06:58 +02008015 * [14] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008016 *
8017 * @param[in] exp Parsed XPath expression.
8018 * @param[in] exp_idx Position in the expression @p exp.
8019 * @param[in] repeat How many times this expression is repeated.
8020 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8021 * @param[in] options XPath options.
8022 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8023 */
8024static LY_ERR
8025eval_and_expr(struct lyxp_expr *exp, uint16_t *exp_idx, uint16_t repeat, struct lyxp_set *set, int options)
8026{
8027 LY_ERR rc;
8028 struct lyxp_set orig_set, set2;
8029 uint16_t i;
8030
8031 assert(repeat);
8032
8033 set_init(&orig_set, set);
8034 set_init(&set2, set);
8035
8036 set_fill_set(&orig_set, set);
8037
8038 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_AND, set, options);
8039 LY_CHECK_GOTO(rc, cleanup);
8040
8041 /* cast to boolean, we know that will be the final result */
8042 if (set && (options & LYXP_SCNODE_ALL)) {
8043 set_scnode_clear_ctx(set);
8044 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008045 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008046 }
8047
8048 /* ('and' EqualityExpr)* */
8049 for (i = 0; i < repeat; ++i) {
8050 assert(exp->tokens[*exp_idx] == LYXP_TOKEN_OPERATOR_LOG);
8051 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (!set || !set->val.bool ? "skipped" : "parsed"),
8052 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
8053 ++(*exp_idx);
8054
8055 /* lazy evaluation */
8056 if (!set || ((set->type == LYXP_SET_BOOLEAN) && !set->val.bool)) {
8057 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_AND, NULL, options);
8058 LY_CHECK_GOTO(rc, cleanup);
8059 continue;
8060 }
8061
8062 set_fill_set(&set2, &orig_set);
8063 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_AND, &set2, options);
8064 LY_CHECK_GOTO(rc, cleanup);
8065
8066 /* eval - just get boolean value actually */
8067 if (set->type == LYXP_SET_SCNODE_SET) {
8068 set_scnode_clear_ctx(&set2);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008069 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008070 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008071 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008072 set_fill_set(set, &set2);
8073 }
8074 }
8075
8076cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008077 lyxp_set_free_content(&orig_set);
8078 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008079 return rc;
8080}
8081
8082/**
8083 * @brief Evaluate OrExpr. Logs directly on error.
8084 *
Michal Vaskod3678892020-05-21 10:06:58 +02008085 * [13] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008086 *
8087 * @param[in] exp Parsed XPath expression.
8088 * @param[in] exp_idx Position in the expression @p exp.
8089 * @param[in] repeat How many times this expression is repeated.
8090 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8091 * @param[in] options XPath options.
8092 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8093 */
8094static LY_ERR
8095eval_or_expr(struct lyxp_expr *exp, uint16_t *exp_idx, uint16_t repeat, struct lyxp_set *set, int options)
8096{
8097 LY_ERR rc;
8098 struct lyxp_set orig_set, set2;
8099 uint16_t i;
8100
8101 assert(repeat);
8102
8103 set_init(&orig_set, set);
8104 set_init(&set2, set);
8105
8106 set_fill_set(&orig_set, set);
8107
8108 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_OR, set, options);
8109 LY_CHECK_GOTO(rc, cleanup);
8110
8111 /* cast to boolean, we know that will be the final result */
8112 if (set && (options & LYXP_SCNODE_ALL)) {
8113 set_scnode_clear_ctx(set);
8114 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008115 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008116 }
8117
8118 /* ('or' AndExpr)* */
8119 for (i = 0; i < repeat; ++i) {
8120 assert(exp->tokens[*exp_idx] == LYXP_TOKEN_OPERATOR_LOG);
8121 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (!set || set->val.bool ? "skipped" : "parsed"),
8122 print_token(exp->tokens[*exp_idx]), exp->tok_pos[*exp_idx]);
8123 ++(*exp_idx);
8124
8125 /* lazy evaluation */
8126 if (!set || ((set->type == LYXP_SET_BOOLEAN) && set->val.bool)) {
8127 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_OR, NULL, options);
8128 LY_CHECK_GOTO(rc, cleanup);
8129 continue;
8130 }
8131
8132 set_fill_set(&set2, &orig_set);
8133 /* expr_type cound have been LYXP_EXPR_NONE in all these later calls (except for the first one),
8134 * but it does not matter */
8135 rc = eval_expr_select(exp, exp_idx, LYXP_EXPR_OR, &set2, options);
8136 LY_CHECK_GOTO(rc, cleanup);
8137
8138 /* eval - just get boolean value actually */
8139 if (set->type == LYXP_SET_SCNODE_SET) {
8140 set_scnode_clear_ctx(&set2);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008141 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008142 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008143 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008144 set_fill_set(set, &set2);
8145 }
8146 }
8147
8148cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008149 lyxp_set_free_content(&orig_set);
8150 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008151 return rc;
8152}
8153
8154/**
8155 * @brief Decide what expression is at the pointer @p exp_idx and evaluate it accordingly.
8156 *
8157 * @param[in] exp Parsed XPath expression.
8158 * @param[in] exp_idx Position in the expression @p exp.
8159 * @param[in] etype Expression type to evaluate.
8160 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8161 * @param[in] options XPath options.
8162 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8163 */
8164static LY_ERR
8165eval_expr_select(struct lyxp_expr *exp, uint16_t *exp_idx, enum lyxp_expr_type etype, struct lyxp_set *set, int options)
8166{
8167 uint16_t i, count;
8168 enum lyxp_expr_type next_etype;
8169 LY_ERR rc;
8170
8171 /* process operator repeats */
8172 if (!exp->repeat[*exp_idx]) {
8173 next_etype = LYXP_EXPR_NONE;
8174 } else {
8175 /* find etype repeat */
8176 for (i = 0; exp->repeat[*exp_idx][i] > etype; ++i);
8177
8178 /* select one-priority lower because etype expression called us */
8179 if (i) {
8180 next_etype = exp->repeat[*exp_idx][i - 1];
8181 /* count repeats for that expression */
8182 for (count = 0; i && exp->repeat[*exp_idx][i - 1] == next_etype; ++count, --i);
8183 } else {
8184 next_etype = LYXP_EXPR_NONE;
8185 }
8186 }
8187
8188 /* decide what expression are we parsing based on the repeat */
8189 switch (next_etype) {
8190 case LYXP_EXPR_OR:
8191 rc = eval_or_expr(exp, exp_idx, count, set, options);
8192 break;
8193 case LYXP_EXPR_AND:
8194 rc = eval_and_expr(exp, exp_idx, count, set, options);
8195 break;
8196 case LYXP_EXPR_EQUALITY:
8197 rc = eval_equality_expr(exp, exp_idx, count, set, options);
8198 break;
8199 case LYXP_EXPR_RELATIONAL:
8200 rc = eval_relational_expr(exp, exp_idx, count, set, options);
8201 break;
8202 case LYXP_EXPR_ADDITIVE:
8203 rc = eval_additive_expr(exp, exp_idx, count, set, options);
8204 break;
8205 case LYXP_EXPR_MULTIPLICATIVE:
8206 rc = eval_multiplicative_expr(exp, exp_idx, count, set, options);
8207 break;
8208 case LYXP_EXPR_UNARY:
8209 rc = eval_unary_expr(exp, exp_idx, count, set, options);
8210 break;
8211 case LYXP_EXPR_UNION:
8212 rc = eval_union_expr(exp, exp_idx, count, set, options);
8213 break;
8214 case LYXP_EXPR_NONE:
8215 rc = eval_path_expr(exp, exp_idx, set, options);
8216 break;
8217 default:
8218 LOGINT_RET(set->ctx);
8219 }
8220
8221 return rc;
8222}
8223
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008224/**
8225 * @brief Get root type.
8226 *
8227 * @param[in] ctx_node Context node.
8228 * @param[in] ctx_scnode Schema context node.
8229 * @param[in] options XPath options.
8230 * @return Root type.
8231 */
8232static enum lyxp_node_type
8233lyxp_get_root_type(const struct lyd_node *ctx_node, const struct lysc_node *ctx_scnode, int options)
8234{
8235 if (options & LYXP_SCNODE_ALL) {
8236 if (options & LYXP_SCNODE) {
8237 /* general root that can access everything */
8238 return LYXP_NODE_ROOT;
8239 } else if (!ctx_scnode || (ctx_scnode->flags & LYS_CONFIG_W)) {
8240 /* root context node can access only config data (because we said so, it is unspecified) */
8241 return LYXP_NODE_ROOT_CONFIG;
8242 } else {
8243 return LYXP_NODE_ROOT;
8244 }
8245 }
8246
8247 if (!ctx_node || (ctx_node->schema->flags & LYS_CONFIG_W)) {
8248 /* root context node can access only config data (because we said so, it is unspecified) */
8249 return LYXP_NODE_ROOT_CONFIG;
8250 }
8251
8252 return LYXP_NODE_ROOT;
8253}
8254
Michal Vasko03ff5a72019-09-11 13:49:33 +02008255LY_ERR
Michal Vaskoecd62de2019-11-13 12:35:11 +01008256lyxp_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 +01008257 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 +02008258{
Michal Vasko03ff5a72019-09-11 13:49:33 +02008259 uint16_t exp_idx = 0;
8260 LY_ERR rc;
8261
Michal Vaskoecd62de2019-11-13 12:35:11 +01008262 LY_CHECK_ARG_RET(NULL, exp, local_mod, set, LY_EINVAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008263
8264 /* prepare set for evaluation */
8265 exp_idx = 0;
8266 memset(set, 0, sizeof *set);
Michal Vaskod3678892020-05-21 10:06:58 +02008267 set->type = LYXP_SET_NODE_SET;
Michal Vasko9b368d32020-02-14 13:53:31 +01008268 set_insert_node(set, (struct lyd_node *)ctx_node, 0, ctx_node_type, 0);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008269 set->ctx = local_mod->ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008270 set->ctx_node = ctx_node;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008271 set->root_type = lyxp_get_root_type(ctx_node, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008272 set->local_mod = local_mod;
Michal Vaskof03ed032020-03-04 13:31:44 +01008273 set->tree = tree;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008274 set->format = format;
8275
8276 /* evaluate */
8277 rc = eval_expr_select(exp, &exp_idx, 0, set, options);
8278 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02008279 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008280 }
8281
Michal Vasko03ff5a72019-09-11 13:49:33 +02008282 return rc;
8283}
8284
8285#if 0
8286
8287/* full xml printing of set elements, not used currently */
8288
8289void
8290lyxp_set_print_xml(FILE *f, struct lyxp_set *set)
8291{
8292 uint32_t i;
8293 char *str_num;
8294 struct lyout out;
8295
8296 memset(&out, 0, sizeof out);
8297
8298 out.type = LYOUT_STREAM;
8299 out.method.f = f;
8300
8301 switch (set->type) {
8302 case LYXP_SET_EMPTY:
8303 ly_print(&out, "Empty XPath set\n\n");
8304 break;
8305 case LYXP_SET_BOOLEAN:
8306 ly_print(&out, "Boolean XPath set:\n");
8307 ly_print(&out, "%s\n\n", set->value.bool ? "true" : "false");
8308 break;
8309 case LYXP_SET_STRING:
8310 ly_print(&out, "String XPath set:\n");
8311 ly_print(&out, "\"%s\"\n\n", set->value.str);
8312 break;
8313 case LYXP_SET_NUMBER:
8314 ly_print(&out, "Number XPath set:\n");
8315
8316 if (isnan(set->value.num)) {
8317 str_num = strdup("NaN");
8318 } else if ((set->value.num == 0) || (set->value.num == -0.0f)) {
8319 str_num = strdup("0");
8320 } else if (isinf(set->value.num) && !signbit(set->value.num)) {
8321 str_num = strdup("Infinity");
8322 } else if (isinf(set->value.num) && signbit(set->value.num)) {
8323 str_num = strdup("-Infinity");
8324 } else if ((long long)set->value.num == set->value.num) {
8325 if (asprintf(&str_num, "%lld", (long long)set->value.num) == -1) {
8326 str_num = NULL;
8327 }
8328 } else {
8329 if (asprintf(&str_num, "%03.1Lf", set->value.num) == -1) {
8330 str_num = NULL;
8331 }
8332 }
8333 if (!str_num) {
8334 LOGMEM;
8335 return;
8336 }
8337 ly_print(&out, "%s\n\n", str_num);
8338 free(str_num);
8339 break;
8340 case LYXP_SET_NODE_SET:
8341 ly_print(&out, "Node XPath set:\n");
8342
8343 for (i = 0; i < set->used; ++i) {
8344 ly_print(&out, "%d. ", i + 1);
8345 switch (set->node_type[i]) {
8346 case LYXP_NODE_ROOT_ALL:
8347 ly_print(&out, "ROOT all\n\n");
8348 break;
8349 case LYXP_NODE_ROOT_CONFIG:
8350 ly_print(&out, "ROOT config\n\n");
8351 break;
8352 case LYXP_NODE_ROOT_STATE:
8353 ly_print(&out, "ROOT state\n\n");
8354 break;
8355 case LYXP_NODE_ROOT_NOTIF:
8356 ly_print(&out, "ROOT notification \"%s\"\n\n", set->value.nodes[i]->schema->name);
8357 break;
8358 case LYXP_NODE_ROOT_RPC:
8359 ly_print(&out, "ROOT rpc \"%s\"\n\n", set->value.nodes[i]->schema->name);
8360 break;
8361 case LYXP_NODE_ROOT_OUTPUT:
8362 ly_print(&out, "ROOT output \"%s\"\n\n", set->value.nodes[i]->schema->name);
8363 break;
8364 case LYXP_NODE_ELEM:
8365 ly_print(&out, "ELEM \"%s\"\n", set->value.nodes[i]->schema->name);
8366 xml_print_node(&out, 1, set->value.nodes[i], 1, LYP_FORMAT);
8367 ly_print(&out, "\n");
8368 break;
8369 case LYXP_NODE_TEXT:
8370 ly_print(&out, "TEXT \"%s\"\n\n", ((struct lyd_node_leaf_list *)set->value.nodes[i])->value_str);
8371 break;
8372 case LYXP_NODE_ATTR:
8373 ly_print(&out, "ATTR \"%s\" = \"%s\"\n\n", set->value.attrs[i]->name, set->value.attrs[i]->value);
8374 break;
8375 }
8376 }
8377 break;
8378 }
8379}
8380
8381#endif
8382
8383LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008384lyxp_set_cast(struct lyxp_set *set, enum lyxp_set_type target)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008385{
8386 long double num;
8387 char *str;
8388 LY_ERR rc;
8389
8390 if (!set || (set->type == target)) {
8391 return LY_SUCCESS;
8392 }
8393
8394 /* it's not possible to convert anything into a node set */
Michal Vaskod3678892020-05-21 10:06:58 +02008395 assert(target != LYXP_SET_NODE_SET);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008396
8397 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vaskod3678892020-05-21 10:06:58 +02008398 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008399 return LY_EINVAL;
8400 }
8401
8402 /* to STRING */
Michal Vaskod3678892020-05-21 10:06:58 +02008403 if ((target == LYXP_SET_STRING) || ((target == LYXP_SET_NUMBER) && (set->type == LYXP_SET_NODE_SET))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008404 switch (set->type) {
8405 case LYXP_SET_NUMBER:
8406 if (isnan(set->val.num)) {
8407 set->val.str = strdup("NaN");
8408 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8409 } else if ((set->val.num == 0) || (set->val.num == -0.0f)) {
8410 set->val.str = strdup("0");
8411 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8412 } else if (isinf(set->val.num) && !signbit(set->val.num)) {
8413 set->val.str = strdup("Infinity");
8414 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8415 } else if (isinf(set->val.num) && signbit(set->val.num)) {
8416 set->val.str = strdup("-Infinity");
8417 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8418 } else if ((long long)set->val.num == set->val.num) {
8419 if (asprintf(&str, "%lld", (long long)set->val.num) == -1) {
8420 LOGMEM_RET(set->ctx);
8421 }
8422 set->val.str = str;
8423 } else {
8424 if (asprintf(&str, "%03.1Lf", set->val.num) == -1) {
8425 LOGMEM_RET(set->ctx);
8426 }
8427 set->val.str = str;
8428 }
8429 break;
8430 case LYXP_SET_BOOLEAN:
8431 if (set->val.bool) {
8432 set->val.str = strdup("true");
8433 } else {
8434 set->val.str = strdup("false");
8435 }
8436 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM);
8437 break;
8438 case LYXP_SET_NODE_SET:
8439 assert(set->used);
8440
8441 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008442 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008443
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008444 rc = cast_node_set_to_string(set, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008445 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02008446 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008447 set->val.str = str;
8448 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008449 default:
8450 LOGINT_RET(set->ctx);
8451 }
8452 set->type = LYXP_SET_STRING;
8453 }
8454
8455 /* to NUMBER */
8456 if (target == LYXP_SET_NUMBER) {
8457 switch (set->type) {
8458 case LYXP_SET_STRING:
8459 num = cast_string_to_number(set->val.str);
Michal Vaskod3678892020-05-21 10:06:58 +02008460 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008461 set->val.num = num;
8462 break;
8463 case LYXP_SET_BOOLEAN:
8464 if (set->val.bool) {
8465 set->val.num = 1;
8466 } else {
8467 set->val.num = 0;
8468 }
8469 break;
8470 default:
8471 LOGINT_RET(set->ctx);
8472 }
8473 set->type = LYXP_SET_NUMBER;
8474 }
8475
8476 /* to BOOLEAN */
8477 if (target == LYXP_SET_BOOLEAN) {
8478 switch (set->type) {
8479 case LYXP_SET_NUMBER:
8480 if ((set->val.num == 0) || (set->val.num == -0.0f) || isnan(set->val.num)) {
8481 set->val.bool = 0;
8482 } else {
8483 set->val.bool = 1;
8484 }
8485 break;
8486 case LYXP_SET_STRING:
8487 if (set->val.str[0]) {
Michal Vaskod3678892020-05-21 10:06:58 +02008488 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008489 set->val.bool = 1;
8490 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02008491 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008492 set->val.bool = 0;
8493 }
8494 break;
8495 case LYXP_SET_NODE_SET:
Michal Vaskod3678892020-05-21 10:06:58 +02008496 if (set->used) {
8497 lyxp_set_free_content(set);
8498 set->val.bool = 1;
8499 } else {
8500 lyxp_set_free_content(set);
8501 set->val.bool = 0;
8502 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008503 break;
8504 default:
8505 LOGINT_RET(set->ctx);
8506 }
8507 set->type = LYXP_SET_BOOLEAN;
8508 }
8509
Michal Vasko03ff5a72019-09-11 13:49:33 +02008510 return LY_SUCCESS;
8511}
8512
8513LY_ERR
8514lyxp_atomize(struct lyxp_expr *exp, LYD_FORMAT format, const struct lys_module *local_mod, const struct lysc_node *ctx_scnode,
8515 enum lyxp_node_type ctx_scnode_type, struct lyxp_set *set, int options)
8516{
8517 struct ly_ctx *ctx;
8518 uint16_t exp_idx = 0;
8519
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008520 LY_CHECK_ARG_RET(NULL, exp, local_mod, set, LY_EINVAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008521
8522 ctx = local_mod->ctx;
8523
8524 /* prepare set for evaluation */
8525 exp_idx = 0;
8526 memset(set, 0, sizeof *set);
8527 set->type = LYXP_SET_SCNODE_SET;
Michal Vaskoecd62de2019-11-13 12:35:11 +01008528 lyxp_set_scnode_insert_node(set, ctx_scnode, ctx_scnode_type);
Michal Vasko5c4e5892019-11-14 12:31:38 +01008529 set->val.scnodes[0].in_ctx = -2;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008530 set->ctx = ctx;
8531 set->ctx_scnode = ctx_scnode;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008532 set->root_type = lyxp_get_root_type(NULL, ctx_scnode, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008533 set->local_mod = local_mod;
8534 set->format = format;
8535
8536 /* evaluate */
8537 return eval_expr_select(exp, &exp_idx, 0, set, options);
8538}