blob: f73988bd42c961762dfffd01a336cdc5c3079b80 [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 Krejci241f6b52020-05-21 18:13:49 +0200419 out = ly_out_new_memory(&buf, 0);
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;
3614 LY_ERR rc = LY_SUCCESS;
3615
3616 if (options & LYXP_SCNODE_ALL) {
3617 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3618 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003619 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3620 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 +02003621 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_LEAFREF) && !warn_is_specific_type(sleaf->type, LY_TYPE_INST)) {
3622 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"leafref\" nor \"instance-identifier\".",
3623 __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003624 }
3625 set_scnode_clear_ctx(set);
Michal Vasko42e497c2020-01-06 08:38:25 +01003626 if (sleaf && (sleaf->type->basetype == LY_TYPE_LEAFREF)) {
Michal Vasko004d3152020-06-11 19:59:22 +02003627 lref = (struct lysc_type_leafref *)sleaf->type;
3628
3629 /* it was already evaluated on schema, it must succeed */
3630 if (set->format == LYD_JSON) {
3631 rc = ly_path_compile(sleaf->module, (struct lysc_node *)sleaf, lref->path, LY_PATH_LREF_TRUE,
3632 lydjson_resolve_prefix, NULL, LYD_JSON, &p);
3633 } else {
3634 assert(set->format == LYD_SCHEMA);
3635 rc = ly_path_compile(sleaf->module, (struct lysc_node *)sleaf, lref->path, LY_PATH_LREF_TRUE,
3636 lys_resolve_prefix, lref->path_context, LYD_SCHEMA, &p);
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003637 }
Michal Vasko004d3152020-06-11 19:59:22 +02003638 assert(!rc);
3639
3640 /* get the target node */
3641 target = p[LY_ARRAY_SIZE(p) - 1].node;
3642 ly_path_free(set->ctx, p);
3643
3644 lyxp_set_scnode_insert_node(set, target, LYXP_NODE_ELEM);
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003645 }
3646
Michal Vasko03ff5a72019-09-11 13:49:33 +02003647 return rc;
3648 }
3649
Michal Vaskod3678892020-05-21 10:06:58 +02003650 if (args[0]->type != LYXP_SET_NODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003651 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "deref(node-set)");
3652 return LY_EVALID;
3653 }
3654
Michal Vaskod3678892020-05-21 10:06:58 +02003655 lyxp_set_free_content(set);
3656 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003657 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
3658 sleaf = (struct lysc_node_leaf *)leaf->schema;
3659 if (sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
3660 if (sleaf->type->basetype == LY_TYPE_LEAFREF) {
3661 /* find leafref target */
Michal Vasko004d3152020-06-11 19:59:22 +02003662 if (ly_type_find_leafref((struct lysc_type_leafref *)sleaf->type, (struct lyd_node *)leaf,
3663 &leaf->value, set->tree, &node, &errmsg)) {
3664 LOGERR(set->ctx, LY_EVALID, errmsg);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003665 free(errmsg);
Michal Vasko004d3152020-06-11 19:59:22 +02003666 return LY_EVALID;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003667 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003668 } else {
3669 assert(sleaf->type->basetype == LY_TYPE_INST);
Michal Vasko004d3152020-06-11 19:59:22 +02003670 if (ly_path_eval(leaf->value.target, set->tree, &node)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003671 val = lyd_value2str(leaf, &dynamic);
3672 LOGERR(set->ctx, LY_EVALID, "Invalid instance-identifier \"%s\" value - required instance not found.", val);
3673 if (dynamic) {
3674 free((char *)val);
3675 }
3676 return LY_EVALID;
3677 }
3678 }
Michal Vasko004d3152020-06-11 19:59:22 +02003679
3680 /* insert it */
3681 set_insert_node(set, node, 0, LYXP_NODE_ELEM, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003682 }
3683 }
3684
3685 return LY_SUCCESS;
3686}
3687
3688/**
3689 * @brief Execute the YANG 1.1 derived-from(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
3690 * on whether the first argument nodes contain a node of an identity derived from the second
3691 * argument identity.
3692 *
3693 * @param[in] args Array of arguments.
3694 * @param[in] arg_count Count of elements in @p args.
3695 * @param[in,out] set Context and result set at the same time.
3696 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003697 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003698 */
3699static LY_ERR
3700xpath_derived_from(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3701{
3702 uint16_t i;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003703 LY_ARRAY_SIZE_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003704 struct lyd_node_term *leaf;
3705 struct lysc_node_leaf *sleaf;
3706 struct lyd_value data = {0};
3707 struct ly_err_item *err = NULL;
3708 LY_ERR rc = LY_SUCCESS;
3709 int found;
3710
3711 if (options & LYXP_SCNODE_ALL) {
3712 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3713 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003714 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3715 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 +02003716 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_IDENT)) {
3717 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"identityref\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003718 }
3719
3720 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3721 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3722 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 +02003723 } else if (!warn_is_string_type(sleaf->type)) {
3724 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003725 }
3726 }
3727 set_scnode_clear_ctx(set);
3728 return rc;
3729 }
3730
Michal Vaskod3678892020-05-21 10:06:58 +02003731 if (args[0]->type != LYXP_SET_NODE_SET) {
3732 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
3733 "derived-from(node-set, string)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003734 return LY_EVALID;
3735 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003736 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003737 LY_CHECK_RET(rc);
3738
3739 set_fill_boolean(set, 0);
Michal Vaskod3678892020-05-21 10:06:58 +02003740 found = 0;
3741 for (i = 0; i < args[0]->used; ++i) {
3742 leaf = (struct lyd_node_term *)args[0]->val.nodes[i].node;
3743 sleaf = (struct lysc_node_leaf *)leaf->schema;
3744 if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_IDENT)) {
3745 /* store args[1] into ident */
3746 rc = sleaf->type->plugin->store(set->ctx, sleaf->type, args[1]->val.str, strlen(args[1]->val.str),
3747 LY_TYPE_OPTS_STORE, lys_resolve_prefix, (void *)sleaf->dflt_mod, set->format,
3748 (struct lyd_node *)leaf, set->tree, &data, NULL, &err);
3749 if (err) {
3750 ly_err_print(err);
3751 ly_err_free(err);
3752 }
3753 LY_CHECK_RET(rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003754
Michal Vaskod3678892020-05-21 10:06:58 +02003755 LY_ARRAY_FOR(data.ident->derived, u) {
3756 if (data.ident->derived[u] == leaf->value.ident) {
3757 set_fill_boolean(set, 1);
3758 found = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003759 break;
3760 }
3761 }
Michal Vaskod3678892020-05-21 10:06:58 +02003762 if (found) {
3763 break;
3764 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003765 }
3766 }
3767
3768 return LY_SUCCESS;
3769}
3770
3771/**
3772 * @brief Execute the YANG 1.1 derived-from-or-self(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
3773 * on whether the first argument nodes contain a node of an identity that either is or is derived from
3774 * the second argument identity.
3775 *
3776 * @param[in] args Array of arguments.
3777 * @param[in] arg_count Count of elements in @p args.
3778 * @param[in,out] set Context and result set at the same time.
3779 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003780 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003781 */
3782static LY_ERR
3783xpath_derived_from_or_self(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3784{
3785 uint16_t i;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003786 LY_ARRAY_SIZE_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003787 struct lyd_node_term *leaf;
3788 struct lysc_node_leaf *sleaf;
3789 struct lyd_value data = {0};
3790 struct ly_err_item *err = NULL;
3791 LY_ERR rc = LY_SUCCESS;
3792 int found;
3793
3794 if (options & LYXP_SCNODE_ALL) {
3795 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3796 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003797 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3798 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 +02003799 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_IDENT)) {
3800 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"identityref\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003801 }
3802
3803 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3804 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3805 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 +02003806 } else if (!warn_is_string_type(sleaf->type)) {
3807 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003808 }
3809 }
3810 set_scnode_clear_ctx(set);
3811 return rc;
3812 }
3813
Michal Vaskod3678892020-05-21 10:06:58 +02003814 if (args[0]->type != LYXP_SET_NODE_SET) {
3815 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
3816 "derived-from-or-self(node-set, string)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003817 return LY_EVALID;
3818 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003819 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003820 LY_CHECK_RET(rc);
3821
3822 set_fill_boolean(set, 0);
Michal Vaskod3678892020-05-21 10:06:58 +02003823 found = 0;
3824 for (i = 0; i < args[0]->used; ++i) {
3825 leaf = (struct lyd_node_term *)args[0]->val.nodes[i].node;
3826 sleaf = (struct lysc_node_leaf *)leaf->schema;
3827 if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_IDENT)) {
3828 /* store args[1] into ident */
3829 rc = sleaf->type->plugin->store(set->ctx, sleaf->type, args[1]->val.str, strlen(args[1]->val.str),
3830 LY_TYPE_OPTS_STORE, lys_resolve_prefix, (void *)sleaf->dflt_mod, set->format,
3831 (struct lyd_node *)leaf, set->tree, &data, NULL, &err);
3832 if (err) {
3833 ly_err_print(err);
3834 ly_err_free(err);
3835 }
3836 LY_CHECK_RET(rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003837
Michal Vaskod3678892020-05-21 10:06:58 +02003838 if (data.ident == leaf->value.ident) {
3839 set_fill_boolean(set, 1);
3840 break;
3841 }
3842 LY_ARRAY_FOR(data.ident->derived, u) {
3843 if (data.ident->derived[u] == leaf->value.ident) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003844 set_fill_boolean(set, 1);
Michal Vaskod3678892020-05-21 10:06:58 +02003845 found = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003846 break;
3847 }
Michal Vaskod3678892020-05-21 10:06:58 +02003848 }
3849 if (found) {
3850 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003851 }
3852 }
3853 }
3854
3855 return LY_SUCCESS;
3856}
3857
3858/**
3859 * @brief Execute the YANG 1.1 enum-value(node-set) function. Returns LYXP_SET_NUMBER
3860 * with the integer value of the first node's enum value, otherwise NaN.
3861 *
3862 * @param[in] args Array of arguments.
3863 * @param[in] arg_count Count of elements in @p args.
3864 * @param[in,out] set Context and result set at the same time.
3865 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003866 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003867 */
3868static LY_ERR
3869xpath_enum_value(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3870{
3871 struct lyd_node_term *leaf;
3872 struct lysc_node_leaf *sleaf;
3873 LY_ERR rc = LY_SUCCESS;
3874
3875 if (options & LYXP_SCNODE_ALL) {
3876 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3877 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003878 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3879 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 +02003880 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_ENUM)) {
3881 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"enumeration\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003882 }
3883 set_scnode_clear_ctx(set);
3884 return rc;
3885 }
3886
Michal Vaskod3678892020-05-21 10:06:58 +02003887 if (args[0]->type != LYXP_SET_NODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003888 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "enum-value(node-set)");
3889 return LY_EVALID;
3890 }
3891
3892 set_fill_number(set, NAN);
Michal Vaskod3678892020-05-21 10:06:58 +02003893 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003894 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
3895 sleaf = (struct lysc_node_leaf *)leaf->schema;
3896 if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_ENUM)) {
3897 set_fill_number(set, leaf->value.enum_item->value);
3898 }
3899 }
3900
3901 return LY_SUCCESS;
3902}
3903
3904/**
3905 * @brief Execute the XPath false() function. Returns LYXP_SET_BOOLEAN
3906 * with false value.
3907 *
3908 * @param[in] args Array of arguments.
3909 * @param[in] arg_count Count of elements in @p args.
3910 * @param[in,out] set Context and result set at the same time.
3911 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003912 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003913 */
3914static LY_ERR
3915xpath_false(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3916{
3917 if (options & LYXP_SCNODE_ALL) {
3918 set_scnode_clear_ctx(set);
3919 return LY_SUCCESS;
3920 }
3921
3922 set_fill_boolean(set, 0);
3923 return LY_SUCCESS;
3924}
3925
3926/**
3927 * @brief Execute the XPath floor(number) function. Returns LYXP_SET_NUMBER
3928 * with the first argument floored (truncated).
3929 *
3930 * @param[in] args Array of arguments.
3931 * @param[in] arg_count Count of elements in @p args.
3932 * @param[in,out] set Context and result set at the same time.
3933 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003934 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003935 */
3936static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003937xpath_floor(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int UNUSED(options))
Michal Vasko03ff5a72019-09-11 13:49:33 +02003938{
3939 LY_ERR rc;
3940
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003941 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003942 LY_CHECK_RET(rc);
3943 if (isfinite(args[0]->val.num)) {
3944 set_fill_number(set, (long long)args[0]->val.num);
3945 }
3946
3947 return LY_SUCCESS;
3948}
3949
3950/**
3951 * @brief Execute the XPath lang(string) function. Returns LYXP_SET_BOOLEAN
3952 * whether the language of the text matches the one from the argument.
3953 *
3954 * @param[in] args Array of arguments.
3955 * @param[in] arg_count Count of elements in @p args.
3956 * @param[in,out] set Context and result set at the same time.
3957 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003958 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003959 */
3960static LY_ERR
3961xpath_lang(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3962{
3963 const struct lyd_node *node;
3964 struct lysc_node_leaf *sleaf;
Michal Vasko9f96a052020-03-10 09:41:45 +01003965 struct lyd_meta *meta = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003966 const char *val;
3967 int i, dynamic;
3968 LY_ERR rc = LY_SUCCESS;
3969
3970 if (options & LYXP_SCNODE_ALL) {
3971 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3972 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3973 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 +02003974 } else if (!warn_is_string_type(sleaf->type)) {
3975 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003976 }
3977 }
3978 set_scnode_clear_ctx(set);
3979 return rc;
3980 }
3981
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003982 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003983 LY_CHECK_RET(rc);
3984
Michal Vasko03ff5a72019-09-11 13:49:33 +02003985 if (set->type != LYXP_SET_NODE_SET) {
3986 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "lang(string)");
3987 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02003988 } else if (!set->used) {
3989 set_fill_boolean(set, 0);
3990 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003991 }
3992
3993 switch (set->val.nodes[0].type) {
3994 case LYXP_NODE_ELEM:
3995 case LYXP_NODE_TEXT:
3996 node = set->val.nodes[0].node;
3997 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01003998 case LYXP_NODE_META:
3999 node = set->val.meta[0].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004000 break;
4001 default:
4002 /* nothing to do with roots */
4003 set_fill_boolean(set, 0);
4004 return LY_SUCCESS;
4005 }
4006
Michal Vasko9f96a052020-03-10 09:41:45 +01004007 /* find lang metadata */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004008 for (; node; node = (struct lyd_node *)node->parent) {
Michal Vasko9f96a052020-03-10 09:41:45 +01004009 for (meta = node->meta; meta; meta = meta->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004010 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004011 if (meta->name && !strcmp(meta->name, "lang") && !strcmp(meta->annotation->module->name, "xml")) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004012 break;
4013 }
4014 }
4015
Michal Vasko9f96a052020-03-10 09:41:45 +01004016 if (meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004017 break;
4018 }
4019 }
4020
4021 /* compare languages */
Michal Vasko9f96a052020-03-10 09:41:45 +01004022 if (!meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004023 set_fill_boolean(set, 0);
4024 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01004025 val = lyd_meta2str(meta, &dynamic);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004026 for (i = 0; args[0]->val.str[i]; ++i) {
4027 if (tolower(args[0]->val.str[i]) != tolower(val[i])) {
4028 set_fill_boolean(set, 0);
4029 break;
4030 }
4031 }
4032 if (!args[0]->val.str[i]) {
4033 if (!val[i] || (val[i] == '-')) {
4034 set_fill_boolean(set, 1);
4035 } else {
4036 set_fill_boolean(set, 0);
4037 }
4038 }
4039 if (dynamic) {
4040 free((char *)val);
4041 }
4042 }
4043
4044 return LY_SUCCESS;
4045}
4046
4047/**
4048 * @brief Execute the XPath last() function. Returns LYXP_SET_NUMBER
4049 * with the context size.
4050 *
4051 * @param[in] args Array of arguments.
4052 * @param[in] arg_count Count of elements in @p args.
4053 * @param[in,out] set Context and result set at the same time.
4054 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004055 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004056 */
4057static LY_ERR
4058xpath_last(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4059{
4060 if (options & LYXP_SCNODE_ALL) {
4061 set_scnode_clear_ctx(set);
4062 return LY_SUCCESS;
4063 }
4064
Michal Vasko03ff5a72019-09-11 13:49:33 +02004065 if (set->type != LYXP_SET_NODE_SET) {
4066 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "last()");
4067 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004068 } else if (!set->used) {
4069 set_fill_number(set, 0);
4070 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004071 }
4072
4073 set_fill_number(set, set->ctx_size);
4074 return LY_SUCCESS;
4075}
4076
4077/**
4078 * @brief Execute the XPath local-name(node-set?) function. Returns LYXP_SET_STRING
4079 * with the node name without namespace from the argument or the context.
4080 *
4081 * @param[in] args Array of arguments.
4082 * @param[in] arg_count Count of elements in @p args.
4083 * @param[in,out] set Context and result set at the same time.
4084 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004085 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004086 */
4087static LY_ERR
4088xpath_local_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
4089{
4090 struct lyxp_set_node *item;
4091 /* suppress unused variable warning */
4092 (void)options;
4093
4094 if (options & LYXP_SCNODE_ALL) {
4095 set_scnode_clear_ctx(set);
4096 return LY_SUCCESS;
4097 }
4098
4099 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004100 if (args[0]->type != LYXP_SET_NODE_SET) {
4101 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "local-name(node-set?)");
4102 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004103 } else if (!args[0]->used) {
4104 set_fill_string(set, "", 0);
4105 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004106 }
4107
4108 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004109 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004110
4111 item = &args[0]->val.nodes[0];
4112 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004113 if (set->type != LYXP_SET_NODE_SET) {
4114 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "local-name(node-set?)");
4115 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004116 } else if (!set->used) {
4117 set_fill_string(set, "", 0);
4118 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004119 }
4120
4121 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004122 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004123
4124 item = &set->val.nodes[0];
4125 }
4126
4127 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004128 case LYXP_NODE_NONE:
4129 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004130 case LYXP_NODE_ROOT:
4131 case LYXP_NODE_ROOT_CONFIG:
4132 case LYXP_NODE_TEXT:
4133 set_fill_string(set, "", 0);
4134 break;
4135 case LYXP_NODE_ELEM:
4136 set_fill_string(set, item->node->schema->name, strlen(item->node->schema->name));
4137 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004138 case LYXP_NODE_META:
4139 set_fill_string(set, ((struct lyd_meta *)item->node)->name, strlen(((struct lyd_meta *)item->node)->name));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004140 break;
4141 }
4142
4143 return LY_SUCCESS;
4144}
4145
4146/**
4147 * @brief Execute the XPath name(node-set?) function. Returns LYXP_SET_STRING
4148 * with the node name fully qualified (with namespace) from the argument or the context.
4149 *
4150 * @param[in] args Array of arguments.
4151 * @param[in] arg_count Count of elements in @p args.
4152 * @param[in,out] set Context and result set at the same time.
4153 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004154 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004155 */
4156static LY_ERR
4157xpath_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
4158{
4159 struct lyxp_set_node *item;
4160 struct lys_module *mod;
4161 char *str;
4162 const char *name;
4163 int rc;
4164
4165 if (options & LYXP_SCNODE_ALL) {
4166 set_scnode_clear_ctx(set);
4167 return LY_SUCCESS;
4168 }
4169
4170 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004171 if (args[0]->type != LYXP_SET_NODE_SET) {
4172 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "name(node-set?)");
4173 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004174 } else if (!args[0]->used) {
4175 set_fill_string(set, "", 0);
4176 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004177 }
4178
4179 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004180 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004181
4182 item = &args[0]->val.nodes[0];
4183 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004184 if (set->type != LYXP_SET_NODE_SET) {
4185 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "name(node-set?)");
4186 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004187 } else if (!set->used) {
4188 set_fill_string(set, "", 0);
4189 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004190 }
4191
4192 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004193 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004194
4195 item = &set->val.nodes[0];
4196 }
4197
4198 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004199 case LYXP_NODE_NONE:
4200 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004201 case LYXP_NODE_ROOT:
4202 case LYXP_NODE_ROOT_CONFIG:
4203 case LYXP_NODE_TEXT:
4204 mod = NULL;
4205 name = NULL;
4206 break;
4207 case LYXP_NODE_ELEM:
4208 mod = item->node->schema->module;
4209 name = item->node->schema->name;
4210 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004211 case LYXP_NODE_META:
4212 mod = ((struct lyd_meta *)item->node)->annotation->module;
4213 name = ((struct lyd_meta *)item->node)->name;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004214 break;
4215 }
4216
4217 if (mod && name) {
4218 switch (set->format) {
Michal Vasko52927e22020-03-16 17:26:14 +01004219 case LYD_SCHEMA:
Michal Vasko03ff5a72019-09-11 13:49:33 +02004220 rc = asprintf(&str, "%s:%s", lys_prefix_find_module(set->local_mod, mod), name);
4221 break;
4222 case LYD_JSON:
4223 rc = asprintf(&str, "%s:%s", mod->name, name);
4224 break;
Michal Vasko52927e22020-03-16 17:26:14 +01004225 case LYD_XML:
Michal Vasko9409ef62019-09-12 11:47:17 +02004226 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004227 }
4228 LY_CHECK_ERR_RET(rc == -1, LOGMEM(set->ctx), LY_EMEM);
4229 set_fill_string(set, str, strlen(str));
4230 free(str);
4231 } else {
4232 set_fill_string(set, "", 0);
4233 }
4234
4235 return LY_SUCCESS;
4236}
4237
4238/**
4239 * @brief Execute the XPath namespace-uri(node-set?) function. Returns LYXP_SET_STRING
4240 * with the namespace of the node from the argument or the context.
4241 *
4242 * @param[in] args Array of arguments.
4243 * @param[in] arg_count Count of elements in @p args.
4244 * @param[in,out] set Context and result set at the same time.
4245 * @param[in] options XPath options.
4246 * @return LY_ERR (LY_EINVAL for wrong arguments on schema)
4247 */
4248static LY_ERR
4249xpath_namespace_uri(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
4250{
4251 struct lyxp_set_node *item;
4252 struct lys_module *mod;
4253 /* suppress unused variable warning */
4254 (void)options;
4255
4256 if (options & LYXP_SCNODE_ALL) {
4257 set_scnode_clear_ctx(set);
4258 return LY_SUCCESS;
4259 }
4260
4261 if (arg_count) {
Michal Vaskod3678892020-05-21 10:06:58 +02004262 if (args[0]->type != LYXP_SET_NODE_SET) {
4263 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
4264 "namespace-uri(node-set?)");
4265 return LY_EVALID;
4266 } else if (!args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004267 set_fill_string(set, "", 0);
4268 return LY_SUCCESS;
4269 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004270
4271 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004272 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004273
4274 item = &args[0]->val.nodes[0];
4275 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004276 if (set->type != LYXP_SET_NODE_SET) {
4277 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "namespace-uri(node-set?)");
4278 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004279 } else if (!set->used) {
4280 set_fill_string(set, "", 0);
4281 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004282 }
4283
4284 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004285 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004286
4287 item = &set->val.nodes[0];
4288 }
4289
4290 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004291 case LYXP_NODE_NONE:
4292 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004293 case LYXP_NODE_ROOT:
4294 case LYXP_NODE_ROOT_CONFIG:
4295 case LYXP_NODE_TEXT:
4296 set_fill_string(set, "", 0);
4297 break;
4298 case LYXP_NODE_ELEM:
Michal Vasko9f96a052020-03-10 09:41:45 +01004299 case LYXP_NODE_META:
Michal Vasko03ff5a72019-09-11 13:49:33 +02004300 if (item->type == LYXP_NODE_ELEM) {
4301 mod = item->node->schema->module;
Michal Vasko9f96a052020-03-10 09:41:45 +01004302 } else { /* LYXP_NODE_META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004303 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004304 mod = ((struct lyd_meta *)item->node)->annotation->module;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004305 }
4306
4307 set_fill_string(set, mod->ns, strlen(mod->ns));
4308 break;
4309 }
4310
4311 return LY_SUCCESS;
4312}
4313
4314/**
4315 * @brief Execute the XPath node() function (node type). Returns LYXP_SET_NODE_SET
4316 * with only nodes from the context. In practice it either leaves the context
4317 * as it is or returns an empty node set.
4318 *
4319 * @param[in] args Array of arguments.
4320 * @param[in] arg_count Count of elements in @p args.
4321 * @param[in,out] set Context and result set at the same time.
4322 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004323 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004324 */
4325static LY_ERR
4326xpath_node(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4327{
4328 if (options & LYXP_SCNODE_ALL) {
4329 set_scnode_clear_ctx(set);
4330 return LY_SUCCESS;
4331 }
4332
4333 if (set->type != LYXP_SET_NODE_SET) {
Michal Vaskod3678892020-05-21 10:06:58 +02004334 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004335 }
4336 return LY_SUCCESS;
4337}
4338
4339/**
4340 * @brief Execute the XPath normalize-space(string?) function. Returns LYXP_SET_STRING
4341 * with normalized value (no leading, trailing, double white spaces) of the node
4342 * from the argument or the context.
4343 *
4344 * @param[in] args Array of arguments.
4345 * @param[in] arg_count Count of elements in @p args.
4346 * @param[in,out] set Context and result set at the same time.
4347 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004348 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004349 */
4350static LY_ERR
4351xpath_normalize_space(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
4352{
4353 uint16_t i, new_used;
4354 char *new;
4355 int have_spaces = 0, space_before = 0;
4356 struct lysc_node_leaf *sleaf;
4357 LY_ERR rc = LY_SUCCESS;
4358
4359 if (options & LYXP_SCNODE_ALL) {
4360 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4361 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4362 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 +02004363 } else if (!warn_is_string_type(sleaf->type)) {
4364 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004365 }
4366 }
4367 set_scnode_clear_ctx(set);
4368 return rc;
4369 }
4370
4371 if (arg_count) {
4372 set_fill_set(set, args[0]);
4373 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004374 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004375 LY_CHECK_RET(rc);
4376
4377 /* is there any normalization necessary? */
4378 for (i = 0; set->val.str[i]; ++i) {
4379 if (is_xmlws(set->val.str[i])) {
4380 if ((i == 0) || space_before || (!set->val.str[i + 1])) {
4381 have_spaces = 1;
4382 break;
4383 }
4384 space_before = 1;
4385 } else {
4386 space_before = 0;
4387 }
4388 }
4389
4390 /* yep, there is */
4391 if (have_spaces) {
4392 /* it's enough, at least one character will go, makes space for ending '\0' */
4393 new = malloc(strlen(set->val.str) * sizeof(char));
4394 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4395 new_used = 0;
4396
4397 space_before = 0;
4398 for (i = 0; set->val.str[i]; ++i) {
4399 if (is_xmlws(set->val.str[i])) {
4400 if ((i == 0) || space_before) {
4401 space_before = 1;
4402 continue;
4403 } else {
4404 space_before = 1;
4405 }
4406 } else {
4407 space_before = 0;
4408 }
4409
4410 new[new_used] = (space_before ? ' ' : set->val.str[i]);
4411 ++new_used;
4412 }
4413
4414 /* at worst there is one trailing space now */
4415 if (new_used && is_xmlws(new[new_used - 1])) {
4416 --new_used;
4417 }
4418
4419 new = ly_realloc(new, (new_used + 1) * sizeof(char));
4420 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4421 new[new_used] = '\0';
4422
4423 free(set->val.str);
4424 set->val.str = new;
4425 }
4426
4427 return LY_SUCCESS;
4428}
4429
4430/**
4431 * @brief Execute the XPath not(boolean) function. Returns LYXP_SET_BOOLEAN
4432 * with the argument converted to boolean and logically inverted.
4433 *
4434 * @param[in] args Array of arguments.
4435 * @param[in] arg_count Count of elements in @p args.
4436 * @param[in,out] set Context and result set at the same time.
4437 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004438 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004439 */
4440static LY_ERR
4441xpath_not(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4442{
4443 if (options & LYXP_SCNODE_ALL) {
4444 set_scnode_clear_ctx(set);
4445 return LY_SUCCESS;
4446 }
4447
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004448 lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02004449 if (args[0]->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004450 set_fill_boolean(set, 0);
4451 } else {
4452 set_fill_boolean(set, 1);
4453 }
4454
4455 return LY_SUCCESS;
4456}
4457
4458/**
4459 * @brief Execute the XPath number(object?) function. Returns LYXP_SET_NUMBER
4460 * with the number representation of either the argument or the context.
4461 *
4462 * @param[in] args Array of arguments.
4463 * @param[in] arg_count Count of elements in @p args.
4464 * @param[in,out] set Context and result set at the same time.
4465 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004466 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004467 */
4468static LY_ERR
4469xpath_number(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
4470{
4471 LY_ERR rc;
4472
4473 if (options & LYXP_SCNODE_ALL) {
4474 set_scnode_clear_ctx(set);
4475 return LY_SUCCESS;
4476 }
4477
4478 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004479 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004480 LY_CHECK_RET(rc);
4481 set_fill_set(set, args[0]);
4482 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004483 rc = lyxp_set_cast(set, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004484 LY_CHECK_RET(rc);
4485 }
4486
4487 return LY_SUCCESS;
4488}
4489
4490/**
4491 * @brief Execute the XPath position() function. Returns LYXP_SET_NUMBER
4492 * with the context position.
4493 *
4494 * @param[in] args Array of arguments.
4495 * @param[in] arg_count Count of elements in @p args.
4496 * @param[in,out] set Context and result set at the same time.
4497 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004498 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004499 */
4500static LY_ERR
4501xpath_position(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4502{
4503 if (options & LYXP_SCNODE_ALL) {
4504 set_scnode_clear_ctx(set);
4505 return LY_SUCCESS;
4506 }
4507
Michal Vasko03ff5a72019-09-11 13:49:33 +02004508 if (set->type != LYXP_SET_NODE_SET) {
4509 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "position()");
4510 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004511 } else if (!set->used) {
4512 set_fill_number(set, 0);
4513 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004514 }
4515
4516 set_fill_number(set, set->ctx_pos);
4517
4518 /* UNUSED in 'Release' build type */
4519 (void)options;
4520 return LY_SUCCESS;
4521}
4522
4523/**
4524 * @brief Execute the YANG 1.1 re-match(string, string) function. Returns LYXP_SET_BOOLEAN
4525 * depending on whether the second argument regex matches the first argument string. For details refer to
4526 * YANG 1.1 RFC section 10.2.1.
4527 *
4528 * @param[in] args Array of arguments.
4529 * @param[in] arg_count Count of elements in @p args.
4530 * @param[in,out] set Context and result set at the same time.
4531 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004532 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004533 */
4534static LY_ERR
4535xpath_re_match(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4536{
4537 struct lysc_pattern **patterns = NULL, **pattern;
4538 struct lysc_node_leaf *sleaf;
4539 char *path;
4540 LY_ERR rc = LY_SUCCESS;
4541 struct ly_err_item *err;
4542
4543 if (options & LYXP_SCNODE_ALL) {
4544 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4545 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4546 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 +02004547 } else if (!warn_is_string_type(sleaf->type)) {
4548 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004549 }
4550 }
4551
4552 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4553 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4554 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 +02004555 } else if (!warn_is_string_type(sleaf->type)) {
4556 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004557 }
4558 }
4559 set_scnode_clear_ctx(set);
4560 return rc;
4561 }
4562
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004563 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004564 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004565 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004566 LY_CHECK_RET(rc);
4567
4568 LY_ARRAY_NEW_RET(set->ctx, patterns, pattern, LY_EMEM);
4569 *pattern = malloc(sizeof **pattern);
4570 path = lyd_path(set->ctx_node, LYD_PATH_LOG, NULL, 0);
4571 rc = lys_compile_type_pattern_check(set->ctx, path, args[1]->val.str, &(*pattern)->code);
4572 free(path);
4573 if (rc != LY_SUCCESS) {
4574 LY_ARRAY_FREE(patterns);
4575 return rc;
4576 }
4577
4578 rc = ly_type_validate_patterns(patterns, args[0]->val.str, strlen(args[0]->val.str), &err);
4579 pcre2_code_free((*pattern)->code);
4580 free(*pattern);
4581 LY_ARRAY_FREE(patterns);
4582 if (rc && (rc != LY_EVALID)) {
4583 ly_err_print(err);
4584 ly_err_free(err);
4585 return rc;
4586 }
4587
4588 if (rc == LY_EVALID) {
4589 ly_err_free(err);
4590 set_fill_boolean(set, 0);
4591 } else {
4592 set_fill_boolean(set, 1);
4593 }
4594
4595 return LY_SUCCESS;
4596}
4597
4598/**
4599 * @brief Execute the XPath round(number) function. Returns LYXP_SET_NUMBER
4600 * with the rounded first argument. For details refer to
4601 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-round.
4602 *
4603 * @param[in] args Array of arguments.
4604 * @param[in] arg_count Count of elements in @p args.
4605 * @param[in,out] set Context and result set at the same time.
4606 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004607 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004608 */
4609static LY_ERR
4610xpath_round(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4611{
4612 struct lysc_node_leaf *sleaf;
4613 LY_ERR rc = LY_SUCCESS;
4614
4615 if (options & LYXP_SCNODE_ALL) {
4616 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4617 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004618 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4619 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 +02004620 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
4621 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004622 }
4623 set_scnode_clear_ctx(set);
4624 return rc;
4625 }
4626
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004627 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004628 LY_CHECK_RET(rc);
4629
4630 /* cover only the cases where floor can't be used */
4631 if ((args[0]->val.num == -0.0f) || ((args[0]->val.num < 0) && (args[0]->val.num >= -0.5))) {
4632 set_fill_number(set, -0.0f);
4633 } else {
4634 args[0]->val.num += 0.5;
4635 rc = xpath_floor(args, 1, args[0], options);
4636 LY_CHECK_RET(rc);
4637 set_fill_number(set, args[0]->val.num);
4638 }
4639
4640 return LY_SUCCESS;
4641}
4642
4643/**
4644 * @brief Execute the XPath starts-with(string, string) function.
4645 * Returns LYXP_SET_BOOLEAN whether the second argument is
4646 * the prefix of the first or not.
4647 *
4648 * @param[in] args Array of arguments.
4649 * @param[in] arg_count Count of elements in @p args.
4650 * @param[in,out] set Context and result set at the same time.
4651 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004652 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004653 */
4654static LY_ERR
4655xpath_starts_with(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4656{
4657 struct lysc_node_leaf *sleaf;
4658 LY_ERR rc = LY_SUCCESS;
4659
4660 if (options & LYXP_SCNODE_ALL) {
4661 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4662 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4663 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 +02004664 } else if (!warn_is_string_type(sleaf->type)) {
4665 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004666 }
4667 }
4668
4669 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4670 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4671 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 +02004672 } else if (!warn_is_string_type(sleaf->type)) {
4673 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004674 }
4675 }
4676 set_scnode_clear_ctx(set);
4677 return rc;
4678 }
4679
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004680 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004681 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004682 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004683 LY_CHECK_RET(rc);
4684
4685 if (strncmp(args[0]->val.str, args[1]->val.str, strlen(args[1]->val.str))) {
4686 set_fill_boolean(set, 0);
4687 } else {
4688 set_fill_boolean(set, 1);
4689 }
4690
4691 return LY_SUCCESS;
4692}
4693
4694/**
4695 * @brief Execute the XPath string(object?) function. Returns LYXP_SET_STRING
4696 * with the string representation of either the argument or the context.
4697 *
4698 * @param[in] args Array of arguments.
4699 * @param[in] arg_count Count of elements in @p args.
4700 * @param[in,out] set Context and result set at the same time.
4701 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004702 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004703 */
4704static LY_ERR
4705xpath_string(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
4706{
4707 LY_ERR rc;
4708
4709 if (options & LYXP_SCNODE_ALL) {
4710 set_scnode_clear_ctx(set);
4711 return LY_SUCCESS;
4712 }
4713
4714 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004715 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004716 LY_CHECK_RET(rc);
4717 set_fill_set(set, args[0]);
4718 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004719 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004720 LY_CHECK_RET(rc);
4721 }
4722
4723 return LY_SUCCESS;
4724}
4725
4726/**
4727 * @brief Execute the XPath string-length(string?) function. Returns LYXP_SET_NUMBER
4728 * with the length of the string in either the argument or the context.
4729 *
4730 * @param[in] args Array of arguments.
4731 * @param[in] arg_count Count of elements in @p args.
4732 * @param[in,out] set Context and result set at the same time.
4733 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004734 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004735 */
4736static LY_ERR
4737xpath_string_length(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
4738{
4739 struct lysc_node_leaf *sleaf;
4740 LY_ERR rc = LY_SUCCESS;
4741
4742 if (options & LYXP_SCNODE_ALL) {
4743 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4744 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4745 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 +02004746 } else if (!warn_is_string_type(sleaf->type)) {
4747 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004748 }
4749 }
4750 if (!arg_count && (set->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set))) {
4751 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4752 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 +02004753 } else if (!warn_is_string_type(sleaf->type)) {
4754 LOGWRN(set->ctx, "Argument #0 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004755 }
4756 }
4757 set_scnode_clear_ctx(set);
4758 return rc;
4759 }
4760
4761 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004762 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004763 LY_CHECK_RET(rc);
4764 set_fill_number(set, strlen(args[0]->val.str));
4765 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004766 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004767 LY_CHECK_RET(rc);
4768 set_fill_number(set, strlen(set->val.str));
4769 }
4770
4771 return LY_SUCCESS;
4772}
4773
4774/**
4775 * @brief Execute the XPath substring(string, number, number?) function.
4776 * Returns LYXP_SET_STRING substring of the first argument starting
4777 * on the second argument index ending on the third argument index,
4778 * indexed from 1. For exact definition refer to
4779 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-substring.
4780 *
4781 * @param[in] args Array of arguments.
4782 * @param[in] arg_count Count of elements in @p args.
4783 * @param[in,out] set Context and result set at the same time.
4784 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004785 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004786 */
4787static LY_ERR
4788xpath_substring(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
4789{
4790 int start, len;
4791 uint16_t str_start, str_len, pos;
4792 struct lysc_node_leaf *sleaf;
4793 LY_ERR rc = LY_SUCCESS;
4794
4795 if (options & LYXP_SCNODE_ALL) {
4796 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4797 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4798 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 +02004799 } else if (!warn_is_string_type(sleaf->type)) {
4800 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004801 }
4802 }
4803
4804 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4805 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4806 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 +02004807 } else if (!warn_is_numeric_type(sleaf->type)) {
4808 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004809 }
4810 }
4811
4812 if ((arg_count == 3) && (args[2]->type == LYXP_SET_SCNODE_SET)
4813 && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
4814 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4815 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 +02004816 } else if (!warn_is_numeric_type(sleaf->type)) {
4817 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004818 }
4819 }
4820 set_scnode_clear_ctx(set);
4821 return rc;
4822 }
4823
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004824 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004825 LY_CHECK_RET(rc);
4826
4827 /* start */
4828 if (xpath_round(&args[1], 1, args[1], options)) {
4829 return -1;
4830 }
4831 if (isfinite(args[1]->val.num)) {
4832 start = args[1]->val.num - 1;
4833 } else if (isinf(args[1]->val.num) && signbit(args[1]->val.num)) {
4834 start = INT_MIN;
4835 } else {
4836 start = INT_MAX;
4837 }
4838
4839 /* len */
4840 if (arg_count == 3) {
4841 rc = xpath_round(&args[2], 1, args[2], options);
4842 LY_CHECK_RET(rc);
4843 if (isfinite(args[2]->val.num)) {
4844 len = args[2]->val.num;
4845 } else if (isnan(args[2]->val.num) || signbit(args[2]->val.num)) {
4846 len = 0;
4847 } else {
4848 len = INT_MAX;
4849 }
4850 } else {
4851 len = INT_MAX;
4852 }
4853
4854 /* find matching character positions */
4855 str_start = 0;
4856 str_len = 0;
4857 for (pos = 0; args[0]->val.str[pos]; ++pos) {
4858 if (pos < start) {
4859 ++str_start;
4860 } else if (pos < start + len) {
4861 ++str_len;
4862 } else {
4863 break;
4864 }
4865 }
4866
4867 set_fill_string(set, args[0]->val.str + str_start, str_len);
4868 return LY_SUCCESS;
4869}
4870
4871/**
4872 * @brief Execute the XPath substring-after(string, string) function.
4873 * Returns LYXP_SET_STRING with the string succeeding the occurance
4874 * of the second argument in the first or an empty string.
4875 *
4876 * @param[in] args Array of arguments.
4877 * @param[in] arg_count Count of elements in @p args.
4878 * @param[in,out] set Context and result set at the same time.
4879 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004880 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004881 */
4882static LY_ERR
4883xpath_substring_after(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4884{
4885 char *ptr;
4886 struct lysc_node_leaf *sleaf;
4887 LY_ERR rc = LY_SUCCESS;
4888
4889 if (options & LYXP_SCNODE_ALL) {
4890 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4891 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4892 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 +02004893 } else if (!warn_is_string_type(sleaf->type)) {
4894 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004895 }
4896 }
4897
4898 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4899 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4900 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 +02004901 } else if (!warn_is_string_type(sleaf->type)) {
4902 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004903 }
4904 }
4905 set_scnode_clear_ctx(set);
4906 return rc;
4907 }
4908
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004909 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004910 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004911 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004912 LY_CHECK_RET(rc);
4913
4914 ptr = strstr(args[0]->val.str, args[1]->val.str);
4915 if (ptr) {
4916 set_fill_string(set, ptr + strlen(args[1]->val.str), strlen(ptr + strlen(args[1]->val.str)));
4917 } else {
4918 set_fill_string(set, "", 0);
4919 }
4920
4921 return LY_SUCCESS;
4922}
4923
4924/**
4925 * @brief Execute the XPath substring-before(string, string) function.
4926 * Returns LYXP_SET_STRING with the string preceding the occurance
4927 * of the second argument in the first or an empty string.
4928 *
4929 * @param[in] args Array of arguments.
4930 * @param[in] arg_count Count of elements in @p args.
4931 * @param[in,out] set Context and result set at the same time.
4932 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004933 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004934 */
4935static LY_ERR
4936xpath_substring_before(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4937{
4938 char *ptr;
4939 struct lysc_node_leaf *sleaf;
4940 LY_ERR rc = LY_SUCCESS;
4941
4942 if (options & LYXP_SCNODE_ALL) {
4943 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4944 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4945 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 +02004946 } else if (!warn_is_string_type(sleaf->type)) {
4947 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004948 }
4949 }
4950
4951 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4952 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4953 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 +02004954 } else if (!warn_is_string_type(sleaf->type)) {
4955 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004956 }
4957 }
4958 set_scnode_clear_ctx(set);
4959 return rc;
4960 }
4961
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004962 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004963 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004964 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004965 LY_CHECK_RET(rc);
4966
4967 ptr = strstr(args[0]->val.str, args[1]->val.str);
4968 if (ptr) {
4969 set_fill_string(set, args[0]->val.str, ptr - args[0]->val.str);
4970 } else {
4971 set_fill_string(set, "", 0);
4972 }
4973
4974 return LY_SUCCESS;
4975}
4976
4977/**
4978 * @brief Execute the XPath sum(node-set) function. Returns LYXP_SET_NUMBER
4979 * with the sum of all the nodes in the context.
4980 *
4981 * @param[in] args Array of arguments.
4982 * @param[in] arg_count Count of elements in @p args.
4983 * @param[in,out] set Context and result set at the same time.
4984 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004985 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004986 */
4987static LY_ERR
4988xpath_sum(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4989{
4990 long double num;
4991 char *str;
4992 uint16_t i;
4993 struct lyxp_set set_item;
4994 struct lysc_node_leaf *sleaf;
4995 LY_ERR rc = LY_SUCCESS;
4996
4997 if (options & LYXP_SCNODE_ALL) {
4998 if (args[0]->type == LYXP_SET_SCNODE_SET) {
4999 for (i = 0; i < args[0]->used; ++i) {
5000 if (args[0]->val.scnodes[i].in_ctx == 1) {
5001 sleaf = (struct lysc_node_leaf *)args[0]->val.scnodes[i].scnode;
5002 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5003 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__,
5004 lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005005 } else if (!warn_is_numeric_type(sleaf->type)) {
5006 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005007 }
5008 }
5009 }
5010 }
5011 set_scnode_clear_ctx(set);
5012 return rc;
5013 }
5014
5015 set_fill_number(set, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005016
5017 if (args[0]->type != LYXP_SET_NODE_SET) {
5018 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "sum(node-set)");
5019 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02005020 } else if (!args[0]->used) {
5021 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005022 }
5023
Michal Vasko5c4e5892019-11-14 12:31:38 +01005024 set_init(&set_item, set);
5025
Michal Vasko03ff5a72019-09-11 13:49:33 +02005026 set_item.type = LYXP_SET_NODE_SET;
5027 set_item.val.nodes = malloc(sizeof *set_item.val.nodes);
5028 LY_CHECK_ERR_RET(!set_item.val.nodes, LOGMEM(set->ctx), LY_EMEM);
5029
5030 set_item.used = 1;
5031 set_item.size = 1;
5032
5033 for (i = 0; i < args[0]->used; ++i) {
5034 set_item.val.nodes[0] = args[0]->val.nodes[i];
5035
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005036 rc = cast_node_set_to_string(&set_item, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005037 LY_CHECK_RET(rc);
5038 num = cast_string_to_number(str);
5039 free(str);
5040 set->val.num += num;
5041 }
5042
5043 free(set_item.val.nodes);
5044
5045 return LY_SUCCESS;
5046}
5047
5048/**
5049 * @brief Execute the XPath text() function (node type). Returns LYXP_SET_NODE_SET
5050 * with the text content of the nodes in the context.
5051 *
5052 * @param[in] args Array of arguments.
5053 * @param[in] arg_count Count of elements in @p args.
5054 * @param[in,out] set Context and result set at the same time.
5055 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005056 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005057 */
5058static LY_ERR
5059xpath_text(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
5060{
5061 uint32_t i;
5062
5063 if (options & LYXP_SCNODE_ALL) {
5064 set_scnode_clear_ctx(set);
5065 return LY_SUCCESS;
5066 }
5067
Michal Vasko03ff5a72019-09-11 13:49:33 +02005068 if (set->type != LYXP_SET_NODE_SET) {
5069 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "text()");
5070 return LY_EVALID;
5071 }
5072
5073 for (i = 0; i < set->used;) {
5074 switch (set->val.nodes[i].type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01005075 case LYXP_NODE_NONE:
5076 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005077 case LYXP_NODE_ELEM:
Michal Vasko03ff5a72019-09-11 13:49:33 +02005078 if (set->val.nodes[i].node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
5079 set->val.nodes[i].type = LYXP_NODE_TEXT;
5080 ++i;
5081 break;
5082 }
5083 /* fall through */
5084 case LYXP_NODE_ROOT:
5085 case LYXP_NODE_ROOT_CONFIG:
5086 case LYXP_NODE_TEXT:
Michal Vasko9f96a052020-03-10 09:41:45 +01005087 case LYXP_NODE_META:
Michal Vasko03ff5a72019-09-11 13:49:33 +02005088 set_remove_node(set, i);
5089 break;
5090 }
5091 }
5092
5093 return LY_SUCCESS;
5094}
5095
5096/**
5097 * @brief Execute the XPath translate(string, string, string) function.
5098 * Returns LYXP_SET_STRING with the first argument with the characters
5099 * from the second argument replaced by those on the corresponding
5100 * positions in the third argument.
5101 *
5102 * @param[in] args Array of arguments.
5103 * @param[in] arg_count Count of elements in @p args.
5104 * @param[in,out] set Context and result set at the same time.
5105 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005106 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005107 */
5108static LY_ERR
5109xpath_translate(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
5110{
5111 uint16_t i, j, new_used;
5112 char *new;
5113 int found, have_removed;
5114 struct lysc_node_leaf *sleaf;
5115 LY_ERR rc = LY_SUCCESS;
5116
5117 if (options & LYXP_SCNODE_ALL) {
5118 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5119 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5120 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 +02005121 } else if (!warn_is_string_type(sleaf->type)) {
5122 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005123 }
5124 }
5125
5126 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5127 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5128 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 +02005129 } else if (!warn_is_string_type(sleaf->type)) {
5130 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005131 }
5132 }
5133
5134 if ((args[2]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
5135 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5136 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 +02005137 } else if (!warn_is_string_type(sleaf->type)) {
5138 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005139 }
5140 }
5141 set_scnode_clear_ctx(set);
5142 return rc;
5143 }
5144
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005145 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005146 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005147 rc = lyxp_set_cast(args[1], 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[2], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005150 LY_CHECK_RET(rc);
5151
5152 new = malloc((strlen(args[0]->val.str) + 1) * sizeof(char));
5153 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5154 new_used = 0;
5155
5156 have_removed = 0;
5157 for (i = 0; args[0]->val.str[i]; ++i) {
5158 found = 0;
5159
5160 for (j = 0; args[1]->val.str[j]; ++j) {
5161 if (args[0]->val.str[i] == args[1]->val.str[j]) {
5162 /* removing this char */
5163 if (j >= strlen(args[2]->val.str)) {
5164 have_removed = 1;
5165 found = 1;
5166 break;
5167 }
5168 /* replacing this char */
5169 new[new_used] = args[2]->val.str[j];
5170 ++new_used;
5171 found = 1;
5172 break;
5173 }
5174 }
5175
5176 /* copying this char */
5177 if (!found) {
5178 new[new_used] = args[0]->val.str[i];
5179 ++new_used;
5180 }
5181 }
5182
5183 if (have_removed) {
5184 new = ly_realloc(new, (new_used + 1) * sizeof(char));
5185 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5186 }
5187 new[new_used] = '\0';
5188
Michal Vaskod3678892020-05-21 10:06:58 +02005189 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005190 set->type = LYXP_SET_STRING;
5191 set->val.str = new;
5192
5193 return LY_SUCCESS;
5194}
5195
5196/**
5197 * @brief Execute the XPath true() function. Returns LYXP_SET_BOOLEAN
5198 * with true value.
5199 *
5200 * @param[in] args Array of arguments.
5201 * @param[in] arg_count Count of elements in @p args.
5202 * @param[in,out] set Context and result set at the same time.
5203 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005204 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005205 */
5206static LY_ERR
5207xpath_true(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
5208{
5209 if (options & LYXP_SCNODE_ALL) {
5210 set_scnode_clear_ctx(set);
5211 return LY_SUCCESS;
5212 }
5213
5214 set_fill_boolean(set, 1);
5215 return LY_SUCCESS;
5216}
5217
5218/*
5219 * moveto functions
5220 *
5221 * They and only they actually change the context (set).
5222 */
5223
5224/**
Michal Vasko6346ece2019-09-24 13:12:53 +02005225 * @brief Skip prefix and return corresponding model if there is a prefix. Logs directly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005226 *
Michal Vasko2104e9f2020-03-06 08:23:25 +01005227 * XPath @p set is expected to be a (sc)node set!
5228 *
Michal Vasko6346ece2019-09-24 13:12:53 +02005229 * @param[in,out] qname Qualified node name. If includes prefix, it is skipped.
5230 * @param[in,out] qname_len Length of @p qname, is updated accordingly.
5231 * @param[in] set Set with XPath context.
5232 * @param[out] moveto_mod Expected module of a matching node.
5233 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005234 */
Michal Vasko6346ece2019-09-24 13:12:53 +02005235static LY_ERR
5236moveto_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 +02005237{
Michal Vasko6346ece2019-09-24 13:12:53 +02005238 const struct lys_module *mod;
5239 const char *ptr;
5240 int pref_len;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005241 char *str;
5242
Michal Vasko2104e9f2020-03-06 08:23:25 +01005243 assert((set->type == LYXP_SET_NODE_SET) || (set->type == LYXP_SET_SCNODE_SET));
5244
Michal Vasko6346ece2019-09-24 13:12:53 +02005245 if ((ptr = ly_strnchr(*qname, ':', *qname_len))) {
5246 /* specific module */
5247 pref_len = ptr - *qname;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005248
Michal Vasko6346ece2019-09-24 13:12:53 +02005249 switch (set->format) {
Michal Vasko52927e22020-03-16 17:26:14 +01005250 case LYD_SCHEMA:
Michal Vasko6346ece2019-09-24 13:12:53 +02005251 /* schema, search all local module imports */
5252 mod = lys_module_find_prefix(set->local_mod, *qname, pref_len);
5253 break;
5254 case LYD_JSON:
5255 /* JSON data, search in context */
5256 str = strndup(*qname, pref_len);
Michal Vasko97e76fd2020-05-27 15:22:01 +02005257 mod = ly_ctx_get_module_implemented(set->ctx, str);
Michal Vasko6346ece2019-09-24 13:12:53 +02005258 free(str);
5259 break;
Michal Vasko52927e22020-03-16 17:26:14 +01005260 case LYD_XML:
Michal Vasko6346ece2019-09-24 13:12:53 +02005261 LOGINT_RET(set->ctx);
5262 }
5263
Michal Vasko004d3152020-06-11 19:59:22 +02005264 /* check for errors and non-implemented modules, as they are not valid */
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005265 if (!mod || !mod->implemented) {
Michal Vasko2104e9f2020-03-06 08:23:25 +01005266 if (set->type == LYXP_SET_SCNODE_SET) {
5267 LOGVAL(set->ctx, LY_VLOG_LYSC, set->ctx_scnode, LY_VCODE_XP_INMOD, pref_len, *qname);
5268 } else {
5269 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INMOD, pref_len, *qname);
5270 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005271 return LY_EVALID;
5272 }
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005273
Michal Vasko6346ece2019-09-24 13:12:53 +02005274 *qname += pref_len + 1;
5275 *qname_len -= pref_len + 1;
5276 } else if (((*qname)[0] == '*') && (*qname_len == 1)) {
5277 /* all modules - special case */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005278 mod = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02005279 } else if (set->type == LYXP_SET_SCNODE_SET) {
5280 /* current node module */
5281 mod = set->ctx_scnode->module;
Michal Vasko6346ece2019-09-24 13:12:53 +02005282 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02005283 /* current node module */
5284 mod = set->ctx_node->schema->module;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005285 }
5286
Michal Vasko6346ece2019-09-24 13:12:53 +02005287 *moveto_mod = mod;
5288 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005289}
5290
5291/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02005292 * @brief Move context @p set to the root. Handles absolute path.
5293 * Result is LYXP_SET_NODE_SET.
5294 *
5295 * @param[in,out] set Set to use.
5296 * @param[in] options Xpath options.
5297 */
5298static void
5299moveto_root(struct lyxp_set *set, int options)
5300{
Michal Vasko03ff5a72019-09-11 13:49:33 +02005301 if (!set) {
5302 return;
5303 }
5304
5305 if (options & LYXP_SCNODE_ALL) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005306 set_scnode_clear_ctx(set);
Michal Vaskoecd62de2019-11-13 12:35:11 +01005307 lyxp_set_scnode_insert_node(set, NULL, set->root_type);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005308 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005309 set->type = LYXP_SET_NODE_SET;
5310 set->used = 0;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005311 set_insert_node(set, NULL, 0, set->root_type, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005312 }
5313}
5314
5315/**
Michal Vaskoa1424542019-11-14 16:08:52 +01005316 * @brief Check whether a node has some unresolved "when".
5317 *
5318 * @param[in] node Node to check.
5319 * @return LY_ERR value (LY_EINCOMPLETE if there are some unresolved "when")
5320 */
5321static LY_ERR
5322moveto_when_check(const struct lyd_node *node)
5323{
5324 const struct lysc_node *schema;
5325
5326 if (!node) {
5327 return LY_SUCCESS;
5328 }
5329
5330 schema = node->schema;
5331 do {
5332 if (schema->when && !(node->flags & LYD_WHEN_TRUE)) {
5333 return LY_EINCOMPLETE;
5334 }
5335 schema = schema->parent;
5336 } while (schema && (schema->nodetype & (LYS_CASE | LYS_CHOICE)));
5337
5338 return LY_SUCCESS;
5339}
5340
5341/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02005342 * @brief Check @p node as a part of NameTest processing.
5343 *
5344 * @param[in] node Node to check.
5345 * @param[in] root_type XPath root node type.
Michal Vaskod3678892020-05-21 10:06:58 +02005346 * @param[in] node_name Node name in the dictionary to move to, NULL for any node.
5347 * @param[in] moveto_mod Expected module of the node, NULL for any.
Michal Vasko6346ece2019-09-24 13:12:53 +02005348 * @return LY_ERR (LY_ENOT if node does not match, LY_EINCOMPLETE on unresolved when,
5349 * LY_EINVAL if netither node nor any children match)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005350 */
5351static LY_ERR
5352moveto_node_check(const struct lyd_node *node, enum lyxp_node_type root_type, const char *node_name,
5353 const struct lys_module *moveto_mod)
5354{
5355 /* module check */
5356 if (moveto_mod && (node->schema->module != moveto_mod)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005357 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005358 }
5359
Michal Vasko5c4e5892019-11-14 12:31:38 +01005360 /* context check */
5361 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005362 return LY_EINVAL;
5363 }
5364
5365 /* name check */
Michal Vasko61ac2f62020-05-25 12:39:51 +02005366 if (node_name && strcmp(node_name, "*") && (node->schema->name != node_name)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005367 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005368 }
5369
Michal Vaskoa1424542019-11-14 16:08:52 +01005370 /* when check */
5371 if (moveto_when_check(node)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005372 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01005373 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005374
5375 /* match */
5376 return LY_SUCCESS;
5377}
5378
5379/**
5380 * @brief Check @p node as a part of schema NameTest processing.
5381 *
5382 * @param[in] node Schema node to check.
5383 * @param[in] root_type XPath root node type.
Michal Vaskod3678892020-05-21 10:06:58 +02005384 * @param[in] node_name Node name in the dictionary to move to, NULL for any nodes.
5385 * @param[in] moveto_mod Expected module of the node, NULL for any.
Michal Vasko6346ece2019-09-24 13:12:53 +02005386 * @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 +02005387 */
5388static LY_ERR
5389moveto_scnode_check(const struct lysc_node *node, enum lyxp_node_type root_type, const char *node_name,
Michal Vaskocafad9d2019-11-07 15:20:03 +01005390 const struct lys_module *moveto_mod)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005391{
Michal Vasko03ff5a72019-09-11 13:49:33 +02005392 /* module check */
Michal Vaskod3678892020-05-21 10:06:58 +02005393 if (moveto_mod && (node->module != moveto_mod)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005394 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005395 }
5396
5397 /* context check */
5398 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (node->flags & LYS_CONFIG_R)) {
5399 return LY_EINVAL;
5400 }
5401
5402 /* name check */
Michal Vasko61ac2f62020-05-25 12:39:51 +02005403 if (node_name && strcmp(node_name, "*") && (node->name != node_name)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005404 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005405 }
5406
5407 /* match */
5408 return LY_SUCCESS;
5409}
5410
5411/**
Michal Vaskod3678892020-05-21 10:06:58 +02005412 * @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 +02005413 *
5414 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005415 * @param[in] mod Matching node module, NULL for any.
5416 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005417 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5418 */
5419static LY_ERR
Michal Vaskod3678892020-05-21 10:06:58 +02005420moveto_node(struct lyxp_set *set, const struct lys_module *mod, const char *ncname)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005421{
Michal Vaskof03ed032020-03-04 13:31:44 +01005422 uint32_t i;
Michal Vasko6346ece2019-09-24 13:12:53 +02005423 int replaced;
Michal Vaskod3678892020-05-21 10:06:58 +02005424 const struct lyd_node *siblings, *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005425 LY_ERR rc;
5426
Michal Vaskod3678892020-05-21 10:06:58 +02005427 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005428 return LY_SUCCESS;
5429 }
5430
5431 if (set->type != LYXP_SET_NODE_SET) {
5432 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
5433 return LY_EVALID;
5434 }
5435
Michal Vasko03ff5a72019-09-11 13:49:33 +02005436 for (i = 0; i < set->used; ) {
5437 replaced = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02005438 siblings = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005439
5440 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 +01005441 assert(!set->val.nodes[i].node);
Michal Vaskod3678892020-05-21 10:06:58 +02005442
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005443 /* search in all the trees */
Michal Vaskod3678892020-05-21 10:06:58 +02005444 siblings = set->tree;
Michal Vasko5c4e5892019-11-14 12:31:38 +01005445 } else if (!(set->val.nodes[i].node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA))) {
Michal Vaskod3678892020-05-21 10:06:58 +02005446 /* search in children */
5447 siblings = lyd_node_children(set->val.nodes[i].node);
5448 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005449
Michal Vaskod3678892020-05-21 10:06:58 +02005450 for (sub = siblings; sub; sub = sub->next) {
5451 rc = moveto_node_check(sub, set->root_type, ncname, mod);
5452 if (rc == LY_SUCCESS) {
5453 if (!replaced) {
5454 set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i);
5455 replaced = 1;
5456 } else {
5457 set_insert_node(set, sub, 0, LYXP_NODE_ELEM, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005458 }
Michal Vaskod3678892020-05-21 10:06:58 +02005459 ++i;
5460 } else if (rc == LY_EINCOMPLETE) {
5461 return rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005462 }
5463 }
5464
5465 if (!replaced) {
5466 /* no match */
5467 set_remove_node(set, i);
5468 }
5469 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005470
5471 return LY_SUCCESS;
5472}
5473
5474/**
Michal Vaskod3678892020-05-21 10:06:58 +02005475 * @brief Move context @p set to a node using hashes. Result is LYXP_SET_NODE_SET (or LYXP_SET_EMPTY).
5476 * Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005477 *
5478 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005479 * @param[in] scnode Matching node schema.
Michal Vasko004d3152020-06-11 19:59:22 +02005480 * @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 +02005481 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5482 */
5483static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02005484moveto_node_hash(struct lyxp_set *set, const struct lysc_node *scnode, const struct ly_path_predicate *predicates)
Michal Vaskod3678892020-05-21 10:06:58 +02005485{
Michal Vasko004d3152020-06-11 19:59:22 +02005486 LY_ERR ret = LY_SUCCESS;
Michal Vaskod3678892020-05-21 10:06:58 +02005487 uint32_t i;
5488 int replaced;
5489 const struct lyd_node *siblings;
Michal Vasko004d3152020-06-11 19:59:22 +02005490 struct lyd_node *sub, *inst = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02005491
Michal Vasko004d3152020-06-11 19:59:22 +02005492 assert(scnode && (!(scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)) || predicates));
Michal Vaskod3678892020-05-21 10:06:58 +02005493
5494 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02005495 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005496 }
5497
5498 if (set->type != LYXP_SET_NODE_SET) {
5499 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 +02005500 ret = LY_EVALID;
5501 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005502 }
5503
5504 /* context check for all the nodes since we have the schema node */
5505 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) {
5506 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02005507 goto cleanup;
5508 }
5509
5510 /* create specific data instance if needed */
5511 if (scnode->nodetype == LYS_LIST) {
5512 LY_CHECK_GOTO(ret = lyd_create_list(scnode, predicates, &inst), cleanup);
5513 } else if (scnode->nodetype == LYS_LEAFLIST) {
5514 LY_CHECK_GOTO(ret = lyd_create_term2(scnode, &predicates[0].value, &inst), cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02005515 }
5516
5517 for (i = 0; i < set->used; ) {
5518 replaced = 0;
5519 siblings = NULL;
5520
5521 if ((set->val.nodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.nodes[i].type == LYXP_NODE_ROOT)) {
5522 assert(!set->val.nodes[i].node);
5523
5524 /* search in all the trees */
5525 siblings = set->tree;
5526 } else if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
5527 /* search in children */
5528 siblings = lyd_node_children(set->val.nodes[i].node);
5529 }
5530
5531 /* find the node using hashes */
Michal Vasko004d3152020-06-11 19:59:22 +02005532 if (inst) {
5533 lyd_find_sibling_first(siblings, inst, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02005534 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02005535 lyd_find_sibling_val(siblings, scnode, NULL, 0, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02005536 }
5537
5538 /* when check */
5539 if (sub && moveto_when_check(sub)) {
Michal Vasko004d3152020-06-11 19:59:22 +02005540 ret = LY_EINCOMPLETE;
5541 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005542 }
5543
5544 if (sub) {
5545 /* pos filled later */
5546 if (!replaced) {
5547 set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i);
5548 replaced = 1;
5549 } else {
5550 set_insert_node(set, sub, 0, LYXP_NODE_ELEM, i);
5551 }
5552 ++i;
5553 }
5554
5555 if (!replaced) {
5556 /* no match */
5557 set_remove_node(set, i);
5558 }
5559 }
5560
Michal Vasko004d3152020-06-11 19:59:22 +02005561cleanup:
5562 lyd_free_tree(inst);
5563 return ret;
Michal Vaskod3678892020-05-21 10:06:58 +02005564}
5565
5566/**
5567 * @brief Move context @p set to a schema node. Result is LYXP_SET_SCNODE_SET (or LYXP_SET_EMPTY).
5568 *
5569 * @param[in,out] set Set to use.
5570 * @param[in] mod Matching node module, NULL for any.
5571 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005572 * @param[in] options XPath options.
5573 * @return LY_ERR
5574 */
5575static LY_ERR
Michal Vaskod3678892020-05-21 10:06:58 +02005576moveto_scnode(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, int options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005577{
Michal Vaskocafad9d2019-11-07 15:20:03 +01005578 int i, orig_used, idx, temp_ctx = 0, getnext_opts;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005579 uint32_t mod_idx;
Michal Vasko519fd602020-05-26 12:17:39 +02005580 const struct lysc_node *iter, *start_parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005581
Michal Vaskod3678892020-05-21 10:06:58 +02005582 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005583 return LY_SUCCESS;
5584 }
5585
5586 if (set->type != LYXP_SET_SCNODE_SET) {
Michal Vaskof6e51882019-12-16 09:59:45 +01005587 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 +02005588 return LY_EVALID;
5589 }
5590
Michal Vaskocafad9d2019-11-07 15:20:03 +01005591 /* getnext opts */
5592 getnext_opts = LYS_GETNEXT_NOSTATECHECK;
5593 if (options & LYXP_SCNODE_OUTPUT) {
5594 getnext_opts |= LYS_GETNEXT_OUTPUT;
5595 }
5596
Michal Vasko03ff5a72019-09-11 13:49:33 +02005597 orig_used = set->used;
5598 for (i = 0; i < orig_used; ++i) {
5599 if (set->val.scnodes[i].in_ctx != 1) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01005600 if (set->val.scnodes[i].in_ctx != -2) {
5601 continue;
5602 }
5603
5604 /* remember context node */
5605 set->val.scnodes[i].in_ctx = -1;
Michal Vaskoec4df482019-12-16 10:02:18 +01005606 } else {
5607 set->val.scnodes[i].in_ctx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005608 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005609
5610 start_parent = set->val.scnodes[i].scnode;
5611
5612 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 +02005613 /* 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 +02005614 * so use it directly (root node itself is useless in this case) */
5615 mod_idx = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02005616 while (mod || (mod = (struct lys_module *)ly_ctx_get_module_iter(set->ctx, &mod_idx))) {
Michal Vasko519fd602020-05-26 12:17:39 +02005617 iter = NULL;
Michal Vasko509de4d2019-12-10 14:51:30 +01005618 /* module may not be implemented */
Michal Vasko519fd602020-05-26 12:17:39 +02005619 while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) {
5620 if (!moveto_scnode_check(iter, set->root_type, ncname, mod)) {
5621 idx = lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005622 /* we need to prevent these nodes from being considered in this moveto */
5623 if ((idx < orig_used) && (idx > i)) {
5624 set->val.scnodes[idx].in_ctx = 2;
5625 temp_ctx = 1;
5626 }
5627 }
5628 }
5629
5630 if (!mod_idx) {
Michal Vaskod3678892020-05-21 10:06:58 +02005631 /* mod was specified, we are not going through the whole context */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005632 break;
5633 }
5634 /* next iteration */
Michal Vaskod3678892020-05-21 10:06:58 +02005635 mod = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005636 }
5637
Michal Vasko519fd602020-05-26 12:17:39 +02005638 } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
5639 iter = NULL;
5640 while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) {
5641 if (!moveto_scnode_check(iter, set->root_type, ncname, (mod ? mod : set->local_mod))) {
5642 idx = lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005643 if ((idx < orig_used) && (idx > i)) {
5644 set->val.scnodes[idx].in_ctx = 2;
5645 temp_ctx = 1;
5646 }
5647 }
5648 }
5649 }
5650 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005651
5652 /* correct temporary in_ctx values */
5653 if (temp_ctx) {
5654 for (i = 0; i < orig_used; ++i) {
5655 if (set->val.scnodes[i].in_ctx == 2) {
5656 set->val.scnodes[i].in_ctx = 1;
5657 }
5658 }
5659 }
5660
5661 return LY_SUCCESS;
5662}
5663
5664/**
Michal Vaskod3678892020-05-21 10:06:58 +02005665 * @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 +02005666 * Context position aware.
5667 *
5668 * @param[in] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005669 * @param[in] mod Matching node module, NULL for any.
5670 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005671 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5672 */
5673static LY_ERR
Michal Vaskod3678892020-05-21 10:06:58 +02005674moveto_node_alldesc(struct lyxp_set *set, const struct lys_module *mod, const char *ncname)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005675{
5676 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005677 const struct lyd_node *next, *elem, *start;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005678 struct lyxp_set ret_set;
5679 LY_ERR rc;
5680
Michal Vaskod3678892020-05-21 10:06:58 +02005681 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005682 return LY_SUCCESS;
5683 }
5684
5685 if (set->type != LYXP_SET_NODE_SET) {
5686 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
5687 return LY_EVALID;
5688 }
5689
Michal Vasko9f96a052020-03-10 09:41:45 +01005690 /* 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 +02005691 rc = moveto_node(set, NULL, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005692 LY_CHECK_RET(rc);
5693
Michal Vasko6346ece2019-09-24 13:12:53 +02005694 /* this loop traverses all the nodes in the set and adds/keeps only those that match qname */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005695 set_init(&ret_set, set);
5696 for (i = 0; i < set->used; ++i) {
5697
5698 /* TREE DFS */
5699 start = set->val.nodes[i].node;
5700 for (elem = next = start; elem; elem = next) {
Michal Vaskod3678892020-05-21 10:06:58 +02005701 rc = moveto_node_check(elem, set->root_type, ncname, mod);
Michal Vasko6346ece2019-09-24 13:12:53 +02005702 if (!rc) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005703 /* add matching node into result set */
5704 set_insert_node(&ret_set, elem, 0, LYXP_NODE_ELEM, ret_set.used);
5705 if (set_dup_node_check(set, elem, LYXP_NODE_ELEM, i)) {
5706 /* the node is a duplicate, we'll process it later in the set */
5707 goto skip_children;
5708 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005709 } else if (rc == LY_EINCOMPLETE) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005710 return rc;
5711 } else if (rc == LY_EINVAL) {
5712 goto skip_children;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005713 }
5714
5715 /* TREE DFS NEXT ELEM */
5716 /* select element for the next run - children first */
Michal Vaskod3678892020-05-21 10:06:58 +02005717 next = lyd_node_children(elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005718 if (!next) {
5719skip_children:
5720 /* no children, so try siblings, but only if it's not the start,
5721 * that is considered to be the root and it's siblings are not traversed */
5722 if (elem != start) {
5723 next = elem->next;
5724 } else {
5725 break;
5726 }
5727 }
5728 while (!next) {
5729 /* no siblings, go back through the parents */
5730 if ((struct lyd_node *)elem->parent == start) {
5731 /* we are done, no next element to process */
5732 break;
5733 }
5734 /* parent is already processed, go to its sibling */
5735 elem = (struct lyd_node *)elem->parent;
5736 next = elem->next;
5737 }
5738 }
5739 }
5740
5741 /* make the temporary set the current one */
5742 ret_set.ctx_pos = set->ctx_pos;
5743 ret_set.ctx_size = set->ctx_size;
Michal Vaskod3678892020-05-21 10:06:58 +02005744 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005745 memcpy(set, &ret_set, sizeof *set);
5746
5747 return LY_SUCCESS;
5748}
5749
5750/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005751 * @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 +02005752 *
5753 * @param[in] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005754 * @param[in] mod Matching node module, NULL for any.
5755 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005756 * @param[in] options XPath options.
5757 * @return LY_ERR
5758 */
5759static LY_ERR
Michal Vaskod3678892020-05-21 10:06:58 +02005760moveto_scnode_alldesc(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, int options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005761{
Michal Vasko6346ece2019-09-24 13:12:53 +02005762 int i, orig_used, idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005763 const struct lysc_node *next, *elem, *start;
Michal Vasko6346ece2019-09-24 13:12:53 +02005764 LY_ERR rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005765
Michal Vaskod3678892020-05-21 10:06:58 +02005766 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005767 return LY_SUCCESS;
5768 }
5769
5770 if (set->type != LYXP_SET_SCNODE_SET) {
Michal Vaskof6e51882019-12-16 09:59:45 +01005771 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 +02005772 return LY_EVALID;
5773 }
5774
Michal Vasko03ff5a72019-09-11 13:49:33 +02005775 orig_used = set->used;
5776 for (i = 0; i < orig_used; ++i) {
5777 if (set->val.scnodes[i].in_ctx != 1) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01005778 if (set->val.scnodes[i].in_ctx != -2) {
5779 continue;
5780 }
5781
5782 /* remember context node */
5783 set->val.scnodes[i].in_ctx = -1;
Michal Vaskoec4df482019-12-16 10:02:18 +01005784 } else {
5785 set->val.scnodes[i].in_ctx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005786 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005787
5788 /* TREE DFS */
5789 start = set->val.scnodes[i].scnode;
5790 for (elem = next = start; elem; elem = next) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005791 if ((elem == start) || (elem->nodetype & (LYS_CHOICE | LYS_CASE))) {
5792 /* schema-only nodes, skip root */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005793 goto next_iter;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005794 }
5795
Michal Vaskod3678892020-05-21 10:06:58 +02005796 rc = moveto_scnode_check(elem, set->root_type, ncname, mod);
Michal Vasko6346ece2019-09-24 13:12:53 +02005797 if (!rc) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01005798 if ((idx = lyxp_set_scnode_dup_node_check(set, elem, LYXP_NODE_ELEM, i)) > -1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005799 set->val.scnodes[idx].in_ctx = 1;
5800 if (idx > i) {
5801 /* we will process it later in the set */
5802 goto skip_children;
5803 }
5804 } else {
Michal Vaskoecd62de2019-11-13 12:35:11 +01005805 lyxp_set_scnode_insert_node(set, elem, LYXP_NODE_ELEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005806 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005807 } else if (rc == LY_EINVAL) {
5808 goto skip_children;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005809 }
5810
5811next_iter:
5812 /* TREE DFS NEXT ELEM */
5813 /* select element for the next run - children first */
5814 next = lysc_node_children(elem, options & LYXP_SCNODE_OUTPUT ? LYS_CONFIG_R : LYS_CONFIG_W);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005815 if (!next) {
5816skip_children:
5817 /* no children, so try siblings, but only if it's not the start,
5818 * that is considered to be the root and it's siblings are not traversed */
5819 if (elem != start) {
5820 next = elem->next;
5821 } else {
5822 break;
5823 }
5824 }
5825 while (!next) {
5826 /* no siblings, go back through the parents */
5827 if (elem->parent == start) {
5828 /* we are done, no next element to process */
5829 break;
5830 }
5831 /* parent is already processed, go to its sibling */
5832 elem = elem->parent;
5833 next = elem->next;
5834 }
5835 }
5836 }
5837
5838 return LY_SUCCESS;
5839}
5840
5841/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005842 * @brief Move context @p set to an attribute. Result is LYXP_SET_NODE_SET.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005843 * Indirectly context position aware.
5844 *
5845 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005846 * @param[in] mod Matching metadata module, NULL for any.
5847 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005848 * @return LY_ERR
5849 */
5850static LY_ERR
Michal Vaskod3678892020-05-21 10:06:58 +02005851moveto_attr(struct lyxp_set *set, const struct lys_module *mod, const char *ncname)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005852{
5853 uint32_t i;
Michal Vaskod3678892020-05-21 10:06:58 +02005854 int replaced;
Michal Vasko9f96a052020-03-10 09:41:45 +01005855 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005856
Michal Vaskod3678892020-05-21 10:06:58 +02005857 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005858 return LY_SUCCESS;
5859 }
5860
5861 if (set->type != LYXP_SET_NODE_SET) {
5862 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
5863 return LY_EVALID;
5864 }
5865
Michal Vasko03ff5a72019-09-11 13:49:33 +02005866 for (i = 0; i < set->used; ) {
5867 replaced = 0;
5868
5869 /* only attributes of an elem (not dummy) can be in the result, skip all the rest;
5870 * our attributes are always qualified */
Michal Vasko5c4e5892019-11-14 12:31:38 +01005871 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01005872 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005873
5874 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02005875 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005876 continue;
5877 }
5878
Michal Vaskod3678892020-05-21 10:06:58 +02005879 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005880 /* match */
5881 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01005882 set->val.meta[i].meta = sub;
5883 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005884 /* pos does not change */
5885 replaced = 1;
5886 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01005887 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 +02005888 }
5889 ++i;
5890 }
5891 }
5892 }
5893
5894 if (!replaced) {
5895 /* no match */
5896 set_remove_node(set, i);
5897 }
5898 }
5899
5900 return LY_SUCCESS;
5901}
5902
5903/**
5904 * @brief Move context @p set1 to union with @p set2. @p set2 is emptied afterwards.
Michal Vasko61ac2f62020-05-25 12:39:51 +02005905 * Result is LYXP_SET_NODE_SET. Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005906 *
5907 * @param[in,out] set1 Set to use for the result.
5908 * @param[in] set2 Set that is copied to @p set1.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005909 * @return LY_ERR
5910 */
5911static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005912moveto_union(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005913{
5914 LY_ERR rc;
5915
Michal Vaskod3678892020-05-21 10:06:58 +02005916 if ((set1->type != LYXP_SET_NODE_SET) || (set2->type != LYXP_SET_NODE_SET)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005917 LOGVAL(set1->ctx, LY_VLOG_LYD, set1->ctx_node, LY_VCODE_XP_INOP_2, "union", print_set_type(set1), print_set_type(set2));
5918 return LY_EVALID;
5919 }
5920
5921 /* set2 is empty or both set1 and set2 */
Michal Vaskod3678892020-05-21 10:06:58 +02005922 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005923 return LY_SUCCESS;
5924 }
5925
Michal Vaskod3678892020-05-21 10:06:58 +02005926 if (!set1->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005927 memcpy(set1, set2, sizeof *set1);
5928 /* dynamic memory belongs to set1 now, do not free */
Michal Vaskod3678892020-05-21 10:06:58 +02005929 memset(set2, 0, sizeof *set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005930 return LY_SUCCESS;
5931 }
5932
5933 /* we assume sets are sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005934 assert(!set_sort(set1) && !set_sort(set2));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005935
5936 /* sort, remove duplicates */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005937 rc = set_sorted_merge(set1, set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005938 LY_CHECK_RET(rc);
5939
5940 /* final set must be sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005941 assert(!set_sort(set1));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005942
5943 return LY_SUCCESS;
5944}
5945
5946/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005947 * @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 +02005948 * Context position aware.
5949 *
5950 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005951 * @param[in] mod Matching metadata module, NULL for any.
5952 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005953 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5954 */
5955static int
Michal Vaskod3678892020-05-21 10:06:58 +02005956moveto_attr_alldesc(struct lyxp_set *set, const struct lys_module *mod, const char *ncname)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005957{
5958 uint32_t i;
Michal Vaskod3678892020-05-21 10:06:58 +02005959 int replaced;
Michal Vasko9f96a052020-03-10 09:41:45 +01005960 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005961 struct lyxp_set *set_all_desc = NULL;
5962 LY_ERR rc;
5963
Michal Vaskod3678892020-05-21 10:06:58 +02005964 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005965 return LY_SUCCESS;
5966 }
5967
5968 if (set->type != LYXP_SET_NODE_SET) {
5969 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
5970 return LY_EVALID;
5971 }
5972
Michal Vasko03ff5a72019-09-11 13:49:33 +02005973 /* can be optimized similarly to moveto_node_alldesc() and save considerable amount of memory,
5974 * but it likely won't be used much, so it's a waste of time */
5975 /* copy the context */
5976 set_all_desc = set_copy(set);
5977 /* get all descendant nodes (the original context nodes are removed) */
Michal Vaskod3678892020-05-21 10:06:58 +02005978 rc = moveto_node_alldesc(set_all_desc, NULL, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005979 if (rc != LY_SUCCESS) {
5980 lyxp_set_free(set_all_desc);
5981 return rc;
5982 }
5983 /* prepend the original context nodes */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005984 rc = moveto_union(set, set_all_desc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005985 if (rc != LY_SUCCESS) {
5986 lyxp_set_free(set_all_desc);
5987 return rc;
5988 }
5989 lyxp_set_free(set_all_desc);
5990
Michal Vasko03ff5a72019-09-11 13:49:33 +02005991 for (i = 0; i < set->used; ) {
5992 replaced = 0;
5993
5994 /* only attributes of an elem can be in the result, skip all the rest,
5995 * we have all attributes qualified in lyd tree */
5996 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01005997 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005998 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02005999 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006000 continue;
6001 }
6002
Michal Vaskod3678892020-05-21 10:06:58 +02006003 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006004 /* match */
6005 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01006006 set->val.meta[i].meta = sub;
6007 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006008 /* pos does not change */
6009 replaced = 1;
6010 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01006011 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 +02006012 }
6013 ++i;
6014 }
6015 }
6016 }
6017
6018 if (!replaced) {
6019 /* no match */
6020 set_remove_node(set, i);
6021 }
6022 }
6023
6024 return LY_SUCCESS;
6025}
6026
6027/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02006028 * @brief Move context @p set to self and al chilren, recursively. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET.
6029 * Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006030 *
6031 * @param[in] parent Current parent.
6032 * @param[in] parent_pos Position of @p parent.
6033 * @param[in] parent_type Node type of @p parent.
6034 * @param[in,out] to_set Set to use.
6035 * @param[in] dup_check_set Set for checking duplicities.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006036 * @param[in] options XPath options.
6037 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6038 */
6039static LY_ERR
6040moveto_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 +01006041 struct lyxp_set *to_set, const struct lyxp_set *dup_check_set, int options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006042{
Michal Vasko61ac2f62020-05-25 12:39:51 +02006043 const struct lyd_node *iter, *first;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006044 LY_ERR rc;
6045
6046 switch (parent_type) {
6047 case LYXP_NODE_ROOT:
6048 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko61ac2f62020-05-25 12:39:51 +02006049 assert(!parent);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006050
Michal Vasko61ac2f62020-05-25 12:39:51 +02006051 /* add all top-level nodes as elements */
6052 first = to_set->tree;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006053 break;
6054 case LYXP_NODE_ELEM:
Michal Vasko61ac2f62020-05-25 12:39:51 +02006055 /* add just the text node of this term element node */
6056 if (parent->schema->nodetype & (LYD_NODE_TERM | LYD_NODE_ANY)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006057 if (!set_dup_node_check(dup_check_set, parent, LYXP_NODE_TEXT, -1)) {
6058 set_insert_node(to_set, parent, parent_pos, LYXP_NODE_TEXT, to_set->used);
6059 }
Michal Vasko61ac2f62020-05-25 12:39:51 +02006060 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006061 }
Michal Vasko61ac2f62020-05-25 12:39:51 +02006062
6063 /* add all the children of this node */
6064 first = lyd_node_children(parent);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006065 break;
6066 default:
6067 LOGINT_RET(parent->schema->module->ctx);
6068 }
6069
Michal Vasko61ac2f62020-05-25 12:39:51 +02006070 /* add all top-level nodes as elements */
6071 LY_LIST_FOR(first, iter) {
6072 /* context check */
6073 if ((parent_type == LYXP_NODE_ROOT_CONFIG) && (iter->schema->flags & LYS_CONFIG_R)) {
6074 continue;
6075 }
6076
6077 /* when check */
6078 if (moveto_when_check(iter)) {
6079 return LY_EINCOMPLETE;
6080 }
6081
6082 if (!set_dup_node_check(dup_check_set, iter, LYXP_NODE_ELEM, -1)) {
6083 set_insert_node(to_set, iter, 0, LYXP_NODE_ELEM, to_set->used);
6084
6085 /* also add all the children of this node, recursively */
6086 rc = moveto_self_add_children_r(iter, 0, LYXP_NODE_ELEM, to_set, dup_check_set, options);
6087 LY_CHECK_RET(rc);
6088 }
6089 }
6090
Michal Vasko03ff5a72019-09-11 13:49:33 +02006091 return LY_SUCCESS;
6092}
6093
6094/**
6095 * @brief Move context @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET
6096 * (or LYXP_SET_EMPTY). Context position aware.
6097 *
6098 * @param[in,out] set Set to use.
6099 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6100 * @param[in] options XPath options.
6101 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6102 */
6103static LY_ERR
6104moveto_self(struct lyxp_set *set, int all_desc, int options)
6105{
6106 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006107 struct lyxp_set ret_set;
6108 LY_ERR rc;
6109
Michal Vaskod3678892020-05-21 10:06:58 +02006110 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006111 return LY_SUCCESS;
6112 }
6113
6114 if (set->type != LYXP_SET_NODE_SET) {
6115 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
6116 return LY_EVALID;
6117 }
6118
6119 /* nothing to do */
6120 if (!all_desc) {
6121 return LY_SUCCESS;
6122 }
6123
Michal Vasko03ff5a72019-09-11 13:49:33 +02006124 /* add all the children, they get added recursively */
6125 set_init(&ret_set, set);
6126 for (i = 0; i < set->used; ++i) {
6127 /* copy the current node to tmp */
6128 set_insert_node(&ret_set, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, ret_set.used);
6129
6130 /* do not touch attributes and text nodes */
Michal Vasko9f96a052020-03-10 09:41:45 +01006131 if ((set->val.nodes[i].type == LYXP_NODE_TEXT) || (set->val.nodes[i].type == LYXP_NODE_META)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006132 continue;
6133 }
6134
Michal Vasko03ff5a72019-09-11 13:49:33 +02006135 /* add all the children */
6136 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 +01006137 set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006138 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006139 lyxp_set_free_content(&ret_set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006140 return rc;
6141 }
6142 }
6143
6144 /* use the temporary set as the current one */
6145 ret_set.ctx_pos = set->ctx_pos;
6146 ret_set.ctx_size = set->ctx_size;
Michal Vaskod3678892020-05-21 10:06:58 +02006147 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006148 memcpy(set, &ret_set, sizeof *set);
6149
6150 return LY_SUCCESS;
6151}
6152
6153/**
6154 * @brief Move context schema @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_SCNODE_SET
6155 * (or LYXP_SET_EMPTY).
6156 *
6157 * @param[in,out] set Set to use.
6158 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6159 * @param[in] options XPath options.
6160 * @return LY_ERR
6161 */
6162static LY_ERR
6163moveto_scnode_self(struct lyxp_set *set, int all_desc, int options)
6164{
Michal Vasko519fd602020-05-26 12:17:39 +02006165 int getnext_opts;
6166 uint32_t i, mod_idx;
6167 const struct lysc_node *iter, *start_parent;
6168 const struct lys_module *mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006169
Michal Vaskod3678892020-05-21 10:06:58 +02006170 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006171 return LY_SUCCESS;
6172 }
6173
6174 if (set->type != LYXP_SET_SCNODE_SET) {
Michal Vaskof6e51882019-12-16 09:59:45 +01006175 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 +02006176 return LY_EVALID;
6177 }
6178
6179 /* nothing to do */
6180 if (!all_desc) {
6181 return LY_SUCCESS;
6182 }
6183
Michal Vasko519fd602020-05-26 12:17:39 +02006184 /* getnext opts */
6185 getnext_opts = LYS_GETNEXT_NOSTATECHECK;
6186 if (options & LYXP_SCNODE_OUTPUT) {
6187 getnext_opts |= LYS_GETNEXT_OUTPUT;
6188 }
6189
6190 /* add all the children, recursively as they are being added into the same set */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006191 for (i = 0; i < set->used; ++i) {
6192 if (set->val.scnodes[i].in_ctx != 1) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006193 if (set->val.scnodes[i].in_ctx != -2) {
6194 continue;
6195 }
6196
Michal Vasko519fd602020-05-26 12:17:39 +02006197 /* remember context node */
6198 set->val.scnodes[i].in_ctx = -1;
6199 } else {
6200 set->val.scnodes[i].in_ctx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006201 }
6202
Michal Vasko519fd602020-05-26 12:17:39 +02006203 start_parent = set->val.scnodes[i].scnode;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006204
Michal Vasko519fd602020-05-26 12:17:39 +02006205 if ((set->val.scnodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.scnodes[i].type == LYXP_NODE_ROOT)) {
6206 /* it can actually be in any module, it's all <running> */
6207 mod_idx = 0;
6208 while ((mod = (struct lys_module *)ly_ctx_get_module_iter(set->ctx, &mod_idx))) {
6209 iter = NULL;
6210 /* module may not be implemented */
6211 while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) {
6212 /* context check */
6213 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) {
6214 continue;
6215 }
6216
6217 lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM);
6218 /* throw away the insert index, we want to consider that node again, recursively */
6219 }
6220 }
6221
6222 } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
6223 iter = NULL;
6224 while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006225 /* context check */
Michal Vasko519fd602020-05-26 12:17:39 +02006226 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006227 continue;
6228 }
6229
Michal Vasko519fd602020-05-26 12:17:39 +02006230 lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006231 }
6232 }
6233 }
6234
6235 return LY_SUCCESS;
6236}
6237
6238/**
6239 * @brief Move context @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_NODE_SET
6240 * (or LYXP_SET_EMPTY). Context position aware.
6241 *
6242 * @param[in] set Set to use.
6243 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6244 * @param[in] options XPath options.
6245 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6246 */
6247static LY_ERR
6248moveto_parent(struct lyxp_set *set, int all_desc, int options)
6249{
6250 LY_ERR rc;
6251 uint32_t i;
6252 struct lyd_node *node, *new_node;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006253 enum lyxp_node_type new_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006254
Michal Vaskod3678892020-05-21 10:06:58 +02006255 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006256 return LY_SUCCESS;
6257 }
6258
6259 if (set->type != LYXP_SET_NODE_SET) {
6260 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
6261 return LY_EVALID;
6262 }
6263
6264 if (all_desc) {
6265 /* <path>//.. == <path>//./.. */
6266 rc = moveto_self(set, 1, options);
6267 LY_CHECK_RET(rc);
6268 }
6269
Michal Vasko57eab132019-09-24 11:46:26 +02006270 for (i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006271 node = set->val.nodes[i].node;
6272
6273 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
6274 new_node = (struct lyd_node *)node->parent;
6275 } else if (set->val.nodes[i].type == LYXP_NODE_TEXT) {
6276 new_node = node;
Michal Vasko9f96a052020-03-10 09:41:45 +01006277 } else if (set->val.nodes[i].type == LYXP_NODE_META) {
6278 new_node = set->val.meta[i].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006279 if (!new_node) {
6280 LOGINT_RET(set->ctx);
6281 }
6282 } else {
6283 /* root does not have a parent */
Michal Vasko2caefc12019-11-14 16:07:56 +01006284 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006285 continue;
6286 }
6287
Michal Vaskoa1424542019-11-14 16:08:52 +01006288 /* when check */
6289 if (moveto_when_check(new_node)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006290 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01006291 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006292
6293 /* node already there can also be the root */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006294 if (!new_node) {
6295 new_type = set->root_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006296
6297 /* node has a standard parent (it can equal the root, it's not the root yet since they are fake) */
6298 } else {
6299 new_type = LYXP_NODE_ELEM;
6300 }
6301
Michal Vasko03ff5a72019-09-11 13:49:33 +02006302 if (set_dup_node_check(set, new_node, new_type, -1)) {
Michal Vasko2caefc12019-11-14 16:07:56 +01006303 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006304 } else {
6305 set_replace_node(set, new_node, 0, new_type, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006306 }
6307 }
6308
Michal Vasko2caefc12019-11-14 16:07:56 +01006309 set_remove_nodes_none(set);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006310 assert(!set_sort(set) && !set_sorted_dup_node_clean(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006311
6312 return LY_SUCCESS;
6313}
6314
6315/**
6316 * @brief Move context schema @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_SCNODE_SET
6317 * (or LYXP_SET_EMPTY).
6318 *
6319 * @param[in] set Set to use.
6320 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6321 * @param[in] options XPath options.
6322 * @return LY_ERR
6323 */
6324static LY_ERR
6325moveto_scnode_parent(struct lyxp_set *set, int all_desc, int options)
6326{
6327 int idx, i, orig_used, temp_ctx = 0;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006328 const struct lysc_node *node, *new_node;
6329 enum lyxp_node_type new_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006330 LY_ERR rc;
6331
Michal Vaskod3678892020-05-21 10:06:58 +02006332 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006333 return LY_SUCCESS;
6334 }
6335
6336 if (set->type != LYXP_SET_SCNODE_SET) {
Michal Vaskof6e51882019-12-16 09:59:45 +01006337 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 +02006338 return LY_EVALID;
6339 }
6340
6341 if (all_desc) {
6342 /* <path>//.. == <path>//./.. */
6343 rc = moveto_scnode_self(set, 1, options);
6344 LY_CHECK_RET(rc);
6345 }
6346
Michal Vasko03ff5a72019-09-11 13:49:33 +02006347 orig_used = set->used;
6348 for (i = 0; i < orig_used; ++i) {
6349 if (set->val.scnodes[i].in_ctx != 1) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006350 if (set->val.scnodes[i].in_ctx != -2) {
6351 continue;
6352 }
6353
6354 /* remember context node */
6355 set->val.scnodes[i].in_ctx = -1;
Michal Vaskoec4df482019-12-16 10:02:18 +01006356 } else {
6357 set->val.scnodes[i].in_ctx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006358 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006359
6360 node = set->val.scnodes[i].scnode;
6361
6362 if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
Michal Vaskod3678892020-05-21 10:06:58 +02006363 new_node = lysc_data_parent(node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006364 } else {
6365 /* root does not have a parent */
6366 continue;
6367 }
6368
Michal Vasko03ff5a72019-09-11 13:49:33 +02006369 /* node has no parent */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006370 if (!new_node) {
6371 new_type = set->root_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006372
6373 /* node has a standard parent (it can equal the root, it's not the root yet since they are fake) */
6374 } else {
6375 new_type = LYXP_NODE_ELEM;
6376 }
6377
Michal Vaskoecd62de2019-11-13 12:35:11 +01006378 idx = lyxp_set_scnode_insert_node(set, new_node, new_type);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006379 if ((idx < orig_used) && (idx > i)) {
6380 set->val.scnodes[idx].in_ctx = 2;
6381 temp_ctx = 1;
6382 }
6383 }
6384
6385 if (temp_ctx) {
6386 for (i = 0; i < orig_used; ++i) {
6387 if (set->val.scnodes[i].in_ctx == 2) {
6388 set->val.scnodes[i].in_ctx = 1;
6389 }
6390 }
6391 }
6392
6393 return LY_SUCCESS;
6394}
6395
6396/**
6397 * @brief Move context @p set to the result of a comparison. Handles '=', '!=', '<=', '<', '>=', or '>'.
6398 * Result is LYXP_SET_BOOLEAN. Indirectly context position aware.
6399 *
6400 * @param[in,out] set1 Set to use for the result.
6401 * @param[in] set2 Set acting as the second operand for @p op.
6402 * @param[in] op Comparison operator to process.
6403 * @param[in] options XPath options.
6404 * @return LY_ERR
6405 */
6406static LY_ERR
6407moveto_op_comp(struct lyxp_set *set1, struct lyxp_set *set2, const char *op, int options)
6408{
6409 /*
6410 * NODE SET + NODE SET = NODE SET + STRING /(1 NODE SET) 2 STRING
6411 * NODE SET + STRING = STRING + STRING /1 STRING (2 STRING)
6412 * NODE SET + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6413 * NODE SET + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6414 * STRING + NODE SET = STRING + STRING /(1 STRING) 2 STRING
6415 * NUMBER + NODE SET = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6416 * BOOLEAN + NODE SET = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6417 *
6418 * '=' or '!='
6419 * BOOLEAN + BOOLEAN
6420 * BOOLEAN + STRING = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6421 * BOOLEAN + NUMBER = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6422 * STRING + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6423 * NUMBER + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6424 * NUMBER + NUMBER
6425 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6426 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6427 * STRING + STRING
6428 *
6429 * '<=', '<', '>=', '>'
6430 * NUMBER + NUMBER
6431 * BOOLEAN + BOOLEAN = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6432 * BOOLEAN + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6433 * BOOLEAN + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6434 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6435 * STRING + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6436 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6437 * NUMBER + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6438 * STRING + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6439 */
6440 struct lyxp_set iter1, iter2;
6441 int result;
6442 int64_t i;
6443 LY_ERR rc;
6444
Michal Vasko004d3152020-06-11 19:59:22 +02006445 memset(&iter1, 0, sizeof iter1);
6446 memset(&iter2, 0, sizeof iter2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006447
6448 /* iterative evaluation with node-sets */
6449 if ((set1->type == LYXP_SET_NODE_SET) || (set2->type == LYXP_SET_NODE_SET)) {
6450 if (set1->type == LYXP_SET_NODE_SET) {
6451 for (i = 0; i < set1->used; ++i) {
6452 switch (set2->type) {
6453 case LYXP_SET_NUMBER:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006454 rc = set_comp_cast(&iter1, set1, LYXP_SET_NUMBER, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006455 break;
6456 case LYXP_SET_BOOLEAN:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006457 rc = set_comp_cast(&iter1, set1, LYXP_SET_BOOLEAN, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006458 break;
6459 default:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006460 rc = set_comp_cast(&iter1, set1, LYXP_SET_STRING, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006461 break;
6462 }
6463 LY_CHECK_RET(rc);
6464
6465 rc = moveto_op_comp(&iter1, set2, op, options);
6466 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006467 lyxp_set_free_content(&iter1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006468 return rc;
6469 }
6470
6471 /* lazy evaluation until true */
Michal Vasko004d3152020-06-11 19:59:22 +02006472 if (iter1.val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006473 set_fill_boolean(set1, 1);
6474 return LY_SUCCESS;
6475 }
6476 }
6477 } else {
6478 for (i = 0; i < set2->used; ++i) {
6479 switch (set1->type) {
6480 case LYXP_SET_NUMBER:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006481 rc = set_comp_cast(&iter2, set2, LYXP_SET_NUMBER, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006482 break;
6483 case LYXP_SET_BOOLEAN:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006484 rc = set_comp_cast(&iter2, set2, LYXP_SET_BOOLEAN, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006485 break;
6486 default:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006487 rc = set_comp_cast(&iter2, set2, LYXP_SET_STRING, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006488 break;
6489 }
6490 LY_CHECK_RET(rc);
6491
6492 set_fill_set(&iter1, set1);
6493
6494 rc = moveto_op_comp(&iter1, &iter2, op, options);
6495 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006496 lyxp_set_free_content(&iter1);
6497 lyxp_set_free_content(&iter2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006498 return rc;
6499 }
Michal Vaskod3678892020-05-21 10:06:58 +02006500 lyxp_set_free_content(&iter2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006501
6502 /* lazy evaluation until true */
Michal Vasko004d3152020-06-11 19:59:22 +02006503 if (iter1.val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006504 set_fill_boolean(set1, 1);
6505 return LY_SUCCESS;
6506 }
6507 }
6508 }
6509
6510 /* false for all nodes */
6511 set_fill_boolean(set1, 0);
6512 return LY_SUCCESS;
6513 }
6514
6515 /* first convert properly */
6516 if ((op[0] == '=') || (op[0] == '!')) {
6517 if ((set1->type == LYXP_SET_BOOLEAN) || (set2->type == LYXP_SET_BOOLEAN)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006518 lyxp_set_cast(set1, LYXP_SET_BOOLEAN);
6519 lyxp_set_cast(set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006520 } else if ((set1->type == LYXP_SET_NUMBER) || (set2->type == LYXP_SET_NUMBER)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006521 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006522 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006523 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006524 LY_CHECK_RET(rc);
6525 } /* else we have 2 strings */
6526 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006527 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006528 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006529 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006530 LY_CHECK_RET(rc);
6531 }
6532
6533 assert(set1->type == set2->type);
6534
6535 /* compute result */
6536 if (op[0] == '=') {
6537 if (set1->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02006538 result = (set1->val.bln == set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006539 } else if (set1->type == LYXP_SET_NUMBER) {
6540 result = (set1->val.num == set2->val.num);
6541 } else {
6542 assert(set1->type == LYXP_SET_STRING);
Michal Vaskoac6c72f2019-11-14 16:09:34 +01006543 result = !strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006544 }
6545 } else if (op[0] == '!') {
6546 if (set1->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02006547 result = (set1->val.bln != set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006548 } else if (set1->type == LYXP_SET_NUMBER) {
6549 result = (set1->val.num != set2->val.num);
6550 } else {
6551 assert(set1->type == LYXP_SET_STRING);
Michal Vaskoac6c72f2019-11-14 16:09:34 +01006552 result = !strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006553 }
6554 } else {
6555 assert(set1->type == LYXP_SET_NUMBER);
6556 if (op[0] == '<') {
6557 if (op[1] == '=') {
6558 result = (set1->val.num <= set2->val.num);
6559 } else {
6560 result = (set1->val.num < set2->val.num);
6561 }
6562 } else {
6563 if (op[1] == '=') {
6564 result = (set1->val.num >= set2->val.num);
6565 } else {
6566 result = (set1->val.num > set2->val.num);
6567 }
6568 }
6569 }
6570
6571 /* assign result */
6572 if (result) {
6573 set_fill_boolean(set1, 1);
6574 } else {
6575 set_fill_boolean(set1, 0);
6576 }
6577
6578 return LY_SUCCESS;
6579}
6580
6581/**
6582 * @brief Move context @p set to the result of a basic operation. Handles '+', '-', unary '-', '*', 'div',
6583 * or 'mod'. Result is LYXP_SET_NUMBER. Indirectly context position aware.
6584 *
6585 * @param[in,out] set1 Set to use for the result.
6586 * @param[in] set2 Set acting as the second operand for @p op.
6587 * @param[in] op Operator to process.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006588 * @return LY_ERR
6589 */
6590static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006591moveto_op_math(struct lyxp_set *set1, struct lyxp_set *set2, const char *op)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006592{
6593 LY_ERR rc;
6594
6595 /* unary '-' */
6596 if (!set2 && (op[0] == '-')) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006597 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006598 LY_CHECK_RET(rc);
6599 set1->val.num *= -1;
6600 lyxp_set_free(set2);
6601 return LY_SUCCESS;
6602 }
6603
6604 assert(set1 && set2);
6605
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006606 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006607 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006608 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006609 LY_CHECK_RET(rc);
6610
6611 switch (op[0]) {
6612 /* '+' */
6613 case '+':
6614 set1->val.num += set2->val.num;
6615 break;
6616
6617 /* '-' */
6618 case '-':
6619 set1->val.num -= set2->val.num;
6620 break;
6621
6622 /* '*' */
6623 case '*':
6624 set1->val.num *= set2->val.num;
6625 break;
6626
6627 /* 'div' */
6628 case 'd':
6629 set1->val.num /= set2->val.num;
6630 break;
6631
6632 /* 'mod' */
6633 case 'm':
6634 set1->val.num = ((long long)set1->val.num) % ((long long)set2->val.num);
6635 break;
6636
6637 default:
6638 LOGINT_RET(set1->ctx);
6639 }
6640
6641 return LY_SUCCESS;
6642}
6643
6644/*
6645 * eval functions
6646 *
6647 * They execute a parsed XPath expression on some data subtree.
6648 */
6649
6650/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02006651 * @brief Evaluate Predicate. Logs directly on error.
6652 *
Michal Vaskod3678892020-05-21 10:06:58 +02006653 * [9] Predicate ::= '[' Expr ']'
Michal Vasko03ff5a72019-09-11 13:49:33 +02006654 *
6655 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02006656 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006657 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
6658 * @param[in] options XPath options.
6659 * @param[in] parent_pos_pred Whether parent predicate was a positional one.
6660 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6661 */
6662static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02006663eval_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 +02006664{
6665 LY_ERR rc;
Michal Vasko57eab132019-09-24 11:46:26 +02006666 uint16_t i, orig_exp;
Michal Vasko5c4e5892019-11-14 12:31:38 +01006667 uint32_t orig_pos, orig_size;
6668 int32_t pred_in_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006669 struct lyxp_set set2;
6670 struct lyd_node *orig_parent;
6671
6672 /* '[' */
6673 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02006674 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
6675 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006676
6677 if (!set) {
6678only_parse:
Michal Vasko004d3152020-06-11 19:59:22 +02006679 rc = eval_expr_select(exp, tok_idx, 0, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006680 LY_CHECK_RET(rc);
6681 } else if (set->type == LYXP_SET_NODE_SET) {
6682 /* 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 +01006683 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006684
6685 /* empty set, nothing to evaluate */
6686 if (!set->used) {
6687 goto only_parse;
6688 }
6689
Michal Vasko004d3152020-06-11 19:59:22 +02006690 orig_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006691 orig_pos = 0;
6692 orig_size = set->used;
6693 orig_parent = NULL;
Michal Vasko39dbf352020-05-21 10:08:59 +02006694 for (i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006695 set_init(&set2, set);
6696 set_insert_node(&set2, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, 0);
6697 /* remember the node context position for position() and context size for last(),
6698 * predicates should always be evaluated with respect to the child axis (since we do
6699 * not support explicit axes) so we assign positions based on their parents */
6700 if (parent_pos_pred && ((struct lyd_node *)set->val.nodes[i].node->parent != orig_parent)) {
6701 orig_parent = (struct lyd_node *)set->val.nodes[i].node->parent;
6702 orig_pos = 1;
6703 } else {
6704 ++orig_pos;
6705 }
6706
6707 set2.ctx_pos = orig_pos;
6708 set2.ctx_size = orig_size;
Michal Vasko004d3152020-06-11 19:59:22 +02006709 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006710
Michal Vasko004d3152020-06-11 19:59:22 +02006711 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006712 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006713 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006714 return rc;
6715 }
6716
6717 /* number is a position */
6718 if (set2.type == LYXP_SET_NUMBER) {
6719 if ((long long)set2.val.num == orig_pos) {
6720 set2.val.num = 1;
6721 } else {
6722 set2.val.num = 0;
6723 }
6724 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006725 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006726
6727 /* predicate satisfied or not? */
Michal Vasko004d3152020-06-11 19:59:22 +02006728 if (!set2.val.bln) {
Michal Vasko2caefc12019-11-14 16:07:56 +01006729 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006730 }
6731 }
Michal Vasko2caefc12019-11-14 16:07:56 +01006732 set_remove_nodes_none(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006733
6734 } else if (set->type == LYXP_SET_SCNODE_SET) {
6735 for (i = 0; i < set->used; ++i) {
6736 if (set->val.scnodes[i].in_ctx == 1) {
6737 /* there is a currently-valid node */
6738 break;
6739 }
6740 }
6741 /* empty set, nothing to evaluate */
6742 if (i == set->used) {
6743 goto only_parse;
6744 }
6745
Michal Vasko004d3152020-06-11 19:59:22 +02006746 orig_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006747
Michal Vasko03ff5a72019-09-11 13:49:33 +02006748 /* set special in_ctx to all the valid snodes */
6749 pred_in_ctx = set_scnode_new_in_ctx(set);
6750
6751 /* use the valid snodes one-by-one */
6752 for (i = 0; i < set->used; ++i) {
6753 if (set->val.scnodes[i].in_ctx != pred_in_ctx) {
6754 continue;
6755 }
6756 set->val.scnodes[i].in_ctx = 1;
6757
Michal Vasko004d3152020-06-11 19:59:22 +02006758 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006759
Michal Vasko004d3152020-06-11 19:59:22 +02006760 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006761 LY_CHECK_RET(rc);
6762
6763 set->val.scnodes[i].in_ctx = pred_in_ctx;
6764 }
6765
6766 /* restore the state as it was before the predicate */
6767 for (i = 0; i < set->used; ++i) {
6768 if (set->val.scnodes[i].in_ctx == 1) {
6769 set->val.scnodes[i].in_ctx = 0;
6770 } else if (set->val.scnodes[i].in_ctx == pred_in_ctx) {
6771 set->val.scnodes[i].in_ctx = 1;
6772 }
6773 }
6774
6775 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02006776 set2.type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006777 set_fill_set(&set2, set);
6778
Michal Vasko004d3152020-06-11 19:59:22 +02006779 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006780 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006781 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006782 return rc;
6783 }
6784
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006785 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02006786 if (!set2.val.bln) {
Michal Vaskod3678892020-05-21 10:06:58 +02006787 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006788 }
Michal Vaskod3678892020-05-21 10:06:58 +02006789 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006790 }
6791
6792 /* ']' */
Michal Vasko004d3152020-06-11 19:59:22 +02006793 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_BRACK2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006794 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02006795 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
6796 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006797
6798 return LY_SUCCESS;
6799}
6800
6801/**
Michal Vaskod3678892020-05-21 10:06:58 +02006802 * @brief Evaluate Literal. Logs directly on error.
6803 *
6804 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02006805 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02006806 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
6807 */
6808static void
Michal Vasko004d3152020-06-11 19:59:22 +02006809eval_literal(struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set)
Michal Vaskod3678892020-05-21 10:06:58 +02006810{
6811 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02006812 if (exp->tok_len[*tok_idx] == 2) {
Michal Vaskod3678892020-05-21 10:06:58 +02006813 set_fill_string(set, "", 0);
6814 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02006815 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 +02006816 }
6817 }
6818 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02006819 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
6820 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02006821}
6822
6823/**
Michal Vasko004d3152020-06-11 19:59:22 +02006824 * @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 +02006825 *
Michal Vasko004d3152020-06-11 19:59:22 +02006826 * @param[in] exp Full parsed XPath expression.
6827 * @param[in,out] tok_idx Index in @p exp at the beginning of the predicate, is updated on success.
6828 * @param[in] scnode Found schema node as context for the predicate.
6829 * @param[in] format Format of any prefixes in key names/values.
6830 * @param[out] predicates Parsed predicates.
6831 * @param[out] pred_type Type of @p predicates.
6832 * @return LY_SUCCESS on success,
6833 * @return LY_ERR on any error.
Michal Vaskod3678892020-05-21 10:06:58 +02006834 */
6835static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02006836eval_name_test_try_compile_predicates(struct lyxp_expr *exp, uint16_t *tok_idx, const struct lysc_node *scnode,
6837 LYD_FORMAT format, struct ly_path_predicate **predicates, enum ly_path_pred_type *pred_type)
Michal Vaskod3678892020-05-21 10:06:58 +02006838{
Michal Vasko004d3152020-06-11 19:59:22 +02006839 LY_ERR ret = LY_SUCCESS;
6840 uint16_t key_count, e_idx, pred_idx = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02006841 const struct lysc_node *key;
Michal Vasko004d3152020-06-11 19:59:22 +02006842 size_t pred_len;
6843 int prev_lo;
6844 struct lyxp_expr *exp2 = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02006845
Michal Vasko004d3152020-06-11 19:59:22 +02006846 assert(scnode->nodetype & (LYS_LIST | LYS_LEAFLIST));
Michal Vaskod3678892020-05-21 10:06:58 +02006847
Michal Vasko004d3152020-06-11 19:59:22 +02006848 if (scnode->nodetype == LYS_LIST) {
6849 /* get key count */
6850 if (scnode->flags & LYS_KEYLESS) {
6851 return LY_EINVAL;
6852 }
6853 for (key_count = 0, key = lysc_node_children(scnode, 0); key && (key->flags & LYS_KEY); key = key->next, ++key_count);
6854 assert(key_count);
Michal Vaskod3678892020-05-21 10:06:58 +02006855
Michal Vasko004d3152020-06-11 19:59:22 +02006856 /* learn where the predicates end */
6857 e_idx = *tok_idx;
6858 while (key_count) {
6859 /* '[' */
6860 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
6861 return LY_EINVAL;
6862 }
6863 ++e_idx;
6864
6865 /* ']' */
6866 while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
6867 ++e_idx;
6868 }
6869 ++e_idx;
6870
6871 /* another presumably key predicate parsed */
6872 --key_count;
6873 }
6874
6875 if (key_count) {
6876 /* some keys missing for sure */
6877 return LY_EINVAL;
6878 }
6879 } else {
6880 /* learn just where this single predicate ends */
6881 e_idx = *tok_idx;
6882
Michal Vaskod3678892020-05-21 10:06:58 +02006883 /* '[' */
Michal Vasko004d3152020-06-11 19:59:22 +02006884 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
6885 return LY_EINVAL;
6886 }
6887 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02006888
6889 /* ']' */
Michal Vasko004d3152020-06-11 19:59:22 +02006890 while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
6891 ++e_idx;
6892 }
6893 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02006894 }
6895
Michal Vasko004d3152020-06-11 19:59:22 +02006896 /* get the joined length of all the keys predicates/of the single leaf-list predicate */
6897 pred_len = (exp->tok_pos[e_idx - 1] + exp->tok_len[e_idx - 1]) - exp->tok_pos[*tok_idx];
6898
6899 /* turn logging off */
6900 prev_lo = ly_log_options(0);
6901
6902 /* parse the predicate(s) */
6903 LY_CHECK_GOTO(ret = ly_path_parse_predicate(scnode->module->ctx, exp->expr + exp->tok_pos[*tok_idx], pred_len,
6904 LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &exp2), cleanup);
6905
6906 /* compile */
6907 switch (format) {
6908 case LYD_SCHEMA:
6909 ret = ly_path_compile_predicate(scnode->module, scnode, exp2, &pred_idx, lys_resolve_prefix, scnode->module,
6910 LYD_SCHEMA, predicates, pred_type);
6911 break;
6912 case LYD_JSON:
6913 ret = ly_path_compile_predicate(scnode->module, scnode, exp2, &pred_idx, lydjson_resolve_prefix, NULL, LYD_JSON,
6914 predicates, pred_type);
6915 break;
6916 case LYD_XML:
6917 ret = LY_EINT;
6918 goto cleanup;
6919 }
6920 LY_CHECK_GOTO(ret, cleanup);
6921
6922 /* success, the predicate must include all the needed information for hash-based search */
6923 *tok_idx = e_idx;
6924
6925cleanup:
6926 ly_log_options(prev_lo);
6927 lyxp_expr_free(scnode->module->ctx, exp2);
6928 return ret;
Michal Vaskod3678892020-05-21 10:06:58 +02006929}
6930
6931/**
6932 * @brief Evaluate NameTest and any following Predicates. Logs directly on error.
6933 *
6934 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
6935 * [6] NodeTest ::= NameTest | NodeType '(' ')'
6936 * [7] NameTest ::= '*' | NCName ':' '*' | QName
6937 *
6938 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02006939 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02006940 * @param[in] attr_axis Whether to search attributes or standard nodes.
6941 * @param[in] all_desc Whether to search all the descendants or children only.
6942 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
6943 * @param[in] options XPath options.
6944 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6945 */
6946static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02006947eval_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 +02006948 int options)
6949{
6950 int i;
6951 char *path;
Michal Vasko004d3152020-06-11 19:59:22 +02006952 const char *ncname, *ncname_dict = NULL;
6953 uint16_t ncname_len;
Michal Vaskod3678892020-05-21 10:06:58 +02006954 const struct lys_module *moveto_mod;
6955 const struct lysc_node *scnode = NULL, *tmp;
Michal Vasko004d3152020-06-11 19:59:22 +02006956 struct ly_path_predicate *predicates = NULL;
6957 enum ly_path_pred_type pred_type = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02006958 LY_ERR rc = LY_SUCCESS;
6959
6960 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02006961 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
6962 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02006963
6964 if (!set) {
6965 goto moveto;
6966 }
6967
Michal Vasko004d3152020-06-11 19:59:22 +02006968 ncname = &exp->expr[exp->tok_pos[*tok_idx - 1]];
6969 ncname_len = exp->tok_len[*tok_idx - 1];
Michal Vaskod3678892020-05-21 10:06:58 +02006970
6971 /* parse (and skip) module name */
6972 rc = moveto_resolve_model(&ncname, &ncname_len, set, &moveto_mod);
6973 LY_CHECK_GOTO(rc, cleanup);
6974
6975 if (moveto_mod && !attr_axis && !all_desc && (set->type == LYXP_SET_NODE_SET)) {
6976 /* find the matching schema node in some parent in the context */
6977 for (i = 0; i < (signed)set->used; ++i) {
6978 if (set->val.nodes[i].type == set->root_type) {
6979 tmp = lys_find_child(NULL, moveto_mod, ncname, ncname_len, 0, 0);
6980 } else if ((set->val.nodes[i].type == LYXP_NODE_ELEM)
6981 && (!scnode || (lysc_data_parent(scnode) != set->val.nodes[i].node->schema))) {
6982 /* do not repeat the same search */
6983 tmp = lys_find_child(set->val.nodes[i].node->schema, moveto_mod, ncname, ncname_len, 0, 0);
6984 }
6985
6986 /* additional context check */
6987 if (tmp && (set->root_type == LYXP_NODE_ROOT_CONFIG) && (tmp->flags & LYS_CONFIG_R)) {
6988 tmp = NULL;
6989 }
6990
6991 if (tmp) {
6992 if (scnode) {
6993 /* we found a schema node with the same name but at different level, give up, too complicated */
6994 scnode = NULL;
6995 break;
6996 } else {
6997 /* remember the found schema node and continue to make sure it can be used */
6998 scnode = tmp;
6999 }
7000 tmp = NULL;
7001 }
7002 }
7003
Michal Vasko004d3152020-06-11 19:59:22 +02007004 if (scnode && (scnode->nodetype & (LYS_LIST | LYS_LEAFLIST))) {
7005 /* try to create the predicates */
7006 if (eval_name_test_try_compile_predicates(exp, tok_idx, scnode, set->format, &predicates, &pred_type)) {
7007 /* hashes cannot be used */
Michal Vaskod3678892020-05-21 10:06:58 +02007008 scnode = NULL;
7009 }
7010 }
7011 }
7012
Michal Vasko004d3152020-06-11 19:59:22 +02007013 if (!scnode && moveto_mod) {
7014 /* we are not able to match based on a schema node and not all the modules match,
7015 * use dictionary for efficient comparison */
7016 ncname_dict = lydict_insert(set->ctx, ncname, ncname_len);
Michal Vaskod3678892020-05-21 10:06:58 +02007017 }
7018
7019moveto:
7020 /* move to the attribute(s), data node(s), or schema node(s) */
7021 if (attr_axis) {
7022 if (set && (options & LYXP_SCNODE_ALL)) {
7023 set_scnode_clear_ctx(set);
7024 } else {
7025 if (all_desc) {
Michal Vasko004d3152020-06-11 19:59:22 +02007026 rc = moveto_attr_alldesc(set, moveto_mod, ncname_dict);
Michal Vaskod3678892020-05-21 10:06:58 +02007027 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007028 rc = moveto_attr(set, moveto_mod, ncname_dict);
Michal Vaskod3678892020-05-21 10:06:58 +02007029 }
7030 LY_CHECK_GOTO(rc, cleanup);
7031 }
7032 } else {
7033 if (set && (options & LYXP_SCNODE_ALL)) {
7034 if (all_desc) {
Michal Vasko004d3152020-06-11 19:59:22 +02007035 rc = moveto_scnode_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007036 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007037 rc = moveto_scnode(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007038 }
7039 LY_CHECK_GOTO(rc, cleanup);
7040
7041 for (i = set->used - 1; i > -1; --i) {
7042 if (set->val.scnodes[i].in_ctx > 0) {
7043 break;
7044 }
7045 }
7046 if (i == -1) {
7047 path = lysc_path(set->ctx_scnode, LYSC_PATH_LOG, NULL, 0);
7048 LOGWRN(set->ctx, "Schema node \"%.*s\" not found (%.*s) with context node \"%s\".",
Michal Vasko004d3152020-06-11 19:59:22 +02007049 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]],
7050 exp->tok_pos[*tok_idx] + exp->tok_len[*tok_idx], exp->expr, path);
Michal Vaskod3678892020-05-21 10:06:58 +02007051 free(path);
7052 }
7053 } else {
7054 if (all_desc) {
Michal Vasko004d3152020-06-11 19:59:22 +02007055 rc = moveto_node_alldesc(set, moveto_mod, ncname_dict);
Michal Vaskod3678892020-05-21 10:06:58 +02007056 } else {
7057 if (scnode) {
7058 /* we can find the nodes using hashes */
Michal Vasko004d3152020-06-11 19:59:22 +02007059 rc = moveto_node_hash(set, scnode, predicates);
Michal Vaskod3678892020-05-21 10:06:58 +02007060 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007061 rc = moveto_node(set, moveto_mod, ncname_dict);
Michal Vaskod3678892020-05-21 10:06:58 +02007062 }
7063 }
7064 LY_CHECK_GOTO(rc, cleanup);
7065 }
7066 }
7067
7068 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007069 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7070 rc = eval_predicate(exp, tok_idx, set, options, 1);
Michal Vaskod3678892020-05-21 10:06:58 +02007071 LY_CHECK_RET(rc);
7072 }
7073
7074cleanup:
Michal Vaskodb51a8d2020-05-27 15:22:29 +02007075 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007076 lydict_remove(set->ctx, ncname_dict);
7077 ly_path_predicates_free(set->ctx, pred_type, scnode, predicates);
Michal Vaskodb51a8d2020-05-27 15:22:29 +02007078 }
Michal Vaskod3678892020-05-21 10:06:58 +02007079 return rc;
7080}
7081
7082/**
7083 * @brief Evaluate NodeType and any following Predicates. Logs directly on error.
7084 *
7085 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
7086 * [6] NodeTest ::= NameTest | NodeType '(' ')'
7087 * [8] NodeType ::= 'text' | 'node'
7088 *
7089 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007090 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02007091 * @param[in] attr_axis Whether to search attributes or standard nodes.
7092 * @param[in] all_desc Whether to search all the descendants or children only.
7093 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7094 * @param[in] options XPath options.
7095 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7096 */
7097static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007098eval_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 +02007099 struct lyxp_set *set, int options)
7100{
7101 LY_ERR rc;
7102
7103 /* TODO */
7104 (void)attr_axis;
7105 (void)all_desc;
7106
7107 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007108 assert(exp->tok_len[*tok_idx] == 4);
Michal Vaskod3678892020-05-21 10:06:58 +02007109 if (set->type == LYXP_SET_SCNODE_SET) {
7110 set_scnode_clear_ctx(set);
7111 /* just for the debug message below */
7112 set = NULL;
7113 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007114 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "node", 4)) {
Michal Vaskod3678892020-05-21 10:06:58 +02007115 rc = xpath_node(NULL, 0, set, options);
7116 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007117 assert(!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "text", 4));
Michal Vaskod3678892020-05-21 10:06:58 +02007118 rc = xpath_text(NULL, 0, set, options);
7119 }
7120 LY_CHECK_RET(rc);
7121 }
7122 }
7123 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007124 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7125 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007126
7127 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02007128 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
Michal Vaskod3678892020-05-21 10:06:58 +02007129 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007130 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7131 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007132
7133 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007134 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
Michal Vaskod3678892020-05-21 10:06:58 +02007135 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007136 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7137 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007138
7139 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007140 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7141 rc = eval_predicate(exp, tok_idx, set, options, 1);
Michal Vaskod3678892020-05-21 10:06:58 +02007142 LY_CHECK_RET(rc);
7143 }
7144
7145 return LY_SUCCESS;
7146}
7147
7148/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02007149 * @brief Evaluate RelativeLocationPath. Logs directly on error.
7150 *
7151 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
7152 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
Michal Vaskod3678892020-05-21 10:06:58 +02007153 * [6] NodeTest ::= NameTest | NodeType '(' ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007154 *
7155 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007156 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007157 * @param[in] all_desc Whether to search all the descendants or children only.
7158 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7159 * @param[in] options XPath options.
7160 * @return LY_ERR (YL_EINCOMPLETE on unresolved when)
7161 */
7162static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007163eval_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 +02007164{
7165 int attr_axis;
7166 LY_ERR rc;
7167
7168 goto step;
7169 do {
7170 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02007171 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007172 all_desc = 0;
7173 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007174 assert(exp->tok_len[*tok_idx] == 2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007175 all_desc = 1;
7176 }
7177 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007178 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7179 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007180
7181step:
Michal Vaskod3678892020-05-21 10:06:58 +02007182 /* evaluate abbreviated axis '@'? if any */
Michal Vasko004d3152020-06-11 19:59:22 +02007183 if (exp->tokens[*tok_idx] == LYXP_TOKEN_AT) {
Michal Vaskod3678892020-05-21 10:06:58 +02007184 attr_axis = 1;
7185
7186 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007187 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7188 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007189 } else {
7190 attr_axis = 0;
7191 }
7192
Michal Vasko03ff5a72019-09-11 13:49:33 +02007193 /* Step */
Michal Vasko004d3152020-06-11 19:59:22 +02007194 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007195 case LYXP_TOKEN_DOT:
7196 /* evaluate '.' */
7197 if (set && (options & LYXP_SCNODE_ALL)) {
7198 rc = moveto_scnode_self(set, all_desc, options);
7199 } else {
7200 rc = moveto_self(set, all_desc, options);
7201 }
7202 LY_CHECK_RET(rc);
7203 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007204 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7205 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007206 break;
7207
7208 case LYXP_TOKEN_DDOT:
7209 /* evaluate '..' */
7210 if (set && (options & LYXP_SCNODE_ALL)) {
7211 rc = moveto_scnode_parent(set, all_desc, options);
7212 } else {
7213 rc = moveto_parent(set, all_desc, options);
7214 }
7215 LY_CHECK_RET(rc);
7216 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007217 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7218 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007219 break;
7220
Michal Vasko03ff5a72019-09-11 13:49:33 +02007221 case LYXP_TOKEN_NAMETEST:
Michal Vaskod3678892020-05-21 10:06:58 +02007222 /* evaluate NameTest Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007223 rc = eval_name_test_with_predicate(exp, tok_idx, attr_axis, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007224 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02007225 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007226
Michal Vaskod3678892020-05-21 10:06:58 +02007227 case LYXP_TOKEN_NODETYPE:
7228 /* evaluate NodeType Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007229 rc = eval_node_type_with_predicate(exp, tok_idx, attr_axis, all_desc, set, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007230 LY_CHECK_RET(rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007231 break;
7232
7233 default:
Michal Vasko02a77382019-09-12 11:47:35 +02007234 LOGINT_RET(set ? set->ctx : NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007235 }
Michal Vasko004d3152020-06-11 19:59:22 +02007236 } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007237
7238 return LY_SUCCESS;
7239}
7240
7241/**
7242 * @brief Evaluate AbsoluteLocationPath. Logs directly on error.
7243 *
7244 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
7245 *
7246 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007247 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007248 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7249 * @param[in] options XPath options.
7250 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7251 */
7252static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007253eval_absolute_location_path(struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set, int options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007254{
7255 int all_desc;
7256 LY_ERR rc;
7257
7258 if (set) {
7259 /* no matter what tokens follow, we need to be at the root */
7260 moveto_root(set, options);
7261 }
7262
7263 /* '/' RelativeLocationPath? */
Michal Vasko004d3152020-06-11 19:59:22 +02007264 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007265 /* evaluate '/' - deferred */
7266 all_desc = 0;
7267 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007268 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7269 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007270
Michal Vasko004d3152020-06-11 19:59:22 +02007271 if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007272 return LY_SUCCESS;
7273 }
Michal Vasko004d3152020-06-11 19:59:22 +02007274 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007275 case LYXP_TOKEN_DOT:
7276 case LYXP_TOKEN_DDOT:
7277 case LYXP_TOKEN_AT:
7278 case LYXP_TOKEN_NAMETEST:
7279 case LYXP_TOKEN_NODETYPE:
Michal Vasko004d3152020-06-11 19:59:22 +02007280 rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007281 LY_CHECK_RET(rc);
7282 break;
7283 default:
7284 break;
7285 }
7286
7287 /* '//' RelativeLocationPath */
7288 } else {
7289 /* evaluate '//' - deferred so as not to waste memory by remembering all the nodes */
7290 all_desc = 1;
7291 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007292 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7293 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007294
Michal Vasko004d3152020-06-11 19:59:22 +02007295 rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007296 LY_CHECK_RET(rc);
7297 }
7298
7299 return LY_SUCCESS;
7300}
7301
7302/**
7303 * @brief Evaluate FunctionCall. Logs directly on error.
7304 *
Michal Vaskod3678892020-05-21 10:06:58 +02007305 * [11] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007306 *
7307 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007308 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007309 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7310 * @param[in] options XPath options.
7311 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7312 */
7313static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007314eval_function_call(struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set, int options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007315{
7316 LY_ERR rc;
7317 LY_ERR (*xpath_func)(struct lyxp_set **, uint16_t, struct lyxp_set *, int) = NULL;
Michal Vasko0cbf54f2019-12-16 10:01:06 +01007318 uint16_t arg_count = 0, i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007319 struct lyxp_set **args = NULL, **args_aux;
7320
7321 if (set) {
7322 /* FunctionName */
Michal Vasko004d3152020-06-11 19:59:22 +02007323 switch (exp->tok_len[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007324 case 3:
Michal Vasko004d3152020-06-11 19:59:22 +02007325 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007326 xpath_func = &xpath_not;
Michal Vasko004d3152020-06-11 19:59:22 +02007327 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007328 xpath_func = &xpath_sum;
7329 }
7330 break;
7331 case 4:
Michal Vasko004d3152020-06-11 19:59:22 +02007332 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007333 xpath_func = &xpath_lang;
Michal Vasko004d3152020-06-11 19:59:22 +02007334 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007335 xpath_func = &xpath_last;
Michal Vasko004d3152020-06-11 19:59:22 +02007336 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007337 xpath_func = &xpath_name;
Michal Vasko004d3152020-06-11 19:59:22 +02007338 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007339 xpath_func = &xpath_true;
7340 }
7341 break;
7342 case 5:
Michal Vasko004d3152020-06-11 19:59:22 +02007343 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007344 xpath_func = &xpath_count;
Michal Vasko004d3152020-06-11 19:59:22 +02007345 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007346 xpath_func = &xpath_false;
Michal Vasko004d3152020-06-11 19:59:22 +02007347 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007348 xpath_func = &xpath_floor;
Michal Vasko004d3152020-06-11 19:59:22 +02007349 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007350 xpath_func = &xpath_round;
Michal Vasko004d3152020-06-11 19:59:22 +02007351 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007352 xpath_func = &xpath_deref;
7353 }
7354 break;
7355 case 6:
Michal Vasko004d3152020-06-11 19:59:22 +02007356 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007357 xpath_func = &xpath_concat;
Michal Vasko004d3152020-06-11 19:59:22 +02007358 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007359 xpath_func = &xpath_number;
Michal Vasko004d3152020-06-11 19:59:22 +02007360 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007361 xpath_func = &xpath_string;
7362 }
7363 break;
7364 case 7:
Michal Vasko004d3152020-06-11 19:59:22 +02007365 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007366 xpath_func = &xpath_boolean;
Michal Vasko004d3152020-06-11 19:59:22 +02007367 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007368 xpath_func = &xpath_ceiling;
Michal Vasko004d3152020-06-11 19:59:22 +02007369 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007370 xpath_func = &xpath_current;
7371 }
7372 break;
7373 case 8:
Michal Vasko004d3152020-06-11 19:59:22 +02007374 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007375 xpath_func = &xpath_contains;
Michal Vasko004d3152020-06-11 19:59:22 +02007376 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007377 xpath_func = &xpath_position;
Michal Vasko004d3152020-06-11 19:59:22 +02007378 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007379 xpath_func = &xpath_re_match;
7380 }
7381 break;
7382 case 9:
Michal Vasko004d3152020-06-11 19:59:22 +02007383 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007384 xpath_func = &xpath_substring;
Michal Vasko004d3152020-06-11 19:59:22 +02007385 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007386 xpath_func = &xpath_translate;
7387 }
7388 break;
7389 case 10:
Michal Vasko004d3152020-06-11 19:59:22 +02007390 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007391 xpath_func = &xpath_local_name;
Michal Vasko004d3152020-06-11 19:59:22 +02007392 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007393 xpath_func = &xpath_enum_value;
Michal Vasko004d3152020-06-11 19:59:22 +02007394 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007395 xpath_func = &xpath_bit_is_set;
7396 }
7397 break;
7398 case 11:
Michal Vasko004d3152020-06-11 19:59:22 +02007399 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007400 xpath_func = &xpath_starts_with;
7401 }
7402 break;
7403 case 12:
Michal Vasko004d3152020-06-11 19:59:22 +02007404 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007405 xpath_func = &xpath_derived_from;
7406 }
7407 break;
7408 case 13:
Michal Vasko004d3152020-06-11 19:59:22 +02007409 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007410 xpath_func = &xpath_namespace_uri;
Michal Vasko004d3152020-06-11 19:59:22 +02007411 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007412 xpath_func = &xpath_string_length;
7413 }
7414 break;
7415 case 15:
Michal Vasko004d3152020-06-11 19:59:22 +02007416 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007417 xpath_func = &xpath_normalize_space;
Michal Vasko004d3152020-06-11 19:59:22 +02007418 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007419 xpath_func = &xpath_substring_after;
7420 }
7421 break;
7422 case 16:
Michal Vasko004d3152020-06-11 19:59:22 +02007423 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007424 xpath_func = &xpath_substring_before;
7425 }
7426 break;
7427 case 20:
Michal Vasko004d3152020-06-11 19:59:22 +02007428 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007429 xpath_func = &xpath_derived_from_or_self;
7430 }
7431 break;
7432 }
7433
7434 if (!xpath_func) {
Michal Vasko004d3152020-06-11 19:59:22 +02007435 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 +02007436 return LY_EVALID;
7437 }
7438 }
7439
7440 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007441 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7442 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007443
7444 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02007445 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007446 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007447 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7448 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007449
7450 /* ( Expr ( ',' Expr )* )? */
Michal Vasko004d3152020-06-11 19:59:22 +02007451 if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007452 if (set) {
7453 args = malloc(sizeof *args);
7454 LY_CHECK_ERR_GOTO(!args, LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7455 arg_count = 1;
7456 args[0] = set_copy(set);
7457 if (!args[0]) {
7458 rc = LY_EMEM;
7459 goto cleanup;
7460 }
7461
Michal Vasko004d3152020-06-11 19:59:22 +02007462 rc = eval_expr_select(exp, tok_idx, 0, args[0], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007463 LY_CHECK_GOTO(rc, cleanup);
7464 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007465 rc = eval_expr_select(exp, tok_idx, 0, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007466 LY_CHECK_GOTO(rc, cleanup);
7467 }
7468 }
Michal Vasko004d3152020-06-11 19:59:22 +02007469 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007470 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007471 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7472 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007473
7474 if (set) {
7475 ++arg_count;
7476 args_aux = realloc(args, arg_count * sizeof *args);
7477 LY_CHECK_ERR_GOTO(!args_aux, arg_count--; LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7478 args = args_aux;
7479 args[arg_count - 1] = set_copy(set);
7480 if (!args[arg_count - 1]) {
7481 rc = LY_EMEM;
7482 goto cleanup;
7483 }
7484
Michal Vasko004d3152020-06-11 19:59:22 +02007485 rc = eval_expr_select(exp, tok_idx, 0, args[arg_count - 1], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007486 LY_CHECK_GOTO(rc, cleanup);
7487 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007488 rc = eval_expr_select(exp, tok_idx, 0, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007489 LY_CHECK_GOTO(rc, cleanup);
7490 }
7491 }
7492
7493 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007494 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007495 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007496 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7497 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007498
7499 if (set) {
7500 /* evaluate function */
7501 rc = xpath_func(args, arg_count, set, options);
7502
7503 if (options & LYXP_SCNODE_ALL) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007504 /* merge all nodes from arg evaluations */
7505 for (i = 0; i < arg_count; ++i) {
7506 set_scnode_clear_ctx(args[i]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007507 lyxp_set_scnode_merge(set, args[i]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007508 }
7509 }
7510 } else {
7511 rc = LY_SUCCESS;
7512 }
7513
7514cleanup:
7515 for (i = 0; i < arg_count; ++i) {
7516 lyxp_set_free(args[i]);
7517 }
7518 free(args);
7519
7520 return rc;
7521}
7522
7523/**
7524 * @brief Evaluate Number. Logs directly on error.
7525 *
7526 * @param[in] ctx Context for errors.
7527 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007528 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007529 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7530 * @return LY_ERR
7531 */
7532static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007533eval_number(struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007534{
7535 long double num;
7536 char *endptr;
7537
7538 if (set) {
7539 errno = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02007540 num = strtold(&exp->expr[exp->tok_pos[*tok_idx]], &endptr);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007541 if (errno) {
Michal Vasko004d3152020-06-11 19:59:22 +02007542 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 +02007543 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 +02007544 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]], strerror(errno));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007545 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02007546 } else if (endptr - &exp->expr[exp->tok_pos[*tok_idx]] != exp->tok_len[*tok_idx]) {
7547 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 +02007548 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 +02007549 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007550 return LY_EVALID;
7551 }
7552
7553 set_fill_number(set, num);
7554 }
7555
7556 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007557 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7558 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007559 return LY_SUCCESS;
7560}
7561
7562/**
7563 * @brief Evaluate PathExpr. Logs directly on error.
7564 *
Michal Vaskod3678892020-05-21 10:06:58 +02007565 * [12] PathExpr ::= LocationPath | PrimaryExpr Predicate*
Michal Vasko03ff5a72019-09-11 13:49:33 +02007566 * | PrimaryExpr Predicate* '/' RelativeLocationPath
7567 * | PrimaryExpr Predicate* '//' RelativeLocationPath
7568 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
Michal Vaskod3678892020-05-21 10:06:58 +02007569 * [10] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall
Michal Vasko03ff5a72019-09-11 13:49:33 +02007570 *
7571 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007572 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007573 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7574 * @param[in] options XPath options.
7575 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7576 */
7577static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007578eval_path_expr(struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set, int options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007579{
7580 int all_desc, parent_pos_pred;
7581 LY_ERR rc;
7582
Michal Vasko004d3152020-06-11 19:59:22 +02007583 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007584 case LYXP_TOKEN_PAR1:
7585 /* '(' Expr ')' */
7586
7587 /* '(' */
7588 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007589 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7590 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007591
7592 /* Expr */
Michal Vasko004d3152020-06-11 19:59:22 +02007593 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007594 LY_CHECK_RET(rc);
7595
7596 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007597 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007598 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007599 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7600 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007601
7602 parent_pos_pred = 0;
7603 goto predicate;
7604
7605 case LYXP_TOKEN_DOT:
7606 case LYXP_TOKEN_DDOT:
7607 case LYXP_TOKEN_AT:
7608 case LYXP_TOKEN_NAMETEST:
7609 case LYXP_TOKEN_NODETYPE:
7610 /* RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007611 rc = eval_relative_location_path(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007612 LY_CHECK_RET(rc);
7613 break;
7614
7615 case LYXP_TOKEN_FUNCNAME:
7616 /* FunctionCall */
7617 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007618 rc = eval_function_call(exp, tok_idx, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007619 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007620 rc = eval_function_call(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007621 }
7622 LY_CHECK_RET(rc);
7623
7624 parent_pos_pred = 1;
7625 goto predicate;
7626
Michal Vasko3e48bf32020-06-01 08:39:07 +02007627 case LYXP_TOKEN_OPER_PATH:
7628 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02007629 /* AbsoluteLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007630 rc = eval_absolute_location_path(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007631 LY_CHECK_RET(rc);
7632 break;
7633
7634 case LYXP_TOKEN_LITERAL:
7635 /* Literal */
7636 if (!set || (options & LYXP_SCNODE_ALL)) {
7637 if (set) {
7638 set_scnode_clear_ctx(set);
7639 }
Michal Vasko004d3152020-06-11 19:59:22 +02007640 eval_literal(exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007641 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007642 eval_literal(exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007643 }
7644
7645 parent_pos_pred = 1;
7646 goto predicate;
7647
7648 case LYXP_TOKEN_NUMBER:
7649 /* Number */
7650 if (!set || (options & LYXP_SCNODE_ALL)) {
7651 if (set) {
7652 set_scnode_clear_ctx(set);
7653 }
Michal Vasko004d3152020-06-11 19:59:22 +02007654 rc = eval_number(NULL, exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007655 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007656 rc = eval_number(set->ctx, exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007657 }
7658 LY_CHECK_RET(rc);
7659
7660 parent_pos_pred = 1;
7661 goto predicate;
7662
7663 default:
Michal Vasko004d3152020-06-11 19:59:22 +02007664 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INTOK, lyxp_print_token(exp->tokens[*tok_idx]),
7665 &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007666 return LY_EVALID;
7667 }
7668
7669 return LY_SUCCESS;
7670
7671predicate:
7672 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007673 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7674 rc = eval_predicate(exp, tok_idx, set, options, parent_pos_pred);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007675 LY_CHECK_RET(rc);
7676 }
7677
7678 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007679 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007680
7681 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02007682 if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007683 all_desc = 0;
7684 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007685 all_desc = 1;
7686 }
7687
7688 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007689 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7690 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007691
Michal Vasko004d3152020-06-11 19:59:22 +02007692 rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007693 LY_CHECK_RET(rc);
7694 }
7695
7696 return LY_SUCCESS;
7697}
7698
7699/**
7700 * @brief Evaluate UnionExpr. Logs directly on error.
7701 *
Michal Vaskod3678892020-05-21 10:06:58 +02007702 * [20] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007703 *
7704 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007705 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007706 * @param[in] repeat How many times this expression is repeated.
7707 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7708 * @param[in] options XPath options.
7709 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7710 */
7711static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007712eval_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 +02007713{
7714 LY_ERR rc = LY_SUCCESS;
7715 struct lyxp_set orig_set, set2;
7716 uint16_t i;
7717
7718 assert(repeat);
7719
7720 set_init(&orig_set, set);
7721 set_init(&set2, set);
7722
7723 set_fill_set(&orig_set, set);
7724
Michal Vasko004d3152020-06-11 19:59:22 +02007725 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007726 LY_CHECK_GOTO(rc, cleanup);
7727
7728 /* ('|' PathExpr)* */
7729 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007730 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_UNI);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007731 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007732 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7733 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007734
7735 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007736 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007737 LY_CHECK_GOTO(rc, cleanup);
7738 continue;
7739 }
7740
7741 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02007742 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007743 LY_CHECK_GOTO(rc, cleanup);
7744
7745 /* eval */
7746 if (options & LYXP_SCNODE_ALL) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01007747 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007748 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007749 rc = moveto_union(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007750 LY_CHECK_GOTO(rc, cleanup);
7751 }
7752 }
7753
7754cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007755 lyxp_set_free_content(&orig_set);
7756 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007757 return rc;
7758}
7759
7760/**
7761 * @brief Evaluate UnaryExpr. Logs directly on error.
7762 *
Michal Vaskod3678892020-05-21 10:06:58 +02007763 * [19] UnaryExpr ::= UnionExpr | '-' UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007764 *
7765 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007766 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007767 * @param[in] repeat How many times this expression is repeated.
7768 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7769 * @param[in] options XPath options.
7770 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7771 */
7772static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007773eval_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 +02007774{
7775 LY_ERR rc;
7776 uint16_t this_op, i;
7777
7778 assert(repeat);
7779
7780 /* ('-')+ */
Michal Vasko004d3152020-06-11 19:59:22 +02007781 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007782 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007783 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 +02007784
7785 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007786 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7787 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007788 }
7789
Michal Vasko004d3152020-06-11 19:59:22 +02007790 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNARY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007791 LY_CHECK_RET(rc);
7792
7793 if (set && (repeat % 2)) {
7794 if (options & LYXP_SCNODE_ALL) {
7795 warn_operands(set->ctx, set, NULL, 1, exp->expr, exp->tok_pos[this_op]);
7796 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007797 rc = moveto_op_math(set, NULL, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007798 LY_CHECK_RET(rc);
7799 }
7800 }
7801
7802 return LY_SUCCESS;
7803}
7804
7805/**
7806 * @brief Evaluate MultiplicativeExpr. Logs directly on error.
7807 *
Michal Vaskod3678892020-05-21 10:06:58 +02007808 * [18] MultiplicativeExpr ::= UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007809 * | MultiplicativeExpr '*' UnaryExpr
7810 * | MultiplicativeExpr 'div' UnaryExpr
7811 * | MultiplicativeExpr 'mod' UnaryExpr
7812 *
7813 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007814 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007815 * @param[in] repeat How many times this expression is repeated.
7816 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7817 * @param[in] options XPath options.
7818 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7819 */
7820static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007821eval_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 +02007822{
7823 LY_ERR rc;
7824 uint16_t this_op;
7825 struct lyxp_set orig_set, set2;
7826 uint16_t i;
7827
7828 assert(repeat);
7829
7830 set_init(&orig_set, set);
7831 set_init(&set2, set);
7832
7833 set_fill_set(&orig_set, set);
7834
Michal Vasko004d3152020-06-11 19:59:22 +02007835 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007836 LY_CHECK_GOTO(rc, cleanup);
7837
7838 /* ('*' / 'div' / 'mod' UnaryExpr)* */
7839 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007840 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007841
Michal Vasko004d3152020-06-11 19:59:22 +02007842 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007843 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007844 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7845 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007846
7847 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007848 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007849 LY_CHECK_GOTO(rc, cleanup);
7850 continue;
7851 }
7852
7853 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02007854 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007855 LY_CHECK_GOTO(rc, cleanup);
7856
7857 /* eval */
7858 if (options & LYXP_SCNODE_ALL) {
7859 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007860 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007861 set_scnode_clear_ctx(set);
7862 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007863 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007864 LY_CHECK_GOTO(rc, cleanup);
7865 }
7866 }
7867
7868cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007869 lyxp_set_free_content(&orig_set);
7870 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007871 return rc;
7872}
7873
7874/**
7875 * @brief Evaluate AdditiveExpr. Logs directly on error.
7876 *
Michal Vaskod3678892020-05-21 10:06:58 +02007877 * [17] AdditiveExpr ::= MultiplicativeExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007878 * | AdditiveExpr '+' MultiplicativeExpr
7879 * | AdditiveExpr '-' MultiplicativeExpr
7880 *
7881 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007882 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007883 * @param[in] repeat How many times this expression is repeated.
7884 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7885 * @param[in] options XPath options.
7886 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7887 */
7888static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007889eval_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 +02007890{
7891 LY_ERR rc;
7892 uint16_t this_op;
7893 struct lyxp_set orig_set, set2;
7894 uint16_t i;
7895
7896 assert(repeat);
7897
7898 set_init(&orig_set, set);
7899 set_init(&set2, set);
7900
7901 set_fill_set(&orig_set, set);
7902
Michal Vasko004d3152020-06-11 19:59:22 +02007903 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007904 LY_CHECK_GOTO(rc, cleanup);
7905
7906 /* ('+' / '-' MultiplicativeExpr)* */
7907 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007908 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007909
Michal Vasko004d3152020-06-11 19:59:22 +02007910 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007911 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007912 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7913 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007914
7915 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007916 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007917 LY_CHECK_GOTO(rc, cleanup);
7918 continue;
7919 }
7920
7921 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02007922 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007923 LY_CHECK_GOTO(rc, cleanup);
7924
7925 /* eval */
7926 if (options & LYXP_SCNODE_ALL) {
7927 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007928 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007929 set_scnode_clear_ctx(set);
7930 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007931 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007932 LY_CHECK_GOTO(rc, cleanup);
7933 }
7934 }
7935
7936cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007937 lyxp_set_free_content(&orig_set);
7938 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007939 return rc;
7940}
7941
7942/**
7943 * @brief Evaluate RelationalExpr. Logs directly on error.
7944 *
Michal Vaskod3678892020-05-21 10:06:58 +02007945 * [16] RelationalExpr ::= AdditiveExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007946 * | RelationalExpr '<' AdditiveExpr
7947 * | RelationalExpr '>' AdditiveExpr
7948 * | RelationalExpr '<=' AdditiveExpr
7949 * | RelationalExpr '>=' AdditiveExpr
7950 *
7951 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007952 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007953 * @param[in] repeat How many times this expression is repeated.
7954 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7955 * @param[in] options XPath options.
7956 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7957 */
7958static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007959eval_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 +02007960{
7961 LY_ERR rc;
7962 uint16_t this_op;
7963 struct lyxp_set orig_set, set2;
7964 uint16_t i;
7965
7966 assert(repeat);
7967
7968 set_init(&orig_set, set);
7969 set_init(&set2, set);
7970
7971 set_fill_set(&orig_set, set);
7972
Michal Vasko004d3152020-06-11 19:59:22 +02007973 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007974 LY_CHECK_GOTO(rc, cleanup);
7975
7976 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
7977 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007978 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007979
Michal Vasko004d3152020-06-11 19:59:22 +02007980 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_COMP);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007981 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007982 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7983 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007984
7985 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007986 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007987 LY_CHECK_GOTO(rc, cleanup);
7988 continue;
7989 }
7990
7991 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02007992 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007993 LY_CHECK_GOTO(rc, cleanup);
7994
7995 /* eval */
7996 if (options & LYXP_SCNODE_ALL) {
7997 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007998 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007999 set_scnode_clear_ctx(set);
8000 } else {
8001 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options);
8002 LY_CHECK_GOTO(rc, cleanup);
8003 }
8004 }
8005
8006cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008007 lyxp_set_free_content(&orig_set);
8008 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008009 return rc;
8010}
8011
8012/**
8013 * @brief Evaluate EqualityExpr. Logs directly on error.
8014 *
Michal Vaskod3678892020-05-21 10:06:58 +02008015 * [15] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008016 * | EqualityExpr '!=' RelationalExpr
8017 *
8018 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008019 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008020 * @param[in] repeat How many times this expression is repeated.
8021 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8022 * @param[in] options XPath options.
8023 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8024 */
8025static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02008026eval_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 +02008027{
8028 LY_ERR rc;
8029 uint16_t this_op;
8030 struct lyxp_set orig_set, set2;
8031 uint16_t i;
8032
8033 assert(repeat);
8034
8035 set_init(&orig_set, set);
8036 set_init(&set2, set);
8037
8038 set_fill_set(&orig_set, set);
8039
Michal Vasko004d3152020-06-11 19:59:22 +02008040 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008041 LY_CHECK_GOTO(rc, cleanup);
8042
8043 /* ('=' / '!=' RelationalExpr)* */
8044 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008045 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008046
Michal Vasko004d3152020-06-11 19:59:22 +02008047 assert((exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_EQUAL) || (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_NEQUAL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008048 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02008049 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
8050 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008051
8052 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02008053 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008054 LY_CHECK_GOTO(rc, cleanup);
8055 continue;
8056 }
8057
8058 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008059 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008060 LY_CHECK_GOTO(rc, cleanup);
8061
8062 /* eval */
8063 if (options & LYXP_SCNODE_ALL) {
8064 warn_operands(set->ctx, set, &set2, 0, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vasko004d3152020-06-11 19:59:22 +02008065 warn_equality_value(exp, set, *tok_idx - 1, this_op - 1, *tok_idx - 1);
8066 warn_equality_value(exp, &set2, this_op - 1, this_op - 1, *tok_idx - 1);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008067 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008068 set_scnode_clear_ctx(set);
8069 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008070 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options);
8071 LY_CHECK_GOTO(rc, cleanup);
8072 }
8073 }
8074
8075cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008076 lyxp_set_free_content(&orig_set);
8077 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008078 return rc;
8079}
8080
8081/**
8082 * @brief Evaluate AndExpr. Logs directly on error.
8083 *
Michal Vaskod3678892020-05-21 10:06:58 +02008084 * [14] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008085 *
8086 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008087 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008088 * @param[in] repeat How many times this expression is repeated.
8089 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8090 * @param[in] options XPath options.
8091 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8092 */
8093static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02008094eval_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 +02008095{
8096 LY_ERR rc;
8097 struct lyxp_set orig_set, set2;
8098 uint16_t i;
8099
8100 assert(repeat);
8101
8102 set_init(&orig_set, set);
8103 set_init(&set2, set);
8104
8105 set_fill_set(&orig_set, set);
8106
Michal Vasko004d3152020-06-11 19:59:22 +02008107 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008108 LY_CHECK_GOTO(rc, cleanup);
8109
8110 /* cast to boolean, we know that will be the final result */
8111 if (set && (options & LYXP_SCNODE_ALL)) {
8112 set_scnode_clear_ctx(set);
8113 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008114 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008115 }
8116
8117 /* ('and' EqualityExpr)* */
8118 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008119 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
8120 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (!set || !set->val.bln ? "skipped" : "parsed"),
8121 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
8122 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008123
8124 /* lazy evaluation */
Michal Vasko004d3152020-06-11 19:59:22 +02008125 if (!set || ((set->type == LYXP_SET_BOOLEAN) && !set->val.bln)) {
8126 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008127 LY_CHECK_GOTO(rc, cleanup);
8128 continue;
8129 }
8130
8131 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008132 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008133 LY_CHECK_GOTO(rc, cleanup);
8134
8135 /* eval - just get boolean value actually */
8136 if (set->type == LYXP_SET_SCNODE_SET) {
8137 set_scnode_clear_ctx(&set2);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008138 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008139 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008140 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008141 set_fill_set(set, &set2);
8142 }
8143 }
8144
8145cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008146 lyxp_set_free_content(&orig_set);
8147 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008148 return rc;
8149}
8150
8151/**
8152 * @brief Evaluate OrExpr. Logs directly on error.
8153 *
Michal Vaskod3678892020-05-21 10:06:58 +02008154 * [13] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008155 *
8156 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008157 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008158 * @param[in] repeat How many times this expression is repeated.
8159 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8160 * @param[in] options XPath options.
8161 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8162 */
8163static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02008164eval_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 +02008165{
8166 LY_ERR rc;
8167 struct lyxp_set orig_set, set2;
8168 uint16_t i;
8169
8170 assert(repeat);
8171
8172 set_init(&orig_set, set);
8173 set_init(&set2, set);
8174
8175 set_fill_set(&orig_set, set);
8176
Michal Vasko004d3152020-06-11 19:59:22 +02008177 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008178 LY_CHECK_GOTO(rc, cleanup);
8179
8180 /* cast to boolean, we know that will be the final result */
8181 if (set && (options & LYXP_SCNODE_ALL)) {
8182 set_scnode_clear_ctx(set);
8183 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008184 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008185 }
8186
8187 /* ('or' AndExpr)* */
8188 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008189 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
8190 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (!set || set->val.bln ? "skipped" : "parsed"),
8191 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
8192 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008193
8194 /* lazy evaluation */
Michal Vasko004d3152020-06-11 19:59:22 +02008195 if (!set || ((set->type == LYXP_SET_BOOLEAN) && set->val.bln)) {
8196 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008197 LY_CHECK_GOTO(rc, cleanup);
8198 continue;
8199 }
8200
8201 set_fill_set(&set2, &orig_set);
8202 /* expr_type cound have been LYXP_EXPR_NONE in all these later calls (except for the first one),
8203 * but it does not matter */
Michal Vasko004d3152020-06-11 19:59:22 +02008204 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008205 LY_CHECK_GOTO(rc, cleanup);
8206
8207 /* eval - just get boolean value actually */
8208 if (set->type == LYXP_SET_SCNODE_SET) {
8209 set_scnode_clear_ctx(&set2);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008210 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008211 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008212 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008213 set_fill_set(set, &set2);
8214 }
8215 }
8216
8217cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008218 lyxp_set_free_content(&orig_set);
8219 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008220 return rc;
8221}
8222
8223/**
Michal Vasko004d3152020-06-11 19:59:22 +02008224 * @brief Decide what expression is at the pointer @p tok_idx and evaluate it accordingly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008225 *
8226 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008227 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008228 * @param[in] etype Expression type to evaluate.
8229 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8230 * @param[in] options XPath options.
8231 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8232 */
8233static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02008234eval_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 +02008235{
8236 uint16_t i, count;
8237 enum lyxp_expr_type next_etype;
8238 LY_ERR rc;
8239
8240 /* process operator repeats */
Michal Vasko004d3152020-06-11 19:59:22 +02008241 if (!exp->repeat[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008242 next_etype = LYXP_EXPR_NONE;
8243 } else {
8244 /* find etype repeat */
Michal Vasko004d3152020-06-11 19:59:22 +02008245 for (i = 0; exp->repeat[*tok_idx][i] > etype; ++i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008246
8247 /* select one-priority lower because etype expression called us */
8248 if (i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008249 next_etype = exp->repeat[*tok_idx][i - 1];
Michal Vasko03ff5a72019-09-11 13:49:33 +02008250 /* count repeats for that expression */
Michal Vasko004d3152020-06-11 19:59:22 +02008251 for (count = 0; i && exp->repeat[*tok_idx][i - 1] == next_etype; ++count, --i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008252 } else {
8253 next_etype = LYXP_EXPR_NONE;
8254 }
8255 }
8256
8257 /* decide what expression are we parsing based on the repeat */
8258 switch (next_etype) {
8259 case LYXP_EXPR_OR:
Michal Vasko004d3152020-06-11 19:59:22 +02008260 rc = eval_or_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008261 break;
8262 case LYXP_EXPR_AND:
Michal Vasko004d3152020-06-11 19:59:22 +02008263 rc = eval_and_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008264 break;
8265 case LYXP_EXPR_EQUALITY:
Michal Vasko004d3152020-06-11 19:59:22 +02008266 rc = eval_equality_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008267 break;
8268 case LYXP_EXPR_RELATIONAL:
Michal Vasko004d3152020-06-11 19:59:22 +02008269 rc = eval_relational_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008270 break;
8271 case LYXP_EXPR_ADDITIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02008272 rc = eval_additive_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008273 break;
8274 case LYXP_EXPR_MULTIPLICATIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02008275 rc = eval_multiplicative_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008276 break;
8277 case LYXP_EXPR_UNARY:
Michal Vasko004d3152020-06-11 19:59:22 +02008278 rc = eval_unary_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008279 break;
8280 case LYXP_EXPR_UNION:
Michal Vasko004d3152020-06-11 19:59:22 +02008281 rc = eval_union_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008282 break;
8283 case LYXP_EXPR_NONE:
Michal Vasko004d3152020-06-11 19:59:22 +02008284 rc = eval_path_expr(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008285 break;
8286 default:
8287 LOGINT_RET(set->ctx);
8288 }
8289
8290 return rc;
8291}
8292
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008293/**
8294 * @brief Get root type.
8295 *
8296 * @param[in] ctx_node Context node.
8297 * @param[in] ctx_scnode Schema context node.
8298 * @param[in] options XPath options.
8299 * @return Root type.
8300 */
8301static enum lyxp_node_type
8302lyxp_get_root_type(const struct lyd_node *ctx_node, const struct lysc_node *ctx_scnode, int options)
8303{
8304 if (options & LYXP_SCNODE_ALL) {
8305 if (options & LYXP_SCNODE) {
8306 /* general root that can access everything */
8307 return LYXP_NODE_ROOT;
8308 } else if (!ctx_scnode || (ctx_scnode->flags & LYS_CONFIG_W)) {
8309 /* root context node can access only config data (because we said so, it is unspecified) */
8310 return LYXP_NODE_ROOT_CONFIG;
8311 } else {
8312 return LYXP_NODE_ROOT;
8313 }
8314 }
8315
8316 if (!ctx_node || (ctx_node->schema->flags & LYS_CONFIG_W)) {
8317 /* root context node can access only config data (because we said so, it is unspecified) */
8318 return LYXP_NODE_ROOT_CONFIG;
8319 }
8320
8321 return LYXP_NODE_ROOT;
8322}
8323
Michal Vasko03ff5a72019-09-11 13:49:33 +02008324LY_ERR
Michal Vaskoecd62de2019-11-13 12:35:11 +01008325lyxp_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 +01008326 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 +02008327{
Michal Vasko004d3152020-06-11 19:59:22 +02008328 uint16_t tok_idx = 0;
8329 const struct lyd_node *real_ctx_node;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008330 LY_ERR rc;
8331
Michal Vasko004d3152020-06-11 19:59:22 +02008332 LY_CHECK_ARG_RET(NULL, exp, local_mod, ctx_node, set, LY_EINVAL);
8333
8334 if ((ctx_node_type == LYXP_NODE_ROOT) || (ctx_node_type == LYXP_NODE_ROOT_CONFIG)) {
8335 /* we always need some context node because it is used for resolving unqualified names */
8336 real_ctx_node = NULL;
8337 } else {
8338 real_ctx_node = ctx_node;
8339 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008340
8341 /* prepare set for evaluation */
Michal Vasko004d3152020-06-11 19:59:22 +02008342 tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008343 memset(set, 0, sizeof *set);
Michal Vaskod3678892020-05-21 10:06:58 +02008344 set->type = LYXP_SET_NODE_SET;
Michal Vasko004d3152020-06-11 19:59:22 +02008345 set_insert_node(set, (struct lyd_node *)real_ctx_node, 0, ctx_node_type, 0);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008346 set->ctx = local_mod->ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008347 set->ctx_node = ctx_node;
Michal Vasko004d3152020-06-11 19:59:22 +02008348 set->root_type = lyxp_get_root_type(real_ctx_node, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008349 set->local_mod = local_mod;
Michal Vaskof03ed032020-03-04 13:31:44 +01008350 set->tree = tree;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008351 set->format = format;
8352
8353 /* evaluate */
Michal Vasko004d3152020-06-11 19:59:22 +02008354 rc = eval_expr_select(exp, &tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008355 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02008356 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008357 }
8358
Michal Vasko03ff5a72019-09-11 13:49:33 +02008359 return rc;
8360}
8361
8362#if 0
8363
8364/* full xml printing of set elements, not used currently */
8365
8366void
8367lyxp_set_print_xml(FILE *f, struct lyxp_set *set)
8368{
8369 uint32_t i;
8370 char *str_num;
8371 struct lyout out;
8372
8373 memset(&out, 0, sizeof out);
8374
8375 out.type = LYOUT_STREAM;
8376 out.method.f = f;
8377
8378 switch (set->type) {
8379 case LYXP_SET_EMPTY:
8380 ly_print(&out, "Empty XPath set\n\n");
8381 break;
8382 case LYXP_SET_BOOLEAN:
8383 ly_print(&out, "Boolean XPath set:\n");
8384 ly_print(&out, "%s\n\n", set->value.bool ? "true" : "false");
8385 break;
8386 case LYXP_SET_STRING:
8387 ly_print(&out, "String XPath set:\n");
8388 ly_print(&out, "\"%s\"\n\n", set->value.str);
8389 break;
8390 case LYXP_SET_NUMBER:
8391 ly_print(&out, "Number XPath set:\n");
8392
8393 if (isnan(set->value.num)) {
8394 str_num = strdup("NaN");
8395 } else if ((set->value.num == 0) || (set->value.num == -0.0f)) {
8396 str_num = strdup("0");
8397 } else if (isinf(set->value.num) && !signbit(set->value.num)) {
8398 str_num = strdup("Infinity");
8399 } else if (isinf(set->value.num) && signbit(set->value.num)) {
8400 str_num = strdup("-Infinity");
8401 } else if ((long long)set->value.num == set->value.num) {
8402 if (asprintf(&str_num, "%lld", (long long)set->value.num) == -1) {
8403 str_num = NULL;
8404 }
8405 } else {
8406 if (asprintf(&str_num, "%03.1Lf", set->value.num) == -1) {
8407 str_num = NULL;
8408 }
8409 }
8410 if (!str_num) {
8411 LOGMEM;
8412 return;
8413 }
8414 ly_print(&out, "%s\n\n", str_num);
8415 free(str_num);
8416 break;
8417 case LYXP_SET_NODE_SET:
8418 ly_print(&out, "Node XPath set:\n");
8419
8420 for (i = 0; i < set->used; ++i) {
8421 ly_print(&out, "%d. ", i + 1);
8422 switch (set->node_type[i]) {
8423 case LYXP_NODE_ROOT_ALL:
8424 ly_print(&out, "ROOT all\n\n");
8425 break;
8426 case LYXP_NODE_ROOT_CONFIG:
8427 ly_print(&out, "ROOT config\n\n");
8428 break;
8429 case LYXP_NODE_ROOT_STATE:
8430 ly_print(&out, "ROOT state\n\n");
8431 break;
8432 case LYXP_NODE_ROOT_NOTIF:
8433 ly_print(&out, "ROOT notification \"%s\"\n\n", set->value.nodes[i]->schema->name);
8434 break;
8435 case LYXP_NODE_ROOT_RPC:
8436 ly_print(&out, "ROOT rpc \"%s\"\n\n", set->value.nodes[i]->schema->name);
8437 break;
8438 case LYXP_NODE_ROOT_OUTPUT:
8439 ly_print(&out, "ROOT output \"%s\"\n\n", set->value.nodes[i]->schema->name);
8440 break;
8441 case LYXP_NODE_ELEM:
8442 ly_print(&out, "ELEM \"%s\"\n", set->value.nodes[i]->schema->name);
8443 xml_print_node(&out, 1, set->value.nodes[i], 1, LYP_FORMAT);
8444 ly_print(&out, "\n");
8445 break;
8446 case LYXP_NODE_TEXT:
8447 ly_print(&out, "TEXT \"%s\"\n\n", ((struct lyd_node_leaf_list *)set->value.nodes[i])->value_str);
8448 break;
8449 case LYXP_NODE_ATTR:
8450 ly_print(&out, "ATTR \"%s\" = \"%s\"\n\n", set->value.attrs[i]->name, set->value.attrs[i]->value);
8451 break;
8452 }
8453 }
8454 break;
8455 }
8456}
8457
8458#endif
8459
8460LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008461lyxp_set_cast(struct lyxp_set *set, enum lyxp_set_type target)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008462{
8463 long double num;
8464 char *str;
8465 LY_ERR rc;
8466
8467 if (!set || (set->type == target)) {
8468 return LY_SUCCESS;
8469 }
8470
8471 /* it's not possible to convert anything into a node set */
Michal Vaskod3678892020-05-21 10:06:58 +02008472 assert(target != LYXP_SET_NODE_SET);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008473
8474 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vaskod3678892020-05-21 10:06:58 +02008475 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008476 return LY_EINVAL;
8477 }
8478
8479 /* to STRING */
Michal Vaskod3678892020-05-21 10:06:58 +02008480 if ((target == LYXP_SET_STRING) || ((target == LYXP_SET_NUMBER) && (set->type == LYXP_SET_NODE_SET))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008481 switch (set->type) {
8482 case LYXP_SET_NUMBER:
8483 if (isnan(set->val.num)) {
8484 set->val.str = strdup("NaN");
8485 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8486 } else if ((set->val.num == 0) || (set->val.num == -0.0f)) {
8487 set->val.str = strdup("0");
8488 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8489 } else if (isinf(set->val.num) && !signbit(set->val.num)) {
8490 set->val.str = strdup("Infinity");
8491 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8492 } else if (isinf(set->val.num) && signbit(set->val.num)) {
8493 set->val.str = strdup("-Infinity");
8494 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8495 } else if ((long long)set->val.num == set->val.num) {
8496 if (asprintf(&str, "%lld", (long long)set->val.num) == -1) {
8497 LOGMEM_RET(set->ctx);
8498 }
8499 set->val.str = str;
8500 } else {
8501 if (asprintf(&str, "%03.1Lf", set->val.num) == -1) {
8502 LOGMEM_RET(set->ctx);
8503 }
8504 set->val.str = str;
8505 }
8506 break;
8507 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02008508 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008509 set->val.str = strdup("true");
8510 } else {
8511 set->val.str = strdup("false");
8512 }
8513 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM);
8514 break;
8515 case LYXP_SET_NODE_SET:
8516 assert(set->used);
8517
8518 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008519 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008520
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008521 rc = cast_node_set_to_string(set, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008522 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02008523 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008524 set->val.str = str;
8525 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008526 default:
8527 LOGINT_RET(set->ctx);
8528 }
8529 set->type = LYXP_SET_STRING;
8530 }
8531
8532 /* to NUMBER */
8533 if (target == LYXP_SET_NUMBER) {
8534 switch (set->type) {
8535 case LYXP_SET_STRING:
8536 num = cast_string_to_number(set->val.str);
Michal Vaskod3678892020-05-21 10:06:58 +02008537 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008538 set->val.num = num;
8539 break;
8540 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02008541 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008542 set->val.num = 1;
8543 } else {
8544 set->val.num = 0;
8545 }
8546 break;
8547 default:
8548 LOGINT_RET(set->ctx);
8549 }
8550 set->type = LYXP_SET_NUMBER;
8551 }
8552
8553 /* to BOOLEAN */
8554 if (target == LYXP_SET_BOOLEAN) {
8555 switch (set->type) {
8556 case LYXP_SET_NUMBER:
8557 if ((set->val.num == 0) || (set->val.num == -0.0f) || isnan(set->val.num)) {
Michal Vasko004d3152020-06-11 19:59:22 +02008558 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008559 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02008560 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008561 }
8562 break;
8563 case LYXP_SET_STRING:
8564 if (set->val.str[0]) {
Michal Vaskod3678892020-05-21 10:06:58 +02008565 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008566 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008567 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02008568 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008569 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008570 }
8571 break;
8572 case LYXP_SET_NODE_SET:
Michal Vaskod3678892020-05-21 10:06:58 +02008573 if (set->used) {
8574 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008575 set->val.bln = 1;
Michal Vaskod3678892020-05-21 10:06:58 +02008576 } else {
8577 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008578 set->val.bln = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02008579 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008580 break;
8581 default:
8582 LOGINT_RET(set->ctx);
8583 }
8584 set->type = LYXP_SET_BOOLEAN;
8585 }
8586
Michal Vasko03ff5a72019-09-11 13:49:33 +02008587 return LY_SUCCESS;
8588}
8589
8590LY_ERR
8591lyxp_atomize(struct lyxp_expr *exp, LYD_FORMAT format, const struct lys_module *local_mod, const struct lysc_node *ctx_scnode,
8592 enum lyxp_node_type ctx_scnode_type, struct lyxp_set *set, int options)
8593{
8594 struct ly_ctx *ctx;
Michal Vasko004d3152020-06-11 19:59:22 +02008595 const struct lysc_node *real_ctx_scnode;
8596 uint16_t tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008597
Michal Vasko004d3152020-06-11 19:59:22 +02008598 LY_CHECK_ARG_RET(NULL, exp, local_mod, ctx_scnode, set, LY_EINVAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008599
8600 ctx = local_mod->ctx;
Michal Vasko004d3152020-06-11 19:59:22 +02008601 if ((ctx_scnode_type == LYXP_NODE_ROOT) || (ctx_scnode_type == LYXP_NODE_ROOT_CONFIG)) {
8602 /* we always need some context node because it is used for resolving unqualified names */
8603 real_ctx_scnode = NULL;
8604 } else {
8605 real_ctx_scnode = ctx_scnode;
8606 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008607
8608 /* prepare set for evaluation */
Michal Vasko004d3152020-06-11 19:59:22 +02008609 tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008610 memset(set, 0, sizeof *set);
8611 set->type = LYXP_SET_SCNODE_SET;
Michal Vasko004d3152020-06-11 19:59:22 +02008612 lyxp_set_scnode_insert_node(set, real_ctx_scnode, ctx_scnode_type);
Michal Vasko5c4e5892019-11-14 12:31:38 +01008613 set->val.scnodes[0].in_ctx = -2;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008614 set->ctx = ctx;
8615 set->ctx_scnode = ctx_scnode;
Michal Vasko004d3152020-06-11 19:59:22 +02008616 set->root_type = lyxp_get_root_type(NULL, real_ctx_scnode, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008617 set->local_mod = local_mod;
8618 set->format = format;
8619
8620 /* evaluate */
Michal Vasko004d3152020-06-11 19:59:22 +02008621 return eval_expr_select(exp, &tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008622}