blob: 2b2dcbc2d1be179a1fec0ab5c514344e2c4aa298 [file] [log] [blame]
Radek Krejcib1646a92018-11-02 16:08:26 +01001/**
2 * @file xpath.c
3 * @author Michal Vasko <mvasko@cesnet.cz>
4 * @brief YANG XPath evaluation functions
5 *
Michal Vasko61ac2f62020-05-25 12:39:51 +02006 * Copyright (c) 2015 - 2020 CESNET, z.s.p.o.
Radek Krejcib1646a92018-11-02 16:08:26 +01007 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
Michal Vasko03ff5a72019-09-11 13:49:33 +020014#define _GNU_SOURCE
15
16/* needed by libmath functions isfinite(), isinf(), isnan(), signbit(), ... */
17#define _ISOC99_SOURCE
Radek Krejcib1646a92018-11-02 16:08:26 +010018
Radek Krejci535ea9f2020-05-29 16:01:05 +020019#include "xpath.h"
Radek Krejcib1646a92018-11-02 16:08:26 +010020
Radek Krejci535ea9f2020-05-29 16:01:05 +020021#include <assert.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010022#include <ctype.h>
Radek Krejci535ea9f2020-05-29 16:01:05 +020023#include <errno.h>
24#include <limits.h>
25#include <math.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020026#include <stdint.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010027#include <stdio.h>
28#include <stdlib.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010029#include <string.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010030
Radek Krejci535ea9f2020-05-29 16:01:05 +020031#include "common.h"
Michal Vasko5aa44c02020-06-29 11:47:02 +020032#include "compat.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020033#include "context.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020034#include "dict.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020035#include "hash_table.h"
Michal Vasko004d3152020-06-11 19:59:22 +020036#include "path.h"
Michal Vasko03ff5a72019-09-11 13:49:33 +020037#include "plugins_types.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020038#include "printer.h"
39#include "printer_data.h"
40#include "tree.h"
41#include "tree_data_internal.h"
42#include "tree_schema_internal.h"
43#include "xml.h"
Michal Vasko03ff5a72019-09-11 13:49:33 +020044
Michal Vasko004d3152020-06-11 19:59:22 +020045static LY_ERR reparse_or_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx);
46static LY_ERR eval_expr_select(struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_expr_type etype, struct lyxp_set *set, int options);
Michal Vasko03ff5a72019-09-11 13:49:33 +020047
48/**
49 * @brief Print the type of an XPath \p set.
50 *
51 * @param[in] set Set to use.
52 * @return Set type string.
53 */
54static const char *
55print_set_type(struct lyxp_set *set)
56{
57 switch (set->type) {
Michal Vasko03ff5a72019-09-11 13:49:33 +020058 case LYXP_SET_NODE_SET:
59 return "node set";
60 case LYXP_SET_SCNODE_SET:
61 return "schema node set";
62 case LYXP_SET_BOOLEAN:
63 return "boolean";
64 case LYXP_SET_NUMBER:
65 return "number";
66 case LYXP_SET_STRING:
67 return "string";
68 }
69
70 return NULL;
71}
72
Michal Vasko24cddf82020-06-01 08:17:01 +020073const char *
74lyxp_print_token(enum lyxp_token tok)
Michal Vasko03ff5a72019-09-11 13:49:33 +020075{
76 switch (tok) {
77 case LYXP_TOKEN_PAR1:
78 return "(";
79 case LYXP_TOKEN_PAR2:
80 return ")";
81 case LYXP_TOKEN_BRACK1:
82 return "[";
83 case LYXP_TOKEN_BRACK2:
84 return "]";
85 case LYXP_TOKEN_DOT:
86 return ".";
87 case LYXP_TOKEN_DDOT:
88 return "..";
89 case LYXP_TOKEN_AT:
90 return "@";
91 case LYXP_TOKEN_COMMA:
92 return ",";
93 case LYXP_TOKEN_NAMETEST:
94 return "NameTest";
95 case LYXP_TOKEN_NODETYPE:
96 return "NodeType";
97 case LYXP_TOKEN_FUNCNAME:
98 return "FunctionName";
Michal Vasko3e48bf32020-06-01 08:39:07 +020099 case LYXP_TOKEN_OPER_LOG:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200100 return "Operator(Logic)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200101 case LYXP_TOKEN_OPER_EQUAL:
102 return "Operator(Equal)";
103 case LYXP_TOKEN_OPER_NEQUAL:
104 return "Operator(Non-equal)";
105 case LYXP_TOKEN_OPER_COMP:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200106 return "Operator(Comparison)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200107 case LYXP_TOKEN_OPER_MATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200108 return "Operator(Math)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200109 case LYXP_TOKEN_OPER_UNI:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200110 return "Operator(Union)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200111 case LYXP_TOKEN_OPER_PATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200112 return "Operator(Path)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200113 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko14676352020-05-29 11:35:55 +0200114 return "Operator(Recursive Path)";
Michal Vasko03ff5a72019-09-11 13:49:33 +0200115 case LYXP_TOKEN_LITERAL:
116 return "Literal";
117 case LYXP_TOKEN_NUMBER:
118 return "Number";
119 default:
120 LOGINT(NULL);
121 return "";
122 }
123}
124
125/**
126 * @brief Print the whole expression \p exp to debug output.
127 *
128 * @param[in] exp Expression to use.
129 */
130static void
131print_expr_struct_debug(struct lyxp_expr *exp)
132{
133 uint16_t i, j;
134 char tmp[128];
135
136 if (!exp || (ly_log_level < LY_LLDBG)) {
137 return;
138 }
139
140 LOGDBG(LY_LDGXPATH, "expression \"%s\":", exp->expr);
141 for (i = 0; i < exp->used; ++i) {
Michal Vasko24cddf82020-06-01 08:17:01 +0200142 sprintf(tmp, "\ttoken %s, in expression \"%.*s\"", lyxp_print_token(exp->tokens[i]), exp->tok_len[i],
Michal Vasko03ff5a72019-09-11 13:49:33 +0200143 &exp->expr[exp->tok_pos[i]]);
144 if (exp->repeat[i]) {
145 sprintf(tmp + strlen(tmp), " (repeat %d", exp->repeat[i][0]);
146 for (j = 1; exp->repeat[i][j]; ++j) {
147 sprintf(tmp + strlen(tmp), ", %d", exp->repeat[i][j]);
148 }
149 strcat(tmp, ")");
150 }
151 LOGDBG(LY_LDGXPATH, tmp);
152 }
153}
154
155#ifndef NDEBUG
156
157/**
158 * @brief Print XPath set content to debug output.
159 *
160 * @param[in] set Set to print.
161 */
162static void
163print_set_debug(struct lyxp_set *set)
164{
165 uint32_t i;
166 char *str;
167 int dynamic;
168 struct lyxp_set_node *item;
169 struct lyxp_set_scnode *sitem;
170
171 if (ly_log_level < LY_LLDBG) {
172 return;
173 }
174
175 switch (set->type) {
176 case LYXP_SET_NODE_SET:
177 LOGDBG(LY_LDGXPATH, "set NODE SET:");
178 for (i = 0; i < set->used; ++i) {
179 item = &set->val.nodes[i];
180
181 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +0100182 case LYXP_NODE_NONE:
183 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): NONE", i + 1, item->pos);
184 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200185 case LYXP_NODE_ROOT:
186 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT", i + 1, item->pos);
187 break;
188 case LYXP_NODE_ROOT_CONFIG:
189 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT CONFIG", i + 1, item->pos);
190 break;
191 case LYXP_NODE_ELEM:
192 if ((item->node->schema->nodetype == LYS_LIST)
193 && (((struct lyd_node_inner *)item->node)->child->schema->nodetype == LYS_LEAF)) {
194 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (1st child val: %s)", i + 1, item->pos,
195 item->node->schema->name,
Michal Vasko5bfd4be2020-06-23 13:26:19 +0200196 (str = (char *)lyd_value2str((struct lyd_node_term *)lyd_node_children(item->node, 0), &dynamic)));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200197 if (dynamic) {
198 free(str);
199 }
200 } else if (((struct lyd_node_inner *)item->node)->schema->nodetype == LYS_LEAFLIST) {
201 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (val: %s)", i + 1, item->pos,
202 item->node->schema->name,
203 (str = (char *)lyd_value2str((struct lyd_node_term *)item->node, &dynamic)));
204 if (dynamic) {
205 free(str);
206 }
207 } else {
208 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s", i + 1, item->pos, item->node->schema->name);
209 }
210 break;
211 case LYXP_NODE_TEXT:
212 if (item->node->schema->nodetype & LYS_ANYDATA) {
213 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): TEXT <%s>", i + 1, item->pos,
214 item->node->schema->nodetype == LYS_ANYXML ? "anyxml" : "anydata");
215 } else {
216 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): TEXT %s", i + 1, item->pos,
217 (str = (char *)lyd_value2str((struct lyd_node_term *)item->node, &dynamic)));
218 if (dynamic) {
219 free(str);
220 }
221 }
222 break;
Michal Vasko9f96a052020-03-10 09:41:45 +0100223 case LYXP_NODE_META:
224 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): META %s = %s", i + 1, item->pos, set->val.meta[i].meta->name,
225 set->val.meta[i].meta->value);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200226 break;
227 }
228 }
229 break;
230
231 case LYXP_SET_SCNODE_SET:
232 LOGDBG(LY_LDGXPATH, "set SCNODE SET:");
233 for (i = 0; i < set->used; ++i) {
234 sitem = &set->val.scnodes[i];
235
236 switch (sitem->type) {
237 case LYXP_NODE_ROOT:
238 LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT", i + 1, sitem->in_ctx);
239 break;
240 case LYXP_NODE_ROOT_CONFIG:
241 LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT CONFIG", i + 1, sitem->in_ctx);
242 break;
243 case LYXP_NODE_ELEM:
244 LOGDBG(LY_LDGXPATH, "\t%d (%u): ELEM %s", i + 1, sitem->in_ctx, sitem->scnode->name);
245 break;
246 default:
247 LOGINT(NULL);
248 break;
249 }
250 }
251 break;
252
Michal Vasko03ff5a72019-09-11 13:49:33 +0200253 case LYXP_SET_BOOLEAN:
254 LOGDBG(LY_LDGXPATH, "set BOOLEAN");
Michal Vasko004d3152020-06-11 19:59:22 +0200255 LOGDBG(LY_LDGXPATH, "\t%s", (set->val.bln ? "true" : "false"));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200256 break;
257
258 case LYXP_SET_STRING:
259 LOGDBG(LY_LDGXPATH, "set STRING");
260 LOGDBG(LY_LDGXPATH, "\t%s", set->val.str);
261 break;
262
263 case LYXP_SET_NUMBER:
264 LOGDBG(LY_LDGXPATH, "set NUMBER");
265
266 if (isnan(set->val.num)) {
267 str = strdup("NaN");
268 } else if ((set->val.num == 0) || (set->val.num == -0.0f)) {
269 str = strdup("0");
270 } else if (isinf(set->val.num) && !signbit(set->val.num)) {
271 str = strdup("Infinity");
272 } else if (isinf(set->val.num) && signbit(set->val.num)) {
273 str = strdup("-Infinity");
274 } else if ((long long)set->val.num == set->val.num) {
275 if (asprintf(&str, "%lld", (long long)set->val.num) == -1) {
276 str = NULL;
277 }
278 } else {
279 if (asprintf(&str, "%03.1Lf", set->val.num) == -1) {
280 str = NULL;
281 }
282 }
283 LY_CHECK_ERR_RET(!str, LOGMEM(NULL), );
284
285 LOGDBG(LY_LDGXPATH, "\t%s", str);
286 free(str);
287 }
288}
289
290#endif
291
292/**
293 * @brief Realloc the string \p str.
294 *
295 * @param[in] ctx libyang context for logging.
296 * @param[in] needed How much free space is required.
297 * @param[in,out] str Pointer to the string to use.
298 * @param[in,out] used Used bytes in \p str.
299 * @param[in,out] size Allocated bytes in \p str.
300 * @return LY_ERR
301 */
302static LY_ERR
Michal Vasko52927e22020-03-16 17:26:14 +0100303cast_string_realloc(const struct ly_ctx *ctx, uint16_t needed, char **str, uint16_t *used, uint16_t *size)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200304{
305 if (*size - *used < needed) {
306 do {
307 if ((UINT16_MAX - *size) < LYXP_STRING_CAST_SIZE_STEP) {
308 LOGERR(ctx, LY_EINVAL, "XPath string length limit (%u) reached.", UINT16_MAX);
309 return LY_EINVAL;
310 }
311 *size += LYXP_STRING_CAST_SIZE_STEP;
312 } while (*size - *used < needed);
313 *str = ly_realloc(*str, *size * sizeof(char));
314 LY_CHECK_ERR_RET(!(*str), LOGMEM(ctx), LY_EMEM);
315 }
316
317 return LY_SUCCESS;
318}
319
320/**
321 * @brief Cast nodes recursively to one string @p str.
322 *
323 * @param[in] node Node to cast.
324 * @param[in] fake_cont Whether to put the data into a "fake" container.
325 * @param[in] root_type Type of the XPath root.
326 * @param[in] indent Current indent.
327 * @param[in,out] str Resulting string.
328 * @param[in,out] used Used bytes in @p str.
329 * @param[in,out] size Allocated bytes in @p str.
330 * @return LY_ERR
331 */
332static LY_ERR
333cast_string_recursive(const struct lyd_node *node, int fake_cont, enum lyxp_node_type root_type, uint16_t indent, char **str,
334 uint16_t *used, uint16_t *size)
335{
336 char *buf, *line, *ptr;
337 const char *value_str;
338 int dynamic;
339 const struct lyd_node *child;
340 struct lyd_node_any *any;
341 LY_ERR rc;
342
343 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) {
344 return LY_SUCCESS;
345 }
346
347 if (fake_cont) {
348 rc = cast_string_realloc(LYD_NODE_CTX(node), 1, str, used, size);
349 LY_CHECK_RET(rc);
350 strcpy(*str + (*used - 1), "\n");
351 ++(*used);
352
353 ++indent;
354 }
355
356 switch (node->schema->nodetype) {
357 case LYS_CONTAINER:
358 case LYS_LIST:
359 case LYS_RPC:
360 case LYS_NOTIF:
361 rc = cast_string_realloc(LYD_NODE_CTX(node), 1, str, used, size);
362 LY_CHECK_RET(rc);
363 strcpy(*str + (*used - 1), "\n");
364 ++(*used);
365
Michal Vasko5bfd4be2020-06-23 13:26:19 +0200366 for (child = lyd_node_children(node, 0); child; child = child->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200367 rc = cast_string_recursive(child, 0, root_type, indent + 1, str, used, size);
368 LY_CHECK_RET(rc);
369 }
370
371 break;
372
373 case LYS_LEAF:
374 case LYS_LEAFLIST:
375 value_str = lyd_value2str(((struct lyd_node_term *)node), &dynamic);
376
377 /* print indent */
378 rc = cast_string_realloc(LYD_NODE_CTX(node), indent * 2 + strlen(value_str) + 1, str, used, size);
379 if (rc != LY_SUCCESS) {
380 if (dynamic) {
381 free((char *)value_str);
382 }
383 return rc;
384 }
385 memset(*str + (*used - 1), ' ', indent * 2);
386 *used += indent * 2;
387
388 /* print value */
389 if (*used == 1) {
390 sprintf(*str + (*used - 1), "%s", value_str);
391 *used += strlen(value_str);
392 } else {
393 sprintf(*str + (*used - 1), "%s\n", value_str);
394 *used += strlen(value_str) + 1;
395 }
396 if (dynamic) {
397 free((char *)value_str);
398 }
399
400 break;
401
402 case LYS_ANYXML:
403 case LYS_ANYDATA:
404 any = (struct lyd_node_any *)node;
405 if (!(void *)any->value.tree) {
406 /* no content */
407 buf = strdup("");
408 LY_CHECK_ERR_RET(!buf, LOGMEM(LYD_NODE_CTX(node)), LY_EMEM);
409 } else {
Radek Krejci241f6b52020-05-21 18:13:49 +0200410 struct ly_out *out;
Radek Krejcia5bba312020-01-09 15:41:20 +0100411
Michal Vasko03ff5a72019-09-11 13:49:33 +0200412 switch (any->value_type) {
413 case LYD_ANYDATA_STRING:
414 case LYD_ANYDATA_XML:
415 case LYD_ANYDATA_JSON:
416 buf = strdup(any->value.json);
417 LY_CHECK_ERR_RET(!buf, LOGMEM(LYD_NODE_CTX(node)), LY_EMEM);
418 break;
419 case LYD_ANYDATA_DATATREE:
Radek Krejci84ce7b12020-06-11 17:28:25 +0200420 LY_CHECK_RET(ly_out_new_memory(&buf, 0, &out));
Radek Krejcia5bba312020-01-09 15:41:20 +0100421 rc = lyd_print(out, any->value.tree, LYD_XML, LYDP_WITHSIBLINGS);
Radek Krejci241f6b52020-05-21 18:13:49 +0200422 ly_out_free(out, NULL, 0);
Radek Krejcia5bba312020-01-09 15:41:20 +0100423 LY_CHECK_RET(rc < 0, -rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200424 break;
425 /* TODO case LYD_ANYDATA_LYB:
426 LOGERR(LYD_NODE_CTX(node), LY_EINVAL, "Cannot convert LYB anydata into string.");
427 return -1;*/
428 }
429 }
430
431 line = strtok_r(buf, "\n", &ptr);
432 do {
433 rc = cast_string_realloc(LYD_NODE_CTX(node), indent * 2 + strlen(line) + 1, str, used, size);
434 if (rc != LY_SUCCESS) {
435 free(buf);
436 return rc;
437 }
438 memset(*str + (*used - 1), ' ', indent * 2);
439 *used += indent * 2;
440
441 strcpy(*str + (*used - 1), line);
442 *used += strlen(line);
443
444 strcpy(*str + (*used - 1), "\n");
445 *used += 1;
446 } while ((line = strtok_r(NULL, "\n", &ptr)));
447
448 free(buf);
449 break;
450
451 default:
452 LOGINT_RET(LYD_NODE_CTX(node));
453 }
454
455 if (fake_cont) {
456 rc = cast_string_realloc(LYD_NODE_CTX(node), 1, str, used, size);
457 LY_CHECK_RET(rc);
458 strcpy(*str + (*used - 1), "\n");
459 ++(*used);
460
461 --indent;
462 }
463
464 return LY_SUCCESS;
465}
466
467/**
468 * @brief Cast an element into a string.
469 *
470 * @param[in] node Node to cast.
471 * @param[in] fake_cont Whether to put the data into a "fake" container.
472 * @param[in] root_type Type of the XPath root.
473 * @param[out] str Element cast to dynamically-allocated string.
474 * @return LY_ERR
475 */
476static LY_ERR
477cast_string_elem(struct lyd_node *node, int fake_cont, enum lyxp_node_type root_type, char **str)
478{
479 uint16_t used, size;
480 LY_ERR rc;
481
482 *str = malloc(LYXP_STRING_CAST_SIZE_START * sizeof(char));
483 LY_CHECK_ERR_RET(!*str, LOGMEM(LYD_NODE_CTX(node)), LY_EMEM);
484 (*str)[0] = '\0';
485 used = 1;
486 size = LYXP_STRING_CAST_SIZE_START;
487
488 rc = cast_string_recursive(node, fake_cont, root_type, 0, str, &used, &size);
489 if (rc != LY_SUCCESS) {
490 free(*str);
491 return rc;
492 }
493
494 if (size > used) {
495 *str = ly_realloc(*str, used * sizeof(char));
496 LY_CHECK_ERR_RET(!*str, LOGMEM(LYD_NODE_CTX(node)), LY_EMEM);
497 }
498 return LY_SUCCESS;
499}
500
501/**
502 * @brief Cast a LYXP_SET_NODE_SET set into a string.
503 * Context position aware.
504 *
505 * @param[in] set Set to cast.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200506 * @param[out] str Cast dynamically-allocated string.
507 * @return LY_ERR
508 */
509static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100510cast_node_set_to_string(struct lyxp_set *set, char **str)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200511{
Michal Vasko03ff5a72019-09-11 13:49:33 +0200512 int dynamic;
513
Michal Vasko03ff5a72019-09-11 13:49:33 +0200514 switch (set->val.nodes[0].type) {
Michal Vasko2caefc12019-11-14 16:07:56 +0100515 case LYXP_NODE_NONE:
516 /* invalid */
517 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200518 case LYXP_NODE_ROOT:
519 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100520 return cast_string_elem(set->val.nodes[0].node, 1, set->root_type, str);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200521 case LYXP_NODE_ELEM:
522 case LYXP_NODE_TEXT:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100523 return cast_string_elem(set->val.nodes[0].node, 0, set->root_type, str);
Michal Vasko9f96a052020-03-10 09:41:45 +0100524 case LYXP_NODE_META:
525 *str = (char *)lyd_meta2str(set->val.meta[0].meta, &dynamic);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200526 if (!dynamic) {
527 *str = strdup(*str);
528 if (!*str) {
529 LOGMEM_RET(set->ctx);
530 }
531 }
532 return LY_SUCCESS;
533 }
534
535 LOGINT_RET(set->ctx);
536}
537
538/**
539 * @brief Cast a string into an XPath number.
540 *
541 * @param[in] str String to use.
542 * @return Cast number.
543 */
544static long double
545cast_string_to_number(const char *str)
546{
547 long double num;
548 char *ptr;
549
550 errno = 0;
551 num = strtold(str, &ptr);
552 if (errno || *ptr) {
553 num = NAN;
554 }
555 return num;
556}
557
558/**
559 * @brief Callback for checking value equality.
560 *
561 * @param[in] val1_p First value.
562 * @param[in] val2_p Second value.
563 * @param[in] mod Whether hash table is being modified.
564 * @param[in] cb_data Callback data.
565 * @return 0 if not equal, non-zero if equal.
566 */
567static int
568set_values_equal_cb(void *val1_p, void *val2_p, int UNUSED(mod), void *UNUSED(cb_data))
569{
570 struct lyxp_set_hash_node *val1, *val2;
571
572 val1 = (struct lyxp_set_hash_node *)val1_p;
573 val2 = (struct lyxp_set_hash_node *)val2_p;
574
575 if ((val1->node == val2->node) && (val1->type == val2->type)) {
576 return 1;
577 }
578
579 return 0;
580}
581
582/**
583 * @brief Insert node and its hash into set.
584 *
585 * @param[in] set et to insert to.
586 * @param[in] node Node with hash.
587 * @param[in] type Node type.
588 */
589static void
590set_insert_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type)
591{
592 int r;
593 uint32_t i, hash;
594 struct lyxp_set_hash_node hnode;
595
596 if (!set->ht && (set->used >= LYD_HT_MIN_ITEMS)) {
597 /* create hash table and add all the nodes */
598 set->ht = lyht_new(1, sizeof(struct lyxp_set_hash_node), set_values_equal_cb, NULL, 1);
599 for (i = 0; i < set->used; ++i) {
600 hnode.node = set->val.nodes[i].node;
601 hnode.type = set->val.nodes[i].type;
602
603 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
604 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
605 hash = dict_hash_multi(hash, NULL, 0);
606
607 r = lyht_insert(set->ht, &hnode, hash, NULL);
608 assert(!r);
609 (void)r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200610
Michal Vasko9d6befd2019-12-11 14:56:56 +0100611 if (hnode.node == node) {
612 /* it was just added, do not add it twice */
613 node = NULL;
614 }
615 }
616 }
617
618 if (set->ht && node) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200619 /* add the new node into hash table */
620 hnode.node = node;
621 hnode.type = type;
622
623 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
624 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
625 hash = dict_hash_multi(hash, NULL, 0);
626
627 r = lyht_insert(set->ht, &hnode, hash, NULL);
628 assert(!r);
629 (void)r;
630 }
631}
632
633/**
634 * @brief Remove node and its hash from set.
635 *
636 * @param[in] set Set to remove from.
637 * @param[in] node Node to remove.
638 * @param[in] type Node type.
639 */
640static void
641set_remove_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type)
642{
643 int r;
644 struct lyxp_set_hash_node hnode;
645 uint32_t hash;
646
647 if (set->ht) {
648 hnode.node = node;
649 hnode.type = type;
650
651 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
652 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
653 hash = dict_hash_multi(hash, NULL, 0);
654
655 r = lyht_remove(set->ht, &hnode, hash);
656 assert(!r);
657 (void)r;
658
659 if (!set->ht->used) {
660 lyht_free(set->ht);
661 set->ht = NULL;
662 }
663 }
664}
665
666/**
667 * @brief Check whether node is in set based on its hash.
668 *
669 * @param[in] set Set to search in.
670 * @param[in] node Node to search for.
671 * @param[in] type Node type.
672 * @param[in] skip_idx Index in @p set to skip.
673 * @return LY_ERR
674 */
675static LY_ERR
676set_dup_node_hash_check(const struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type, int skip_idx)
677{
678 struct lyxp_set_hash_node hnode, *match_p;
679 uint32_t hash;
680
681 hnode.node = node;
682 hnode.type = type;
683
684 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
685 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
686 hash = dict_hash_multi(hash, NULL, 0);
687
688 if (!lyht_find(set->ht, &hnode, hash, (void **)&match_p)) {
689 if ((skip_idx > -1) && (set->val.nodes[skip_idx].node == match_p->node) && (set->val.nodes[skip_idx].type == match_p->type)) {
690 /* we found it on the index that should be skipped, find another */
691 hnode = *match_p;
692 if (lyht_find_next(set->ht, &hnode, hash, (void **)&match_p)) {
693 /* none other found */
694 return LY_SUCCESS;
695 }
696 }
697
698 return LY_EEXIST;
699 }
700
701 /* not found */
702 return LY_SUCCESS;
703}
704
Michal Vaskod3678892020-05-21 10:06:58 +0200705void
706lyxp_set_free_content(struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200707{
708 if (!set) {
709 return;
710 }
711
712 if (set->type == LYXP_SET_NODE_SET) {
713 free(set->val.nodes);
714 lyht_free(set->ht);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200715 } else if (set->type == LYXP_SET_SCNODE_SET) {
716 free(set->val.scnodes);
Michal Vaskod3678892020-05-21 10:06:58 +0200717 lyht_free(set->ht);
718 } else {
719 if (set->type == LYXP_SET_STRING) {
720 free(set->val.str);
721 }
722 set->type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200723 }
Michal Vaskod3678892020-05-21 10:06:58 +0200724
725 set->val.nodes = NULL;
726 set->used = 0;
727 set->size = 0;
728 set->ht = NULL;
729 set->ctx_pos = 0;
730 set->ctx_pos = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200731}
732
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100733/**
734 * @brief Free dynamically-allocated set.
735 *
736 * @param[in] set Set to free.
737 */
738static void
Michal Vasko03ff5a72019-09-11 13:49:33 +0200739lyxp_set_free(struct lyxp_set *set)
740{
741 if (!set) {
742 return;
743 }
744
Michal Vaskod3678892020-05-21 10:06:58 +0200745 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200746 free(set);
747}
748
749/**
750 * @brief Initialize set context.
751 *
752 * @param[in] new Set to initialize.
753 * @param[in] set Arbitrary initialized set.
754 */
755static void
756set_init(struct lyxp_set *new, struct lyxp_set *set)
757{
758 memset(new, 0, sizeof *new);
Michal Vasko02a77382019-09-12 11:47:35 +0200759 if (set) {
760 new->ctx = set->ctx;
761 new->ctx_node = set->ctx_node;
Michal Vasko588112f2019-12-10 14:51:53 +0100762 new->root_type = set->root_type;
Michal Vasko02a77382019-09-12 11:47:35 +0200763 new->local_mod = set->local_mod;
Michal Vaskof03ed032020-03-04 13:31:44 +0100764 new->tree = set->tree;
Michal Vasko02a77382019-09-12 11:47:35 +0200765 new->format = set->format;
766 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200767}
768
769/**
770 * @brief Create a deep copy of a set.
771 *
772 * @param[in] set Set to copy.
773 * @return Copy of @p set.
774 */
775static struct lyxp_set *
776set_copy(struct lyxp_set *set)
777{
778 struct lyxp_set *ret;
779 uint16_t i;
Michal Vaskoba716542019-12-16 10:01:58 +0100780 int idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200781
782 if (!set) {
783 return NULL;
784 }
785
786 ret = malloc(sizeof *ret);
787 LY_CHECK_ERR_RET(!ret, LOGMEM(set->ctx), NULL);
788 set_init(ret, set);
789
790 if (set->type == LYXP_SET_SCNODE_SET) {
791 ret->type = set->type;
792
793 for (i = 0; i < set->used; ++i) {
Michal Vaskoba716542019-12-16 10:01:58 +0100794 if ((set->val.scnodes[i].in_ctx == 1) || (set->val.scnodes[i].in_ctx == -2)) {
795 idx = lyxp_set_scnode_insert_node(ret, set->val.scnodes[i].scnode, set->val.scnodes[i].type);
Michal Vasko3f27c522020-01-06 08:37:49 +0100796 /* coverity seems to think scnodes can be NULL */
797 if ((idx == -1) || !ret->val.scnodes) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200798 lyxp_set_free(ret);
799 return NULL;
800 }
Michal Vaskoba716542019-12-16 10:01:58 +0100801 ret->val.scnodes[idx].in_ctx = set->val.scnodes[i].in_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200802 }
803 }
804 } else if (set->type == LYXP_SET_NODE_SET) {
805 ret->type = set->type;
806 ret->val.nodes = malloc(set->used * sizeof *ret->val.nodes);
807 LY_CHECK_ERR_RET(!ret->val.nodes, LOGMEM(set->ctx); free(ret), NULL);
808 memcpy(ret->val.nodes, set->val.nodes, set->used * sizeof *ret->val.nodes);
809
810 ret->used = ret->size = set->used;
811 ret->ctx_pos = set->ctx_pos;
812 ret->ctx_size = set->ctx_size;
813 ret->ht = lyht_dup(set->ht);
814 } else {
815 memcpy(ret, set, sizeof *ret);
816 if (set->type == LYXP_SET_STRING) {
817 ret->val.str = strdup(set->val.str);
818 LY_CHECK_ERR_RET(!ret->val.str, LOGMEM(set->ctx); free(ret), NULL);
819 }
820 }
821
822 return ret;
823}
824
825/**
826 * @brief Fill XPath set with a string. Any current data are disposed of.
827 *
828 * @param[in] set Set to fill.
829 * @param[in] string String to fill into \p set.
830 * @param[in] str_len Length of \p string. 0 is a valid value!
831 */
832static void
833set_fill_string(struct lyxp_set *set, const char *string, uint16_t str_len)
834{
Michal Vaskod3678892020-05-21 10:06:58 +0200835 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200836
837 set->type = LYXP_SET_STRING;
838 if ((str_len == 0) && (string[0] != '\0')) {
839 string = "";
840 }
841 set->val.str = strndup(string, str_len);
842}
843
844/**
845 * @brief Fill XPath set with a number. Any current data are disposed of.
846 *
847 * @param[in] set Set to fill.
848 * @param[in] number Number to fill into \p set.
849 */
850static void
851set_fill_number(struct lyxp_set *set, long double number)
852{
Michal Vaskod3678892020-05-21 10:06:58 +0200853 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200854
855 set->type = LYXP_SET_NUMBER;
856 set->val.num = number;
857}
858
859/**
860 * @brief Fill XPath set with a boolean. Any current data are disposed of.
861 *
862 * @param[in] set Set to fill.
863 * @param[in] boolean Boolean to fill into \p set.
864 */
865static void
866set_fill_boolean(struct lyxp_set *set, int boolean)
867{
Michal Vaskod3678892020-05-21 10:06:58 +0200868 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200869
870 set->type = LYXP_SET_BOOLEAN;
Michal Vasko004d3152020-06-11 19:59:22 +0200871 set->val.bln = boolean;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200872}
873
874/**
875 * @brief Fill XPath set with the value from another set (deep assign).
876 * Any current data are disposed of.
877 *
878 * @param[in] trg Set to fill.
879 * @param[in] src Source set to copy into \p trg.
880 */
881static void
882set_fill_set(struct lyxp_set *trg, struct lyxp_set *src)
883{
884 if (!trg || !src) {
885 return;
886 }
887
888 if (trg->type == LYXP_SET_NODE_SET) {
889 free(trg->val.nodes);
890 } else if (trg->type == LYXP_SET_STRING) {
891 free(trg->val.str);
892 }
893 set_init(trg, src);
894
895 if (src->type == LYXP_SET_SCNODE_SET) {
896 trg->type = LYXP_SET_SCNODE_SET;
897 trg->used = src->used;
898 trg->size = src->used;
899
900 trg->val.scnodes = ly_realloc(trg->val.scnodes, trg->size * sizeof *trg->val.scnodes);
901 LY_CHECK_ERR_RET(!trg->val.scnodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), );
902 memcpy(trg->val.scnodes, src->val.scnodes, src->used * sizeof *src->val.scnodes);
903 } else if (src->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +0200904 set_fill_boolean(trg, src->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200905 } else if (src->type == LYXP_SET_NUMBER) {
906 set_fill_number(trg, src->val.num);
907 } else if (src->type == LYXP_SET_STRING) {
908 set_fill_string(trg, src->val.str, strlen(src->val.str));
909 } else {
910 if (trg->type == LYXP_SET_NODE_SET) {
911 free(trg->val.nodes);
912 } else if (trg->type == LYXP_SET_STRING) {
913 free(trg->val.str);
914 }
915
Michal Vaskod3678892020-05-21 10:06:58 +0200916 assert(src->type == LYXP_SET_NODE_SET);
917
918 trg->type = LYXP_SET_NODE_SET;
919 trg->used = src->used;
920 trg->size = src->used;
921 trg->ctx_pos = src->ctx_pos;
922 trg->ctx_size = src->ctx_size;
923
924 trg->val.nodes = malloc(trg->used * sizeof *trg->val.nodes);
925 LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), );
926 memcpy(trg->val.nodes, src->val.nodes, src->used * sizeof *src->val.nodes);
927 if (src->ht) {
928 trg->ht = lyht_dup(src->ht);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200929 } else {
Michal Vaskod3678892020-05-21 10:06:58 +0200930 trg->ht = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200931 }
932 }
933}
934
935/**
936 * @brief Clear context of all schema nodes.
937 *
938 * @param[in] set Set to clear.
939 */
940static void
941set_scnode_clear_ctx(struct lyxp_set *set)
942{
943 uint32_t i;
944
945 for (i = 0; i < set->used; ++i) {
946 if (set->val.scnodes[i].in_ctx == 1) {
947 set->val.scnodes[i].in_ctx = 0;
Michal Vasko5c4e5892019-11-14 12:31:38 +0100948 } else if (set->val.scnodes[i].in_ctx == -2) {
949 set->val.scnodes[i].in_ctx = -1;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200950 }
951 }
952}
953
954/**
955 * @brief Remove a node from a set. Removing last node changes
956 * set into LYXP_SET_EMPTY. Context position aware.
957 *
958 * @param[in] set Set to use.
959 * @param[in] idx Index from @p set of the node to be removed.
960 */
961static void
962set_remove_node(struct lyxp_set *set, uint32_t idx)
963{
964 assert(set && (set->type == LYXP_SET_NODE_SET));
965 assert(idx < set->used);
966
967 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
968
969 --set->used;
970 if (set->used) {
971 memmove(&set->val.nodes[idx], &set->val.nodes[idx + 1],
972 (set->used - idx) * sizeof *set->val.nodes);
973 } else {
Michal Vaskod3678892020-05-21 10:06:58 +0200974 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200975 }
976}
977
978/**
Michal Vasko2caefc12019-11-14 16:07:56 +0100979 * @brief Remove a node from a set by setting its type to LYXP_NODE_NONE.
Michal Vasko57eab132019-09-24 11:46:26 +0200980 *
981 * @param[in] set Set to use.
982 * @param[in] idx Index from @p set of the node to be removed.
983 */
984static void
Michal Vasko2caefc12019-11-14 16:07:56 +0100985set_remove_node_none(struct lyxp_set *set, uint32_t idx)
Michal Vasko57eab132019-09-24 11:46:26 +0200986{
987 assert(set && (set->type == LYXP_SET_NODE_SET));
988 assert(idx < set->used);
989
Michal Vasko2caefc12019-11-14 16:07:56 +0100990 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
991 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
992 }
993 set->val.nodes[idx].type = LYXP_NODE_NONE;
Michal Vasko57eab132019-09-24 11:46:26 +0200994}
995
996/**
Michal Vasko2caefc12019-11-14 16:07:56 +0100997 * @brief Remove all LYXP_NODE_NONE nodes from a set. Removing last node changes
Michal Vasko57eab132019-09-24 11:46:26 +0200998 * set into LYXP_SET_EMPTY. Context position aware.
999 *
1000 * @param[in] set Set to consolidate.
1001 */
1002static void
Michal Vasko2caefc12019-11-14 16:07:56 +01001003set_remove_nodes_none(struct lyxp_set *set)
Michal Vasko57eab132019-09-24 11:46:26 +02001004{
1005 uint16_t i, orig_used, end;
1006 int32_t start;
1007
Michal Vaskod3678892020-05-21 10:06:58 +02001008 assert(set);
Michal Vasko57eab132019-09-24 11:46:26 +02001009
1010 orig_used = set->used;
1011 set->used = 0;
1012 for (i = 0; i < orig_used;) {
1013 start = -1;
1014 do {
Michal Vasko2caefc12019-11-14 16:07:56 +01001015 if ((set->val.nodes[i].type != LYXP_NODE_NONE) && (start == -1)) {
Michal Vasko57eab132019-09-24 11:46:26 +02001016 start = i;
Michal Vasko2caefc12019-11-14 16:07:56 +01001017 } else if ((start > -1) && (set->val.nodes[i].type == LYXP_NODE_NONE)) {
Michal Vasko57eab132019-09-24 11:46:26 +02001018 end = i;
1019 ++i;
1020 break;
1021 }
1022
1023 ++i;
1024 if (i == orig_used) {
1025 end = i;
1026 }
1027 } while (i < orig_used);
1028
1029 if (start > -1) {
1030 /* move the whole chunk of valid nodes together */
1031 if (set->used != (unsigned)start) {
1032 memmove(&set->val.nodes[set->used], &set->val.nodes[start], (end - start) * sizeof *set->val.nodes);
1033 }
1034 set->used += end - start;
1035 }
1036 }
Michal Vasko57eab132019-09-24 11:46:26 +02001037}
1038
1039/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02001040 * @brief Check for duplicates in a node set.
1041 *
1042 * @param[in] set Set to check.
1043 * @param[in] node Node to look for in @p set.
1044 * @param[in] node_type Type of @p node.
1045 * @param[in] skip_idx Index from @p set to skip.
1046 * @return LY_ERR
1047 */
1048static LY_ERR
1049set_dup_node_check(const struct lyxp_set *set, const struct lyd_node *node, enum lyxp_node_type node_type, int skip_idx)
1050{
1051 uint32_t i;
1052
Michal Vasko2caefc12019-11-14 16:07:56 +01001053 if (set->ht && node) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001054 return set_dup_node_hash_check(set, (struct lyd_node *)node, node_type, skip_idx);
1055 }
1056
1057 for (i = 0; i < set->used; ++i) {
1058 if ((skip_idx > -1) && (i == (unsigned)skip_idx)) {
1059 continue;
1060 }
1061
1062 if ((set->val.nodes[i].node == node) && (set->val.nodes[i].type == node_type)) {
1063 return LY_EEXIST;
1064 }
1065 }
1066
1067 return LY_SUCCESS;
1068}
1069
Michal Vaskoecd62de2019-11-13 12:35:11 +01001070int
1071lyxp_set_scnode_dup_node_check(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type, int skip_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001072{
1073 uint32_t i;
1074
1075 for (i = 0; i < set->used; ++i) {
1076 if ((skip_idx > -1) && (i == (unsigned)skip_idx)) {
1077 continue;
1078 }
1079
1080 if ((set->val.scnodes[i].scnode == node) && (set->val.scnodes[i].type == node_type)) {
1081 return i;
1082 }
1083 }
1084
1085 return -1;
1086}
1087
Michal Vaskoecd62de2019-11-13 12:35:11 +01001088void
1089lyxp_set_scnode_merge(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001090{
1091 uint32_t orig_used, i, j;
1092
Michal Vaskod3678892020-05-21 10:06:58 +02001093 assert((set1->type == LYXP_SET_SCNODE_SET) && (set2->type == LYXP_SET_SCNODE_SET));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001094
Michal Vaskod3678892020-05-21 10:06:58 +02001095 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001096 return;
1097 }
1098
Michal Vaskod3678892020-05-21 10:06:58 +02001099 if (!set1->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001100 memcpy(set1, set2, sizeof *set1);
1101 return;
1102 }
1103
1104 if (set1->used + set2->used > set1->size) {
1105 set1->size = set1->used + set2->used;
1106 set1->val.scnodes = ly_realloc(set1->val.scnodes, set1->size * sizeof *set1->val.scnodes);
1107 LY_CHECK_ERR_RET(!set1->val.scnodes, LOGMEM(set1->ctx), );
1108 }
1109
1110 orig_used = set1->used;
1111
1112 for (i = 0; i < set2->used; ++i) {
1113 for (j = 0; j < orig_used; ++j) {
1114 /* detect duplicities */
1115 if (set1->val.scnodes[j].scnode == set2->val.scnodes[i].scnode) {
1116 break;
1117 }
1118 }
1119
1120 if (j == orig_used) {
1121 memcpy(&set1->val.scnodes[set1->used], &set2->val.scnodes[i], sizeof *set2->val.scnodes);
1122 ++set1->used;
1123 }
1124 }
1125
Michal Vaskod3678892020-05-21 10:06:58 +02001126 lyxp_set_free_content(set2);
1127 set2->type = LYXP_SET_SCNODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001128}
1129
1130/**
1131 * @brief Insert a node into a set. Context position aware.
1132 *
1133 * @param[in] set Set to use.
1134 * @param[in] node Node to insert to @p set.
1135 * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting.
1136 * @param[in] node_type Node type of @p node.
1137 * @param[in] idx Index in @p set to insert into.
1138 */
1139static void
1140set_insert_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx)
1141{
Michal Vaskod3678892020-05-21 10:06:58 +02001142 assert(set && (set->type == LYXP_SET_NODE_SET));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001143
Michal Vaskod3678892020-05-21 10:06:58 +02001144 if (!set->size) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001145 /* first item */
1146 if (idx) {
1147 /* no real harm done, but it is a bug */
1148 LOGINT(set->ctx);
1149 idx = 0;
1150 }
1151 set->val.nodes = malloc(LYXP_SET_SIZE_START * sizeof *set->val.nodes);
1152 LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), );
1153 set->type = LYXP_SET_NODE_SET;
1154 set->used = 0;
1155 set->size = LYXP_SET_SIZE_START;
1156 set->ctx_pos = 1;
1157 set->ctx_size = 1;
1158 set->ht = NULL;
1159 } else {
1160 /* not an empty set */
1161 if (set->used == set->size) {
1162
1163 /* set is full */
1164 set->val.nodes = ly_realloc(set->val.nodes, (set->size + LYXP_SET_SIZE_STEP) * sizeof *set->val.nodes);
1165 LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), );
1166 set->size += LYXP_SET_SIZE_STEP;
1167 }
1168
1169 if (idx > set->used) {
1170 LOGINT(set->ctx);
1171 idx = set->used;
1172 }
1173
1174 /* make space for the new node */
1175 if (idx < set->used) {
1176 memmove(&set->val.nodes[idx + 1], &set->val.nodes[idx], (set->used - idx) * sizeof *set->val.nodes);
1177 }
1178 }
1179
1180 /* finally assign the value */
1181 set->val.nodes[idx].node = (struct lyd_node *)node;
1182 set->val.nodes[idx].type = node_type;
1183 set->val.nodes[idx].pos = pos;
1184 ++set->used;
1185
Michal Vasko2caefc12019-11-14 16:07:56 +01001186 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1187 set_insert_node_hash(set, (struct lyd_node *)node, node_type);
1188 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001189}
1190
Michal Vaskoecd62de2019-11-13 12:35:11 +01001191int
1192lyxp_set_scnode_insert_node(struct lyxp_set *set, const struct lysc_node *node, enum lyxp_node_type node_type)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001193{
1194 int ret;
1195
1196 assert(set->type == LYXP_SET_SCNODE_SET);
1197
Michal Vaskoecd62de2019-11-13 12:35:11 +01001198 ret = lyxp_set_scnode_dup_node_check(set, node, node_type, -1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001199 if (ret > -1) {
1200 set->val.scnodes[ret].in_ctx = 1;
1201 } else {
1202 if (set->used == set->size) {
1203 set->val.scnodes = ly_realloc(set->val.scnodes, (set->size + LYXP_SET_SIZE_STEP) * sizeof *set->val.scnodes);
1204 LY_CHECK_ERR_RET(!set->val.scnodes, LOGMEM(set->ctx), -1);
1205 set->size += LYXP_SET_SIZE_STEP;
1206 }
1207
1208 ret = set->used;
1209 set->val.scnodes[ret].scnode = (struct lysc_node *)node;
1210 set->val.scnodes[ret].type = node_type;
1211 set->val.scnodes[ret].in_ctx = 1;
1212 ++set->used;
1213 }
1214
1215 return ret;
1216}
1217
1218/**
1219 * @brief Replace a node in a set with another. Context position aware.
1220 *
1221 * @param[in] set Set to use.
1222 * @param[in] node Node to insert to @p set.
1223 * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting.
1224 * @param[in] node_type Node type of @p node.
1225 * @param[in] idx Index in @p set of the node to replace.
1226 */
1227static void
1228set_replace_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx)
1229{
1230 assert(set && (idx < set->used));
1231
Michal Vasko2caefc12019-11-14 16:07:56 +01001232 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1233 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1234 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001235 set->val.nodes[idx].node = (struct lyd_node *)node;
1236 set->val.nodes[idx].type = node_type;
1237 set->val.nodes[idx].pos = pos;
Michal Vasko2caefc12019-11-14 16:07:56 +01001238 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1239 set_insert_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1240 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001241}
1242
1243/**
1244 * @brief Set all nodes with ctx 1 to a new unique context value.
1245 *
1246 * @param[in] set Set to modify.
1247 * @return New context value.
1248 */
Michal Vasko5c4e5892019-11-14 12:31:38 +01001249static int32_t
Michal Vasko03ff5a72019-09-11 13:49:33 +02001250set_scnode_new_in_ctx(struct lyxp_set *set)
1251{
Michal Vasko5c4e5892019-11-14 12:31:38 +01001252 uint32_t i;
1253 int32_t ret_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001254
1255 assert(set->type == LYXP_SET_SCNODE_SET);
1256
1257 ret_ctx = 3;
1258retry:
1259 for (i = 0; i < set->used; ++i) {
1260 if (set->val.scnodes[i].in_ctx >= ret_ctx) {
1261 ret_ctx = set->val.scnodes[i].in_ctx + 1;
1262 goto retry;
1263 }
1264 }
1265 for (i = 0; i < set->used; ++i) {
1266 if (set->val.scnodes[i].in_ctx == 1) {
1267 set->val.scnodes[i].in_ctx = ret_ctx;
1268 }
1269 }
1270
1271 return ret_ctx;
1272}
1273
1274/**
1275 * @brief Get unique @p node position in the data.
1276 *
1277 * @param[in] node Node to find.
1278 * @param[in] node_type Node type of @p node.
1279 * @param[in] root Root node.
1280 * @param[in] root_type Type of the XPath @p root node.
1281 * @param[in] prev Node that we think is before @p node in DFS from @p root. Can optionally
1282 * be used to increase efficiency and start the DFS from this node.
1283 * @param[in] prev_pos Node @p prev position. Optional, but must be set if @p prev is set.
1284 * @return Node position.
1285 */
1286static uint32_t
1287get_node_pos(const struct lyd_node *node, enum lyxp_node_type node_type, const struct lyd_node *root,
1288 enum lyxp_node_type root_type, const struct lyd_node **prev, uint32_t *prev_pos)
1289{
1290 const struct lyd_node *next, *elem, *top_sibling;
1291 uint32_t pos = 1;
1292
1293 assert(prev && prev_pos && !root->prev->next);
1294
1295 if ((node_type == LYXP_NODE_ROOT) || (node_type == LYXP_NODE_ROOT_CONFIG)) {
1296 return 0;
1297 }
1298
1299 if (*prev) {
1300 /* start from the previous element instead from the root */
1301 elem = next = *prev;
1302 pos = *prev_pos;
1303 for (top_sibling = elem; top_sibling->parent; top_sibling = (struct lyd_node *)top_sibling->parent);
1304 goto dfs_search;
1305 }
1306
1307 for (top_sibling = root; top_sibling; top_sibling = top_sibling->next) {
1308 /* TREE DFS */
1309 LYD_TREE_DFS_BEGIN(top_sibling, next, elem) {
1310dfs_search:
1311 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (elem->schema->flags & LYS_CONFIG_R)) {
1312 goto skip_children;
1313 }
1314
1315 if (elem == node) {
1316 break;
1317 }
1318 ++pos;
1319
1320 /* TREE DFS END */
1321 /* select element for the next run - children first,
1322 * child exception for lyd_node_leaf and lyd_node_leaflist, but not the root */
1323 if (elem->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) {
1324 next = NULL;
1325 } else {
Michal Vasko5bfd4be2020-06-23 13:26:19 +02001326 next = lyd_node_children(elem, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001327 }
1328 if (!next) {
1329skip_children:
1330 /* no children */
1331 if (elem == top_sibling) {
1332 /* we are done, root has no children */
1333 elem = NULL;
1334 break;
1335 }
1336 /* try siblings */
1337 next = elem->next;
1338 }
1339 while (!next) {
1340 /* no siblings, go back through parents */
1341 if (elem->parent == top_sibling->parent) {
1342 /* we are done, no next element to process */
1343 elem = NULL;
1344 break;
1345 }
1346 /* parent is already processed, go to its sibling */
1347 elem = (struct lyd_node *)elem->parent;
1348 next = elem->next;
1349 }
1350 }
1351
1352 /* node found */
1353 if (elem) {
1354 break;
1355 }
1356 }
1357
1358 if (!elem) {
1359 if (!(*prev)) {
1360 /* we went from root and failed to find it, cannot be */
1361 LOGINT(node->schema->module->ctx);
1362 return 0;
1363 } else {
1364 *prev = NULL;
1365 *prev_pos = 0;
1366
1367 elem = next = top_sibling = root;
1368 pos = 1;
1369 goto dfs_search;
1370 }
1371 }
1372
1373 /* remember the last found node for next time */
1374 *prev = node;
1375 *prev_pos = pos;
1376
1377 return pos;
1378}
1379
1380/**
1381 * @brief Assign (fill) missing node positions.
1382 *
1383 * @param[in] set Set to fill positions in.
1384 * @param[in] root Context root node.
1385 * @param[in] root_type Context root type.
1386 * @return LY_ERR
1387 */
1388static LY_ERR
1389set_assign_pos(struct lyxp_set *set, const struct lyd_node *root, enum lyxp_node_type root_type)
1390{
1391 const struct lyd_node *prev = NULL, *tmp_node;
1392 uint32_t i, tmp_pos = 0;
1393
1394 for (i = 0; i < set->used; ++i) {
1395 if (!set->val.nodes[i].pos) {
1396 tmp_node = NULL;
1397 switch (set->val.nodes[i].type) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001398 case LYXP_NODE_META:
1399 tmp_node = set->val.meta[i].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001400 if (!tmp_node) {
1401 LOGINT_RET(root->schema->module->ctx);
1402 }
1403 /* fallthrough */
1404 case LYXP_NODE_ELEM:
1405 case LYXP_NODE_TEXT:
1406 if (!tmp_node) {
1407 tmp_node = set->val.nodes[i].node;
1408 }
1409 set->val.nodes[i].pos = get_node_pos(tmp_node, set->val.nodes[i].type, root, root_type, &prev, &tmp_pos);
1410 break;
1411 default:
1412 /* all roots have position 0 */
1413 break;
1414 }
1415 }
1416 }
1417
1418 return LY_SUCCESS;
1419}
1420
1421/**
Michal Vasko9f96a052020-03-10 09:41:45 +01001422 * @brief Get unique @p meta position in the parent metadata.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001423 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001424 * @param[in] meta Metadata to use.
1425 * @return Metadata position.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001426 */
1427static uint16_t
Michal Vasko9f96a052020-03-10 09:41:45 +01001428get_meta_pos(struct lyd_meta *meta)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001429{
1430 uint16_t pos = 0;
Michal Vasko9f96a052020-03-10 09:41:45 +01001431 struct lyd_meta *meta2;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001432
Michal Vasko9f96a052020-03-10 09:41:45 +01001433 for (meta2 = meta->parent->meta; meta2 && (meta2 != meta); meta2 = meta2->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001434 ++pos;
1435 }
1436
Michal Vasko9f96a052020-03-10 09:41:45 +01001437 assert(meta2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001438 return pos;
1439}
1440
1441/**
1442 * @brief Compare 2 nodes in respect to XPath document order.
1443 *
1444 * @param[in] item1 1st node.
1445 * @param[in] item2 2nd node.
1446 * @return If 1st > 2nd returns 1, 1st == 2nd returns 0, and 1st < 2nd returns -1.
1447 */
1448static int
1449set_sort_compare(struct lyxp_set_node *item1, struct lyxp_set_node *item2)
1450{
Michal Vasko9f96a052020-03-10 09:41:45 +01001451 uint32_t meta_pos1 = 0, meta_pos2 = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001452
1453 if (item1->pos < item2->pos) {
1454 return -1;
1455 }
1456
1457 if (item1->pos > item2->pos) {
1458 return 1;
1459 }
1460
1461 /* node positions are equal, the fun case */
1462
1463 /* 1st ELEM - == - 2nd TEXT, 1st TEXT - == - 2nd ELEM */
1464 /* special case since text nodes are actually saved as their parents */
1465 if ((item1->node == item2->node) && (item1->type != item2->type)) {
1466 if (item1->type == LYXP_NODE_ELEM) {
1467 assert(item2->type == LYXP_NODE_TEXT);
1468 return -1;
1469 } else {
1470 assert((item1->type == LYXP_NODE_TEXT) && (item2->type == LYXP_NODE_ELEM));
1471 return 1;
1472 }
1473 }
1474
Michal Vasko9f96a052020-03-10 09:41:45 +01001475 /* we need meta positions now */
1476 if (item1->type == LYXP_NODE_META) {
1477 meta_pos1 = get_meta_pos((struct lyd_meta *)item1->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001478 }
Michal Vasko9f96a052020-03-10 09:41:45 +01001479 if (item2->type == LYXP_NODE_META) {
1480 meta_pos2 = get_meta_pos((struct lyd_meta *)item2->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001481 }
1482
Michal Vasko9f96a052020-03-10 09:41:45 +01001483 /* 1st ROOT - 2nd ROOT, 1st ELEM - 2nd ELEM, 1st TEXT - 2nd TEXT, 1st META - =pos= - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001484 /* check for duplicates */
1485 if (item1->node == item2->node) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001486 assert((item1->type == item2->type) && ((item1->type != LYXP_NODE_META) || (meta_pos1 == meta_pos2)));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001487 return 0;
1488 }
1489
Michal Vasko9f96a052020-03-10 09:41:45 +01001490 /* 1st ELEM - 2nd TEXT, 1st ELEM - any pos - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001491 /* elem is always first, 2nd node is after it */
1492 if (item1->type == LYXP_NODE_ELEM) {
1493 assert(item2->type != LYXP_NODE_ELEM);
1494 return -1;
1495 }
1496
Michal Vasko9f96a052020-03-10 09:41:45 +01001497 /* 1st TEXT - 2nd ELEM, 1st TEXT - any pos - 2nd META, 1st META - any pos - 2nd ELEM, 1st META - >pos> - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001498 /* 2nd is before 1st */
1499 if (((item1->type == LYXP_NODE_TEXT)
Michal Vasko9f96a052020-03-10 09:41:45 +01001500 && ((item2->type == LYXP_NODE_ELEM) || (item2->type == LYXP_NODE_META)))
1501 || ((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_ELEM))
1502 || (((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_META))
1503 && (meta_pos1 > meta_pos2))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001504 return 1;
1505 }
1506
Michal Vasko9f96a052020-03-10 09:41:45 +01001507 /* 1st META - any pos - 2nd TEXT, 1st META <pos< - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001508 /* 2nd is after 1st */
1509 return -1;
1510}
1511
1512/**
1513 * @brief Set cast for comparisons.
1514 *
1515 * @param[in] trg Target set to cast source into.
1516 * @param[in] src Source set.
1517 * @param[in] type Target set type.
1518 * @param[in] src_idx Source set node index.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001519 * @return LY_ERR
1520 */
1521static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001522set_comp_cast(struct lyxp_set *trg, struct lyxp_set *src, enum lyxp_set_type type, uint32_t src_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001523{
1524 assert(src->type == LYXP_SET_NODE_SET);
1525
1526 set_init(trg, src);
1527
1528 /* insert node into target set */
1529 set_insert_node(trg, src->val.nodes[src_idx].node, src->val.nodes[src_idx].pos, src->val.nodes[src_idx].type, 0);
1530
1531 /* cast target set appropriately */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001532 return lyxp_set_cast(trg, type);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001533}
1534
1535#ifndef NDEBUG
1536
1537/**
1538 * @brief Bubble sort @p set into XPath document order.
1539 * Context position aware. Unused in the 'Release' build target.
1540 *
1541 * @param[in] set Set to sort.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001542 * @return How many times the whole set was traversed - 1 (if set was sorted, returns 0).
1543 */
1544static int
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001545set_sort(struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001546{
1547 uint32_t i, j;
1548 int ret = 0, cmp, inverted, change;
1549 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001550 struct lyxp_set_node item;
1551 struct lyxp_set_hash_node hnode;
1552 uint64_t hash;
1553
Michal Vasko3cf8fbf2020-05-27 15:21:21 +02001554 if ((set->type != LYXP_SET_NODE_SET) || (set->used < 2)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001555 return 0;
1556 }
1557
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001558 /* find first top-level node to be used as anchor for positions */
1559 for (root = set->ctx_node; root->parent; root = (const struct lyd_node *)root->parent);
1560 for (; root->prev->next; root = root->prev);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001561
1562 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001563 if (set_assign_pos(set, root, set->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001564 return -1;
1565 }
1566
1567 LOGDBG(LY_LDGXPATH, "SORT BEGIN");
1568 print_set_debug(set);
1569
1570 for (i = 0; i < set->used; ++i) {
1571 inverted = 0;
1572 change = 0;
1573
1574 for (j = 1; j < set->used - i; ++j) {
1575 /* compare node positions */
1576 if (inverted) {
1577 cmp = set_sort_compare(&set->val.nodes[j], &set->val.nodes[j - 1]);
1578 } else {
1579 cmp = set_sort_compare(&set->val.nodes[j - 1], &set->val.nodes[j]);
1580 }
1581
1582 /* swap if needed */
1583 if ((inverted && (cmp < 0)) || (!inverted && (cmp > 0))) {
1584 change = 1;
1585
1586 item = set->val.nodes[j - 1];
1587 set->val.nodes[j - 1] = set->val.nodes[j];
1588 set->val.nodes[j] = item;
1589 } else {
1590 /* whether node_pos1 should be smaller than node_pos2 or the other way around */
1591 inverted = !inverted;
1592 }
1593 }
1594
1595 ++ret;
1596
1597 if (!change) {
1598 break;
1599 }
1600 }
1601
1602 LOGDBG(LY_LDGXPATH, "SORT END %d", ret);
1603 print_set_debug(set);
1604
1605 /* check node hashes */
1606 if (set->used >= LYD_HT_MIN_ITEMS) {
1607 assert(set->ht);
1608 for (i = 0; i < set->used; ++i) {
1609 hnode.node = set->val.nodes[i].node;
1610 hnode.type = set->val.nodes[i].type;
1611
1612 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
1613 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
1614 hash = dict_hash_multi(hash, NULL, 0);
1615
1616 assert(!lyht_find(set->ht, &hnode, hash, NULL));
1617 }
1618 }
1619
1620 return ret - 1;
1621}
1622
1623/**
1624 * @brief Remove duplicate entries in a sorted node set.
1625 *
1626 * @param[in] set Sorted set to check.
1627 * @return LY_ERR (LY_EEXIST if some duplicates are found)
1628 */
1629static LY_ERR
1630set_sorted_dup_node_clean(struct lyxp_set *set)
1631{
1632 uint32_t i = 0;
1633 LY_ERR ret = LY_SUCCESS;
1634
1635 if (set->used > 1) {
1636 while (i < set->used - 1) {
1637 if ((set->val.nodes[i].node == set->val.nodes[i + 1].node)
1638 && (set->val.nodes[i].type == set->val.nodes[i + 1].type)) {
Michal Vasko2caefc12019-11-14 16:07:56 +01001639 set_remove_node_none(set, i + 1);
Michal Vasko57eab132019-09-24 11:46:26 +02001640 ret = LY_EEXIST;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001641 }
Michal Vasko57eab132019-09-24 11:46:26 +02001642 ++i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001643 }
1644 }
1645
Michal Vasko2caefc12019-11-14 16:07:56 +01001646 set_remove_nodes_none(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001647 return ret;
1648}
1649
1650#endif
1651
1652/**
1653 * @brief Merge 2 sorted sets into one.
1654 *
1655 * @param[in,out] trg Set to merge into. Duplicates are removed.
1656 * @param[in] src Set to be merged into @p trg. It is cast to #LYXP_SET_EMPTY on success.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001657 * @return LY_ERR
1658 */
1659static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001660set_sorted_merge(struct lyxp_set *trg, struct lyxp_set *src)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001661{
1662 uint32_t i, j, k, count, dup_count;
1663 int cmp;
1664 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001665
Michal Vaskod3678892020-05-21 10:06:58 +02001666 if ((trg->type != LYXP_SET_NODE_SET) || (src->type != LYXP_SET_NODE_SET)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001667 return LY_EINVAL;
1668 }
1669
Michal Vaskod3678892020-05-21 10:06:58 +02001670 if (!src->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001671 return LY_SUCCESS;
Michal Vaskod3678892020-05-21 10:06:58 +02001672 } else if (!trg->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001673 set_fill_set(trg, src);
Michal Vaskod3678892020-05-21 10:06:58 +02001674 lyxp_set_free_content(src);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001675 return LY_SUCCESS;
1676 }
1677
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001678 /* find first top-level node to be used as anchor for positions */
1679 for (root = trg->ctx_node; root->parent; root = (const struct lyd_node *)root->parent);
1680 for (; root->prev->next; root = root->prev);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001681
1682 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001683 if (set_assign_pos(trg, root, trg->root_type) || set_assign_pos(src, root, src->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001684 return LY_EINT;
1685 }
1686
1687#ifndef NDEBUG
1688 LOGDBG(LY_LDGXPATH, "MERGE target");
1689 print_set_debug(trg);
1690 LOGDBG(LY_LDGXPATH, "MERGE source");
1691 print_set_debug(src);
1692#endif
1693
1694 /* make memory for the merge (duplicates are not detected yet, so space
1695 * will likely be wasted on them, too bad) */
1696 if (trg->size - trg->used < src->used) {
1697 trg->size = trg->used + src->used;
1698
1699 trg->val.nodes = ly_realloc(trg->val.nodes, trg->size * sizeof *trg->val.nodes);
1700 LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx), LY_EMEM);
1701 }
1702
1703 i = 0;
1704 j = 0;
1705 count = 0;
1706 dup_count = 0;
1707 do {
1708 cmp = set_sort_compare(&src->val.nodes[i], &trg->val.nodes[j]);
1709 if (!cmp) {
1710 if (!count) {
1711 /* duplicate, just skip it */
1712 ++i;
1713 ++j;
1714 } else {
1715 /* we are copying something already, so let's copy the duplicate too,
1716 * we are hoping that afterwards there are some more nodes to
1717 * copy and this way we can copy them all together */
1718 ++count;
1719 ++dup_count;
1720 ++i;
1721 ++j;
1722 }
1723 } else if (cmp < 0) {
1724 /* inserting src node into trg, just remember it for now */
1725 ++count;
1726 ++i;
1727
1728 /* insert the hash now */
1729 set_insert_node_hash(trg, src->val.nodes[i - 1].node, src->val.nodes[i - 1].type);
1730 } else if (count) {
1731copy_nodes:
1732 /* time to actually copy the nodes, we have found the largest block of nodes */
1733 memmove(&trg->val.nodes[j + (count - dup_count)],
1734 &trg->val.nodes[j],
1735 (trg->used - j) * sizeof *trg->val.nodes);
1736 memcpy(&trg->val.nodes[j - dup_count], &src->val.nodes[i - count], count * sizeof *src->val.nodes);
1737
1738 trg->used += count - dup_count;
1739 /* do not change i, except the copying above, we are basically doing exactly what is in the else branch below */
1740 j += count - dup_count;
1741
1742 count = 0;
1743 dup_count = 0;
1744 } else {
1745 ++j;
1746 }
1747 } while ((i < src->used) && (j < trg->used));
1748
1749 if ((i < src->used) || count) {
1750 /* insert all the hashes first */
1751 for (k = i; k < src->used; ++k) {
1752 set_insert_node_hash(trg, src->val.nodes[k].node, src->val.nodes[k].type);
1753 }
1754
1755 /* loop ended, but we need to copy something at trg end */
1756 count += src->used - i;
1757 i = src->used;
1758 goto copy_nodes;
1759 }
1760
1761 /* we are inserting hashes before the actual node insert, which causes
1762 * situations when there were initially not enough items for a hash table,
1763 * but even after some were inserted, hash table was not created (during
1764 * insertion the number of items is not updated yet) */
1765 if (!trg->ht && (trg->used >= LYD_HT_MIN_ITEMS)) {
1766 set_insert_node_hash(trg, NULL, 0);
1767 }
1768
1769#ifndef NDEBUG
1770 LOGDBG(LY_LDGXPATH, "MERGE result");
1771 print_set_debug(trg);
1772#endif
1773
Michal Vaskod3678892020-05-21 10:06:58 +02001774 lyxp_set_free_content(src);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001775 return LY_SUCCESS;
1776}
1777
1778/*
1779 * (re)parse functions
1780 *
1781 * Parse functions parse the expression into
1782 * tokens (syntactic analysis).
1783 *
1784 * Reparse functions perform semantic analysis
1785 * (do not save the result, just a check) of
1786 * the expression and fill repeat indices.
1787 */
1788
Michal Vasko14676352020-05-29 11:35:55 +02001789LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02001790lyxp_check_token(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t tok_idx, enum lyxp_token want_tok)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001791{
Michal Vasko004d3152020-06-11 19:59:22 +02001792 if (exp->used == tok_idx) {
Michal Vasko14676352020-05-29 11:35:55 +02001793 if (ctx) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001794 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOF);
1795 }
Michal Vasko14676352020-05-29 11:35:55 +02001796 return LY_EINCOMPLETE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001797 }
1798
Michal Vasko004d3152020-06-11 19:59:22 +02001799 if (want_tok && (exp->tokens[tok_idx] != want_tok)) {
Michal Vasko14676352020-05-29 11:35:55 +02001800 if (ctx) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001801 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK,
Michal Vasko004d3152020-06-11 19:59:22 +02001802 lyxp_print_token(exp->tokens[tok_idx]), &exp->expr[exp->tok_pos[tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001803 }
Michal Vasko14676352020-05-29 11:35:55 +02001804 return LY_ENOT;
1805 }
1806
1807 return LY_SUCCESS;
1808}
1809
Michal Vasko004d3152020-06-11 19:59:22 +02001810LY_ERR
1811lyxp_next_token(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_token want_tok)
1812{
1813 LY_CHECK_RET(lyxp_check_token(ctx, exp, *tok_idx, want_tok));
1814
1815 /* skip the token */
1816 ++(*tok_idx);
1817
1818 return LY_SUCCESS;
1819}
1820
Michal Vasko14676352020-05-29 11:35:55 +02001821/* just like lyxp_check_token() but tests for 2 tokens */
1822static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02001823exp_check_token2(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t tok_idx, enum lyxp_token want_tok1,
Michal Vasko14676352020-05-29 11:35:55 +02001824 enum lyxp_token want_tok2)
1825{
Michal Vasko004d3152020-06-11 19:59:22 +02001826 if (exp->used == tok_idx) {
Michal Vasko14676352020-05-29 11:35:55 +02001827 if (ctx) {
1828 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOF);
1829 }
1830 return LY_EINCOMPLETE;
1831 }
1832
Michal Vasko004d3152020-06-11 19:59:22 +02001833 if ((exp->tokens[tok_idx] != want_tok1) && (exp->tokens[tok_idx] != want_tok2)) {
Michal Vasko14676352020-05-29 11:35:55 +02001834 if (ctx) {
1835 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK,
Michal Vasko004d3152020-06-11 19:59:22 +02001836 lyxp_print_token(exp->tokens[tok_idx]), &exp->expr[exp->tok_pos[tok_idx]]);
Michal Vasko14676352020-05-29 11:35:55 +02001837 }
1838 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001839 }
1840
1841 return LY_SUCCESS;
1842}
1843
1844/**
1845 * @brief Stack operation push on the repeat array.
1846 *
1847 * @param[in] exp Expression to use.
Michal Vasko004d3152020-06-11 19:59:22 +02001848 * @param[in] tok_idx Position in the expresion \p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001849 * @param[in] repeat_op_idx Index from \p exp of the operator token. This value is pushed.
1850 */
1851static void
Michal Vasko004d3152020-06-11 19:59:22 +02001852exp_repeat_push(struct lyxp_expr *exp, uint16_t tok_idx, uint16_t repeat_op_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001853{
1854 uint16_t i;
1855
Michal Vasko004d3152020-06-11 19:59:22 +02001856 if (exp->repeat[tok_idx]) {
1857 for (i = 0; exp->repeat[tok_idx][i]; ++i);
1858 exp->repeat[tok_idx] = realloc(exp->repeat[tok_idx], (i + 2) * sizeof *exp->repeat[tok_idx]);
1859 LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), );
1860 exp->repeat[tok_idx][i] = repeat_op_idx;
1861 exp->repeat[tok_idx][i + 1] = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001862 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02001863 exp->repeat[tok_idx] = calloc(2, sizeof *exp->repeat[tok_idx]);
1864 LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), );
1865 exp->repeat[tok_idx][0] = repeat_op_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001866 }
1867}
1868
1869/**
1870 * @brief Reparse Predicate. Logs directly on error.
1871 *
1872 * [7] Predicate ::= '[' Expr ']'
1873 *
1874 * @param[in] ctx Context for logging.
1875 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02001876 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001877 * @return LY_ERR
1878 */
1879static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02001880reparse_predicate(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001881{
1882 LY_ERR rc;
1883
Michal Vasko004d3152020-06-11 19:59:22 +02001884 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001885 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02001886 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001887
Michal Vasko004d3152020-06-11 19:59:22 +02001888 rc = reparse_or_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001889 LY_CHECK_RET(rc);
1890
Michal Vasko004d3152020-06-11 19:59:22 +02001891 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001892 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02001893 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001894
1895 return LY_SUCCESS;
1896}
1897
1898/**
1899 * @brief Reparse RelativeLocationPath. Logs directly on error.
1900 *
1901 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
1902 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
1903 * [6] NodeTest ::= NameTest | NodeType '(' ')'
1904 *
1905 * @param[in] ctx Context for logging.
1906 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02001907 * @param[in] tok_idx Position in the expression \p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001908 * @return LY_ERR (LY_EINCOMPLETE on forward reference)
1909 */
1910static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02001911reparse_relative_location_path(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001912{
1913 LY_ERR rc;
1914
Michal Vasko004d3152020-06-11 19:59:22 +02001915 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001916 LY_CHECK_RET(rc);
1917
1918 goto step;
1919 do {
1920 /* '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02001921 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001922
Michal Vasko004d3152020-06-11 19:59:22 +02001923 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001924 LY_CHECK_RET(rc);
1925step:
1926 /* Step */
Michal Vasko004d3152020-06-11 19:59:22 +02001927 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001928 case LYXP_TOKEN_DOT:
Michal Vasko004d3152020-06-11 19:59:22 +02001929 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001930 break;
1931
1932 case LYXP_TOKEN_DDOT:
Michal Vasko004d3152020-06-11 19:59:22 +02001933 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001934 break;
1935
1936 case LYXP_TOKEN_AT:
Michal Vasko004d3152020-06-11 19:59:22 +02001937 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001938
Michal Vasko004d3152020-06-11 19:59:22 +02001939 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001940 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02001941 if ((exp->tokens[*tok_idx] != LYXP_TOKEN_NAMETEST) && (exp->tokens[*tok_idx] != LYXP_TOKEN_NODETYPE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001942 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK,
Michal Vasko004d3152020-06-11 19:59:22 +02001943 lyxp_print_token(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001944 return LY_EVALID;
1945 }
1946 /* fall through */
1947 case LYXP_TOKEN_NAMETEST:
Michal Vasko004d3152020-06-11 19:59:22 +02001948 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001949 goto reparse_predicate;
1950 break;
1951
1952 case LYXP_TOKEN_NODETYPE:
Michal Vasko004d3152020-06-11 19:59:22 +02001953 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001954
1955 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02001956 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001957 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02001958 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001959
1960 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02001961 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001962 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02001963 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001964
1965reparse_predicate:
1966 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02001967 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
1968 rc = reparse_predicate(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001969 LY_CHECK_RET(rc);
1970 }
1971 break;
1972 default:
1973 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK,
Michal Vasko004d3152020-06-11 19:59:22 +02001974 lyxp_print_token(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001975 return LY_EVALID;
1976 }
Michal Vasko004d3152020-06-11 19:59:22 +02001977 } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001978
1979 return LY_SUCCESS;
1980}
1981
1982/**
1983 * @brief Reparse AbsoluteLocationPath. Logs directly on error.
1984 *
1985 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
1986 *
1987 * @param[in] ctx Context for logging.
1988 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02001989 * @param[in] tok_idx Position in the expression \p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001990 * @return LY_ERR
1991 */
1992static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02001993reparse_absolute_location_path(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001994{
1995 LY_ERR rc;
1996
Michal Vasko004d3152020-06-11 19:59:22 +02001997 LY_CHECK_RET(exp_check_token2(ctx, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001998
1999 /* '/' RelativeLocationPath? */
Michal Vasko004d3152020-06-11 19:59:22 +02002000 if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002001 /* '/' */
Michal Vasko004d3152020-06-11 19:59:22 +02002002 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002003
Michal Vasko004d3152020-06-11 19:59:22 +02002004 if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002005 return LY_SUCCESS;
2006 }
Michal Vasko004d3152020-06-11 19:59:22 +02002007 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002008 case LYXP_TOKEN_DOT:
2009 case LYXP_TOKEN_DDOT:
2010 case LYXP_TOKEN_AT:
2011 case LYXP_TOKEN_NAMETEST:
2012 case LYXP_TOKEN_NODETYPE:
Michal Vasko004d3152020-06-11 19:59:22 +02002013 rc = reparse_relative_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002014 LY_CHECK_RET(rc);
2015 /* fall through */
2016 default:
2017 break;
2018 }
2019
2020 /* '//' RelativeLocationPath */
2021 } else {
2022 /* '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02002023 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002024
Michal Vasko004d3152020-06-11 19:59:22 +02002025 rc = reparse_relative_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002026 LY_CHECK_RET(rc);
2027 }
2028
2029 return LY_SUCCESS;
2030}
2031
2032/**
2033 * @brief Reparse FunctionCall. Logs directly on error.
2034 *
2035 * [9] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
2036 *
2037 * @param[in] ctx Context for logging.
2038 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002039 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002040 * @return LY_ERR
2041 */
2042static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002043reparse_function_call(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002044{
2045 int min_arg_count = -1, max_arg_count, arg_count;
Michal Vasko004d3152020-06-11 19:59:22 +02002046 uint16_t func_tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002047 LY_ERR rc;
2048
Michal Vasko004d3152020-06-11 19:59:22 +02002049 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_FUNCNAME);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002050 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002051 func_tok_idx = *tok_idx;
2052 switch (exp->tok_len[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002053 case 3:
Michal Vasko004d3152020-06-11 19:59:22 +02002054 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002055 min_arg_count = 1;
2056 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002057 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002058 min_arg_count = 1;
2059 max_arg_count = 1;
2060 }
2061 break;
2062 case 4:
Michal Vasko004d3152020-06-11 19:59:22 +02002063 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002064 min_arg_count = 1;
2065 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002066 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002067 min_arg_count = 0;
2068 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002069 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002070 min_arg_count = 0;
2071 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002072 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002073 min_arg_count = 0;
2074 max_arg_count = 0;
2075 }
2076 break;
2077 case 5:
Michal Vasko004d3152020-06-11 19:59:22 +02002078 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002079 min_arg_count = 1;
2080 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002081 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002082 min_arg_count = 0;
2083 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002084 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002085 min_arg_count = 1;
2086 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002087 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002088 min_arg_count = 1;
2089 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002090 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002091 min_arg_count = 1;
2092 max_arg_count = 1;
2093 }
2094 break;
2095 case 6:
Michal Vasko004d3152020-06-11 19:59:22 +02002096 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002097 min_arg_count = 2;
Michal Vaskobe2e3562019-10-15 15:35:35 +02002098 max_arg_count = INT_MAX;
Michal Vasko004d3152020-06-11 19:59:22 +02002099 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002100 min_arg_count = 0;
2101 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002102 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002103 min_arg_count = 0;
2104 max_arg_count = 1;
2105 }
2106 break;
2107 case 7:
Michal Vasko004d3152020-06-11 19:59:22 +02002108 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002109 min_arg_count = 1;
2110 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002111 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002112 min_arg_count = 1;
2113 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002114 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002115 min_arg_count = 0;
2116 max_arg_count = 0;
2117 }
2118 break;
2119 case 8:
Michal Vasko004d3152020-06-11 19:59:22 +02002120 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002121 min_arg_count = 2;
2122 max_arg_count = 2;
Michal Vasko004d3152020-06-11 19:59:22 +02002123 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002124 min_arg_count = 0;
2125 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002126 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002127 min_arg_count = 2;
2128 max_arg_count = 2;
2129 }
2130 break;
2131 case 9:
Michal Vasko004d3152020-06-11 19:59:22 +02002132 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002133 min_arg_count = 2;
2134 max_arg_count = 3;
Michal Vasko004d3152020-06-11 19:59:22 +02002135 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002136 min_arg_count = 3;
2137 max_arg_count = 3;
2138 }
2139 break;
2140 case 10:
Michal Vasko004d3152020-06-11 19:59:22 +02002141 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002142 min_arg_count = 0;
2143 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002144 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002145 min_arg_count = 1;
2146 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002147 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002148 min_arg_count = 2;
2149 max_arg_count = 2;
2150 }
2151 break;
2152 case 11:
Michal Vasko004d3152020-06-11 19:59:22 +02002153 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002154 min_arg_count = 2;
2155 max_arg_count = 2;
2156 }
2157 break;
2158 case 12:
Michal Vasko004d3152020-06-11 19:59:22 +02002159 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002160 min_arg_count = 2;
2161 max_arg_count = 2;
2162 }
2163 break;
2164 case 13:
Michal Vasko004d3152020-06-11 19:59:22 +02002165 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002166 min_arg_count = 0;
2167 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002168 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002169 min_arg_count = 0;
2170 max_arg_count = 1;
2171 }
2172 break;
2173 case 15:
Michal Vasko004d3152020-06-11 19:59:22 +02002174 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002175 min_arg_count = 0;
2176 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002177 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002178 min_arg_count = 2;
2179 max_arg_count = 2;
2180 }
2181 break;
2182 case 16:
Michal Vasko004d3152020-06-11 19:59:22 +02002183 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002184 min_arg_count = 2;
2185 max_arg_count = 2;
2186 }
2187 break;
2188 case 20:
Michal Vasko004d3152020-06-11 19:59:22 +02002189 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002190 min_arg_count = 2;
2191 max_arg_count = 2;
2192 }
2193 break;
2194 }
2195 if (min_arg_count == -1) {
Michal Vasko004d3152020-06-11 19:59:22 +02002196 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INFUNC, exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002197 return LY_EINVAL;
2198 }
Michal Vasko004d3152020-06-11 19:59:22 +02002199 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002200
2201 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02002202 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002203 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002204 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002205
2206 /* ( Expr ( ',' Expr )* )? */
2207 arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002208 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002209 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002210 if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002211 ++arg_count;
Michal Vasko004d3152020-06-11 19:59:22 +02002212 rc = reparse_or_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002213 LY_CHECK_RET(rc);
2214 }
Michal Vasko004d3152020-06-11 19:59:22 +02002215 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) {
2216 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002217
2218 ++arg_count;
Michal Vasko004d3152020-06-11 19:59:22 +02002219 rc = reparse_or_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002220 LY_CHECK_RET(rc);
2221 }
2222
2223 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02002224 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002225 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002226 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002227
2228 if ((arg_count < min_arg_count) || (arg_count > max_arg_count)) {
Michal Vasko004d3152020-06-11 19:59:22 +02002229 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INARGCOUNT, arg_count, exp->tok_len[func_tok_idx],
2230 &exp->expr[exp->tok_pos[func_tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002231 return LY_EVALID;
2232 }
2233
2234 return LY_SUCCESS;
2235}
2236
2237/**
2238 * @brief Reparse PathExpr. Logs directly on error.
2239 *
2240 * [10] PathExpr ::= LocationPath | PrimaryExpr Predicate*
2241 * | PrimaryExpr Predicate* '/' RelativeLocationPath
2242 * | PrimaryExpr Predicate* '//' RelativeLocationPath
2243 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
2244 * [8] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall
2245 *
2246 * @param[in] ctx Context for logging.
2247 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002248 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002249 * @return LY_ERR
2250 */
2251static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002252reparse_path_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002253{
2254 LY_ERR rc;
2255
Michal Vasko004d3152020-06-11 19:59:22 +02002256 if (lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko14676352020-05-29 11:35:55 +02002257 return LY_EVALID;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002258 }
2259
Michal Vasko004d3152020-06-11 19:59:22 +02002260 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002261 case LYXP_TOKEN_PAR1:
2262 /* '(' Expr ')' Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002263 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002264
Michal Vasko004d3152020-06-11 19:59:22 +02002265 rc = reparse_or_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002266 LY_CHECK_RET(rc);
2267
Michal Vasko004d3152020-06-11 19:59:22 +02002268 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002269 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002270 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002271 goto predicate;
2272 break;
2273 case LYXP_TOKEN_DOT:
2274 case LYXP_TOKEN_DDOT:
2275 case LYXP_TOKEN_AT:
2276 case LYXP_TOKEN_NAMETEST:
2277 case LYXP_TOKEN_NODETYPE:
2278 /* RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002279 rc = reparse_relative_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002280 LY_CHECK_RET(rc);
2281 break;
2282 case LYXP_TOKEN_FUNCNAME:
2283 /* FunctionCall */
Michal Vasko004d3152020-06-11 19:59:22 +02002284 rc = reparse_function_call(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002285 LY_CHECK_RET(rc);
2286 goto predicate;
2287 break;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002288 case LYXP_TOKEN_OPER_PATH:
2289 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02002290 /* AbsoluteLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002291 rc = reparse_absolute_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002292 LY_CHECK_RET(rc);
2293 break;
2294 case LYXP_TOKEN_LITERAL:
2295 /* Literal */
Michal Vasko004d3152020-06-11 19:59:22 +02002296 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002297 goto predicate;
2298 break;
2299 case LYXP_TOKEN_NUMBER:
2300 /* Number */
Michal Vasko004d3152020-06-11 19:59:22 +02002301 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002302 goto predicate;
2303 break;
2304 default:
2305 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK,
Michal Vasko004d3152020-06-11 19:59:22 +02002306 lyxp_print_token(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002307 return LY_EVALID;
2308 }
2309
2310 return LY_SUCCESS;
2311
2312predicate:
2313 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002314 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
2315 rc = reparse_predicate(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002316 LY_CHECK_RET(rc);
2317 }
2318
2319 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002320 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002321
2322 /* '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02002323 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002324
Michal Vasko004d3152020-06-11 19:59:22 +02002325 rc = reparse_relative_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002326 LY_CHECK_RET(rc);
2327 }
2328
2329 return LY_SUCCESS;
2330}
2331
2332/**
2333 * @brief Reparse UnaryExpr. Logs directly on error.
2334 *
2335 * [17] UnaryExpr ::= UnionExpr | '-' UnaryExpr
2336 * [18] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
2337 *
2338 * @param[in] ctx Context for logging.
2339 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002340 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002341 * @return LY_ERR
2342 */
2343static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002344reparse_unary_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002345{
2346 uint16_t prev_exp;
2347 LY_ERR rc;
2348
2349 /* ('-')* */
Michal Vasko004d3152020-06-11 19:59:22 +02002350 prev_exp = *tok_idx;
2351 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH)
2352 && (exp->expr[exp->tok_pos[*tok_idx]] == '-')) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002353 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNARY);
Michal Vasko004d3152020-06-11 19:59:22 +02002354 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002355 }
2356
2357 /* PathExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002358 prev_exp = *tok_idx;
2359 rc = reparse_path_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002360 LY_CHECK_RET(rc);
2361
2362 /* ('|' PathExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002363 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_UNI)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002364 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNION);
Michal Vasko004d3152020-06-11 19:59:22 +02002365 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002366
Michal Vasko004d3152020-06-11 19:59:22 +02002367 rc = reparse_path_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002368 LY_CHECK_RET(rc);
2369 }
2370
2371 return LY_SUCCESS;
2372}
2373
2374/**
2375 * @brief Reparse AdditiveExpr. Logs directly on error.
2376 *
2377 * [15] AdditiveExpr ::= MultiplicativeExpr
2378 * | AdditiveExpr '+' MultiplicativeExpr
2379 * | AdditiveExpr '-' MultiplicativeExpr
2380 * [16] MultiplicativeExpr ::= UnaryExpr
2381 * | MultiplicativeExpr '*' UnaryExpr
2382 * | MultiplicativeExpr 'div' UnaryExpr
2383 * | MultiplicativeExpr 'mod' UnaryExpr
2384 *
2385 * @param[in] ctx Context for logging.
2386 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002387 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002388 * @return LY_ERR
2389 */
2390static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002391reparse_additive_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002392{
2393 uint16_t prev_add_exp, prev_mul_exp;
2394 LY_ERR rc;
2395
Michal Vasko004d3152020-06-11 19:59:22 +02002396 prev_add_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002397 goto reparse_multiplicative_expr;
2398
2399 /* ('+' / '-' MultiplicativeExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002400 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH)
2401 && ((exp->expr[exp->tok_pos[*tok_idx]] == '+') || (exp->expr[exp->tok_pos[*tok_idx]] == '-'))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002402 exp_repeat_push(exp, prev_add_exp, LYXP_EXPR_ADDITIVE);
Michal Vasko004d3152020-06-11 19:59:22 +02002403 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002404
2405reparse_multiplicative_expr:
2406 /* UnaryExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002407 prev_mul_exp = *tok_idx;
2408 rc = reparse_unary_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002409 LY_CHECK_RET(rc);
2410
2411 /* ('*' / 'div' / 'mod' UnaryExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002412 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH)
2413 && ((exp->expr[exp->tok_pos[*tok_idx]] == '*') || (exp->tok_len[*tok_idx] == 3))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002414 exp_repeat_push(exp, prev_mul_exp, LYXP_EXPR_MULTIPLICATIVE);
Michal Vasko004d3152020-06-11 19:59:22 +02002415 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002416
Michal Vasko004d3152020-06-11 19:59:22 +02002417 rc = reparse_unary_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002418 LY_CHECK_RET(rc);
2419 }
2420 }
2421
2422 return LY_SUCCESS;
2423}
2424
2425/**
2426 * @brief Reparse EqualityExpr. Logs directly on error.
2427 *
2428 * [13] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
2429 * | EqualityExpr '!=' RelationalExpr
2430 * [14] RelationalExpr ::= AdditiveExpr
2431 * | RelationalExpr '<' AdditiveExpr
2432 * | RelationalExpr '>' AdditiveExpr
2433 * | RelationalExpr '<=' AdditiveExpr
2434 * | RelationalExpr '>=' AdditiveExpr
2435 *
2436 * @param[in] ctx Context for logging.
2437 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002438 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002439 * @return LY_ERR
2440 */
2441static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002442reparse_equality_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002443{
2444 uint16_t prev_eq_exp, prev_rel_exp;
2445 LY_ERR rc;
2446
Michal Vasko004d3152020-06-11 19:59:22 +02002447 prev_eq_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002448 goto reparse_additive_expr;
2449
2450 /* ('=' / '!=' RelationalExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002451 while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_EQUAL, LYXP_TOKEN_OPER_NEQUAL)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002452 exp_repeat_push(exp, prev_eq_exp, LYXP_EXPR_EQUALITY);
Michal Vasko004d3152020-06-11 19:59:22 +02002453 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002454
2455reparse_additive_expr:
2456 /* AdditiveExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002457 prev_rel_exp = *tok_idx;
2458 rc = reparse_additive_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002459 LY_CHECK_RET(rc);
2460
2461 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002462 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_COMP)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002463 exp_repeat_push(exp, prev_rel_exp, LYXP_EXPR_RELATIONAL);
Michal Vasko004d3152020-06-11 19:59:22 +02002464 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002465
Michal Vasko004d3152020-06-11 19:59:22 +02002466 rc = reparse_additive_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002467 LY_CHECK_RET(rc);
2468 }
2469 }
2470
2471 return LY_SUCCESS;
2472}
2473
2474/**
2475 * @brief Reparse OrExpr. Logs directly on error.
2476 *
2477 * [11] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
2478 * [12] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
2479 *
2480 * @param[in] ctx Context for logging.
2481 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002482 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002483 * @return LY_ERR
2484 */
2485static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002486reparse_or_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002487{
2488 uint16_t prev_or_exp, prev_and_exp;
2489 LY_ERR rc;
2490
Michal Vasko004d3152020-06-11 19:59:22 +02002491 prev_or_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002492 goto reparse_equality_expr;
2493
2494 /* ('or' AndExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002495 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_LOG) && (exp->tok_len[*tok_idx] == 2)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002496 exp_repeat_push(exp, prev_or_exp, LYXP_EXPR_OR);
Michal Vasko004d3152020-06-11 19:59:22 +02002497 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002498
2499reparse_equality_expr:
2500 /* EqualityExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002501 prev_and_exp = *tok_idx;
2502 rc = reparse_equality_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002503 LY_CHECK_RET(rc);
2504
2505 /* ('and' EqualityExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002506 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_LOG) && (exp->tok_len[*tok_idx] == 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002507 exp_repeat_push(exp, prev_and_exp, LYXP_EXPR_AND);
Michal Vasko004d3152020-06-11 19:59:22 +02002508 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002509
Michal Vasko004d3152020-06-11 19:59:22 +02002510 rc = reparse_equality_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002511 LY_CHECK_RET(rc);
2512 }
2513 }
2514
2515 return LY_SUCCESS;
2516}
Radek Krejcib1646a92018-11-02 16:08:26 +01002517
2518/**
2519 * @brief Parse NCName.
2520 *
2521 * @param[in] ncname Name to parse.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002522 * @return Length of @p ncname valid bytes.
Radek Krejcib1646a92018-11-02 16:08:26 +01002523 */
Radek Krejcid4270262019-01-07 15:07:25 +01002524static long int
Radek Krejcib1646a92018-11-02 16:08:26 +01002525parse_ncname(const char *ncname)
2526{
2527 unsigned int uc;
Radek Krejcid4270262019-01-07 15:07:25 +01002528 size_t size;
2529 long int len = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002530
2531 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), 0);
2532 if (!is_xmlqnamestartchar(uc) || (uc == ':')) {
2533 return len;
2534 }
2535
2536 do {
2537 len += size;
Radek Krejci9a564c92019-01-07 14:53:57 +01002538 if (!*ncname) {
2539 break;
2540 }
Radek Krejcid4270262019-01-07 15:07:25 +01002541 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), -len);
Radek Krejcib1646a92018-11-02 16:08:26 +01002542 } while (is_xmlqnamechar(uc) && (uc != ':'));
2543
2544 return len;
2545}
2546
2547/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02002548 * @brief Add @p token into the expression @p exp.
Radek Krejcib1646a92018-11-02 16:08:26 +01002549 *
Michal Vasko03ff5a72019-09-11 13:49:33 +02002550 * @param[in] ctx Context for logging.
Radek Krejcib1646a92018-11-02 16:08:26 +01002551 * @param[in] exp Expression to use.
2552 * @param[in] token Token to add.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002553 * @param[in] tok_pos Token position in the XPath expression.
Radek Krejcib1646a92018-11-02 16:08:26 +01002554 * @param[in] tok_len Token length in the XPath expression.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002555 * @return LY_ERR
Radek Krejcib1646a92018-11-02 16:08:26 +01002556 */
2557static LY_ERR
Michal Vasko14676352020-05-29 11:35:55 +02002558exp_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 +01002559{
2560 uint32_t prev;
2561
2562 if (exp->used == exp->size) {
2563 prev = exp->size;
2564 exp->size += LYXP_EXPR_SIZE_STEP;
2565 if (prev > exp->size) {
2566 LOGINT(ctx);
2567 return LY_EINT;
2568 }
2569
2570 exp->tokens = ly_realloc(exp->tokens, exp->size * sizeof *exp->tokens);
2571 LY_CHECK_ERR_RET(!exp->tokens, LOGMEM(ctx), LY_EMEM);
2572 exp->tok_pos = ly_realloc(exp->tok_pos, exp->size * sizeof *exp->tok_pos);
2573 LY_CHECK_ERR_RET(!exp->tok_pos, LOGMEM(ctx), LY_EMEM);
2574 exp->tok_len = ly_realloc(exp->tok_len, exp->size * sizeof *exp->tok_len);
2575 LY_CHECK_ERR_RET(!exp->tok_len, LOGMEM(ctx), LY_EMEM);
2576 }
2577
2578 exp->tokens[exp->used] = token;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002579 exp->tok_pos[exp->used] = tok_pos;
Radek Krejcib1646a92018-11-02 16:08:26 +01002580 exp->tok_len[exp->used] = tok_len;
2581 ++exp->used;
2582 return LY_SUCCESS;
2583}
2584
2585void
Michal Vasko14676352020-05-29 11:35:55 +02002586lyxp_expr_free(const struct ly_ctx *ctx, struct lyxp_expr *expr)
Radek Krejcib1646a92018-11-02 16:08:26 +01002587{
2588 uint16_t i;
2589
2590 if (!expr) {
2591 return;
2592 }
2593
2594 lydict_remove(ctx, expr->expr);
2595 free(expr->tokens);
2596 free(expr->tok_pos);
2597 free(expr->tok_len);
2598 if (expr->repeat) {
2599 for (i = 0; i < expr->used; ++i) {
2600 free(expr->repeat[i]);
2601 }
2602 }
2603 free(expr->repeat);
2604 free(expr);
2605}
2606
2607struct lyxp_expr *
Michal Vasko004d3152020-06-11 19:59:22 +02002608lyxp_expr_parse(const struct ly_ctx *ctx, const char *expr, size_t expr_len, int reparse)
Radek Krejcib1646a92018-11-02 16:08:26 +01002609{
2610 struct lyxp_expr *ret;
Radek Krejcid4270262019-01-07 15:07:25 +01002611 size_t parsed = 0, tok_len;
2612 long int ncname_len;
Radek Krejcib1646a92018-11-02 16:08:26 +01002613 enum lyxp_token tok_type;
2614 int prev_function_check = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002615 uint16_t tok_idx = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002616
Michal Vasko004d3152020-06-11 19:59:22 +02002617 if (!expr[0]) {
2618 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOF);
2619 return NULL;
2620 }
2621
2622 if (!expr_len) {
2623 expr_len = strlen(expr);
2624 }
2625 if (expr_len > UINT16_MAX) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002626 LOGERR(ctx, LY_EINVAL, "XPath expression cannot be longer than %ud characters.", UINT16_MAX);
2627 return NULL;
2628 }
2629
2630 /* init lyxp_expr structure */
2631 ret = calloc(1, sizeof *ret);
2632 LY_CHECK_ERR_GOTO(!ret, LOGMEM(ctx), error);
Michal Vasko004d3152020-06-11 19:59:22 +02002633 ret->expr = lydict_insert(ctx, expr, expr_len);
Radek Krejcib1646a92018-11-02 16:08:26 +01002634 LY_CHECK_ERR_GOTO(!ret->expr, LOGMEM(ctx), error);
2635 ret->used = 0;
2636 ret->size = LYXP_EXPR_SIZE_START;
2637 ret->tokens = malloc(ret->size * sizeof *ret->tokens);
2638 LY_CHECK_ERR_GOTO(!ret->tokens, LOGMEM(ctx), error);
2639
2640 ret->tok_pos = malloc(ret->size * sizeof *ret->tok_pos);
2641 LY_CHECK_ERR_GOTO(!ret->tok_pos, LOGMEM(ctx), error);
2642
2643 ret->tok_len = malloc(ret->size * sizeof *ret->tok_len);
2644 LY_CHECK_ERR_GOTO(!ret->tok_len, LOGMEM(ctx), error);
2645
Michal Vasko004d3152020-06-11 19:59:22 +02002646 /* make expr 0-terminated */
2647 expr = ret->expr;
2648
Radek Krejcib1646a92018-11-02 16:08:26 +01002649 while (is_xmlws(expr[parsed])) {
2650 ++parsed;
2651 }
2652
2653 do {
2654 if (expr[parsed] == '(') {
2655
2656 /* '(' */
2657 tok_len = 1;
2658 tok_type = LYXP_TOKEN_PAR1;
2659
2660 if (prev_function_check && ret->used && (ret->tokens[ret->used - 1] == LYXP_TOKEN_NAMETEST)) {
2661 /* it is a NodeType/FunctionName after all */
2662 if (((ret->tok_len[ret->used - 1] == 4)
2663 && (!strncmp(&expr[ret->tok_pos[ret->used - 1]], "node", 4)
2664 || !strncmp(&expr[ret->tok_pos[ret->used - 1]], "text", 4))) ||
2665 ((ret->tok_len[ret->used - 1] == 7)
2666 && !strncmp(&expr[ret->tok_pos[ret->used - 1]], "comment", 7))) {
2667 ret->tokens[ret->used - 1] = LYXP_TOKEN_NODETYPE;
2668 } else {
2669 ret->tokens[ret->used - 1] = LYXP_TOKEN_FUNCNAME;
2670 }
2671 prev_function_check = 0;
2672 }
2673
2674 } else if (expr[parsed] == ')') {
2675
2676 /* ')' */
2677 tok_len = 1;
2678 tok_type = LYXP_TOKEN_PAR2;
2679
2680 } else if (expr[parsed] == '[') {
2681
2682 /* '[' */
2683 tok_len = 1;
2684 tok_type = LYXP_TOKEN_BRACK1;
2685
2686 } else if (expr[parsed] == ']') {
2687
2688 /* ']' */
2689 tok_len = 1;
2690 tok_type = LYXP_TOKEN_BRACK2;
2691
2692 } else if (!strncmp(&expr[parsed], "..", 2)) {
2693
2694 /* '..' */
2695 tok_len = 2;
2696 tok_type = LYXP_TOKEN_DDOT;
2697
2698 } else if ((expr[parsed] == '.') && (!isdigit(expr[parsed + 1]))) {
2699
2700 /* '.' */
2701 tok_len = 1;
2702 tok_type = LYXP_TOKEN_DOT;
2703
2704 } else if (expr[parsed] == '@') {
2705
2706 /* '@' */
2707 tok_len = 1;
2708 tok_type = LYXP_TOKEN_AT;
2709
2710 } else if (expr[parsed] == ',') {
2711
2712 /* ',' */
2713 tok_len = 1;
2714 tok_type = LYXP_TOKEN_COMMA;
2715
2716 } else if (expr[parsed] == '\'') {
2717
2718 /* Literal with ' */
2719 for (tok_len = 1; (expr[parsed + tok_len] != '\0') && (expr[parsed + tok_len] != '\''); ++tok_len);
2720 LY_CHECK_ERR_GOTO(expr[parsed + tok_len] == '\0',
2721 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOE, expr[parsed], &expr[parsed]), error);
2722 ++tok_len;
2723 tok_type = LYXP_TOKEN_LITERAL;
2724
2725 } else if (expr[parsed] == '\"') {
2726
2727 /* Literal with " */
2728 for (tok_len = 1; (expr[parsed + tok_len] != '\0') && (expr[parsed + tok_len] != '\"'); ++tok_len);
2729 LY_CHECK_ERR_GOTO(expr[parsed + tok_len] == '\0',
2730 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOE, expr[parsed], &expr[parsed]), error);
2731 ++tok_len;
2732 tok_type = LYXP_TOKEN_LITERAL;
2733
2734 } else if ((expr[parsed] == '.') || (isdigit(expr[parsed]))) {
2735
2736 /* Number */
2737 for (tok_len = 0; isdigit(expr[parsed + tok_len]); ++tok_len);
2738 if (expr[parsed + tok_len] == '.') {
2739 ++tok_len;
2740 for (; isdigit(expr[parsed + tok_len]); ++tok_len);
2741 }
2742 tok_type = LYXP_TOKEN_NUMBER;
2743
2744 } else if (expr[parsed] == '/') {
2745
2746 /* Operator '/', '//' */
2747 if (!strncmp(&expr[parsed], "//", 2)) {
2748 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002749 tok_type = LYXP_TOKEN_OPER_RPATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002750 } else {
2751 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002752 tok_type = LYXP_TOKEN_OPER_PATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002753 }
Radek Krejcib1646a92018-11-02 16:08:26 +01002754
Michal Vasko3e48bf32020-06-01 08:39:07 +02002755 } else if (!strncmp(&expr[parsed], "!=", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002756
Michal Vasko3e48bf32020-06-01 08:39:07 +02002757 /* Operator '!=' */
Radek Krejcib1646a92018-11-02 16:08:26 +01002758 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002759 tok_type = LYXP_TOKEN_OPER_NEQUAL;
2760
2761 } else if (!strncmp(&expr[parsed], "<=", 2) || !strncmp(&expr[parsed], ">=", 2)) {
2762
2763 /* Operator '<=', '>=' */
2764 tok_len = 2;
2765 tok_type = LYXP_TOKEN_OPER_COMP;
Radek Krejcib1646a92018-11-02 16:08:26 +01002766
2767 } else if (expr[parsed] == '|') {
2768
2769 /* Operator '|' */
2770 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002771 tok_type = LYXP_TOKEN_OPER_UNI;
Radek Krejcib1646a92018-11-02 16:08:26 +01002772
2773 } else if ((expr[parsed] == '+') || (expr[parsed] == '-')) {
2774
2775 /* Operator '+', '-' */
2776 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002777 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002778
Michal Vasko3e48bf32020-06-01 08:39:07 +02002779 } else if (expr[parsed] == '=') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002780
Michal Vasko3e48bf32020-06-01 08:39:07 +02002781 /* Operator '=' */
Radek Krejcib1646a92018-11-02 16:08:26 +01002782 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002783 tok_type = LYXP_TOKEN_OPER_EQUAL;
2784
2785 } else if ((expr[parsed] == '<') || (expr[parsed] == '>')) {
2786
2787 /* Operator '<', '>' */
2788 tok_len = 1;
2789 tok_type = LYXP_TOKEN_OPER_COMP;
Radek Krejcib1646a92018-11-02 16:08:26 +01002790
2791 } else if (ret->used && (ret->tokens[ret->used - 1] != LYXP_TOKEN_AT)
2792 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_PAR1)
2793 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_BRACK1)
2794 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_COMMA)
Michal Vasko3e48bf32020-06-01 08:39:07 +02002795 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_LOG)
2796 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_EQUAL)
2797 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_NEQUAL)
2798 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_COMP)
2799 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_MATH)
2800 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_UNI)
2801 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_PATH)
2802 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_RPATH)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002803
2804 /* Operator '*', 'or', 'and', 'mod', or 'div' */
2805 if (expr[parsed] == '*') {
2806 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002807 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002808
2809 } else if (!strncmp(&expr[parsed], "or", 2)) {
2810 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002811 tok_type = LYXP_TOKEN_OPER_LOG;
Radek Krejcib1646a92018-11-02 16:08:26 +01002812
2813 } else if (!strncmp(&expr[parsed], "and", 3)) {
2814 tok_len = 3;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002815 tok_type = LYXP_TOKEN_OPER_LOG;
Radek Krejcib1646a92018-11-02 16:08:26 +01002816
2817 } else if (!strncmp(&expr[parsed], "mod", 3) || !strncmp(&expr[parsed], "div", 3)) {
2818 tok_len = 3;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002819 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002820
2821 } else if (prev_function_check) {
Michal Vasko53078572019-05-24 08:50:15 +02002822 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "Invalid character 0x%x ('%c'), perhaps \"%.*s\" is supposed to be a function call.",
2823 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 +01002824 goto error;
2825 } else {
Radek Krejcid4270262019-01-07 15:07:25 +01002826 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INEXPR, parsed + 1, expr);
Radek Krejcib1646a92018-11-02 16:08:26 +01002827 goto error;
2828 }
2829 } else if (expr[parsed] == '*') {
2830
2831 /* NameTest '*' */
2832 tok_len = 1;
2833 tok_type = LYXP_TOKEN_NAMETEST;
2834
2835 } else {
2836
2837 /* NameTest (NCName ':' '*' | QName) or NodeType/FunctionName */
2838 ncname_len = parse_ncname(&expr[parsed]);
Radek Krejcid4270262019-01-07 15:07:25 +01002839 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 +01002840 tok_len = ncname_len;
2841
2842 if (expr[parsed + tok_len] == ':') {
2843 ++tok_len;
2844 if (expr[parsed + tok_len] == '*') {
2845 ++tok_len;
2846 } else {
2847 ncname_len = parse_ncname(&expr[parsed + tok_len]);
Radek Krejcid4270262019-01-07 15:07:25 +01002848 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 +01002849 tok_len += ncname_len;
2850 }
2851 /* remove old flag to prevent ambiguities */
2852 prev_function_check = 0;
2853 tok_type = LYXP_TOKEN_NAMETEST;
2854 } else {
2855 /* there is no prefix so it can still be NodeType/FunctionName, we can't finally decide now */
2856 prev_function_check = 1;
2857 tok_type = LYXP_TOKEN_NAMETEST;
2858 }
2859 }
2860
2861 /* store the token, move on to the next one */
2862 LY_CHECK_GOTO(exp_add_token(ctx, ret, tok_type, parsed, tok_len), error);
2863 parsed += tok_len;
2864 while (is_xmlws(expr[parsed])) {
2865 ++parsed;
2866 }
2867
2868 } while (expr[parsed]);
2869
Michal Vasko004d3152020-06-11 19:59:22 +02002870 if (reparse) {
2871 /* prealloc repeat */
2872 ret->repeat = calloc(ret->size, sizeof *ret->repeat);
2873 LY_CHECK_ERR_GOTO(!ret->repeat, LOGMEM(ctx), error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002874
Michal Vasko004d3152020-06-11 19:59:22 +02002875 /* fill repeat */
2876 LY_CHECK_GOTO(reparse_or_expr(ctx, ret, &tok_idx), error);
2877 if (ret->used > tok_idx) {
2878 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "Unparsed characters \"%s\" left at the end of an XPath expression.",
2879 &ret->expr[ret->tok_pos[tok_idx]]);
2880 goto error;
2881 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02002882 }
2883
2884 print_expr_struct_debug(ret);
2885
Radek Krejcib1646a92018-11-02 16:08:26 +01002886 return ret;
2887
2888error:
2889 lyxp_expr_free(ctx, ret);
2890 return NULL;
2891}
2892
Michal Vasko004d3152020-06-11 19:59:22 +02002893struct lyxp_expr *
2894lyxp_expr_dup(const struct ly_ctx *ctx, const struct lyxp_expr *exp)
2895{
2896 struct lyxp_expr *dup;
2897 uint32_t i, j;
2898
2899 dup = calloc(1, sizeof *dup);
2900 LY_CHECK_ERR_GOTO(!dup, LOGMEM(ctx), error);
2901
2902 dup->tokens = malloc(exp->used * sizeof *dup->tokens);
2903 LY_CHECK_ERR_GOTO(!dup->tokens, LOGMEM(ctx), error);
2904 memcpy(dup->tokens, exp->tokens, exp->used * sizeof *dup->tokens);
2905
2906 dup->tok_pos = malloc(exp->used * sizeof *dup->tok_pos);
2907 LY_CHECK_ERR_GOTO(!dup->tok_pos, LOGMEM(ctx), error);
2908 memcpy(dup->tok_pos, exp->tok_pos, exp->used * sizeof *dup->tok_pos);
2909
2910 dup->tok_len = malloc(exp->used * sizeof *dup->tok_len);
2911 LY_CHECK_ERR_GOTO(!dup->tok_len, LOGMEM(ctx), error);
2912 memcpy(dup->tok_len, exp->tok_len, exp->used * sizeof *dup->tok_len);
2913
2914 dup->repeat = malloc(exp->used * sizeof *dup->repeat);
2915 LY_CHECK_ERR_GOTO(!dup->repeat, LOGMEM(ctx), error);
2916 for (i = 0; i < exp->used; ++i) {
2917 if (!exp->repeat[i]) {
2918 dup->repeat[i] = NULL;
2919 } else {
2920 for (j = 0; exp->repeat[i][j]; ++j);
2921 /* the ending 0 as well */
2922 ++j;
2923
2924 dup->repeat[i] = malloc(j * sizeof *dup->repeat);
2925 LY_CHECK_ERR_GOTO(!dup->repeat[i], LOGMEM(ctx), error);
2926 memcpy(dup->repeat[i], exp->repeat[i], j * sizeof **dup->repeat);
2927 dup->repeat[i][j - 1] = 0;
2928 }
2929 }
2930
2931 dup->used = exp->used;
2932 dup->size = exp->used;
2933 dup->expr = lydict_insert(ctx, exp->expr, 0);
2934
2935 return dup;
2936
2937error:
2938 lyxp_expr_free(ctx, dup);
2939 return NULL;
2940}
2941
Michal Vasko03ff5a72019-09-11 13:49:33 +02002942/*
2943 * warn functions
2944 *
2945 * Warn functions check specific reasonable conditions for schema XPath
2946 * and print a warning if they are not satisfied.
2947 */
2948
2949/**
2950 * @brief Get the last-added schema node that is currently in the context.
2951 *
2952 * @param[in] set Set to search in.
2953 * @return Last-added schema context node, NULL if no node is in context.
2954 */
2955static struct lysc_node *
2956warn_get_scnode_in_ctx(struct lyxp_set *set)
2957{
2958 uint32_t i;
2959
2960 if (!set || (set->type != LYXP_SET_SCNODE_SET)) {
2961 return NULL;
2962 }
2963
2964 i = set->used;
2965 do {
2966 --i;
2967 if (set->val.scnodes[i].in_ctx == 1) {
2968 /* if there are more, simply return the first found (last added) */
2969 return set->val.scnodes[i].scnode;
2970 }
2971 } while (i);
2972
2973 return NULL;
2974}
2975
2976/**
2977 * @brief Test whether a type is numeric - integer type or decimal64.
2978 *
2979 * @param[in] type Type to test.
2980 * @return 1 if numeric, 0 otherwise.
2981 */
2982static int
2983warn_is_numeric_type(struct lysc_type *type)
2984{
2985 struct lysc_type_union *uni;
2986 int ret;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002987 LY_ARRAY_SIZE_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002988
2989 switch (type->basetype) {
2990 case LY_TYPE_DEC64:
2991 case LY_TYPE_INT8:
2992 case LY_TYPE_UINT8:
2993 case LY_TYPE_INT16:
2994 case LY_TYPE_UINT16:
2995 case LY_TYPE_INT32:
2996 case LY_TYPE_UINT32:
2997 case LY_TYPE_INT64:
2998 case LY_TYPE_UINT64:
2999 return 1;
3000 case LY_TYPE_UNION:
3001 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003002 LY_ARRAY_FOR(uni->types, u) {
3003 ret = warn_is_numeric_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003004 if (ret) {
3005 /* found a suitable type */
3006 return 1;
3007 }
3008 }
3009 /* did not find any suitable type */
3010 return 0;
3011 case LY_TYPE_LEAFREF:
3012 return warn_is_numeric_type(((struct lysc_type_leafref *)type)->realtype);
3013 default:
3014 return 0;
3015 }
3016}
3017
3018/**
3019 * @brief Test whether a type is string-like - no integers, decimal64 or binary.
3020 *
3021 * @param[in] type Type to test.
3022 * @return 1 if string, 0 otherwise.
3023 */
3024static int
3025warn_is_string_type(struct lysc_type *type)
3026{
3027 struct lysc_type_union *uni;
3028 int ret;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003029 LY_ARRAY_SIZE_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003030
3031 switch (type->basetype) {
3032 case LY_TYPE_BITS:
3033 case LY_TYPE_ENUM:
3034 case LY_TYPE_IDENT:
3035 case LY_TYPE_INST:
3036 case LY_TYPE_STRING:
3037 return 1;
3038 case LY_TYPE_UNION:
3039 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003040 LY_ARRAY_FOR(uni->types, u) {
3041 ret = warn_is_string_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003042 if (ret) {
3043 /* found a suitable type */
3044 return 1;
3045 }
3046 }
3047 /* did not find any suitable type */
3048 return 0;
3049 case LY_TYPE_LEAFREF:
3050 return warn_is_string_type(((struct lysc_type_leafref *)type)->realtype);
3051 default:
3052 return 0;
3053 }
3054}
3055
3056/**
3057 * @brief Test whether a type is one specific type.
3058 *
3059 * @param[in] type Type to test.
3060 * @param[in] base Expected type.
3061 * @return 1 if it is, 0 otherwise.
3062 */
3063static int
3064warn_is_specific_type(struct lysc_type *type, LY_DATA_TYPE base)
3065{
3066 struct lysc_type_union *uni;
3067 int ret;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003068 LY_ARRAY_SIZE_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003069
3070 if (type->basetype == base) {
3071 return 1;
3072 } else if (type->basetype == LY_TYPE_UNION) {
3073 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003074 LY_ARRAY_FOR(uni->types, u) {
3075 ret = warn_is_specific_type(uni->types[u], base);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003076 if (ret) {
3077 /* found a suitable type */
3078 return 1;
3079 }
3080 }
3081 /* did not find any suitable type */
3082 return 0;
3083 } else if (type->basetype == LY_TYPE_LEAFREF) {
3084 return warn_is_specific_type(((struct lysc_type_leafref *)type)->realtype, base);
3085 }
3086
3087 return 0;
3088}
3089
3090/**
3091 * @brief Get next type of a (union) type.
3092 *
3093 * @param[in] type Base type.
3094 * @param[in] prev_type Previously returned type.
3095 * @return Next type or NULL.
3096 */
3097static struct lysc_type *
3098warn_is_equal_type_next_type(struct lysc_type *type, struct lysc_type *prev_type)
3099{
3100 struct lysc_type_union *uni;
3101 int found = 0;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003102 LY_ARRAY_SIZE_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003103
3104 switch (type->basetype) {
3105 case LY_TYPE_UNION:
3106 uni = (struct lysc_type_union *)type;
3107 if (!prev_type) {
3108 return uni->types[0];
3109 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003110 LY_ARRAY_FOR(uni->types, u) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003111 if (found) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003112 return uni->types[u];
Michal Vasko03ff5a72019-09-11 13:49:33 +02003113 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003114 if (prev_type == uni->types[u]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003115 found = 1;
3116 }
3117 }
3118 return NULL;
3119 default:
3120 if (prev_type) {
3121 assert(type == prev_type);
3122 return NULL;
3123 } else {
3124 return type;
3125 }
3126 }
3127}
3128
3129/**
3130 * @brief Test whether 2 types have a common type.
3131 *
3132 * @param[in] type1 First type.
3133 * @param[in] type2 Second type.
3134 * @return 1 if they do, 0 otherwise.
3135 */
3136static int
3137warn_is_equal_type(struct lysc_type *type1, struct lysc_type *type2)
3138{
3139 struct lysc_type *t1, *rt1, *t2, *rt2;
3140
3141 t1 = NULL;
3142 while ((t1 = warn_is_equal_type_next_type(type1, t1))) {
3143 if (t1->basetype == LY_TYPE_LEAFREF) {
3144 rt1 = ((struct lysc_type_leafref *)t1)->realtype;
3145 } else {
3146 rt1 = t1;
3147 }
3148
3149 t2 = NULL;
3150 while ((t2 = warn_is_equal_type_next_type(type2, t2))) {
3151 if (t2->basetype == LY_TYPE_LEAFREF) {
3152 rt2 = ((struct lysc_type_leafref *)t2)->realtype;
3153 } else {
3154 rt2 = t2;
3155 }
3156
3157 if (rt2->basetype == rt1->basetype) {
3158 /* match found */
3159 return 1;
3160 }
3161 }
3162 }
3163
3164 return 0;
3165}
3166
3167/**
3168 * @brief Check both operands of comparison operators.
3169 *
3170 * @param[in] ctx Context for errors.
3171 * @param[in] set1 First operand set.
3172 * @param[in] set2 Second operand set.
3173 * @param[in] numbers_only Whether accept only numbers or other types are fine too (for '=' and '!=').
3174 * @param[in] expr Start of the expression to print with the warning.
3175 * @param[in] tok_pos Token position.
3176 */
3177static void
3178warn_operands(struct ly_ctx *ctx, struct lyxp_set *set1, struct lyxp_set *set2, int numbers_only, const char *expr, uint16_t tok_pos)
3179{
3180 struct lysc_node_leaf *node1, *node2;
3181 int leaves = 1, warning = 0;
3182
3183 node1 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set1);
3184 node2 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set2);
3185
3186 if (!node1 && !node2) {
3187 /* no node-sets involved, nothing to do */
3188 return;
3189 }
3190
3191 if (node1) {
3192 if (!(node1->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3193 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node1->nodetype), node1->name);
3194 warning = 1;
3195 leaves = 0;
3196 } else if (numbers_only && !warn_is_numeric_type(node1->type)) {
3197 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node1->name);
3198 warning = 1;
3199 }
3200 }
3201
3202 if (node2) {
3203 if (!(node2->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3204 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node2->nodetype), node2->name);
3205 warning = 1;
3206 leaves = 0;
3207 } else if (numbers_only && !warn_is_numeric_type(node2->type)) {
3208 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node2->name);
3209 warning = 1;
3210 }
3211 }
3212
3213 if (node1 && node2 && leaves && !numbers_only) {
3214 if ((warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type))
3215 || (!warn_is_numeric_type(node1->type) && warn_is_numeric_type(node2->type))
3216 || (!warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type)
3217 && !warn_is_equal_type(node1->type, node2->type))) {
3218 LOGWRN(ctx, "Incompatible types of operands \"%s\" and \"%s\" for comparison.", node1->name, node2->name);
3219 warning = 1;
3220 }
3221 }
3222
3223 if (warning) {
3224 LOGWRN(ctx, "Previous warning generated by XPath subexpression[%u] \"%.20s\".", tok_pos, expr + tok_pos);
3225 }
3226}
3227
3228/**
3229 * @brief Check that a value is valid for a leaf. If not applicable, does nothing.
3230 *
3231 * @param[in] exp Parsed XPath expression.
3232 * @param[in] set Set with the leaf/leaf-list.
3233 * @param[in] val_exp Index of the value (literal/number) in @p exp.
3234 * @param[in] equal_exp Index of the start of the equality expression in @p exp.
3235 * @param[in] last_equal_exp Index of the end of the equality expression in @p exp.
3236 */
3237static void
3238warn_equality_value(struct lyxp_expr *exp, struct lyxp_set *set, uint16_t val_exp, uint16_t equal_exp, uint16_t last_equal_exp)
3239{
3240 struct lysc_node *scnode;
3241 struct lysc_type *type;
3242 char *value;
3243 LY_ERR rc;
3244 struct ly_err_item *err = NULL;
3245
3246 if ((scnode = warn_get_scnode_in_ctx(set)) && (scnode->nodetype & (LYS_LEAF | LYS_LEAFLIST))
3247 && ((exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) || (exp->tokens[val_exp] == LYXP_TOKEN_NUMBER))) {
3248 /* check that the node can have the specified value */
3249 if (exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) {
3250 value = strndup(exp->expr + exp->tok_pos[val_exp] + 1, exp->tok_len[val_exp] - 2);
3251 } else {
3252 value = strndup(exp->expr + exp->tok_pos[val_exp], exp->tok_len[val_exp]);
3253 }
3254 if (!value) {
3255 LOGMEM(set->ctx);
3256 return;
3257 }
3258
3259 if ((((struct lysc_node_leaf *)scnode)->type->basetype == LY_TYPE_IDENT) && !strchr(value, ':')) {
3260 LOGWRN(set->ctx, "Identityref \"%s\" comparison with identity \"%s\" without prefix, consider adding"
3261 " a prefix or best using \"derived-from(-or-self)()\" functions.", scnode->name, value);
3262 LOGWRN(set->ctx, "Previous warning generated by XPath subexpression[%u] \"%.*s\".", exp->tok_pos[equal_exp],
3263 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
3264 exp->expr + exp->tok_pos[equal_exp]);
3265 }
3266
3267 type = ((struct lysc_node_leaf *)scnode)->type;
3268 if (type->basetype != LY_TYPE_IDENT) {
3269 rc = type->plugin->store(set->ctx, type, value, strlen(value), LY_TYPE_OPTS_SCHEMA,
3270 lys_resolve_prefix, (void *)type->dflt_mod, LYD_XML, NULL, NULL, NULL, NULL, &err);
3271
3272 if (err) {
3273 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type (%s).", value, err->msg);
3274 ly_err_free(err);
3275 } else if (rc != LY_SUCCESS) {
3276 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type.", value);
3277 }
3278 if (rc != LY_SUCCESS) {
3279 LOGWRN(set->ctx, "Previous warning generated by XPath subexpression[%u] \"%.*s\".", exp->tok_pos[equal_exp],
3280 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
3281 exp->expr + exp->tok_pos[equal_exp]);
3282 }
3283 }
3284 free(value);
3285 }
3286}
3287
3288/*
3289 * XPath functions
3290 */
3291
3292/**
3293 * @brief Execute the YANG 1.1 bit-is-set(node-set, string) function. Returns LYXP_SET_BOOLEAN
3294 * depending on whether the first node bit value from the second argument is set.
3295 *
3296 * @param[in] args Array of arguments.
3297 * @param[in] arg_count Count of elements in @p args.
3298 * @param[in,out] set Context and result set at the same time.
3299 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003300 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003301 */
3302static LY_ERR
3303xpath_bit_is_set(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3304{
3305 struct lyd_node_term *leaf;
3306 struct lysc_node_leaf *sleaf;
3307 struct lysc_type_bits *bits;
3308 LY_ERR rc = LY_SUCCESS;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003309 LY_ARRAY_SIZE_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003310
3311 if (options & LYXP_SCNODE_ALL) {
3312 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3313 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003314 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3315 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 +02003316 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_BITS)) {
3317 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"bits\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003318 }
3319
3320 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3321 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3322 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 +02003323 } else if (!warn_is_string_type(sleaf->type)) {
3324 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003325 }
3326 }
3327 set_scnode_clear_ctx(set);
3328 return rc;
3329 }
3330
Michal Vaskod3678892020-05-21 10:06:58 +02003331 if (args[0]->type != LYXP_SET_NODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003332 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)");
3333 return LY_EVALID;
3334 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003335 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003336 LY_CHECK_RET(rc);
3337
3338 set_fill_boolean(set, 0);
Michal Vaskod3678892020-05-21 10:06:58 +02003339 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003340 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
3341 if ((leaf->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST))
3342 && (((struct lysc_node_leaf *)leaf->schema)->type->basetype == LY_TYPE_BITS)) {
3343 bits = (struct lysc_type_bits *)((struct lysc_node_leaf *)leaf->schema)->type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003344 LY_ARRAY_FOR(bits->bits, u) {
3345 if (!strcmp(bits->bits[u].name, args[1]->val.str)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003346 set_fill_boolean(set, 1);
3347 break;
3348 }
3349 }
3350 }
3351 }
3352
3353 return LY_SUCCESS;
3354}
3355
3356/**
3357 * @brief Execute the XPath boolean(object) function. Returns LYXP_SET_BOOLEAN
3358 * with the argument converted to boolean.
3359 *
3360 * @param[in] args Array of arguments.
3361 * @param[in] arg_count Count of elements in @p args.
3362 * @param[in,out] set Context and result set at the same time.
3363 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003364 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003365 */
3366static LY_ERR
3367xpath_boolean(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3368{
3369 LY_ERR rc;
3370
3371 if (options & LYXP_SCNODE_ALL) {
3372 set_scnode_clear_ctx(set);
3373 return LY_SUCCESS;
3374 }
3375
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003376 rc = lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003377 LY_CHECK_RET(rc);
3378 set_fill_set(set, args[0]);
3379
3380 return LY_SUCCESS;
3381}
3382
3383/**
3384 * @brief Execute the XPath ceiling(number) function. Returns LYXP_SET_NUMBER
3385 * with the first argument rounded up to the nearest integer.
3386 *
3387 * @param[in] args Array of arguments.
3388 * @param[in] arg_count Count of elements in @p args.
3389 * @param[in,out] set Context and result set at the same time.
3390 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003391 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003392 */
3393static LY_ERR
3394xpath_ceiling(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3395{
3396 struct lysc_node_leaf *sleaf;
3397 LY_ERR rc = LY_SUCCESS;
3398
3399 if (options & LYXP_SCNODE_ALL) {
3400 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3401 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003402 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3403 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 +02003404 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
3405 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003406 }
3407 set_scnode_clear_ctx(set);
3408 return rc;
3409 }
3410
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003411 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003412 LY_CHECK_RET(rc);
3413 if ((long long)args[0]->val.num != args[0]->val.num) {
3414 set_fill_number(set, ((long long)args[0]->val.num) + 1);
3415 } else {
3416 set_fill_number(set, args[0]->val.num);
3417 }
3418
3419 return LY_SUCCESS;
3420}
3421
3422/**
3423 * @brief Execute the XPath concat(string, string, string*) function.
3424 * Returns LYXP_SET_STRING with the concatenation of all the arguments.
3425 *
3426 * @param[in] args Array of arguments.
3427 * @param[in] arg_count Count of elements in @p args.
3428 * @param[in,out] set Context and result set at the same time.
3429 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003430 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003431 */
3432static LY_ERR
3433xpath_concat(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
3434{
3435 uint16_t i;
3436 char *str = NULL;
3437 size_t used = 1;
3438 LY_ERR rc = LY_SUCCESS;
3439 struct lysc_node_leaf *sleaf;
3440
3441 if (options & LYXP_SCNODE_ALL) {
3442 for (i = 0; i < arg_count; ++i) {
3443 if ((args[i]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[i]))) {
3444 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3445 LOGWRN(set->ctx, "Argument #%u of %s is a %s node \"%s\".",
3446 i + 1, __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003447 } else if (!warn_is_string_type(sleaf->type)) {
3448 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 +02003449 }
3450 }
3451 }
3452 set_scnode_clear_ctx(set);
3453 return rc;
3454 }
3455
3456 for (i = 0; i < arg_count; ++i) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003457 rc = lyxp_set_cast(args[i], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003458 if (rc != LY_SUCCESS) {
3459 free(str);
3460 return rc;
3461 }
3462
3463 str = ly_realloc(str, (used + strlen(args[i]->val.str)) * sizeof(char));
3464 LY_CHECK_ERR_RET(!str, LOGMEM(set->ctx), LY_EMEM);
3465 strcpy(str + used - 1, args[i]->val.str);
3466 used += strlen(args[i]->val.str);
3467 }
3468
3469 /* free, kind of */
Michal Vaskod3678892020-05-21 10:06:58 +02003470 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003471 set->type = LYXP_SET_STRING;
3472 set->val.str = str;
3473
3474 return LY_SUCCESS;
3475}
3476
3477/**
3478 * @brief Execute the XPath contains(string, string) function.
3479 * Returns LYXP_SET_BOOLEAN whether the second argument can
3480 * be found in the first or not.
3481 *
3482 * @param[in] args Array of arguments.
3483 * @param[in] arg_count Count of elements in @p args.
3484 * @param[in,out] set Context and result set at the same time.
3485 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003486 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003487 */
3488static LY_ERR
3489xpath_contains(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3490{
3491 struct lysc_node_leaf *sleaf;
3492 LY_ERR rc = LY_SUCCESS;
3493
3494 if (options & LYXP_SCNODE_ALL) {
3495 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3496 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3497 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 +02003498 } else if (!warn_is_string_type(sleaf->type)) {
3499 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003500 }
3501 }
3502
3503 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3504 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3505 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 +02003506 } else if (!warn_is_string_type(sleaf->type)) {
3507 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003508 }
3509 }
3510 set_scnode_clear_ctx(set);
3511 return rc;
3512 }
3513
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003514 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003515 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003516 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003517 LY_CHECK_RET(rc);
3518
3519 if (strstr(args[0]->val.str, args[1]->val.str)) {
3520 set_fill_boolean(set, 1);
3521 } else {
3522 set_fill_boolean(set, 0);
3523 }
3524
3525 return LY_SUCCESS;
3526}
3527
3528/**
3529 * @brief Execute the XPath count(node-set) function. Returns LYXP_SET_NUMBER
3530 * with the size of the node-set from the argument.
3531 *
3532 * @param[in] args Array of arguments.
3533 * @param[in] arg_count Count of elements in @p args.
3534 * @param[in,out] set Context and result set at the same time.
3535 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003536 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003537 */
3538static LY_ERR
3539xpath_count(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3540{
3541 struct lysc_node *scnode = NULL;
3542 LY_ERR rc = LY_SUCCESS;
3543
3544 if (options & LYXP_SCNODE_ALL) {
3545 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(scnode = warn_get_scnode_in_ctx(args[0]))) {
3546 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003547 }
3548 set_scnode_clear_ctx(set);
3549 return rc;
3550 }
3551
Michal Vasko03ff5a72019-09-11 13:49:33 +02003552 if (args[0]->type != LYXP_SET_NODE_SET) {
3553 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "count(node-set)");
3554 return LY_EVALID;
3555 }
3556
3557 set_fill_number(set, args[0]->used);
3558 return LY_SUCCESS;
3559}
3560
3561/**
3562 * @brief Execute the XPath current() function. Returns LYXP_SET_NODE_SET
3563 * with the context with the intial node.
3564 *
3565 * @param[in] args Array of arguments.
3566 * @param[in] arg_count Count of elements in @p args.
3567 * @param[in,out] set Context and result set at the same time.
3568 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003569 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003570 */
3571static LY_ERR
3572xpath_current(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
3573{
3574 if (arg_count || args) {
3575 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGCOUNT, arg_count, "current()");
3576 return LY_EVALID;
3577 }
3578
3579 if (options & LYXP_SCNODE_ALL) {
3580 set_scnode_clear_ctx(set);
3581
Michal Vaskoecd62de2019-11-13 12:35:11 +01003582 lyxp_set_scnode_insert_node(set, set->ctx_scnode, LYXP_NODE_ELEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003583 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02003584 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003585
3586 /* position is filled later */
3587 set_insert_node(set, set->ctx_node, 0, LYXP_NODE_ELEM, 0);
3588 }
3589
3590 return LY_SUCCESS;
3591}
3592
3593/**
3594 * @brief Execute the YANG 1.1 deref(node-set) function. Returns LYXP_SET_NODE_SET with either
3595 * leafref or instance-identifier target node(s).
3596 *
3597 * @param[in] args Array of arguments.
3598 * @param[in] arg_count Count of elements in @p args.
3599 * @param[in,out] set Context and result set at the same time.
3600 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003601 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003602 */
3603static LY_ERR
3604xpath_deref(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3605{
3606 struct lyd_node_term *leaf;
Michal Vasko42e497c2020-01-06 08:38:25 +01003607 struct lysc_node_leaf *sleaf = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02003608 struct lysc_type_leafref *lref;
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003609 const struct lysc_node *target;
Michal Vasko004d3152020-06-11 19:59:22 +02003610 struct ly_path *p;
3611 struct lyd_node *node;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003612 char *errmsg = NULL;
3613 const char *val;
3614 int dynamic;
Michal Vasko00cbf532020-06-15 13:58:47 +02003615 uint8_t oper;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003616 LY_ERR rc = LY_SUCCESS;
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_LEAFREF) && !warn_is_specific_type(sleaf->type, LY_TYPE_INST)) {
3624 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"leafref\" nor \"instance-identifier\".",
3625 __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003626 }
3627 set_scnode_clear_ctx(set);
Michal Vasko42e497c2020-01-06 08:38:25 +01003628 if (sleaf && (sleaf->type->basetype == LY_TYPE_LEAFREF)) {
Michal Vasko004d3152020-06-11 19:59:22 +02003629 lref = (struct lysc_type_leafref *)sleaf->type;
Michal Vasko00cbf532020-06-15 13:58:47 +02003630 oper = lysc_is_output((struct lysc_node *)sleaf) ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT;
Michal Vasko004d3152020-06-11 19:59:22 +02003631
3632 /* it was already evaluated on schema, it must succeed */
3633 if (set->format == LYD_JSON) {
Michal Vasko00cbf532020-06-15 13:58:47 +02003634 rc = ly_path_compile(set->ctx, sleaf->module, (struct lysc_node *)sleaf, lref->path, LY_PATH_LREF_TRUE,
3635 oper, LY_PATH_TARGET_MANY, lydjson_resolve_prefix, NULL, LYD_JSON, &p);
Michal Vasko004d3152020-06-11 19:59:22 +02003636 } else {
3637 assert(set->format == LYD_SCHEMA);
Michal Vasko00cbf532020-06-15 13:58:47 +02003638 rc = ly_path_compile(set->ctx, sleaf->module, (struct lysc_node *)sleaf, lref->path, LY_PATH_LREF_TRUE,
3639 oper, LY_PATH_TARGET_MANY, lys_resolve_prefix, lref->path_context, LYD_SCHEMA, &p);
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003640 }
Michal Vasko004d3152020-06-11 19:59:22 +02003641 assert(!rc);
3642
3643 /* get the target node */
3644 target = p[LY_ARRAY_SIZE(p) - 1].node;
3645 ly_path_free(set->ctx, p);
3646
3647 lyxp_set_scnode_insert_node(set, target, LYXP_NODE_ELEM);
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003648 }
3649
Michal Vasko03ff5a72019-09-11 13:49:33 +02003650 return rc;
3651 }
3652
Michal Vaskod3678892020-05-21 10:06:58 +02003653 if (args[0]->type != LYXP_SET_NODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003654 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "deref(node-set)");
3655 return LY_EVALID;
3656 }
3657
Michal Vaskod3678892020-05-21 10:06:58 +02003658 lyxp_set_free_content(set);
3659 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003660 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
3661 sleaf = (struct lysc_node_leaf *)leaf->schema;
3662 if (sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
3663 if (sleaf->type->basetype == LY_TYPE_LEAFREF) {
3664 /* find leafref target */
Michal Vasko004d3152020-06-11 19:59:22 +02003665 if (ly_type_find_leafref((struct lysc_type_leafref *)sleaf->type, (struct lyd_node *)leaf,
3666 &leaf->value, set->tree, &node, &errmsg)) {
3667 LOGERR(set->ctx, LY_EVALID, errmsg);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003668 free(errmsg);
Michal Vasko004d3152020-06-11 19:59:22 +02003669 return LY_EVALID;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003670 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003671 } else {
3672 assert(sleaf->type->basetype == LY_TYPE_INST);
Michal Vasko004d3152020-06-11 19:59:22 +02003673 if (ly_path_eval(leaf->value.target, set->tree, &node)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003674 val = lyd_value2str(leaf, &dynamic);
3675 LOGERR(set->ctx, LY_EVALID, "Invalid instance-identifier \"%s\" value - required instance not found.", val);
3676 if (dynamic) {
3677 free((char *)val);
3678 }
3679 return LY_EVALID;
3680 }
3681 }
Michal Vasko004d3152020-06-11 19:59:22 +02003682
3683 /* insert it */
3684 set_insert_node(set, node, 0, LYXP_NODE_ELEM, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003685 }
3686 }
3687
3688 return LY_SUCCESS;
3689}
3690
3691/**
3692 * @brief Execute the YANG 1.1 derived-from(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
3693 * on whether the first argument nodes contain a node of an identity derived from the second
3694 * argument identity.
3695 *
3696 * @param[in] args Array of arguments.
3697 * @param[in] arg_count Count of elements in @p args.
3698 * @param[in,out] set Context and result set at the same time.
3699 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003700 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003701 */
3702static LY_ERR
3703xpath_derived_from(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3704{
3705 uint16_t i;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003706 LY_ARRAY_SIZE_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003707 struct lyd_node_term *leaf;
3708 struct lysc_node_leaf *sleaf;
3709 struct lyd_value data = {0};
3710 struct ly_err_item *err = NULL;
3711 LY_ERR rc = LY_SUCCESS;
3712 int found;
3713
3714 if (options & LYXP_SCNODE_ALL) {
3715 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3716 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003717 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3718 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 +02003719 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_IDENT)) {
3720 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"identityref\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003721 }
3722
3723 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3724 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3725 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 +02003726 } else if (!warn_is_string_type(sleaf->type)) {
3727 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003728 }
3729 }
3730 set_scnode_clear_ctx(set);
3731 return rc;
3732 }
3733
Michal Vaskod3678892020-05-21 10:06:58 +02003734 if (args[0]->type != LYXP_SET_NODE_SET) {
3735 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
3736 "derived-from(node-set, string)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003737 return LY_EVALID;
3738 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003739 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003740 LY_CHECK_RET(rc);
3741
3742 set_fill_boolean(set, 0);
Michal Vaskod3678892020-05-21 10:06:58 +02003743 found = 0;
3744 for (i = 0; i < args[0]->used; ++i) {
3745 leaf = (struct lyd_node_term *)args[0]->val.nodes[i].node;
3746 sleaf = (struct lysc_node_leaf *)leaf->schema;
3747 if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_IDENT)) {
3748 /* store args[1] into ident */
3749 rc = sleaf->type->plugin->store(set->ctx, sleaf->type, args[1]->val.str, strlen(args[1]->val.str),
3750 LY_TYPE_OPTS_STORE, lys_resolve_prefix, (void *)sleaf->dflt_mod, set->format,
3751 (struct lyd_node *)leaf, set->tree, &data, NULL, &err);
3752 if (err) {
3753 ly_err_print(err);
3754 ly_err_free(err);
3755 }
3756 LY_CHECK_RET(rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003757
Michal Vaskod3678892020-05-21 10:06:58 +02003758 LY_ARRAY_FOR(data.ident->derived, u) {
3759 if (data.ident->derived[u] == leaf->value.ident) {
3760 set_fill_boolean(set, 1);
3761 found = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003762 break;
3763 }
3764 }
Michal Vaskod3678892020-05-21 10:06:58 +02003765 if (found) {
3766 break;
3767 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003768 }
3769 }
3770
3771 return LY_SUCCESS;
3772}
3773
3774/**
3775 * @brief Execute the YANG 1.1 derived-from-or-self(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
3776 * on whether the first argument nodes contain a node of an identity that either is or is derived from
3777 * the second argument identity.
3778 *
3779 * @param[in] args Array of arguments.
3780 * @param[in] arg_count Count of elements in @p args.
3781 * @param[in,out] set Context and result set at the same time.
3782 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003783 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003784 */
3785static LY_ERR
3786xpath_derived_from_or_self(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3787{
3788 uint16_t i;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003789 LY_ARRAY_SIZE_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003790 struct lyd_node_term *leaf;
3791 struct lysc_node_leaf *sleaf;
3792 struct lyd_value data = {0};
3793 struct ly_err_item *err = NULL;
3794 LY_ERR rc = LY_SUCCESS;
3795 int found;
3796
3797 if (options & LYXP_SCNODE_ALL) {
3798 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3799 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003800 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3801 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 +02003802 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_IDENT)) {
3803 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"identityref\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003804 }
3805
3806 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3807 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3808 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 +02003809 } else if (!warn_is_string_type(sleaf->type)) {
3810 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003811 }
3812 }
3813 set_scnode_clear_ctx(set);
3814 return rc;
3815 }
3816
Michal Vaskod3678892020-05-21 10:06:58 +02003817 if (args[0]->type != LYXP_SET_NODE_SET) {
3818 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
3819 "derived-from-or-self(node-set, string)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003820 return LY_EVALID;
3821 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003822 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003823 LY_CHECK_RET(rc);
3824
3825 set_fill_boolean(set, 0);
Michal Vaskod3678892020-05-21 10:06:58 +02003826 found = 0;
3827 for (i = 0; i < args[0]->used; ++i) {
3828 leaf = (struct lyd_node_term *)args[0]->val.nodes[i].node;
3829 sleaf = (struct lysc_node_leaf *)leaf->schema;
3830 if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_IDENT)) {
3831 /* store args[1] into ident */
3832 rc = sleaf->type->plugin->store(set->ctx, sleaf->type, args[1]->val.str, strlen(args[1]->val.str),
3833 LY_TYPE_OPTS_STORE, lys_resolve_prefix, (void *)sleaf->dflt_mod, set->format,
3834 (struct lyd_node *)leaf, set->tree, &data, NULL, &err);
3835 if (err) {
3836 ly_err_print(err);
3837 ly_err_free(err);
3838 }
3839 LY_CHECK_RET(rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003840
Michal Vaskod3678892020-05-21 10:06:58 +02003841 if (data.ident == leaf->value.ident) {
3842 set_fill_boolean(set, 1);
3843 break;
3844 }
3845 LY_ARRAY_FOR(data.ident->derived, u) {
3846 if (data.ident->derived[u] == leaf->value.ident) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003847 set_fill_boolean(set, 1);
Michal Vaskod3678892020-05-21 10:06:58 +02003848 found = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003849 break;
3850 }
Michal Vaskod3678892020-05-21 10:06:58 +02003851 }
3852 if (found) {
3853 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003854 }
3855 }
3856 }
3857
3858 return LY_SUCCESS;
3859}
3860
3861/**
3862 * @brief Execute the YANG 1.1 enum-value(node-set) function. Returns LYXP_SET_NUMBER
3863 * with the integer value of the first node's enum value, otherwise NaN.
3864 *
3865 * @param[in] args Array of arguments.
3866 * @param[in] arg_count Count of elements in @p args.
3867 * @param[in,out] set Context and result set at the same time.
3868 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003869 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003870 */
3871static LY_ERR
3872xpath_enum_value(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3873{
3874 struct lyd_node_term *leaf;
3875 struct lysc_node_leaf *sleaf;
3876 LY_ERR rc = LY_SUCCESS;
3877
3878 if (options & LYXP_SCNODE_ALL) {
3879 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3880 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003881 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3882 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 +02003883 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_ENUM)) {
3884 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"enumeration\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003885 }
3886 set_scnode_clear_ctx(set);
3887 return rc;
3888 }
3889
Michal Vaskod3678892020-05-21 10:06:58 +02003890 if (args[0]->type != LYXP_SET_NODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003891 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "enum-value(node-set)");
3892 return LY_EVALID;
3893 }
3894
3895 set_fill_number(set, NAN);
Michal Vaskod3678892020-05-21 10:06:58 +02003896 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003897 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
3898 sleaf = (struct lysc_node_leaf *)leaf->schema;
3899 if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_ENUM)) {
3900 set_fill_number(set, leaf->value.enum_item->value);
3901 }
3902 }
3903
3904 return LY_SUCCESS;
3905}
3906
3907/**
3908 * @brief Execute the XPath false() function. Returns LYXP_SET_BOOLEAN
3909 * with false value.
3910 *
3911 * @param[in] args Array of arguments.
3912 * @param[in] arg_count Count of elements in @p args.
3913 * @param[in,out] set Context and result set at the same time.
3914 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003915 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003916 */
3917static LY_ERR
3918xpath_false(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3919{
3920 if (options & LYXP_SCNODE_ALL) {
3921 set_scnode_clear_ctx(set);
3922 return LY_SUCCESS;
3923 }
3924
3925 set_fill_boolean(set, 0);
3926 return LY_SUCCESS;
3927}
3928
3929/**
3930 * @brief Execute the XPath floor(number) function. Returns LYXP_SET_NUMBER
3931 * with the first argument floored (truncated).
3932 *
3933 * @param[in] args Array of arguments.
3934 * @param[in] arg_count Count of elements in @p args.
3935 * @param[in,out] set Context and result set at the same time.
3936 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003937 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003938 */
3939static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003940xpath_floor(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int UNUSED(options))
Michal Vasko03ff5a72019-09-11 13:49:33 +02003941{
3942 LY_ERR rc;
3943
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003944 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003945 LY_CHECK_RET(rc);
3946 if (isfinite(args[0]->val.num)) {
3947 set_fill_number(set, (long long)args[0]->val.num);
3948 }
3949
3950 return LY_SUCCESS;
3951}
3952
3953/**
3954 * @brief Execute the XPath lang(string) function. Returns LYXP_SET_BOOLEAN
3955 * whether the language of the text matches the one from the argument.
3956 *
3957 * @param[in] args Array of arguments.
3958 * @param[in] arg_count Count of elements in @p args.
3959 * @param[in,out] set Context and result set at the same time.
3960 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003961 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003962 */
3963static LY_ERR
3964xpath_lang(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3965{
3966 const struct lyd_node *node;
3967 struct lysc_node_leaf *sleaf;
Michal Vasko9f96a052020-03-10 09:41:45 +01003968 struct lyd_meta *meta = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003969 const char *val;
3970 int i, dynamic;
3971 LY_ERR rc = LY_SUCCESS;
3972
3973 if (options & LYXP_SCNODE_ALL) {
3974 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3975 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3976 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 +02003977 } else if (!warn_is_string_type(sleaf->type)) {
3978 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003979 }
3980 }
3981 set_scnode_clear_ctx(set);
3982 return rc;
3983 }
3984
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003985 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003986 LY_CHECK_RET(rc);
3987
Michal Vasko03ff5a72019-09-11 13:49:33 +02003988 if (set->type != LYXP_SET_NODE_SET) {
3989 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "lang(string)");
3990 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02003991 } else if (!set->used) {
3992 set_fill_boolean(set, 0);
3993 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003994 }
3995
3996 switch (set->val.nodes[0].type) {
3997 case LYXP_NODE_ELEM:
3998 case LYXP_NODE_TEXT:
3999 node = set->val.nodes[0].node;
4000 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004001 case LYXP_NODE_META:
4002 node = set->val.meta[0].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004003 break;
4004 default:
4005 /* nothing to do with roots */
4006 set_fill_boolean(set, 0);
4007 return LY_SUCCESS;
4008 }
4009
Michal Vasko9f96a052020-03-10 09:41:45 +01004010 /* find lang metadata */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004011 for (; node; node = (struct lyd_node *)node->parent) {
Michal Vasko9f96a052020-03-10 09:41:45 +01004012 for (meta = node->meta; meta; meta = meta->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004013 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004014 if (meta->name && !strcmp(meta->name, "lang") && !strcmp(meta->annotation->module->name, "xml")) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004015 break;
4016 }
4017 }
4018
Michal Vasko9f96a052020-03-10 09:41:45 +01004019 if (meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004020 break;
4021 }
4022 }
4023
4024 /* compare languages */
Michal Vasko9f96a052020-03-10 09:41:45 +01004025 if (!meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004026 set_fill_boolean(set, 0);
4027 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01004028 val = lyd_meta2str(meta, &dynamic);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004029 for (i = 0; args[0]->val.str[i]; ++i) {
4030 if (tolower(args[0]->val.str[i]) != tolower(val[i])) {
4031 set_fill_boolean(set, 0);
4032 break;
4033 }
4034 }
4035 if (!args[0]->val.str[i]) {
4036 if (!val[i] || (val[i] == '-')) {
4037 set_fill_boolean(set, 1);
4038 } else {
4039 set_fill_boolean(set, 0);
4040 }
4041 }
4042 if (dynamic) {
4043 free((char *)val);
4044 }
4045 }
4046
4047 return LY_SUCCESS;
4048}
4049
4050/**
4051 * @brief Execute the XPath last() function. Returns LYXP_SET_NUMBER
4052 * with the context size.
4053 *
4054 * @param[in] args Array of arguments.
4055 * @param[in] arg_count Count of elements in @p args.
4056 * @param[in,out] set Context and result set at the same time.
4057 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004058 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004059 */
4060static LY_ERR
4061xpath_last(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4062{
4063 if (options & LYXP_SCNODE_ALL) {
4064 set_scnode_clear_ctx(set);
4065 return LY_SUCCESS;
4066 }
4067
Michal Vasko03ff5a72019-09-11 13:49:33 +02004068 if (set->type != LYXP_SET_NODE_SET) {
4069 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "last()");
4070 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004071 } else if (!set->used) {
4072 set_fill_number(set, 0);
4073 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004074 }
4075
4076 set_fill_number(set, set->ctx_size);
4077 return LY_SUCCESS;
4078}
4079
4080/**
4081 * @brief Execute the XPath local-name(node-set?) function. Returns LYXP_SET_STRING
4082 * with the node name without namespace from the argument or the context.
4083 *
4084 * @param[in] args Array of arguments.
4085 * @param[in] arg_count Count of elements in @p args.
4086 * @param[in,out] set Context and result set at the same time.
4087 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004088 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004089 */
4090static LY_ERR
4091xpath_local_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
4092{
4093 struct lyxp_set_node *item;
4094 /* suppress unused variable warning */
4095 (void)options;
4096
4097 if (options & LYXP_SCNODE_ALL) {
4098 set_scnode_clear_ctx(set);
4099 return LY_SUCCESS;
4100 }
4101
4102 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004103 if (args[0]->type != LYXP_SET_NODE_SET) {
4104 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "local-name(node-set?)");
4105 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004106 } else if (!args[0]->used) {
4107 set_fill_string(set, "", 0);
4108 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004109 }
4110
4111 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004112 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004113
4114 item = &args[0]->val.nodes[0];
4115 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004116 if (set->type != LYXP_SET_NODE_SET) {
4117 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "local-name(node-set?)");
4118 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004119 } else if (!set->used) {
4120 set_fill_string(set, "", 0);
4121 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004122 }
4123
4124 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004125 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004126
4127 item = &set->val.nodes[0];
4128 }
4129
4130 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004131 case LYXP_NODE_NONE:
4132 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004133 case LYXP_NODE_ROOT:
4134 case LYXP_NODE_ROOT_CONFIG:
4135 case LYXP_NODE_TEXT:
4136 set_fill_string(set, "", 0);
4137 break;
4138 case LYXP_NODE_ELEM:
4139 set_fill_string(set, item->node->schema->name, strlen(item->node->schema->name));
4140 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004141 case LYXP_NODE_META:
4142 set_fill_string(set, ((struct lyd_meta *)item->node)->name, strlen(((struct lyd_meta *)item->node)->name));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004143 break;
4144 }
4145
4146 return LY_SUCCESS;
4147}
4148
4149/**
4150 * @brief Execute the XPath name(node-set?) function. Returns LYXP_SET_STRING
4151 * with the node name fully qualified (with namespace) from the argument or the context.
4152 *
4153 * @param[in] args Array of arguments.
4154 * @param[in] arg_count Count of elements in @p args.
4155 * @param[in,out] set Context and result set at the same time.
4156 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004157 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004158 */
4159static LY_ERR
4160xpath_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
4161{
4162 struct lyxp_set_node *item;
4163 struct lys_module *mod;
4164 char *str;
4165 const char *name;
4166 int rc;
4167
4168 if (options & LYXP_SCNODE_ALL) {
4169 set_scnode_clear_ctx(set);
4170 return LY_SUCCESS;
4171 }
4172
4173 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004174 if (args[0]->type != LYXP_SET_NODE_SET) {
4175 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "name(node-set?)");
4176 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004177 } else if (!args[0]->used) {
4178 set_fill_string(set, "", 0);
4179 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004180 }
4181
4182 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004183 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004184
4185 item = &args[0]->val.nodes[0];
4186 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004187 if (set->type != LYXP_SET_NODE_SET) {
4188 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "name(node-set?)");
4189 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004190 } else if (!set->used) {
4191 set_fill_string(set, "", 0);
4192 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004193 }
4194
4195 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004196 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004197
4198 item = &set->val.nodes[0];
4199 }
4200
4201 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004202 case LYXP_NODE_NONE:
4203 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004204 case LYXP_NODE_ROOT:
4205 case LYXP_NODE_ROOT_CONFIG:
4206 case LYXP_NODE_TEXT:
4207 mod = NULL;
4208 name = NULL;
4209 break;
4210 case LYXP_NODE_ELEM:
4211 mod = item->node->schema->module;
4212 name = item->node->schema->name;
4213 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004214 case LYXP_NODE_META:
4215 mod = ((struct lyd_meta *)item->node)->annotation->module;
4216 name = ((struct lyd_meta *)item->node)->name;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004217 break;
4218 }
4219
4220 if (mod && name) {
4221 switch (set->format) {
Michal Vasko52927e22020-03-16 17:26:14 +01004222 case LYD_SCHEMA:
Michal Vasko03ff5a72019-09-11 13:49:33 +02004223 rc = asprintf(&str, "%s:%s", lys_prefix_find_module(set->local_mod, mod), name);
4224 break;
4225 case LYD_JSON:
4226 rc = asprintf(&str, "%s:%s", mod->name, name);
4227 break;
Michal Vasko52927e22020-03-16 17:26:14 +01004228 case LYD_XML:
Michal Vasko9409ef62019-09-12 11:47:17 +02004229 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004230 }
4231 LY_CHECK_ERR_RET(rc == -1, LOGMEM(set->ctx), LY_EMEM);
4232 set_fill_string(set, str, strlen(str));
4233 free(str);
4234 } else {
4235 set_fill_string(set, "", 0);
4236 }
4237
4238 return LY_SUCCESS;
4239}
4240
4241/**
4242 * @brief Execute the XPath namespace-uri(node-set?) function. Returns LYXP_SET_STRING
4243 * with the namespace of the node from the argument or the context.
4244 *
4245 * @param[in] args Array of arguments.
4246 * @param[in] arg_count Count of elements in @p args.
4247 * @param[in,out] set Context and result set at the same time.
4248 * @param[in] options XPath options.
4249 * @return LY_ERR (LY_EINVAL for wrong arguments on schema)
4250 */
4251static LY_ERR
4252xpath_namespace_uri(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
4253{
4254 struct lyxp_set_node *item;
4255 struct lys_module *mod;
4256 /* suppress unused variable warning */
4257 (void)options;
4258
4259 if (options & LYXP_SCNODE_ALL) {
4260 set_scnode_clear_ctx(set);
4261 return LY_SUCCESS;
4262 }
4263
4264 if (arg_count) {
Michal Vaskod3678892020-05-21 10:06:58 +02004265 if (args[0]->type != LYXP_SET_NODE_SET) {
4266 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
4267 "namespace-uri(node-set?)");
4268 return LY_EVALID;
4269 } else if (!args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004270 set_fill_string(set, "", 0);
4271 return LY_SUCCESS;
4272 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004273
4274 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004275 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004276
4277 item = &args[0]->val.nodes[0];
4278 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004279 if (set->type != LYXP_SET_NODE_SET) {
4280 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "namespace-uri(node-set?)");
4281 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004282 } else if (!set->used) {
4283 set_fill_string(set, "", 0);
4284 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004285 }
4286
4287 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004288 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004289
4290 item = &set->val.nodes[0];
4291 }
4292
4293 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004294 case LYXP_NODE_NONE:
4295 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004296 case LYXP_NODE_ROOT:
4297 case LYXP_NODE_ROOT_CONFIG:
4298 case LYXP_NODE_TEXT:
4299 set_fill_string(set, "", 0);
4300 break;
4301 case LYXP_NODE_ELEM:
Michal Vasko9f96a052020-03-10 09:41:45 +01004302 case LYXP_NODE_META:
Michal Vasko03ff5a72019-09-11 13:49:33 +02004303 if (item->type == LYXP_NODE_ELEM) {
4304 mod = item->node->schema->module;
Michal Vasko9f96a052020-03-10 09:41:45 +01004305 } else { /* LYXP_NODE_META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004306 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004307 mod = ((struct lyd_meta *)item->node)->annotation->module;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004308 }
4309
4310 set_fill_string(set, mod->ns, strlen(mod->ns));
4311 break;
4312 }
4313
4314 return LY_SUCCESS;
4315}
4316
4317/**
4318 * @brief Execute the XPath node() function (node type). Returns LYXP_SET_NODE_SET
4319 * with only nodes from the context. In practice it either leaves the context
4320 * as it is or returns an empty node set.
4321 *
4322 * @param[in] args Array of arguments.
4323 * @param[in] arg_count Count of elements in @p args.
4324 * @param[in,out] set Context and result set at the same time.
4325 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004326 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004327 */
4328static LY_ERR
4329xpath_node(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4330{
4331 if (options & LYXP_SCNODE_ALL) {
4332 set_scnode_clear_ctx(set);
4333 return LY_SUCCESS;
4334 }
4335
4336 if (set->type != LYXP_SET_NODE_SET) {
Michal Vaskod3678892020-05-21 10:06:58 +02004337 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004338 }
4339 return LY_SUCCESS;
4340}
4341
4342/**
4343 * @brief Execute the XPath normalize-space(string?) function. Returns LYXP_SET_STRING
4344 * with normalized value (no leading, trailing, double white spaces) of the node
4345 * from the argument or the context.
4346 *
4347 * @param[in] args Array of arguments.
4348 * @param[in] arg_count Count of elements in @p args.
4349 * @param[in,out] set Context and result set at the same time.
4350 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004351 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004352 */
4353static LY_ERR
4354xpath_normalize_space(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
4355{
4356 uint16_t i, new_used;
4357 char *new;
4358 int have_spaces = 0, space_before = 0;
4359 struct lysc_node_leaf *sleaf;
4360 LY_ERR rc = LY_SUCCESS;
4361
4362 if (options & LYXP_SCNODE_ALL) {
4363 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4364 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4365 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 +02004366 } else if (!warn_is_string_type(sleaf->type)) {
4367 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004368 }
4369 }
4370 set_scnode_clear_ctx(set);
4371 return rc;
4372 }
4373
4374 if (arg_count) {
4375 set_fill_set(set, args[0]);
4376 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004377 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004378 LY_CHECK_RET(rc);
4379
4380 /* is there any normalization necessary? */
4381 for (i = 0; set->val.str[i]; ++i) {
4382 if (is_xmlws(set->val.str[i])) {
4383 if ((i == 0) || space_before || (!set->val.str[i + 1])) {
4384 have_spaces = 1;
4385 break;
4386 }
4387 space_before = 1;
4388 } else {
4389 space_before = 0;
4390 }
4391 }
4392
4393 /* yep, there is */
4394 if (have_spaces) {
4395 /* it's enough, at least one character will go, makes space for ending '\0' */
4396 new = malloc(strlen(set->val.str) * sizeof(char));
4397 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4398 new_used = 0;
4399
4400 space_before = 0;
4401 for (i = 0; set->val.str[i]; ++i) {
4402 if (is_xmlws(set->val.str[i])) {
4403 if ((i == 0) || space_before) {
4404 space_before = 1;
4405 continue;
4406 } else {
4407 space_before = 1;
4408 }
4409 } else {
4410 space_before = 0;
4411 }
4412
4413 new[new_used] = (space_before ? ' ' : set->val.str[i]);
4414 ++new_used;
4415 }
4416
4417 /* at worst there is one trailing space now */
4418 if (new_used && is_xmlws(new[new_used - 1])) {
4419 --new_used;
4420 }
4421
4422 new = ly_realloc(new, (new_used + 1) * sizeof(char));
4423 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4424 new[new_used] = '\0';
4425
4426 free(set->val.str);
4427 set->val.str = new;
4428 }
4429
4430 return LY_SUCCESS;
4431}
4432
4433/**
4434 * @brief Execute the XPath not(boolean) function. Returns LYXP_SET_BOOLEAN
4435 * with the argument converted to boolean and logically inverted.
4436 *
4437 * @param[in] args Array of arguments.
4438 * @param[in] arg_count Count of elements in @p args.
4439 * @param[in,out] set Context and result set at the same time.
4440 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004441 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004442 */
4443static LY_ERR
4444xpath_not(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4445{
4446 if (options & LYXP_SCNODE_ALL) {
4447 set_scnode_clear_ctx(set);
4448 return LY_SUCCESS;
4449 }
4450
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004451 lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02004452 if (args[0]->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004453 set_fill_boolean(set, 0);
4454 } else {
4455 set_fill_boolean(set, 1);
4456 }
4457
4458 return LY_SUCCESS;
4459}
4460
4461/**
4462 * @brief Execute the XPath number(object?) function. Returns LYXP_SET_NUMBER
4463 * with the number representation of either the argument or the context.
4464 *
4465 * @param[in] args Array of arguments.
4466 * @param[in] arg_count Count of elements in @p args.
4467 * @param[in,out] set Context and result set at the same time.
4468 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004469 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004470 */
4471static LY_ERR
4472xpath_number(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
4473{
4474 LY_ERR rc;
4475
4476 if (options & LYXP_SCNODE_ALL) {
4477 set_scnode_clear_ctx(set);
4478 return LY_SUCCESS;
4479 }
4480
4481 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004482 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004483 LY_CHECK_RET(rc);
4484 set_fill_set(set, args[0]);
4485 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004486 rc = lyxp_set_cast(set, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004487 LY_CHECK_RET(rc);
4488 }
4489
4490 return LY_SUCCESS;
4491}
4492
4493/**
4494 * @brief Execute the XPath position() function. Returns LYXP_SET_NUMBER
4495 * with the context position.
4496 *
4497 * @param[in] args Array of arguments.
4498 * @param[in] arg_count Count of elements in @p args.
4499 * @param[in,out] set Context and result set at the same time.
4500 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004501 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004502 */
4503static LY_ERR
4504xpath_position(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4505{
4506 if (options & LYXP_SCNODE_ALL) {
4507 set_scnode_clear_ctx(set);
4508 return LY_SUCCESS;
4509 }
4510
Michal Vasko03ff5a72019-09-11 13:49:33 +02004511 if (set->type != LYXP_SET_NODE_SET) {
4512 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "position()");
4513 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004514 } else if (!set->used) {
4515 set_fill_number(set, 0);
4516 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004517 }
4518
4519 set_fill_number(set, set->ctx_pos);
4520
4521 /* UNUSED in 'Release' build type */
4522 (void)options;
4523 return LY_SUCCESS;
4524}
4525
4526/**
4527 * @brief Execute the YANG 1.1 re-match(string, string) function. Returns LYXP_SET_BOOLEAN
4528 * depending on whether the second argument regex matches the first argument string. For details refer to
4529 * YANG 1.1 RFC section 10.2.1.
4530 *
4531 * @param[in] args Array of arguments.
4532 * @param[in] arg_count Count of elements in @p args.
4533 * @param[in,out] set Context and result set at the same time.
4534 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004535 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004536 */
4537static LY_ERR
4538xpath_re_match(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4539{
4540 struct lysc_pattern **patterns = NULL, **pattern;
4541 struct lysc_node_leaf *sleaf;
4542 char *path;
4543 LY_ERR rc = LY_SUCCESS;
4544 struct ly_err_item *err;
4545
4546 if (options & LYXP_SCNODE_ALL) {
4547 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4548 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4549 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 +02004550 } else if (!warn_is_string_type(sleaf->type)) {
4551 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004552 }
4553 }
4554
4555 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4556 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4557 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 +02004558 } else if (!warn_is_string_type(sleaf->type)) {
4559 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004560 }
4561 }
4562 set_scnode_clear_ctx(set);
4563 return rc;
4564 }
4565
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004566 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004567 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004568 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004569 LY_CHECK_RET(rc);
4570
4571 LY_ARRAY_NEW_RET(set->ctx, patterns, pattern, LY_EMEM);
4572 *pattern = malloc(sizeof **pattern);
4573 path = lyd_path(set->ctx_node, LYD_PATH_LOG, NULL, 0);
4574 rc = lys_compile_type_pattern_check(set->ctx, path, args[1]->val.str, &(*pattern)->code);
4575 free(path);
4576 if (rc != LY_SUCCESS) {
4577 LY_ARRAY_FREE(patterns);
4578 return rc;
4579 }
4580
4581 rc = ly_type_validate_patterns(patterns, args[0]->val.str, strlen(args[0]->val.str), &err);
4582 pcre2_code_free((*pattern)->code);
4583 free(*pattern);
4584 LY_ARRAY_FREE(patterns);
4585 if (rc && (rc != LY_EVALID)) {
4586 ly_err_print(err);
4587 ly_err_free(err);
4588 return rc;
4589 }
4590
4591 if (rc == LY_EVALID) {
4592 ly_err_free(err);
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 round(number) function. Returns LYXP_SET_NUMBER
4603 * with the rounded first argument. For details refer to
4604 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-round.
4605 *
4606 * @param[in] args Array of arguments.
4607 * @param[in] arg_count Count of elements in @p args.
4608 * @param[in,out] set Context and result set at the same time.
4609 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004610 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004611 */
4612static LY_ERR
4613xpath_round(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4614{
4615 struct lysc_node_leaf *sleaf;
4616 LY_ERR rc = LY_SUCCESS;
4617
4618 if (options & LYXP_SCNODE_ALL) {
4619 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4620 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004621 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4622 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 +02004623 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
4624 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004625 }
4626 set_scnode_clear_ctx(set);
4627 return rc;
4628 }
4629
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004630 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004631 LY_CHECK_RET(rc);
4632
4633 /* cover only the cases where floor can't be used */
4634 if ((args[0]->val.num == -0.0f) || ((args[0]->val.num < 0) && (args[0]->val.num >= -0.5))) {
4635 set_fill_number(set, -0.0f);
4636 } else {
4637 args[0]->val.num += 0.5;
4638 rc = xpath_floor(args, 1, args[0], options);
4639 LY_CHECK_RET(rc);
4640 set_fill_number(set, args[0]->val.num);
4641 }
4642
4643 return LY_SUCCESS;
4644}
4645
4646/**
4647 * @brief Execute the XPath starts-with(string, string) function.
4648 * Returns LYXP_SET_BOOLEAN whether the second argument is
4649 * the prefix of the first or not.
4650 *
4651 * @param[in] args Array of arguments.
4652 * @param[in] arg_count Count of elements in @p args.
4653 * @param[in,out] set Context and result set at the same time.
4654 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004655 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004656 */
4657static LY_ERR
4658xpath_starts_with(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4659{
4660 struct lysc_node_leaf *sleaf;
4661 LY_ERR rc = LY_SUCCESS;
4662
4663 if (options & LYXP_SCNODE_ALL) {
4664 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4665 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4666 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 +02004667 } else if (!warn_is_string_type(sleaf->type)) {
4668 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004669 }
4670 }
4671
4672 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4673 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4674 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 +02004675 } else if (!warn_is_string_type(sleaf->type)) {
4676 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004677 }
4678 }
4679 set_scnode_clear_ctx(set);
4680 return rc;
4681 }
4682
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004683 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004684 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004685 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004686 LY_CHECK_RET(rc);
4687
4688 if (strncmp(args[0]->val.str, args[1]->val.str, strlen(args[1]->val.str))) {
4689 set_fill_boolean(set, 0);
4690 } else {
4691 set_fill_boolean(set, 1);
4692 }
4693
4694 return LY_SUCCESS;
4695}
4696
4697/**
4698 * @brief Execute the XPath string(object?) function. Returns LYXP_SET_STRING
4699 * with the string representation of either the argument or the context.
4700 *
4701 * @param[in] args Array of arguments.
4702 * @param[in] arg_count Count of elements in @p args.
4703 * @param[in,out] set Context and result set at the same time.
4704 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004705 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004706 */
4707static LY_ERR
4708xpath_string(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
4709{
4710 LY_ERR rc;
4711
4712 if (options & LYXP_SCNODE_ALL) {
4713 set_scnode_clear_ctx(set);
4714 return LY_SUCCESS;
4715 }
4716
4717 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004718 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004719 LY_CHECK_RET(rc);
4720 set_fill_set(set, args[0]);
4721 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004722 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004723 LY_CHECK_RET(rc);
4724 }
4725
4726 return LY_SUCCESS;
4727}
4728
4729/**
4730 * @brief Execute the XPath string-length(string?) function. Returns LYXP_SET_NUMBER
4731 * with the length of the string in either the argument or the context.
4732 *
4733 * @param[in] args Array of arguments.
4734 * @param[in] arg_count Count of elements in @p args.
4735 * @param[in,out] set Context and result set at the same time.
4736 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004737 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004738 */
4739static LY_ERR
4740xpath_string_length(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
4741{
4742 struct lysc_node_leaf *sleaf;
4743 LY_ERR rc = LY_SUCCESS;
4744
4745 if (options & LYXP_SCNODE_ALL) {
4746 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4747 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4748 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 +02004749 } else if (!warn_is_string_type(sleaf->type)) {
4750 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004751 }
4752 }
4753 if (!arg_count && (set->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set))) {
4754 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4755 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 +02004756 } else if (!warn_is_string_type(sleaf->type)) {
4757 LOGWRN(set->ctx, "Argument #0 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004758 }
4759 }
4760 set_scnode_clear_ctx(set);
4761 return rc;
4762 }
4763
4764 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004765 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004766 LY_CHECK_RET(rc);
4767 set_fill_number(set, strlen(args[0]->val.str));
4768 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004769 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004770 LY_CHECK_RET(rc);
4771 set_fill_number(set, strlen(set->val.str));
4772 }
4773
4774 return LY_SUCCESS;
4775}
4776
4777/**
4778 * @brief Execute the XPath substring(string, number, number?) function.
4779 * Returns LYXP_SET_STRING substring of the first argument starting
4780 * on the second argument index ending on the third argument index,
4781 * indexed from 1. For exact definition refer to
4782 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-substring.
4783 *
4784 * @param[in] args Array of arguments.
4785 * @param[in] arg_count Count of elements in @p args.
4786 * @param[in,out] set Context and result set at the same time.
4787 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004788 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004789 */
4790static LY_ERR
4791xpath_substring(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
4792{
4793 int start, len;
4794 uint16_t str_start, str_len, pos;
4795 struct lysc_node_leaf *sleaf;
4796 LY_ERR rc = LY_SUCCESS;
4797
4798 if (options & LYXP_SCNODE_ALL) {
4799 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4800 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4801 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 +02004802 } else if (!warn_is_string_type(sleaf->type)) {
4803 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004804 }
4805 }
4806
4807 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4808 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4809 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 +02004810 } else if (!warn_is_numeric_type(sleaf->type)) {
4811 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004812 }
4813 }
4814
4815 if ((arg_count == 3) && (args[2]->type == LYXP_SET_SCNODE_SET)
4816 && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
4817 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4818 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 +02004819 } else if (!warn_is_numeric_type(sleaf->type)) {
4820 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004821 }
4822 }
4823 set_scnode_clear_ctx(set);
4824 return rc;
4825 }
4826
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004827 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004828 LY_CHECK_RET(rc);
4829
4830 /* start */
4831 if (xpath_round(&args[1], 1, args[1], options)) {
4832 return -1;
4833 }
4834 if (isfinite(args[1]->val.num)) {
4835 start = args[1]->val.num - 1;
4836 } else if (isinf(args[1]->val.num) && signbit(args[1]->val.num)) {
4837 start = INT_MIN;
4838 } else {
4839 start = INT_MAX;
4840 }
4841
4842 /* len */
4843 if (arg_count == 3) {
4844 rc = xpath_round(&args[2], 1, args[2], options);
4845 LY_CHECK_RET(rc);
4846 if (isfinite(args[2]->val.num)) {
4847 len = args[2]->val.num;
4848 } else if (isnan(args[2]->val.num) || signbit(args[2]->val.num)) {
4849 len = 0;
4850 } else {
4851 len = INT_MAX;
4852 }
4853 } else {
4854 len = INT_MAX;
4855 }
4856
4857 /* find matching character positions */
4858 str_start = 0;
4859 str_len = 0;
4860 for (pos = 0; args[0]->val.str[pos]; ++pos) {
4861 if (pos < start) {
4862 ++str_start;
4863 } else if (pos < start + len) {
4864 ++str_len;
4865 } else {
4866 break;
4867 }
4868 }
4869
4870 set_fill_string(set, args[0]->val.str + str_start, str_len);
4871 return LY_SUCCESS;
4872}
4873
4874/**
4875 * @brief Execute the XPath substring-after(string, string) function.
4876 * Returns LYXP_SET_STRING with the string succeeding the occurance
4877 * of the second argument in the first or an empty string.
4878 *
4879 * @param[in] args Array of arguments.
4880 * @param[in] arg_count Count of elements in @p args.
4881 * @param[in,out] set Context and result set at the same time.
4882 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004883 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004884 */
4885static LY_ERR
4886xpath_substring_after(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4887{
4888 char *ptr;
4889 struct lysc_node_leaf *sleaf;
4890 LY_ERR rc = LY_SUCCESS;
4891
4892 if (options & LYXP_SCNODE_ALL) {
4893 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4894 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4895 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 +02004896 } else if (!warn_is_string_type(sleaf->type)) {
4897 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004898 }
4899 }
4900
4901 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4902 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4903 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 +02004904 } else if (!warn_is_string_type(sleaf->type)) {
4905 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004906 }
4907 }
4908 set_scnode_clear_ctx(set);
4909 return rc;
4910 }
4911
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004912 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004913 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004914 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004915 LY_CHECK_RET(rc);
4916
4917 ptr = strstr(args[0]->val.str, args[1]->val.str);
4918 if (ptr) {
4919 set_fill_string(set, ptr + strlen(args[1]->val.str), strlen(ptr + strlen(args[1]->val.str)));
4920 } else {
4921 set_fill_string(set, "", 0);
4922 }
4923
4924 return LY_SUCCESS;
4925}
4926
4927/**
4928 * @brief Execute the XPath substring-before(string, string) function.
4929 * Returns LYXP_SET_STRING with the string preceding the occurance
4930 * of the second argument in the first or an empty string.
4931 *
4932 * @param[in] args Array of arguments.
4933 * @param[in] arg_count Count of elements in @p args.
4934 * @param[in,out] set Context and result set at the same time.
4935 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004936 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004937 */
4938static LY_ERR
4939xpath_substring_before(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4940{
4941 char *ptr;
4942 struct lysc_node_leaf *sleaf;
4943 LY_ERR rc = LY_SUCCESS;
4944
4945 if (options & LYXP_SCNODE_ALL) {
4946 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4947 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4948 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 +02004949 } else if (!warn_is_string_type(sleaf->type)) {
4950 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004951 }
4952 }
4953
4954 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4955 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4956 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 +02004957 } else if (!warn_is_string_type(sleaf->type)) {
4958 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004959 }
4960 }
4961 set_scnode_clear_ctx(set);
4962 return rc;
4963 }
4964
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004965 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004966 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004967 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004968 LY_CHECK_RET(rc);
4969
4970 ptr = strstr(args[0]->val.str, args[1]->val.str);
4971 if (ptr) {
4972 set_fill_string(set, args[0]->val.str, ptr - args[0]->val.str);
4973 } else {
4974 set_fill_string(set, "", 0);
4975 }
4976
4977 return LY_SUCCESS;
4978}
4979
4980/**
4981 * @brief Execute the XPath sum(node-set) function. Returns LYXP_SET_NUMBER
4982 * with the sum of all the nodes in the context.
4983 *
4984 * @param[in] args Array of arguments.
4985 * @param[in] arg_count Count of elements in @p args.
4986 * @param[in,out] set Context and result set at the same time.
4987 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004988 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004989 */
4990static LY_ERR
4991xpath_sum(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4992{
4993 long double num;
4994 char *str;
4995 uint16_t i;
4996 struct lyxp_set set_item;
4997 struct lysc_node_leaf *sleaf;
4998 LY_ERR rc = LY_SUCCESS;
4999
5000 if (options & LYXP_SCNODE_ALL) {
5001 if (args[0]->type == LYXP_SET_SCNODE_SET) {
5002 for (i = 0; i < args[0]->used; ++i) {
5003 if (args[0]->val.scnodes[i].in_ctx == 1) {
5004 sleaf = (struct lysc_node_leaf *)args[0]->val.scnodes[i].scnode;
5005 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5006 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__,
5007 lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005008 } else if (!warn_is_numeric_type(sleaf->type)) {
5009 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005010 }
5011 }
5012 }
5013 }
5014 set_scnode_clear_ctx(set);
5015 return rc;
5016 }
5017
5018 set_fill_number(set, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005019
5020 if (args[0]->type != LYXP_SET_NODE_SET) {
5021 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "sum(node-set)");
5022 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02005023 } else if (!args[0]->used) {
5024 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005025 }
5026
Michal Vasko5c4e5892019-11-14 12:31:38 +01005027 set_init(&set_item, set);
5028
Michal Vasko03ff5a72019-09-11 13:49:33 +02005029 set_item.type = LYXP_SET_NODE_SET;
5030 set_item.val.nodes = malloc(sizeof *set_item.val.nodes);
5031 LY_CHECK_ERR_RET(!set_item.val.nodes, LOGMEM(set->ctx), LY_EMEM);
5032
5033 set_item.used = 1;
5034 set_item.size = 1;
5035
5036 for (i = 0; i < args[0]->used; ++i) {
5037 set_item.val.nodes[0] = args[0]->val.nodes[i];
5038
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005039 rc = cast_node_set_to_string(&set_item, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005040 LY_CHECK_RET(rc);
5041 num = cast_string_to_number(str);
5042 free(str);
5043 set->val.num += num;
5044 }
5045
5046 free(set_item.val.nodes);
5047
5048 return LY_SUCCESS;
5049}
5050
5051/**
5052 * @brief Execute the XPath text() function (node type). Returns LYXP_SET_NODE_SET
5053 * with the text content of the nodes in the context.
5054 *
5055 * @param[in] args Array of arguments.
5056 * @param[in] arg_count Count of elements in @p args.
5057 * @param[in,out] set Context and result set at the same time.
5058 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005059 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005060 */
5061static LY_ERR
5062xpath_text(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
5063{
5064 uint32_t i;
5065
5066 if (options & LYXP_SCNODE_ALL) {
5067 set_scnode_clear_ctx(set);
5068 return LY_SUCCESS;
5069 }
5070
Michal Vasko03ff5a72019-09-11 13:49:33 +02005071 if (set->type != LYXP_SET_NODE_SET) {
5072 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "text()");
5073 return LY_EVALID;
5074 }
5075
5076 for (i = 0; i < set->used;) {
5077 switch (set->val.nodes[i].type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01005078 case LYXP_NODE_NONE:
5079 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005080 case LYXP_NODE_ELEM:
Michal Vasko03ff5a72019-09-11 13:49:33 +02005081 if (set->val.nodes[i].node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
5082 set->val.nodes[i].type = LYXP_NODE_TEXT;
5083 ++i;
5084 break;
5085 }
5086 /* fall through */
5087 case LYXP_NODE_ROOT:
5088 case LYXP_NODE_ROOT_CONFIG:
5089 case LYXP_NODE_TEXT:
Michal Vasko9f96a052020-03-10 09:41:45 +01005090 case LYXP_NODE_META:
Michal Vasko03ff5a72019-09-11 13:49:33 +02005091 set_remove_node(set, i);
5092 break;
5093 }
5094 }
5095
5096 return LY_SUCCESS;
5097}
5098
5099/**
5100 * @brief Execute the XPath translate(string, string, string) function.
5101 * Returns LYXP_SET_STRING with the first argument with the characters
5102 * from the second argument replaced by those on the corresponding
5103 * positions in the third argument.
5104 *
5105 * @param[in] args Array of arguments.
5106 * @param[in] arg_count Count of elements in @p args.
5107 * @param[in,out] set Context and result set at the same time.
5108 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005109 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005110 */
5111static LY_ERR
5112xpath_translate(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
5113{
5114 uint16_t i, j, new_used;
5115 char *new;
5116 int found, have_removed;
5117 struct lysc_node_leaf *sleaf;
5118 LY_ERR rc = LY_SUCCESS;
5119
5120 if (options & LYXP_SCNODE_ALL) {
5121 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5122 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5123 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 +02005124 } else if (!warn_is_string_type(sleaf->type)) {
5125 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005126 }
5127 }
5128
5129 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5130 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5131 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 +02005132 } else if (!warn_is_string_type(sleaf->type)) {
5133 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005134 }
5135 }
5136
5137 if ((args[2]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
5138 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5139 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 +02005140 } else if (!warn_is_string_type(sleaf->type)) {
5141 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005142 }
5143 }
5144 set_scnode_clear_ctx(set);
5145 return rc;
5146 }
5147
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005148 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005149 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005150 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005151 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005152 rc = lyxp_set_cast(args[2], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005153 LY_CHECK_RET(rc);
5154
5155 new = malloc((strlen(args[0]->val.str) + 1) * sizeof(char));
5156 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5157 new_used = 0;
5158
5159 have_removed = 0;
5160 for (i = 0; args[0]->val.str[i]; ++i) {
5161 found = 0;
5162
5163 for (j = 0; args[1]->val.str[j]; ++j) {
5164 if (args[0]->val.str[i] == args[1]->val.str[j]) {
5165 /* removing this char */
5166 if (j >= strlen(args[2]->val.str)) {
5167 have_removed = 1;
5168 found = 1;
5169 break;
5170 }
5171 /* replacing this char */
5172 new[new_used] = args[2]->val.str[j];
5173 ++new_used;
5174 found = 1;
5175 break;
5176 }
5177 }
5178
5179 /* copying this char */
5180 if (!found) {
5181 new[new_used] = args[0]->val.str[i];
5182 ++new_used;
5183 }
5184 }
5185
5186 if (have_removed) {
5187 new = ly_realloc(new, (new_used + 1) * sizeof(char));
5188 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5189 }
5190 new[new_used] = '\0';
5191
Michal Vaskod3678892020-05-21 10:06:58 +02005192 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005193 set->type = LYXP_SET_STRING;
5194 set->val.str = new;
5195
5196 return LY_SUCCESS;
5197}
5198
5199/**
5200 * @brief Execute the XPath true() function. Returns LYXP_SET_BOOLEAN
5201 * with true value.
5202 *
5203 * @param[in] args Array of arguments.
5204 * @param[in] arg_count Count of elements in @p args.
5205 * @param[in,out] set Context and result set at the same time.
5206 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005207 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005208 */
5209static LY_ERR
5210xpath_true(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
5211{
5212 if (options & LYXP_SCNODE_ALL) {
5213 set_scnode_clear_ctx(set);
5214 return LY_SUCCESS;
5215 }
5216
5217 set_fill_boolean(set, 1);
5218 return LY_SUCCESS;
5219}
5220
5221/*
5222 * moveto functions
5223 *
5224 * They and only they actually change the context (set).
5225 */
5226
5227/**
Michal Vasko6346ece2019-09-24 13:12:53 +02005228 * @brief Skip prefix and return corresponding model if there is a prefix. Logs directly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005229 *
Michal Vasko2104e9f2020-03-06 08:23:25 +01005230 * XPath @p set is expected to be a (sc)node set!
5231 *
Michal Vasko6346ece2019-09-24 13:12:53 +02005232 * @param[in,out] qname Qualified node name. If includes prefix, it is skipped.
5233 * @param[in,out] qname_len Length of @p qname, is updated accordingly.
5234 * @param[in] set Set with XPath context.
5235 * @param[out] moveto_mod Expected module of a matching node.
5236 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005237 */
Michal Vasko6346ece2019-09-24 13:12:53 +02005238static LY_ERR
5239moveto_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 +02005240{
Michal Vasko6346ece2019-09-24 13:12:53 +02005241 const struct lys_module *mod;
5242 const char *ptr;
5243 int pref_len;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005244 char *str;
5245
Michal Vasko2104e9f2020-03-06 08:23:25 +01005246 assert((set->type == LYXP_SET_NODE_SET) || (set->type == LYXP_SET_SCNODE_SET));
5247
Michal Vasko6346ece2019-09-24 13:12:53 +02005248 if ((ptr = ly_strnchr(*qname, ':', *qname_len))) {
5249 /* specific module */
5250 pref_len = ptr - *qname;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005251
Michal Vasko6346ece2019-09-24 13:12:53 +02005252 switch (set->format) {
Michal Vasko52927e22020-03-16 17:26:14 +01005253 case LYD_SCHEMA:
Michal Vasko6346ece2019-09-24 13:12:53 +02005254 /* schema, search all local module imports */
5255 mod = lys_module_find_prefix(set->local_mod, *qname, pref_len);
5256 break;
5257 case LYD_JSON:
5258 /* JSON data, search in context */
5259 str = strndup(*qname, pref_len);
Michal Vasko97e76fd2020-05-27 15:22:01 +02005260 mod = ly_ctx_get_module_implemented(set->ctx, str);
Michal Vasko6346ece2019-09-24 13:12:53 +02005261 free(str);
5262 break;
Michal Vasko52927e22020-03-16 17:26:14 +01005263 case LYD_XML:
Michal Vasko6346ece2019-09-24 13:12:53 +02005264 LOGINT_RET(set->ctx);
5265 }
5266
Michal Vasko004d3152020-06-11 19:59:22 +02005267 /* check for errors and non-implemented modules, as they are not valid */
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005268 if (!mod || !mod->implemented) {
Michal Vasko2104e9f2020-03-06 08:23:25 +01005269 if (set->type == LYXP_SET_SCNODE_SET) {
5270 LOGVAL(set->ctx, LY_VLOG_LYSC, set->ctx_scnode, LY_VCODE_XP_INMOD, pref_len, *qname);
5271 } else {
5272 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INMOD, pref_len, *qname);
5273 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005274 return LY_EVALID;
5275 }
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005276
Michal Vasko6346ece2019-09-24 13:12:53 +02005277 *qname += pref_len + 1;
5278 *qname_len -= pref_len + 1;
5279 } else if (((*qname)[0] == '*') && (*qname_len == 1)) {
5280 /* all modules - special case */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005281 mod = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02005282 } else if (set->type == LYXP_SET_SCNODE_SET) {
5283 /* current node module */
5284 mod = set->ctx_scnode->module;
Michal Vasko6346ece2019-09-24 13:12:53 +02005285 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02005286 /* current node module */
5287 mod = set->ctx_node->schema->module;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005288 }
5289
Michal Vasko6346ece2019-09-24 13:12:53 +02005290 *moveto_mod = mod;
5291 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005292}
5293
5294/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02005295 * @brief Move context @p set to the root. Handles absolute path.
5296 * Result is LYXP_SET_NODE_SET.
5297 *
5298 * @param[in,out] set Set to use.
5299 * @param[in] options Xpath options.
5300 */
5301static void
5302moveto_root(struct lyxp_set *set, int options)
5303{
Michal Vasko03ff5a72019-09-11 13:49:33 +02005304 if (!set) {
5305 return;
5306 }
5307
5308 if (options & LYXP_SCNODE_ALL) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005309 set_scnode_clear_ctx(set);
Michal Vaskoecd62de2019-11-13 12:35:11 +01005310 lyxp_set_scnode_insert_node(set, NULL, set->root_type);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005311 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005312 set->type = LYXP_SET_NODE_SET;
5313 set->used = 0;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005314 set_insert_node(set, NULL, 0, set->root_type, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005315 }
5316}
5317
5318/**
Michal Vaskoa1424542019-11-14 16:08:52 +01005319 * @brief Check whether a node has some unresolved "when".
5320 *
5321 * @param[in] node Node to check.
5322 * @return LY_ERR value (LY_EINCOMPLETE if there are some unresolved "when")
5323 */
5324static LY_ERR
5325moveto_when_check(const struct lyd_node *node)
5326{
5327 const struct lysc_node *schema;
5328
5329 if (!node) {
5330 return LY_SUCCESS;
5331 }
5332
5333 schema = node->schema;
5334 do {
5335 if (schema->when && !(node->flags & LYD_WHEN_TRUE)) {
5336 return LY_EINCOMPLETE;
5337 }
5338 schema = schema->parent;
5339 } while (schema && (schema->nodetype & (LYS_CASE | LYS_CHOICE)));
5340
5341 return LY_SUCCESS;
5342}
5343
5344/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02005345 * @brief Check @p node as a part of NameTest processing.
5346 *
5347 * @param[in] node Node to check.
5348 * @param[in] root_type XPath root node type.
Michal Vaskod3678892020-05-21 10:06:58 +02005349 * @param[in] node_name Node name in the dictionary to move to, NULL for any node.
5350 * @param[in] moveto_mod Expected module of the node, NULL for any.
Michal Vasko6346ece2019-09-24 13:12:53 +02005351 * @return LY_ERR (LY_ENOT if node does not match, LY_EINCOMPLETE on unresolved when,
5352 * LY_EINVAL if netither node nor any children match)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005353 */
5354static LY_ERR
5355moveto_node_check(const struct lyd_node *node, enum lyxp_node_type root_type, const char *node_name,
5356 const struct lys_module *moveto_mod)
5357{
5358 /* module check */
5359 if (moveto_mod && (node->schema->module != moveto_mod)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005360 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005361 }
5362
Michal Vasko5c4e5892019-11-14 12:31:38 +01005363 /* context check */
5364 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005365 return LY_EINVAL;
5366 }
5367
5368 /* name check */
Michal Vasko61ac2f62020-05-25 12:39:51 +02005369 if (node_name && strcmp(node_name, "*") && (node->schema->name != node_name)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005370 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005371 }
5372
Michal Vaskoa1424542019-11-14 16:08:52 +01005373 /* when check */
5374 if (moveto_when_check(node)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005375 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01005376 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005377
5378 /* match */
5379 return LY_SUCCESS;
5380}
5381
5382/**
5383 * @brief Check @p node as a part of schema NameTest processing.
5384 *
5385 * @param[in] node Schema node to check.
5386 * @param[in] root_type XPath root node type.
Michal Vaskod3678892020-05-21 10:06:58 +02005387 * @param[in] node_name Node name in the dictionary to move to, NULL for any nodes.
5388 * @param[in] moveto_mod Expected module of the node, NULL for any.
Michal Vasko6346ece2019-09-24 13:12:53 +02005389 * @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 +02005390 */
5391static LY_ERR
5392moveto_scnode_check(const struct lysc_node *node, enum lyxp_node_type root_type, const char *node_name,
Michal Vaskocafad9d2019-11-07 15:20:03 +01005393 const struct lys_module *moveto_mod)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005394{
Michal Vasko03ff5a72019-09-11 13:49:33 +02005395 /* module check */
Michal Vaskod3678892020-05-21 10:06:58 +02005396 if (moveto_mod && (node->module != moveto_mod)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005397 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005398 }
5399
5400 /* context check */
5401 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (node->flags & LYS_CONFIG_R)) {
5402 return LY_EINVAL;
5403 }
5404
5405 /* name check */
Michal Vasko61ac2f62020-05-25 12:39:51 +02005406 if (node_name && strcmp(node_name, "*") && (node->name != node_name)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005407 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005408 }
5409
5410 /* match */
5411 return LY_SUCCESS;
5412}
5413
5414/**
Michal Vaskod3678892020-05-21 10:06:58 +02005415 * @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 +02005416 *
5417 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005418 * @param[in] mod Matching node module, NULL for any.
5419 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005420 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5421 */
5422static LY_ERR
Michal Vaskod3678892020-05-21 10:06:58 +02005423moveto_node(struct lyxp_set *set, const struct lys_module *mod, const char *ncname)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005424{
Michal Vaskof03ed032020-03-04 13:31:44 +01005425 uint32_t i;
Michal Vasko6346ece2019-09-24 13:12:53 +02005426 int replaced;
Michal Vaskod3678892020-05-21 10:06:58 +02005427 const struct lyd_node *siblings, *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005428 LY_ERR rc;
5429
Michal Vaskod3678892020-05-21 10:06:58 +02005430 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005431 return LY_SUCCESS;
5432 }
5433
5434 if (set->type != LYXP_SET_NODE_SET) {
5435 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
5436 return LY_EVALID;
5437 }
5438
Michal Vasko03ff5a72019-09-11 13:49:33 +02005439 for (i = 0; i < set->used; ) {
5440 replaced = 0;
5441
5442 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 +01005443 assert(!set->val.nodes[i].node);
Michal Vaskod3678892020-05-21 10:06:58 +02005444
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005445 /* search in all the trees */
Michal Vaskod3678892020-05-21 10:06:58 +02005446 siblings = set->tree;
Michal Vasko5bfd4be2020-06-23 13:26:19 +02005447 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005448 /* search in children */
Michal Vasko5bfd4be2020-06-23 13:26:19 +02005449 siblings = lyd_node_children(set->val.nodes[i].node, 0);
Michal Vaskod3678892020-05-21 10:06:58 +02005450 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005451
Michal Vaskod3678892020-05-21 10:06:58 +02005452 for (sub = siblings; sub; sub = sub->next) {
5453 rc = moveto_node_check(sub, set->root_type, ncname, mod);
5454 if (rc == LY_SUCCESS) {
5455 if (!replaced) {
5456 set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i);
5457 replaced = 1;
5458 } else {
5459 set_insert_node(set, sub, 0, LYXP_NODE_ELEM, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005460 }
Michal Vaskod3678892020-05-21 10:06:58 +02005461 ++i;
5462 } else if (rc == LY_EINCOMPLETE) {
5463 return rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005464 }
5465 }
5466
5467 if (!replaced) {
5468 /* no match */
5469 set_remove_node(set, i);
5470 }
5471 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005472
5473 return LY_SUCCESS;
5474}
5475
5476/**
Michal Vaskod3678892020-05-21 10:06:58 +02005477 * @brief Move context @p set to a node using hashes. Result is LYXP_SET_NODE_SET (or LYXP_SET_EMPTY).
5478 * Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005479 *
5480 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005481 * @param[in] scnode Matching node schema.
Michal Vasko004d3152020-06-11 19:59:22 +02005482 * @param[in] predicates If @p scnode is ::LYS_LIST or ::LYS_LEAFLIST, the predicates specifying a single instance.
Michal Vaskod3678892020-05-21 10:06:58 +02005483 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5484 */
5485static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02005486moveto_node_hash(struct lyxp_set *set, const struct lysc_node *scnode, const struct ly_path_predicate *predicates)
Michal Vaskod3678892020-05-21 10:06:58 +02005487{
Michal Vasko004d3152020-06-11 19:59:22 +02005488 LY_ERR ret = LY_SUCCESS;
Michal Vaskod3678892020-05-21 10:06:58 +02005489 uint32_t i;
5490 int replaced;
5491 const struct lyd_node *siblings;
Michal Vasko004d3152020-06-11 19:59:22 +02005492 struct lyd_node *sub, *inst = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02005493
Michal Vasko004d3152020-06-11 19:59:22 +02005494 assert(scnode && (!(scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)) || predicates));
Michal Vaskod3678892020-05-21 10:06:58 +02005495
5496 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02005497 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005498 }
5499
5500 if (set->type != LYXP_SET_NODE_SET) {
5501 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
Michal Vasko004d3152020-06-11 19:59:22 +02005502 ret = LY_EVALID;
5503 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005504 }
5505
5506 /* context check for all the nodes since we have the schema node */
5507 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) {
5508 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02005509 goto cleanup;
5510 }
5511
5512 /* create specific data instance if needed */
5513 if (scnode->nodetype == LYS_LIST) {
5514 LY_CHECK_GOTO(ret = lyd_create_list(scnode, predicates, &inst), cleanup);
5515 } else if (scnode->nodetype == LYS_LEAFLIST) {
5516 LY_CHECK_GOTO(ret = lyd_create_term2(scnode, &predicates[0].value, &inst), cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02005517 }
5518
5519 for (i = 0; i < set->used; ) {
5520 replaced = 0;
5521 siblings = NULL;
5522
5523 if ((set->val.nodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.nodes[i].type == LYXP_NODE_ROOT)) {
5524 assert(!set->val.nodes[i].node);
5525
5526 /* search in all the trees */
5527 siblings = set->tree;
5528 } else if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
5529 /* search in children */
Michal Vasko5bfd4be2020-06-23 13:26:19 +02005530 siblings = lyd_node_children(set->val.nodes[i].node, 0);
Michal Vaskod3678892020-05-21 10:06:58 +02005531 }
5532
5533 /* find the node using hashes */
Michal Vasko004d3152020-06-11 19:59:22 +02005534 if (inst) {
5535 lyd_find_sibling_first(siblings, inst, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02005536 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02005537 lyd_find_sibling_val(siblings, scnode, NULL, 0, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02005538 }
5539
5540 /* when check */
5541 if (sub && moveto_when_check(sub)) {
Michal Vasko004d3152020-06-11 19:59:22 +02005542 ret = LY_EINCOMPLETE;
5543 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005544 }
5545
5546 if (sub) {
5547 /* pos filled later */
5548 if (!replaced) {
5549 set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i);
5550 replaced = 1;
5551 } else {
5552 set_insert_node(set, sub, 0, LYXP_NODE_ELEM, i);
5553 }
5554 ++i;
5555 }
5556
5557 if (!replaced) {
5558 /* no match */
5559 set_remove_node(set, i);
5560 }
5561 }
5562
Michal Vasko004d3152020-06-11 19:59:22 +02005563cleanup:
5564 lyd_free_tree(inst);
5565 return ret;
Michal Vaskod3678892020-05-21 10:06:58 +02005566}
5567
5568/**
5569 * @brief Move context @p set to a schema node. Result is LYXP_SET_SCNODE_SET (or LYXP_SET_EMPTY).
5570 *
5571 * @param[in,out] set Set to use.
5572 * @param[in] mod Matching node module, NULL for any.
5573 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005574 * @param[in] options XPath options.
5575 * @return LY_ERR
5576 */
5577static LY_ERR
Michal Vaskod3678892020-05-21 10:06:58 +02005578moveto_scnode(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, int options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005579{
Michal Vaskocafad9d2019-11-07 15:20:03 +01005580 int i, orig_used, idx, temp_ctx = 0, getnext_opts;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005581 uint32_t mod_idx;
Michal Vasko519fd602020-05-26 12:17:39 +02005582 const struct lysc_node *iter, *start_parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005583
Michal Vaskod3678892020-05-21 10:06:58 +02005584 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005585 return LY_SUCCESS;
5586 }
5587
5588 if (set->type != LYXP_SET_SCNODE_SET) {
Michal Vaskof6e51882019-12-16 09:59:45 +01005589 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 +02005590 return LY_EVALID;
5591 }
5592
Michal Vaskocafad9d2019-11-07 15:20:03 +01005593 /* getnext opts */
5594 getnext_opts = LYS_GETNEXT_NOSTATECHECK;
5595 if (options & LYXP_SCNODE_OUTPUT) {
5596 getnext_opts |= LYS_GETNEXT_OUTPUT;
5597 }
5598
Michal Vasko03ff5a72019-09-11 13:49:33 +02005599 orig_used = set->used;
5600 for (i = 0; i < orig_used; ++i) {
5601 if (set->val.scnodes[i].in_ctx != 1) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01005602 if (set->val.scnodes[i].in_ctx != -2) {
5603 continue;
5604 }
5605
5606 /* remember context node */
5607 set->val.scnodes[i].in_ctx = -1;
Michal Vaskoec4df482019-12-16 10:02:18 +01005608 } else {
5609 set->val.scnodes[i].in_ctx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005610 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005611
5612 start_parent = set->val.scnodes[i].scnode;
5613
5614 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 +02005615 /* 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 +02005616 * so use it directly (root node itself is useless in this case) */
5617 mod_idx = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02005618 while (mod || (mod = (struct lys_module *)ly_ctx_get_module_iter(set->ctx, &mod_idx))) {
Michal Vasko519fd602020-05-26 12:17:39 +02005619 iter = NULL;
Michal Vasko509de4d2019-12-10 14:51:30 +01005620 /* module may not be implemented */
Michal Vasko519fd602020-05-26 12:17:39 +02005621 while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) {
5622 if (!moveto_scnode_check(iter, set->root_type, ncname, mod)) {
5623 idx = lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005624 /* we need to prevent these nodes from being considered in this moveto */
5625 if ((idx < orig_used) && (idx > i)) {
5626 set->val.scnodes[idx].in_ctx = 2;
5627 temp_ctx = 1;
5628 }
5629 }
5630 }
5631
5632 if (!mod_idx) {
Michal Vaskod3678892020-05-21 10:06:58 +02005633 /* mod was specified, we are not going through the whole context */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005634 break;
5635 }
5636 /* next iteration */
Michal Vaskod3678892020-05-21 10:06:58 +02005637 mod = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005638 }
5639
Michal Vasko519fd602020-05-26 12:17:39 +02005640 } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
5641 iter = NULL;
5642 while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) {
5643 if (!moveto_scnode_check(iter, set->root_type, ncname, (mod ? mod : set->local_mod))) {
5644 idx = lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005645 if ((idx < orig_used) && (idx > i)) {
5646 set->val.scnodes[idx].in_ctx = 2;
5647 temp_ctx = 1;
5648 }
5649 }
5650 }
5651 }
5652 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005653
5654 /* correct temporary in_ctx values */
5655 if (temp_ctx) {
5656 for (i = 0; i < orig_used; ++i) {
5657 if (set->val.scnodes[i].in_ctx == 2) {
5658 set->val.scnodes[i].in_ctx = 1;
5659 }
5660 }
5661 }
5662
5663 return LY_SUCCESS;
5664}
5665
5666/**
Michal Vaskod3678892020-05-21 10:06:58 +02005667 * @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 +02005668 * Context position aware.
5669 *
5670 * @param[in] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005671 * @param[in] mod Matching node module, NULL for any.
5672 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005673 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5674 */
5675static LY_ERR
Michal Vaskod3678892020-05-21 10:06:58 +02005676moveto_node_alldesc(struct lyxp_set *set, const struct lys_module *mod, const char *ncname)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005677{
5678 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005679 const struct lyd_node *next, *elem, *start;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005680 struct lyxp_set ret_set;
5681 LY_ERR rc;
5682
Michal Vaskod3678892020-05-21 10:06:58 +02005683 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005684 return LY_SUCCESS;
5685 }
5686
5687 if (set->type != LYXP_SET_NODE_SET) {
5688 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
5689 return LY_EVALID;
5690 }
5691
Michal Vasko9f96a052020-03-10 09:41:45 +01005692 /* 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 +02005693 rc = moveto_node(set, NULL, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005694 LY_CHECK_RET(rc);
5695
Michal Vasko6346ece2019-09-24 13:12:53 +02005696 /* this loop traverses all the nodes in the set and adds/keeps only those that match qname */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005697 set_init(&ret_set, set);
5698 for (i = 0; i < set->used; ++i) {
5699
5700 /* TREE DFS */
5701 start = set->val.nodes[i].node;
5702 for (elem = next = start; elem; elem = next) {
Michal Vaskod3678892020-05-21 10:06:58 +02005703 rc = moveto_node_check(elem, set->root_type, ncname, mod);
Michal Vasko6346ece2019-09-24 13:12:53 +02005704 if (!rc) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005705 /* add matching node into result set */
5706 set_insert_node(&ret_set, elem, 0, LYXP_NODE_ELEM, ret_set.used);
5707 if (set_dup_node_check(set, elem, LYXP_NODE_ELEM, i)) {
5708 /* the node is a duplicate, we'll process it later in the set */
5709 goto skip_children;
5710 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005711 } else if (rc == LY_EINCOMPLETE) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005712 return rc;
5713 } else if (rc == LY_EINVAL) {
5714 goto skip_children;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005715 }
5716
5717 /* TREE DFS NEXT ELEM */
5718 /* select element for the next run - children first */
Michal Vasko5bfd4be2020-06-23 13:26:19 +02005719 next = lyd_node_children(elem, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005720 if (!next) {
5721skip_children:
5722 /* no children, so try siblings, but only if it's not the start,
5723 * that is considered to be the root and it's siblings are not traversed */
5724 if (elem != start) {
5725 next = elem->next;
5726 } else {
5727 break;
5728 }
5729 }
5730 while (!next) {
5731 /* no siblings, go back through the parents */
5732 if ((struct lyd_node *)elem->parent == start) {
5733 /* we are done, no next element to process */
5734 break;
5735 }
5736 /* parent is already processed, go to its sibling */
5737 elem = (struct lyd_node *)elem->parent;
5738 next = elem->next;
5739 }
5740 }
5741 }
5742
5743 /* make the temporary set the current one */
5744 ret_set.ctx_pos = set->ctx_pos;
5745 ret_set.ctx_size = set->ctx_size;
Michal Vaskod3678892020-05-21 10:06:58 +02005746 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005747 memcpy(set, &ret_set, sizeof *set);
5748
5749 return LY_SUCCESS;
5750}
5751
5752/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005753 * @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 +02005754 *
5755 * @param[in] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005756 * @param[in] mod Matching node module, NULL for any.
5757 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005758 * @param[in] options XPath options.
5759 * @return LY_ERR
5760 */
5761static LY_ERR
Michal Vaskod3678892020-05-21 10:06:58 +02005762moveto_scnode_alldesc(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, int options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005763{
Michal Vasko6346ece2019-09-24 13:12:53 +02005764 int i, orig_used, idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005765 const struct lysc_node *next, *elem, *start;
Michal Vasko6346ece2019-09-24 13:12:53 +02005766 LY_ERR rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005767
Michal Vaskod3678892020-05-21 10:06:58 +02005768 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005769 return LY_SUCCESS;
5770 }
5771
5772 if (set->type != LYXP_SET_SCNODE_SET) {
Michal Vaskof6e51882019-12-16 09:59:45 +01005773 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 +02005774 return LY_EVALID;
5775 }
5776
Michal Vasko03ff5a72019-09-11 13:49:33 +02005777 orig_used = set->used;
5778 for (i = 0; i < orig_used; ++i) {
5779 if (set->val.scnodes[i].in_ctx != 1) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01005780 if (set->val.scnodes[i].in_ctx != -2) {
5781 continue;
5782 }
5783
5784 /* remember context node */
5785 set->val.scnodes[i].in_ctx = -1;
Michal Vaskoec4df482019-12-16 10:02:18 +01005786 } else {
5787 set->val.scnodes[i].in_ctx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005788 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005789
5790 /* TREE DFS */
5791 start = set->val.scnodes[i].scnode;
5792 for (elem = next = start; elem; elem = next) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005793 if ((elem == start) || (elem->nodetype & (LYS_CHOICE | LYS_CASE))) {
5794 /* schema-only nodes, skip root */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005795 goto next_iter;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005796 }
5797
Michal Vaskod3678892020-05-21 10:06:58 +02005798 rc = moveto_scnode_check(elem, set->root_type, ncname, mod);
Michal Vasko6346ece2019-09-24 13:12:53 +02005799 if (!rc) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01005800 if ((idx = lyxp_set_scnode_dup_node_check(set, elem, LYXP_NODE_ELEM, i)) > -1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005801 set->val.scnodes[idx].in_ctx = 1;
5802 if (idx > i) {
5803 /* we will process it later in the set */
5804 goto skip_children;
5805 }
5806 } else {
Michal Vaskoecd62de2019-11-13 12:35:11 +01005807 lyxp_set_scnode_insert_node(set, elem, LYXP_NODE_ELEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005808 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005809 } else if (rc == LY_EINVAL) {
5810 goto skip_children;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005811 }
5812
5813next_iter:
5814 /* TREE DFS NEXT ELEM */
5815 /* select element for the next run - children first */
5816 next = lysc_node_children(elem, options & LYXP_SCNODE_OUTPUT ? LYS_CONFIG_R : LYS_CONFIG_W);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005817 if (!next) {
5818skip_children:
5819 /* no children, so try siblings, but only if it's not the start,
5820 * that is considered to be the root and it's siblings are not traversed */
5821 if (elem != start) {
5822 next = elem->next;
5823 } else {
5824 break;
5825 }
5826 }
5827 while (!next) {
5828 /* no siblings, go back through the parents */
5829 if (elem->parent == start) {
5830 /* we are done, no next element to process */
5831 break;
5832 }
5833 /* parent is already processed, go to its sibling */
5834 elem = elem->parent;
5835 next = elem->next;
5836 }
5837 }
5838 }
5839
5840 return LY_SUCCESS;
5841}
5842
5843/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005844 * @brief Move context @p set to an attribute. Result is LYXP_SET_NODE_SET.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005845 * Indirectly context position aware.
5846 *
5847 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005848 * @param[in] mod Matching metadata module, NULL for any.
5849 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005850 * @return LY_ERR
5851 */
5852static LY_ERR
Michal Vaskod3678892020-05-21 10:06:58 +02005853moveto_attr(struct lyxp_set *set, const struct lys_module *mod, const char *ncname)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005854{
5855 uint32_t i;
Michal Vaskod3678892020-05-21 10:06:58 +02005856 int replaced;
Michal Vasko9f96a052020-03-10 09:41:45 +01005857 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005858
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 for (i = 0; i < set->used; ) {
5869 replaced = 0;
5870
5871 /* only attributes of an elem (not dummy) can be in the result, skip all the rest;
5872 * our attributes are always qualified */
Michal Vasko5c4e5892019-11-14 12:31:38 +01005873 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01005874 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005875
5876 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02005877 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005878 continue;
5879 }
5880
Michal Vaskod3678892020-05-21 10:06:58 +02005881 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005882 /* match */
5883 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01005884 set->val.meta[i].meta = sub;
5885 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005886 /* pos does not change */
5887 replaced = 1;
5888 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01005889 set_insert_node(set, (struct lyd_node *)sub, set->val.nodes[i].pos, LYXP_NODE_META, i + 1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005890 }
5891 ++i;
5892 }
5893 }
5894 }
5895
5896 if (!replaced) {
5897 /* no match */
5898 set_remove_node(set, i);
5899 }
5900 }
5901
5902 return LY_SUCCESS;
5903}
5904
5905/**
5906 * @brief Move context @p set1 to union with @p set2. @p set2 is emptied afterwards.
Michal Vasko61ac2f62020-05-25 12:39:51 +02005907 * Result is LYXP_SET_NODE_SET. Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005908 *
5909 * @param[in,out] set1 Set to use for the result.
5910 * @param[in] set2 Set that is copied to @p set1.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005911 * @return LY_ERR
5912 */
5913static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005914moveto_union(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005915{
5916 LY_ERR rc;
5917
Michal Vaskod3678892020-05-21 10:06:58 +02005918 if ((set1->type != LYXP_SET_NODE_SET) || (set2->type != LYXP_SET_NODE_SET)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005919 LOGVAL(set1->ctx, LY_VLOG_LYD, set1->ctx_node, LY_VCODE_XP_INOP_2, "union", print_set_type(set1), print_set_type(set2));
5920 return LY_EVALID;
5921 }
5922
5923 /* set2 is empty or both set1 and set2 */
Michal Vaskod3678892020-05-21 10:06:58 +02005924 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005925 return LY_SUCCESS;
5926 }
5927
Michal Vaskod3678892020-05-21 10:06:58 +02005928 if (!set1->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005929 memcpy(set1, set2, sizeof *set1);
5930 /* dynamic memory belongs to set1 now, do not free */
Michal Vaskod3678892020-05-21 10:06:58 +02005931 memset(set2, 0, sizeof *set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005932 return LY_SUCCESS;
5933 }
5934
5935 /* we assume sets are sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005936 assert(!set_sort(set1) && !set_sort(set2));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005937
5938 /* sort, remove duplicates */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005939 rc = set_sorted_merge(set1, set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005940 LY_CHECK_RET(rc);
5941
5942 /* final set must be sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005943 assert(!set_sort(set1));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005944
5945 return LY_SUCCESS;
5946}
5947
5948/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005949 * @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 +02005950 * Context position aware.
5951 *
5952 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005953 * @param[in] mod Matching metadata module, NULL for any.
5954 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005955 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5956 */
5957static int
Michal Vaskod3678892020-05-21 10:06:58 +02005958moveto_attr_alldesc(struct lyxp_set *set, const struct lys_module *mod, const char *ncname)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005959{
5960 uint32_t i;
Michal Vaskod3678892020-05-21 10:06:58 +02005961 int replaced;
Michal Vasko9f96a052020-03-10 09:41:45 +01005962 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005963 struct lyxp_set *set_all_desc = NULL;
5964 LY_ERR rc;
5965
Michal Vaskod3678892020-05-21 10:06:58 +02005966 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005967 return LY_SUCCESS;
5968 }
5969
5970 if (set->type != LYXP_SET_NODE_SET) {
5971 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
5972 return LY_EVALID;
5973 }
5974
Michal Vasko03ff5a72019-09-11 13:49:33 +02005975 /* can be optimized similarly to moveto_node_alldesc() and save considerable amount of memory,
5976 * but it likely won't be used much, so it's a waste of time */
5977 /* copy the context */
5978 set_all_desc = set_copy(set);
5979 /* get all descendant nodes (the original context nodes are removed) */
Michal Vaskod3678892020-05-21 10:06:58 +02005980 rc = moveto_node_alldesc(set_all_desc, NULL, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005981 if (rc != LY_SUCCESS) {
5982 lyxp_set_free(set_all_desc);
5983 return rc;
5984 }
5985 /* prepend the original context nodes */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005986 rc = moveto_union(set, set_all_desc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005987 if (rc != LY_SUCCESS) {
5988 lyxp_set_free(set_all_desc);
5989 return rc;
5990 }
5991 lyxp_set_free(set_all_desc);
5992
Michal Vasko03ff5a72019-09-11 13:49:33 +02005993 for (i = 0; i < set->used; ) {
5994 replaced = 0;
5995
5996 /* only attributes of an elem can be in the result, skip all the rest,
5997 * we have all attributes qualified in lyd tree */
5998 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01005999 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006000 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02006001 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006002 continue;
6003 }
6004
Michal Vaskod3678892020-05-21 10:06:58 +02006005 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006006 /* match */
6007 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01006008 set->val.meta[i].meta = sub;
6009 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006010 /* pos does not change */
6011 replaced = 1;
6012 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01006013 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 +02006014 }
6015 ++i;
6016 }
6017 }
6018 }
6019
6020 if (!replaced) {
6021 /* no match */
6022 set_remove_node(set, i);
6023 }
6024 }
6025
6026 return LY_SUCCESS;
6027}
6028
6029/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02006030 * @brief Move context @p set to self and al chilren, recursively. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET.
6031 * Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006032 *
6033 * @param[in] parent Current parent.
6034 * @param[in] parent_pos Position of @p parent.
6035 * @param[in] parent_type Node type of @p parent.
6036 * @param[in,out] to_set Set to use.
6037 * @param[in] dup_check_set Set for checking duplicities.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006038 * @param[in] options XPath options.
6039 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6040 */
6041static LY_ERR
6042moveto_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 +01006043 struct lyxp_set *to_set, const struct lyxp_set *dup_check_set, int options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006044{
Michal Vasko61ac2f62020-05-25 12:39:51 +02006045 const struct lyd_node *iter, *first;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006046 LY_ERR rc;
6047
6048 switch (parent_type) {
6049 case LYXP_NODE_ROOT:
6050 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko61ac2f62020-05-25 12:39:51 +02006051 assert(!parent);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006052
Michal Vasko61ac2f62020-05-25 12:39:51 +02006053 /* add all top-level nodes as elements */
6054 first = to_set->tree;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006055 break;
6056 case LYXP_NODE_ELEM:
Michal Vasko61ac2f62020-05-25 12:39:51 +02006057 /* add just the text node of this term element node */
6058 if (parent->schema->nodetype & (LYD_NODE_TERM | LYD_NODE_ANY)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006059 if (!set_dup_node_check(dup_check_set, parent, LYXP_NODE_TEXT, -1)) {
6060 set_insert_node(to_set, parent, parent_pos, LYXP_NODE_TEXT, to_set->used);
6061 }
Michal Vasko61ac2f62020-05-25 12:39:51 +02006062 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006063 }
Michal Vasko61ac2f62020-05-25 12:39:51 +02006064
6065 /* add all the children of this node */
Michal Vasko5bfd4be2020-06-23 13:26:19 +02006066 first = lyd_node_children(parent, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006067 break;
6068 default:
6069 LOGINT_RET(parent->schema->module->ctx);
6070 }
6071
Michal Vasko61ac2f62020-05-25 12:39:51 +02006072 /* add all top-level nodes as elements */
6073 LY_LIST_FOR(first, iter) {
6074 /* context check */
6075 if ((parent_type == LYXP_NODE_ROOT_CONFIG) && (iter->schema->flags & LYS_CONFIG_R)) {
6076 continue;
6077 }
6078
6079 /* when check */
6080 if (moveto_when_check(iter)) {
6081 return LY_EINCOMPLETE;
6082 }
6083
6084 if (!set_dup_node_check(dup_check_set, iter, LYXP_NODE_ELEM, -1)) {
6085 set_insert_node(to_set, iter, 0, LYXP_NODE_ELEM, to_set->used);
6086
6087 /* also add all the children of this node, recursively */
6088 rc = moveto_self_add_children_r(iter, 0, LYXP_NODE_ELEM, to_set, dup_check_set, options);
6089 LY_CHECK_RET(rc);
6090 }
6091 }
6092
Michal Vasko03ff5a72019-09-11 13:49:33 +02006093 return LY_SUCCESS;
6094}
6095
6096/**
6097 * @brief Move context @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET
6098 * (or LYXP_SET_EMPTY). Context position aware.
6099 *
6100 * @param[in,out] set Set to use.
6101 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6102 * @param[in] options XPath options.
6103 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6104 */
6105static LY_ERR
6106moveto_self(struct lyxp_set *set, int all_desc, int options)
6107{
6108 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006109 struct lyxp_set ret_set;
6110 LY_ERR rc;
6111
Michal Vaskod3678892020-05-21 10:06:58 +02006112 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006113 return LY_SUCCESS;
6114 }
6115
6116 if (set->type != LYXP_SET_NODE_SET) {
6117 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
6118 return LY_EVALID;
6119 }
6120
6121 /* nothing to do */
6122 if (!all_desc) {
6123 return LY_SUCCESS;
6124 }
6125
Michal Vasko03ff5a72019-09-11 13:49:33 +02006126 /* add all the children, they get added recursively */
6127 set_init(&ret_set, set);
6128 for (i = 0; i < set->used; ++i) {
6129 /* copy the current node to tmp */
6130 set_insert_node(&ret_set, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, ret_set.used);
6131
6132 /* do not touch attributes and text nodes */
Michal Vasko9f96a052020-03-10 09:41:45 +01006133 if ((set->val.nodes[i].type == LYXP_NODE_TEXT) || (set->val.nodes[i].type == LYXP_NODE_META)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006134 continue;
6135 }
6136
Michal Vasko03ff5a72019-09-11 13:49:33 +02006137 /* add all the children */
6138 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 +01006139 set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006140 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006141 lyxp_set_free_content(&ret_set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006142 return rc;
6143 }
6144 }
6145
6146 /* use the temporary set as the current one */
6147 ret_set.ctx_pos = set->ctx_pos;
6148 ret_set.ctx_size = set->ctx_size;
Michal Vaskod3678892020-05-21 10:06:58 +02006149 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006150 memcpy(set, &ret_set, sizeof *set);
6151
6152 return LY_SUCCESS;
6153}
6154
6155/**
6156 * @brief Move context schema @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_SCNODE_SET
6157 * (or LYXP_SET_EMPTY).
6158 *
6159 * @param[in,out] set Set to use.
6160 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6161 * @param[in] options XPath options.
6162 * @return LY_ERR
6163 */
6164static LY_ERR
6165moveto_scnode_self(struct lyxp_set *set, int all_desc, int options)
6166{
Michal Vasko519fd602020-05-26 12:17:39 +02006167 int getnext_opts;
6168 uint32_t i, mod_idx;
6169 const struct lysc_node *iter, *start_parent;
6170 const struct lys_module *mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006171
Michal Vaskod3678892020-05-21 10:06:58 +02006172 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006173 return LY_SUCCESS;
6174 }
6175
6176 if (set->type != LYXP_SET_SCNODE_SET) {
Michal Vaskof6e51882019-12-16 09:59:45 +01006177 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 +02006178 return LY_EVALID;
6179 }
6180
6181 /* nothing to do */
6182 if (!all_desc) {
6183 return LY_SUCCESS;
6184 }
6185
Michal Vasko519fd602020-05-26 12:17:39 +02006186 /* getnext opts */
6187 getnext_opts = LYS_GETNEXT_NOSTATECHECK;
6188 if (options & LYXP_SCNODE_OUTPUT) {
6189 getnext_opts |= LYS_GETNEXT_OUTPUT;
6190 }
6191
6192 /* add all the children, recursively as they are being added into the same set */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006193 for (i = 0; i < set->used; ++i) {
6194 if (set->val.scnodes[i].in_ctx != 1) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006195 if (set->val.scnodes[i].in_ctx != -2) {
6196 continue;
6197 }
6198
Michal Vasko519fd602020-05-26 12:17:39 +02006199 /* remember context node */
6200 set->val.scnodes[i].in_ctx = -1;
6201 } else {
6202 set->val.scnodes[i].in_ctx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006203 }
6204
Michal Vasko519fd602020-05-26 12:17:39 +02006205 start_parent = set->val.scnodes[i].scnode;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006206
Michal Vasko519fd602020-05-26 12:17:39 +02006207 if ((set->val.scnodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.scnodes[i].type == LYXP_NODE_ROOT)) {
6208 /* it can actually be in any module, it's all <running> */
6209 mod_idx = 0;
6210 while ((mod = (struct lys_module *)ly_ctx_get_module_iter(set->ctx, &mod_idx))) {
6211 iter = NULL;
6212 /* module may not be implemented */
6213 while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) {
6214 /* context check */
6215 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) {
6216 continue;
6217 }
6218
6219 lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM);
6220 /* throw away the insert index, we want to consider that node again, recursively */
6221 }
6222 }
6223
6224 } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
6225 iter = NULL;
6226 while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006227 /* context check */
Michal Vasko519fd602020-05-26 12:17:39 +02006228 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006229 continue;
6230 }
6231
Michal Vasko519fd602020-05-26 12:17:39 +02006232 lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006233 }
6234 }
6235 }
6236
6237 return LY_SUCCESS;
6238}
6239
6240/**
6241 * @brief Move context @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_NODE_SET
6242 * (or LYXP_SET_EMPTY). Context position aware.
6243 *
6244 * @param[in] set Set to use.
6245 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6246 * @param[in] options XPath options.
6247 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6248 */
6249static LY_ERR
6250moveto_parent(struct lyxp_set *set, int all_desc, int options)
6251{
6252 LY_ERR rc;
6253 uint32_t i;
6254 struct lyd_node *node, *new_node;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006255 enum lyxp_node_type new_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006256
Michal Vaskod3678892020-05-21 10:06:58 +02006257 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006258 return LY_SUCCESS;
6259 }
6260
6261 if (set->type != LYXP_SET_NODE_SET) {
6262 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
6263 return LY_EVALID;
6264 }
6265
6266 if (all_desc) {
6267 /* <path>//.. == <path>//./.. */
6268 rc = moveto_self(set, 1, options);
6269 LY_CHECK_RET(rc);
6270 }
6271
Michal Vasko57eab132019-09-24 11:46:26 +02006272 for (i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006273 node = set->val.nodes[i].node;
6274
6275 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
6276 new_node = (struct lyd_node *)node->parent;
6277 } else if (set->val.nodes[i].type == LYXP_NODE_TEXT) {
6278 new_node = node;
Michal Vasko9f96a052020-03-10 09:41:45 +01006279 } else if (set->val.nodes[i].type == LYXP_NODE_META) {
6280 new_node = set->val.meta[i].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006281 if (!new_node) {
6282 LOGINT_RET(set->ctx);
6283 }
6284 } else {
6285 /* root does not have a parent */
Michal Vasko2caefc12019-11-14 16:07:56 +01006286 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006287 continue;
6288 }
6289
Michal Vaskoa1424542019-11-14 16:08:52 +01006290 /* when check */
6291 if (moveto_when_check(new_node)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006292 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01006293 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006294
6295 /* node already there can also be the root */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006296 if (!new_node) {
6297 new_type = set->root_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006298
6299 /* node has a standard parent (it can equal the root, it's not the root yet since they are fake) */
6300 } else {
6301 new_type = LYXP_NODE_ELEM;
6302 }
6303
Michal Vasko03ff5a72019-09-11 13:49:33 +02006304 if (set_dup_node_check(set, new_node, new_type, -1)) {
Michal Vasko2caefc12019-11-14 16:07:56 +01006305 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006306 } else {
6307 set_replace_node(set, new_node, 0, new_type, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006308 }
6309 }
6310
Michal Vasko2caefc12019-11-14 16:07:56 +01006311 set_remove_nodes_none(set);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006312 assert(!set_sort(set) && !set_sorted_dup_node_clean(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006313
6314 return LY_SUCCESS;
6315}
6316
6317/**
6318 * @brief Move context schema @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_SCNODE_SET
6319 * (or LYXP_SET_EMPTY).
6320 *
6321 * @param[in] set Set to use.
6322 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6323 * @param[in] options XPath options.
6324 * @return LY_ERR
6325 */
6326static LY_ERR
6327moveto_scnode_parent(struct lyxp_set *set, int all_desc, int options)
6328{
6329 int idx, i, orig_used, temp_ctx = 0;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006330 const struct lysc_node *node, *new_node;
6331 enum lyxp_node_type new_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006332 LY_ERR rc;
6333
Michal Vaskod3678892020-05-21 10:06:58 +02006334 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006335 return LY_SUCCESS;
6336 }
6337
6338 if (set->type != LYXP_SET_SCNODE_SET) {
Michal Vaskof6e51882019-12-16 09:59:45 +01006339 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 +02006340 return LY_EVALID;
6341 }
6342
6343 if (all_desc) {
6344 /* <path>//.. == <path>//./.. */
6345 rc = moveto_scnode_self(set, 1, options);
6346 LY_CHECK_RET(rc);
6347 }
6348
Michal Vasko03ff5a72019-09-11 13:49:33 +02006349 orig_used = set->used;
6350 for (i = 0; i < orig_used; ++i) {
6351 if (set->val.scnodes[i].in_ctx != 1) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006352 if (set->val.scnodes[i].in_ctx != -2) {
6353 continue;
6354 }
6355
6356 /* remember context node */
6357 set->val.scnodes[i].in_ctx = -1;
Michal Vaskoec4df482019-12-16 10:02:18 +01006358 } else {
6359 set->val.scnodes[i].in_ctx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006360 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006361
6362 node = set->val.scnodes[i].scnode;
6363
6364 if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
Michal Vaskod3678892020-05-21 10:06:58 +02006365 new_node = lysc_data_parent(node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006366 } else {
6367 /* root does not have a parent */
6368 continue;
6369 }
6370
Michal Vasko03ff5a72019-09-11 13:49:33 +02006371 /* node has no parent */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006372 if (!new_node) {
6373 new_type = set->root_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006374
6375 /* node has a standard parent (it can equal the root, it's not the root yet since they are fake) */
6376 } else {
6377 new_type = LYXP_NODE_ELEM;
6378 }
6379
Michal Vaskoecd62de2019-11-13 12:35:11 +01006380 idx = lyxp_set_scnode_insert_node(set, new_node, new_type);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006381 if ((idx < orig_used) && (idx > i)) {
6382 set->val.scnodes[idx].in_ctx = 2;
6383 temp_ctx = 1;
6384 }
6385 }
6386
6387 if (temp_ctx) {
6388 for (i = 0; i < orig_used; ++i) {
6389 if (set->val.scnodes[i].in_ctx == 2) {
6390 set->val.scnodes[i].in_ctx = 1;
6391 }
6392 }
6393 }
6394
6395 return LY_SUCCESS;
6396}
6397
6398/**
6399 * @brief Move context @p set to the result of a comparison. Handles '=', '!=', '<=', '<', '>=', or '>'.
6400 * Result is LYXP_SET_BOOLEAN. Indirectly context position aware.
6401 *
6402 * @param[in,out] set1 Set to use for the result.
6403 * @param[in] set2 Set acting as the second operand for @p op.
6404 * @param[in] op Comparison operator to process.
6405 * @param[in] options XPath options.
6406 * @return LY_ERR
6407 */
6408static LY_ERR
6409moveto_op_comp(struct lyxp_set *set1, struct lyxp_set *set2, const char *op, int options)
6410{
6411 /*
6412 * NODE SET + NODE SET = NODE SET + STRING /(1 NODE SET) 2 STRING
6413 * NODE SET + STRING = STRING + STRING /1 STRING (2 STRING)
6414 * NODE SET + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6415 * NODE SET + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6416 * STRING + NODE SET = STRING + STRING /(1 STRING) 2 STRING
6417 * NUMBER + NODE SET = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6418 * BOOLEAN + NODE SET = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6419 *
6420 * '=' or '!='
6421 * BOOLEAN + BOOLEAN
6422 * BOOLEAN + STRING = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6423 * BOOLEAN + NUMBER = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6424 * STRING + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6425 * NUMBER + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6426 * NUMBER + NUMBER
6427 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6428 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6429 * STRING + STRING
6430 *
6431 * '<=', '<', '>=', '>'
6432 * NUMBER + NUMBER
6433 * BOOLEAN + BOOLEAN = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6434 * BOOLEAN + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6435 * BOOLEAN + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6436 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6437 * STRING + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6438 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6439 * NUMBER + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6440 * STRING + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6441 */
6442 struct lyxp_set iter1, iter2;
6443 int result;
6444 int64_t i;
6445 LY_ERR rc;
6446
Michal Vasko004d3152020-06-11 19:59:22 +02006447 memset(&iter1, 0, sizeof iter1);
6448 memset(&iter2, 0, sizeof iter2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006449
6450 /* iterative evaluation with node-sets */
6451 if ((set1->type == LYXP_SET_NODE_SET) || (set2->type == LYXP_SET_NODE_SET)) {
6452 if (set1->type == LYXP_SET_NODE_SET) {
6453 for (i = 0; i < set1->used; ++i) {
6454 switch (set2->type) {
6455 case LYXP_SET_NUMBER:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006456 rc = set_comp_cast(&iter1, set1, LYXP_SET_NUMBER, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006457 break;
6458 case LYXP_SET_BOOLEAN:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006459 rc = set_comp_cast(&iter1, set1, LYXP_SET_BOOLEAN, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006460 break;
6461 default:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006462 rc = set_comp_cast(&iter1, set1, LYXP_SET_STRING, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006463 break;
6464 }
6465 LY_CHECK_RET(rc);
6466
6467 rc = moveto_op_comp(&iter1, set2, op, options);
6468 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006469 lyxp_set_free_content(&iter1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006470 return rc;
6471 }
6472
6473 /* lazy evaluation until true */
Michal Vasko004d3152020-06-11 19:59:22 +02006474 if (iter1.val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006475 set_fill_boolean(set1, 1);
6476 return LY_SUCCESS;
6477 }
6478 }
6479 } else {
6480 for (i = 0; i < set2->used; ++i) {
6481 switch (set1->type) {
6482 case LYXP_SET_NUMBER:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006483 rc = set_comp_cast(&iter2, set2, LYXP_SET_NUMBER, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006484 break;
6485 case LYXP_SET_BOOLEAN:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006486 rc = set_comp_cast(&iter2, set2, LYXP_SET_BOOLEAN, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006487 break;
6488 default:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006489 rc = set_comp_cast(&iter2, set2, LYXP_SET_STRING, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006490 break;
6491 }
6492 LY_CHECK_RET(rc);
6493
6494 set_fill_set(&iter1, set1);
6495
6496 rc = moveto_op_comp(&iter1, &iter2, op, options);
6497 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006498 lyxp_set_free_content(&iter1);
6499 lyxp_set_free_content(&iter2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006500 return rc;
6501 }
Michal Vaskod3678892020-05-21 10:06:58 +02006502 lyxp_set_free_content(&iter2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006503
6504 /* lazy evaluation until true */
Michal Vasko004d3152020-06-11 19:59:22 +02006505 if (iter1.val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006506 set_fill_boolean(set1, 1);
6507 return LY_SUCCESS;
6508 }
6509 }
6510 }
6511
6512 /* false for all nodes */
6513 set_fill_boolean(set1, 0);
6514 return LY_SUCCESS;
6515 }
6516
6517 /* first convert properly */
6518 if ((op[0] == '=') || (op[0] == '!')) {
6519 if ((set1->type == LYXP_SET_BOOLEAN) || (set2->type == LYXP_SET_BOOLEAN)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006520 lyxp_set_cast(set1, LYXP_SET_BOOLEAN);
6521 lyxp_set_cast(set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006522 } else if ((set1->type == LYXP_SET_NUMBER) || (set2->type == LYXP_SET_NUMBER)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006523 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006524 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006525 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006526 LY_CHECK_RET(rc);
6527 } /* else we have 2 strings */
6528 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006529 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006530 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006531 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006532 LY_CHECK_RET(rc);
6533 }
6534
6535 assert(set1->type == set2->type);
6536
6537 /* compute result */
6538 if (op[0] == '=') {
6539 if (set1->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02006540 result = (set1->val.bln == set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006541 } else if (set1->type == LYXP_SET_NUMBER) {
6542 result = (set1->val.num == set2->val.num);
6543 } else {
6544 assert(set1->type == LYXP_SET_STRING);
Michal Vaskoac6c72f2019-11-14 16:09:34 +01006545 result = !strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006546 }
6547 } else if (op[0] == '!') {
6548 if (set1->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02006549 result = (set1->val.bln != set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006550 } else if (set1->type == LYXP_SET_NUMBER) {
6551 result = (set1->val.num != set2->val.num);
6552 } else {
6553 assert(set1->type == LYXP_SET_STRING);
Michal Vaskoac6c72f2019-11-14 16:09:34 +01006554 result = !strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006555 }
6556 } else {
6557 assert(set1->type == LYXP_SET_NUMBER);
6558 if (op[0] == '<') {
6559 if (op[1] == '=') {
6560 result = (set1->val.num <= set2->val.num);
6561 } else {
6562 result = (set1->val.num < set2->val.num);
6563 }
6564 } else {
6565 if (op[1] == '=') {
6566 result = (set1->val.num >= set2->val.num);
6567 } else {
6568 result = (set1->val.num > set2->val.num);
6569 }
6570 }
6571 }
6572
6573 /* assign result */
6574 if (result) {
6575 set_fill_boolean(set1, 1);
6576 } else {
6577 set_fill_boolean(set1, 0);
6578 }
6579
6580 return LY_SUCCESS;
6581}
6582
6583/**
6584 * @brief Move context @p set to the result of a basic operation. Handles '+', '-', unary '-', '*', 'div',
6585 * or 'mod'. Result is LYXP_SET_NUMBER. Indirectly context position aware.
6586 *
6587 * @param[in,out] set1 Set to use for the result.
6588 * @param[in] set2 Set acting as the second operand for @p op.
6589 * @param[in] op Operator to process.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006590 * @return LY_ERR
6591 */
6592static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006593moveto_op_math(struct lyxp_set *set1, struct lyxp_set *set2, const char *op)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006594{
6595 LY_ERR rc;
6596
6597 /* unary '-' */
6598 if (!set2 && (op[0] == '-')) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006599 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006600 LY_CHECK_RET(rc);
6601 set1->val.num *= -1;
6602 lyxp_set_free(set2);
6603 return LY_SUCCESS;
6604 }
6605
6606 assert(set1 && set2);
6607
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006608 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006609 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006610 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006611 LY_CHECK_RET(rc);
6612
6613 switch (op[0]) {
6614 /* '+' */
6615 case '+':
6616 set1->val.num += set2->val.num;
6617 break;
6618
6619 /* '-' */
6620 case '-':
6621 set1->val.num -= set2->val.num;
6622 break;
6623
6624 /* '*' */
6625 case '*':
6626 set1->val.num *= set2->val.num;
6627 break;
6628
6629 /* 'div' */
6630 case 'd':
6631 set1->val.num /= set2->val.num;
6632 break;
6633
6634 /* 'mod' */
6635 case 'm':
6636 set1->val.num = ((long long)set1->val.num) % ((long long)set2->val.num);
6637 break;
6638
6639 default:
6640 LOGINT_RET(set1->ctx);
6641 }
6642
6643 return LY_SUCCESS;
6644}
6645
6646/*
6647 * eval functions
6648 *
6649 * They execute a parsed XPath expression on some data subtree.
6650 */
6651
6652/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02006653 * @brief Evaluate Predicate. Logs directly on error.
6654 *
Michal Vaskod3678892020-05-21 10:06:58 +02006655 * [9] Predicate ::= '[' Expr ']'
Michal Vasko03ff5a72019-09-11 13:49:33 +02006656 *
6657 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02006658 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006659 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
6660 * @param[in] options XPath options.
6661 * @param[in] parent_pos_pred Whether parent predicate was a positional one.
6662 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6663 */
6664static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02006665eval_predicate(struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set, int options, int parent_pos_pred)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006666{
6667 LY_ERR rc;
Michal Vasko57eab132019-09-24 11:46:26 +02006668 uint16_t i, orig_exp;
Michal Vasko5c4e5892019-11-14 12:31:38 +01006669 uint32_t orig_pos, orig_size;
6670 int32_t pred_in_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006671 struct lyxp_set set2;
6672 struct lyd_node *orig_parent;
6673
6674 /* '[' */
6675 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02006676 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
6677 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006678
6679 if (!set) {
6680only_parse:
Michal Vasko004d3152020-06-11 19:59:22 +02006681 rc = eval_expr_select(exp, tok_idx, 0, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006682 LY_CHECK_RET(rc);
6683 } else if (set->type == LYXP_SET_NODE_SET) {
6684 /* 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 +01006685 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006686
6687 /* empty set, nothing to evaluate */
6688 if (!set->used) {
6689 goto only_parse;
6690 }
6691
Michal Vasko004d3152020-06-11 19:59:22 +02006692 orig_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006693 orig_pos = 0;
6694 orig_size = set->used;
6695 orig_parent = NULL;
Michal Vasko39dbf352020-05-21 10:08:59 +02006696 for (i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006697 set_init(&set2, set);
6698 set_insert_node(&set2, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, 0);
6699 /* remember the node context position for position() and context size for last(),
6700 * predicates should always be evaluated with respect to the child axis (since we do
6701 * not support explicit axes) so we assign positions based on their parents */
6702 if (parent_pos_pred && ((struct lyd_node *)set->val.nodes[i].node->parent != orig_parent)) {
6703 orig_parent = (struct lyd_node *)set->val.nodes[i].node->parent;
6704 orig_pos = 1;
6705 } else {
6706 ++orig_pos;
6707 }
6708
6709 set2.ctx_pos = orig_pos;
6710 set2.ctx_size = orig_size;
Michal Vasko004d3152020-06-11 19:59:22 +02006711 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006712
Michal Vasko004d3152020-06-11 19:59:22 +02006713 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006714 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006715 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006716 return rc;
6717 }
6718
6719 /* number is a position */
6720 if (set2.type == LYXP_SET_NUMBER) {
6721 if ((long long)set2.val.num == orig_pos) {
6722 set2.val.num = 1;
6723 } else {
6724 set2.val.num = 0;
6725 }
6726 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006727 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006728
6729 /* predicate satisfied or not? */
Michal Vasko004d3152020-06-11 19:59:22 +02006730 if (!set2.val.bln) {
Michal Vasko2caefc12019-11-14 16:07:56 +01006731 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006732 }
6733 }
Michal Vasko2caefc12019-11-14 16:07:56 +01006734 set_remove_nodes_none(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006735
6736 } else if (set->type == LYXP_SET_SCNODE_SET) {
6737 for (i = 0; i < set->used; ++i) {
6738 if (set->val.scnodes[i].in_ctx == 1) {
6739 /* there is a currently-valid node */
6740 break;
6741 }
6742 }
6743 /* empty set, nothing to evaluate */
6744 if (i == set->used) {
6745 goto only_parse;
6746 }
6747
Michal Vasko004d3152020-06-11 19:59:22 +02006748 orig_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006749
Michal Vasko03ff5a72019-09-11 13:49:33 +02006750 /* set special in_ctx to all the valid snodes */
6751 pred_in_ctx = set_scnode_new_in_ctx(set);
6752
6753 /* use the valid snodes one-by-one */
6754 for (i = 0; i < set->used; ++i) {
6755 if (set->val.scnodes[i].in_ctx != pred_in_ctx) {
6756 continue;
6757 }
6758 set->val.scnodes[i].in_ctx = 1;
6759
Michal Vasko004d3152020-06-11 19:59:22 +02006760 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006761
Michal Vasko004d3152020-06-11 19:59:22 +02006762 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006763 LY_CHECK_RET(rc);
6764
6765 set->val.scnodes[i].in_ctx = pred_in_ctx;
6766 }
6767
6768 /* restore the state as it was before the predicate */
6769 for (i = 0; i < set->used; ++i) {
6770 if (set->val.scnodes[i].in_ctx == 1) {
6771 set->val.scnodes[i].in_ctx = 0;
6772 } else if (set->val.scnodes[i].in_ctx == pred_in_ctx) {
6773 set->val.scnodes[i].in_ctx = 1;
6774 }
6775 }
6776
6777 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02006778 set2.type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006779 set_fill_set(&set2, set);
6780
Michal Vasko004d3152020-06-11 19:59:22 +02006781 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006782 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006783 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006784 return rc;
6785 }
6786
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006787 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02006788 if (!set2.val.bln) {
Michal Vaskod3678892020-05-21 10:06:58 +02006789 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006790 }
Michal Vaskod3678892020-05-21 10:06:58 +02006791 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006792 }
6793
6794 /* ']' */
Michal Vasko004d3152020-06-11 19:59:22 +02006795 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_BRACK2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006796 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02006797 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
6798 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006799
6800 return LY_SUCCESS;
6801}
6802
6803/**
Michal Vaskod3678892020-05-21 10:06:58 +02006804 * @brief Evaluate Literal. Logs directly on error.
6805 *
6806 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02006807 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02006808 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
6809 */
6810static void
Michal Vasko004d3152020-06-11 19:59:22 +02006811eval_literal(struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set)
Michal Vaskod3678892020-05-21 10:06:58 +02006812{
6813 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02006814 if (exp->tok_len[*tok_idx] == 2) {
Michal Vaskod3678892020-05-21 10:06:58 +02006815 set_fill_string(set, "", 0);
6816 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02006817 set_fill_string(set, &exp->expr[exp->tok_pos[*tok_idx] + 1], exp->tok_len[*tok_idx] - 2);
Michal Vaskod3678892020-05-21 10:06:58 +02006818 }
6819 }
6820 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02006821 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
6822 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02006823}
6824
6825/**
Michal Vasko004d3152020-06-11 19:59:22 +02006826 * @brief Try to compile list or leaf-list predicate in the known format to be used for hash-based instance search.
Michal Vaskod3678892020-05-21 10:06:58 +02006827 *
Michal Vasko004d3152020-06-11 19:59:22 +02006828 * @param[in] exp Full parsed XPath expression.
6829 * @param[in,out] tok_idx Index in @p exp at the beginning of the predicate, is updated on success.
6830 * @param[in] scnode Found schema node as context for the predicate.
6831 * @param[in] format Format of any prefixes in key names/values.
6832 * @param[out] predicates Parsed predicates.
6833 * @param[out] pred_type Type of @p predicates.
6834 * @return LY_SUCCESS on success,
6835 * @return LY_ERR on any error.
Michal Vaskod3678892020-05-21 10:06:58 +02006836 */
6837static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02006838eval_name_test_try_compile_predicates(struct lyxp_expr *exp, uint16_t *tok_idx, const struct lysc_node *scnode,
6839 LYD_FORMAT format, struct ly_path_predicate **predicates, enum ly_path_pred_type *pred_type)
Michal Vaskod3678892020-05-21 10:06:58 +02006840{
Michal Vasko004d3152020-06-11 19:59:22 +02006841 LY_ERR ret = LY_SUCCESS;
6842 uint16_t key_count, e_idx, pred_idx = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02006843 const struct lysc_node *key;
Michal Vasko004d3152020-06-11 19:59:22 +02006844 size_t pred_len;
6845 int prev_lo;
6846 struct lyxp_expr *exp2 = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02006847
Michal Vasko004d3152020-06-11 19:59:22 +02006848 assert(scnode->nodetype & (LYS_LIST | LYS_LEAFLIST));
Michal Vaskod3678892020-05-21 10:06:58 +02006849
Michal Vasko004d3152020-06-11 19:59:22 +02006850 if (scnode->nodetype == LYS_LIST) {
6851 /* get key count */
6852 if (scnode->flags & LYS_KEYLESS) {
6853 return LY_EINVAL;
6854 }
6855 for (key_count = 0, key = lysc_node_children(scnode, 0); key && (key->flags & LYS_KEY); key = key->next, ++key_count);
6856 assert(key_count);
Michal Vaskod3678892020-05-21 10:06:58 +02006857
Michal Vasko004d3152020-06-11 19:59:22 +02006858 /* learn where the predicates end */
6859 e_idx = *tok_idx;
6860 while (key_count) {
6861 /* '[' */
6862 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
6863 return LY_EINVAL;
6864 }
6865 ++e_idx;
6866
6867 /* ']' */
6868 while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
6869 ++e_idx;
6870 }
6871 ++e_idx;
6872
6873 /* another presumably key predicate parsed */
6874 --key_count;
6875 }
6876
6877 if (key_count) {
6878 /* some keys missing for sure */
6879 return LY_EINVAL;
6880 }
6881 } else {
6882 /* learn just where this single predicate ends */
6883 e_idx = *tok_idx;
6884
Michal Vaskod3678892020-05-21 10:06:58 +02006885 /* '[' */
Michal Vasko004d3152020-06-11 19:59:22 +02006886 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
6887 return LY_EINVAL;
6888 }
6889 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02006890
6891 /* ']' */
Michal Vasko004d3152020-06-11 19:59:22 +02006892 while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
6893 ++e_idx;
6894 }
6895 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02006896 }
6897
Michal Vasko004d3152020-06-11 19:59:22 +02006898 /* get the joined length of all the keys predicates/of the single leaf-list predicate */
6899 pred_len = (exp->tok_pos[e_idx - 1] + exp->tok_len[e_idx - 1]) - exp->tok_pos[*tok_idx];
6900
6901 /* turn logging off */
6902 prev_lo = ly_log_options(0);
6903
6904 /* parse the predicate(s) */
6905 LY_CHECK_GOTO(ret = ly_path_parse_predicate(scnode->module->ctx, exp->expr + exp->tok_pos[*tok_idx], pred_len,
6906 LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &exp2), cleanup);
6907
6908 /* compile */
6909 switch (format) {
6910 case LYD_SCHEMA:
Michal Vasko00cbf532020-06-15 13:58:47 +02006911 ret = ly_path_compile_predicate(scnode->module->ctx, scnode->module, scnode, exp2, &pred_idx, lys_resolve_prefix,
6912 scnode->module, LYD_SCHEMA, predicates, pred_type);
Michal Vasko004d3152020-06-11 19:59:22 +02006913 break;
6914 case LYD_JSON:
Michal Vasko00cbf532020-06-15 13:58:47 +02006915 ret = ly_path_compile_predicate(scnode->module->ctx, scnode->module, scnode, exp2, &pred_idx,
6916 lydjson_resolve_prefix, NULL, LYD_JSON, predicates, pred_type);
Michal Vasko004d3152020-06-11 19:59:22 +02006917 break;
6918 case LYD_XML:
6919 ret = LY_EINT;
6920 goto cleanup;
6921 }
6922 LY_CHECK_GOTO(ret, cleanup);
6923
6924 /* success, the predicate must include all the needed information for hash-based search */
6925 *tok_idx = e_idx;
6926
6927cleanup:
6928 ly_log_options(prev_lo);
6929 lyxp_expr_free(scnode->module->ctx, exp2);
6930 return ret;
Michal Vaskod3678892020-05-21 10:06:58 +02006931}
6932
6933/**
6934 * @brief Evaluate NameTest and any following Predicates. Logs directly on error.
6935 *
6936 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
6937 * [6] NodeTest ::= NameTest | NodeType '(' ')'
6938 * [7] NameTest ::= '*' | NCName ':' '*' | QName
6939 *
6940 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02006941 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02006942 * @param[in] attr_axis Whether to search attributes or standard nodes.
6943 * @param[in] all_desc Whether to search all the descendants or children only.
6944 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
6945 * @param[in] options XPath options.
6946 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6947 */
6948static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02006949eval_name_test_with_predicate(struct lyxp_expr *exp, uint16_t *tok_idx, int attr_axis, int all_desc, struct lyxp_set *set,
Michal Vaskod3678892020-05-21 10:06:58 +02006950 int options)
6951{
6952 int i;
6953 char *path;
Michal Vasko004d3152020-06-11 19:59:22 +02006954 const char *ncname, *ncname_dict = NULL;
6955 uint16_t ncname_len;
Michal Vaskod3678892020-05-21 10:06:58 +02006956 const struct lys_module *moveto_mod;
6957 const struct lysc_node *scnode = NULL, *tmp;
Michal Vasko004d3152020-06-11 19:59:22 +02006958 struct ly_path_predicate *predicates = NULL;
6959 enum ly_path_pred_type pred_type = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02006960 LY_ERR rc = LY_SUCCESS;
6961
6962 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02006963 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
6964 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02006965
6966 if (!set) {
6967 goto moveto;
6968 }
6969
Michal Vasko004d3152020-06-11 19:59:22 +02006970 ncname = &exp->expr[exp->tok_pos[*tok_idx - 1]];
6971 ncname_len = exp->tok_len[*tok_idx - 1];
Michal Vaskod3678892020-05-21 10:06:58 +02006972
6973 /* parse (and skip) module name */
6974 rc = moveto_resolve_model(&ncname, &ncname_len, set, &moveto_mod);
6975 LY_CHECK_GOTO(rc, cleanup);
6976
6977 if (moveto_mod && !attr_axis && !all_desc && (set->type == LYXP_SET_NODE_SET)) {
6978 /* find the matching schema node in some parent in the context */
6979 for (i = 0; i < (signed)set->used; ++i) {
6980 if (set->val.nodes[i].type == set->root_type) {
6981 tmp = lys_find_child(NULL, moveto_mod, ncname, ncname_len, 0, 0);
6982 } else if ((set->val.nodes[i].type == LYXP_NODE_ELEM)
6983 && (!scnode || (lysc_data_parent(scnode) != set->val.nodes[i].node->schema))) {
6984 /* do not repeat the same search */
6985 tmp = lys_find_child(set->val.nodes[i].node->schema, moveto_mod, ncname, ncname_len, 0, 0);
6986 }
6987
6988 /* additional context check */
6989 if (tmp && (set->root_type == LYXP_NODE_ROOT_CONFIG) && (tmp->flags & LYS_CONFIG_R)) {
6990 tmp = NULL;
6991 }
6992
6993 if (tmp) {
6994 if (scnode) {
6995 /* we found a schema node with the same name but at different level, give up, too complicated */
6996 scnode = NULL;
6997 break;
6998 } else {
6999 /* remember the found schema node and continue to make sure it can be used */
7000 scnode = tmp;
7001 }
7002 tmp = NULL;
7003 }
7004 }
7005
Michal Vasko004d3152020-06-11 19:59:22 +02007006 if (scnode && (scnode->nodetype & (LYS_LIST | LYS_LEAFLIST))) {
7007 /* try to create the predicates */
7008 if (eval_name_test_try_compile_predicates(exp, tok_idx, scnode, set->format, &predicates, &pred_type)) {
7009 /* hashes cannot be used */
Michal Vaskod3678892020-05-21 10:06:58 +02007010 scnode = NULL;
7011 }
7012 }
7013 }
7014
Michal Vasko004d3152020-06-11 19:59:22 +02007015 if (!scnode && moveto_mod) {
7016 /* we are not able to match based on a schema node and not all the modules match,
7017 * use dictionary for efficient comparison */
7018 ncname_dict = lydict_insert(set->ctx, ncname, ncname_len);
Michal Vaskod3678892020-05-21 10:06:58 +02007019 }
7020
7021moveto:
7022 /* move to the attribute(s), data node(s), or schema node(s) */
7023 if (attr_axis) {
7024 if (set && (options & LYXP_SCNODE_ALL)) {
7025 set_scnode_clear_ctx(set);
7026 } else {
7027 if (all_desc) {
Michal Vasko004d3152020-06-11 19:59:22 +02007028 rc = moveto_attr_alldesc(set, moveto_mod, ncname_dict);
Michal Vaskod3678892020-05-21 10:06:58 +02007029 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007030 rc = moveto_attr(set, moveto_mod, ncname_dict);
Michal Vaskod3678892020-05-21 10:06:58 +02007031 }
7032 LY_CHECK_GOTO(rc, cleanup);
7033 }
7034 } else {
7035 if (set && (options & LYXP_SCNODE_ALL)) {
7036 if (all_desc) {
Michal Vasko004d3152020-06-11 19:59:22 +02007037 rc = moveto_scnode_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007038 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007039 rc = moveto_scnode(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007040 }
7041 LY_CHECK_GOTO(rc, cleanup);
7042
7043 for (i = set->used - 1; i > -1; --i) {
7044 if (set->val.scnodes[i].in_ctx > 0) {
7045 break;
7046 }
7047 }
7048 if (i == -1) {
7049 path = lysc_path(set->ctx_scnode, LYSC_PATH_LOG, NULL, 0);
7050 LOGWRN(set->ctx, "Schema node \"%.*s\" not found (%.*s) with context node \"%s\".",
Michal Vasko004d3152020-06-11 19:59:22 +02007051 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]],
7052 exp->tok_pos[*tok_idx] + exp->tok_len[*tok_idx], exp->expr, path);
Michal Vaskod3678892020-05-21 10:06:58 +02007053 free(path);
7054 }
7055 } else {
7056 if (all_desc) {
Michal Vasko004d3152020-06-11 19:59:22 +02007057 rc = moveto_node_alldesc(set, moveto_mod, ncname_dict);
Michal Vaskod3678892020-05-21 10:06:58 +02007058 } else {
7059 if (scnode) {
7060 /* we can find the nodes using hashes */
Michal Vasko004d3152020-06-11 19:59:22 +02007061 rc = moveto_node_hash(set, scnode, predicates);
Michal Vaskod3678892020-05-21 10:06:58 +02007062 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007063 rc = moveto_node(set, moveto_mod, ncname_dict);
Michal Vaskod3678892020-05-21 10:06:58 +02007064 }
7065 }
7066 LY_CHECK_GOTO(rc, cleanup);
7067 }
7068 }
7069
7070 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007071 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7072 rc = eval_predicate(exp, tok_idx, set, options, 1);
Michal Vaskod3678892020-05-21 10:06:58 +02007073 LY_CHECK_RET(rc);
7074 }
7075
7076cleanup:
Michal Vaskodb51a8d2020-05-27 15:22:29 +02007077 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007078 lydict_remove(set->ctx, ncname_dict);
7079 ly_path_predicates_free(set->ctx, pred_type, scnode, predicates);
Michal Vaskodb51a8d2020-05-27 15:22:29 +02007080 }
Michal Vaskod3678892020-05-21 10:06:58 +02007081 return rc;
7082}
7083
7084/**
7085 * @brief Evaluate NodeType and any following Predicates. Logs directly on error.
7086 *
7087 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
7088 * [6] NodeTest ::= NameTest | NodeType '(' ')'
7089 * [8] NodeType ::= 'text' | 'node'
7090 *
7091 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007092 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02007093 * @param[in] attr_axis Whether to search attributes or standard nodes.
7094 * @param[in] all_desc Whether to search all the descendants or children only.
7095 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7096 * @param[in] options XPath options.
7097 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7098 */
7099static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007100eval_node_type_with_predicate(struct lyxp_expr *exp, uint16_t *tok_idx, int attr_axis, int all_desc,
Michal Vaskod3678892020-05-21 10:06:58 +02007101 struct lyxp_set *set, int options)
7102{
7103 LY_ERR rc;
7104
7105 /* TODO */
7106 (void)attr_axis;
7107 (void)all_desc;
7108
7109 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007110 assert(exp->tok_len[*tok_idx] == 4);
Michal Vaskod3678892020-05-21 10:06:58 +02007111 if (set->type == LYXP_SET_SCNODE_SET) {
7112 set_scnode_clear_ctx(set);
7113 /* just for the debug message below */
7114 set = NULL;
7115 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007116 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "node", 4)) {
Michal Vaskod3678892020-05-21 10:06:58 +02007117 rc = xpath_node(NULL, 0, set, options);
7118 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007119 assert(!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "text", 4));
Michal Vaskod3678892020-05-21 10:06:58 +02007120 rc = xpath_text(NULL, 0, set, options);
7121 }
7122 LY_CHECK_RET(rc);
7123 }
7124 }
7125 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007126 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7127 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007128
7129 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02007130 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
Michal Vaskod3678892020-05-21 10:06:58 +02007131 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007132 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7133 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007134
7135 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007136 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
Michal Vaskod3678892020-05-21 10:06:58 +02007137 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007138 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7139 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007140
7141 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007142 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7143 rc = eval_predicate(exp, tok_idx, set, options, 1);
Michal Vaskod3678892020-05-21 10:06:58 +02007144 LY_CHECK_RET(rc);
7145 }
7146
7147 return LY_SUCCESS;
7148}
7149
7150/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02007151 * @brief Evaluate RelativeLocationPath. Logs directly on error.
7152 *
7153 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
7154 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
Michal Vaskod3678892020-05-21 10:06:58 +02007155 * [6] NodeTest ::= NameTest | NodeType '(' ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007156 *
7157 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007158 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007159 * @param[in] all_desc Whether to search all the descendants or children only.
7160 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7161 * @param[in] options XPath options.
7162 * @return LY_ERR (YL_EINCOMPLETE on unresolved when)
7163 */
7164static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007165eval_relative_location_path(struct lyxp_expr *exp, uint16_t *tok_idx, int all_desc, struct lyxp_set *set, int options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007166{
7167 int attr_axis;
7168 LY_ERR rc;
7169
7170 goto step;
7171 do {
7172 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02007173 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007174 all_desc = 0;
7175 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007176 assert(exp->tok_len[*tok_idx] == 2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007177 all_desc = 1;
7178 }
7179 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007180 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7181 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007182
7183step:
Michal Vaskod3678892020-05-21 10:06:58 +02007184 /* evaluate abbreviated axis '@'? if any */
Michal Vasko004d3152020-06-11 19:59:22 +02007185 if (exp->tokens[*tok_idx] == LYXP_TOKEN_AT) {
Michal Vaskod3678892020-05-21 10:06:58 +02007186 attr_axis = 1;
7187
7188 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007189 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7190 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007191 } else {
7192 attr_axis = 0;
7193 }
7194
Michal Vasko03ff5a72019-09-11 13:49:33 +02007195 /* Step */
Michal Vasko004d3152020-06-11 19:59:22 +02007196 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007197 case LYXP_TOKEN_DOT:
7198 /* evaluate '.' */
7199 if (set && (options & LYXP_SCNODE_ALL)) {
7200 rc = moveto_scnode_self(set, all_desc, options);
7201 } else {
7202 rc = moveto_self(set, all_desc, options);
7203 }
7204 LY_CHECK_RET(rc);
7205 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007206 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7207 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007208 break;
7209
7210 case LYXP_TOKEN_DDOT:
7211 /* evaluate '..' */
7212 if (set && (options & LYXP_SCNODE_ALL)) {
7213 rc = moveto_scnode_parent(set, all_desc, options);
7214 } else {
7215 rc = moveto_parent(set, all_desc, options);
7216 }
7217 LY_CHECK_RET(rc);
7218 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007219 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7220 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007221 break;
7222
Michal Vasko03ff5a72019-09-11 13:49:33 +02007223 case LYXP_TOKEN_NAMETEST:
Michal Vaskod3678892020-05-21 10:06:58 +02007224 /* evaluate NameTest Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007225 rc = eval_name_test_with_predicate(exp, tok_idx, attr_axis, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007226 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02007227 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007228
Michal Vaskod3678892020-05-21 10:06:58 +02007229 case LYXP_TOKEN_NODETYPE:
7230 /* evaluate NodeType Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007231 rc = eval_node_type_with_predicate(exp, tok_idx, attr_axis, all_desc, set, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007232 LY_CHECK_RET(rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007233 break;
7234
7235 default:
Michal Vasko02a77382019-09-12 11:47:35 +02007236 LOGINT_RET(set ? set->ctx : NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007237 }
Michal Vasko004d3152020-06-11 19:59:22 +02007238 } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007239
7240 return LY_SUCCESS;
7241}
7242
7243/**
7244 * @brief Evaluate AbsoluteLocationPath. Logs directly on error.
7245 *
7246 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
7247 *
7248 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007249 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007250 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7251 * @param[in] options XPath options.
7252 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7253 */
7254static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007255eval_absolute_location_path(struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set, int options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007256{
7257 int all_desc;
7258 LY_ERR rc;
7259
7260 if (set) {
7261 /* no matter what tokens follow, we need to be at the root */
7262 moveto_root(set, options);
7263 }
7264
7265 /* '/' RelativeLocationPath? */
Michal Vasko004d3152020-06-11 19:59:22 +02007266 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007267 /* evaluate '/' - deferred */
7268 all_desc = 0;
7269 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007270 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7271 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007272
Michal Vasko004d3152020-06-11 19:59:22 +02007273 if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007274 return LY_SUCCESS;
7275 }
Michal Vasko004d3152020-06-11 19:59:22 +02007276 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007277 case LYXP_TOKEN_DOT:
7278 case LYXP_TOKEN_DDOT:
7279 case LYXP_TOKEN_AT:
7280 case LYXP_TOKEN_NAMETEST:
7281 case LYXP_TOKEN_NODETYPE:
Michal Vasko004d3152020-06-11 19:59:22 +02007282 rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007283 LY_CHECK_RET(rc);
7284 break;
7285 default:
7286 break;
7287 }
7288
7289 /* '//' RelativeLocationPath */
7290 } else {
7291 /* evaluate '//' - deferred so as not to waste memory by remembering all the nodes */
7292 all_desc = 1;
7293 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007294 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7295 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007296
Michal Vasko004d3152020-06-11 19:59:22 +02007297 rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007298 LY_CHECK_RET(rc);
7299 }
7300
7301 return LY_SUCCESS;
7302}
7303
7304/**
7305 * @brief Evaluate FunctionCall. Logs directly on error.
7306 *
Michal Vaskod3678892020-05-21 10:06:58 +02007307 * [11] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007308 *
7309 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007310 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007311 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7312 * @param[in] options XPath options.
7313 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7314 */
7315static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007316eval_function_call(struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set, int options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007317{
7318 LY_ERR rc;
7319 LY_ERR (*xpath_func)(struct lyxp_set **, uint16_t, struct lyxp_set *, int) = NULL;
Michal Vasko0cbf54f2019-12-16 10:01:06 +01007320 uint16_t arg_count = 0, i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007321 struct lyxp_set **args = NULL, **args_aux;
7322
7323 if (set) {
7324 /* FunctionName */
Michal Vasko004d3152020-06-11 19:59:22 +02007325 switch (exp->tok_len[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007326 case 3:
Michal Vasko004d3152020-06-11 19:59:22 +02007327 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007328 xpath_func = &xpath_not;
Michal Vasko004d3152020-06-11 19:59:22 +02007329 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007330 xpath_func = &xpath_sum;
7331 }
7332 break;
7333 case 4:
Michal Vasko004d3152020-06-11 19:59:22 +02007334 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007335 xpath_func = &xpath_lang;
Michal Vasko004d3152020-06-11 19:59:22 +02007336 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007337 xpath_func = &xpath_last;
Michal Vasko004d3152020-06-11 19:59:22 +02007338 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007339 xpath_func = &xpath_name;
Michal Vasko004d3152020-06-11 19:59:22 +02007340 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007341 xpath_func = &xpath_true;
7342 }
7343 break;
7344 case 5:
Michal Vasko004d3152020-06-11 19:59:22 +02007345 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007346 xpath_func = &xpath_count;
Michal Vasko004d3152020-06-11 19:59:22 +02007347 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007348 xpath_func = &xpath_false;
Michal Vasko004d3152020-06-11 19:59:22 +02007349 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007350 xpath_func = &xpath_floor;
Michal Vasko004d3152020-06-11 19:59:22 +02007351 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007352 xpath_func = &xpath_round;
Michal Vasko004d3152020-06-11 19:59:22 +02007353 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007354 xpath_func = &xpath_deref;
7355 }
7356 break;
7357 case 6:
Michal Vasko004d3152020-06-11 19:59:22 +02007358 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007359 xpath_func = &xpath_concat;
Michal Vasko004d3152020-06-11 19:59:22 +02007360 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007361 xpath_func = &xpath_number;
Michal Vasko004d3152020-06-11 19:59:22 +02007362 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007363 xpath_func = &xpath_string;
7364 }
7365 break;
7366 case 7:
Michal Vasko004d3152020-06-11 19:59:22 +02007367 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007368 xpath_func = &xpath_boolean;
Michal Vasko004d3152020-06-11 19:59:22 +02007369 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007370 xpath_func = &xpath_ceiling;
Michal Vasko004d3152020-06-11 19:59:22 +02007371 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007372 xpath_func = &xpath_current;
7373 }
7374 break;
7375 case 8:
Michal Vasko004d3152020-06-11 19:59:22 +02007376 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007377 xpath_func = &xpath_contains;
Michal Vasko004d3152020-06-11 19:59:22 +02007378 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007379 xpath_func = &xpath_position;
Michal Vasko004d3152020-06-11 19:59:22 +02007380 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007381 xpath_func = &xpath_re_match;
7382 }
7383 break;
7384 case 9:
Michal Vasko004d3152020-06-11 19:59:22 +02007385 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007386 xpath_func = &xpath_substring;
Michal Vasko004d3152020-06-11 19:59:22 +02007387 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007388 xpath_func = &xpath_translate;
7389 }
7390 break;
7391 case 10:
Michal Vasko004d3152020-06-11 19:59:22 +02007392 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007393 xpath_func = &xpath_local_name;
Michal Vasko004d3152020-06-11 19:59:22 +02007394 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007395 xpath_func = &xpath_enum_value;
Michal Vasko004d3152020-06-11 19:59:22 +02007396 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007397 xpath_func = &xpath_bit_is_set;
7398 }
7399 break;
7400 case 11:
Michal Vasko004d3152020-06-11 19:59:22 +02007401 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007402 xpath_func = &xpath_starts_with;
7403 }
7404 break;
7405 case 12:
Michal Vasko004d3152020-06-11 19:59:22 +02007406 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007407 xpath_func = &xpath_derived_from;
7408 }
7409 break;
7410 case 13:
Michal Vasko004d3152020-06-11 19:59:22 +02007411 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007412 xpath_func = &xpath_namespace_uri;
Michal Vasko004d3152020-06-11 19:59:22 +02007413 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007414 xpath_func = &xpath_string_length;
7415 }
7416 break;
7417 case 15:
Michal Vasko004d3152020-06-11 19:59:22 +02007418 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007419 xpath_func = &xpath_normalize_space;
Michal Vasko004d3152020-06-11 19:59:22 +02007420 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007421 xpath_func = &xpath_substring_after;
7422 }
7423 break;
7424 case 16:
Michal Vasko004d3152020-06-11 19:59:22 +02007425 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007426 xpath_func = &xpath_substring_before;
7427 }
7428 break;
7429 case 20:
Michal Vasko004d3152020-06-11 19:59:22 +02007430 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007431 xpath_func = &xpath_derived_from_or_self;
7432 }
7433 break;
7434 }
7435
7436 if (!xpath_func) {
Michal Vasko004d3152020-06-11 19:59:22 +02007437 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INFUNC, exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007438 return LY_EVALID;
7439 }
7440 }
7441
7442 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007443 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7444 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007445
7446 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02007447 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007448 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007449 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7450 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007451
7452 /* ( Expr ( ',' Expr )* )? */
Michal Vasko004d3152020-06-11 19:59:22 +02007453 if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007454 if (set) {
7455 args = malloc(sizeof *args);
7456 LY_CHECK_ERR_GOTO(!args, LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7457 arg_count = 1;
7458 args[0] = set_copy(set);
7459 if (!args[0]) {
7460 rc = LY_EMEM;
7461 goto cleanup;
7462 }
7463
Michal Vasko004d3152020-06-11 19:59:22 +02007464 rc = eval_expr_select(exp, tok_idx, 0, args[0], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007465 LY_CHECK_GOTO(rc, cleanup);
7466 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007467 rc = eval_expr_select(exp, tok_idx, 0, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007468 LY_CHECK_GOTO(rc, cleanup);
7469 }
7470 }
Michal Vasko004d3152020-06-11 19:59:22 +02007471 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007472 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007473 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7474 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007475
7476 if (set) {
7477 ++arg_count;
7478 args_aux = realloc(args, arg_count * sizeof *args);
7479 LY_CHECK_ERR_GOTO(!args_aux, arg_count--; LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7480 args = args_aux;
7481 args[arg_count - 1] = set_copy(set);
7482 if (!args[arg_count - 1]) {
7483 rc = LY_EMEM;
7484 goto cleanup;
7485 }
7486
Michal Vasko004d3152020-06-11 19:59:22 +02007487 rc = eval_expr_select(exp, tok_idx, 0, args[arg_count - 1], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007488 LY_CHECK_GOTO(rc, cleanup);
7489 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007490 rc = eval_expr_select(exp, tok_idx, 0, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007491 LY_CHECK_GOTO(rc, cleanup);
7492 }
7493 }
7494
7495 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007496 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007497 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007498 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7499 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007500
7501 if (set) {
7502 /* evaluate function */
7503 rc = xpath_func(args, arg_count, set, options);
7504
7505 if (options & LYXP_SCNODE_ALL) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007506 /* merge all nodes from arg evaluations */
7507 for (i = 0; i < arg_count; ++i) {
7508 set_scnode_clear_ctx(args[i]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007509 lyxp_set_scnode_merge(set, args[i]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007510 }
7511 }
7512 } else {
7513 rc = LY_SUCCESS;
7514 }
7515
7516cleanup:
7517 for (i = 0; i < arg_count; ++i) {
7518 lyxp_set_free(args[i]);
7519 }
7520 free(args);
7521
7522 return rc;
7523}
7524
7525/**
7526 * @brief Evaluate Number. Logs directly on error.
7527 *
7528 * @param[in] ctx Context for errors.
7529 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007530 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007531 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7532 * @return LY_ERR
7533 */
7534static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007535eval_number(struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007536{
7537 long double num;
7538 char *endptr;
7539
7540 if (set) {
7541 errno = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02007542 num = strtold(&exp->expr[exp->tok_pos[*tok_idx]], &endptr);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007543 if (errno) {
Michal Vasko004d3152020-06-11 19:59:22 +02007544 LOGVAL(ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007545 LOGVAL(ctx, LY_VLOG_LYD, set->ctx_node, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double (%s).",
Michal Vasko004d3152020-06-11 19:59:22 +02007546 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]], strerror(errno));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007547 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02007548 } else if (endptr - &exp->expr[exp->tok_pos[*tok_idx]] != exp->tok_len[*tok_idx]) {
7549 LOGVAL(ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INTOK, "Unknown", &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007550 LOGVAL(ctx, LY_VLOG_LYD, set->ctx_node, LYVE_XPATH, "Failed to convert \"%.*s\" into a long double.",
Michal Vasko004d3152020-06-11 19:59:22 +02007551 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007552 return LY_EVALID;
7553 }
7554
7555 set_fill_number(set, num);
7556 }
7557
7558 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007559 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7560 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007561 return LY_SUCCESS;
7562}
7563
7564/**
7565 * @brief Evaluate PathExpr. Logs directly on error.
7566 *
Michal Vaskod3678892020-05-21 10:06:58 +02007567 * [12] PathExpr ::= LocationPath | PrimaryExpr Predicate*
Michal Vasko03ff5a72019-09-11 13:49:33 +02007568 * | PrimaryExpr Predicate* '/' RelativeLocationPath
7569 * | PrimaryExpr Predicate* '//' RelativeLocationPath
7570 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
Michal Vaskod3678892020-05-21 10:06:58 +02007571 * [10] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall
Michal Vasko03ff5a72019-09-11 13:49:33 +02007572 *
7573 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007574 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007575 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7576 * @param[in] options XPath options.
7577 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7578 */
7579static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007580eval_path_expr(struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set, int options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007581{
7582 int all_desc, parent_pos_pred;
7583 LY_ERR rc;
7584
Michal Vasko004d3152020-06-11 19:59:22 +02007585 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007586 case LYXP_TOKEN_PAR1:
7587 /* '(' Expr ')' */
7588
7589 /* '(' */
7590 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007591 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7592 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007593
7594 /* Expr */
Michal Vasko004d3152020-06-11 19:59:22 +02007595 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007596 LY_CHECK_RET(rc);
7597
7598 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007599 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007600 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007601 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7602 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007603
7604 parent_pos_pred = 0;
7605 goto predicate;
7606
7607 case LYXP_TOKEN_DOT:
7608 case LYXP_TOKEN_DDOT:
7609 case LYXP_TOKEN_AT:
7610 case LYXP_TOKEN_NAMETEST:
7611 case LYXP_TOKEN_NODETYPE:
7612 /* RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007613 rc = eval_relative_location_path(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007614 LY_CHECK_RET(rc);
7615 break;
7616
7617 case LYXP_TOKEN_FUNCNAME:
7618 /* FunctionCall */
7619 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007620 rc = eval_function_call(exp, tok_idx, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007621 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007622 rc = eval_function_call(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007623 }
7624 LY_CHECK_RET(rc);
7625
7626 parent_pos_pred = 1;
7627 goto predicate;
7628
Michal Vasko3e48bf32020-06-01 08:39:07 +02007629 case LYXP_TOKEN_OPER_PATH:
7630 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02007631 /* AbsoluteLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007632 rc = eval_absolute_location_path(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007633 LY_CHECK_RET(rc);
7634 break;
7635
7636 case LYXP_TOKEN_LITERAL:
7637 /* Literal */
7638 if (!set || (options & LYXP_SCNODE_ALL)) {
7639 if (set) {
7640 set_scnode_clear_ctx(set);
7641 }
Michal Vasko004d3152020-06-11 19:59:22 +02007642 eval_literal(exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007643 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007644 eval_literal(exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007645 }
7646
7647 parent_pos_pred = 1;
7648 goto predicate;
7649
7650 case LYXP_TOKEN_NUMBER:
7651 /* Number */
7652 if (!set || (options & LYXP_SCNODE_ALL)) {
7653 if (set) {
7654 set_scnode_clear_ctx(set);
7655 }
Michal Vasko004d3152020-06-11 19:59:22 +02007656 rc = eval_number(NULL, exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007657 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007658 rc = eval_number(set->ctx, exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007659 }
7660 LY_CHECK_RET(rc);
7661
7662 parent_pos_pred = 1;
7663 goto predicate;
7664
7665 default:
Michal Vasko004d3152020-06-11 19:59:22 +02007666 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INTOK, lyxp_print_token(exp->tokens[*tok_idx]),
7667 &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007668 return LY_EVALID;
7669 }
7670
7671 return LY_SUCCESS;
7672
7673predicate:
7674 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007675 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7676 rc = eval_predicate(exp, tok_idx, set, options, parent_pos_pred);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007677 LY_CHECK_RET(rc);
7678 }
7679
7680 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007681 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007682
7683 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02007684 if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007685 all_desc = 0;
7686 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007687 all_desc = 1;
7688 }
7689
7690 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007691 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7692 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007693
Michal Vasko004d3152020-06-11 19:59:22 +02007694 rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007695 LY_CHECK_RET(rc);
7696 }
7697
7698 return LY_SUCCESS;
7699}
7700
7701/**
7702 * @brief Evaluate UnionExpr. Logs directly on error.
7703 *
Michal Vaskod3678892020-05-21 10:06:58 +02007704 * [20] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007705 *
7706 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007707 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007708 * @param[in] repeat How many times this expression is repeated.
7709 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7710 * @param[in] options XPath options.
7711 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7712 */
7713static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007714eval_union_expr(struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, int options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007715{
7716 LY_ERR rc = LY_SUCCESS;
7717 struct lyxp_set orig_set, set2;
7718 uint16_t i;
7719
7720 assert(repeat);
7721
7722 set_init(&orig_set, set);
7723 set_init(&set2, set);
7724
7725 set_fill_set(&orig_set, set);
7726
Michal Vasko004d3152020-06-11 19:59:22 +02007727 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007728 LY_CHECK_GOTO(rc, cleanup);
7729
7730 /* ('|' PathExpr)* */
7731 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007732 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_UNI);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007733 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007734 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7735 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007736
7737 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007738 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007739 LY_CHECK_GOTO(rc, cleanup);
7740 continue;
7741 }
7742
7743 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02007744 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007745 LY_CHECK_GOTO(rc, cleanup);
7746
7747 /* eval */
7748 if (options & LYXP_SCNODE_ALL) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01007749 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007750 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007751 rc = moveto_union(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007752 LY_CHECK_GOTO(rc, cleanup);
7753 }
7754 }
7755
7756cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007757 lyxp_set_free_content(&orig_set);
7758 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007759 return rc;
7760}
7761
7762/**
7763 * @brief Evaluate UnaryExpr. Logs directly on error.
7764 *
Michal Vaskod3678892020-05-21 10:06:58 +02007765 * [19] UnaryExpr ::= UnionExpr | '-' UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007766 *
7767 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007768 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007769 * @param[in] repeat How many times this expression is repeated.
7770 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7771 * @param[in] options XPath options.
7772 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7773 */
7774static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007775eval_unary_expr(struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, int options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007776{
7777 LY_ERR rc;
7778 uint16_t this_op, i;
7779
7780 assert(repeat);
7781
7782 /* ('-')+ */
Michal Vasko004d3152020-06-11 19:59:22 +02007783 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007784 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007785 assert(!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH) && (exp->expr[exp->tok_pos[*tok_idx]] == '-'));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007786
7787 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007788 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7789 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007790 }
7791
Michal Vasko004d3152020-06-11 19:59:22 +02007792 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNARY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007793 LY_CHECK_RET(rc);
7794
7795 if (set && (repeat % 2)) {
7796 if (options & LYXP_SCNODE_ALL) {
7797 warn_operands(set->ctx, set, NULL, 1, exp->expr, exp->tok_pos[this_op]);
7798 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007799 rc = moveto_op_math(set, NULL, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007800 LY_CHECK_RET(rc);
7801 }
7802 }
7803
7804 return LY_SUCCESS;
7805}
7806
7807/**
7808 * @brief Evaluate MultiplicativeExpr. Logs directly on error.
7809 *
Michal Vaskod3678892020-05-21 10:06:58 +02007810 * [18] MultiplicativeExpr ::= UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007811 * | MultiplicativeExpr '*' UnaryExpr
7812 * | MultiplicativeExpr 'div' UnaryExpr
7813 * | MultiplicativeExpr 'mod' UnaryExpr
7814 *
7815 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007816 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007817 * @param[in] repeat How many times this expression is repeated.
7818 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7819 * @param[in] options XPath options.
7820 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7821 */
7822static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007823eval_multiplicative_expr(struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, int options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007824{
7825 LY_ERR rc;
7826 uint16_t this_op;
7827 struct lyxp_set orig_set, set2;
7828 uint16_t i;
7829
7830 assert(repeat);
7831
7832 set_init(&orig_set, set);
7833 set_init(&set2, set);
7834
7835 set_fill_set(&orig_set, set);
7836
Michal Vasko004d3152020-06-11 19:59:22 +02007837 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007838 LY_CHECK_GOTO(rc, cleanup);
7839
7840 /* ('*' / 'div' / 'mod' UnaryExpr)* */
7841 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007842 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007843
Michal Vasko004d3152020-06-11 19:59:22 +02007844 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007845 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007846 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7847 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007848
7849 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007850 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007851 LY_CHECK_GOTO(rc, cleanup);
7852 continue;
7853 }
7854
7855 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02007856 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007857 LY_CHECK_GOTO(rc, cleanup);
7858
7859 /* eval */
7860 if (options & LYXP_SCNODE_ALL) {
7861 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007862 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007863 set_scnode_clear_ctx(set);
7864 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007865 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007866 LY_CHECK_GOTO(rc, cleanup);
7867 }
7868 }
7869
7870cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007871 lyxp_set_free_content(&orig_set);
7872 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007873 return rc;
7874}
7875
7876/**
7877 * @brief Evaluate AdditiveExpr. Logs directly on error.
7878 *
Michal Vaskod3678892020-05-21 10:06:58 +02007879 * [17] AdditiveExpr ::= MultiplicativeExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007880 * | AdditiveExpr '+' MultiplicativeExpr
7881 * | AdditiveExpr '-' MultiplicativeExpr
7882 *
7883 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007884 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007885 * @param[in] repeat How many times this expression is repeated.
7886 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7887 * @param[in] options XPath options.
7888 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7889 */
7890static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007891eval_additive_expr(struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, int options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007892{
7893 LY_ERR rc;
7894 uint16_t this_op;
7895 struct lyxp_set orig_set, set2;
7896 uint16_t i;
7897
7898 assert(repeat);
7899
7900 set_init(&orig_set, set);
7901 set_init(&set2, set);
7902
7903 set_fill_set(&orig_set, set);
7904
Michal Vasko004d3152020-06-11 19:59:22 +02007905 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007906 LY_CHECK_GOTO(rc, cleanup);
7907
7908 /* ('+' / '-' MultiplicativeExpr)* */
7909 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007910 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007911
Michal Vasko004d3152020-06-11 19:59:22 +02007912 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007913 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007914 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7915 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007916
7917 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007918 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007919 LY_CHECK_GOTO(rc, cleanup);
7920 continue;
7921 }
7922
7923 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02007924 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007925 LY_CHECK_GOTO(rc, cleanup);
7926
7927 /* eval */
7928 if (options & LYXP_SCNODE_ALL) {
7929 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007930 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007931 set_scnode_clear_ctx(set);
7932 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007933 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007934 LY_CHECK_GOTO(rc, cleanup);
7935 }
7936 }
7937
7938cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007939 lyxp_set_free_content(&orig_set);
7940 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007941 return rc;
7942}
7943
7944/**
7945 * @brief Evaluate RelationalExpr. Logs directly on error.
7946 *
Michal Vaskod3678892020-05-21 10:06:58 +02007947 * [16] RelationalExpr ::= AdditiveExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007948 * | RelationalExpr '<' AdditiveExpr
7949 * | RelationalExpr '>' AdditiveExpr
7950 * | RelationalExpr '<=' AdditiveExpr
7951 * | RelationalExpr '>=' AdditiveExpr
7952 *
7953 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007954 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007955 * @param[in] repeat How many times this expression is repeated.
7956 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7957 * @param[in] options XPath options.
7958 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7959 */
7960static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007961eval_relational_expr(struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, int options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007962{
7963 LY_ERR rc;
7964 uint16_t this_op;
7965 struct lyxp_set orig_set, set2;
7966 uint16_t i;
7967
7968 assert(repeat);
7969
7970 set_init(&orig_set, set);
7971 set_init(&set2, set);
7972
7973 set_fill_set(&orig_set, set);
7974
Michal Vasko004d3152020-06-11 19:59:22 +02007975 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007976 LY_CHECK_GOTO(rc, cleanup);
7977
7978 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
7979 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007980 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007981
Michal Vasko004d3152020-06-11 19:59:22 +02007982 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_COMP);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007983 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007984 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7985 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007986
7987 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007988 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007989 LY_CHECK_GOTO(rc, cleanup);
7990 continue;
7991 }
7992
7993 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02007994 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007995 LY_CHECK_GOTO(rc, cleanup);
7996
7997 /* eval */
7998 if (options & LYXP_SCNODE_ALL) {
7999 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008000 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008001 set_scnode_clear_ctx(set);
8002 } else {
8003 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options);
8004 LY_CHECK_GOTO(rc, cleanup);
8005 }
8006 }
8007
8008cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008009 lyxp_set_free_content(&orig_set);
8010 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008011 return rc;
8012}
8013
8014/**
8015 * @brief Evaluate EqualityExpr. Logs directly on error.
8016 *
Michal Vaskod3678892020-05-21 10:06:58 +02008017 * [15] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008018 * | EqualityExpr '!=' RelationalExpr
8019 *
8020 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008021 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008022 * @param[in] repeat How many times this expression is repeated.
8023 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8024 * @param[in] options XPath options.
8025 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8026 */
8027static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02008028eval_equality_expr(struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, int options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008029{
8030 LY_ERR rc;
8031 uint16_t this_op;
8032 struct lyxp_set orig_set, set2;
8033 uint16_t i;
8034
8035 assert(repeat);
8036
8037 set_init(&orig_set, set);
8038 set_init(&set2, set);
8039
8040 set_fill_set(&orig_set, set);
8041
Michal Vasko004d3152020-06-11 19:59:22 +02008042 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008043 LY_CHECK_GOTO(rc, cleanup);
8044
8045 /* ('=' / '!=' RelationalExpr)* */
8046 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008047 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008048
Michal Vasko004d3152020-06-11 19:59:22 +02008049 assert((exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_EQUAL) || (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_NEQUAL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008050 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02008051 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
8052 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008053
8054 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02008055 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008056 LY_CHECK_GOTO(rc, cleanup);
8057 continue;
8058 }
8059
8060 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008061 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008062 LY_CHECK_GOTO(rc, cleanup);
8063
8064 /* eval */
8065 if (options & LYXP_SCNODE_ALL) {
8066 warn_operands(set->ctx, set, &set2, 0, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vasko004d3152020-06-11 19:59:22 +02008067 warn_equality_value(exp, set, *tok_idx - 1, this_op - 1, *tok_idx - 1);
8068 warn_equality_value(exp, &set2, this_op - 1, this_op - 1, *tok_idx - 1);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008069 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008070 set_scnode_clear_ctx(set);
8071 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008072 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options);
8073 LY_CHECK_GOTO(rc, cleanup);
8074 }
8075 }
8076
8077cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008078 lyxp_set_free_content(&orig_set);
8079 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008080 return rc;
8081}
8082
8083/**
8084 * @brief Evaluate AndExpr. Logs directly on error.
8085 *
Michal Vaskod3678892020-05-21 10:06:58 +02008086 * [14] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008087 *
8088 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008089 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008090 * @param[in] repeat How many times this expression is repeated.
8091 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8092 * @param[in] options XPath options.
8093 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8094 */
8095static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02008096eval_and_expr(struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, int options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008097{
8098 LY_ERR rc;
8099 struct lyxp_set orig_set, set2;
8100 uint16_t i;
8101
8102 assert(repeat);
8103
8104 set_init(&orig_set, set);
8105 set_init(&set2, set);
8106
8107 set_fill_set(&orig_set, set);
8108
Michal Vasko004d3152020-06-11 19:59:22 +02008109 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008110 LY_CHECK_GOTO(rc, cleanup);
8111
8112 /* cast to boolean, we know that will be the final result */
8113 if (set && (options & LYXP_SCNODE_ALL)) {
8114 set_scnode_clear_ctx(set);
8115 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008116 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008117 }
8118
8119 /* ('and' EqualityExpr)* */
8120 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008121 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
8122 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (!set || !set->val.bln ? "skipped" : "parsed"),
8123 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
8124 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008125
8126 /* lazy evaluation */
Michal Vasko004d3152020-06-11 19:59:22 +02008127 if (!set || ((set->type == LYXP_SET_BOOLEAN) && !set->val.bln)) {
8128 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008129 LY_CHECK_GOTO(rc, cleanup);
8130 continue;
8131 }
8132
8133 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008134 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008135 LY_CHECK_GOTO(rc, cleanup);
8136
8137 /* eval - just get boolean value actually */
8138 if (set->type == LYXP_SET_SCNODE_SET) {
8139 set_scnode_clear_ctx(&set2);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008140 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008141 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008142 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008143 set_fill_set(set, &set2);
8144 }
8145 }
8146
8147cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008148 lyxp_set_free_content(&orig_set);
8149 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008150 return rc;
8151}
8152
8153/**
8154 * @brief Evaluate OrExpr. Logs directly on error.
8155 *
Michal Vaskod3678892020-05-21 10:06:58 +02008156 * [13] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008157 *
8158 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008159 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008160 * @param[in] repeat How many times this expression is repeated.
8161 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8162 * @param[in] options XPath options.
8163 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8164 */
8165static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02008166eval_or_expr(struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, int options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008167{
8168 LY_ERR rc;
8169 struct lyxp_set orig_set, set2;
8170 uint16_t i;
8171
8172 assert(repeat);
8173
8174 set_init(&orig_set, set);
8175 set_init(&set2, set);
8176
8177 set_fill_set(&orig_set, set);
8178
Michal Vasko004d3152020-06-11 19:59:22 +02008179 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008180 LY_CHECK_GOTO(rc, cleanup);
8181
8182 /* cast to boolean, we know that will be the final result */
8183 if (set && (options & LYXP_SCNODE_ALL)) {
8184 set_scnode_clear_ctx(set);
8185 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008186 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008187 }
8188
8189 /* ('or' AndExpr)* */
8190 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008191 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
8192 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (!set || set->val.bln ? "skipped" : "parsed"),
8193 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
8194 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008195
8196 /* lazy evaluation */
Michal Vasko004d3152020-06-11 19:59:22 +02008197 if (!set || ((set->type == LYXP_SET_BOOLEAN) && set->val.bln)) {
8198 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008199 LY_CHECK_GOTO(rc, cleanup);
8200 continue;
8201 }
8202
8203 set_fill_set(&set2, &orig_set);
8204 /* expr_type cound have been LYXP_EXPR_NONE in all these later calls (except for the first one),
8205 * but it does not matter */
Michal Vasko004d3152020-06-11 19:59:22 +02008206 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008207 LY_CHECK_GOTO(rc, cleanup);
8208
8209 /* eval - just get boolean value actually */
8210 if (set->type == LYXP_SET_SCNODE_SET) {
8211 set_scnode_clear_ctx(&set2);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008212 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008213 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008214 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008215 set_fill_set(set, &set2);
8216 }
8217 }
8218
8219cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008220 lyxp_set_free_content(&orig_set);
8221 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008222 return rc;
8223}
8224
8225/**
Michal Vasko004d3152020-06-11 19:59:22 +02008226 * @brief Decide what expression is at the pointer @p tok_idx and evaluate it accordingly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008227 *
8228 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008229 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008230 * @param[in] etype Expression type to evaluate.
8231 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8232 * @param[in] options XPath options.
8233 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8234 */
8235static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02008236eval_expr_select(struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_expr_type etype, struct lyxp_set *set, int options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008237{
8238 uint16_t i, count;
8239 enum lyxp_expr_type next_etype;
8240 LY_ERR rc;
8241
8242 /* process operator repeats */
Michal Vasko004d3152020-06-11 19:59:22 +02008243 if (!exp->repeat[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008244 next_etype = LYXP_EXPR_NONE;
8245 } else {
8246 /* find etype repeat */
Michal Vasko004d3152020-06-11 19:59:22 +02008247 for (i = 0; exp->repeat[*tok_idx][i] > etype; ++i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008248
8249 /* select one-priority lower because etype expression called us */
8250 if (i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008251 next_etype = exp->repeat[*tok_idx][i - 1];
Michal Vasko03ff5a72019-09-11 13:49:33 +02008252 /* count repeats for that expression */
Michal Vasko004d3152020-06-11 19:59:22 +02008253 for (count = 0; i && exp->repeat[*tok_idx][i - 1] == next_etype; ++count, --i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008254 } else {
8255 next_etype = LYXP_EXPR_NONE;
8256 }
8257 }
8258
8259 /* decide what expression are we parsing based on the repeat */
8260 switch (next_etype) {
8261 case LYXP_EXPR_OR:
Michal Vasko004d3152020-06-11 19:59:22 +02008262 rc = eval_or_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008263 break;
8264 case LYXP_EXPR_AND:
Michal Vasko004d3152020-06-11 19:59:22 +02008265 rc = eval_and_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008266 break;
8267 case LYXP_EXPR_EQUALITY:
Michal Vasko004d3152020-06-11 19:59:22 +02008268 rc = eval_equality_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008269 break;
8270 case LYXP_EXPR_RELATIONAL:
Michal Vasko004d3152020-06-11 19:59:22 +02008271 rc = eval_relational_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008272 break;
8273 case LYXP_EXPR_ADDITIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02008274 rc = eval_additive_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008275 break;
8276 case LYXP_EXPR_MULTIPLICATIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02008277 rc = eval_multiplicative_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008278 break;
8279 case LYXP_EXPR_UNARY:
Michal Vasko004d3152020-06-11 19:59:22 +02008280 rc = eval_unary_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008281 break;
8282 case LYXP_EXPR_UNION:
Michal Vasko004d3152020-06-11 19:59:22 +02008283 rc = eval_union_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008284 break;
8285 case LYXP_EXPR_NONE:
Michal Vasko004d3152020-06-11 19:59:22 +02008286 rc = eval_path_expr(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008287 break;
8288 default:
8289 LOGINT_RET(set->ctx);
8290 }
8291
8292 return rc;
8293}
8294
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008295/**
8296 * @brief Get root type.
8297 *
8298 * @param[in] ctx_node Context node.
8299 * @param[in] ctx_scnode Schema context node.
8300 * @param[in] options XPath options.
8301 * @return Root type.
8302 */
8303static enum lyxp_node_type
8304lyxp_get_root_type(const struct lyd_node *ctx_node, const struct lysc_node *ctx_scnode, int options)
8305{
8306 if (options & LYXP_SCNODE_ALL) {
8307 if (options & LYXP_SCNODE) {
8308 /* general root that can access everything */
8309 return LYXP_NODE_ROOT;
8310 } else if (!ctx_scnode || (ctx_scnode->flags & LYS_CONFIG_W)) {
8311 /* root context node can access only config data (because we said so, it is unspecified) */
8312 return LYXP_NODE_ROOT_CONFIG;
8313 } else {
8314 return LYXP_NODE_ROOT;
8315 }
8316 }
8317
8318 if (!ctx_node || (ctx_node->schema->flags & LYS_CONFIG_W)) {
8319 /* root context node can access only config data (because we said so, it is unspecified) */
8320 return LYXP_NODE_ROOT_CONFIG;
8321 }
8322
8323 return LYXP_NODE_ROOT;
8324}
8325
Michal Vasko03ff5a72019-09-11 13:49:33 +02008326LY_ERR
Michal Vaskoecd62de2019-11-13 12:35:11 +01008327lyxp_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 +01008328 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 +02008329{
Michal Vasko004d3152020-06-11 19:59:22 +02008330 uint16_t tok_idx = 0;
8331 const struct lyd_node *real_ctx_node;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008332 LY_ERR rc;
8333
Michal Vasko004d3152020-06-11 19:59:22 +02008334 LY_CHECK_ARG_RET(NULL, exp, local_mod, ctx_node, set, LY_EINVAL);
8335
8336 if ((ctx_node_type == LYXP_NODE_ROOT) || (ctx_node_type == LYXP_NODE_ROOT_CONFIG)) {
8337 /* we always need some context node because it is used for resolving unqualified names */
8338 real_ctx_node = NULL;
8339 } else {
8340 real_ctx_node = ctx_node;
8341 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008342
8343 /* prepare set for evaluation */
Michal Vasko004d3152020-06-11 19:59:22 +02008344 tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008345 memset(set, 0, sizeof *set);
Michal Vaskod3678892020-05-21 10:06:58 +02008346 set->type = LYXP_SET_NODE_SET;
Michal Vasko004d3152020-06-11 19:59:22 +02008347 set_insert_node(set, (struct lyd_node *)real_ctx_node, 0, ctx_node_type, 0);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008348 set->ctx = local_mod->ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008349 set->ctx_node = ctx_node;
Michal Vasko004d3152020-06-11 19:59:22 +02008350 set->root_type = lyxp_get_root_type(real_ctx_node, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008351 set->local_mod = local_mod;
Michal Vaskof03ed032020-03-04 13:31:44 +01008352 set->tree = tree;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008353 set->format = format;
8354
8355 /* evaluate */
Michal Vasko004d3152020-06-11 19:59:22 +02008356 rc = eval_expr_select(exp, &tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008357 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02008358 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008359 }
8360
Michal Vasko03ff5a72019-09-11 13:49:33 +02008361 return rc;
8362}
8363
8364#if 0
8365
8366/* full xml printing of set elements, not used currently */
8367
8368void
8369lyxp_set_print_xml(FILE *f, struct lyxp_set *set)
8370{
8371 uint32_t i;
8372 char *str_num;
8373 struct lyout out;
8374
8375 memset(&out, 0, sizeof out);
8376
8377 out.type = LYOUT_STREAM;
8378 out.method.f = f;
8379
8380 switch (set->type) {
8381 case LYXP_SET_EMPTY:
8382 ly_print(&out, "Empty XPath set\n\n");
8383 break;
8384 case LYXP_SET_BOOLEAN:
8385 ly_print(&out, "Boolean XPath set:\n");
8386 ly_print(&out, "%s\n\n", set->value.bool ? "true" : "false");
8387 break;
8388 case LYXP_SET_STRING:
8389 ly_print(&out, "String XPath set:\n");
8390 ly_print(&out, "\"%s\"\n\n", set->value.str);
8391 break;
8392 case LYXP_SET_NUMBER:
8393 ly_print(&out, "Number XPath set:\n");
8394
8395 if (isnan(set->value.num)) {
8396 str_num = strdup("NaN");
8397 } else if ((set->value.num == 0) || (set->value.num == -0.0f)) {
8398 str_num = strdup("0");
8399 } else if (isinf(set->value.num) && !signbit(set->value.num)) {
8400 str_num = strdup("Infinity");
8401 } else if (isinf(set->value.num) && signbit(set->value.num)) {
8402 str_num = strdup("-Infinity");
8403 } else if ((long long)set->value.num == set->value.num) {
8404 if (asprintf(&str_num, "%lld", (long long)set->value.num) == -1) {
8405 str_num = NULL;
8406 }
8407 } else {
8408 if (asprintf(&str_num, "%03.1Lf", set->value.num) == -1) {
8409 str_num = NULL;
8410 }
8411 }
8412 if (!str_num) {
8413 LOGMEM;
8414 return;
8415 }
8416 ly_print(&out, "%s\n\n", str_num);
8417 free(str_num);
8418 break;
8419 case LYXP_SET_NODE_SET:
8420 ly_print(&out, "Node XPath set:\n");
8421
8422 for (i = 0; i < set->used; ++i) {
8423 ly_print(&out, "%d. ", i + 1);
8424 switch (set->node_type[i]) {
8425 case LYXP_NODE_ROOT_ALL:
8426 ly_print(&out, "ROOT all\n\n");
8427 break;
8428 case LYXP_NODE_ROOT_CONFIG:
8429 ly_print(&out, "ROOT config\n\n");
8430 break;
8431 case LYXP_NODE_ROOT_STATE:
8432 ly_print(&out, "ROOT state\n\n");
8433 break;
8434 case LYXP_NODE_ROOT_NOTIF:
8435 ly_print(&out, "ROOT notification \"%s\"\n\n", set->value.nodes[i]->schema->name);
8436 break;
8437 case LYXP_NODE_ROOT_RPC:
8438 ly_print(&out, "ROOT rpc \"%s\"\n\n", set->value.nodes[i]->schema->name);
8439 break;
8440 case LYXP_NODE_ROOT_OUTPUT:
8441 ly_print(&out, "ROOT output \"%s\"\n\n", set->value.nodes[i]->schema->name);
8442 break;
8443 case LYXP_NODE_ELEM:
8444 ly_print(&out, "ELEM \"%s\"\n", set->value.nodes[i]->schema->name);
8445 xml_print_node(&out, 1, set->value.nodes[i], 1, LYP_FORMAT);
8446 ly_print(&out, "\n");
8447 break;
8448 case LYXP_NODE_TEXT:
8449 ly_print(&out, "TEXT \"%s\"\n\n", ((struct lyd_node_leaf_list *)set->value.nodes[i])->value_str);
8450 break;
8451 case LYXP_NODE_ATTR:
8452 ly_print(&out, "ATTR \"%s\" = \"%s\"\n\n", set->value.attrs[i]->name, set->value.attrs[i]->value);
8453 break;
8454 }
8455 }
8456 break;
8457 }
8458}
8459
8460#endif
8461
8462LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008463lyxp_set_cast(struct lyxp_set *set, enum lyxp_set_type target)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008464{
8465 long double num;
8466 char *str;
8467 LY_ERR rc;
8468
8469 if (!set || (set->type == target)) {
8470 return LY_SUCCESS;
8471 }
8472
8473 /* it's not possible to convert anything into a node set */
Michal Vaskod3678892020-05-21 10:06:58 +02008474 assert(target != LYXP_SET_NODE_SET);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008475
8476 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vaskod3678892020-05-21 10:06:58 +02008477 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008478 return LY_EINVAL;
8479 }
8480
8481 /* to STRING */
Michal Vaskod3678892020-05-21 10:06:58 +02008482 if ((target == LYXP_SET_STRING) || ((target == LYXP_SET_NUMBER) && (set->type == LYXP_SET_NODE_SET))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008483 switch (set->type) {
8484 case LYXP_SET_NUMBER:
8485 if (isnan(set->val.num)) {
8486 set->val.str = strdup("NaN");
8487 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8488 } else if ((set->val.num == 0) || (set->val.num == -0.0f)) {
8489 set->val.str = strdup("0");
8490 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8491 } else if (isinf(set->val.num) && !signbit(set->val.num)) {
8492 set->val.str = strdup("Infinity");
8493 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8494 } else if (isinf(set->val.num) && signbit(set->val.num)) {
8495 set->val.str = strdup("-Infinity");
8496 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8497 } else if ((long long)set->val.num == set->val.num) {
8498 if (asprintf(&str, "%lld", (long long)set->val.num) == -1) {
8499 LOGMEM_RET(set->ctx);
8500 }
8501 set->val.str = str;
8502 } else {
8503 if (asprintf(&str, "%03.1Lf", set->val.num) == -1) {
8504 LOGMEM_RET(set->ctx);
8505 }
8506 set->val.str = str;
8507 }
8508 break;
8509 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02008510 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008511 set->val.str = strdup("true");
8512 } else {
8513 set->val.str = strdup("false");
8514 }
8515 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM);
8516 break;
8517 case LYXP_SET_NODE_SET:
8518 assert(set->used);
8519
8520 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008521 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008522
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008523 rc = cast_node_set_to_string(set, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008524 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02008525 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008526 set->val.str = str;
8527 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008528 default:
8529 LOGINT_RET(set->ctx);
8530 }
8531 set->type = LYXP_SET_STRING;
8532 }
8533
8534 /* to NUMBER */
8535 if (target == LYXP_SET_NUMBER) {
8536 switch (set->type) {
8537 case LYXP_SET_STRING:
8538 num = cast_string_to_number(set->val.str);
Michal Vaskod3678892020-05-21 10:06:58 +02008539 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008540 set->val.num = num;
8541 break;
8542 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02008543 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008544 set->val.num = 1;
8545 } else {
8546 set->val.num = 0;
8547 }
8548 break;
8549 default:
8550 LOGINT_RET(set->ctx);
8551 }
8552 set->type = LYXP_SET_NUMBER;
8553 }
8554
8555 /* to BOOLEAN */
8556 if (target == LYXP_SET_BOOLEAN) {
8557 switch (set->type) {
8558 case LYXP_SET_NUMBER:
8559 if ((set->val.num == 0) || (set->val.num == -0.0f) || isnan(set->val.num)) {
Michal Vasko004d3152020-06-11 19:59:22 +02008560 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008561 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02008562 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008563 }
8564 break;
8565 case LYXP_SET_STRING:
8566 if (set->val.str[0]) {
Michal Vaskod3678892020-05-21 10:06:58 +02008567 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008568 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008569 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02008570 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008571 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008572 }
8573 break;
8574 case LYXP_SET_NODE_SET:
Michal Vaskod3678892020-05-21 10:06:58 +02008575 if (set->used) {
8576 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008577 set->val.bln = 1;
Michal Vaskod3678892020-05-21 10:06:58 +02008578 } else {
8579 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008580 set->val.bln = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02008581 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008582 break;
8583 default:
8584 LOGINT_RET(set->ctx);
8585 }
8586 set->type = LYXP_SET_BOOLEAN;
8587 }
8588
Michal Vasko03ff5a72019-09-11 13:49:33 +02008589 return LY_SUCCESS;
8590}
8591
8592LY_ERR
8593lyxp_atomize(struct lyxp_expr *exp, LYD_FORMAT format, const struct lys_module *local_mod, const struct lysc_node *ctx_scnode,
8594 enum lyxp_node_type ctx_scnode_type, struct lyxp_set *set, int options)
8595{
8596 struct ly_ctx *ctx;
Michal Vasko004d3152020-06-11 19:59:22 +02008597 const struct lysc_node *real_ctx_scnode;
8598 uint16_t tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008599
Michal Vasko004d3152020-06-11 19:59:22 +02008600 LY_CHECK_ARG_RET(NULL, exp, local_mod, ctx_scnode, set, LY_EINVAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008601
8602 ctx = local_mod->ctx;
Michal Vasko004d3152020-06-11 19:59:22 +02008603 if ((ctx_scnode_type == LYXP_NODE_ROOT) || (ctx_scnode_type == LYXP_NODE_ROOT_CONFIG)) {
8604 /* we always need some context node because it is used for resolving unqualified names */
8605 real_ctx_scnode = NULL;
8606 } else {
8607 real_ctx_scnode = ctx_scnode;
8608 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008609
8610 /* prepare set for evaluation */
Michal Vasko004d3152020-06-11 19:59:22 +02008611 tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008612 memset(set, 0, sizeof *set);
8613 set->type = LYXP_SET_SCNODE_SET;
Michal Vasko004d3152020-06-11 19:59:22 +02008614 lyxp_set_scnode_insert_node(set, real_ctx_scnode, ctx_scnode_type);
Michal Vasko5c4e5892019-11-14 12:31:38 +01008615 set->val.scnodes[0].in_ctx = -2;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008616 set->ctx = ctx;
8617 set->ctx_scnode = ctx_scnode;
Michal Vasko004d3152020-06-11 19:59:22 +02008618 set->root_type = lyxp_get_root_type(NULL, real_ctx_scnode, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008619 set->local_mod = local_mod;
8620 set->format = format;
8621
8622 /* evaluate */
Michal Vasko004d3152020-06-11 19:59:22 +02008623 return eval_expr_select(exp, &tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008624}