blob: 39c7365b88680757d64da6c5fe84fe790237dc84 [file] [log] [blame]
Radek Krejcib1646a92018-11-02 16:08:26 +01001/**
2 * @file xpath.c
3 * @author Michal Vasko <mvasko@cesnet.cz>
4 * @brief YANG XPath evaluation functions
5 *
Michal Vasko61ac2f62020-05-25 12:39:51 +02006 * Copyright (c) 2015 - 2020 CESNET, z.s.p.o.
Radek Krejcib1646a92018-11-02 16:08:26 +01007 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
Michal Vasko03ff5a72019-09-11 13:49:33 +020014#define _GNU_SOURCE
15
16/* needed by libmath functions isfinite(), isinf(), isnan(), signbit(), ... */
17#define _ISOC99_SOURCE
Radek Krejcib1646a92018-11-02 16:08:26 +010018
Radek Krejci535ea9f2020-05-29 16:01:05 +020019#include "xpath.h"
Radek Krejcib1646a92018-11-02 16:08:26 +010020
Radek Krejci535ea9f2020-05-29 16:01:05 +020021#include <assert.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010022#include <ctype.h>
Radek Krejci535ea9f2020-05-29 16:01:05 +020023#include <errno.h>
24#include <limits.h>
25#include <math.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020026#include <stdint.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010027#include <stdio.h>
28#include <stdlib.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010029#include <string.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010030
Radek Krejci535ea9f2020-05-29 16:01:05 +020031#include "common.h"
32#include "context.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020033#include "dict.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020034#include "hash_table.h"
Michal Vasko004d3152020-06-11 19:59:22 +020035#include "path.h"
Michal Vasko03ff5a72019-09-11 13:49:33 +020036#include "plugins_types.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020037#include "printer.h"
38#include "printer_data.h"
39#include "tree.h"
40#include "tree_data_internal.h"
41#include "tree_schema_internal.h"
42#include "xml.h"
Michal Vasko03ff5a72019-09-11 13:49:33 +020043
Michal Vasko004d3152020-06-11 19:59:22 +020044static LY_ERR reparse_or_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx);
45static 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 +020046
47/**
48 * @brief Print the type of an XPath \p set.
49 *
50 * @param[in] set Set to use.
51 * @return Set type string.
52 */
53static const char *
54print_set_type(struct lyxp_set *set)
55{
56 switch (set->type) {
Michal Vasko03ff5a72019-09-11 13:49:33 +020057 case LYXP_SET_NODE_SET:
58 return "node set";
59 case LYXP_SET_SCNODE_SET:
60 return "schema node set";
61 case LYXP_SET_BOOLEAN:
62 return "boolean";
63 case LYXP_SET_NUMBER:
64 return "number";
65 case LYXP_SET_STRING:
66 return "string";
67 }
68
69 return NULL;
70}
71
Michal Vasko24cddf82020-06-01 08:17:01 +020072const char *
73lyxp_print_token(enum lyxp_token tok)
Michal Vasko03ff5a72019-09-11 13:49:33 +020074{
75 switch (tok) {
76 case LYXP_TOKEN_PAR1:
77 return "(";
78 case LYXP_TOKEN_PAR2:
79 return ")";
80 case LYXP_TOKEN_BRACK1:
81 return "[";
82 case LYXP_TOKEN_BRACK2:
83 return "]";
84 case LYXP_TOKEN_DOT:
85 return ".";
86 case LYXP_TOKEN_DDOT:
87 return "..";
88 case LYXP_TOKEN_AT:
89 return "@";
90 case LYXP_TOKEN_COMMA:
91 return ",";
92 case LYXP_TOKEN_NAMETEST:
93 return "NameTest";
94 case LYXP_TOKEN_NODETYPE:
95 return "NodeType";
96 case LYXP_TOKEN_FUNCNAME:
97 return "FunctionName";
Michal Vasko3e48bf32020-06-01 08:39:07 +020098 case LYXP_TOKEN_OPER_LOG:
Michal Vasko03ff5a72019-09-11 13:49:33 +020099 return "Operator(Logic)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200100 case LYXP_TOKEN_OPER_EQUAL:
101 return "Operator(Equal)";
102 case LYXP_TOKEN_OPER_NEQUAL:
103 return "Operator(Non-equal)";
104 case LYXP_TOKEN_OPER_COMP:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200105 return "Operator(Comparison)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200106 case LYXP_TOKEN_OPER_MATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200107 return "Operator(Math)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200108 case LYXP_TOKEN_OPER_UNI:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200109 return "Operator(Union)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200110 case LYXP_TOKEN_OPER_PATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200111 return "Operator(Path)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200112 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko14676352020-05-29 11:35:55 +0200113 return "Operator(Recursive Path)";
Michal Vasko03ff5a72019-09-11 13:49:33 +0200114 case LYXP_TOKEN_LITERAL:
115 return "Literal";
116 case LYXP_TOKEN_NUMBER:
117 return "Number";
118 default:
119 LOGINT(NULL);
120 return "";
121 }
122}
123
124/**
125 * @brief Print the whole expression \p exp to debug output.
126 *
127 * @param[in] exp Expression to use.
128 */
129static void
130print_expr_struct_debug(struct lyxp_expr *exp)
131{
132 uint16_t i, j;
133 char tmp[128];
134
135 if (!exp || (ly_log_level < LY_LLDBG)) {
136 return;
137 }
138
139 LOGDBG(LY_LDGXPATH, "expression \"%s\":", exp->expr);
140 for (i = 0; i < exp->used; ++i) {
Michal Vasko24cddf82020-06-01 08:17:01 +0200141 sprintf(tmp, "\ttoken %s, in expression \"%.*s\"", lyxp_print_token(exp->tokens[i]), exp->tok_len[i],
Michal Vasko03ff5a72019-09-11 13:49:33 +0200142 &exp->expr[exp->tok_pos[i]]);
143 if (exp->repeat[i]) {
144 sprintf(tmp + strlen(tmp), " (repeat %d", exp->repeat[i][0]);
145 for (j = 1; exp->repeat[i][j]; ++j) {
146 sprintf(tmp + strlen(tmp), ", %d", exp->repeat[i][j]);
147 }
148 strcat(tmp, ")");
149 }
150 LOGDBG(LY_LDGXPATH, tmp);
151 }
152}
153
154#ifndef NDEBUG
155
156/**
157 * @brief Print XPath set content to debug output.
158 *
159 * @param[in] set Set to print.
160 */
161static void
162print_set_debug(struct lyxp_set *set)
163{
164 uint32_t i;
165 char *str;
166 int dynamic;
167 struct lyxp_set_node *item;
168 struct lyxp_set_scnode *sitem;
169
170 if (ly_log_level < LY_LLDBG) {
171 return;
172 }
173
174 switch (set->type) {
175 case LYXP_SET_NODE_SET:
176 LOGDBG(LY_LDGXPATH, "set NODE SET:");
177 for (i = 0; i < set->used; ++i) {
178 item = &set->val.nodes[i];
179
180 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +0100181 case LYXP_NODE_NONE:
182 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): NONE", i + 1, item->pos);
183 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200184 case LYXP_NODE_ROOT:
185 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT", i + 1, item->pos);
186 break;
187 case LYXP_NODE_ROOT_CONFIG:
188 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT CONFIG", i + 1, item->pos);
189 break;
190 case LYXP_NODE_ELEM:
191 if ((item->node->schema->nodetype == LYS_LIST)
192 && (((struct lyd_node_inner *)item->node)->child->schema->nodetype == LYS_LEAF)) {
193 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (1st child val: %s)", i + 1, item->pos,
194 item->node->schema->name,
195 (str = (char *)lyd_value2str((struct lyd_node_term *)lyd_node_children(item->node), &dynamic)));
196 if (dynamic) {
197 free(str);
198 }
199 } else if (((struct lyd_node_inner *)item->node)->schema->nodetype == LYS_LEAFLIST) {
200 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (val: %s)", i + 1, item->pos,
201 item->node->schema->name,
202 (str = (char *)lyd_value2str((struct lyd_node_term *)item->node, &dynamic)));
203 if (dynamic) {
204 free(str);
205 }
206 } else {
207 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s", i + 1, item->pos, item->node->schema->name);
208 }
209 break;
210 case LYXP_NODE_TEXT:
211 if (item->node->schema->nodetype & LYS_ANYDATA) {
212 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): TEXT <%s>", i + 1, item->pos,
213 item->node->schema->nodetype == LYS_ANYXML ? "anyxml" : "anydata");
214 } else {
215 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): TEXT %s", i + 1, item->pos,
216 (str = (char *)lyd_value2str((struct lyd_node_term *)item->node, &dynamic)));
217 if (dynamic) {
218 free(str);
219 }
220 }
221 break;
Michal Vasko9f96a052020-03-10 09:41:45 +0100222 case LYXP_NODE_META:
223 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): META %s = %s", i + 1, item->pos, set->val.meta[i].meta->name,
224 set->val.meta[i].meta->value);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200225 break;
226 }
227 }
228 break;
229
230 case LYXP_SET_SCNODE_SET:
231 LOGDBG(LY_LDGXPATH, "set SCNODE SET:");
232 for (i = 0; i < set->used; ++i) {
233 sitem = &set->val.scnodes[i];
234
235 switch (sitem->type) {
236 case LYXP_NODE_ROOT:
237 LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT", i + 1, sitem->in_ctx);
238 break;
239 case LYXP_NODE_ROOT_CONFIG:
240 LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT CONFIG", i + 1, sitem->in_ctx);
241 break;
242 case LYXP_NODE_ELEM:
243 LOGDBG(LY_LDGXPATH, "\t%d (%u): ELEM %s", i + 1, sitem->in_ctx, sitem->scnode->name);
244 break;
245 default:
246 LOGINT(NULL);
247 break;
248 }
249 }
250 break;
251
Michal Vasko03ff5a72019-09-11 13:49:33 +0200252 case LYXP_SET_BOOLEAN:
253 LOGDBG(LY_LDGXPATH, "set BOOLEAN");
Michal Vasko004d3152020-06-11 19:59:22 +0200254 LOGDBG(LY_LDGXPATH, "\t%s", (set->val.bln ? "true" : "false"));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200255 break;
256
257 case LYXP_SET_STRING:
258 LOGDBG(LY_LDGXPATH, "set STRING");
259 LOGDBG(LY_LDGXPATH, "\t%s", set->val.str);
260 break;
261
262 case LYXP_SET_NUMBER:
263 LOGDBG(LY_LDGXPATH, "set NUMBER");
264
265 if (isnan(set->val.num)) {
266 str = strdup("NaN");
267 } else if ((set->val.num == 0) || (set->val.num == -0.0f)) {
268 str = strdup("0");
269 } else if (isinf(set->val.num) && !signbit(set->val.num)) {
270 str = strdup("Infinity");
271 } else if (isinf(set->val.num) && signbit(set->val.num)) {
272 str = strdup("-Infinity");
273 } else if ((long long)set->val.num == set->val.num) {
274 if (asprintf(&str, "%lld", (long long)set->val.num) == -1) {
275 str = NULL;
276 }
277 } else {
278 if (asprintf(&str, "%03.1Lf", set->val.num) == -1) {
279 str = NULL;
280 }
281 }
282 LY_CHECK_ERR_RET(!str, LOGMEM(NULL), );
283
284 LOGDBG(LY_LDGXPATH, "\t%s", str);
285 free(str);
286 }
287}
288
289#endif
290
291/**
292 * @brief Realloc the string \p str.
293 *
294 * @param[in] ctx libyang context for logging.
295 * @param[in] needed How much free space is required.
296 * @param[in,out] str Pointer to the string to use.
297 * @param[in,out] used Used bytes in \p str.
298 * @param[in,out] size Allocated bytes in \p str.
299 * @return LY_ERR
300 */
301static LY_ERR
Michal Vasko52927e22020-03-16 17:26:14 +0100302cast_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 +0200303{
304 if (*size - *used < needed) {
305 do {
306 if ((UINT16_MAX - *size) < LYXP_STRING_CAST_SIZE_STEP) {
307 LOGERR(ctx, LY_EINVAL, "XPath string length limit (%u) reached.", UINT16_MAX);
308 return LY_EINVAL;
309 }
310 *size += LYXP_STRING_CAST_SIZE_STEP;
311 } while (*size - *used < needed);
312 *str = ly_realloc(*str, *size * sizeof(char));
313 LY_CHECK_ERR_RET(!(*str), LOGMEM(ctx), LY_EMEM);
314 }
315
316 return LY_SUCCESS;
317}
318
319/**
320 * @brief Cast nodes recursively to one string @p str.
321 *
322 * @param[in] node Node to cast.
323 * @param[in] fake_cont Whether to put the data into a "fake" container.
324 * @param[in] root_type Type of the XPath root.
325 * @param[in] indent Current indent.
326 * @param[in,out] str Resulting string.
327 * @param[in,out] used Used bytes in @p str.
328 * @param[in,out] size Allocated bytes in @p str.
329 * @return LY_ERR
330 */
331static LY_ERR
332cast_string_recursive(const struct lyd_node *node, int fake_cont, enum lyxp_node_type root_type, uint16_t indent, char **str,
333 uint16_t *used, uint16_t *size)
334{
335 char *buf, *line, *ptr;
336 const char *value_str;
337 int dynamic;
338 const struct lyd_node *child;
339 struct lyd_node_any *any;
340 LY_ERR rc;
341
342 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) {
343 return LY_SUCCESS;
344 }
345
346 if (fake_cont) {
347 rc = cast_string_realloc(LYD_NODE_CTX(node), 1, str, used, size);
348 LY_CHECK_RET(rc);
349 strcpy(*str + (*used - 1), "\n");
350 ++(*used);
351
352 ++indent;
353 }
354
355 switch (node->schema->nodetype) {
356 case LYS_CONTAINER:
357 case LYS_LIST:
358 case LYS_RPC:
359 case LYS_NOTIF:
360 rc = cast_string_realloc(LYD_NODE_CTX(node), 1, str, used, size);
361 LY_CHECK_RET(rc);
362 strcpy(*str + (*used - 1), "\n");
363 ++(*used);
364
365 for (child = lyd_node_children(node); child; child = child->next) {
366 rc = cast_string_recursive(child, 0, root_type, indent + 1, str, used, size);
367 LY_CHECK_RET(rc);
368 }
369
370 break;
371
372 case LYS_LEAF:
373 case LYS_LEAFLIST:
374 value_str = lyd_value2str(((struct lyd_node_term *)node), &dynamic);
375
376 /* print indent */
377 rc = cast_string_realloc(LYD_NODE_CTX(node), indent * 2 + strlen(value_str) + 1, str, used, size);
378 if (rc != LY_SUCCESS) {
379 if (dynamic) {
380 free((char *)value_str);
381 }
382 return rc;
383 }
384 memset(*str + (*used - 1), ' ', indent * 2);
385 *used += indent * 2;
386
387 /* print value */
388 if (*used == 1) {
389 sprintf(*str + (*used - 1), "%s", value_str);
390 *used += strlen(value_str);
391 } else {
392 sprintf(*str + (*used - 1), "%s\n", value_str);
393 *used += strlen(value_str) + 1;
394 }
395 if (dynamic) {
396 free((char *)value_str);
397 }
398
399 break;
400
401 case LYS_ANYXML:
402 case LYS_ANYDATA:
403 any = (struct lyd_node_any *)node;
404 if (!(void *)any->value.tree) {
405 /* no content */
406 buf = strdup("");
407 LY_CHECK_ERR_RET(!buf, LOGMEM(LYD_NODE_CTX(node)), LY_EMEM);
408 } else {
Radek Krejci241f6b52020-05-21 18:13:49 +0200409 struct ly_out *out;
Radek Krejcia5bba312020-01-09 15:41:20 +0100410
Michal Vasko03ff5a72019-09-11 13:49:33 +0200411 switch (any->value_type) {
412 case LYD_ANYDATA_STRING:
413 case LYD_ANYDATA_XML:
414 case LYD_ANYDATA_JSON:
415 buf = strdup(any->value.json);
416 LY_CHECK_ERR_RET(!buf, LOGMEM(LYD_NODE_CTX(node)), LY_EMEM);
417 break;
418 case LYD_ANYDATA_DATATREE:
Radek Krejci84ce7b12020-06-11 17:28:25 +0200419 LY_CHECK_RET(ly_out_new_memory(&buf, 0, &out));
Radek Krejcia5bba312020-01-09 15:41:20 +0100420 rc = lyd_print(out, any->value.tree, LYD_XML, LYDP_WITHSIBLINGS);
Radek Krejci241f6b52020-05-21 18:13:49 +0200421 ly_out_free(out, NULL, 0);
Radek Krejcia5bba312020-01-09 15:41:20 +0100422 LY_CHECK_RET(rc < 0, -rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200423 break;
424 /* TODO case LYD_ANYDATA_LYB:
425 LOGERR(LYD_NODE_CTX(node), LY_EINVAL, "Cannot convert LYB anydata into string.");
426 return -1;*/
427 }
428 }
429
430 line = strtok_r(buf, "\n", &ptr);
431 do {
432 rc = cast_string_realloc(LYD_NODE_CTX(node), indent * 2 + strlen(line) + 1, str, used, size);
433 if (rc != LY_SUCCESS) {
434 free(buf);
435 return rc;
436 }
437 memset(*str + (*used - 1), ' ', indent * 2);
438 *used += indent * 2;
439
440 strcpy(*str + (*used - 1), line);
441 *used += strlen(line);
442
443 strcpy(*str + (*used - 1), "\n");
444 *used += 1;
445 } while ((line = strtok_r(NULL, "\n", &ptr)));
446
447 free(buf);
448 break;
449
450 default:
451 LOGINT_RET(LYD_NODE_CTX(node));
452 }
453
454 if (fake_cont) {
455 rc = cast_string_realloc(LYD_NODE_CTX(node), 1, str, used, size);
456 LY_CHECK_RET(rc);
457 strcpy(*str + (*used - 1), "\n");
458 ++(*used);
459
460 --indent;
461 }
462
463 return LY_SUCCESS;
464}
465
466/**
467 * @brief Cast an element into a string.
468 *
469 * @param[in] node Node to cast.
470 * @param[in] fake_cont Whether to put the data into a "fake" container.
471 * @param[in] root_type Type of the XPath root.
472 * @param[out] str Element cast to dynamically-allocated string.
473 * @return LY_ERR
474 */
475static LY_ERR
476cast_string_elem(struct lyd_node *node, int fake_cont, enum lyxp_node_type root_type, char **str)
477{
478 uint16_t used, size;
479 LY_ERR rc;
480
481 *str = malloc(LYXP_STRING_CAST_SIZE_START * sizeof(char));
482 LY_CHECK_ERR_RET(!*str, LOGMEM(LYD_NODE_CTX(node)), LY_EMEM);
483 (*str)[0] = '\0';
484 used = 1;
485 size = LYXP_STRING_CAST_SIZE_START;
486
487 rc = cast_string_recursive(node, fake_cont, root_type, 0, str, &used, &size);
488 if (rc != LY_SUCCESS) {
489 free(*str);
490 return rc;
491 }
492
493 if (size > used) {
494 *str = ly_realloc(*str, used * sizeof(char));
495 LY_CHECK_ERR_RET(!*str, LOGMEM(LYD_NODE_CTX(node)), LY_EMEM);
496 }
497 return LY_SUCCESS;
498}
499
500/**
501 * @brief Cast a LYXP_SET_NODE_SET set into a string.
502 * Context position aware.
503 *
504 * @param[in] set Set to cast.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200505 * @param[out] str Cast dynamically-allocated string.
506 * @return LY_ERR
507 */
508static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100509cast_node_set_to_string(struct lyxp_set *set, char **str)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200510{
Michal Vasko03ff5a72019-09-11 13:49:33 +0200511 int dynamic;
512
Michal Vasko03ff5a72019-09-11 13:49:33 +0200513 switch (set->val.nodes[0].type) {
Michal Vasko2caefc12019-11-14 16:07:56 +0100514 case LYXP_NODE_NONE:
515 /* invalid */
516 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200517 case LYXP_NODE_ROOT:
518 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100519 return cast_string_elem(set->val.nodes[0].node, 1, set->root_type, str);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200520 case LYXP_NODE_ELEM:
521 case LYXP_NODE_TEXT:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100522 return cast_string_elem(set->val.nodes[0].node, 0, set->root_type, str);
Michal Vasko9f96a052020-03-10 09:41:45 +0100523 case LYXP_NODE_META:
524 *str = (char *)lyd_meta2str(set->val.meta[0].meta, &dynamic);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200525 if (!dynamic) {
526 *str = strdup(*str);
527 if (!*str) {
528 LOGMEM_RET(set->ctx);
529 }
530 }
531 return LY_SUCCESS;
532 }
533
534 LOGINT_RET(set->ctx);
535}
536
537/**
538 * @brief Cast a string into an XPath number.
539 *
540 * @param[in] str String to use.
541 * @return Cast number.
542 */
543static long double
544cast_string_to_number(const char *str)
545{
546 long double num;
547 char *ptr;
548
549 errno = 0;
550 num = strtold(str, &ptr);
551 if (errno || *ptr) {
552 num = NAN;
553 }
554 return num;
555}
556
557/**
558 * @brief Callback for checking value equality.
559 *
560 * @param[in] val1_p First value.
561 * @param[in] val2_p Second value.
562 * @param[in] mod Whether hash table is being modified.
563 * @param[in] cb_data Callback data.
564 * @return 0 if not equal, non-zero if equal.
565 */
566static int
567set_values_equal_cb(void *val1_p, void *val2_p, int UNUSED(mod), void *UNUSED(cb_data))
568{
569 struct lyxp_set_hash_node *val1, *val2;
570
571 val1 = (struct lyxp_set_hash_node *)val1_p;
572 val2 = (struct lyxp_set_hash_node *)val2_p;
573
574 if ((val1->node == val2->node) && (val1->type == val2->type)) {
575 return 1;
576 }
577
578 return 0;
579}
580
581/**
582 * @brief Insert node and its hash into set.
583 *
584 * @param[in] set et to insert to.
585 * @param[in] node Node with hash.
586 * @param[in] type Node type.
587 */
588static void
589set_insert_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type)
590{
591 int r;
592 uint32_t i, hash;
593 struct lyxp_set_hash_node hnode;
594
595 if (!set->ht && (set->used >= LYD_HT_MIN_ITEMS)) {
596 /* create hash table and add all the nodes */
597 set->ht = lyht_new(1, sizeof(struct lyxp_set_hash_node), set_values_equal_cb, NULL, 1);
598 for (i = 0; i < set->used; ++i) {
599 hnode.node = set->val.nodes[i].node;
600 hnode.type = set->val.nodes[i].type;
601
602 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
603 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
604 hash = dict_hash_multi(hash, NULL, 0);
605
606 r = lyht_insert(set->ht, &hnode, hash, NULL);
607 assert(!r);
608 (void)r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200609
Michal Vasko9d6befd2019-12-11 14:56:56 +0100610 if (hnode.node == node) {
611 /* it was just added, do not add it twice */
612 node = NULL;
613 }
614 }
615 }
616
617 if (set->ht && node) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200618 /* add the new node into hash table */
619 hnode.node = node;
620 hnode.type = type;
621
622 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
623 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
624 hash = dict_hash_multi(hash, NULL, 0);
625
626 r = lyht_insert(set->ht, &hnode, hash, NULL);
627 assert(!r);
628 (void)r;
629 }
630}
631
632/**
633 * @brief Remove node and its hash from set.
634 *
635 * @param[in] set Set to remove from.
636 * @param[in] node Node to remove.
637 * @param[in] type Node type.
638 */
639static void
640set_remove_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type)
641{
642 int r;
643 struct lyxp_set_hash_node hnode;
644 uint32_t hash;
645
646 if (set->ht) {
647 hnode.node = node;
648 hnode.type = type;
649
650 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
651 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
652 hash = dict_hash_multi(hash, NULL, 0);
653
654 r = lyht_remove(set->ht, &hnode, hash);
655 assert(!r);
656 (void)r;
657
658 if (!set->ht->used) {
659 lyht_free(set->ht);
660 set->ht = NULL;
661 }
662 }
663}
664
665/**
666 * @brief Check whether node is in set based on its hash.
667 *
668 * @param[in] set Set to search in.
669 * @param[in] node Node to search for.
670 * @param[in] type Node type.
671 * @param[in] skip_idx Index in @p set to skip.
672 * @return LY_ERR
673 */
674static LY_ERR
675set_dup_node_hash_check(const struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type, int skip_idx)
676{
677 struct lyxp_set_hash_node hnode, *match_p;
678 uint32_t hash;
679
680 hnode.node = node;
681 hnode.type = type;
682
683 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
684 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
685 hash = dict_hash_multi(hash, NULL, 0);
686
687 if (!lyht_find(set->ht, &hnode, hash, (void **)&match_p)) {
688 if ((skip_idx > -1) && (set->val.nodes[skip_idx].node == match_p->node) && (set->val.nodes[skip_idx].type == match_p->type)) {
689 /* we found it on the index that should be skipped, find another */
690 hnode = *match_p;
691 if (lyht_find_next(set->ht, &hnode, hash, (void **)&match_p)) {
692 /* none other found */
693 return LY_SUCCESS;
694 }
695 }
696
697 return LY_EEXIST;
698 }
699
700 /* not found */
701 return LY_SUCCESS;
702}
703
Michal Vaskod3678892020-05-21 10:06:58 +0200704void
705lyxp_set_free_content(struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200706{
707 if (!set) {
708 return;
709 }
710
711 if (set->type == LYXP_SET_NODE_SET) {
712 free(set->val.nodes);
713 lyht_free(set->ht);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200714 } else if (set->type == LYXP_SET_SCNODE_SET) {
715 free(set->val.scnodes);
Michal Vaskod3678892020-05-21 10:06:58 +0200716 lyht_free(set->ht);
717 } else {
718 if (set->type == LYXP_SET_STRING) {
719 free(set->val.str);
720 }
721 set->type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200722 }
Michal Vaskod3678892020-05-21 10:06:58 +0200723
724 set->val.nodes = NULL;
725 set->used = 0;
726 set->size = 0;
727 set->ht = NULL;
728 set->ctx_pos = 0;
729 set->ctx_pos = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200730}
731
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100732/**
733 * @brief Free dynamically-allocated set.
734 *
735 * @param[in] set Set to free.
736 */
737static void
Michal Vasko03ff5a72019-09-11 13:49:33 +0200738lyxp_set_free(struct lyxp_set *set)
739{
740 if (!set) {
741 return;
742 }
743
Michal Vaskod3678892020-05-21 10:06:58 +0200744 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200745 free(set);
746}
747
748/**
749 * @brief Initialize set context.
750 *
751 * @param[in] new Set to initialize.
752 * @param[in] set Arbitrary initialized set.
753 */
754static void
755set_init(struct lyxp_set *new, struct lyxp_set *set)
756{
757 memset(new, 0, sizeof *new);
Michal Vasko02a77382019-09-12 11:47:35 +0200758 if (set) {
759 new->ctx = set->ctx;
760 new->ctx_node = set->ctx_node;
Michal Vasko588112f2019-12-10 14:51:53 +0100761 new->root_type = set->root_type;
Michal Vasko02a77382019-09-12 11:47:35 +0200762 new->local_mod = set->local_mod;
Michal Vaskof03ed032020-03-04 13:31:44 +0100763 new->tree = set->tree;
Michal Vasko02a77382019-09-12 11:47:35 +0200764 new->format = set->format;
765 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200766}
767
768/**
769 * @brief Create a deep copy of a set.
770 *
771 * @param[in] set Set to copy.
772 * @return Copy of @p set.
773 */
774static struct lyxp_set *
775set_copy(struct lyxp_set *set)
776{
777 struct lyxp_set *ret;
778 uint16_t i;
Michal Vaskoba716542019-12-16 10:01:58 +0100779 int idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200780
781 if (!set) {
782 return NULL;
783 }
784
785 ret = malloc(sizeof *ret);
786 LY_CHECK_ERR_RET(!ret, LOGMEM(set->ctx), NULL);
787 set_init(ret, set);
788
789 if (set->type == LYXP_SET_SCNODE_SET) {
790 ret->type = set->type;
791
792 for (i = 0; i < set->used; ++i) {
Michal Vaskoba716542019-12-16 10:01:58 +0100793 if ((set->val.scnodes[i].in_ctx == 1) || (set->val.scnodes[i].in_ctx == -2)) {
794 idx = lyxp_set_scnode_insert_node(ret, set->val.scnodes[i].scnode, set->val.scnodes[i].type);
Michal Vasko3f27c522020-01-06 08:37:49 +0100795 /* coverity seems to think scnodes can be NULL */
796 if ((idx == -1) || !ret->val.scnodes) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200797 lyxp_set_free(ret);
798 return NULL;
799 }
Michal Vaskoba716542019-12-16 10:01:58 +0100800 ret->val.scnodes[idx].in_ctx = set->val.scnodes[i].in_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200801 }
802 }
803 } else if (set->type == LYXP_SET_NODE_SET) {
804 ret->type = set->type;
805 ret->val.nodes = malloc(set->used * sizeof *ret->val.nodes);
806 LY_CHECK_ERR_RET(!ret->val.nodes, LOGMEM(set->ctx); free(ret), NULL);
807 memcpy(ret->val.nodes, set->val.nodes, set->used * sizeof *ret->val.nodes);
808
809 ret->used = ret->size = set->used;
810 ret->ctx_pos = set->ctx_pos;
811 ret->ctx_size = set->ctx_size;
812 ret->ht = lyht_dup(set->ht);
813 } else {
814 memcpy(ret, set, sizeof *ret);
815 if (set->type == LYXP_SET_STRING) {
816 ret->val.str = strdup(set->val.str);
817 LY_CHECK_ERR_RET(!ret->val.str, LOGMEM(set->ctx); free(ret), NULL);
818 }
819 }
820
821 return ret;
822}
823
824/**
825 * @brief Fill XPath set with a string. Any current data are disposed of.
826 *
827 * @param[in] set Set to fill.
828 * @param[in] string String to fill into \p set.
829 * @param[in] str_len Length of \p string. 0 is a valid value!
830 */
831static void
832set_fill_string(struct lyxp_set *set, const char *string, uint16_t str_len)
833{
Michal Vaskod3678892020-05-21 10:06:58 +0200834 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200835
836 set->type = LYXP_SET_STRING;
837 if ((str_len == 0) && (string[0] != '\0')) {
838 string = "";
839 }
840 set->val.str = strndup(string, str_len);
841}
842
843/**
844 * @brief Fill XPath set with a number. Any current data are disposed of.
845 *
846 * @param[in] set Set to fill.
847 * @param[in] number Number to fill into \p set.
848 */
849static void
850set_fill_number(struct lyxp_set *set, long double number)
851{
Michal Vaskod3678892020-05-21 10:06:58 +0200852 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200853
854 set->type = LYXP_SET_NUMBER;
855 set->val.num = number;
856}
857
858/**
859 * @brief Fill XPath set with a boolean. Any current data are disposed of.
860 *
861 * @param[in] set Set to fill.
862 * @param[in] boolean Boolean to fill into \p set.
863 */
864static void
865set_fill_boolean(struct lyxp_set *set, int boolean)
866{
Michal Vaskod3678892020-05-21 10:06:58 +0200867 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200868
869 set->type = LYXP_SET_BOOLEAN;
Michal Vasko004d3152020-06-11 19:59:22 +0200870 set->val.bln = boolean;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200871}
872
873/**
874 * @brief Fill XPath set with the value from another set (deep assign).
875 * Any current data are disposed of.
876 *
877 * @param[in] trg Set to fill.
878 * @param[in] src Source set to copy into \p trg.
879 */
880static void
881set_fill_set(struct lyxp_set *trg, struct lyxp_set *src)
882{
883 if (!trg || !src) {
884 return;
885 }
886
887 if (trg->type == LYXP_SET_NODE_SET) {
888 free(trg->val.nodes);
889 } else if (trg->type == LYXP_SET_STRING) {
890 free(trg->val.str);
891 }
892 set_init(trg, src);
893
894 if (src->type == LYXP_SET_SCNODE_SET) {
895 trg->type = LYXP_SET_SCNODE_SET;
896 trg->used = src->used;
897 trg->size = src->used;
898
899 trg->val.scnodes = ly_realloc(trg->val.scnodes, trg->size * sizeof *trg->val.scnodes);
900 LY_CHECK_ERR_RET(!trg->val.scnodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), );
901 memcpy(trg->val.scnodes, src->val.scnodes, src->used * sizeof *src->val.scnodes);
902 } else if (src->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +0200903 set_fill_boolean(trg, src->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200904 } else if (src->type == LYXP_SET_NUMBER) {
905 set_fill_number(trg, src->val.num);
906 } else if (src->type == LYXP_SET_STRING) {
907 set_fill_string(trg, src->val.str, strlen(src->val.str));
908 } else {
909 if (trg->type == LYXP_SET_NODE_SET) {
910 free(trg->val.nodes);
911 } else if (trg->type == LYXP_SET_STRING) {
912 free(trg->val.str);
913 }
914
Michal Vaskod3678892020-05-21 10:06:58 +0200915 assert(src->type == LYXP_SET_NODE_SET);
916
917 trg->type = LYXP_SET_NODE_SET;
918 trg->used = src->used;
919 trg->size = src->used;
920 trg->ctx_pos = src->ctx_pos;
921 trg->ctx_size = src->ctx_size;
922
923 trg->val.nodes = malloc(trg->used * sizeof *trg->val.nodes);
924 LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), );
925 memcpy(trg->val.nodes, src->val.nodes, src->used * sizeof *src->val.nodes);
926 if (src->ht) {
927 trg->ht = lyht_dup(src->ht);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200928 } else {
Michal Vaskod3678892020-05-21 10:06:58 +0200929 trg->ht = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200930 }
931 }
932}
933
934/**
935 * @brief Clear context of all schema nodes.
936 *
937 * @param[in] set Set to clear.
938 */
939static void
940set_scnode_clear_ctx(struct lyxp_set *set)
941{
942 uint32_t i;
943
944 for (i = 0; i < set->used; ++i) {
945 if (set->val.scnodes[i].in_ctx == 1) {
946 set->val.scnodes[i].in_ctx = 0;
Michal Vasko5c4e5892019-11-14 12:31:38 +0100947 } else if (set->val.scnodes[i].in_ctx == -2) {
948 set->val.scnodes[i].in_ctx = -1;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200949 }
950 }
951}
952
953/**
954 * @brief Remove a node from a set. Removing last node changes
955 * set into LYXP_SET_EMPTY. Context position aware.
956 *
957 * @param[in] set Set to use.
958 * @param[in] idx Index from @p set of the node to be removed.
959 */
960static void
961set_remove_node(struct lyxp_set *set, uint32_t idx)
962{
963 assert(set && (set->type == LYXP_SET_NODE_SET));
964 assert(idx < set->used);
965
966 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
967
968 --set->used;
969 if (set->used) {
970 memmove(&set->val.nodes[idx], &set->val.nodes[idx + 1],
971 (set->used - idx) * sizeof *set->val.nodes);
972 } else {
Michal Vaskod3678892020-05-21 10:06:58 +0200973 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200974 }
975}
976
977/**
Michal Vasko2caefc12019-11-14 16:07:56 +0100978 * @brief Remove a node from a set by setting its type to LYXP_NODE_NONE.
Michal Vasko57eab132019-09-24 11:46:26 +0200979 *
980 * @param[in] set Set to use.
981 * @param[in] idx Index from @p set of the node to be removed.
982 */
983static void
Michal Vasko2caefc12019-11-14 16:07:56 +0100984set_remove_node_none(struct lyxp_set *set, uint32_t idx)
Michal Vasko57eab132019-09-24 11:46:26 +0200985{
986 assert(set && (set->type == LYXP_SET_NODE_SET));
987 assert(idx < set->used);
988
Michal Vasko2caefc12019-11-14 16:07:56 +0100989 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
990 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
991 }
992 set->val.nodes[idx].type = LYXP_NODE_NONE;
Michal Vasko57eab132019-09-24 11:46:26 +0200993}
994
995/**
Michal Vasko2caefc12019-11-14 16:07:56 +0100996 * @brief Remove all LYXP_NODE_NONE nodes from a set. Removing last node changes
Michal Vasko57eab132019-09-24 11:46:26 +0200997 * set into LYXP_SET_EMPTY. Context position aware.
998 *
999 * @param[in] set Set to consolidate.
1000 */
1001static void
Michal Vasko2caefc12019-11-14 16:07:56 +01001002set_remove_nodes_none(struct lyxp_set *set)
Michal Vasko57eab132019-09-24 11:46:26 +02001003{
1004 uint16_t i, orig_used, end;
1005 int32_t start;
1006
Michal Vaskod3678892020-05-21 10:06:58 +02001007 assert(set);
Michal Vasko57eab132019-09-24 11:46:26 +02001008
1009 orig_used = set->used;
1010 set->used = 0;
1011 for (i = 0; i < orig_used;) {
1012 start = -1;
1013 do {
Michal Vasko2caefc12019-11-14 16:07:56 +01001014 if ((set->val.nodes[i].type != LYXP_NODE_NONE) && (start == -1)) {
Michal Vasko57eab132019-09-24 11:46:26 +02001015 start = i;
Michal Vasko2caefc12019-11-14 16:07:56 +01001016 } else if ((start > -1) && (set->val.nodes[i].type == LYXP_NODE_NONE)) {
Michal Vasko57eab132019-09-24 11:46:26 +02001017 end = i;
1018 ++i;
1019 break;
1020 }
1021
1022 ++i;
1023 if (i == orig_used) {
1024 end = i;
1025 }
1026 } while (i < orig_used);
1027
1028 if (start > -1) {
1029 /* move the whole chunk of valid nodes together */
1030 if (set->used != (unsigned)start) {
1031 memmove(&set->val.nodes[set->used], &set->val.nodes[start], (end - start) * sizeof *set->val.nodes);
1032 }
1033 set->used += end - start;
1034 }
1035 }
Michal Vasko57eab132019-09-24 11:46:26 +02001036}
1037
1038/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02001039 * @brief Check for duplicates in a node set.
1040 *
1041 * @param[in] set Set to check.
1042 * @param[in] node Node to look for in @p set.
1043 * @param[in] node_type Type of @p node.
1044 * @param[in] skip_idx Index from @p set to skip.
1045 * @return LY_ERR
1046 */
1047static LY_ERR
1048set_dup_node_check(const struct lyxp_set *set, const struct lyd_node *node, enum lyxp_node_type node_type, int skip_idx)
1049{
1050 uint32_t i;
1051
Michal Vasko2caefc12019-11-14 16:07:56 +01001052 if (set->ht && node) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001053 return set_dup_node_hash_check(set, (struct lyd_node *)node, node_type, skip_idx);
1054 }
1055
1056 for (i = 0; i < set->used; ++i) {
1057 if ((skip_idx > -1) && (i == (unsigned)skip_idx)) {
1058 continue;
1059 }
1060
1061 if ((set->val.nodes[i].node == node) && (set->val.nodes[i].type == node_type)) {
1062 return LY_EEXIST;
1063 }
1064 }
1065
1066 return LY_SUCCESS;
1067}
1068
Michal Vaskoecd62de2019-11-13 12:35:11 +01001069int
1070lyxp_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 +02001071{
1072 uint32_t i;
1073
1074 for (i = 0; i < set->used; ++i) {
1075 if ((skip_idx > -1) && (i == (unsigned)skip_idx)) {
1076 continue;
1077 }
1078
1079 if ((set->val.scnodes[i].scnode == node) && (set->val.scnodes[i].type == node_type)) {
1080 return i;
1081 }
1082 }
1083
1084 return -1;
1085}
1086
Michal Vaskoecd62de2019-11-13 12:35:11 +01001087void
1088lyxp_set_scnode_merge(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001089{
1090 uint32_t orig_used, i, j;
1091
Michal Vaskod3678892020-05-21 10:06:58 +02001092 assert((set1->type == LYXP_SET_SCNODE_SET) && (set2->type == LYXP_SET_SCNODE_SET));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001093
Michal Vaskod3678892020-05-21 10:06:58 +02001094 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001095 return;
1096 }
1097
Michal Vaskod3678892020-05-21 10:06:58 +02001098 if (!set1->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001099 memcpy(set1, set2, sizeof *set1);
1100 return;
1101 }
1102
1103 if (set1->used + set2->used > set1->size) {
1104 set1->size = set1->used + set2->used;
1105 set1->val.scnodes = ly_realloc(set1->val.scnodes, set1->size * sizeof *set1->val.scnodes);
1106 LY_CHECK_ERR_RET(!set1->val.scnodes, LOGMEM(set1->ctx), );
1107 }
1108
1109 orig_used = set1->used;
1110
1111 for (i = 0; i < set2->used; ++i) {
1112 for (j = 0; j < orig_used; ++j) {
1113 /* detect duplicities */
1114 if (set1->val.scnodes[j].scnode == set2->val.scnodes[i].scnode) {
1115 break;
1116 }
1117 }
1118
1119 if (j == orig_used) {
1120 memcpy(&set1->val.scnodes[set1->used], &set2->val.scnodes[i], sizeof *set2->val.scnodes);
1121 ++set1->used;
1122 }
1123 }
1124
Michal Vaskod3678892020-05-21 10:06:58 +02001125 lyxp_set_free_content(set2);
1126 set2->type = LYXP_SET_SCNODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001127}
1128
1129/**
1130 * @brief Insert a node into a set. Context position aware.
1131 *
1132 * @param[in] set Set to use.
1133 * @param[in] node Node to insert to @p set.
1134 * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting.
1135 * @param[in] node_type Node type of @p node.
1136 * @param[in] idx Index in @p set to insert into.
1137 */
1138static void
1139set_insert_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx)
1140{
Michal Vaskod3678892020-05-21 10:06:58 +02001141 assert(set && (set->type == LYXP_SET_NODE_SET));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001142
Michal Vaskod3678892020-05-21 10:06:58 +02001143 if (!set->size) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001144 /* first item */
1145 if (idx) {
1146 /* no real harm done, but it is a bug */
1147 LOGINT(set->ctx);
1148 idx = 0;
1149 }
1150 set->val.nodes = malloc(LYXP_SET_SIZE_START * sizeof *set->val.nodes);
1151 LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), );
1152 set->type = LYXP_SET_NODE_SET;
1153 set->used = 0;
1154 set->size = LYXP_SET_SIZE_START;
1155 set->ctx_pos = 1;
1156 set->ctx_size = 1;
1157 set->ht = NULL;
1158 } else {
1159 /* not an empty set */
1160 if (set->used == set->size) {
1161
1162 /* set is full */
1163 set->val.nodes = ly_realloc(set->val.nodes, (set->size + LYXP_SET_SIZE_STEP) * sizeof *set->val.nodes);
1164 LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), );
1165 set->size += LYXP_SET_SIZE_STEP;
1166 }
1167
1168 if (idx > set->used) {
1169 LOGINT(set->ctx);
1170 idx = set->used;
1171 }
1172
1173 /* make space for the new node */
1174 if (idx < set->used) {
1175 memmove(&set->val.nodes[idx + 1], &set->val.nodes[idx], (set->used - idx) * sizeof *set->val.nodes);
1176 }
1177 }
1178
1179 /* finally assign the value */
1180 set->val.nodes[idx].node = (struct lyd_node *)node;
1181 set->val.nodes[idx].type = node_type;
1182 set->val.nodes[idx].pos = pos;
1183 ++set->used;
1184
Michal Vasko2caefc12019-11-14 16:07:56 +01001185 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1186 set_insert_node_hash(set, (struct lyd_node *)node, node_type);
1187 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001188}
1189
Michal Vaskoecd62de2019-11-13 12:35:11 +01001190int
1191lyxp_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 +02001192{
1193 int ret;
1194
1195 assert(set->type == LYXP_SET_SCNODE_SET);
1196
Michal Vaskoecd62de2019-11-13 12:35:11 +01001197 ret = lyxp_set_scnode_dup_node_check(set, node, node_type, -1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001198 if (ret > -1) {
1199 set->val.scnodes[ret].in_ctx = 1;
1200 } else {
1201 if (set->used == set->size) {
1202 set->val.scnodes = ly_realloc(set->val.scnodes, (set->size + LYXP_SET_SIZE_STEP) * sizeof *set->val.scnodes);
1203 LY_CHECK_ERR_RET(!set->val.scnodes, LOGMEM(set->ctx), -1);
1204 set->size += LYXP_SET_SIZE_STEP;
1205 }
1206
1207 ret = set->used;
1208 set->val.scnodes[ret].scnode = (struct lysc_node *)node;
1209 set->val.scnodes[ret].type = node_type;
1210 set->val.scnodes[ret].in_ctx = 1;
1211 ++set->used;
1212 }
1213
1214 return ret;
1215}
1216
1217/**
1218 * @brief Replace a node in a set with another. Context position aware.
1219 *
1220 * @param[in] set Set to use.
1221 * @param[in] node Node to insert to @p set.
1222 * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting.
1223 * @param[in] node_type Node type of @p node.
1224 * @param[in] idx Index in @p set of the node to replace.
1225 */
1226static void
1227set_replace_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx)
1228{
1229 assert(set && (idx < set->used));
1230
Michal Vasko2caefc12019-11-14 16:07:56 +01001231 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1232 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1233 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001234 set->val.nodes[idx].node = (struct lyd_node *)node;
1235 set->val.nodes[idx].type = node_type;
1236 set->val.nodes[idx].pos = pos;
Michal Vasko2caefc12019-11-14 16:07:56 +01001237 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1238 set_insert_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1239 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001240}
1241
1242/**
1243 * @brief Set all nodes with ctx 1 to a new unique context value.
1244 *
1245 * @param[in] set Set to modify.
1246 * @return New context value.
1247 */
Michal Vasko5c4e5892019-11-14 12:31:38 +01001248static int32_t
Michal Vasko03ff5a72019-09-11 13:49:33 +02001249set_scnode_new_in_ctx(struct lyxp_set *set)
1250{
Michal Vasko5c4e5892019-11-14 12:31:38 +01001251 uint32_t i;
1252 int32_t ret_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001253
1254 assert(set->type == LYXP_SET_SCNODE_SET);
1255
1256 ret_ctx = 3;
1257retry:
1258 for (i = 0; i < set->used; ++i) {
1259 if (set->val.scnodes[i].in_ctx >= ret_ctx) {
1260 ret_ctx = set->val.scnodes[i].in_ctx + 1;
1261 goto retry;
1262 }
1263 }
1264 for (i = 0; i < set->used; ++i) {
1265 if (set->val.scnodes[i].in_ctx == 1) {
1266 set->val.scnodes[i].in_ctx = ret_ctx;
1267 }
1268 }
1269
1270 return ret_ctx;
1271}
1272
1273/**
1274 * @brief Get unique @p node position in the data.
1275 *
1276 * @param[in] node Node to find.
1277 * @param[in] node_type Node type of @p node.
1278 * @param[in] root Root node.
1279 * @param[in] root_type Type of the XPath @p root node.
1280 * @param[in] prev Node that we think is before @p node in DFS from @p root. Can optionally
1281 * be used to increase efficiency and start the DFS from this node.
1282 * @param[in] prev_pos Node @p prev position. Optional, but must be set if @p prev is set.
1283 * @return Node position.
1284 */
1285static uint32_t
1286get_node_pos(const struct lyd_node *node, enum lyxp_node_type node_type, const struct lyd_node *root,
1287 enum lyxp_node_type root_type, const struct lyd_node **prev, uint32_t *prev_pos)
1288{
1289 const struct lyd_node *next, *elem, *top_sibling;
1290 uint32_t pos = 1;
1291
1292 assert(prev && prev_pos && !root->prev->next);
1293
1294 if ((node_type == LYXP_NODE_ROOT) || (node_type == LYXP_NODE_ROOT_CONFIG)) {
1295 return 0;
1296 }
1297
1298 if (*prev) {
1299 /* start from the previous element instead from the root */
1300 elem = next = *prev;
1301 pos = *prev_pos;
1302 for (top_sibling = elem; top_sibling->parent; top_sibling = (struct lyd_node *)top_sibling->parent);
1303 goto dfs_search;
1304 }
1305
1306 for (top_sibling = root; top_sibling; top_sibling = top_sibling->next) {
1307 /* TREE DFS */
1308 LYD_TREE_DFS_BEGIN(top_sibling, next, elem) {
1309dfs_search:
1310 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (elem->schema->flags & LYS_CONFIG_R)) {
1311 goto skip_children;
1312 }
1313
1314 if (elem == node) {
1315 break;
1316 }
1317 ++pos;
1318
1319 /* TREE DFS END */
1320 /* select element for the next run - children first,
1321 * child exception for lyd_node_leaf and lyd_node_leaflist, but not the root */
1322 if (elem->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) {
1323 next = NULL;
1324 } else {
1325 next = lyd_node_children(elem);
1326 }
1327 if (!next) {
1328skip_children:
1329 /* no children */
1330 if (elem == top_sibling) {
1331 /* we are done, root has no children */
1332 elem = NULL;
1333 break;
1334 }
1335 /* try siblings */
1336 next = elem->next;
1337 }
1338 while (!next) {
1339 /* no siblings, go back through parents */
1340 if (elem->parent == top_sibling->parent) {
1341 /* we are done, no next element to process */
1342 elem = NULL;
1343 break;
1344 }
1345 /* parent is already processed, go to its sibling */
1346 elem = (struct lyd_node *)elem->parent;
1347 next = elem->next;
1348 }
1349 }
1350
1351 /* node found */
1352 if (elem) {
1353 break;
1354 }
1355 }
1356
1357 if (!elem) {
1358 if (!(*prev)) {
1359 /* we went from root and failed to find it, cannot be */
1360 LOGINT(node->schema->module->ctx);
1361 return 0;
1362 } else {
1363 *prev = NULL;
1364 *prev_pos = 0;
1365
1366 elem = next = top_sibling = root;
1367 pos = 1;
1368 goto dfs_search;
1369 }
1370 }
1371
1372 /* remember the last found node for next time */
1373 *prev = node;
1374 *prev_pos = pos;
1375
1376 return pos;
1377}
1378
1379/**
1380 * @brief Assign (fill) missing node positions.
1381 *
1382 * @param[in] set Set to fill positions in.
1383 * @param[in] root Context root node.
1384 * @param[in] root_type Context root type.
1385 * @return LY_ERR
1386 */
1387static LY_ERR
1388set_assign_pos(struct lyxp_set *set, const struct lyd_node *root, enum lyxp_node_type root_type)
1389{
1390 const struct lyd_node *prev = NULL, *tmp_node;
1391 uint32_t i, tmp_pos = 0;
1392
1393 for (i = 0; i < set->used; ++i) {
1394 if (!set->val.nodes[i].pos) {
1395 tmp_node = NULL;
1396 switch (set->val.nodes[i].type) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001397 case LYXP_NODE_META:
1398 tmp_node = set->val.meta[i].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001399 if (!tmp_node) {
1400 LOGINT_RET(root->schema->module->ctx);
1401 }
1402 /* fallthrough */
1403 case LYXP_NODE_ELEM:
1404 case LYXP_NODE_TEXT:
1405 if (!tmp_node) {
1406 tmp_node = set->val.nodes[i].node;
1407 }
1408 set->val.nodes[i].pos = get_node_pos(tmp_node, set->val.nodes[i].type, root, root_type, &prev, &tmp_pos);
1409 break;
1410 default:
1411 /* all roots have position 0 */
1412 break;
1413 }
1414 }
1415 }
1416
1417 return LY_SUCCESS;
1418}
1419
1420/**
Michal Vasko9f96a052020-03-10 09:41:45 +01001421 * @brief Get unique @p meta position in the parent metadata.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001422 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001423 * @param[in] meta Metadata to use.
1424 * @return Metadata position.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001425 */
1426static uint16_t
Michal Vasko9f96a052020-03-10 09:41:45 +01001427get_meta_pos(struct lyd_meta *meta)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001428{
1429 uint16_t pos = 0;
Michal Vasko9f96a052020-03-10 09:41:45 +01001430 struct lyd_meta *meta2;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001431
Michal Vasko9f96a052020-03-10 09:41:45 +01001432 for (meta2 = meta->parent->meta; meta2 && (meta2 != meta); meta2 = meta2->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001433 ++pos;
1434 }
1435
Michal Vasko9f96a052020-03-10 09:41:45 +01001436 assert(meta2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001437 return pos;
1438}
1439
1440/**
1441 * @brief Compare 2 nodes in respect to XPath document order.
1442 *
1443 * @param[in] item1 1st node.
1444 * @param[in] item2 2nd node.
1445 * @return If 1st > 2nd returns 1, 1st == 2nd returns 0, and 1st < 2nd returns -1.
1446 */
1447static int
1448set_sort_compare(struct lyxp_set_node *item1, struct lyxp_set_node *item2)
1449{
Michal Vasko9f96a052020-03-10 09:41:45 +01001450 uint32_t meta_pos1 = 0, meta_pos2 = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001451
1452 if (item1->pos < item2->pos) {
1453 return -1;
1454 }
1455
1456 if (item1->pos > item2->pos) {
1457 return 1;
1458 }
1459
1460 /* node positions are equal, the fun case */
1461
1462 /* 1st ELEM - == - 2nd TEXT, 1st TEXT - == - 2nd ELEM */
1463 /* special case since text nodes are actually saved as their parents */
1464 if ((item1->node == item2->node) && (item1->type != item2->type)) {
1465 if (item1->type == LYXP_NODE_ELEM) {
1466 assert(item2->type == LYXP_NODE_TEXT);
1467 return -1;
1468 } else {
1469 assert((item1->type == LYXP_NODE_TEXT) && (item2->type == LYXP_NODE_ELEM));
1470 return 1;
1471 }
1472 }
1473
Michal Vasko9f96a052020-03-10 09:41:45 +01001474 /* we need meta positions now */
1475 if (item1->type == LYXP_NODE_META) {
1476 meta_pos1 = get_meta_pos((struct lyd_meta *)item1->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001477 }
Michal Vasko9f96a052020-03-10 09:41:45 +01001478 if (item2->type == LYXP_NODE_META) {
1479 meta_pos2 = get_meta_pos((struct lyd_meta *)item2->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001480 }
1481
Michal Vasko9f96a052020-03-10 09:41:45 +01001482 /* 1st ROOT - 2nd ROOT, 1st ELEM - 2nd ELEM, 1st TEXT - 2nd TEXT, 1st META - =pos= - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001483 /* check for duplicates */
1484 if (item1->node == item2->node) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001485 assert((item1->type == item2->type) && ((item1->type != LYXP_NODE_META) || (meta_pos1 == meta_pos2)));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001486 return 0;
1487 }
1488
Michal Vasko9f96a052020-03-10 09:41:45 +01001489 /* 1st ELEM - 2nd TEXT, 1st ELEM - any pos - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001490 /* elem is always first, 2nd node is after it */
1491 if (item1->type == LYXP_NODE_ELEM) {
1492 assert(item2->type != LYXP_NODE_ELEM);
1493 return -1;
1494 }
1495
Michal Vasko9f96a052020-03-10 09:41:45 +01001496 /* 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 +02001497 /* 2nd is before 1st */
1498 if (((item1->type == LYXP_NODE_TEXT)
Michal Vasko9f96a052020-03-10 09:41:45 +01001499 && ((item2->type == LYXP_NODE_ELEM) || (item2->type == LYXP_NODE_META)))
1500 || ((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_ELEM))
1501 || (((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_META))
1502 && (meta_pos1 > meta_pos2))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001503 return 1;
1504 }
1505
Michal Vasko9f96a052020-03-10 09:41:45 +01001506 /* 1st META - any pos - 2nd TEXT, 1st META <pos< - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001507 /* 2nd is after 1st */
1508 return -1;
1509}
1510
1511/**
1512 * @brief Set cast for comparisons.
1513 *
1514 * @param[in] trg Target set to cast source into.
1515 * @param[in] src Source set.
1516 * @param[in] type Target set type.
1517 * @param[in] src_idx Source set node index.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001518 * @return LY_ERR
1519 */
1520static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001521set_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 +02001522{
1523 assert(src->type == LYXP_SET_NODE_SET);
1524
1525 set_init(trg, src);
1526
1527 /* insert node into target set */
1528 set_insert_node(trg, src->val.nodes[src_idx].node, src->val.nodes[src_idx].pos, src->val.nodes[src_idx].type, 0);
1529
1530 /* cast target set appropriately */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001531 return lyxp_set_cast(trg, type);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001532}
1533
1534#ifndef NDEBUG
1535
1536/**
1537 * @brief Bubble sort @p set into XPath document order.
1538 * Context position aware. Unused in the 'Release' build target.
1539 *
1540 * @param[in] set Set to sort.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001541 * @return How many times the whole set was traversed - 1 (if set was sorted, returns 0).
1542 */
1543static int
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001544set_sort(struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001545{
1546 uint32_t i, j;
1547 int ret = 0, cmp, inverted, change;
1548 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001549 struct lyxp_set_node item;
1550 struct lyxp_set_hash_node hnode;
1551 uint64_t hash;
1552
Michal Vasko3cf8fbf2020-05-27 15:21:21 +02001553 if ((set->type != LYXP_SET_NODE_SET) || (set->used < 2)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001554 return 0;
1555 }
1556
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001557 /* find first top-level node to be used as anchor for positions */
1558 for (root = set->ctx_node; root->parent; root = (const struct lyd_node *)root->parent);
1559 for (; root->prev->next; root = root->prev);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001560
1561 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001562 if (set_assign_pos(set, root, set->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001563 return -1;
1564 }
1565
1566 LOGDBG(LY_LDGXPATH, "SORT BEGIN");
1567 print_set_debug(set);
1568
1569 for (i = 0; i < set->used; ++i) {
1570 inverted = 0;
1571 change = 0;
1572
1573 for (j = 1; j < set->used - i; ++j) {
1574 /* compare node positions */
1575 if (inverted) {
1576 cmp = set_sort_compare(&set->val.nodes[j], &set->val.nodes[j - 1]);
1577 } else {
1578 cmp = set_sort_compare(&set->val.nodes[j - 1], &set->val.nodes[j]);
1579 }
1580
1581 /* swap if needed */
1582 if ((inverted && (cmp < 0)) || (!inverted && (cmp > 0))) {
1583 change = 1;
1584
1585 item = set->val.nodes[j - 1];
1586 set->val.nodes[j - 1] = set->val.nodes[j];
1587 set->val.nodes[j] = item;
1588 } else {
1589 /* whether node_pos1 should be smaller than node_pos2 or the other way around */
1590 inverted = !inverted;
1591 }
1592 }
1593
1594 ++ret;
1595
1596 if (!change) {
1597 break;
1598 }
1599 }
1600
1601 LOGDBG(LY_LDGXPATH, "SORT END %d", ret);
1602 print_set_debug(set);
1603
1604 /* check node hashes */
1605 if (set->used >= LYD_HT_MIN_ITEMS) {
1606 assert(set->ht);
1607 for (i = 0; i < set->used; ++i) {
1608 hnode.node = set->val.nodes[i].node;
1609 hnode.type = set->val.nodes[i].type;
1610
1611 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
1612 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
1613 hash = dict_hash_multi(hash, NULL, 0);
1614
1615 assert(!lyht_find(set->ht, &hnode, hash, NULL));
1616 }
1617 }
1618
1619 return ret - 1;
1620}
1621
1622/**
1623 * @brief Remove duplicate entries in a sorted node set.
1624 *
1625 * @param[in] set Sorted set to check.
1626 * @return LY_ERR (LY_EEXIST if some duplicates are found)
1627 */
1628static LY_ERR
1629set_sorted_dup_node_clean(struct lyxp_set *set)
1630{
1631 uint32_t i = 0;
1632 LY_ERR ret = LY_SUCCESS;
1633
1634 if (set->used > 1) {
1635 while (i < set->used - 1) {
1636 if ((set->val.nodes[i].node == set->val.nodes[i + 1].node)
1637 && (set->val.nodes[i].type == set->val.nodes[i + 1].type)) {
Michal Vasko2caefc12019-11-14 16:07:56 +01001638 set_remove_node_none(set, i + 1);
Michal Vasko57eab132019-09-24 11:46:26 +02001639 ret = LY_EEXIST;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001640 }
Michal Vasko57eab132019-09-24 11:46:26 +02001641 ++i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001642 }
1643 }
1644
Michal Vasko2caefc12019-11-14 16:07:56 +01001645 set_remove_nodes_none(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001646 return ret;
1647}
1648
1649#endif
1650
1651/**
1652 * @brief Merge 2 sorted sets into one.
1653 *
1654 * @param[in,out] trg Set to merge into. Duplicates are removed.
1655 * @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 +02001656 * @return LY_ERR
1657 */
1658static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001659set_sorted_merge(struct lyxp_set *trg, struct lyxp_set *src)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001660{
1661 uint32_t i, j, k, count, dup_count;
1662 int cmp;
1663 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001664
Michal Vaskod3678892020-05-21 10:06:58 +02001665 if ((trg->type != LYXP_SET_NODE_SET) || (src->type != LYXP_SET_NODE_SET)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001666 return LY_EINVAL;
1667 }
1668
Michal Vaskod3678892020-05-21 10:06:58 +02001669 if (!src->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001670 return LY_SUCCESS;
Michal Vaskod3678892020-05-21 10:06:58 +02001671 } else if (!trg->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001672 set_fill_set(trg, src);
Michal Vaskod3678892020-05-21 10:06:58 +02001673 lyxp_set_free_content(src);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001674 return LY_SUCCESS;
1675 }
1676
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001677 /* find first top-level node to be used as anchor for positions */
1678 for (root = trg->ctx_node; root->parent; root = (const struct lyd_node *)root->parent);
1679 for (; root->prev->next; root = root->prev);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001680
1681 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001682 if (set_assign_pos(trg, root, trg->root_type) || set_assign_pos(src, root, src->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001683 return LY_EINT;
1684 }
1685
1686#ifndef NDEBUG
1687 LOGDBG(LY_LDGXPATH, "MERGE target");
1688 print_set_debug(trg);
1689 LOGDBG(LY_LDGXPATH, "MERGE source");
1690 print_set_debug(src);
1691#endif
1692
1693 /* make memory for the merge (duplicates are not detected yet, so space
1694 * will likely be wasted on them, too bad) */
1695 if (trg->size - trg->used < src->used) {
1696 trg->size = trg->used + src->used;
1697
1698 trg->val.nodes = ly_realloc(trg->val.nodes, trg->size * sizeof *trg->val.nodes);
1699 LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx), LY_EMEM);
1700 }
1701
1702 i = 0;
1703 j = 0;
1704 count = 0;
1705 dup_count = 0;
1706 do {
1707 cmp = set_sort_compare(&src->val.nodes[i], &trg->val.nodes[j]);
1708 if (!cmp) {
1709 if (!count) {
1710 /* duplicate, just skip it */
1711 ++i;
1712 ++j;
1713 } else {
1714 /* we are copying something already, so let's copy the duplicate too,
1715 * we are hoping that afterwards there are some more nodes to
1716 * copy and this way we can copy them all together */
1717 ++count;
1718 ++dup_count;
1719 ++i;
1720 ++j;
1721 }
1722 } else if (cmp < 0) {
1723 /* inserting src node into trg, just remember it for now */
1724 ++count;
1725 ++i;
1726
1727 /* insert the hash now */
1728 set_insert_node_hash(trg, src->val.nodes[i - 1].node, src->val.nodes[i - 1].type);
1729 } else if (count) {
1730copy_nodes:
1731 /* time to actually copy the nodes, we have found the largest block of nodes */
1732 memmove(&trg->val.nodes[j + (count - dup_count)],
1733 &trg->val.nodes[j],
1734 (trg->used - j) * sizeof *trg->val.nodes);
1735 memcpy(&trg->val.nodes[j - dup_count], &src->val.nodes[i - count], count * sizeof *src->val.nodes);
1736
1737 trg->used += count - dup_count;
1738 /* do not change i, except the copying above, we are basically doing exactly what is in the else branch below */
1739 j += count - dup_count;
1740
1741 count = 0;
1742 dup_count = 0;
1743 } else {
1744 ++j;
1745 }
1746 } while ((i < src->used) && (j < trg->used));
1747
1748 if ((i < src->used) || count) {
1749 /* insert all the hashes first */
1750 for (k = i; k < src->used; ++k) {
1751 set_insert_node_hash(trg, src->val.nodes[k].node, src->val.nodes[k].type);
1752 }
1753
1754 /* loop ended, but we need to copy something at trg end */
1755 count += src->used - i;
1756 i = src->used;
1757 goto copy_nodes;
1758 }
1759
1760 /* we are inserting hashes before the actual node insert, which causes
1761 * situations when there were initially not enough items for a hash table,
1762 * but even after some were inserted, hash table was not created (during
1763 * insertion the number of items is not updated yet) */
1764 if (!trg->ht && (trg->used >= LYD_HT_MIN_ITEMS)) {
1765 set_insert_node_hash(trg, NULL, 0);
1766 }
1767
1768#ifndef NDEBUG
1769 LOGDBG(LY_LDGXPATH, "MERGE result");
1770 print_set_debug(trg);
1771#endif
1772
Michal Vaskod3678892020-05-21 10:06:58 +02001773 lyxp_set_free_content(src);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001774 return LY_SUCCESS;
1775}
1776
1777/*
1778 * (re)parse functions
1779 *
1780 * Parse functions parse the expression into
1781 * tokens (syntactic analysis).
1782 *
1783 * Reparse functions perform semantic analysis
1784 * (do not save the result, just a check) of
1785 * the expression and fill repeat indices.
1786 */
1787
Michal Vasko14676352020-05-29 11:35:55 +02001788LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02001789lyxp_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 +02001790{
Michal Vasko004d3152020-06-11 19:59:22 +02001791 if (exp->used == tok_idx) {
Michal Vasko14676352020-05-29 11:35:55 +02001792 if (ctx) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001793 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOF);
1794 }
Michal Vasko14676352020-05-29 11:35:55 +02001795 return LY_EINCOMPLETE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001796 }
1797
Michal Vasko004d3152020-06-11 19:59:22 +02001798 if (want_tok && (exp->tokens[tok_idx] != want_tok)) {
Michal Vasko14676352020-05-29 11:35:55 +02001799 if (ctx) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001800 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK,
Michal Vasko004d3152020-06-11 19:59:22 +02001801 lyxp_print_token(exp->tokens[tok_idx]), &exp->expr[exp->tok_pos[tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001802 }
Michal Vasko14676352020-05-29 11:35:55 +02001803 return LY_ENOT;
1804 }
1805
1806 return LY_SUCCESS;
1807}
1808
Michal Vasko004d3152020-06-11 19:59:22 +02001809LY_ERR
1810lyxp_next_token(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_token want_tok)
1811{
1812 LY_CHECK_RET(lyxp_check_token(ctx, exp, *tok_idx, want_tok));
1813
1814 /* skip the token */
1815 ++(*tok_idx);
1816
1817 return LY_SUCCESS;
1818}
1819
Michal Vasko14676352020-05-29 11:35:55 +02001820/* just like lyxp_check_token() but tests for 2 tokens */
1821static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02001822exp_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 +02001823 enum lyxp_token want_tok2)
1824{
Michal Vasko004d3152020-06-11 19:59:22 +02001825 if (exp->used == tok_idx) {
Michal Vasko14676352020-05-29 11:35:55 +02001826 if (ctx) {
1827 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOF);
1828 }
1829 return LY_EINCOMPLETE;
1830 }
1831
Michal Vasko004d3152020-06-11 19:59:22 +02001832 if ((exp->tokens[tok_idx] != want_tok1) && (exp->tokens[tok_idx] != want_tok2)) {
Michal Vasko14676352020-05-29 11:35:55 +02001833 if (ctx) {
1834 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK,
Michal Vasko004d3152020-06-11 19:59:22 +02001835 lyxp_print_token(exp->tokens[tok_idx]), &exp->expr[exp->tok_pos[tok_idx]]);
Michal Vasko14676352020-05-29 11:35:55 +02001836 }
1837 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001838 }
1839
1840 return LY_SUCCESS;
1841}
1842
1843/**
1844 * @brief Stack operation push on the repeat array.
1845 *
1846 * @param[in] exp Expression to use.
Michal Vasko004d3152020-06-11 19:59:22 +02001847 * @param[in] tok_idx Position in the expresion \p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001848 * @param[in] repeat_op_idx Index from \p exp of the operator token. This value is pushed.
1849 */
1850static void
Michal Vasko004d3152020-06-11 19:59:22 +02001851exp_repeat_push(struct lyxp_expr *exp, uint16_t tok_idx, uint16_t repeat_op_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001852{
1853 uint16_t i;
1854
Michal Vasko004d3152020-06-11 19:59:22 +02001855 if (exp->repeat[tok_idx]) {
1856 for (i = 0; exp->repeat[tok_idx][i]; ++i);
1857 exp->repeat[tok_idx] = realloc(exp->repeat[tok_idx], (i + 2) * sizeof *exp->repeat[tok_idx]);
1858 LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), );
1859 exp->repeat[tok_idx][i] = repeat_op_idx;
1860 exp->repeat[tok_idx][i + 1] = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001861 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02001862 exp->repeat[tok_idx] = calloc(2, sizeof *exp->repeat[tok_idx]);
1863 LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), );
1864 exp->repeat[tok_idx][0] = repeat_op_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001865 }
1866}
1867
1868/**
1869 * @brief Reparse Predicate. Logs directly on error.
1870 *
1871 * [7] Predicate ::= '[' Expr ']'
1872 *
1873 * @param[in] ctx Context for logging.
1874 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02001875 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001876 * @return LY_ERR
1877 */
1878static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02001879reparse_predicate(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001880{
1881 LY_ERR rc;
1882
Michal Vasko004d3152020-06-11 19:59:22 +02001883 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001884 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02001885 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001886
Michal Vasko004d3152020-06-11 19:59:22 +02001887 rc = reparse_or_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001888 LY_CHECK_RET(rc);
1889
Michal Vasko004d3152020-06-11 19:59:22 +02001890 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001891 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02001892 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001893
1894 return LY_SUCCESS;
1895}
1896
1897/**
1898 * @brief Reparse RelativeLocationPath. Logs directly on error.
1899 *
1900 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
1901 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
1902 * [6] NodeTest ::= NameTest | NodeType '(' ')'
1903 *
1904 * @param[in] ctx Context for logging.
1905 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02001906 * @param[in] tok_idx Position in the expression \p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001907 * @return LY_ERR (LY_EINCOMPLETE on forward reference)
1908 */
1909static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02001910reparse_relative_location_path(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001911{
1912 LY_ERR rc;
1913
Michal Vasko004d3152020-06-11 19:59:22 +02001914 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001915 LY_CHECK_RET(rc);
1916
1917 goto step;
1918 do {
1919 /* '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02001920 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001921
Michal Vasko004d3152020-06-11 19:59:22 +02001922 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001923 LY_CHECK_RET(rc);
1924step:
1925 /* Step */
Michal Vasko004d3152020-06-11 19:59:22 +02001926 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001927 case LYXP_TOKEN_DOT:
Michal Vasko004d3152020-06-11 19:59:22 +02001928 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001929 break;
1930
1931 case LYXP_TOKEN_DDOT:
Michal Vasko004d3152020-06-11 19:59:22 +02001932 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001933 break;
1934
1935 case LYXP_TOKEN_AT:
Michal Vasko004d3152020-06-11 19:59:22 +02001936 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001937
Michal Vasko004d3152020-06-11 19:59:22 +02001938 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001939 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02001940 if ((exp->tokens[*tok_idx] != LYXP_TOKEN_NAMETEST) && (exp->tokens[*tok_idx] != LYXP_TOKEN_NODETYPE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001941 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK,
Michal Vasko004d3152020-06-11 19:59:22 +02001942 lyxp_print_token(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001943 return LY_EVALID;
1944 }
1945 /* fall through */
1946 case LYXP_TOKEN_NAMETEST:
Michal Vasko004d3152020-06-11 19:59:22 +02001947 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001948 goto reparse_predicate;
1949 break;
1950
1951 case LYXP_TOKEN_NODETYPE:
Michal Vasko004d3152020-06-11 19:59:22 +02001952 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001953
1954 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02001955 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001956 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02001957 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001958
1959 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02001960 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001961 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02001962 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001963
1964reparse_predicate:
1965 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02001966 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
1967 rc = reparse_predicate(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001968 LY_CHECK_RET(rc);
1969 }
1970 break;
1971 default:
1972 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK,
Michal Vasko004d3152020-06-11 19:59:22 +02001973 lyxp_print_token(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001974 return LY_EVALID;
1975 }
Michal Vasko004d3152020-06-11 19:59:22 +02001976 } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001977
1978 return LY_SUCCESS;
1979}
1980
1981/**
1982 * @brief Reparse AbsoluteLocationPath. Logs directly on error.
1983 *
1984 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
1985 *
1986 * @param[in] ctx Context for logging.
1987 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02001988 * @param[in] tok_idx Position in the expression \p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001989 * @return LY_ERR
1990 */
1991static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02001992reparse_absolute_location_path(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001993{
1994 LY_ERR rc;
1995
Michal Vasko004d3152020-06-11 19:59:22 +02001996 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 +02001997
1998 /* '/' RelativeLocationPath? */
Michal Vasko004d3152020-06-11 19:59:22 +02001999 if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002000 /* '/' */
Michal Vasko004d3152020-06-11 19:59:22 +02002001 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002002
Michal Vasko004d3152020-06-11 19:59:22 +02002003 if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002004 return LY_SUCCESS;
2005 }
Michal Vasko004d3152020-06-11 19:59:22 +02002006 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002007 case LYXP_TOKEN_DOT:
2008 case LYXP_TOKEN_DDOT:
2009 case LYXP_TOKEN_AT:
2010 case LYXP_TOKEN_NAMETEST:
2011 case LYXP_TOKEN_NODETYPE:
Michal Vasko004d3152020-06-11 19:59:22 +02002012 rc = reparse_relative_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002013 LY_CHECK_RET(rc);
2014 /* fall through */
2015 default:
2016 break;
2017 }
2018
2019 /* '//' RelativeLocationPath */
2020 } else {
2021 /* '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02002022 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002023
Michal Vasko004d3152020-06-11 19:59:22 +02002024 rc = reparse_relative_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002025 LY_CHECK_RET(rc);
2026 }
2027
2028 return LY_SUCCESS;
2029}
2030
2031/**
2032 * @brief Reparse FunctionCall. Logs directly on error.
2033 *
2034 * [9] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
2035 *
2036 * @param[in] ctx Context for logging.
2037 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002038 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002039 * @return LY_ERR
2040 */
2041static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002042reparse_function_call(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002043{
2044 int min_arg_count = -1, max_arg_count, arg_count;
Michal Vasko004d3152020-06-11 19:59:22 +02002045 uint16_t func_tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002046 LY_ERR rc;
2047
Michal Vasko004d3152020-06-11 19:59:22 +02002048 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_FUNCNAME);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002049 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002050 func_tok_idx = *tok_idx;
2051 switch (exp->tok_len[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002052 case 3:
Michal Vasko004d3152020-06-11 19:59:22 +02002053 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002054 min_arg_count = 1;
2055 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002056 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002057 min_arg_count = 1;
2058 max_arg_count = 1;
2059 }
2060 break;
2061 case 4:
Michal Vasko004d3152020-06-11 19:59:22 +02002062 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002063 min_arg_count = 1;
2064 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002065 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002066 min_arg_count = 0;
2067 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002068 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002069 min_arg_count = 0;
2070 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002071 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002072 min_arg_count = 0;
2073 max_arg_count = 0;
2074 }
2075 break;
2076 case 5:
Michal Vasko004d3152020-06-11 19:59:22 +02002077 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002078 min_arg_count = 1;
2079 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002080 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002081 min_arg_count = 0;
2082 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002083 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002084 min_arg_count = 1;
2085 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002086 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002087 min_arg_count = 1;
2088 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002089 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002090 min_arg_count = 1;
2091 max_arg_count = 1;
2092 }
2093 break;
2094 case 6:
Michal Vasko004d3152020-06-11 19:59:22 +02002095 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002096 min_arg_count = 2;
Michal Vaskobe2e3562019-10-15 15:35:35 +02002097 max_arg_count = INT_MAX;
Michal Vasko004d3152020-06-11 19:59:22 +02002098 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002099 min_arg_count = 0;
2100 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002101 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002102 min_arg_count = 0;
2103 max_arg_count = 1;
2104 }
2105 break;
2106 case 7:
Michal Vasko004d3152020-06-11 19:59:22 +02002107 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002108 min_arg_count = 1;
2109 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002110 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002111 min_arg_count = 1;
2112 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002113 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002114 min_arg_count = 0;
2115 max_arg_count = 0;
2116 }
2117 break;
2118 case 8:
Michal Vasko004d3152020-06-11 19:59:22 +02002119 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002120 min_arg_count = 2;
2121 max_arg_count = 2;
Michal Vasko004d3152020-06-11 19:59:22 +02002122 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002123 min_arg_count = 0;
2124 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002125 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002126 min_arg_count = 2;
2127 max_arg_count = 2;
2128 }
2129 break;
2130 case 9:
Michal Vasko004d3152020-06-11 19:59:22 +02002131 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002132 min_arg_count = 2;
2133 max_arg_count = 3;
Michal Vasko004d3152020-06-11 19:59:22 +02002134 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002135 min_arg_count = 3;
2136 max_arg_count = 3;
2137 }
2138 break;
2139 case 10:
Michal Vasko004d3152020-06-11 19:59:22 +02002140 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002141 min_arg_count = 0;
2142 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002143 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002144 min_arg_count = 1;
2145 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002146 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002147 min_arg_count = 2;
2148 max_arg_count = 2;
2149 }
2150 break;
2151 case 11:
Michal Vasko004d3152020-06-11 19:59:22 +02002152 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002153 min_arg_count = 2;
2154 max_arg_count = 2;
2155 }
2156 break;
2157 case 12:
Michal Vasko004d3152020-06-11 19:59:22 +02002158 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002159 min_arg_count = 2;
2160 max_arg_count = 2;
2161 }
2162 break;
2163 case 13:
Michal Vasko004d3152020-06-11 19:59:22 +02002164 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002165 min_arg_count = 0;
2166 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002167 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002168 min_arg_count = 0;
2169 max_arg_count = 1;
2170 }
2171 break;
2172 case 15:
Michal Vasko004d3152020-06-11 19:59:22 +02002173 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002174 min_arg_count = 0;
2175 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002176 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002177 min_arg_count = 2;
2178 max_arg_count = 2;
2179 }
2180 break;
2181 case 16:
Michal Vasko004d3152020-06-11 19:59:22 +02002182 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002183 min_arg_count = 2;
2184 max_arg_count = 2;
2185 }
2186 break;
2187 case 20:
Michal Vasko004d3152020-06-11 19:59:22 +02002188 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002189 min_arg_count = 2;
2190 max_arg_count = 2;
2191 }
2192 break;
2193 }
2194 if (min_arg_count == -1) {
Michal Vasko004d3152020-06-11 19:59:22 +02002195 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 +02002196 return LY_EINVAL;
2197 }
Michal Vasko004d3152020-06-11 19:59:22 +02002198 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002199
2200 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02002201 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002202 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002203 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002204
2205 /* ( Expr ( ',' Expr )* )? */
2206 arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002207 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002208 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002209 if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002210 ++arg_count;
Michal Vasko004d3152020-06-11 19:59:22 +02002211 rc = reparse_or_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002212 LY_CHECK_RET(rc);
2213 }
Michal Vasko004d3152020-06-11 19:59:22 +02002214 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) {
2215 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002216
2217 ++arg_count;
Michal Vasko004d3152020-06-11 19:59:22 +02002218 rc = reparse_or_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002219 LY_CHECK_RET(rc);
2220 }
2221
2222 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02002223 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002224 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002225 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002226
2227 if ((arg_count < min_arg_count) || (arg_count > max_arg_count)) {
Michal Vasko004d3152020-06-11 19:59:22 +02002228 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INARGCOUNT, arg_count, exp->tok_len[func_tok_idx],
2229 &exp->expr[exp->tok_pos[func_tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002230 return LY_EVALID;
2231 }
2232
2233 return LY_SUCCESS;
2234}
2235
2236/**
2237 * @brief Reparse PathExpr. Logs directly on error.
2238 *
2239 * [10] PathExpr ::= LocationPath | PrimaryExpr Predicate*
2240 * | PrimaryExpr Predicate* '/' RelativeLocationPath
2241 * | PrimaryExpr Predicate* '//' RelativeLocationPath
2242 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
2243 * [8] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall
2244 *
2245 * @param[in] ctx Context for logging.
2246 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002247 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002248 * @return LY_ERR
2249 */
2250static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002251reparse_path_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002252{
2253 LY_ERR rc;
2254
Michal Vasko004d3152020-06-11 19:59:22 +02002255 if (lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko14676352020-05-29 11:35:55 +02002256 return LY_EVALID;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002257 }
2258
Michal Vasko004d3152020-06-11 19:59:22 +02002259 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002260 case LYXP_TOKEN_PAR1:
2261 /* '(' Expr ')' Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002262 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002263
Michal Vasko004d3152020-06-11 19:59:22 +02002264 rc = reparse_or_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002265 LY_CHECK_RET(rc);
2266
Michal Vasko004d3152020-06-11 19:59:22 +02002267 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002268 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002269 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002270 goto predicate;
2271 break;
2272 case LYXP_TOKEN_DOT:
2273 case LYXP_TOKEN_DDOT:
2274 case LYXP_TOKEN_AT:
2275 case LYXP_TOKEN_NAMETEST:
2276 case LYXP_TOKEN_NODETYPE:
2277 /* RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002278 rc = reparse_relative_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002279 LY_CHECK_RET(rc);
2280 break;
2281 case LYXP_TOKEN_FUNCNAME:
2282 /* FunctionCall */
Michal Vasko004d3152020-06-11 19:59:22 +02002283 rc = reparse_function_call(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002284 LY_CHECK_RET(rc);
2285 goto predicate;
2286 break;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002287 case LYXP_TOKEN_OPER_PATH:
2288 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02002289 /* AbsoluteLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002290 rc = reparse_absolute_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002291 LY_CHECK_RET(rc);
2292 break;
2293 case LYXP_TOKEN_LITERAL:
2294 /* Literal */
Michal Vasko004d3152020-06-11 19:59:22 +02002295 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002296 goto predicate;
2297 break;
2298 case LYXP_TOKEN_NUMBER:
2299 /* Number */
Michal Vasko004d3152020-06-11 19:59:22 +02002300 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002301 goto predicate;
2302 break;
2303 default:
2304 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK,
Michal Vasko004d3152020-06-11 19:59:22 +02002305 lyxp_print_token(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002306 return LY_EVALID;
2307 }
2308
2309 return LY_SUCCESS;
2310
2311predicate:
2312 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002313 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
2314 rc = reparse_predicate(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002315 LY_CHECK_RET(rc);
2316 }
2317
2318 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002319 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002320
2321 /* '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02002322 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002323
Michal Vasko004d3152020-06-11 19:59:22 +02002324 rc = reparse_relative_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002325 LY_CHECK_RET(rc);
2326 }
2327
2328 return LY_SUCCESS;
2329}
2330
2331/**
2332 * @brief Reparse UnaryExpr. Logs directly on error.
2333 *
2334 * [17] UnaryExpr ::= UnionExpr | '-' UnaryExpr
2335 * [18] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
2336 *
2337 * @param[in] ctx Context for logging.
2338 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002339 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002340 * @return LY_ERR
2341 */
2342static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002343reparse_unary_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002344{
2345 uint16_t prev_exp;
2346 LY_ERR rc;
2347
2348 /* ('-')* */
Michal Vasko004d3152020-06-11 19:59:22 +02002349 prev_exp = *tok_idx;
2350 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH)
2351 && (exp->expr[exp->tok_pos[*tok_idx]] == '-')) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002352 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNARY);
Michal Vasko004d3152020-06-11 19:59:22 +02002353 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002354 }
2355
2356 /* PathExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002357 prev_exp = *tok_idx;
2358 rc = reparse_path_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002359 LY_CHECK_RET(rc);
2360
2361 /* ('|' PathExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002362 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_UNI)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002363 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNION);
Michal Vasko004d3152020-06-11 19:59:22 +02002364 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002365
Michal Vasko004d3152020-06-11 19:59:22 +02002366 rc = reparse_path_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002367 LY_CHECK_RET(rc);
2368 }
2369
2370 return LY_SUCCESS;
2371}
2372
2373/**
2374 * @brief Reparse AdditiveExpr. Logs directly on error.
2375 *
2376 * [15] AdditiveExpr ::= MultiplicativeExpr
2377 * | AdditiveExpr '+' MultiplicativeExpr
2378 * | AdditiveExpr '-' MultiplicativeExpr
2379 * [16] MultiplicativeExpr ::= UnaryExpr
2380 * | MultiplicativeExpr '*' UnaryExpr
2381 * | MultiplicativeExpr 'div' UnaryExpr
2382 * | MultiplicativeExpr 'mod' UnaryExpr
2383 *
2384 * @param[in] ctx Context for logging.
2385 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002386 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002387 * @return LY_ERR
2388 */
2389static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002390reparse_additive_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002391{
2392 uint16_t prev_add_exp, prev_mul_exp;
2393 LY_ERR rc;
2394
Michal Vasko004d3152020-06-11 19:59:22 +02002395 prev_add_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002396 goto reparse_multiplicative_expr;
2397
2398 /* ('+' / '-' MultiplicativeExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002399 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH)
2400 && ((exp->expr[exp->tok_pos[*tok_idx]] == '+') || (exp->expr[exp->tok_pos[*tok_idx]] == '-'))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002401 exp_repeat_push(exp, prev_add_exp, LYXP_EXPR_ADDITIVE);
Michal Vasko004d3152020-06-11 19:59:22 +02002402 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002403
2404reparse_multiplicative_expr:
2405 /* UnaryExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002406 prev_mul_exp = *tok_idx;
2407 rc = reparse_unary_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002408 LY_CHECK_RET(rc);
2409
2410 /* ('*' / 'div' / 'mod' UnaryExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002411 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH)
2412 && ((exp->expr[exp->tok_pos[*tok_idx]] == '*') || (exp->tok_len[*tok_idx] == 3))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002413 exp_repeat_push(exp, prev_mul_exp, LYXP_EXPR_MULTIPLICATIVE);
Michal Vasko004d3152020-06-11 19:59:22 +02002414 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002415
Michal Vasko004d3152020-06-11 19:59:22 +02002416 rc = reparse_unary_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002417 LY_CHECK_RET(rc);
2418 }
2419 }
2420
2421 return LY_SUCCESS;
2422}
2423
2424/**
2425 * @brief Reparse EqualityExpr. Logs directly on error.
2426 *
2427 * [13] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
2428 * | EqualityExpr '!=' RelationalExpr
2429 * [14] RelationalExpr ::= AdditiveExpr
2430 * | RelationalExpr '<' AdditiveExpr
2431 * | RelationalExpr '>' AdditiveExpr
2432 * | RelationalExpr '<=' AdditiveExpr
2433 * | RelationalExpr '>=' AdditiveExpr
2434 *
2435 * @param[in] ctx Context for logging.
2436 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002437 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002438 * @return LY_ERR
2439 */
2440static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002441reparse_equality_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002442{
2443 uint16_t prev_eq_exp, prev_rel_exp;
2444 LY_ERR rc;
2445
Michal Vasko004d3152020-06-11 19:59:22 +02002446 prev_eq_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002447 goto reparse_additive_expr;
2448
2449 /* ('=' / '!=' RelationalExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002450 while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_EQUAL, LYXP_TOKEN_OPER_NEQUAL)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002451 exp_repeat_push(exp, prev_eq_exp, LYXP_EXPR_EQUALITY);
Michal Vasko004d3152020-06-11 19:59:22 +02002452 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002453
2454reparse_additive_expr:
2455 /* AdditiveExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002456 prev_rel_exp = *tok_idx;
2457 rc = reparse_additive_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002458 LY_CHECK_RET(rc);
2459
2460 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002461 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_COMP)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002462 exp_repeat_push(exp, prev_rel_exp, LYXP_EXPR_RELATIONAL);
Michal Vasko004d3152020-06-11 19:59:22 +02002463 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002464
Michal Vasko004d3152020-06-11 19:59:22 +02002465 rc = reparse_additive_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002466 LY_CHECK_RET(rc);
2467 }
2468 }
2469
2470 return LY_SUCCESS;
2471}
2472
2473/**
2474 * @brief Reparse OrExpr. Logs directly on error.
2475 *
2476 * [11] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
2477 * [12] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
2478 *
2479 * @param[in] ctx Context for logging.
2480 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002481 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002482 * @return LY_ERR
2483 */
2484static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002485reparse_or_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002486{
2487 uint16_t prev_or_exp, prev_and_exp;
2488 LY_ERR rc;
2489
Michal Vasko004d3152020-06-11 19:59:22 +02002490 prev_or_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002491 goto reparse_equality_expr;
2492
2493 /* ('or' AndExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002494 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 +02002495 exp_repeat_push(exp, prev_or_exp, LYXP_EXPR_OR);
Michal Vasko004d3152020-06-11 19:59:22 +02002496 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002497
2498reparse_equality_expr:
2499 /* EqualityExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002500 prev_and_exp = *tok_idx;
2501 rc = reparse_equality_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002502 LY_CHECK_RET(rc);
2503
2504 /* ('and' EqualityExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002505 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 +02002506 exp_repeat_push(exp, prev_and_exp, LYXP_EXPR_AND);
Michal Vasko004d3152020-06-11 19:59:22 +02002507 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002508
Michal Vasko004d3152020-06-11 19:59:22 +02002509 rc = reparse_equality_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002510 LY_CHECK_RET(rc);
2511 }
2512 }
2513
2514 return LY_SUCCESS;
2515}
Radek Krejcib1646a92018-11-02 16:08:26 +01002516
2517/**
2518 * @brief Parse NCName.
2519 *
2520 * @param[in] ncname Name to parse.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002521 * @return Length of @p ncname valid bytes.
Radek Krejcib1646a92018-11-02 16:08:26 +01002522 */
Radek Krejcid4270262019-01-07 15:07:25 +01002523static long int
Radek Krejcib1646a92018-11-02 16:08:26 +01002524parse_ncname(const char *ncname)
2525{
2526 unsigned int uc;
Radek Krejcid4270262019-01-07 15:07:25 +01002527 size_t size;
2528 long int len = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002529
2530 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), 0);
2531 if (!is_xmlqnamestartchar(uc) || (uc == ':')) {
2532 return len;
2533 }
2534
2535 do {
2536 len += size;
Radek Krejci9a564c92019-01-07 14:53:57 +01002537 if (!*ncname) {
2538 break;
2539 }
Radek Krejcid4270262019-01-07 15:07:25 +01002540 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), -len);
Radek Krejcib1646a92018-11-02 16:08:26 +01002541 } while (is_xmlqnamechar(uc) && (uc != ':'));
2542
2543 return len;
2544}
2545
2546/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02002547 * @brief Add @p token into the expression @p exp.
Radek Krejcib1646a92018-11-02 16:08:26 +01002548 *
Michal Vasko03ff5a72019-09-11 13:49:33 +02002549 * @param[in] ctx Context for logging.
Radek Krejcib1646a92018-11-02 16:08:26 +01002550 * @param[in] exp Expression to use.
2551 * @param[in] token Token to add.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002552 * @param[in] tok_pos Token position in the XPath expression.
Radek Krejcib1646a92018-11-02 16:08:26 +01002553 * @param[in] tok_len Token length in the XPath expression.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002554 * @return LY_ERR
Radek Krejcib1646a92018-11-02 16:08:26 +01002555 */
2556static LY_ERR
Michal Vasko14676352020-05-29 11:35:55 +02002557exp_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 +01002558{
2559 uint32_t prev;
2560
2561 if (exp->used == exp->size) {
2562 prev = exp->size;
2563 exp->size += LYXP_EXPR_SIZE_STEP;
2564 if (prev > exp->size) {
2565 LOGINT(ctx);
2566 return LY_EINT;
2567 }
2568
2569 exp->tokens = ly_realloc(exp->tokens, exp->size * sizeof *exp->tokens);
2570 LY_CHECK_ERR_RET(!exp->tokens, LOGMEM(ctx), LY_EMEM);
2571 exp->tok_pos = ly_realloc(exp->tok_pos, exp->size * sizeof *exp->tok_pos);
2572 LY_CHECK_ERR_RET(!exp->tok_pos, LOGMEM(ctx), LY_EMEM);
2573 exp->tok_len = ly_realloc(exp->tok_len, exp->size * sizeof *exp->tok_len);
2574 LY_CHECK_ERR_RET(!exp->tok_len, LOGMEM(ctx), LY_EMEM);
2575 }
2576
2577 exp->tokens[exp->used] = token;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002578 exp->tok_pos[exp->used] = tok_pos;
Radek Krejcib1646a92018-11-02 16:08:26 +01002579 exp->tok_len[exp->used] = tok_len;
2580 ++exp->used;
2581 return LY_SUCCESS;
2582}
2583
2584void
Michal Vasko14676352020-05-29 11:35:55 +02002585lyxp_expr_free(const struct ly_ctx *ctx, struct lyxp_expr *expr)
Radek Krejcib1646a92018-11-02 16:08:26 +01002586{
2587 uint16_t i;
2588
2589 if (!expr) {
2590 return;
2591 }
2592
2593 lydict_remove(ctx, expr->expr);
2594 free(expr->tokens);
2595 free(expr->tok_pos);
2596 free(expr->tok_len);
2597 if (expr->repeat) {
2598 for (i = 0; i < expr->used; ++i) {
2599 free(expr->repeat[i]);
2600 }
2601 }
2602 free(expr->repeat);
2603 free(expr);
2604}
2605
2606struct lyxp_expr *
Michal Vasko004d3152020-06-11 19:59:22 +02002607lyxp_expr_parse(const struct ly_ctx *ctx, const char *expr, size_t expr_len, int reparse)
Radek Krejcib1646a92018-11-02 16:08:26 +01002608{
2609 struct lyxp_expr *ret;
Radek Krejcid4270262019-01-07 15:07:25 +01002610 size_t parsed = 0, tok_len;
2611 long int ncname_len;
Radek Krejcib1646a92018-11-02 16:08:26 +01002612 enum lyxp_token tok_type;
2613 int prev_function_check = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002614 uint16_t tok_idx = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002615
Michal Vasko004d3152020-06-11 19:59:22 +02002616 if (!expr[0]) {
2617 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOF);
2618 return NULL;
2619 }
2620
2621 if (!expr_len) {
2622 expr_len = strlen(expr);
2623 }
2624 if (expr_len > UINT16_MAX) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002625 LOGERR(ctx, LY_EINVAL, "XPath expression cannot be longer than %ud characters.", UINT16_MAX);
2626 return NULL;
2627 }
2628
2629 /* init lyxp_expr structure */
2630 ret = calloc(1, sizeof *ret);
2631 LY_CHECK_ERR_GOTO(!ret, LOGMEM(ctx), error);
Michal Vasko004d3152020-06-11 19:59:22 +02002632 ret->expr = lydict_insert(ctx, expr, expr_len);
Radek Krejcib1646a92018-11-02 16:08:26 +01002633 LY_CHECK_ERR_GOTO(!ret->expr, LOGMEM(ctx), error);
2634 ret->used = 0;
2635 ret->size = LYXP_EXPR_SIZE_START;
2636 ret->tokens = malloc(ret->size * sizeof *ret->tokens);
2637 LY_CHECK_ERR_GOTO(!ret->tokens, LOGMEM(ctx), error);
2638
2639 ret->tok_pos = malloc(ret->size * sizeof *ret->tok_pos);
2640 LY_CHECK_ERR_GOTO(!ret->tok_pos, LOGMEM(ctx), error);
2641
2642 ret->tok_len = malloc(ret->size * sizeof *ret->tok_len);
2643 LY_CHECK_ERR_GOTO(!ret->tok_len, LOGMEM(ctx), error);
2644
Michal Vasko004d3152020-06-11 19:59:22 +02002645 /* make expr 0-terminated */
2646 expr = ret->expr;
2647
Radek Krejcib1646a92018-11-02 16:08:26 +01002648 while (is_xmlws(expr[parsed])) {
2649 ++parsed;
2650 }
2651
2652 do {
2653 if (expr[parsed] == '(') {
2654
2655 /* '(' */
2656 tok_len = 1;
2657 tok_type = LYXP_TOKEN_PAR1;
2658
2659 if (prev_function_check && ret->used && (ret->tokens[ret->used - 1] == LYXP_TOKEN_NAMETEST)) {
2660 /* it is a NodeType/FunctionName after all */
2661 if (((ret->tok_len[ret->used - 1] == 4)
2662 && (!strncmp(&expr[ret->tok_pos[ret->used - 1]], "node", 4)
2663 || !strncmp(&expr[ret->tok_pos[ret->used - 1]], "text", 4))) ||
2664 ((ret->tok_len[ret->used - 1] == 7)
2665 && !strncmp(&expr[ret->tok_pos[ret->used - 1]], "comment", 7))) {
2666 ret->tokens[ret->used - 1] = LYXP_TOKEN_NODETYPE;
2667 } else {
2668 ret->tokens[ret->used - 1] = LYXP_TOKEN_FUNCNAME;
2669 }
2670 prev_function_check = 0;
2671 }
2672
2673 } else if (expr[parsed] == ')') {
2674
2675 /* ')' */
2676 tok_len = 1;
2677 tok_type = LYXP_TOKEN_PAR2;
2678
2679 } else if (expr[parsed] == '[') {
2680
2681 /* '[' */
2682 tok_len = 1;
2683 tok_type = LYXP_TOKEN_BRACK1;
2684
2685 } else if (expr[parsed] == ']') {
2686
2687 /* ']' */
2688 tok_len = 1;
2689 tok_type = LYXP_TOKEN_BRACK2;
2690
2691 } else if (!strncmp(&expr[parsed], "..", 2)) {
2692
2693 /* '..' */
2694 tok_len = 2;
2695 tok_type = LYXP_TOKEN_DDOT;
2696
2697 } else if ((expr[parsed] == '.') && (!isdigit(expr[parsed + 1]))) {
2698
2699 /* '.' */
2700 tok_len = 1;
2701 tok_type = LYXP_TOKEN_DOT;
2702
2703 } else if (expr[parsed] == '@') {
2704
2705 /* '@' */
2706 tok_len = 1;
2707 tok_type = LYXP_TOKEN_AT;
2708
2709 } else if (expr[parsed] == ',') {
2710
2711 /* ',' */
2712 tok_len = 1;
2713 tok_type = LYXP_TOKEN_COMMA;
2714
2715 } else if (expr[parsed] == '\'') {
2716
2717 /* Literal with ' */
2718 for (tok_len = 1; (expr[parsed + tok_len] != '\0') && (expr[parsed + tok_len] != '\''); ++tok_len);
2719 LY_CHECK_ERR_GOTO(expr[parsed + tok_len] == '\0',
2720 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOE, expr[parsed], &expr[parsed]), error);
2721 ++tok_len;
2722 tok_type = LYXP_TOKEN_LITERAL;
2723
2724 } else if (expr[parsed] == '\"') {
2725
2726 /* Literal with " */
2727 for (tok_len = 1; (expr[parsed + tok_len] != '\0') && (expr[parsed + tok_len] != '\"'); ++tok_len);
2728 LY_CHECK_ERR_GOTO(expr[parsed + tok_len] == '\0',
2729 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOE, expr[parsed], &expr[parsed]), error);
2730 ++tok_len;
2731 tok_type = LYXP_TOKEN_LITERAL;
2732
2733 } else if ((expr[parsed] == '.') || (isdigit(expr[parsed]))) {
2734
2735 /* Number */
2736 for (tok_len = 0; isdigit(expr[parsed + tok_len]); ++tok_len);
2737 if (expr[parsed + tok_len] == '.') {
2738 ++tok_len;
2739 for (; isdigit(expr[parsed + tok_len]); ++tok_len);
2740 }
2741 tok_type = LYXP_TOKEN_NUMBER;
2742
2743 } else if (expr[parsed] == '/') {
2744
2745 /* Operator '/', '//' */
2746 if (!strncmp(&expr[parsed], "//", 2)) {
2747 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002748 tok_type = LYXP_TOKEN_OPER_RPATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002749 } else {
2750 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002751 tok_type = LYXP_TOKEN_OPER_PATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002752 }
Radek Krejcib1646a92018-11-02 16:08:26 +01002753
Michal Vasko3e48bf32020-06-01 08:39:07 +02002754 } else if (!strncmp(&expr[parsed], "!=", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002755
Michal Vasko3e48bf32020-06-01 08:39:07 +02002756 /* Operator '!=' */
Radek Krejcib1646a92018-11-02 16:08:26 +01002757 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002758 tok_type = LYXP_TOKEN_OPER_NEQUAL;
2759
2760 } else if (!strncmp(&expr[parsed], "<=", 2) || !strncmp(&expr[parsed], ">=", 2)) {
2761
2762 /* Operator '<=', '>=' */
2763 tok_len = 2;
2764 tok_type = LYXP_TOKEN_OPER_COMP;
Radek Krejcib1646a92018-11-02 16:08:26 +01002765
2766 } else if (expr[parsed] == '|') {
2767
2768 /* Operator '|' */
2769 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002770 tok_type = LYXP_TOKEN_OPER_UNI;
Radek Krejcib1646a92018-11-02 16:08:26 +01002771
2772 } else if ((expr[parsed] == '+') || (expr[parsed] == '-')) {
2773
2774 /* Operator '+', '-' */
2775 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002776 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002777
Michal Vasko3e48bf32020-06-01 08:39:07 +02002778 } else if (expr[parsed] == '=') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002779
Michal Vasko3e48bf32020-06-01 08:39:07 +02002780 /* Operator '=' */
Radek Krejcib1646a92018-11-02 16:08:26 +01002781 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002782 tok_type = LYXP_TOKEN_OPER_EQUAL;
2783
2784 } else if ((expr[parsed] == '<') || (expr[parsed] == '>')) {
2785
2786 /* Operator '<', '>' */
2787 tok_len = 1;
2788 tok_type = LYXP_TOKEN_OPER_COMP;
Radek Krejcib1646a92018-11-02 16:08:26 +01002789
2790 } else if (ret->used && (ret->tokens[ret->used - 1] != LYXP_TOKEN_AT)
2791 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_PAR1)
2792 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_BRACK1)
2793 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_COMMA)
Michal Vasko3e48bf32020-06-01 08:39:07 +02002794 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_LOG)
2795 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_EQUAL)
2796 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_NEQUAL)
2797 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_COMP)
2798 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_MATH)
2799 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_UNI)
2800 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_PATH)
2801 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_RPATH)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002802
2803 /* Operator '*', 'or', 'and', 'mod', or 'div' */
2804 if (expr[parsed] == '*') {
2805 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002806 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002807
2808 } else if (!strncmp(&expr[parsed], "or", 2)) {
2809 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002810 tok_type = LYXP_TOKEN_OPER_LOG;
Radek Krejcib1646a92018-11-02 16:08:26 +01002811
2812 } else if (!strncmp(&expr[parsed], "and", 3)) {
2813 tok_len = 3;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002814 tok_type = LYXP_TOKEN_OPER_LOG;
Radek Krejcib1646a92018-11-02 16:08:26 +01002815
2816 } else if (!strncmp(&expr[parsed], "mod", 3) || !strncmp(&expr[parsed], "div", 3)) {
2817 tok_len = 3;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002818 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002819
2820 } else if (prev_function_check) {
Michal Vasko53078572019-05-24 08:50:15 +02002821 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "Invalid character 0x%x ('%c'), perhaps \"%.*s\" is supposed to be a function call.",
2822 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 +01002823 goto error;
2824 } else {
Radek Krejcid4270262019-01-07 15:07:25 +01002825 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INEXPR, parsed + 1, expr);
Radek Krejcib1646a92018-11-02 16:08:26 +01002826 goto error;
2827 }
2828 } else if (expr[parsed] == '*') {
2829
2830 /* NameTest '*' */
2831 tok_len = 1;
2832 tok_type = LYXP_TOKEN_NAMETEST;
2833
2834 } else {
2835
2836 /* NameTest (NCName ':' '*' | QName) or NodeType/FunctionName */
2837 ncname_len = parse_ncname(&expr[parsed]);
Radek Krejcid4270262019-01-07 15:07:25 +01002838 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 +01002839 tok_len = ncname_len;
2840
2841 if (expr[parsed + tok_len] == ':') {
2842 ++tok_len;
2843 if (expr[parsed + tok_len] == '*') {
2844 ++tok_len;
2845 } else {
2846 ncname_len = parse_ncname(&expr[parsed + tok_len]);
Radek Krejcid4270262019-01-07 15:07:25 +01002847 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 +01002848 tok_len += ncname_len;
2849 }
2850 /* remove old flag to prevent ambiguities */
2851 prev_function_check = 0;
2852 tok_type = LYXP_TOKEN_NAMETEST;
2853 } else {
2854 /* there is no prefix so it can still be NodeType/FunctionName, we can't finally decide now */
2855 prev_function_check = 1;
2856 tok_type = LYXP_TOKEN_NAMETEST;
2857 }
2858 }
2859
2860 /* store the token, move on to the next one */
2861 LY_CHECK_GOTO(exp_add_token(ctx, ret, tok_type, parsed, tok_len), error);
2862 parsed += tok_len;
2863 while (is_xmlws(expr[parsed])) {
2864 ++parsed;
2865 }
2866
2867 } while (expr[parsed]);
2868
Michal Vasko004d3152020-06-11 19:59:22 +02002869 if (reparse) {
2870 /* prealloc repeat */
2871 ret->repeat = calloc(ret->size, sizeof *ret->repeat);
2872 LY_CHECK_ERR_GOTO(!ret->repeat, LOGMEM(ctx), error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002873
Michal Vasko004d3152020-06-11 19:59:22 +02002874 /* fill repeat */
2875 LY_CHECK_GOTO(reparse_or_expr(ctx, ret, &tok_idx), error);
2876 if (ret->used > tok_idx) {
2877 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "Unparsed characters \"%s\" left at the end of an XPath expression.",
2878 &ret->expr[ret->tok_pos[tok_idx]]);
2879 goto error;
2880 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02002881 }
2882
2883 print_expr_struct_debug(ret);
2884
Radek Krejcib1646a92018-11-02 16:08:26 +01002885 return ret;
2886
2887error:
2888 lyxp_expr_free(ctx, ret);
2889 return NULL;
2890}
2891
Michal Vasko004d3152020-06-11 19:59:22 +02002892struct lyxp_expr *
2893lyxp_expr_dup(const struct ly_ctx *ctx, const struct lyxp_expr *exp)
2894{
2895 struct lyxp_expr *dup;
2896 uint32_t i, j;
2897
2898 dup = calloc(1, sizeof *dup);
2899 LY_CHECK_ERR_GOTO(!dup, LOGMEM(ctx), error);
2900
2901 dup->tokens = malloc(exp->used * sizeof *dup->tokens);
2902 LY_CHECK_ERR_GOTO(!dup->tokens, LOGMEM(ctx), error);
2903 memcpy(dup->tokens, exp->tokens, exp->used * sizeof *dup->tokens);
2904
2905 dup->tok_pos = malloc(exp->used * sizeof *dup->tok_pos);
2906 LY_CHECK_ERR_GOTO(!dup->tok_pos, LOGMEM(ctx), error);
2907 memcpy(dup->tok_pos, exp->tok_pos, exp->used * sizeof *dup->tok_pos);
2908
2909 dup->tok_len = malloc(exp->used * sizeof *dup->tok_len);
2910 LY_CHECK_ERR_GOTO(!dup->tok_len, LOGMEM(ctx), error);
2911 memcpy(dup->tok_len, exp->tok_len, exp->used * sizeof *dup->tok_len);
2912
2913 dup->repeat = malloc(exp->used * sizeof *dup->repeat);
2914 LY_CHECK_ERR_GOTO(!dup->repeat, LOGMEM(ctx), error);
2915 for (i = 0; i < exp->used; ++i) {
2916 if (!exp->repeat[i]) {
2917 dup->repeat[i] = NULL;
2918 } else {
2919 for (j = 0; exp->repeat[i][j]; ++j);
2920 /* the ending 0 as well */
2921 ++j;
2922
2923 dup->repeat[i] = malloc(j * sizeof *dup->repeat);
2924 LY_CHECK_ERR_GOTO(!dup->repeat[i], LOGMEM(ctx), error);
2925 memcpy(dup->repeat[i], exp->repeat[i], j * sizeof **dup->repeat);
2926 dup->repeat[i][j - 1] = 0;
2927 }
2928 }
2929
2930 dup->used = exp->used;
2931 dup->size = exp->used;
2932 dup->expr = lydict_insert(ctx, exp->expr, 0);
2933
2934 return dup;
2935
2936error:
2937 lyxp_expr_free(ctx, dup);
2938 return NULL;
2939}
2940
Michal Vasko03ff5a72019-09-11 13:49:33 +02002941/*
2942 * warn functions
2943 *
2944 * Warn functions check specific reasonable conditions for schema XPath
2945 * and print a warning if they are not satisfied.
2946 */
2947
2948/**
2949 * @brief Get the last-added schema node that is currently in the context.
2950 *
2951 * @param[in] set Set to search in.
2952 * @return Last-added schema context node, NULL if no node is in context.
2953 */
2954static struct lysc_node *
2955warn_get_scnode_in_ctx(struct lyxp_set *set)
2956{
2957 uint32_t i;
2958
2959 if (!set || (set->type != LYXP_SET_SCNODE_SET)) {
2960 return NULL;
2961 }
2962
2963 i = set->used;
2964 do {
2965 --i;
2966 if (set->val.scnodes[i].in_ctx == 1) {
2967 /* if there are more, simply return the first found (last added) */
2968 return set->val.scnodes[i].scnode;
2969 }
2970 } while (i);
2971
2972 return NULL;
2973}
2974
2975/**
2976 * @brief Test whether a type is numeric - integer type or decimal64.
2977 *
2978 * @param[in] type Type to test.
2979 * @return 1 if numeric, 0 otherwise.
2980 */
2981static int
2982warn_is_numeric_type(struct lysc_type *type)
2983{
2984 struct lysc_type_union *uni;
2985 int ret;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02002986 LY_ARRAY_SIZE_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002987
2988 switch (type->basetype) {
2989 case LY_TYPE_DEC64:
2990 case LY_TYPE_INT8:
2991 case LY_TYPE_UINT8:
2992 case LY_TYPE_INT16:
2993 case LY_TYPE_UINT16:
2994 case LY_TYPE_INT32:
2995 case LY_TYPE_UINT32:
2996 case LY_TYPE_INT64:
2997 case LY_TYPE_UINT64:
2998 return 1;
2999 case LY_TYPE_UNION:
3000 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003001 LY_ARRAY_FOR(uni->types, u) {
3002 ret = warn_is_numeric_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003003 if (ret) {
3004 /* found a suitable type */
3005 return 1;
3006 }
3007 }
3008 /* did not find any suitable type */
3009 return 0;
3010 case LY_TYPE_LEAFREF:
3011 return warn_is_numeric_type(((struct lysc_type_leafref *)type)->realtype);
3012 default:
3013 return 0;
3014 }
3015}
3016
3017/**
3018 * @brief Test whether a type is string-like - no integers, decimal64 or binary.
3019 *
3020 * @param[in] type Type to test.
3021 * @return 1 if string, 0 otherwise.
3022 */
3023static int
3024warn_is_string_type(struct lysc_type *type)
3025{
3026 struct lysc_type_union *uni;
3027 int ret;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003028 LY_ARRAY_SIZE_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003029
3030 switch (type->basetype) {
3031 case LY_TYPE_BITS:
3032 case LY_TYPE_ENUM:
3033 case LY_TYPE_IDENT:
3034 case LY_TYPE_INST:
3035 case LY_TYPE_STRING:
3036 return 1;
3037 case LY_TYPE_UNION:
3038 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003039 LY_ARRAY_FOR(uni->types, u) {
3040 ret = warn_is_string_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003041 if (ret) {
3042 /* found a suitable type */
3043 return 1;
3044 }
3045 }
3046 /* did not find any suitable type */
3047 return 0;
3048 case LY_TYPE_LEAFREF:
3049 return warn_is_string_type(((struct lysc_type_leafref *)type)->realtype);
3050 default:
3051 return 0;
3052 }
3053}
3054
3055/**
3056 * @brief Test whether a type is one specific type.
3057 *
3058 * @param[in] type Type to test.
3059 * @param[in] base Expected type.
3060 * @return 1 if it is, 0 otherwise.
3061 */
3062static int
3063warn_is_specific_type(struct lysc_type *type, LY_DATA_TYPE base)
3064{
3065 struct lysc_type_union *uni;
3066 int ret;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003067 LY_ARRAY_SIZE_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003068
3069 if (type->basetype == base) {
3070 return 1;
3071 } else if (type->basetype == LY_TYPE_UNION) {
3072 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003073 LY_ARRAY_FOR(uni->types, u) {
3074 ret = warn_is_specific_type(uni->types[u], base);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003075 if (ret) {
3076 /* found a suitable type */
3077 return 1;
3078 }
3079 }
3080 /* did not find any suitable type */
3081 return 0;
3082 } else if (type->basetype == LY_TYPE_LEAFREF) {
3083 return warn_is_specific_type(((struct lysc_type_leafref *)type)->realtype, base);
3084 }
3085
3086 return 0;
3087}
3088
3089/**
3090 * @brief Get next type of a (union) type.
3091 *
3092 * @param[in] type Base type.
3093 * @param[in] prev_type Previously returned type.
3094 * @return Next type or NULL.
3095 */
3096static struct lysc_type *
3097warn_is_equal_type_next_type(struct lysc_type *type, struct lysc_type *prev_type)
3098{
3099 struct lysc_type_union *uni;
3100 int found = 0;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003101 LY_ARRAY_SIZE_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003102
3103 switch (type->basetype) {
3104 case LY_TYPE_UNION:
3105 uni = (struct lysc_type_union *)type;
3106 if (!prev_type) {
3107 return uni->types[0];
3108 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003109 LY_ARRAY_FOR(uni->types, u) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003110 if (found) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003111 return uni->types[u];
Michal Vasko03ff5a72019-09-11 13:49:33 +02003112 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003113 if (prev_type == uni->types[u]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003114 found = 1;
3115 }
3116 }
3117 return NULL;
3118 default:
3119 if (prev_type) {
3120 assert(type == prev_type);
3121 return NULL;
3122 } else {
3123 return type;
3124 }
3125 }
3126}
3127
3128/**
3129 * @brief Test whether 2 types have a common type.
3130 *
3131 * @param[in] type1 First type.
3132 * @param[in] type2 Second type.
3133 * @return 1 if they do, 0 otherwise.
3134 */
3135static int
3136warn_is_equal_type(struct lysc_type *type1, struct lysc_type *type2)
3137{
3138 struct lysc_type *t1, *rt1, *t2, *rt2;
3139
3140 t1 = NULL;
3141 while ((t1 = warn_is_equal_type_next_type(type1, t1))) {
3142 if (t1->basetype == LY_TYPE_LEAFREF) {
3143 rt1 = ((struct lysc_type_leafref *)t1)->realtype;
3144 } else {
3145 rt1 = t1;
3146 }
3147
3148 t2 = NULL;
3149 while ((t2 = warn_is_equal_type_next_type(type2, t2))) {
3150 if (t2->basetype == LY_TYPE_LEAFREF) {
3151 rt2 = ((struct lysc_type_leafref *)t2)->realtype;
3152 } else {
3153 rt2 = t2;
3154 }
3155
3156 if (rt2->basetype == rt1->basetype) {
3157 /* match found */
3158 return 1;
3159 }
3160 }
3161 }
3162
3163 return 0;
3164}
3165
3166/**
3167 * @brief Check both operands of comparison operators.
3168 *
3169 * @param[in] ctx Context for errors.
3170 * @param[in] set1 First operand set.
3171 * @param[in] set2 Second operand set.
3172 * @param[in] numbers_only Whether accept only numbers or other types are fine too (for '=' and '!=').
3173 * @param[in] expr Start of the expression to print with the warning.
3174 * @param[in] tok_pos Token position.
3175 */
3176static void
3177warn_operands(struct ly_ctx *ctx, struct lyxp_set *set1, struct lyxp_set *set2, int numbers_only, const char *expr, uint16_t tok_pos)
3178{
3179 struct lysc_node_leaf *node1, *node2;
3180 int leaves = 1, warning = 0;
3181
3182 node1 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set1);
3183 node2 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set2);
3184
3185 if (!node1 && !node2) {
3186 /* no node-sets involved, nothing to do */
3187 return;
3188 }
3189
3190 if (node1) {
3191 if (!(node1->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3192 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node1->nodetype), node1->name);
3193 warning = 1;
3194 leaves = 0;
3195 } else if (numbers_only && !warn_is_numeric_type(node1->type)) {
3196 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node1->name);
3197 warning = 1;
3198 }
3199 }
3200
3201 if (node2) {
3202 if (!(node2->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3203 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node2->nodetype), node2->name);
3204 warning = 1;
3205 leaves = 0;
3206 } else if (numbers_only && !warn_is_numeric_type(node2->type)) {
3207 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node2->name);
3208 warning = 1;
3209 }
3210 }
3211
3212 if (node1 && node2 && leaves && !numbers_only) {
3213 if ((warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type))
3214 || (!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_equal_type(node1->type, node2->type))) {
3217 LOGWRN(ctx, "Incompatible types of operands \"%s\" and \"%s\" for comparison.", node1->name, node2->name);
3218 warning = 1;
3219 }
3220 }
3221
3222 if (warning) {
3223 LOGWRN(ctx, "Previous warning generated by XPath subexpression[%u] \"%.20s\".", tok_pos, expr + tok_pos);
3224 }
3225}
3226
3227/**
3228 * @brief Check that a value is valid for a leaf. If not applicable, does nothing.
3229 *
3230 * @param[in] exp Parsed XPath expression.
3231 * @param[in] set Set with the leaf/leaf-list.
3232 * @param[in] val_exp Index of the value (literal/number) in @p exp.
3233 * @param[in] equal_exp Index of the start of the equality expression in @p exp.
3234 * @param[in] last_equal_exp Index of the end of the equality expression in @p exp.
3235 */
3236static void
3237warn_equality_value(struct lyxp_expr *exp, struct lyxp_set *set, uint16_t val_exp, uint16_t equal_exp, uint16_t last_equal_exp)
3238{
3239 struct lysc_node *scnode;
3240 struct lysc_type *type;
3241 char *value;
3242 LY_ERR rc;
3243 struct ly_err_item *err = NULL;
3244
3245 if ((scnode = warn_get_scnode_in_ctx(set)) && (scnode->nodetype & (LYS_LEAF | LYS_LEAFLIST))
3246 && ((exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) || (exp->tokens[val_exp] == LYXP_TOKEN_NUMBER))) {
3247 /* check that the node can have the specified value */
3248 if (exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) {
3249 value = strndup(exp->expr + exp->tok_pos[val_exp] + 1, exp->tok_len[val_exp] - 2);
3250 } else {
3251 value = strndup(exp->expr + exp->tok_pos[val_exp], exp->tok_len[val_exp]);
3252 }
3253 if (!value) {
3254 LOGMEM(set->ctx);
3255 return;
3256 }
3257
3258 if ((((struct lysc_node_leaf *)scnode)->type->basetype == LY_TYPE_IDENT) && !strchr(value, ':')) {
3259 LOGWRN(set->ctx, "Identityref \"%s\" comparison with identity \"%s\" without prefix, consider adding"
3260 " a prefix or best using \"derived-from(-or-self)()\" functions.", scnode->name, value);
3261 LOGWRN(set->ctx, "Previous warning generated by XPath subexpression[%u] \"%.*s\".", exp->tok_pos[equal_exp],
3262 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
3263 exp->expr + exp->tok_pos[equal_exp]);
3264 }
3265
3266 type = ((struct lysc_node_leaf *)scnode)->type;
3267 if (type->basetype != LY_TYPE_IDENT) {
3268 rc = type->plugin->store(set->ctx, type, value, strlen(value), LY_TYPE_OPTS_SCHEMA,
3269 lys_resolve_prefix, (void *)type->dflt_mod, LYD_XML, NULL, NULL, NULL, NULL, &err);
3270
3271 if (err) {
3272 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type (%s).", value, err->msg);
3273 ly_err_free(err);
3274 } else if (rc != LY_SUCCESS) {
3275 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type.", value);
3276 }
3277 if (rc != LY_SUCCESS) {
3278 LOGWRN(set->ctx, "Previous warning generated by XPath subexpression[%u] \"%.*s\".", exp->tok_pos[equal_exp],
3279 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
3280 exp->expr + exp->tok_pos[equal_exp]);
3281 }
3282 }
3283 free(value);
3284 }
3285}
3286
3287/*
3288 * XPath functions
3289 */
3290
3291/**
3292 * @brief Execute the YANG 1.1 bit-is-set(node-set, string) function. Returns LYXP_SET_BOOLEAN
3293 * depending on whether the first node bit value from the second argument is set.
3294 *
3295 * @param[in] args Array of arguments.
3296 * @param[in] arg_count Count of elements in @p args.
3297 * @param[in,out] set Context and result set at the same time.
3298 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003299 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003300 */
3301static LY_ERR
3302xpath_bit_is_set(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3303{
3304 struct lyd_node_term *leaf;
3305 struct lysc_node_leaf *sleaf;
3306 struct lysc_type_bits *bits;
3307 LY_ERR rc = LY_SUCCESS;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003308 LY_ARRAY_SIZE_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003309
3310 if (options & LYXP_SCNODE_ALL) {
3311 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3312 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003313 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3314 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 +02003315 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_BITS)) {
3316 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"bits\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003317 }
3318
3319 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3320 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3321 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 +02003322 } else if (!warn_is_string_type(sleaf->type)) {
3323 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003324 }
3325 }
3326 set_scnode_clear_ctx(set);
3327 return rc;
3328 }
3329
Michal Vaskod3678892020-05-21 10:06:58 +02003330 if (args[0]->type != LYXP_SET_NODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003331 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)");
3332 return LY_EVALID;
3333 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003334 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003335 LY_CHECK_RET(rc);
3336
3337 set_fill_boolean(set, 0);
Michal Vaskod3678892020-05-21 10:06:58 +02003338 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003339 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
3340 if ((leaf->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST))
3341 && (((struct lysc_node_leaf *)leaf->schema)->type->basetype == LY_TYPE_BITS)) {
3342 bits = (struct lysc_type_bits *)((struct lysc_node_leaf *)leaf->schema)->type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003343 LY_ARRAY_FOR(bits->bits, u) {
3344 if (!strcmp(bits->bits[u].name, args[1]->val.str)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003345 set_fill_boolean(set, 1);
3346 break;
3347 }
3348 }
3349 }
3350 }
3351
3352 return LY_SUCCESS;
3353}
3354
3355/**
3356 * @brief Execute the XPath boolean(object) function. Returns LYXP_SET_BOOLEAN
3357 * with the argument converted to boolean.
3358 *
3359 * @param[in] args Array of arguments.
3360 * @param[in] arg_count Count of elements in @p args.
3361 * @param[in,out] set Context and result set at the same time.
3362 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003363 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003364 */
3365static LY_ERR
3366xpath_boolean(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3367{
3368 LY_ERR rc;
3369
3370 if (options & LYXP_SCNODE_ALL) {
3371 set_scnode_clear_ctx(set);
3372 return LY_SUCCESS;
3373 }
3374
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003375 rc = lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003376 LY_CHECK_RET(rc);
3377 set_fill_set(set, args[0]);
3378
3379 return LY_SUCCESS;
3380}
3381
3382/**
3383 * @brief Execute the XPath ceiling(number) function. Returns LYXP_SET_NUMBER
3384 * with the first argument rounded up to the nearest integer.
3385 *
3386 * @param[in] args Array of arguments.
3387 * @param[in] arg_count Count of elements in @p args.
3388 * @param[in,out] set Context and result set at the same time.
3389 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003390 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003391 */
3392static LY_ERR
3393xpath_ceiling(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3394{
3395 struct lysc_node_leaf *sleaf;
3396 LY_ERR rc = LY_SUCCESS;
3397
3398 if (options & LYXP_SCNODE_ALL) {
3399 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3400 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003401 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3402 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 +02003403 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
3404 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003405 }
3406 set_scnode_clear_ctx(set);
3407 return rc;
3408 }
3409
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003410 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003411 LY_CHECK_RET(rc);
3412 if ((long long)args[0]->val.num != args[0]->val.num) {
3413 set_fill_number(set, ((long long)args[0]->val.num) + 1);
3414 } else {
3415 set_fill_number(set, args[0]->val.num);
3416 }
3417
3418 return LY_SUCCESS;
3419}
3420
3421/**
3422 * @brief Execute the XPath concat(string, string, string*) function.
3423 * Returns LYXP_SET_STRING with the concatenation of all the arguments.
3424 *
3425 * @param[in] args Array of arguments.
3426 * @param[in] arg_count Count of elements in @p args.
3427 * @param[in,out] set Context and result set at the same time.
3428 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003429 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003430 */
3431static LY_ERR
3432xpath_concat(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
3433{
3434 uint16_t i;
3435 char *str = NULL;
3436 size_t used = 1;
3437 LY_ERR rc = LY_SUCCESS;
3438 struct lysc_node_leaf *sleaf;
3439
3440 if (options & LYXP_SCNODE_ALL) {
3441 for (i = 0; i < arg_count; ++i) {
3442 if ((args[i]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[i]))) {
3443 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3444 LOGWRN(set->ctx, "Argument #%u of %s is a %s node \"%s\".",
3445 i + 1, __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003446 } else if (!warn_is_string_type(sleaf->type)) {
3447 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 +02003448 }
3449 }
3450 }
3451 set_scnode_clear_ctx(set);
3452 return rc;
3453 }
3454
3455 for (i = 0; i < arg_count; ++i) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003456 rc = lyxp_set_cast(args[i], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003457 if (rc != LY_SUCCESS) {
3458 free(str);
3459 return rc;
3460 }
3461
3462 str = ly_realloc(str, (used + strlen(args[i]->val.str)) * sizeof(char));
3463 LY_CHECK_ERR_RET(!str, LOGMEM(set->ctx), LY_EMEM);
3464 strcpy(str + used - 1, args[i]->val.str);
3465 used += strlen(args[i]->val.str);
3466 }
3467
3468 /* free, kind of */
Michal Vaskod3678892020-05-21 10:06:58 +02003469 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003470 set->type = LYXP_SET_STRING;
3471 set->val.str = str;
3472
3473 return LY_SUCCESS;
3474}
3475
3476/**
3477 * @brief Execute the XPath contains(string, string) function.
3478 * Returns LYXP_SET_BOOLEAN whether the second argument can
3479 * be found in the first or not.
3480 *
3481 * @param[in] args Array of arguments.
3482 * @param[in] arg_count Count of elements in @p args.
3483 * @param[in,out] set Context and result set at the same time.
3484 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003485 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003486 */
3487static LY_ERR
3488xpath_contains(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3489{
3490 struct lysc_node_leaf *sleaf;
3491 LY_ERR rc = LY_SUCCESS;
3492
3493 if (options & LYXP_SCNODE_ALL) {
3494 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3495 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3496 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 +02003497 } else if (!warn_is_string_type(sleaf->type)) {
3498 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003499 }
3500 }
3501
3502 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3503 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3504 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 +02003505 } else if (!warn_is_string_type(sleaf->type)) {
3506 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003507 }
3508 }
3509 set_scnode_clear_ctx(set);
3510 return rc;
3511 }
3512
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003513 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003514 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003515 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003516 LY_CHECK_RET(rc);
3517
3518 if (strstr(args[0]->val.str, args[1]->val.str)) {
3519 set_fill_boolean(set, 1);
3520 } else {
3521 set_fill_boolean(set, 0);
3522 }
3523
3524 return LY_SUCCESS;
3525}
3526
3527/**
3528 * @brief Execute the XPath count(node-set) function. Returns LYXP_SET_NUMBER
3529 * with the size of the node-set from the argument.
3530 *
3531 * @param[in] args Array of arguments.
3532 * @param[in] arg_count Count of elements in @p args.
3533 * @param[in,out] set Context and result set at the same time.
3534 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003535 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003536 */
3537static LY_ERR
3538xpath_count(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3539{
3540 struct lysc_node *scnode = NULL;
3541 LY_ERR rc = LY_SUCCESS;
3542
3543 if (options & LYXP_SCNODE_ALL) {
3544 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(scnode = warn_get_scnode_in_ctx(args[0]))) {
3545 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003546 }
3547 set_scnode_clear_ctx(set);
3548 return rc;
3549 }
3550
Michal Vasko03ff5a72019-09-11 13:49:33 +02003551 if (args[0]->type != LYXP_SET_NODE_SET) {
3552 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "count(node-set)");
3553 return LY_EVALID;
3554 }
3555
3556 set_fill_number(set, args[0]->used);
3557 return LY_SUCCESS;
3558}
3559
3560/**
3561 * @brief Execute the XPath current() function. Returns LYXP_SET_NODE_SET
3562 * with the context with the intial node.
3563 *
3564 * @param[in] args Array of arguments.
3565 * @param[in] arg_count Count of elements in @p args.
3566 * @param[in,out] set Context and result set at the same time.
3567 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003568 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003569 */
3570static LY_ERR
3571xpath_current(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
3572{
3573 if (arg_count || args) {
3574 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGCOUNT, arg_count, "current()");
3575 return LY_EVALID;
3576 }
3577
3578 if (options & LYXP_SCNODE_ALL) {
3579 set_scnode_clear_ctx(set);
3580
Michal Vaskoecd62de2019-11-13 12:35:11 +01003581 lyxp_set_scnode_insert_node(set, set->ctx_scnode, LYXP_NODE_ELEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003582 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02003583 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003584
3585 /* position is filled later */
3586 set_insert_node(set, set->ctx_node, 0, LYXP_NODE_ELEM, 0);
3587 }
3588
3589 return LY_SUCCESS;
3590}
3591
3592/**
3593 * @brief Execute the YANG 1.1 deref(node-set) function. Returns LYXP_SET_NODE_SET with either
3594 * leafref or instance-identifier target node(s).
3595 *
3596 * @param[in] args Array of arguments.
3597 * @param[in] arg_count Count of elements in @p args.
3598 * @param[in,out] set Context and result set at the same time.
3599 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003600 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003601 */
3602static LY_ERR
3603xpath_deref(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3604{
3605 struct lyd_node_term *leaf;
Michal Vasko42e497c2020-01-06 08:38:25 +01003606 struct lysc_node_leaf *sleaf = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02003607 struct lysc_type_leafref *lref;
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003608 const struct lysc_node *target;
Michal Vasko004d3152020-06-11 19:59:22 +02003609 struct ly_path *p;
3610 struct lyd_node *node;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003611 char *errmsg = NULL;
3612 const char *val;
3613 int dynamic;
Michal Vasko00cbf532020-06-15 13:58:47 +02003614 uint8_t oper;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003615 LY_ERR rc = LY_SUCCESS;
3616
3617 if (options & LYXP_SCNODE_ALL) {
3618 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3619 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003620 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3621 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 +02003622 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_LEAFREF) && !warn_is_specific_type(sleaf->type, LY_TYPE_INST)) {
3623 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"leafref\" nor \"instance-identifier\".",
3624 __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003625 }
3626 set_scnode_clear_ctx(set);
Michal Vasko42e497c2020-01-06 08:38:25 +01003627 if (sleaf && (sleaf->type->basetype == LY_TYPE_LEAFREF)) {
Michal Vasko004d3152020-06-11 19:59:22 +02003628 lref = (struct lysc_type_leafref *)sleaf->type;
Michal Vasko00cbf532020-06-15 13:58:47 +02003629 oper = lysc_is_output((struct lysc_node *)sleaf) ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT;
Michal Vasko004d3152020-06-11 19:59:22 +02003630
3631 /* it was already evaluated on schema, it must succeed */
3632 if (set->format == LYD_JSON) {
Michal Vasko00cbf532020-06-15 13:58:47 +02003633 rc = ly_path_compile(set->ctx, sleaf->module, (struct lysc_node *)sleaf, lref->path, LY_PATH_LREF_TRUE,
3634 oper, LY_PATH_TARGET_MANY, lydjson_resolve_prefix, NULL, LYD_JSON, &p);
Michal Vasko004d3152020-06-11 19:59:22 +02003635 } else {
3636 assert(set->format == LYD_SCHEMA);
Michal Vasko00cbf532020-06-15 13:58:47 +02003637 rc = ly_path_compile(set->ctx, sleaf->module, (struct lysc_node *)sleaf, lref->path, LY_PATH_LREF_TRUE,
3638 oper, LY_PATH_TARGET_MANY, lys_resolve_prefix, lref->path_context, LYD_SCHEMA, &p);
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003639 }
Michal Vasko004d3152020-06-11 19:59:22 +02003640 assert(!rc);
3641
3642 /* get the target node */
3643 target = p[LY_ARRAY_SIZE(p) - 1].node;
3644 ly_path_free(set->ctx, p);
3645
3646 lyxp_set_scnode_insert_node(set, target, LYXP_NODE_ELEM);
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003647 }
3648
Michal Vasko03ff5a72019-09-11 13:49:33 +02003649 return rc;
3650 }
3651
Michal Vaskod3678892020-05-21 10:06:58 +02003652 if (args[0]->type != LYXP_SET_NODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003653 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "deref(node-set)");
3654 return LY_EVALID;
3655 }
3656
Michal Vaskod3678892020-05-21 10:06:58 +02003657 lyxp_set_free_content(set);
3658 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003659 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
3660 sleaf = (struct lysc_node_leaf *)leaf->schema;
3661 if (sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
3662 if (sleaf->type->basetype == LY_TYPE_LEAFREF) {
3663 /* find leafref target */
Michal Vasko004d3152020-06-11 19:59:22 +02003664 if (ly_type_find_leafref((struct lysc_type_leafref *)sleaf->type, (struct lyd_node *)leaf,
3665 &leaf->value, set->tree, &node, &errmsg)) {
3666 LOGERR(set->ctx, LY_EVALID, errmsg);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003667 free(errmsg);
Michal Vasko004d3152020-06-11 19:59:22 +02003668 return LY_EVALID;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003669 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003670 } else {
3671 assert(sleaf->type->basetype == LY_TYPE_INST);
Michal Vasko004d3152020-06-11 19:59:22 +02003672 if (ly_path_eval(leaf->value.target, set->tree, &node)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003673 val = lyd_value2str(leaf, &dynamic);
3674 LOGERR(set->ctx, LY_EVALID, "Invalid instance-identifier \"%s\" value - required instance not found.", val);
3675 if (dynamic) {
3676 free((char *)val);
3677 }
3678 return LY_EVALID;
3679 }
3680 }
Michal Vasko004d3152020-06-11 19:59:22 +02003681
3682 /* insert it */
3683 set_insert_node(set, node, 0, LYXP_NODE_ELEM, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003684 }
3685 }
3686
3687 return LY_SUCCESS;
3688}
3689
3690/**
3691 * @brief Execute the YANG 1.1 derived-from(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
3692 * on whether the first argument nodes contain a node of an identity derived from the second
3693 * argument identity.
3694 *
3695 * @param[in] args Array of arguments.
3696 * @param[in] arg_count Count of elements in @p args.
3697 * @param[in,out] set Context and result set at the same time.
3698 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003699 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003700 */
3701static LY_ERR
3702xpath_derived_from(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3703{
3704 uint16_t i;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003705 LY_ARRAY_SIZE_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003706 struct lyd_node_term *leaf;
3707 struct lysc_node_leaf *sleaf;
3708 struct lyd_value data = {0};
3709 struct ly_err_item *err = NULL;
3710 LY_ERR rc = LY_SUCCESS;
3711 int found;
3712
3713 if (options & LYXP_SCNODE_ALL) {
3714 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3715 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003716 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3717 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 +02003718 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_IDENT)) {
3719 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"identityref\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003720 }
3721
3722 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3723 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3724 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 +02003725 } else if (!warn_is_string_type(sleaf->type)) {
3726 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003727 }
3728 }
3729 set_scnode_clear_ctx(set);
3730 return rc;
3731 }
3732
Michal Vaskod3678892020-05-21 10:06:58 +02003733 if (args[0]->type != LYXP_SET_NODE_SET) {
3734 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
3735 "derived-from(node-set, string)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003736 return LY_EVALID;
3737 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003738 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003739 LY_CHECK_RET(rc);
3740
3741 set_fill_boolean(set, 0);
Michal Vaskod3678892020-05-21 10:06:58 +02003742 found = 0;
3743 for (i = 0; i < args[0]->used; ++i) {
3744 leaf = (struct lyd_node_term *)args[0]->val.nodes[i].node;
3745 sleaf = (struct lysc_node_leaf *)leaf->schema;
3746 if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_IDENT)) {
3747 /* store args[1] into ident */
3748 rc = sleaf->type->plugin->store(set->ctx, sleaf->type, args[1]->val.str, strlen(args[1]->val.str),
3749 LY_TYPE_OPTS_STORE, lys_resolve_prefix, (void *)sleaf->dflt_mod, set->format,
3750 (struct lyd_node *)leaf, set->tree, &data, NULL, &err);
3751 if (err) {
3752 ly_err_print(err);
3753 ly_err_free(err);
3754 }
3755 LY_CHECK_RET(rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003756
Michal Vaskod3678892020-05-21 10:06:58 +02003757 LY_ARRAY_FOR(data.ident->derived, u) {
3758 if (data.ident->derived[u] == leaf->value.ident) {
3759 set_fill_boolean(set, 1);
3760 found = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003761 break;
3762 }
3763 }
Michal Vaskod3678892020-05-21 10:06:58 +02003764 if (found) {
3765 break;
3766 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003767 }
3768 }
3769
3770 return LY_SUCCESS;
3771}
3772
3773/**
3774 * @brief Execute the YANG 1.1 derived-from-or-self(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
3775 * on whether the first argument nodes contain a node of an identity that either is or is derived from
3776 * the second argument identity.
3777 *
3778 * @param[in] args Array of arguments.
3779 * @param[in] arg_count Count of elements in @p args.
3780 * @param[in,out] set Context and result set at the same time.
3781 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003782 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003783 */
3784static LY_ERR
3785xpath_derived_from_or_self(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3786{
3787 uint16_t i;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003788 LY_ARRAY_SIZE_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003789 struct lyd_node_term *leaf;
3790 struct lysc_node_leaf *sleaf;
3791 struct lyd_value data = {0};
3792 struct ly_err_item *err = NULL;
3793 LY_ERR rc = LY_SUCCESS;
3794 int found;
3795
3796 if (options & LYXP_SCNODE_ALL) {
3797 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3798 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003799 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3800 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 +02003801 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_IDENT)) {
3802 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"identityref\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003803 }
3804
3805 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3806 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3807 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 +02003808 } else if (!warn_is_string_type(sleaf->type)) {
3809 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003810 }
3811 }
3812 set_scnode_clear_ctx(set);
3813 return rc;
3814 }
3815
Michal Vaskod3678892020-05-21 10:06:58 +02003816 if (args[0]->type != LYXP_SET_NODE_SET) {
3817 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
3818 "derived-from-or-self(node-set, string)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003819 return LY_EVALID;
3820 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003821 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003822 LY_CHECK_RET(rc);
3823
3824 set_fill_boolean(set, 0);
Michal Vaskod3678892020-05-21 10:06:58 +02003825 found = 0;
3826 for (i = 0; i < args[0]->used; ++i) {
3827 leaf = (struct lyd_node_term *)args[0]->val.nodes[i].node;
3828 sleaf = (struct lysc_node_leaf *)leaf->schema;
3829 if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_IDENT)) {
3830 /* store args[1] into ident */
3831 rc = sleaf->type->plugin->store(set->ctx, sleaf->type, args[1]->val.str, strlen(args[1]->val.str),
3832 LY_TYPE_OPTS_STORE, lys_resolve_prefix, (void *)sleaf->dflt_mod, set->format,
3833 (struct lyd_node *)leaf, set->tree, &data, NULL, &err);
3834 if (err) {
3835 ly_err_print(err);
3836 ly_err_free(err);
3837 }
3838 LY_CHECK_RET(rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003839
Michal Vaskod3678892020-05-21 10:06:58 +02003840 if (data.ident == leaf->value.ident) {
3841 set_fill_boolean(set, 1);
3842 break;
3843 }
3844 LY_ARRAY_FOR(data.ident->derived, u) {
3845 if (data.ident->derived[u] == leaf->value.ident) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003846 set_fill_boolean(set, 1);
Michal Vaskod3678892020-05-21 10:06:58 +02003847 found = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003848 break;
3849 }
Michal Vaskod3678892020-05-21 10:06:58 +02003850 }
3851 if (found) {
3852 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003853 }
3854 }
3855 }
3856
3857 return LY_SUCCESS;
3858}
3859
3860/**
3861 * @brief Execute the YANG 1.1 enum-value(node-set) function. Returns LYXP_SET_NUMBER
3862 * with the integer value of the first node's enum value, otherwise NaN.
3863 *
3864 * @param[in] args Array of arguments.
3865 * @param[in] arg_count Count of elements in @p args.
3866 * @param[in,out] set Context and result set at the same time.
3867 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003868 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003869 */
3870static LY_ERR
3871xpath_enum_value(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3872{
3873 struct lyd_node_term *leaf;
3874 struct lysc_node_leaf *sleaf;
3875 LY_ERR rc = LY_SUCCESS;
3876
3877 if (options & LYXP_SCNODE_ALL) {
3878 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3879 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003880 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3881 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 +02003882 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_ENUM)) {
3883 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"enumeration\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003884 }
3885 set_scnode_clear_ctx(set);
3886 return rc;
3887 }
3888
Michal Vaskod3678892020-05-21 10:06:58 +02003889 if (args[0]->type != LYXP_SET_NODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003890 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "enum-value(node-set)");
3891 return LY_EVALID;
3892 }
3893
3894 set_fill_number(set, NAN);
Michal Vaskod3678892020-05-21 10:06:58 +02003895 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003896 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
3897 sleaf = (struct lysc_node_leaf *)leaf->schema;
3898 if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_ENUM)) {
3899 set_fill_number(set, leaf->value.enum_item->value);
3900 }
3901 }
3902
3903 return LY_SUCCESS;
3904}
3905
3906/**
3907 * @brief Execute the XPath false() function. Returns LYXP_SET_BOOLEAN
3908 * with false value.
3909 *
3910 * @param[in] args Array of arguments.
3911 * @param[in] arg_count Count of elements in @p args.
3912 * @param[in,out] set Context and result set at the same time.
3913 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003914 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003915 */
3916static LY_ERR
3917xpath_false(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3918{
3919 if (options & LYXP_SCNODE_ALL) {
3920 set_scnode_clear_ctx(set);
3921 return LY_SUCCESS;
3922 }
3923
3924 set_fill_boolean(set, 0);
3925 return LY_SUCCESS;
3926}
3927
3928/**
3929 * @brief Execute the XPath floor(number) function. Returns LYXP_SET_NUMBER
3930 * with the first argument floored (truncated).
3931 *
3932 * @param[in] args Array of arguments.
3933 * @param[in] arg_count Count of elements in @p args.
3934 * @param[in,out] set Context and result set at the same time.
3935 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003936 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003937 */
3938static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003939xpath_floor(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int UNUSED(options))
Michal Vasko03ff5a72019-09-11 13:49:33 +02003940{
3941 LY_ERR rc;
3942
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003943 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003944 LY_CHECK_RET(rc);
3945 if (isfinite(args[0]->val.num)) {
3946 set_fill_number(set, (long long)args[0]->val.num);
3947 }
3948
3949 return LY_SUCCESS;
3950}
3951
3952/**
3953 * @brief Execute the XPath lang(string) function. Returns LYXP_SET_BOOLEAN
3954 * whether the language of the text matches the one from the argument.
3955 *
3956 * @param[in] args Array of arguments.
3957 * @param[in] arg_count Count of elements in @p args.
3958 * @param[in,out] set Context and result set at the same time.
3959 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003960 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003961 */
3962static LY_ERR
3963xpath_lang(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3964{
3965 const struct lyd_node *node;
3966 struct lysc_node_leaf *sleaf;
Michal Vasko9f96a052020-03-10 09:41:45 +01003967 struct lyd_meta *meta = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003968 const char *val;
3969 int i, dynamic;
3970 LY_ERR rc = LY_SUCCESS;
3971
3972 if (options & LYXP_SCNODE_ALL) {
3973 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3974 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3975 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 +02003976 } else if (!warn_is_string_type(sleaf->type)) {
3977 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003978 }
3979 }
3980 set_scnode_clear_ctx(set);
3981 return rc;
3982 }
3983
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003984 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003985 LY_CHECK_RET(rc);
3986
Michal Vasko03ff5a72019-09-11 13:49:33 +02003987 if (set->type != LYXP_SET_NODE_SET) {
3988 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "lang(string)");
3989 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02003990 } else if (!set->used) {
3991 set_fill_boolean(set, 0);
3992 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003993 }
3994
3995 switch (set->val.nodes[0].type) {
3996 case LYXP_NODE_ELEM:
3997 case LYXP_NODE_TEXT:
3998 node = set->val.nodes[0].node;
3999 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004000 case LYXP_NODE_META:
4001 node = set->val.meta[0].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004002 break;
4003 default:
4004 /* nothing to do with roots */
4005 set_fill_boolean(set, 0);
4006 return LY_SUCCESS;
4007 }
4008
Michal Vasko9f96a052020-03-10 09:41:45 +01004009 /* find lang metadata */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004010 for (; node; node = (struct lyd_node *)node->parent) {
Michal Vasko9f96a052020-03-10 09:41:45 +01004011 for (meta = node->meta; meta; meta = meta->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004012 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004013 if (meta->name && !strcmp(meta->name, "lang") && !strcmp(meta->annotation->module->name, "xml")) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004014 break;
4015 }
4016 }
4017
Michal Vasko9f96a052020-03-10 09:41:45 +01004018 if (meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004019 break;
4020 }
4021 }
4022
4023 /* compare languages */
Michal Vasko9f96a052020-03-10 09:41:45 +01004024 if (!meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004025 set_fill_boolean(set, 0);
4026 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01004027 val = lyd_meta2str(meta, &dynamic);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004028 for (i = 0; args[0]->val.str[i]; ++i) {
4029 if (tolower(args[0]->val.str[i]) != tolower(val[i])) {
4030 set_fill_boolean(set, 0);
4031 break;
4032 }
4033 }
4034 if (!args[0]->val.str[i]) {
4035 if (!val[i] || (val[i] == '-')) {
4036 set_fill_boolean(set, 1);
4037 } else {
4038 set_fill_boolean(set, 0);
4039 }
4040 }
4041 if (dynamic) {
4042 free((char *)val);
4043 }
4044 }
4045
4046 return LY_SUCCESS;
4047}
4048
4049/**
4050 * @brief Execute the XPath last() function. Returns LYXP_SET_NUMBER
4051 * with the context size.
4052 *
4053 * @param[in] args Array of arguments.
4054 * @param[in] arg_count Count of elements in @p args.
4055 * @param[in,out] set Context and result set at the same time.
4056 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004057 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004058 */
4059static LY_ERR
4060xpath_last(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4061{
4062 if (options & LYXP_SCNODE_ALL) {
4063 set_scnode_clear_ctx(set);
4064 return LY_SUCCESS;
4065 }
4066
Michal Vasko03ff5a72019-09-11 13:49:33 +02004067 if (set->type != LYXP_SET_NODE_SET) {
4068 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "last()");
4069 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004070 } else if (!set->used) {
4071 set_fill_number(set, 0);
4072 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004073 }
4074
4075 set_fill_number(set, set->ctx_size);
4076 return LY_SUCCESS;
4077}
4078
4079/**
4080 * @brief Execute the XPath local-name(node-set?) function. Returns LYXP_SET_STRING
4081 * with the node name without namespace from the argument or the context.
4082 *
4083 * @param[in] args Array of arguments.
4084 * @param[in] arg_count Count of elements in @p args.
4085 * @param[in,out] set Context and result set at the same time.
4086 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004087 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004088 */
4089static LY_ERR
4090xpath_local_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
4091{
4092 struct lyxp_set_node *item;
4093 /* suppress unused variable warning */
4094 (void)options;
4095
4096 if (options & LYXP_SCNODE_ALL) {
4097 set_scnode_clear_ctx(set);
4098 return LY_SUCCESS;
4099 }
4100
4101 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004102 if (args[0]->type != LYXP_SET_NODE_SET) {
4103 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "local-name(node-set?)");
4104 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004105 } else if (!args[0]->used) {
4106 set_fill_string(set, "", 0);
4107 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004108 }
4109
4110 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004111 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004112
4113 item = &args[0]->val.nodes[0];
4114 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004115 if (set->type != LYXP_SET_NODE_SET) {
4116 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "local-name(node-set?)");
4117 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004118 } else if (!set->used) {
4119 set_fill_string(set, "", 0);
4120 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004121 }
4122
4123 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004124 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004125
4126 item = &set->val.nodes[0];
4127 }
4128
4129 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004130 case LYXP_NODE_NONE:
4131 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004132 case LYXP_NODE_ROOT:
4133 case LYXP_NODE_ROOT_CONFIG:
4134 case LYXP_NODE_TEXT:
4135 set_fill_string(set, "", 0);
4136 break;
4137 case LYXP_NODE_ELEM:
4138 set_fill_string(set, item->node->schema->name, strlen(item->node->schema->name));
4139 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004140 case LYXP_NODE_META:
4141 set_fill_string(set, ((struct lyd_meta *)item->node)->name, strlen(((struct lyd_meta *)item->node)->name));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004142 break;
4143 }
4144
4145 return LY_SUCCESS;
4146}
4147
4148/**
4149 * @brief Execute the XPath name(node-set?) function. Returns LYXP_SET_STRING
4150 * with the node name fully qualified (with namespace) from the argument or the context.
4151 *
4152 * @param[in] args Array of arguments.
4153 * @param[in] arg_count Count of elements in @p args.
4154 * @param[in,out] set Context and result set at the same time.
4155 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004156 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004157 */
4158static LY_ERR
4159xpath_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
4160{
4161 struct lyxp_set_node *item;
4162 struct lys_module *mod;
4163 char *str;
4164 const char *name;
4165 int rc;
4166
4167 if (options & LYXP_SCNODE_ALL) {
4168 set_scnode_clear_ctx(set);
4169 return LY_SUCCESS;
4170 }
4171
4172 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004173 if (args[0]->type != LYXP_SET_NODE_SET) {
4174 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "name(node-set?)");
4175 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004176 } else if (!args[0]->used) {
4177 set_fill_string(set, "", 0);
4178 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004179 }
4180
4181 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004182 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004183
4184 item = &args[0]->val.nodes[0];
4185 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004186 if (set->type != LYXP_SET_NODE_SET) {
4187 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "name(node-set?)");
4188 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004189 } else if (!set->used) {
4190 set_fill_string(set, "", 0);
4191 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004192 }
4193
4194 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004195 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004196
4197 item = &set->val.nodes[0];
4198 }
4199
4200 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004201 case LYXP_NODE_NONE:
4202 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004203 case LYXP_NODE_ROOT:
4204 case LYXP_NODE_ROOT_CONFIG:
4205 case LYXP_NODE_TEXT:
4206 mod = NULL;
4207 name = NULL;
4208 break;
4209 case LYXP_NODE_ELEM:
4210 mod = item->node->schema->module;
4211 name = item->node->schema->name;
4212 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004213 case LYXP_NODE_META:
4214 mod = ((struct lyd_meta *)item->node)->annotation->module;
4215 name = ((struct lyd_meta *)item->node)->name;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004216 break;
4217 }
4218
4219 if (mod && name) {
4220 switch (set->format) {
Michal Vasko52927e22020-03-16 17:26:14 +01004221 case LYD_SCHEMA:
Michal Vasko03ff5a72019-09-11 13:49:33 +02004222 rc = asprintf(&str, "%s:%s", lys_prefix_find_module(set->local_mod, mod), name);
4223 break;
4224 case LYD_JSON:
4225 rc = asprintf(&str, "%s:%s", mod->name, name);
4226 break;
Michal Vasko52927e22020-03-16 17:26:14 +01004227 case LYD_XML:
Michal Vasko9409ef62019-09-12 11:47:17 +02004228 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004229 }
4230 LY_CHECK_ERR_RET(rc == -1, LOGMEM(set->ctx), LY_EMEM);
4231 set_fill_string(set, str, strlen(str));
4232 free(str);
4233 } else {
4234 set_fill_string(set, "", 0);
4235 }
4236
4237 return LY_SUCCESS;
4238}
4239
4240/**
4241 * @brief Execute the XPath namespace-uri(node-set?) function. Returns LYXP_SET_STRING
4242 * with the namespace of the node from the argument or the context.
4243 *
4244 * @param[in] args Array of arguments.
4245 * @param[in] arg_count Count of elements in @p args.
4246 * @param[in,out] set Context and result set at the same time.
4247 * @param[in] options XPath options.
4248 * @return LY_ERR (LY_EINVAL for wrong arguments on schema)
4249 */
4250static LY_ERR
4251xpath_namespace_uri(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
4252{
4253 struct lyxp_set_node *item;
4254 struct lys_module *mod;
4255 /* suppress unused variable warning */
4256 (void)options;
4257
4258 if (options & LYXP_SCNODE_ALL) {
4259 set_scnode_clear_ctx(set);
4260 return LY_SUCCESS;
4261 }
4262
4263 if (arg_count) {
Michal Vaskod3678892020-05-21 10:06:58 +02004264 if (args[0]->type != LYXP_SET_NODE_SET) {
4265 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
4266 "namespace-uri(node-set?)");
4267 return LY_EVALID;
4268 } else if (!args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004269 set_fill_string(set, "", 0);
4270 return LY_SUCCESS;
4271 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004272
4273 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004274 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004275
4276 item = &args[0]->val.nodes[0];
4277 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004278 if (set->type != LYXP_SET_NODE_SET) {
4279 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "namespace-uri(node-set?)");
4280 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004281 } else if (!set->used) {
4282 set_fill_string(set, "", 0);
4283 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004284 }
4285
4286 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004287 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004288
4289 item = &set->val.nodes[0];
4290 }
4291
4292 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004293 case LYXP_NODE_NONE:
4294 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004295 case LYXP_NODE_ROOT:
4296 case LYXP_NODE_ROOT_CONFIG:
4297 case LYXP_NODE_TEXT:
4298 set_fill_string(set, "", 0);
4299 break;
4300 case LYXP_NODE_ELEM:
Michal Vasko9f96a052020-03-10 09:41:45 +01004301 case LYXP_NODE_META:
Michal Vasko03ff5a72019-09-11 13:49:33 +02004302 if (item->type == LYXP_NODE_ELEM) {
4303 mod = item->node->schema->module;
Michal Vasko9f96a052020-03-10 09:41:45 +01004304 } else { /* LYXP_NODE_META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004305 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004306 mod = ((struct lyd_meta *)item->node)->annotation->module;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004307 }
4308
4309 set_fill_string(set, mod->ns, strlen(mod->ns));
4310 break;
4311 }
4312
4313 return LY_SUCCESS;
4314}
4315
4316/**
4317 * @brief Execute the XPath node() function (node type). Returns LYXP_SET_NODE_SET
4318 * with only nodes from the context. In practice it either leaves the context
4319 * as it is or returns an empty node set.
4320 *
4321 * @param[in] args Array of arguments.
4322 * @param[in] arg_count Count of elements in @p args.
4323 * @param[in,out] set Context and result set at the same time.
4324 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004325 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004326 */
4327static LY_ERR
4328xpath_node(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4329{
4330 if (options & LYXP_SCNODE_ALL) {
4331 set_scnode_clear_ctx(set);
4332 return LY_SUCCESS;
4333 }
4334
4335 if (set->type != LYXP_SET_NODE_SET) {
Michal Vaskod3678892020-05-21 10:06:58 +02004336 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004337 }
4338 return LY_SUCCESS;
4339}
4340
4341/**
4342 * @brief Execute the XPath normalize-space(string?) function. Returns LYXP_SET_STRING
4343 * with normalized value (no leading, trailing, double white spaces) of the node
4344 * from the argument or the context.
4345 *
4346 * @param[in] args Array of arguments.
4347 * @param[in] arg_count Count of elements in @p args.
4348 * @param[in,out] set Context and result set at the same time.
4349 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004350 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004351 */
4352static LY_ERR
4353xpath_normalize_space(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
4354{
4355 uint16_t i, new_used;
4356 char *new;
4357 int have_spaces = 0, space_before = 0;
4358 struct lysc_node_leaf *sleaf;
4359 LY_ERR rc = LY_SUCCESS;
4360
4361 if (options & LYXP_SCNODE_ALL) {
4362 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4363 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4364 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 +02004365 } else if (!warn_is_string_type(sleaf->type)) {
4366 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004367 }
4368 }
4369 set_scnode_clear_ctx(set);
4370 return rc;
4371 }
4372
4373 if (arg_count) {
4374 set_fill_set(set, args[0]);
4375 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004376 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004377 LY_CHECK_RET(rc);
4378
4379 /* is there any normalization necessary? */
4380 for (i = 0; set->val.str[i]; ++i) {
4381 if (is_xmlws(set->val.str[i])) {
4382 if ((i == 0) || space_before || (!set->val.str[i + 1])) {
4383 have_spaces = 1;
4384 break;
4385 }
4386 space_before = 1;
4387 } else {
4388 space_before = 0;
4389 }
4390 }
4391
4392 /* yep, there is */
4393 if (have_spaces) {
4394 /* it's enough, at least one character will go, makes space for ending '\0' */
4395 new = malloc(strlen(set->val.str) * sizeof(char));
4396 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4397 new_used = 0;
4398
4399 space_before = 0;
4400 for (i = 0; set->val.str[i]; ++i) {
4401 if (is_xmlws(set->val.str[i])) {
4402 if ((i == 0) || space_before) {
4403 space_before = 1;
4404 continue;
4405 } else {
4406 space_before = 1;
4407 }
4408 } else {
4409 space_before = 0;
4410 }
4411
4412 new[new_used] = (space_before ? ' ' : set->val.str[i]);
4413 ++new_used;
4414 }
4415
4416 /* at worst there is one trailing space now */
4417 if (new_used && is_xmlws(new[new_used - 1])) {
4418 --new_used;
4419 }
4420
4421 new = ly_realloc(new, (new_used + 1) * sizeof(char));
4422 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4423 new[new_used] = '\0';
4424
4425 free(set->val.str);
4426 set->val.str = new;
4427 }
4428
4429 return LY_SUCCESS;
4430}
4431
4432/**
4433 * @brief Execute the XPath not(boolean) function. Returns LYXP_SET_BOOLEAN
4434 * with the argument converted to boolean and logically inverted.
4435 *
4436 * @param[in] args Array of arguments.
4437 * @param[in] arg_count Count of elements in @p args.
4438 * @param[in,out] set Context and result set at the same time.
4439 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004440 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004441 */
4442static LY_ERR
4443xpath_not(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4444{
4445 if (options & LYXP_SCNODE_ALL) {
4446 set_scnode_clear_ctx(set);
4447 return LY_SUCCESS;
4448 }
4449
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004450 lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02004451 if (args[0]->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004452 set_fill_boolean(set, 0);
4453 } else {
4454 set_fill_boolean(set, 1);
4455 }
4456
4457 return LY_SUCCESS;
4458}
4459
4460/**
4461 * @brief Execute the XPath number(object?) function. Returns LYXP_SET_NUMBER
4462 * with the number representation of either the argument or the context.
4463 *
4464 * @param[in] args Array of arguments.
4465 * @param[in] arg_count Count of elements in @p args.
4466 * @param[in,out] set Context and result set at the same time.
4467 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004468 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004469 */
4470static LY_ERR
4471xpath_number(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
4472{
4473 LY_ERR rc;
4474
4475 if (options & LYXP_SCNODE_ALL) {
4476 set_scnode_clear_ctx(set);
4477 return LY_SUCCESS;
4478 }
4479
4480 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004481 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004482 LY_CHECK_RET(rc);
4483 set_fill_set(set, args[0]);
4484 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004485 rc = lyxp_set_cast(set, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004486 LY_CHECK_RET(rc);
4487 }
4488
4489 return LY_SUCCESS;
4490}
4491
4492/**
4493 * @brief Execute the XPath position() function. Returns LYXP_SET_NUMBER
4494 * with the context position.
4495 *
4496 * @param[in] args Array of arguments.
4497 * @param[in] arg_count Count of elements in @p args.
4498 * @param[in,out] set Context and result set at the same time.
4499 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004500 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004501 */
4502static LY_ERR
4503xpath_position(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4504{
4505 if (options & LYXP_SCNODE_ALL) {
4506 set_scnode_clear_ctx(set);
4507 return LY_SUCCESS;
4508 }
4509
Michal Vasko03ff5a72019-09-11 13:49:33 +02004510 if (set->type != LYXP_SET_NODE_SET) {
4511 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "position()");
4512 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004513 } else if (!set->used) {
4514 set_fill_number(set, 0);
4515 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004516 }
4517
4518 set_fill_number(set, set->ctx_pos);
4519
4520 /* UNUSED in 'Release' build type */
4521 (void)options;
4522 return LY_SUCCESS;
4523}
4524
4525/**
4526 * @brief Execute the YANG 1.1 re-match(string, string) function. Returns LYXP_SET_BOOLEAN
4527 * depending on whether the second argument regex matches the first argument string. For details refer to
4528 * YANG 1.1 RFC section 10.2.1.
4529 *
4530 * @param[in] args Array of arguments.
4531 * @param[in] arg_count Count of elements in @p args.
4532 * @param[in,out] set Context and result set at the same time.
4533 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004534 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004535 */
4536static LY_ERR
4537xpath_re_match(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4538{
4539 struct lysc_pattern **patterns = NULL, **pattern;
4540 struct lysc_node_leaf *sleaf;
4541 char *path;
4542 LY_ERR rc = LY_SUCCESS;
4543 struct ly_err_item *err;
4544
4545 if (options & LYXP_SCNODE_ALL) {
4546 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4547 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4548 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 +02004549 } else if (!warn_is_string_type(sleaf->type)) {
4550 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004551 }
4552 }
4553
4554 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4555 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4556 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 +02004557 } else if (!warn_is_string_type(sleaf->type)) {
4558 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004559 }
4560 }
4561 set_scnode_clear_ctx(set);
4562 return rc;
4563 }
4564
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004565 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004566 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004567 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004568 LY_CHECK_RET(rc);
4569
4570 LY_ARRAY_NEW_RET(set->ctx, patterns, pattern, LY_EMEM);
4571 *pattern = malloc(sizeof **pattern);
4572 path = lyd_path(set->ctx_node, LYD_PATH_LOG, NULL, 0);
4573 rc = lys_compile_type_pattern_check(set->ctx, path, args[1]->val.str, &(*pattern)->code);
4574 free(path);
4575 if (rc != LY_SUCCESS) {
4576 LY_ARRAY_FREE(patterns);
4577 return rc;
4578 }
4579
4580 rc = ly_type_validate_patterns(patterns, args[0]->val.str, strlen(args[0]->val.str), &err);
4581 pcre2_code_free((*pattern)->code);
4582 free(*pattern);
4583 LY_ARRAY_FREE(patterns);
4584 if (rc && (rc != LY_EVALID)) {
4585 ly_err_print(err);
4586 ly_err_free(err);
4587 return rc;
4588 }
4589
4590 if (rc == LY_EVALID) {
4591 ly_err_free(err);
4592 set_fill_boolean(set, 0);
4593 } else {
4594 set_fill_boolean(set, 1);
4595 }
4596
4597 return LY_SUCCESS;
4598}
4599
4600/**
4601 * @brief Execute the XPath round(number) function. Returns LYXP_SET_NUMBER
4602 * with the rounded first argument. For details refer to
4603 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-round.
4604 *
4605 * @param[in] args Array of arguments.
4606 * @param[in] arg_count Count of elements in @p args.
4607 * @param[in,out] set Context and result set at the same time.
4608 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004609 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004610 */
4611static LY_ERR
4612xpath_round(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4613{
4614 struct lysc_node_leaf *sleaf;
4615 LY_ERR rc = LY_SUCCESS;
4616
4617 if (options & LYXP_SCNODE_ALL) {
4618 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4619 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004620 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4621 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 +02004622 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
4623 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004624 }
4625 set_scnode_clear_ctx(set);
4626 return rc;
4627 }
4628
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004629 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004630 LY_CHECK_RET(rc);
4631
4632 /* cover only the cases where floor can't be used */
4633 if ((args[0]->val.num == -0.0f) || ((args[0]->val.num < 0) && (args[0]->val.num >= -0.5))) {
4634 set_fill_number(set, -0.0f);
4635 } else {
4636 args[0]->val.num += 0.5;
4637 rc = xpath_floor(args, 1, args[0], options);
4638 LY_CHECK_RET(rc);
4639 set_fill_number(set, args[0]->val.num);
4640 }
4641
4642 return LY_SUCCESS;
4643}
4644
4645/**
4646 * @brief Execute the XPath starts-with(string, string) function.
4647 * Returns LYXP_SET_BOOLEAN whether the second argument is
4648 * the prefix of the first or not.
4649 *
4650 * @param[in] args Array of arguments.
4651 * @param[in] arg_count Count of elements in @p args.
4652 * @param[in,out] set Context and result set at the same time.
4653 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004654 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004655 */
4656static LY_ERR
4657xpath_starts_with(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4658{
4659 struct lysc_node_leaf *sleaf;
4660 LY_ERR rc = LY_SUCCESS;
4661
4662 if (options & LYXP_SCNODE_ALL) {
4663 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4664 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4665 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 +02004666 } else if (!warn_is_string_type(sleaf->type)) {
4667 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004668 }
4669 }
4670
4671 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4672 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4673 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 +02004674 } else if (!warn_is_string_type(sleaf->type)) {
4675 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004676 }
4677 }
4678 set_scnode_clear_ctx(set);
4679 return rc;
4680 }
4681
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004682 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004683 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004684 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004685 LY_CHECK_RET(rc);
4686
4687 if (strncmp(args[0]->val.str, args[1]->val.str, strlen(args[1]->val.str))) {
4688 set_fill_boolean(set, 0);
4689 } else {
4690 set_fill_boolean(set, 1);
4691 }
4692
4693 return LY_SUCCESS;
4694}
4695
4696/**
4697 * @brief Execute the XPath string(object?) function. Returns LYXP_SET_STRING
4698 * with the string representation of either the argument or the context.
4699 *
4700 * @param[in] args Array of arguments.
4701 * @param[in] arg_count Count of elements in @p args.
4702 * @param[in,out] set Context and result set at the same time.
4703 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004704 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004705 */
4706static LY_ERR
4707xpath_string(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
4708{
4709 LY_ERR rc;
4710
4711 if (options & LYXP_SCNODE_ALL) {
4712 set_scnode_clear_ctx(set);
4713 return LY_SUCCESS;
4714 }
4715
4716 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004717 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004718 LY_CHECK_RET(rc);
4719 set_fill_set(set, args[0]);
4720 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004721 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004722 LY_CHECK_RET(rc);
4723 }
4724
4725 return LY_SUCCESS;
4726}
4727
4728/**
4729 * @brief Execute the XPath string-length(string?) function. Returns LYXP_SET_NUMBER
4730 * with the length of the string in either the argument or the context.
4731 *
4732 * @param[in] args Array of arguments.
4733 * @param[in] arg_count Count of elements in @p args.
4734 * @param[in,out] set Context and result set at the same time.
4735 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004736 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004737 */
4738static LY_ERR
4739xpath_string_length(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
4740{
4741 struct lysc_node_leaf *sleaf;
4742 LY_ERR rc = LY_SUCCESS;
4743
4744 if (options & LYXP_SCNODE_ALL) {
4745 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4746 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4747 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 +02004748 } else if (!warn_is_string_type(sleaf->type)) {
4749 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004750 }
4751 }
4752 if (!arg_count && (set->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set))) {
4753 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4754 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 +02004755 } else if (!warn_is_string_type(sleaf->type)) {
4756 LOGWRN(set->ctx, "Argument #0 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004757 }
4758 }
4759 set_scnode_clear_ctx(set);
4760 return rc;
4761 }
4762
4763 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004764 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004765 LY_CHECK_RET(rc);
4766 set_fill_number(set, strlen(args[0]->val.str));
4767 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004768 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004769 LY_CHECK_RET(rc);
4770 set_fill_number(set, strlen(set->val.str));
4771 }
4772
4773 return LY_SUCCESS;
4774}
4775
4776/**
4777 * @brief Execute the XPath substring(string, number, number?) function.
4778 * Returns LYXP_SET_STRING substring of the first argument starting
4779 * on the second argument index ending on the third argument index,
4780 * indexed from 1. For exact definition refer to
4781 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-substring.
4782 *
4783 * @param[in] args Array of arguments.
4784 * @param[in] arg_count Count of elements in @p args.
4785 * @param[in,out] set Context and result set at the same time.
4786 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004787 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004788 */
4789static LY_ERR
4790xpath_substring(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
4791{
4792 int start, len;
4793 uint16_t str_start, str_len, pos;
4794 struct lysc_node_leaf *sleaf;
4795 LY_ERR rc = LY_SUCCESS;
4796
4797 if (options & LYXP_SCNODE_ALL) {
4798 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4799 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4800 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 +02004801 } else if (!warn_is_string_type(sleaf->type)) {
4802 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004803 }
4804 }
4805
4806 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4807 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4808 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 +02004809 } else if (!warn_is_numeric_type(sleaf->type)) {
4810 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004811 }
4812 }
4813
4814 if ((arg_count == 3) && (args[2]->type == LYXP_SET_SCNODE_SET)
4815 && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
4816 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4817 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 +02004818 } else if (!warn_is_numeric_type(sleaf->type)) {
4819 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004820 }
4821 }
4822 set_scnode_clear_ctx(set);
4823 return rc;
4824 }
4825
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004826 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004827 LY_CHECK_RET(rc);
4828
4829 /* start */
4830 if (xpath_round(&args[1], 1, args[1], options)) {
4831 return -1;
4832 }
4833 if (isfinite(args[1]->val.num)) {
4834 start = args[1]->val.num - 1;
4835 } else if (isinf(args[1]->val.num) && signbit(args[1]->val.num)) {
4836 start = INT_MIN;
4837 } else {
4838 start = INT_MAX;
4839 }
4840
4841 /* len */
4842 if (arg_count == 3) {
4843 rc = xpath_round(&args[2], 1, args[2], options);
4844 LY_CHECK_RET(rc);
4845 if (isfinite(args[2]->val.num)) {
4846 len = args[2]->val.num;
4847 } else if (isnan(args[2]->val.num) || signbit(args[2]->val.num)) {
4848 len = 0;
4849 } else {
4850 len = INT_MAX;
4851 }
4852 } else {
4853 len = INT_MAX;
4854 }
4855
4856 /* find matching character positions */
4857 str_start = 0;
4858 str_len = 0;
4859 for (pos = 0; args[0]->val.str[pos]; ++pos) {
4860 if (pos < start) {
4861 ++str_start;
4862 } else if (pos < start + len) {
4863 ++str_len;
4864 } else {
4865 break;
4866 }
4867 }
4868
4869 set_fill_string(set, args[0]->val.str + str_start, str_len);
4870 return LY_SUCCESS;
4871}
4872
4873/**
4874 * @brief Execute the XPath substring-after(string, string) function.
4875 * Returns LYXP_SET_STRING with the string succeeding the occurance
4876 * of the second argument in the first or an empty string.
4877 *
4878 * @param[in] args Array of arguments.
4879 * @param[in] arg_count Count of elements in @p args.
4880 * @param[in,out] set Context and result set at the same time.
4881 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004882 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004883 */
4884static LY_ERR
4885xpath_substring_after(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4886{
4887 char *ptr;
4888 struct lysc_node_leaf *sleaf;
4889 LY_ERR rc = LY_SUCCESS;
4890
4891 if (options & LYXP_SCNODE_ALL) {
4892 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4893 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4894 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 +02004895 } else if (!warn_is_string_type(sleaf->type)) {
4896 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004897 }
4898 }
4899
4900 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4901 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4902 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 +02004903 } else if (!warn_is_string_type(sleaf->type)) {
4904 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004905 }
4906 }
4907 set_scnode_clear_ctx(set);
4908 return rc;
4909 }
4910
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004911 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004912 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004913 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004914 LY_CHECK_RET(rc);
4915
4916 ptr = strstr(args[0]->val.str, args[1]->val.str);
4917 if (ptr) {
4918 set_fill_string(set, ptr + strlen(args[1]->val.str), strlen(ptr + strlen(args[1]->val.str)));
4919 } else {
4920 set_fill_string(set, "", 0);
4921 }
4922
4923 return LY_SUCCESS;
4924}
4925
4926/**
4927 * @brief Execute the XPath substring-before(string, string) function.
4928 * Returns LYXP_SET_STRING with the string preceding the occurance
4929 * of the second argument in the first or an empty string.
4930 *
4931 * @param[in] args Array of arguments.
4932 * @param[in] arg_count Count of elements in @p args.
4933 * @param[in,out] set Context and result set at the same time.
4934 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004935 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004936 */
4937static LY_ERR
4938xpath_substring_before(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4939{
4940 char *ptr;
4941 struct lysc_node_leaf *sleaf;
4942 LY_ERR rc = LY_SUCCESS;
4943
4944 if (options & LYXP_SCNODE_ALL) {
4945 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4946 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4947 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 +02004948 } else if (!warn_is_string_type(sleaf->type)) {
4949 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004950 }
4951 }
4952
4953 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4954 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4955 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 +02004956 } else if (!warn_is_string_type(sleaf->type)) {
4957 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004958 }
4959 }
4960 set_scnode_clear_ctx(set);
4961 return rc;
4962 }
4963
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004964 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004965 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004966 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004967 LY_CHECK_RET(rc);
4968
4969 ptr = strstr(args[0]->val.str, args[1]->val.str);
4970 if (ptr) {
4971 set_fill_string(set, args[0]->val.str, ptr - args[0]->val.str);
4972 } else {
4973 set_fill_string(set, "", 0);
4974 }
4975
4976 return LY_SUCCESS;
4977}
4978
4979/**
4980 * @brief Execute the XPath sum(node-set) function. Returns LYXP_SET_NUMBER
4981 * with the sum of all the nodes in the context.
4982 *
4983 * @param[in] args Array of arguments.
4984 * @param[in] arg_count Count of elements in @p args.
4985 * @param[in,out] set Context and result set at the same time.
4986 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004987 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004988 */
4989static LY_ERR
4990xpath_sum(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4991{
4992 long double num;
4993 char *str;
4994 uint16_t i;
4995 struct lyxp_set set_item;
4996 struct lysc_node_leaf *sleaf;
4997 LY_ERR rc = LY_SUCCESS;
4998
4999 if (options & LYXP_SCNODE_ALL) {
5000 if (args[0]->type == LYXP_SET_SCNODE_SET) {
5001 for (i = 0; i < args[0]->used; ++i) {
5002 if (args[0]->val.scnodes[i].in_ctx == 1) {
5003 sleaf = (struct lysc_node_leaf *)args[0]->val.scnodes[i].scnode;
5004 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5005 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__,
5006 lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005007 } else if (!warn_is_numeric_type(sleaf->type)) {
5008 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005009 }
5010 }
5011 }
5012 }
5013 set_scnode_clear_ctx(set);
5014 return rc;
5015 }
5016
5017 set_fill_number(set, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005018
5019 if (args[0]->type != LYXP_SET_NODE_SET) {
5020 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "sum(node-set)");
5021 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02005022 } else if (!args[0]->used) {
5023 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005024 }
5025
Michal Vasko5c4e5892019-11-14 12:31:38 +01005026 set_init(&set_item, set);
5027
Michal Vasko03ff5a72019-09-11 13:49:33 +02005028 set_item.type = LYXP_SET_NODE_SET;
5029 set_item.val.nodes = malloc(sizeof *set_item.val.nodes);
5030 LY_CHECK_ERR_RET(!set_item.val.nodes, LOGMEM(set->ctx), LY_EMEM);
5031
5032 set_item.used = 1;
5033 set_item.size = 1;
5034
5035 for (i = 0; i < args[0]->used; ++i) {
5036 set_item.val.nodes[0] = args[0]->val.nodes[i];
5037
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005038 rc = cast_node_set_to_string(&set_item, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005039 LY_CHECK_RET(rc);
5040 num = cast_string_to_number(str);
5041 free(str);
5042 set->val.num += num;
5043 }
5044
5045 free(set_item.val.nodes);
5046
5047 return LY_SUCCESS;
5048}
5049
5050/**
5051 * @brief Execute the XPath text() function (node type). Returns LYXP_SET_NODE_SET
5052 * with the text content of the nodes in the context.
5053 *
5054 * @param[in] args Array of arguments.
5055 * @param[in] arg_count Count of elements in @p args.
5056 * @param[in,out] set Context and result set at the same time.
5057 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005058 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005059 */
5060static LY_ERR
5061xpath_text(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
5062{
5063 uint32_t i;
5064
5065 if (options & LYXP_SCNODE_ALL) {
5066 set_scnode_clear_ctx(set);
5067 return LY_SUCCESS;
5068 }
5069
Michal Vasko03ff5a72019-09-11 13:49:33 +02005070 if (set->type != LYXP_SET_NODE_SET) {
5071 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "text()");
5072 return LY_EVALID;
5073 }
5074
5075 for (i = 0; i < set->used;) {
5076 switch (set->val.nodes[i].type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01005077 case LYXP_NODE_NONE:
5078 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005079 case LYXP_NODE_ELEM:
Michal Vasko03ff5a72019-09-11 13:49:33 +02005080 if (set->val.nodes[i].node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
5081 set->val.nodes[i].type = LYXP_NODE_TEXT;
5082 ++i;
5083 break;
5084 }
5085 /* fall through */
5086 case LYXP_NODE_ROOT:
5087 case LYXP_NODE_ROOT_CONFIG:
5088 case LYXP_NODE_TEXT:
Michal Vasko9f96a052020-03-10 09:41:45 +01005089 case LYXP_NODE_META:
Michal Vasko03ff5a72019-09-11 13:49:33 +02005090 set_remove_node(set, i);
5091 break;
5092 }
5093 }
5094
5095 return LY_SUCCESS;
5096}
5097
5098/**
5099 * @brief Execute the XPath translate(string, string, string) function.
5100 * Returns LYXP_SET_STRING with the first argument with the characters
5101 * from the second argument replaced by those on the corresponding
5102 * positions in the third argument.
5103 *
5104 * @param[in] args Array of arguments.
5105 * @param[in] arg_count Count of elements in @p args.
5106 * @param[in,out] set Context and result set at the same time.
5107 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005108 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005109 */
5110static LY_ERR
5111xpath_translate(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
5112{
5113 uint16_t i, j, new_used;
5114 char *new;
5115 int found, have_removed;
5116 struct lysc_node_leaf *sleaf;
5117 LY_ERR rc = LY_SUCCESS;
5118
5119 if (options & LYXP_SCNODE_ALL) {
5120 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5121 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5122 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 +02005123 } else if (!warn_is_string_type(sleaf->type)) {
5124 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005125 }
5126 }
5127
5128 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5129 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5130 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 +02005131 } else if (!warn_is_string_type(sleaf->type)) {
5132 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005133 }
5134 }
5135
5136 if ((args[2]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
5137 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5138 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 +02005139 } else if (!warn_is_string_type(sleaf->type)) {
5140 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005141 }
5142 }
5143 set_scnode_clear_ctx(set);
5144 return rc;
5145 }
5146
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005147 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005148 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005149 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005150 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005151 rc = lyxp_set_cast(args[2], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005152 LY_CHECK_RET(rc);
5153
5154 new = malloc((strlen(args[0]->val.str) + 1) * sizeof(char));
5155 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5156 new_used = 0;
5157
5158 have_removed = 0;
5159 for (i = 0; args[0]->val.str[i]; ++i) {
5160 found = 0;
5161
5162 for (j = 0; args[1]->val.str[j]; ++j) {
5163 if (args[0]->val.str[i] == args[1]->val.str[j]) {
5164 /* removing this char */
5165 if (j >= strlen(args[2]->val.str)) {
5166 have_removed = 1;
5167 found = 1;
5168 break;
5169 }
5170 /* replacing this char */
5171 new[new_used] = args[2]->val.str[j];
5172 ++new_used;
5173 found = 1;
5174 break;
5175 }
5176 }
5177
5178 /* copying this char */
5179 if (!found) {
5180 new[new_used] = args[0]->val.str[i];
5181 ++new_used;
5182 }
5183 }
5184
5185 if (have_removed) {
5186 new = ly_realloc(new, (new_used + 1) * sizeof(char));
5187 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5188 }
5189 new[new_used] = '\0';
5190
Michal Vaskod3678892020-05-21 10:06:58 +02005191 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005192 set->type = LYXP_SET_STRING;
5193 set->val.str = new;
5194
5195 return LY_SUCCESS;
5196}
5197
5198/**
5199 * @brief Execute the XPath true() function. Returns LYXP_SET_BOOLEAN
5200 * with true value.
5201 *
5202 * @param[in] args Array of arguments.
5203 * @param[in] arg_count Count of elements in @p args.
5204 * @param[in,out] set Context and result set at the same time.
5205 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005206 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005207 */
5208static LY_ERR
5209xpath_true(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
5210{
5211 if (options & LYXP_SCNODE_ALL) {
5212 set_scnode_clear_ctx(set);
5213 return LY_SUCCESS;
5214 }
5215
5216 set_fill_boolean(set, 1);
5217 return LY_SUCCESS;
5218}
5219
5220/*
5221 * moveto functions
5222 *
5223 * They and only they actually change the context (set).
5224 */
5225
5226/**
Michal Vasko6346ece2019-09-24 13:12:53 +02005227 * @brief Skip prefix and return corresponding model if there is a prefix. Logs directly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005228 *
Michal Vasko2104e9f2020-03-06 08:23:25 +01005229 * XPath @p set is expected to be a (sc)node set!
5230 *
Michal Vasko6346ece2019-09-24 13:12:53 +02005231 * @param[in,out] qname Qualified node name. If includes prefix, it is skipped.
5232 * @param[in,out] qname_len Length of @p qname, is updated accordingly.
5233 * @param[in] set Set with XPath context.
5234 * @param[out] moveto_mod Expected module of a matching node.
5235 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005236 */
Michal Vasko6346ece2019-09-24 13:12:53 +02005237static LY_ERR
5238moveto_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 +02005239{
Michal Vasko6346ece2019-09-24 13:12:53 +02005240 const struct lys_module *mod;
5241 const char *ptr;
5242 int pref_len;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005243 char *str;
5244
Michal Vasko2104e9f2020-03-06 08:23:25 +01005245 assert((set->type == LYXP_SET_NODE_SET) || (set->type == LYXP_SET_SCNODE_SET));
5246
Michal Vasko6346ece2019-09-24 13:12:53 +02005247 if ((ptr = ly_strnchr(*qname, ':', *qname_len))) {
5248 /* specific module */
5249 pref_len = ptr - *qname;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005250
Michal Vasko6346ece2019-09-24 13:12:53 +02005251 switch (set->format) {
Michal Vasko52927e22020-03-16 17:26:14 +01005252 case LYD_SCHEMA:
Michal Vasko6346ece2019-09-24 13:12:53 +02005253 /* schema, search all local module imports */
5254 mod = lys_module_find_prefix(set->local_mod, *qname, pref_len);
5255 break;
5256 case LYD_JSON:
5257 /* JSON data, search in context */
5258 str = strndup(*qname, pref_len);
Michal Vasko97e76fd2020-05-27 15:22:01 +02005259 mod = ly_ctx_get_module_implemented(set->ctx, str);
Michal Vasko6346ece2019-09-24 13:12:53 +02005260 free(str);
5261 break;
Michal Vasko52927e22020-03-16 17:26:14 +01005262 case LYD_XML:
Michal Vasko6346ece2019-09-24 13:12:53 +02005263 LOGINT_RET(set->ctx);
5264 }
5265
Michal Vasko004d3152020-06-11 19:59:22 +02005266 /* check for errors and non-implemented modules, as they are not valid */
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005267 if (!mod || !mod->implemented) {
Michal Vasko2104e9f2020-03-06 08:23:25 +01005268 if (set->type == LYXP_SET_SCNODE_SET) {
5269 LOGVAL(set->ctx, LY_VLOG_LYSC, set->ctx_scnode, LY_VCODE_XP_INMOD, pref_len, *qname);
5270 } else {
5271 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INMOD, pref_len, *qname);
5272 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005273 return LY_EVALID;
5274 }
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005275
Michal Vasko6346ece2019-09-24 13:12:53 +02005276 *qname += pref_len + 1;
5277 *qname_len -= pref_len + 1;
5278 } else if (((*qname)[0] == '*') && (*qname_len == 1)) {
5279 /* all modules - special case */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005280 mod = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02005281 } else if (set->type == LYXP_SET_SCNODE_SET) {
5282 /* current node module */
5283 mod = set->ctx_scnode->module;
Michal Vasko6346ece2019-09-24 13:12:53 +02005284 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02005285 /* current node module */
5286 mod = set->ctx_node->schema->module;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005287 }
5288
Michal Vasko6346ece2019-09-24 13:12:53 +02005289 *moveto_mod = mod;
5290 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005291}
5292
5293/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02005294 * @brief Move context @p set to the root. Handles absolute path.
5295 * Result is LYXP_SET_NODE_SET.
5296 *
5297 * @param[in,out] set Set to use.
5298 * @param[in] options Xpath options.
5299 */
5300static void
5301moveto_root(struct lyxp_set *set, int options)
5302{
Michal Vasko03ff5a72019-09-11 13:49:33 +02005303 if (!set) {
5304 return;
5305 }
5306
5307 if (options & LYXP_SCNODE_ALL) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005308 set_scnode_clear_ctx(set);
Michal Vaskoecd62de2019-11-13 12:35:11 +01005309 lyxp_set_scnode_insert_node(set, NULL, set->root_type);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005310 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005311 set->type = LYXP_SET_NODE_SET;
5312 set->used = 0;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005313 set_insert_node(set, NULL, 0, set->root_type, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005314 }
5315}
5316
5317/**
Michal Vaskoa1424542019-11-14 16:08:52 +01005318 * @brief Check whether a node has some unresolved "when".
5319 *
5320 * @param[in] node Node to check.
5321 * @return LY_ERR value (LY_EINCOMPLETE if there are some unresolved "when")
5322 */
5323static LY_ERR
5324moveto_when_check(const struct lyd_node *node)
5325{
5326 const struct lysc_node *schema;
5327
5328 if (!node) {
5329 return LY_SUCCESS;
5330 }
5331
5332 schema = node->schema;
5333 do {
5334 if (schema->when && !(node->flags & LYD_WHEN_TRUE)) {
5335 return LY_EINCOMPLETE;
5336 }
5337 schema = schema->parent;
5338 } while (schema && (schema->nodetype & (LYS_CASE | LYS_CHOICE)));
5339
5340 return LY_SUCCESS;
5341}
5342
5343/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02005344 * @brief Check @p node as a part of NameTest processing.
5345 *
5346 * @param[in] node Node to check.
5347 * @param[in] root_type XPath root node type.
Michal Vaskod3678892020-05-21 10:06:58 +02005348 * @param[in] node_name Node name in the dictionary to move to, NULL for any node.
5349 * @param[in] moveto_mod Expected module of the node, NULL for any.
Michal Vasko6346ece2019-09-24 13:12:53 +02005350 * @return LY_ERR (LY_ENOT if node does not match, LY_EINCOMPLETE on unresolved when,
5351 * LY_EINVAL if netither node nor any children match)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005352 */
5353static LY_ERR
5354moveto_node_check(const struct lyd_node *node, enum lyxp_node_type root_type, const char *node_name,
5355 const struct lys_module *moveto_mod)
5356{
5357 /* module check */
5358 if (moveto_mod && (node->schema->module != moveto_mod)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005359 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005360 }
5361
Michal Vasko5c4e5892019-11-14 12:31:38 +01005362 /* context check */
5363 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005364 return LY_EINVAL;
5365 }
5366
5367 /* name check */
Michal Vasko61ac2f62020-05-25 12:39:51 +02005368 if (node_name && strcmp(node_name, "*") && (node->schema->name != node_name)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005369 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005370 }
5371
Michal Vaskoa1424542019-11-14 16:08:52 +01005372 /* when check */
5373 if (moveto_when_check(node)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005374 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01005375 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005376
5377 /* match */
5378 return LY_SUCCESS;
5379}
5380
5381/**
5382 * @brief Check @p node as a part of schema NameTest processing.
5383 *
5384 * @param[in] node Schema node to check.
5385 * @param[in] root_type XPath root node type.
Michal Vaskod3678892020-05-21 10:06:58 +02005386 * @param[in] node_name Node name in the dictionary to move to, NULL for any nodes.
5387 * @param[in] moveto_mod Expected module of the node, NULL for any.
Michal Vasko6346ece2019-09-24 13:12:53 +02005388 * @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 +02005389 */
5390static LY_ERR
5391moveto_scnode_check(const struct lysc_node *node, enum lyxp_node_type root_type, const char *node_name,
Michal Vaskocafad9d2019-11-07 15:20:03 +01005392 const struct lys_module *moveto_mod)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005393{
Michal Vasko03ff5a72019-09-11 13:49:33 +02005394 /* module check */
Michal Vaskod3678892020-05-21 10:06:58 +02005395 if (moveto_mod && (node->module != moveto_mod)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005396 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005397 }
5398
5399 /* context check */
5400 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (node->flags & LYS_CONFIG_R)) {
5401 return LY_EINVAL;
5402 }
5403
5404 /* name check */
Michal Vasko61ac2f62020-05-25 12:39:51 +02005405 if (node_name && strcmp(node_name, "*") && (node->name != node_name)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005406 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005407 }
5408
5409 /* match */
5410 return LY_SUCCESS;
5411}
5412
5413/**
Michal Vaskod3678892020-05-21 10:06:58 +02005414 * @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 +02005415 *
5416 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005417 * @param[in] mod Matching node module, NULL for any.
5418 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005419 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5420 */
5421static LY_ERR
Michal Vaskod3678892020-05-21 10:06:58 +02005422moveto_node(struct lyxp_set *set, const struct lys_module *mod, const char *ncname)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005423{
Michal Vaskof03ed032020-03-04 13:31:44 +01005424 uint32_t i;
Michal Vasko6346ece2019-09-24 13:12:53 +02005425 int replaced;
Michal Vaskod3678892020-05-21 10:06:58 +02005426 const struct lyd_node *siblings, *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005427 LY_ERR rc;
5428
Michal Vaskod3678892020-05-21 10:06:58 +02005429 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005430 return LY_SUCCESS;
5431 }
5432
5433 if (set->type != LYXP_SET_NODE_SET) {
5434 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
5435 return LY_EVALID;
5436 }
5437
Michal Vasko03ff5a72019-09-11 13:49:33 +02005438 for (i = 0; i < set->used; ) {
5439 replaced = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02005440 siblings = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005441
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 Vasko5c4e5892019-11-14 12:31:38 +01005447 } else if (!(set->val.nodes[i].node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA))) {
Michal Vaskod3678892020-05-21 10:06:58 +02005448 /* search in children */
5449 siblings = lyd_node_children(set->val.nodes[i].node);
5450 }
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 */
5530 siblings = lyd_node_children(set->val.nodes[i].node);
5531 }
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 Vaskod3678892020-05-21 10:06:58 +02005719 next = lyd_node_children(elem);
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 */
6066 first = lyd_node_children(parent);
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}