blob: fae13dcab963757c579bfe528c90f30e3200fdc0 [file] [log] [blame]
Radek Krejcib1646a92018-11-02 16:08:26 +01001/**
2 * @file xpath.c
3 * @author Michal Vasko <mvasko@cesnet.cz>
4 * @brief YANG XPath evaluation functions
5 *
Michal Vasko61ac2f62020-05-25 12:39:51 +02006 * Copyright (c) 2015 - 2020 CESNET, z.s.p.o.
Radek Krejcib1646a92018-11-02 16:08:26 +01007 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
Michal Vasko03ff5a72019-09-11 13:49:33 +020014#define _GNU_SOURCE
15
16/* needed by libmath functions isfinite(), isinf(), isnan(), signbit(), ... */
17#define _ISOC99_SOURCE
Radek Krejcib1646a92018-11-02 16:08:26 +010018
Radek Krejci535ea9f2020-05-29 16:01:05 +020019#include "xpath.h"
Radek Krejcib1646a92018-11-02 16:08:26 +010020
Radek Krejci535ea9f2020-05-29 16:01:05 +020021#include <assert.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010022#include <ctype.h>
Radek Krejci535ea9f2020-05-29 16:01:05 +020023#include <errno.h>
24#include <limits.h>
25#include <math.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020026#include <stdint.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010027#include <stdio.h>
28#include <stdlib.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010029#include <string.h>
Radek Krejcib1646a92018-11-02 16:08:26 +010030
Radek Krejci535ea9f2020-05-29 16:01:05 +020031#include "common.h"
Michal Vasko5aa44c02020-06-29 11:47:02 +020032#include "compat.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020033#include "context.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020034#include "dict.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020035#include "hash_table.h"
Michal Vasko004d3152020-06-11 19:59:22 +020036#include "path.h"
Michal Vasko03ff5a72019-09-11 13:49:33 +020037#include "plugins_types.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020038#include "printer.h"
39#include "printer_data.h"
40#include "tree.h"
41#include "tree_data_internal.h"
42#include "tree_schema_internal.h"
43#include "xml.h"
Michal Vasko03ff5a72019-09-11 13:49:33 +020044
Michal Vasko004d3152020-06-11 19:59:22 +020045static LY_ERR reparse_or_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx);
46static LY_ERR eval_expr_select(struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_expr_type etype, struct lyxp_set *set, int options);
Michal Vasko03ff5a72019-09-11 13:49:33 +020047
48/**
49 * @brief Print the type of an XPath \p set.
50 *
51 * @param[in] set Set to use.
52 * @return Set type string.
53 */
54static const char *
55print_set_type(struct lyxp_set *set)
56{
57 switch (set->type) {
Michal Vasko03ff5a72019-09-11 13:49:33 +020058 case LYXP_SET_NODE_SET:
59 return "node set";
60 case LYXP_SET_SCNODE_SET:
61 return "schema node set";
62 case LYXP_SET_BOOLEAN:
63 return "boolean";
64 case LYXP_SET_NUMBER:
65 return "number";
66 case LYXP_SET_STRING:
67 return "string";
68 }
69
70 return NULL;
71}
72
Michal Vasko24cddf82020-06-01 08:17:01 +020073const char *
74lyxp_print_token(enum lyxp_token tok)
Michal Vasko03ff5a72019-09-11 13:49:33 +020075{
76 switch (tok) {
77 case LYXP_TOKEN_PAR1:
78 return "(";
79 case LYXP_TOKEN_PAR2:
80 return ")";
81 case LYXP_TOKEN_BRACK1:
82 return "[";
83 case LYXP_TOKEN_BRACK2:
84 return "]";
85 case LYXP_TOKEN_DOT:
86 return ".";
87 case LYXP_TOKEN_DDOT:
88 return "..";
89 case LYXP_TOKEN_AT:
90 return "@";
91 case LYXP_TOKEN_COMMA:
92 return ",";
93 case LYXP_TOKEN_NAMETEST:
94 return "NameTest";
95 case LYXP_TOKEN_NODETYPE:
96 return "NodeType";
97 case LYXP_TOKEN_FUNCNAME:
98 return "FunctionName";
Michal Vasko3e48bf32020-06-01 08:39:07 +020099 case LYXP_TOKEN_OPER_LOG:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200100 return "Operator(Logic)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200101 case LYXP_TOKEN_OPER_EQUAL:
102 return "Operator(Equal)";
103 case LYXP_TOKEN_OPER_NEQUAL:
104 return "Operator(Non-equal)";
105 case LYXP_TOKEN_OPER_COMP:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200106 return "Operator(Comparison)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200107 case LYXP_TOKEN_OPER_MATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200108 return "Operator(Math)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200109 case LYXP_TOKEN_OPER_UNI:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200110 return "Operator(Union)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200111 case LYXP_TOKEN_OPER_PATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200112 return "Operator(Path)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200113 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko14676352020-05-29 11:35:55 +0200114 return "Operator(Recursive Path)";
Michal Vasko03ff5a72019-09-11 13:49:33 +0200115 case LYXP_TOKEN_LITERAL:
116 return "Literal";
117 case LYXP_TOKEN_NUMBER:
118 return "Number";
119 default:
120 LOGINT(NULL);
121 return "";
122 }
123}
124
125/**
126 * @brief Print the whole expression \p exp to debug output.
127 *
128 * @param[in] exp Expression to use.
129 */
130static void
131print_expr_struct_debug(struct lyxp_expr *exp)
132{
133 uint16_t i, j;
134 char tmp[128];
135
136 if (!exp || (ly_log_level < LY_LLDBG)) {
137 return;
138 }
139
140 LOGDBG(LY_LDGXPATH, "expression \"%s\":", exp->expr);
141 for (i = 0; i < exp->used; ++i) {
Michal Vasko24cddf82020-06-01 08:17:01 +0200142 sprintf(tmp, "\ttoken %s, in expression \"%.*s\"", lyxp_print_token(exp->tokens[i]), exp->tok_len[i],
Michal Vasko03ff5a72019-09-11 13:49:33 +0200143 &exp->expr[exp->tok_pos[i]]);
144 if (exp->repeat[i]) {
145 sprintf(tmp + strlen(tmp), " (repeat %d", exp->repeat[i][0]);
146 for (j = 1; exp->repeat[i][j]; ++j) {
147 sprintf(tmp + strlen(tmp), ", %d", exp->repeat[i][j]);
148 }
149 strcat(tmp, ")");
150 }
151 LOGDBG(LY_LDGXPATH, tmp);
152 }
153}
154
155#ifndef NDEBUG
156
157/**
158 * @brief Print XPath set content to debug output.
159 *
160 * @param[in] set Set to print.
161 */
162static void
163print_set_debug(struct lyxp_set *set)
164{
165 uint32_t i;
166 char *str;
167 int dynamic;
168 struct lyxp_set_node *item;
169 struct lyxp_set_scnode *sitem;
170
171 if (ly_log_level < LY_LLDBG) {
172 return;
173 }
174
175 switch (set->type) {
176 case LYXP_SET_NODE_SET:
177 LOGDBG(LY_LDGXPATH, "set NODE SET:");
178 for (i = 0; i < set->used; ++i) {
179 item = &set->val.nodes[i];
180
181 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +0100182 case LYXP_NODE_NONE:
183 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): NONE", i + 1, item->pos);
184 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200185 case LYXP_NODE_ROOT:
186 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT", i + 1, item->pos);
187 break;
188 case LYXP_NODE_ROOT_CONFIG:
189 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT CONFIG", i + 1, item->pos);
190 break;
191 case LYXP_NODE_ELEM:
192 if ((item->node->schema->nodetype == LYS_LIST)
193 && (((struct lyd_node_inner *)item->node)->child->schema->nodetype == LYS_LEAF)) {
194 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (1st child val: %s)", i + 1, item->pos,
195 item->node->schema->name,
Michal Vasko5bfd4be2020-06-23 13:26:19 +0200196 (str = (char *)lyd_value2str((struct lyd_node_term *)lyd_node_children(item->node, 0), &dynamic)));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200197 if (dynamic) {
198 free(str);
199 }
200 } else if (((struct lyd_node_inner *)item->node)->schema->nodetype == LYS_LEAFLIST) {
201 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (val: %s)", i + 1, item->pos,
202 item->node->schema->name,
203 (str = (char *)lyd_value2str((struct lyd_node_term *)item->node, &dynamic)));
204 if (dynamic) {
205 free(str);
206 }
207 } else {
208 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s", i + 1, item->pos, item->node->schema->name);
209 }
210 break;
211 case LYXP_NODE_TEXT:
212 if (item->node->schema->nodetype & LYS_ANYDATA) {
213 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): TEXT <%s>", i + 1, item->pos,
214 item->node->schema->nodetype == LYS_ANYXML ? "anyxml" : "anydata");
215 } else {
216 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): TEXT %s", i + 1, item->pos,
217 (str = (char *)lyd_value2str((struct lyd_node_term *)item->node, &dynamic)));
218 if (dynamic) {
219 free(str);
220 }
221 }
222 break;
Michal Vasko9f96a052020-03-10 09:41:45 +0100223 case LYXP_NODE_META:
224 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): META %s = %s", i + 1, item->pos, set->val.meta[i].meta->name,
225 set->val.meta[i].meta->value);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200226 break;
227 }
228 }
229 break;
230
231 case LYXP_SET_SCNODE_SET:
232 LOGDBG(LY_LDGXPATH, "set SCNODE SET:");
233 for (i = 0; i < set->used; ++i) {
234 sitem = &set->val.scnodes[i];
235
236 switch (sitem->type) {
237 case LYXP_NODE_ROOT:
238 LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT", i + 1, sitem->in_ctx);
239 break;
240 case LYXP_NODE_ROOT_CONFIG:
241 LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT CONFIG", i + 1, sitem->in_ctx);
242 break;
243 case LYXP_NODE_ELEM:
244 LOGDBG(LY_LDGXPATH, "\t%d (%u): ELEM %s", i + 1, sitem->in_ctx, sitem->scnode->name);
245 break;
246 default:
247 LOGINT(NULL);
248 break;
249 }
250 }
251 break;
252
Michal Vasko03ff5a72019-09-11 13:49:33 +0200253 case LYXP_SET_BOOLEAN:
254 LOGDBG(LY_LDGXPATH, "set BOOLEAN");
Michal Vasko004d3152020-06-11 19:59:22 +0200255 LOGDBG(LY_LDGXPATH, "\t%s", (set->val.bln ? "true" : "false"));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200256 break;
257
258 case LYXP_SET_STRING:
259 LOGDBG(LY_LDGXPATH, "set STRING");
260 LOGDBG(LY_LDGXPATH, "\t%s", set->val.str);
261 break;
262
263 case LYXP_SET_NUMBER:
264 LOGDBG(LY_LDGXPATH, "set NUMBER");
265
266 if (isnan(set->val.num)) {
267 str = strdup("NaN");
268 } else if ((set->val.num == 0) || (set->val.num == -0.0f)) {
269 str = strdup("0");
270 } else if (isinf(set->val.num) && !signbit(set->val.num)) {
271 str = strdup("Infinity");
272 } else if (isinf(set->val.num) && signbit(set->val.num)) {
273 str = strdup("-Infinity");
274 } else if ((long long)set->val.num == set->val.num) {
275 if (asprintf(&str, "%lld", (long long)set->val.num) == -1) {
276 str = NULL;
277 }
278 } else {
279 if (asprintf(&str, "%03.1Lf", set->val.num) == -1) {
280 str = NULL;
281 }
282 }
283 LY_CHECK_ERR_RET(!str, LOGMEM(NULL), );
284
285 LOGDBG(LY_LDGXPATH, "\t%s", str);
286 free(str);
287 }
288}
289
290#endif
291
292/**
293 * @brief Realloc the string \p str.
294 *
295 * @param[in] ctx libyang context for logging.
296 * @param[in] needed How much free space is required.
297 * @param[in,out] str Pointer to the string to use.
298 * @param[in,out] used Used bytes in \p str.
299 * @param[in,out] size Allocated bytes in \p str.
300 * @return LY_ERR
301 */
302static LY_ERR
Michal Vasko52927e22020-03-16 17:26:14 +0100303cast_string_realloc(const struct ly_ctx *ctx, uint16_t needed, char **str, uint16_t *used, uint16_t *size)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200304{
305 if (*size - *used < needed) {
306 do {
307 if ((UINT16_MAX - *size) < LYXP_STRING_CAST_SIZE_STEP) {
308 LOGERR(ctx, LY_EINVAL, "XPath string length limit (%u) reached.", UINT16_MAX);
309 return LY_EINVAL;
310 }
311 *size += LYXP_STRING_CAST_SIZE_STEP;
312 } while (*size - *used < needed);
313 *str = ly_realloc(*str, *size * sizeof(char));
314 LY_CHECK_ERR_RET(!(*str), LOGMEM(ctx), LY_EMEM);
315 }
316
317 return LY_SUCCESS;
318}
319
320/**
321 * @brief Cast nodes recursively to one string @p str.
322 *
323 * @param[in] node Node to cast.
324 * @param[in] fake_cont Whether to put the data into a "fake" container.
325 * @param[in] root_type Type of the XPath root.
326 * @param[in] indent Current indent.
327 * @param[in,out] str Resulting string.
328 * @param[in,out] used Used bytes in @p str.
329 * @param[in,out] size Allocated bytes in @p str.
330 * @return LY_ERR
331 */
332static LY_ERR
333cast_string_recursive(const struct lyd_node *node, int fake_cont, enum lyxp_node_type root_type, uint16_t indent, char **str,
334 uint16_t *used, uint16_t *size)
335{
336 char *buf, *line, *ptr;
337 const char *value_str;
338 int dynamic;
339 const struct lyd_node *child;
Michal Vasko60ea6352020-06-29 13:39:39 +0200340 struct lyd_node *tree;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200341 struct lyd_node_any *any;
342 LY_ERR rc;
343
344 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) {
345 return LY_SUCCESS;
346 }
347
348 if (fake_cont) {
349 rc = cast_string_realloc(LYD_NODE_CTX(node), 1, str, used, size);
350 LY_CHECK_RET(rc);
351 strcpy(*str + (*used - 1), "\n");
352 ++(*used);
353
354 ++indent;
355 }
356
357 switch (node->schema->nodetype) {
358 case LYS_CONTAINER:
359 case LYS_LIST:
360 case LYS_RPC:
361 case LYS_NOTIF:
362 rc = cast_string_realloc(LYD_NODE_CTX(node), 1, str, used, size);
363 LY_CHECK_RET(rc);
364 strcpy(*str + (*used - 1), "\n");
365 ++(*used);
366
Michal Vasko5bfd4be2020-06-23 13:26:19 +0200367 for (child = lyd_node_children(node, 0); child; child = child->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200368 rc = cast_string_recursive(child, 0, root_type, indent + 1, str, used, size);
369 LY_CHECK_RET(rc);
370 }
371
372 break;
373
374 case LYS_LEAF:
375 case LYS_LEAFLIST:
376 value_str = lyd_value2str(((struct lyd_node_term *)node), &dynamic);
377
378 /* print indent */
379 rc = cast_string_realloc(LYD_NODE_CTX(node), indent * 2 + strlen(value_str) + 1, str, used, size);
380 if (rc != LY_SUCCESS) {
381 if (dynamic) {
382 free((char *)value_str);
383 }
384 return rc;
385 }
386 memset(*str + (*used - 1), ' ', indent * 2);
387 *used += indent * 2;
388
389 /* print value */
390 if (*used == 1) {
391 sprintf(*str + (*used - 1), "%s", value_str);
392 *used += strlen(value_str);
393 } else {
394 sprintf(*str + (*used - 1), "%s\n", value_str);
395 *used += strlen(value_str) + 1;
396 }
397 if (dynamic) {
398 free((char *)value_str);
399 }
400
401 break;
402
403 case LYS_ANYXML:
404 case LYS_ANYDATA:
405 any = (struct lyd_node_any *)node;
406 if (!(void *)any->value.tree) {
407 /* no content */
408 buf = strdup("");
409 LY_CHECK_ERR_RET(!buf, LOGMEM(LYD_NODE_CTX(node)), LY_EMEM);
410 } else {
Radek Krejci241f6b52020-05-21 18:13:49 +0200411 struct ly_out *out;
Radek Krejcia5bba312020-01-09 15:41:20 +0100412
Michal Vasko60ea6352020-06-29 13:39:39 +0200413 if (any->value_type == LYD_ANYDATA_LYB) {
414 /* try to parse it into a data tree */
415 tree = lyd_parse_mem((struct ly_ctx *)LYD_NODE_CTX(node), any->value.mem, LYD_LYB,
416 LYD_OPT_PARSE_ONLY | LYD_OPT_STRICT);
417 if (!ly_errcode(LYD_NODE_CTX(node))) {
418 /* successfully parsed */
419 free(any->value.mem);
420 any->value.tree = tree;
421 any->value_type = LYD_ANYDATA_DATATREE;
422 }
423 }
424
Michal Vasko03ff5a72019-09-11 13:49:33 +0200425 switch (any->value_type) {
426 case LYD_ANYDATA_STRING:
427 case LYD_ANYDATA_XML:
428 case LYD_ANYDATA_JSON:
429 buf = strdup(any->value.json);
430 LY_CHECK_ERR_RET(!buf, LOGMEM(LYD_NODE_CTX(node)), LY_EMEM);
431 break;
432 case LYD_ANYDATA_DATATREE:
Radek Krejci84ce7b12020-06-11 17:28:25 +0200433 LY_CHECK_RET(ly_out_new_memory(&buf, 0, &out));
Radek Krejcia5bba312020-01-09 15:41:20 +0100434 rc = lyd_print(out, any->value.tree, LYD_XML, LYDP_WITHSIBLINGS);
Radek Krejci241f6b52020-05-21 18:13:49 +0200435 ly_out_free(out, NULL, 0);
Radek Krejcia5bba312020-01-09 15:41:20 +0100436 LY_CHECK_RET(rc < 0, -rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200437 break;
Michal Vasko60ea6352020-06-29 13:39:39 +0200438 case LYD_ANYDATA_LYB:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200439 LOGERR(LYD_NODE_CTX(node), LY_EINVAL, "Cannot convert LYB anydata into string.");
Michal Vasko60ea6352020-06-29 13:39:39 +0200440 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200441 }
442 }
443
444 line = strtok_r(buf, "\n", &ptr);
445 do {
446 rc = cast_string_realloc(LYD_NODE_CTX(node), indent * 2 + strlen(line) + 1, str, used, size);
447 if (rc != LY_SUCCESS) {
448 free(buf);
449 return rc;
450 }
451 memset(*str + (*used - 1), ' ', indent * 2);
452 *used += indent * 2;
453
454 strcpy(*str + (*used - 1), line);
455 *used += strlen(line);
456
457 strcpy(*str + (*used - 1), "\n");
458 *used += 1;
459 } while ((line = strtok_r(NULL, "\n", &ptr)));
460
461 free(buf);
462 break;
463
464 default:
465 LOGINT_RET(LYD_NODE_CTX(node));
466 }
467
468 if (fake_cont) {
469 rc = cast_string_realloc(LYD_NODE_CTX(node), 1, str, used, size);
470 LY_CHECK_RET(rc);
471 strcpy(*str + (*used - 1), "\n");
472 ++(*used);
473
474 --indent;
475 }
476
477 return LY_SUCCESS;
478}
479
480/**
481 * @brief Cast an element into a string.
482 *
483 * @param[in] node Node to cast.
484 * @param[in] fake_cont Whether to put the data into a "fake" container.
485 * @param[in] root_type Type of the XPath root.
486 * @param[out] str Element cast to dynamically-allocated string.
487 * @return LY_ERR
488 */
489static LY_ERR
490cast_string_elem(struct lyd_node *node, int fake_cont, enum lyxp_node_type root_type, char **str)
491{
492 uint16_t used, size;
493 LY_ERR rc;
494
495 *str = malloc(LYXP_STRING_CAST_SIZE_START * sizeof(char));
496 LY_CHECK_ERR_RET(!*str, LOGMEM(LYD_NODE_CTX(node)), LY_EMEM);
497 (*str)[0] = '\0';
498 used = 1;
499 size = LYXP_STRING_CAST_SIZE_START;
500
501 rc = cast_string_recursive(node, fake_cont, root_type, 0, str, &used, &size);
502 if (rc != LY_SUCCESS) {
503 free(*str);
504 return rc;
505 }
506
507 if (size > used) {
508 *str = ly_realloc(*str, used * sizeof(char));
509 LY_CHECK_ERR_RET(!*str, LOGMEM(LYD_NODE_CTX(node)), LY_EMEM);
510 }
511 return LY_SUCCESS;
512}
513
514/**
515 * @brief Cast a LYXP_SET_NODE_SET set into a string.
516 * Context position aware.
517 *
518 * @param[in] set Set to cast.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200519 * @param[out] str Cast dynamically-allocated string.
520 * @return LY_ERR
521 */
522static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100523cast_node_set_to_string(struct lyxp_set *set, char **str)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200524{
Michal Vasko03ff5a72019-09-11 13:49:33 +0200525 int dynamic;
526
Michal Vasko03ff5a72019-09-11 13:49:33 +0200527 switch (set->val.nodes[0].type) {
Michal Vasko2caefc12019-11-14 16:07:56 +0100528 case LYXP_NODE_NONE:
529 /* invalid */
530 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200531 case LYXP_NODE_ROOT:
532 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100533 return cast_string_elem(set->val.nodes[0].node, 1, set->root_type, str);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200534 case LYXP_NODE_ELEM:
535 case LYXP_NODE_TEXT:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100536 return cast_string_elem(set->val.nodes[0].node, 0, set->root_type, str);
Michal Vasko9f96a052020-03-10 09:41:45 +0100537 case LYXP_NODE_META:
538 *str = (char *)lyd_meta2str(set->val.meta[0].meta, &dynamic);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200539 if (!dynamic) {
540 *str = strdup(*str);
541 if (!*str) {
542 LOGMEM_RET(set->ctx);
543 }
544 }
545 return LY_SUCCESS;
546 }
547
548 LOGINT_RET(set->ctx);
549}
550
551/**
552 * @brief Cast a string into an XPath number.
553 *
554 * @param[in] str String to use.
555 * @return Cast number.
556 */
557static long double
558cast_string_to_number(const char *str)
559{
560 long double num;
561 char *ptr;
562
563 errno = 0;
564 num = strtold(str, &ptr);
565 if (errno || *ptr) {
566 num = NAN;
567 }
568 return num;
569}
570
571/**
572 * @brief Callback for checking value equality.
573 *
574 * @param[in] val1_p First value.
575 * @param[in] val2_p Second value.
576 * @param[in] mod Whether hash table is being modified.
577 * @param[in] cb_data Callback data.
578 * @return 0 if not equal, non-zero if equal.
579 */
580static int
581set_values_equal_cb(void *val1_p, void *val2_p, int UNUSED(mod), void *UNUSED(cb_data))
582{
583 struct lyxp_set_hash_node *val1, *val2;
584
585 val1 = (struct lyxp_set_hash_node *)val1_p;
586 val2 = (struct lyxp_set_hash_node *)val2_p;
587
588 if ((val1->node == val2->node) && (val1->type == val2->type)) {
589 return 1;
590 }
591
592 return 0;
593}
594
595/**
596 * @brief Insert node and its hash into set.
597 *
598 * @param[in] set et to insert to.
599 * @param[in] node Node with hash.
600 * @param[in] type Node type.
601 */
602static void
603set_insert_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type)
604{
605 int r;
606 uint32_t i, hash;
607 struct lyxp_set_hash_node hnode;
608
609 if (!set->ht && (set->used >= LYD_HT_MIN_ITEMS)) {
610 /* create hash table and add all the nodes */
611 set->ht = lyht_new(1, sizeof(struct lyxp_set_hash_node), set_values_equal_cb, NULL, 1);
612 for (i = 0; i < set->used; ++i) {
613 hnode.node = set->val.nodes[i].node;
614 hnode.type = set->val.nodes[i].type;
615
616 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
617 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
618 hash = dict_hash_multi(hash, NULL, 0);
619
620 r = lyht_insert(set->ht, &hnode, hash, NULL);
621 assert(!r);
622 (void)r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200623
Michal Vasko9d6befd2019-12-11 14:56:56 +0100624 if (hnode.node == node) {
625 /* it was just added, do not add it twice */
626 node = NULL;
627 }
628 }
629 }
630
631 if (set->ht && node) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200632 /* add the new node into hash table */
633 hnode.node = node;
634 hnode.type = type;
635
636 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
637 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
638 hash = dict_hash_multi(hash, NULL, 0);
639
640 r = lyht_insert(set->ht, &hnode, hash, NULL);
641 assert(!r);
642 (void)r;
643 }
644}
645
646/**
647 * @brief Remove node and its hash from set.
648 *
649 * @param[in] set Set to remove from.
650 * @param[in] node Node to remove.
651 * @param[in] type Node type.
652 */
653static void
654set_remove_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type)
655{
656 int r;
657 struct lyxp_set_hash_node hnode;
658 uint32_t hash;
659
660 if (set->ht) {
661 hnode.node = node;
662 hnode.type = type;
663
664 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
665 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
666 hash = dict_hash_multi(hash, NULL, 0);
667
668 r = lyht_remove(set->ht, &hnode, hash);
669 assert(!r);
670 (void)r;
671
672 if (!set->ht->used) {
673 lyht_free(set->ht);
674 set->ht = NULL;
675 }
676 }
677}
678
679/**
680 * @brief Check whether node is in set based on its hash.
681 *
682 * @param[in] set Set to search in.
683 * @param[in] node Node to search for.
684 * @param[in] type Node type.
685 * @param[in] skip_idx Index in @p set to skip.
686 * @return LY_ERR
687 */
688static LY_ERR
689set_dup_node_hash_check(const struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type, int skip_idx)
690{
691 struct lyxp_set_hash_node hnode, *match_p;
692 uint32_t hash;
693
694 hnode.node = node;
695 hnode.type = type;
696
697 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
698 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
699 hash = dict_hash_multi(hash, NULL, 0);
700
701 if (!lyht_find(set->ht, &hnode, hash, (void **)&match_p)) {
702 if ((skip_idx > -1) && (set->val.nodes[skip_idx].node == match_p->node) && (set->val.nodes[skip_idx].type == match_p->type)) {
703 /* we found it on the index that should be skipped, find another */
704 hnode = *match_p;
705 if (lyht_find_next(set->ht, &hnode, hash, (void **)&match_p)) {
706 /* none other found */
707 return LY_SUCCESS;
708 }
709 }
710
711 return LY_EEXIST;
712 }
713
714 /* not found */
715 return LY_SUCCESS;
716}
717
Michal Vaskod3678892020-05-21 10:06:58 +0200718void
719lyxp_set_free_content(struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200720{
721 if (!set) {
722 return;
723 }
724
725 if (set->type == LYXP_SET_NODE_SET) {
726 free(set->val.nodes);
727 lyht_free(set->ht);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200728 } else if (set->type == LYXP_SET_SCNODE_SET) {
729 free(set->val.scnodes);
Michal Vaskod3678892020-05-21 10:06:58 +0200730 lyht_free(set->ht);
731 } else {
732 if (set->type == LYXP_SET_STRING) {
733 free(set->val.str);
734 }
735 set->type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200736 }
Michal Vaskod3678892020-05-21 10:06:58 +0200737
738 set->val.nodes = NULL;
739 set->used = 0;
740 set->size = 0;
741 set->ht = NULL;
742 set->ctx_pos = 0;
743 set->ctx_pos = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200744}
745
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100746/**
747 * @brief Free dynamically-allocated set.
748 *
749 * @param[in] set Set to free.
750 */
751static void
Michal Vasko03ff5a72019-09-11 13:49:33 +0200752lyxp_set_free(struct lyxp_set *set)
753{
754 if (!set) {
755 return;
756 }
757
Michal Vaskod3678892020-05-21 10:06:58 +0200758 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200759 free(set);
760}
761
762/**
763 * @brief Initialize set context.
764 *
765 * @param[in] new Set to initialize.
766 * @param[in] set Arbitrary initialized set.
767 */
768static void
769set_init(struct lyxp_set *new, struct lyxp_set *set)
770{
771 memset(new, 0, sizeof *new);
Michal Vasko02a77382019-09-12 11:47:35 +0200772 if (set) {
773 new->ctx = set->ctx;
774 new->ctx_node = set->ctx_node;
Michal Vasko588112f2019-12-10 14:51:53 +0100775 new->root_type = set->root_type;
Michal Vasko02a77382019-09-12 11:47:35 +0200776 new->local_mod = set->local_mod;
Michal Vaskof03ed032020-03-04 13:31:44 +0100777 new->tree = set->tree;
Michal Vasko02a77382019-09-12 11:47:35 +0200778 new->format = set->format;
779 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200780}
781
782/**
783 * @brief Create a deep copy of a set.
784 *
785 * @param[in] set Set to copy.
786 * @return Copy of @p set.
787 */
788static struct lyxp_set *
789set_copy(struct lyxp_set *set)
790{
791 struct lyxp_set *ret;
792 uint16_t i;
Michal Vaskoba716542019-12-16 10:01:58 +0100793 int idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200794
795 if (!set) {
796 return NULL;
797 }
798
799 ret = malloc(sizeof *ret);
800 LY_CHECK_ERR_RET(!ret, LOGMEM(set->ctx), NULL);
801 set_init(ret, set);
802
803 if (set->type == LYXP_SET_SCNODE_SET) {
804 ret->type = set->type;
805
806 for (i = 0; i < set->used; ++i) {
Michal Vaskoba716542019-12-16 10:01:58 +0100807 if ((set->val.scnodes[i].in_ctx == 1) || (set->val.scnodes[i].in_ctx == -2)) {
808 idx = lyxp_set_scnode_insert_node(ret, set->val.scnodes[i].scnode, set->val.scnodes[i].type);
Michal Vasko3f27c522020-01-06 08:37:49 +0100809 /* coverity seems to think scnodes can be NULL */
810 if ((idx == -1) || !ret->val.scnodes) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200811 lyxp_set_free(ret);
812 return NULL;
813 }
Michal Vaskoba716542019-12-16 10:01:58 +0100814 ret->val.scnodes[idx].in_ctx = set->val.scnodes[i].in_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200815 }
816 }
817 } else if (set->type == LYXP_SET_NODE_SET) {
818 ret->type = set->type;
819 ret->val.nodes = malloc(set->used * sizeof *ret->val.nodes);
820 LY_CHECK_ERR_RET(!ret->val.nodes, LOGMEM(set->ctx); free(ret), NULL);
821 memcpy(ret->val.nodes, set->val.nodes, set->used * sizeof *ret->val.nodes);
822
823 ret->used = ret->size = set->used;
824 ret->ctx_pos = set->ctx_pos;
825 ret->ctx_size = set->ctx_size;
826 ret->ht = lyht_dup(set->ht);
827 } else {
828 memcpy(ret, set, sizeof *ret);
829 if (set->type == LYXP_SET_STRING) {
830 ret->val.str = strdup(set->val.str);
831 LY_CHECK_ERR_RET(!ret->val.str, LOGMEM(set->ctx); free(ret), NULL);
832 }
833 }
834
835 return ret;
836}
837
838/**
839 * @brief Fill XPath set with a string. Any current data are disposed of.
840 *
841 * @param[in] set Set to fill.
842 * @param[in] string String to fill into \p set.
843 * @param[in] str_len Length of \p string. 0 is a valid value!
844 */
845static void
846set_fill_string(struct lyxp_set *set, const char *string, uint16_t str_len)
847{
Michal Vaskod3678892020-05-21 10:06:58 +0200848 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200849
850 set->type = LYXP_SET_STRING;
851 if ((str_len == 0) && (string[0] != '\0')) {
852 string = "";
853 }
854 set->val.str = strndup(string, str_len);
855}
856
857/**
858 * @brief Fill XPath set with a number. Any current data are disposed of.
859 *
860 * @param[in] set Set to fill.
861 * @param[in] number Number to fill into \p set.
862 */
863static void
864set_fill_number(struct lyxp_set *set, long double number)
865{
Michal Vaskod3678892020-05-21 10:06:58 +0200866 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200867
868 set->type = LYXP_SET_NUMBER;
869 set->val.num = number;
870}
871
872/**
873 * @brief Fill XPath set with a boolean. Any current data are disposed of.
874 *
875 * @param[in] set Set to fill.
876 * @param[in] boolean Boolean to fill into \p set.
877 */
878static void
879set_fill_boolean(struct lyxp_set *set, int boolean)
880{
Michal Vaskod3678892020-05-21 10:06:58 +0200881 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200882
883 set->type = LYXP_SET_BOOLEAN;
Michal Vasko004d3152020-06-11 19:59:22 +0200884 set->val.bln = boolean;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200885}
886
887/**
888 * @brief Fill XPath set with the value from another set (deep assign).
889 * Any current data are disposed of.
890 *
891 * @param[in] trg Set to fill.
892 * @param[in] src Source set to copy into \p trg.
893 */
894static void
895set_fill_set(struct lyxp_set *trg, struct lyxp_set *src)
896{
897 if (!trg || !src) {
898 return;
899 }
900
901 if (trg->type == LYXP_SET_NODE_SET) {
902 free(trg->val.nodes);
903 } else if (trg->type == LYXP_SET_STRING) {
904 free(trg->val.str);
905 }
906 set_init(trg, src);
907
908 if (src->type == LYXP_SET_SCNODE_SET) {
909 trg->type = LYXP_SET_SCNODE_SET;
910 trg->used = src->used;
911 trg->size = src->used;
912
913 trg->val.scnodes = ly_realloc(trg->val.scnodes, trg->size * sizeof *trg->val.scnodes);
914 LY_CHECK_ERR_RET(!trg->val.scnodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), );
915 memcpy(trg->val.scnodes, src->val.scnodes, src->used * sizeof *src->val.scnodes);
916 } else if (src->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +0200917 set_fill_boolean(trg, src->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200918 } else if (src->type == LYXP_SET_NUMBER) {
919 set_fill_number(trg, src->val.num);
920 } else if (src->type == LYXP_SET_STRING) {
921 set_fill_string(trg, src->val.str, strlen(src->val.str));
922 } else {
923 if (trg->type == LYXP_SET_NODE_SET) {
924 free(trg->val.nodes);
925 } else if (trg->type == LYXP_SET_STRING) {
926 free(trg->val.str);
927 }
928
Michal Vaskod3678892020-05-21 10:06:58 +0200929 assert(src->type == LYXP_SET_NODE_SET);
930
931 trg->type = LYXP_SET_NODE_SET;
932 trg->used = src->used;
933 trg->size = src->used;
934 trg->ctx_pos = src->ctx_pos;
935 trg->ctx_size = src->ctx_size;
936
937 trg->val.nodes = malloc(trg->used * sizeof *trg->val.nodes);
938 LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), );
939 memcpy(trg->val.nodes, src->val.nodes, src->used * sizeof *src->val.nodes);
940 if (src->ht) {
941 trg->ht = lyht_dup(src->ht);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200942 } else {
Michal Vaskod3678892020-05-21 10:06:58 +0200943 trg->ht = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200944 }
945 }
946}
947
948/**
949 * @brief Clear context of all schema nodes.
950 *
951 * @param[in] set Set to clear.
952 */
953static void
954set_scnode_clear_ctx(struct lyxp_set *set)
955{
956 uint32_t i;
957
958 for (i = 0; i < set->used; ++i) {
959 if (set->val.scnodes[i].in_ctx == 1) {
960 set->val.scnodes[i].in_ctx = 0;
Michal Vasko5c4e5892019-11-14 12:31:38 +0100961 } else if (set->val.scnodes[i].in_ctx == -2) {
962 set->val.scnodes[i].in_ctx = -1;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200963 }
964 }
965}
966
967/**
968 * @brief Remove a node from a set. Removing last node changes
969 * set into LYXP_SET_EMPTY. Context position aware.
970 *
971 * @param[in] set Set to use.
972 * @param[in] idx Index from @p set of the node to be removed.
973 */
974static void
975set_remove_node(struct lyxp_set *set, uint32_t idx)
976{
977 assert(set && (set->type == LYXP_SET_NODE_SET));
978 assert(idx < set->used);
979
980 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
981
982 --set->used;
983 if (set->used) {
984 memmove(&set->val.nodes[idx], &set->val.nodes[idx + 1],
985 (set->used - idx) * sizeof *set->val.nodes);
986 } else {
Michal Vaskod3678892020-05-21 10:06:58 +0200987 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200988 }
989}
990
991/**
Michal Vasko2caefc12019-11-14 16:07:56 +0100992 * @brief Remove a node from a set by setting its type to LYXP_NODE_NONE.
Michal Vasko57eab132019-09-24 11:46:26 +0200993 *
994 * @param[in] set Set to use.
995 * @param[in] idx Index from @p set of the node to be removed.
996 */
997static void
Michal Vasko2caefc12019-11-14 16:07:56 +0100998set_remove_node_none(struct lyxp_set *set, uint32_t idx)
Michal Vasko57eab132019-09-24 11:46:26 +0200999{
1000 assert(set && (set->type == LYXP_SET_NODE_SET));
1001 assert(idx < set->used);
1002
Michal Vasko2caefc12019-11-14 16:07:56 +01001003 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1004 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1005 }
1006 set->val.nodes[idx].type = LYXP_NODE_NONE;
Michal Vasko57eab132019-09-24 11:46:26 +02001007}
1008
1009/**
Michal Vasko2caefc12019-11-14 16:07:56 +01001010 * @brief Remove all LYXP_NODE_NONE nodes from a set. Removing last node changes
Michal Vasko57eab132019-09-24 11:46:26 +02001011 * set into LYXP_SET_EMPTY. Context position aware.
1012 *
1013 * @param[in] set Set to consolidate.
1014 */
1015static void
Michal Vasko2caefc12019-11-14 16:07:56 +01001016set_remove_nodes_none(struct lyxp_set *set)
Michal Vasko57eab132019-09-24 11:46:26 +02001017{
1018 uint16_t i, orig_used, end;
1019 int32_t start;
1020
Michal Vaskod3678892020-05-21 10:06:58 +02001021 assert(set);
Michal Vasko57eab132019-09-24 11:46:26 +02001022
1023 orig_used = set->used;
1024 set->used = 0;
1025 for (i = 0; i < orig_used;) {
1026 start = -1;
1027 do {
Michal Vasko2caefc12019-11-14 16:07:56 +01001028 if ((set->val.nodes[i].type != LYXP_NODE_NONE) && (start == -1)) {
Michal Vasko57eab132019-09-24 11:46:26 +02001029 start = i;
Michal Vasko2caefc12019-11-14 16:07:56 +01001030 } else if ((start > -1) && (set->val.nodes[i].type == LYXP_NODE_NONE)) {
Michal Vasko57eab132019-09-24 11:46:26 +02001031 end = i;
1032 ++i;
1033 break;
1034 }
1035
1036 ++i;
1037 if (i == orig_used) {
1038 end = i;
1039 }
1040 } while (i < orig_used);
1041
1042 if (start > -1) {
1043 /* move the whole chunk of valid nodes together */
1044 if (set->used != (unsigned)start) {
1045 memmove(&set->val.nodes[set->used], &set->val.nodes[start], (end - start) * sizeof *set->val.nodes);
1046 }
1047 set->used += end - start;
1048 }
1049 }
Michal Vasko57eab132019-09-24 11:46:26 +02001050}
1051
1052/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02001053 * @brief Check for duplicates in a node set.
1054 *
1055 * @param[in] set Set to check.
1056 * @param[in] node Node to look for in @p set.
1057 * @param[in] node_type Type of @p node.
1058 * @param[in] skip_idx Index from @p set to skip.
1059 * @return LY_ERR
1060 */
1061static LY_ERR
1062set_dup_node_check(const struct lyxp_set *set, const struct lyd_node *node, enum lyxp_node_type node_type, int skip_idx)
1063{
1064 uint32_t i;
1065
Michal Vasko2caefc12019-11-14 16:07:56 +01001066 if (set->ht && node) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001067 return set_dup_node_hash_check(set, (struct lyd_node *)node, node_type, skip_idx);
1068 }
1069
1070 for (i = 0; i < set->used; ++i) {
1071 if ((skip_idx > -1) && (i == (unsigned)skip_idx)) {
1072 continue;
1073 }
1074
1075 if ((set->val.nodes[i].node == node) && (set->val.nodes[i].type == node_type)) {
1076 return LY_EEXIST;
1077 }
1078 }
1079
1080 return LY_SUCCESS;
1081}
1082
Michal Vaskoecd62de2019-11-13 12:35:11 +01001083int
1084lyxp_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 +02001085{
1086 uint32_t i;
1087
1088 for (i = 0; i < set->used; ++i) {
1089 if ((skip_idx > -1) && (i == (unsigned)skip_idx)) {
1090 continue;
1091 }
1092
1093 if ((set->val.scnodes[i].scnode == node) && (set->val.scnodes[i].type == node_type)) {
1094 return i;
1095 }
1096 }
1097
1098 return -1;
1099}
1100
Michal Vaskoecd62de2019-11-13 12:35:11 +01001101void
1102lyxp_set_scnode_merge(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001103{
1104 uint32_t orig_used, i, j;
1105
Michal Vaskod3678892020-05-21 10:06:58 +02001106 assert((set1->type == LYXP_SET_SCNODE_SET) && (set2->type == LYXP_SET_SCNODE_SET));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001107
Michal Vaskod3678892020-05-21 10:06:58 +02001108 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001109 return;
1110 }
1111
Michal Vaskod3678892020-05-21 10:06:58 +02001112 if (!set1->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001113 memcpy(set1, set2, sizeof *set1);
1114 return;
1115 }
1116
1117 if (set1->used + set2->used > set1->size) {
1118 set1->size = set1->used + set2->used;
1119 set1->val.scnodes = ly_realloc(set1->val.scnodes, set1->size * sizeof *set1->val.scnodes);
1120 LY_CHECK_ERR_RET(!set1->val.scnodes, LOGMEM(set1->ctx), );
1121 }
1122
1123 orig_used = set1->used;
1124
1125 for (i = 0; i < set2->used; ++i) {
1126 for (j = 0; j < orig_used; ++j) {
1127 /* detect duplicities */
1128 if (set1->val.scnodes[j].scnode == set2->val.scnodes[i].scnode) {
1129 break;
1130 }
1131 }
1132
1133 if (j == orig_used) {
1134 memcpy(&set1->val.scnodes[set1->used], &set2->val.scnodes[i], sizeof *set2->val.scnodes);
1135 ++set1->used;
1136 }
1137 }
1138
Michal Vaskod3678892020-05-21 10:06:58 +02001139 lyxp_set_free_content(set2);
1140 set2->type = LYXP_SET_SCNODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001141}
1142
1143/**
1144 * @brief Insert a node into a set. Context position aware.
1145 *
1146 * @param[in] set Set to use.
1147 * @param[in] node Node to insert to @p set.
1148 * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting.
1149 * @param[in] node_type Node type of @p node.
1150 * @param[in] idx Index in @p set to insert into.
1151 */
1152static void
1153set_insert_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx)
1154{
Michal Vaskod3678892020-05-21 10:06:58 +02001155 assert(set && (set->type == LYXP_SET_NODE_SET));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001156
Michal Vaskod3678892020-05-21 10:06:58 +02001157 if (!set->size) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001158 /* first item */
1159 if (idx) {
1160 /* no real harm done, but it is a bug */
1161 LOGINT(set->ctx);
1162 idx = 0;
1163 }
1164 set->val.nodes = malloc(LYXP_SET_SIZE_START * sizeof *set->val.nodes);
1165 LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), );
1166 set->type = LYXP_SET_NODE_SET;
1167 set->used = 0;
1168 set->size = LYXP_SET_SIZE_START;
1169 set->ctx_pos = 1;
1170 set->ctx_size = 1;
1171 set->ht = NULL;
1172 } else {
1173 /* not an empty set */
1174 if (set->used == set->size) {
1175
1176 /* set is full */
1177 set->val.nodes = ly_realloc(set->val.nodes, (set->size + LYXP_SET_SIZE_STEP) * sizeof *set->val.nodes);
1178 LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), );
1179 set->size += LYXP_SET_SIZE_STEP;
1180 }
1181
1182 if (idx > set->used) {
1183 LOGINT(set->ctx);
1184 idx = set->used;
1185 }
1186
1187 /* make space for the new node */
1188 if (idx < set->used) {
1189 memmove(&set->val.nodes[idx + 1], &set->val.nodes[idx], (set->used - idx) * sizeof *set->val.nodes);
1190 }
1191 }
1192
1193 /* finally assign the value */
1194 set->val.nodes[idx].node = (struct lyd_node *)node;
1195 set->val.nodes[idx].type = node_type;
1196 set->val.nodes[idx].pos = pos;
1197 ++set->used;
1198
Michal Vasko2caefc12019-11-14 16:07:56 +01001199 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1200 set_insert_node_hash(set, (struct lyd_node *)node, node_type);
1201 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001202}
1203
Michal Vaskoecd62de2019-11-13 12:35:11 +01001204int
1205lyxp_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 +02001206{
1207 int ret;
1208
1209 assert(set->type == LYXP_SET_SCNODE_SET);
1210
Michal Vaskoecd62de2019-11-13 12:35:11 +01001211 ret = lyxp_set_scnode_dup_node_check(set, node, node_type, -1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001212 if (ret > -1) {
1213 set->val.scnodes[ret].in_ctx = 1;
1214 } else {
1215 if (set->used == set->size) {
1216 set->val.scnodes = ly_realloc(set->val.scnodes, (set->size + LYXP_SET_SIZE_STEP) * sizeof *set->val.scnodes);
1217 LY_CHECK_ERR_RET(!set->val.scnodes, LOGMEM(set->ctx), -1);
1218 set->size += LYXP_SET_SIZE_STEP;
1219 }
1220
1221 ret = set->used;
1222 set->val.scnodes[ret].scnode = (struct lysc_node *)node;
1223 set->val.scnodes[ret].type = node_type;
1224 set->val.scnodes[ret].in_ctx = 1;
1225 ++set->used;
1226 }
1227
1228 return ret;
1229}
1230
1231/**
1232 * @brief Replace a node in a set with another. Context position aware.
1233 *
1234 * @param[in] set Set to use.
1235 * @param[in] node Node to insert to @p set.
1236 * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting.
1237 * @param[in] node_type Node type of @p node.
1238 * @param[in] idx Index in @p set of the node to replace.
1239 */
1240static void
1241set_replace_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx)
1242{
1243 assert(set && (idx < set->used));
1244
Michal Vasko2caefc12019-11-14 16:07:56 +01001245 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1246 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1247 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001248 set->val.nodes[idx].node = (struct lyd_node *)node;
1249 set->val.nodes[idx].type = node_type;
1250 set->val.nodes[idx].pos = pos;
Michal Vasko2caefc12019-11-14 16:07:56 +01001251 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1252 set_insert_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1253 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001254}
1255
1256/**
1257 * @brief Set all nodes with ctx 1 to a new unique context value.
1258 *
1259 * @param[in] set Set to modify.
1260 * @return New context value.
1261 */
Michal Vasko5c4e5892019-11-14 12:31:38 +01001262static int32_t
Michal Vasko03ff5a72019-09-11 13:49:33 +02001263set_scnode_new_in_ctx(struct lyxp_set *set)
1264{
Michal Vasko5c4e5892019-11-14 12:31:38 +01001265 uint32_t i;
1266 int32_t ret_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001267
1268 assert(set->type == LYXP_SET_SCNODE_SET);
1269
1270 ret_ctx = 3;
1271retry:
1272 for (i = 0; i < set->used; ++i) {
1273 if (set->val.scnodes[i].in_ctx >= ret_ctx) {
1274 ret_ctx = set->val.scnodes[i].in_ctx + 1;
1275 goto retry;
1276 }
1277 }
1278 for (i = 0; i < set->used; ++i) {
1279 if (set->val.scnodes[i].in_ctx == 1) {
1280 set->val.scnodes[i].in_ctx = ret_ctx;
1281 }
1282 }
1283
1284 return ret_ctx;
1285}
1286
1287/**
1288 * @brief Get unique @p node position in the data.
1289 *
1290 * @param[in] node Node to find.
1291 * @param[in] node_type Node type of @p node.
1292 * @param[in] root Root node.
1293 * @param[in] root_type Type of the XPath @p root node.
1294 * @param[in] prev Node that we think is before @p node in DFS from @p root. Can optionally
1295 * be used to increase efficiency and start the DFS from this node.
1296 * @param[in] prev_pos Node @p prev position. Optional, but must be set if @p prev is set.
1297 * @return Node position.
1298 */
1299static uint32_t
1300get_node_pos(const struct lyd_node *node, enum lyxp_node_type node_type, const struct lyd_node *root,
1301 enum lyxp_node_type root_type, const struct lyd_node **prev, uint32_t *prev_pos)
1302{
1303 const struct lyd_node *next, *elem, *top_sibling;
1304 uint32_t pos = 1;
1305
1306 assert(prev && prev_pos && !root->prev->next);
1307
1308 if ((node_type == LYXP_NODE_ROOT) || (node_type == LYXP_NODE_ROOT_CONFIG)) {
1309 return 0;
1310 }
1311
1312 if (*prev) {
1313 /* start from the previous element instead from the root */
1314 elem = next = *prev;
1315 pos = *prev_pos;
1316 for (top_sibling = elem; top_sibling->parent; top_sibling = (struct lyd_node *)top_sibling->parent);
1317 goto dfs_search;
1318 }
1319
1320 for (top_sibling = root; top_sibling; top_sibling = top_sibling->next) {
1321 /* TREE DFS */
1322 LYD_TREE_DFS_BEGIN(top_sibling, next, elem) {
1323dfs_search:
1324 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (elem->schema->flags & LYS_CONFIG_R)) {
1325 goto skip_children;
1326 }
1327
1328 if (elem == node) {
1329 break;
1330 }
1331 ++pos;
1332
1333 /* TREE DFS END */
1334 /* select element for the next run - children first,
1335 * child exception for lyd_node_leaf and lyd_node_leaflist, but not the root */
1336 if (elem->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_ANYDATA)) {
1337 next = NULL;
1338 } else {
Michal Vasko5bfd4be2020-06-23 13:26:19 +02001339 next = lyd_node_children(elem, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001340 }
1341 if (!next) {
1342skip_children:
1343 /* no children */
1344 if (elem == top_sibling) {
1345 /* we are done, root has no children */
1346 elem = NULL;
1347 break;
1348 }
1349 /* try siblings */
1350 next = elem->next;
1351 }
1352 while (!next) {
1353 /* no siblings, go back through parents */
1354 if (elem->parent == top_sibling->parent) {
1355 /* we are done, no next element to process */
1356 elem = NULL;
1357 break;
1358 }
1359 /* parent is already processed, go to its sibling */
1360 elem = (struct lyd_node *)elem->parent;
1361 next = elem->next;
1362 }
1363 }
1364
1365 /* node found */
1366 if (elem) {
1367 break;
1368 }
1369 }
1370
1371 if (!elem) {
1372 if (!(*prev)) {
1373 /* we went from root and failed to find it, cannot be */
1374 LOGINT(node->schema->module->ctx);
1375 return 0;
1376 } else {
1377 *prev = NULL;
1378 *prev_pos = 0;
1379
1380 elem = next = top_sibling = root;
1381 pos = 1;
1382 goto dfs_search;
1383 }
1384 }
1385
1386 /* remember the last found node for next time */
1387 *prev = node;
1388 *prev_pos = pos;
1389
1390 return pos;
1391}
1392
1393/**
1394 * @brief Assign (fill) missing node positions.
1395 *
1396 * @param[in] set Set to fill positions in.
1397 * @param[in] root Context root node.
1398 * @param[in] root_type Context root type.
1399 * @return LY_ERR
1400 */
1401static LY_ERR
1402set_assign_pos(struct lyxp_set *set, const struct lyd_node *root, enum lyxp_node_type root_type)
1403{
1404 const struct lyd_node *prev = NULL, *tmp_node;
1405 uint32_t i, tmp_pos = 0;
1406
1407 for (i = 0; i < set->used; ++i) {
1408 if (!set->val.nodes[i].pos) {
1409 tmp_node = NULL;
1410 switch (set->val.nodes[i].type) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001411 case LYXP_NODE_META:
1412 tmp_node = set->val.meta[i].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001413 if (!tmp_node) {
1414 LOGINT_RET(root->schema->module->ctx);
1415 }
1416 /* fallthrough */
1417 case LYXP_NODE_ELEM:
1418 case LYXP_NODE_TEXT:
1419 if (!tmp_node) {
1420 tmp_node = set->val.nodes[i].node;
1421 }
1422 set->val.nodes[i].pos = get_node_pos(tmp_node, set->val.nodes[i].type, root, root_type, &prev, &tmp_pos);
1423 break;
1424 default:
1425 /* all roots have position 0 */
1426 break;
1427 }
1428 }
1429 }
1430
1431 return LY_SUCCESS;
1432}
1433
1434/**
Michal Vasko9f96a052020-03-10 09:41:45 +01001435 * @brief Get unique @p meta position in the parent metadata.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001436 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001437 * @param[in] meta Metadata to use.
1438 * @return Metadata position.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001439 */
1440static uint16_t
Michal Vasko9f96a052020-03-10 09:41:45 +01001441get_meta_pos(struct lyd_meta *meta)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001442{
1443 uint16_t pos = 0;
Michal Vasko9f96a052020-03-10 09:41:45 +01001444 struct lyd_meta *meta2;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001445
Michal Vasko9f96a052020-03-10 09:41:45 +01001446 for (meta2 = meta->parent->meta; meta2 && (meta2 != meta); meta2 = meta2->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001447 ++pos;
1448 }
1449
Michal Vasko9f96a052020-03-10 09:41:45 +01001450 assert(meta2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001451 return pos;
1452}
1453
1454/**
1455 * @brief Compare 2 nodes in respect to XPath document order.
1456 *
1457 * @param[in] item1 1st node.
1458 * @param[in] item2 2nd node.
1459 * @return If 1st > 2nd returns 1, 1st == 2nd returns 0, and 1st < 2nd returns -1.
1460 */
1461static int
1462set_sort_compare(struct lyxp_set_node *item1, struct lyxp_set_node *item2)
1463{
Michal Vasko9f96a052020-03-10 09:41:45 +01001464 uint32_t meta_pos1 = 0, meta_pos2 = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001465
1466 if (item1->pos < item2->pos) {
1467 return -1;
1468 }
1469
1470 if (item1->pos > item2->pos) {
1471 return 1;
1472 }
1473
1474 /* node positions are equal, the fun case */
1475
1476 /* 1st ELEM - == - 2nd TEXT, 1st TEXT - == - 2nd ELEM */
1477 /* special case since text nodes are actually saved as their parents */
1478 if ((item1->node == item2->node) && (item1->type != item2->type)) {
1479 if (item1->type == LYXP_NODE_ELEM) {
1480 assert(item2->type == LYXP_NODE_TEXT);
1481 return -1;
1482 } else {
1483 assert((item1->type == LYXP_NODE_TEXT) && (item2->type == LYXP_NODE_ELEM));
1484 return 1;
1485 }
1486 }
1487
Michal Vasko9f96a052020-03-10 09:41:45 +01001488 /* we need meta positions now */
1489 if (item1->type == LYXP_NODE_META) {
1490 meta_pos1 = get_meta_pos((struct lyd_meta *)item1->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001491 }
Michal Vasko9f96a052020-03-10 09:41:45 +01001492 if (item2->type == LYXP_NODE_META) {
1493 meta_pos2 = get_meta_pos((struct lyd_meta *)item2->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001494 }
1495
Michal Vasko9f96a052020-03-10 09:41:45 +01001496 /* 1st ROOT - 2nd ROOT, 1st ELEM - 2nd ELEM, 1st TEXT - 2nd TEXT, 1st META - =pos= - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001497 /* check for duplicates */
1498 if (item1->node == item2->node) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001499 assert((item1->type == item2->type) && ((item1->type != LYXP_NODE_META) || (meta_pos1 == meta_pos2)));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001500 return 0;
1501 }
1502
Michal Vasko9f96a052020-03-10 09:41:45 +01001503 /* 1st ELEM - 2nd TEXT, 1st ELEM - any pos - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001504 /* elem is always first, 2nd node is after it */
1505 if (item1->type == LYXP_NODE_ELEM) {
1506 assert(item2->type != LYXP_NODE_ELEM);
1507 return -1;
1508 }
1509
Michal Vasko9f96a052020-03-10 09:41:45 +01001510 /* 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 +02001511 /* 2nd is before 1st */
1512 if (((item1->type == LYXP_NODE_TEXT)
Michal Vasko9f96a052020-03-10 09:41:45 +01001513 && ((item2->type == LYXP_NODE_ELEM) || (item2->type == LYXP_NODE_META)))
1514 || ((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_ELEM))
1515 || (((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_META))
1516 && (meta_pos1 > meta_pos2))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001517 return 1;
1518 }
1519
Michal Vasko9f96a052020-03-10 09:41:45 +01001520 /* 1st META - any pos - 2nd TEXT, 1st META <pos< - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001521 /* 2nd is after 1st */
1522 return -1;
1523}
1524
1525/**
1526 * @brief Set cast for comparisons.
1527 *
1528 * @param[in] trg Target set to cast source into.
1529 * @param[in] src Source set.
1530 * @param[in] type Target set type.
1531 * @param[in] src_idx Source set node index.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001532 * @return LY_ERR
1533 */
1534static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001535set_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 +02001536{
1537 assert(src->type == LYXP_SET_NODE_SET);
1538
1539 set_init(trg, src);
1540
1541 /* insert node into target set */
1542 set_insert_node(trg, src->val.nodes[src_idx].node, src->val.nodes[src_idx].pos, src->val.nodes[src_idx].type, 0);
1543
1544 /* cast target set appropriately */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001545 return lyxp_set_cast(trg, type);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001546}
1547
1548#ifndef NDEBUG
1549
1550/**
1551 * @brief Bubble sort @p set into XPath document order.
1552 * Context position aware. Unused in the 'Release' build target.
1553 *
1554 * @param[in] set Set to sort.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001555 * @return How many times the whole set was traversed - 1 (if set was sorted, returns 0).
1556 */
1557static int
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001558set_sort(struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001559{
1560 uint32_t i, j;
1561 int ret = 0, cmp, inverted, change;
1562 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001563 struct lyxp_set_node item;
1564 struct lyxp_set_hash_node hnode;
1565 uint64_t hash;
1566
Michal Vasko3cf8fbf2020-05-27 15:21:21 +02001567 if ((set->type != LYXP_SET_NODE_SET) || (set->used < 2)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001568 return 0;
1569 }
1570
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001571 /* find first top-level node to be used as anchor for positions */
1572 for (root = set->ctx_node; root->parent; root = (const struct lyd_node *)root->parent);
1573 for (; root->prev->next; root = root->prev);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001574
1575 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001576 if (set_assign_pos(set, root, set->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001577 return -1;
1578 }
1579
1580 LOGDBG(LY_LDGXPATH, "SORT BEGIN");
1581 print_set_debug(set);
1582
1583 for (i = 0; i < set->used; ++i) {
1584 inverted = 0;
1585 change = 0;
1586
1587 for (j = 1; j < set->used - i; ++j) {
1588 /* compare node positions */
1589 if (inverted) {
1590 cmp = set_sort_compare(&set->val.nodes[j], &set->val.nodes[j - 1]);
1591 } else {
1592 cmp = set_sort_compare(&set->val.nodes[j - 1], &set->val.nodes[j]);
1593 }
1594
1595 /* swap if needed */
1596 if ((inverted && (cmp < 0)) || (!inverted && (cmp > 0))) {
1597 change = 1;
1598
1599 item = set->val.nodes[j - 1];
1600 set->val.nodes[j - 1] = set->val.nodes[j];
1601 set->val.nodes[j] = item;
1602 } else {
1603 /* whether node_pos1 should be smaller than node_pos2 or the other way around */
1604 inverted = !inverted;
1605 }
1606 }
1607
1608 ++ret;
1609
1610 if (!change) {
1611 break;
1612 }
1613 }
1614
1615 LOGDBG(LY_LDGXPATH, "SORT END %d", ret);
1616 print_set_debug(set);
1617
1618 /* check node hashes */
1619 if (set->used >= LYD_HT_MIN_ITEMS) {
1620 assert(set->ht);
1621 for (i = 0; i < set->used; ++i) {
1622 hnode.node = set->val.nodes[i].node;
1623 hnode.type = set->val.nodes[i].type;
1624
1625 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
1626 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
1627 hash = dict_hash_multi(hash, NULL, 0);
1628
1629 assert(!lyht_find(set->ht, &hnode, hash, NULL));
1630 }
1631 }
1632
1633 return ret - 1;
1634}
1635
1636/**
1637 * @brief Remove duplicate entries in a sorted node set.
1638 *
1639 * @param[in] set Sorted set to check.
1640 * @return LY_ERR (LY_EEXIST if some duplicates are found)
1641 */
1642static LY_ERR
1643set_sorted_dup_node_clean(struct lyxp_set *set)
1644{
1645 uint32_t i = 0;
1646 LY_ERR ret = LY_SUCCESS;
1647
1648 if (set->used > 1) {
1649 while (i < set->used - 1) {
1650 if ((set->val.nodes[i].node == set->val.nodes[i + 1].node)
1651 && (set->val.nodes[i].type == set->val.nodes[i + 1].type)) {
Michal Vasko2caefc12019-11-14 16:07:56 +01001652 set_remove_node_none(set, i + 1);
Michal Vasko57eab132019-09-24 11:46:26 +02001653 ret = LY_EEXIST;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001654 }
Michal Vasko57eab132019-09-24 11:46:26 +02001655 ++i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001656 }
1657 }
1658
Michal Vasko2caefc12019-11-14 16:07:56 +01001659 set_remove_nodes_none(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001660 return ret;
1661}
1662
1663#endif
1664
1665/**
1666 * @brief Merge 2 sorted sets into one.
1667 *
1668 * @param[in,out] trg Set to merge into. Duplicates are removed.
1669 * @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 +02001670 * @return LY_ERR
1671 */
1672static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001673set_sorted_merge(struct lyxp_set *trg, struct lyxp_set *src)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001674{
1675 uint32_t i, j, k, count, dup_count;
1676 int cmp;
1677 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001678
Michal Vaskod3678892020-05-21 10:06:58 +02001679 if ((trg->type != LYXP_SET_NODE_SET) || (src->type != LYXP_SET_NODE_SET)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001680 return LY_EINVAL;
1681 }
1682
Michal Vaskod3678892020-05-21 10:06:58 +02001683 if (!src->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001684 return LY_SUCCESS;
Michal Vaskod3678892020-05-21 10:06:58 +02001685 } else if (!trg->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001686 set_fill_set(trg, src);
Michal Vaskod3678892020-05-21 10:06:58 +02001687 lyxp_set_free_content(src);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001688 return LY_SUCCESS;
1689 }
1690
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001691 /* find first top-level node to be used as anchor for positions */
1692 for (root = trg->ctx_node; root->parent; root = (const struct lyd_node *)root->parent);
1693 for (; root->prev->next; root = root->prev);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001694
1695 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001696 if (set_assign_pos(trg, root, trg->root_type) || set_assign_pos(src, root, src->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001697 return LY_EINT;
1698 }
1699
1700#ifndef NDEBUG
1701 LOGDBG(LY_LDGXPATH, "MERGE target");
1702 print_set_debug(trg);
1703 LOGDBG(LY_LDGXPATH, "MERGE source");
1704 print_set_debug(src);
1705#endif
1706
1707 /* make memory for the merge (duplicates are not detected yet, so space
1708 * will likely be wasted on them, too bad) */
1709 if (trg->size - trg->used < src->used) {
1710 trg->size = trg->used + src->used;
1711
1712 trg->val.nodes = ly_realloc(trg->val.nodes, trg->size * sizeof *trg->val.nodes);
1713 LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx), LY_EMEM);
1714 }
1715
1716 i = 0;
1717 j = 0;
1718 count = 0;
1719 dup_count = 0;
1720 do {
1721 cmp = set_sort_compare(&src->val.nodes[i], &trg->val.nodes[j]);
1722 if (!cmp) {
1723 if (!count) {
1724 /* duplicate, just skip it */
1725 ++i;
1726 ++j;
1727 } else {
1728 /* we are copying something already, so let's copy the duplicate too,
1729 * we are hoping that afterwards there are some more nodes to
1730 * copy and this way we can copy them all together */
1731 ++count;
1732 ++dup_count;
1733 ++i;
1734 ++j;
1735 }
1736 } else if (cmp < 0) {
1737 /* inserting src node into trg, just remember it for now */
1738 ++count;
1739 ++i;
1740
1741 /* insert the hash now */
1742 set_insert_node_hash(trg, src->val.nodes[i - 1].node, src->val.nodes[i - 1].type);
1743 } else if (count) {
1744copy_nodes:
1745 /* time to actually copy the nodes, we have found the largest block of nodes */
1746 memmove(&trg->val.nodes[j + (count - dup_count)],
1747 &trg->val.nodes[j],
1748 (trg->used - j) * sizeof *trg->val.nodes);
1749 memcpy(&trg->val.nodes[j - dup_count], &src->val.nodes[i - count], count * sizeof *src->val.nodes);
1750
1751 trg->used += count - dup_count;
1752 /* do not change i, except the copying above, we are basically doing exactly what is in the else branch below */
1753 j += count - dup_count;
1754
1755 count = 0;
1756 dup_count = 0;
1757 } else {
1758 ++j;
1759 }
1760 } while ((i < src->used) && (j < trg->used));
1761
1762 if ((i < src->used) || count) {
1763 /* insert all the hashes first */
1764 for (k = i; k < src->used; ++k) {
1765 set_insert_node_hash(trg, src->val.nodes[k].node, src->val.nodes[k].type);
1766 }
1767
1768 /* loop ended, but we need to copy something at trg end */
1769 count += src->used - i;
1770 i = src->used;
1771 goto copy_nodes;
1772 }
1773
1774 /* we are inserting hashes before the actual node insert, which causes
1775 * situations when there were initially not enough items for a hash table,
1776 * but even after some were inserted, hash table was not created (during
1777 * insertion the number of items is not updated yet) */
1778 if (!trg->ht && (trg->used >= LYD_HT_MIN_ITEMS)) {
1779 set_insert_node_hash(trg, NULL, 0);
1780 }
1781
1782#ifndef NDEBUG
1783 LOGDBG(LY_LDGXPATH, "MERGE result");
1784 print_set_debug(trg);
1785#endif
1786
Michal Vaskod3678892020-05-21 10:06:58 +02001787 lyxp_set_free_content(src);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001788 return LY_SUCCESS;
1789}
1790
1791/*
1792 * (re)parse functions
1793 *
1794 * Parse functions parse the expression into
1795 * tokens (syntactic analysis).
1796 *
1797 * Reparse functions perform semantic analysis
1798 * (do not save the result, just a check) of
1799 * the expression and fill repeat indices.
1800 */
1801
Michal Vasko14676352020-05-29 11:35:55 +02001802LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02001803lyxp_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 +02001804{
Michal Vasko004d3152020-06-11 19:59:22 +02001805 if (exp->used == tok_idx) {
Michal Vasko14676352020-05-29 11:35:55 +02001806 if (ctx) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001807 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOF);
1808 }
Michal Vasko14676352020-05-29 11:35:55 +02001809 return LY_EINCOMPLETE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001810 }
1811
Michal Vasko004d3152020-06-11 19:59:22 +02001812 if (want_tok && (exp->tokens[tok_idx] != want_tok)) {
Michal Vasko14676352020-05-29 11:35:55 +02001813 if (ctx) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001814 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK,
Michal Vasko004d3152020-06-11 19:59:22 +02001815 lyxp_print_token(exp->tokens[tok_idx]), &exp->expr[exp->tok_pos[tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001816 }
Michal Vasko14676352020-05-29 11:35:55 +02001817 return LY_ENOT;
1818 }
1819
1820 return LY_SUCCESS;
1821}
1822
Michal Vasko004d3152020-06-11 19:59:22 +02001823LY_ERR
1824lyxp_next_token(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_token want_tok)
1825{
1826 LY_CHECK_RET(lyxp_check_token(ctx, exp, *tok_idx, want_tok));
1827
1828 /* skip the token */
1829 ++(*tok_idx);
1830
1831 return LY_SUCCESS;
1832}
1833
Michal Vasko14676352020-05-29 11:35:55 +02001834/* just like lyxp_check_token() but tests for 2 tokens */
1835static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02001836exp_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 +02001837 enum lyxp_token want_tok2)
1838{
Michal Vasko004d3152020-06-11 19:59:22 +02001839 if (exp->used == tok_idx) {
Michal Vasko14676352020-05-29 11:35:55 +02001840 if (ctx) {
1841 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOF);
1842 }
1843 return LY_EINCOMPLETE;
1844 }
1845
Michal Vasko004d3152020-06-11 19:59:22 +02001846 if ((exp->tokens[tok_idx] != want_tok1) && (exp->tokens[tok_idx] != want_tok2)) {
Michal Vasko14676352020-05-29 11:35:55 +02001847 if (ctx) {
1848 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK,
Michal Vasko004d3152020-06-11 19:59:22 +02001849 lyxp_print_token(exp->tokens[tok_idx]), &exp->expr[exp->tok_pos[tok_idx]]);
Michal Vasko14676352020-05-29 11:35:55 +02001850 }
1851 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001852 }
1853
1854 return LY_SUCCESS;
1855}
1856
1857/**
1858 * @brief Stack operation push on the repeat array.
1859 *
1860 * @param[in] exp Expression to use.
Michal Vasko004d3152020-06-11 19:59:22 +02001861 * @param[in] tok_idx Position in the expresion \p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001862 * @param[in] repeat_op_idx Index from \p exp of the operator token. This value is pushed.
1863 */
1864static void
Michal Vasko004d3152020-06-11 19:59:22 +02001865exp_repeat_push(struct lyxp_expr *exp, uint16_t tok_idx, uint16_t repeat_op_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001866{
1867 uint16_t i;
1868
Michal Vasko004d3152020-06-11 19:59:22 +02001869 if (exp->repeat[tok_idx]) {
1870 for (i = 0; exp->repeat[tok_idx][i]; ++i);
1871 exp->repeat[tok_idx] = realloc(exp->repeat[tok_idx], (i + 2) * sizeof *exp->repeat[tok_idx]);
1872 LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), );
1873 exp->repeat[tok_idx][i] = repeat_op_idx;
1874 exp->repeat[tok_idx][i + 1] = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001875 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02001876 exp->repeat[tok_idx] = calloc(2, sizeof *exp->repeat[tok_idx]);
1877 LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), );
1878 exp->repeat[tok_idx][0] = repeat_op_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001879 }
1880}
1881
1882/**
1883 * @brief Reparse Predicate. Logs directly on error.
1884 *
1885 * [7] Predicate ::= '[' Expr ']'
1886 *
1887 * @param[in] ctx Context for logging.
1888 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02001889 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001890 * @return LY_ERR
1891 */
1892static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02001893reparse_predicate(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001894{
1895 LY_ERR rc;
1896
Michal Vasko004d3152020-06-11 19:59:22 +02001897 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001898 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02001899 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001900
Michal Vasko004d3152020-06-11 19:59:22 +02001901 rc = reparse_or_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001902 LY_CHECK_RET(rc);
1903
Michal Vasko004d3152020-06-11 19:59:22 +02001904 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001905 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02001906 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001907
1908 return LY_SUCCESS;
1909}
1910
1911/**
1912 * @brief Reparse RelativeLocationPath. Logs directly on error.
1913 *
1914 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
1915 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
1916 * [6] NodeTest ::= NameTest | NodeType '(' ')'
1917 *
1918 * @param[in] ctx Context for logging.
1919 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02001920 * @param[in] tok_idx Position in the expression \p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001921 * @return LY_ERR (LY_EINCOMPLETE on forward reference)
1922 */
1923static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02001924reparse_relative_location_path(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001925{
1926 LY_ERR rc;
1927
Michal Vasko004d3152020-06-11 19:59:22 +02001928 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001929 LY_CHECK_RET(rc);
1930
1931 goto step;
1932 do {
1933 /* '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02001934 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001935
Michal Vasko004d3152020-06-11 19:59:22 +02001936 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001937 LY_CHECK_RET(rc);
1938step:
1939 /* Step */
Michal Vasko004d3152020-06-11 19:59:22 +02001940 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001941 case LYXP_TOKEN_DOT:
Michal Vasko004d3152020-06-11 19:59:22 +02001942 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001943 break;
1944
1945 case LYXP_TOKEN_DDOT:
Michal Vasko004d3152020-06-11 19:59:22 +02001946 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001947 break;
1948
1949 case LYXP_TOKEN_AT:
Michal Vasko004d3152020-06-11 19:59:22 +02001950 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001951
Michal Vasko004d3152020-06-11 19:59:22 +02001952 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001953 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02001954 if ((exp->tokens[*tok_idx] != LYXP_TOKEN_NAMETEST) && (exp->tokens[*tok_idx] != LYXP_TOKEN_NODETYPE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001955 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK,
Michal Vasko004d3152020-06-11 19:59:22 +02001956 lyxp_print_token(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001957 return LY_EVALID;
1958 }
1959 /* fall through */
1960 case LYXP_TOKEN_NAMETEST:
Michal Vasko004d3152020-06-11 19:59:22 +02001961 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001962 goto reparse_predicate;
1963 break;
1964
1965 case LYXP_TOKEN_NODETYPE:
Michal Vasko004d3152020-06-11 19:59:22 +02001966 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001967
1968 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02001969 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001970 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02001971 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001972
1973 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02001974 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001975 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02001976 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001977
1978reparse_predicate:
1979 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02001980 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
1981 rc = reparse_predicate(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001982 LY_CHECK_RET(rc);
1983 }
1984 break;
1985 default:
1986 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK,
Michal Vasko004d3152020-06-11 19:59:22 +02001987 lyxp_print_token(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001988 return LY_EVALID;
1989 }
Michal Vasko004d3152020-06-11 19:59:22 +02001990 } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001991
1992 return LY_SUCCESS;
1993}
1994
1995/**
1996 * @brief Reparse AbsoluteLocationPath. Logs directly on error.
1997 *
1998 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
1999 *
2000 * @param[in] ctx Context for logging.
2001 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002002 * @param[in] tok_idx Position in the expression \p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002003 * @return LY_ERR
2004 */
2005static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002006reparse_absolute_location_path(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002007{
2008 LY_ERR rc;
2009
Michal Vasko004d3152020-06-11 19:59:22 +02002010 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 +02002011
2012 /* '/' RelativeLocationPath? */
Michal Vasko004d3152020-06-11 19:59:22 +02002013 if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002014 /* '/' */
Michal Vasko004d3152020-06-11 19:59:22 +02002015 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002016
Michal Vasko004d3152020-06-11 19:59:22 +02002017 if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002018 return LY_SUCCESS;
2019 }
Michal Vasko004d3152020-06-11 19:59:22 +02002020 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002021 case LYXP_TOKEN_DOT:
2022 case LYXP_TOKEN_DDOT:
2023 case LYXP_TOKEN_AT:
2024 case LYXP_TOKEN_NAMETEST:
2025 case LYXP_TOKEN_NODETYPE:
Michal Vasko004d3152020-06-11 19:59:22 +02002026 rc = reparse_relative_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002027 LY_CHECK_RET(rc);
2028 /* fall through */
2029 default:
2030 break;
2031 }
2032
2033 /* '//' RelativeLocationPath */
2034 } else {
2035 /* '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02002036 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002037
Michal Vasko004d3152020-06-11 19:59:22 +02002038 rc = reparse_relative_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002039 LY_CHECK_RET(rc);
2040 }
2041
2042 return LY_SUCCESS;
2043}
2044
2045/**
2046 * @brief Reparse FunctionCall. Logs directly on error.
2047 *
2048 * [9] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
2049 *
2050 * @param[in] ctx Context for logging.
2051 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002052 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002053 * @return LY_ERR
2054 */
2055static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002056reparse_function_call(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002057{
2058 int min_arg_count = -1, max_arg_count, arg_count;
Michal Vasko004d3152020-06-11 19:59:22 +02002059 uint16_t func_tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002060 LY_ERR rc;
2061
Michal Vasko004d3152020-06-11 19:59:22 +02002062 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_FUNCNAME);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002063 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002064 func_tok_idx = *tok_idx;
2065 switch (exp->tok_len[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002066 case 3:
Michal Vasko004d3152020-06-11 19:59:22 +02002067 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002068 min_arg_count = 1;
2069 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002070 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002071 min_arg_count = 1;
2072 max_arg_count = 1;
2073 }
2074 break;
2075 case 4:
Michal Vasko004d3152020-06-11 19:59:22 +02002076 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002077 min_arg_count = 1;
2078 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002079 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002080 min_arg_count = 0;
2081 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002082 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002083 min_arg_count = 0;
2084 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002085 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002086 min_arg_count = 0;
2087 max_arg_count = 0;
2088 }
2089 break;
2090 case 5:
Michal Vasko004d3152020-06-11 19:59:22 +02002091 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002092 min_arg_count = 1;
2093 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002094 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002095 min_arg_count = 0;
2096 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002097 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002098 min_arg_count = 1;
2099 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002100 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002101 min_arg_count = 1;
2102 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002103 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002104 min_arg_count = 1;
2105 max_arg_count = 1;
2106 }
2107 break;
2108 case 6:
Michal Vasko004d3152020-06-11 19:59:22 +02002109 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002110 min_arg_count = 2;
Michal Vaskobe2e3562019-10-15 15:35:35 +02002111 max_arg_count = INT_MAX;
Michal Vasko004d3152020-06-11 19:59:22 +02002112 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002113 min_arg_count = 0;
2114 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002115 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002116 min_arg_count = 0;
2117 max_arg_count = 1;
2118 }
2119 break;
2120 case 7:
Michal Vasko004d3152020-06-11 19:59:22 +02002121 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002122 min_arg_count = 1;
2123 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002124 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002125 min_arg_count = 1;
2126 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002127 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002128 min_arg_count = 0;
2129 max_arg_count = 0;
2130 }
2131 break;
2132 case 8:
Michal Vasko004d3152020-06-11 19:59:22 +02002133 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002134 min_arg_count = 2;
2135 max_arg_count = 2;
Michal Vasko004d3152020-06-11 19:59:22 +02002136 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002137 min_arg_count = 0;
2138 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002139 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002140 min_arg_count = 2;
2141 max_arg_count = 2;
2142 }
2143 break;
2144 case 9:
Michal Vasko004d3152020-06-11 19:59:22 +02002145 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002146 min_arg_count = 2;
2147 max_arg_count = 3;
Michal Vasko004d3152020-06-11 19:59:22 +02002148 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002149 min_arg_count = 3;
2150 max_arg_count = 3;
2151 }
2152 break;
2153 case 10:
Michal Vasko004d3152020-06-11 19:59:22 +02002154 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002155 min_arg_count = 0;
2156 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002157 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002158 min_arg_count = 1;
2159 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002160 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002161 min_arg_count = 2;
2162 max_arg_count = 2;
2163 }
2164 break;
2165 case 11:
Michal Vasko004d3152020-06-11 19:59:22 +02002166 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002167 min_arg_count = 2;
2168 max_arg_count = 2;
2169 }
2170 break;
2171 case 12:
Michal Vasko004d3152020-06-11 19:59:22 +02002172 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002173 min_arg_count = 2;
2174 max_arg_count = 2;
2175 }
2176 break;
2177 case 13:
Michal Vasko004d3152020-06-11 19:59:22 +02002178 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002179 min_arg_count = 0;
2180 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002181 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002182 min_arg_count = 0;
2183 max_arg_count = 1;
2184 }
2185 break;
2186 case 15:
Michal Vasko004d3152020-06-11 19:59:22 +02002187 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002188 min_arg_count = 0;
2189 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002190 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002191 min_arg_count = 2;
2192 max_arg_count = 2;
2193 }
2194 break;
2195 case 16:
Michal Vasko004d3152020-06-11 19:59:22 +02002196 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002197 min_arg_count = 2;
2198 max_arg_count = 2;
2199 }
2200 break;
2201 case 20:
Michal Vasko004d3152020-06-11 19:59:22 +02002202 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002203 min_arg_count = 2;
2204 max_arg_count = 2;
2205 }
2206 break;
2207 }
2208 if (min_arg_count == -1) {
Michal Vasko004d3152020-06-11 19:59:22 +02002209 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 +02002210 return LY_EINVAL;
2211 }
Michal Vasko004d3152020-06-11 19:59:22 +02002212 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002213
2214 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02002215 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002216 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002217 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002218
2219 /* ( Expr ( ',' Expr )* )? */
2220 arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002221 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002222 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002223 if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002224 ++arg_count;
Michal Vasko004d3152020-06-11 19:59:22 +02002225 rc = reparse_or_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002226 LY_CHECK_RET(rc);
2227 }
Michal Vasko004d3152020-06-11 19:59:22 +02002228 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) {
2229 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002230
2231 ++arg_count;
Michal Vasko004d3152020-06-11 19:59:22 +02002232 rc = reparse_or_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002233 LY_CHECK_RET(rc);
2234 }
2235
2236 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02002237 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002238 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002239 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002240
2241 if ((arg_count < min_arg_count) || (arg_count > max_arg_count)) {
Michal Vasko004d3152020-06-11 19:59:22 +02002242 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INARGCOUNT, arg_count, exp->tok_len[func_tok_idx],
2243 &exp->expr[exp->tok_pos[func_tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002244 return LY_EVALID;
2245 }
2246
2247 return LY_SUCCESS;
2248}
2249
2250/**
2251 * @brief Reparse PathExpr. Logs directly on error.
2252 *
2253 * [10] PathExpr ::= LocationPath | PrimaryExpr Predicate*
2254 * | PrimaryExpr Predicate* '/' RelativeLocationPath
2255 * | PrimaryExpr Predicate* '//' RelativeLocationPath
2256 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
2257 * [8] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall
2258 *
2259 * @param[in] ctx Context for logging.
2260 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002261 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002262 * @return LY_ERR
2263 */
2264static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002265reparse_path_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002266{
2267 LY_ERR rc;
2268
Michal Vasko004d3152020-06-11 19:59:22 +02002269 if (lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko14676352020-05-29 11:35:55 +02002270 return LY_EVALID;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002271 }
2272
Michal Vasko004d3152020-06-11 19:59:22 +02002273 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002274 case LYXP_TOKEN_PAR1:
2275 /* '(' Expr ')' Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002276 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002277
Michal Vasko004d3152020-06-11 19:59:22 +02002278 rc = reparse_or_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002279 LY_CHECK_RET(rc);
2280
Michal Vasko004d3152020-06-11 19:59:22 +02002281 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002282 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002283 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002284 goto predicate;
2285 break;
2286 case LYXP_TOKEN_DOT:
2287 case LYXP_TOKEN_DDOT:
2288 case LYXP_TOKEN_AT:
2289 case LYXP_TOKEN_NAMETEST:
2290 case LYXP_TOKEN_NODETYPE:
2291 /* RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002292 rc = reparse_relative_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002293 LY_CHECK_RET(rc);
2294 break;
2295 case LYXP_TOKEN_FUNCNAME:
2296 /* FunctionCall */
Michal Vasko004d3152020-06-11 19:59:22 +02002297 rc = reparse_function_call(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002298 LY_CHECK_RET(rc);
2299 goto predicate;
2300 break;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002301 case LYXP_TOKEN_OPER_PATH:
2302 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02002303 /* AbsoluteLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002304 rc = reparse_absolute_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002305 LY_CHECK_RET(rc);
2306 break;
2307 case LYXP_TOKEN_LITERAL:
2308 /* Literal */
Michal Vasko004d3152020-06-11 19:59:22 +02002309 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002310 goto predicate;
2311 break;
2312 case LYXP_TOKEN_NUMBER:
2313 /* Number */
Michal Vasko004d3152020-06-11 19:59:22 +02002314 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002315 goto predicate;
2316 break;
2317 default:
2318 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK,
Michal Vasko004d3152020-06-11 19:59:22 +02002319 lyxp_print_token(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002320 return LY_EVALID;
2321 }
2322
2323 return LY_SUCCESS;
2324
2325predicate:
2326 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002327 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
2328 rc = reparse_predicate(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002329 LY_CHECK_RET(rc);
2330 }
2331
2332 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002333 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002334
2335 /* '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02002336 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002337
Michal Vasko004d3152020-06-11 19:59:22 +02002338 rc = reparse_relative_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002339 LY_CHECK_RET(rc);
2340 }
2341
2342 return LY_SUCCESS;
2343}
2344
2345/**
2346 * @brief Reparse UnaryExpr. Logs directly on error.
2347 *
2348 * [17] UnaryExpr ::= UnionExpr | '-' UnaryExpr
2349 * [18] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
2350 *
2351 * @param[in] ctx Context for logging.
2352 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002353 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002354 * @return LY_ERR
2355 */
2356static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002357reparse_unary_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002358{
2359 uint16_t prev_exp;
2360 LY_ERR rc;
2361
2362 /* ('-')* */
Michal Vasko004d3152020-06-11 19:59:22 +02002363 prev_exp = *tok_idx;
2364 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH)
2365 && (exp->expr[exp->tok_pos[*tok_idx]] == '-')) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002366 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNARY);
Michal Vasko004d3152020-06-11 19:59:22 +02002367 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002368 }
2369
2370 /* PathExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002371 prev_exp = *tok_idx;
2372 rc = reparse_path_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002373 LY_CHECK_RET(rc);
2374
2375 /* ('|' PathExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002376 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_UNI)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002377 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNION);
Michal Vasko004d3152020-06-11 19:59:22 +02002378 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002379
Michal Vasko004d3152020-06-11 19:59:22 +02002380 rc = reparse_path_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002381 LY_CHECK_RET(rc);
2382 }
2383
2384 return LY_SUCCESS;
2385}
2386
2387/**
2388 * @brief Reparse AdditiveExpr. Logs directly on error.
2389 *
2390 * [15] AdditiveExpr ::= MultiplicativeExpr
2391 * | AdditiveExpr '+' MultiplicativeExpr
2392 * | AdditiveExpr '-' MultiplicativeExpr
2393 * [16] MultiplicativeExpr ::= UnaryExpr
2394 * | MultiplicativeExpr '*' UnaryExpr
2395 * | MultiplicativeExpr 'div' UnaryExpr
2396 * | MultiplicativeExpr 'mod' UnaryExpr
2397 *
2398 * @param[in] ctx Context for logging.
2399 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002400 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002401 * @return LY_ERR
2402 */
2403static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002404reparse_additive_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002405{
2406 uint16_t prev_add_exp, prev_mul_exp;
2407 LY_ERR rc;
2408
Michal Vasko004d3152020-06-11 19:59:22 +02002409 prev_add_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002410 goto reparse_multiplicative_expr;
2411
2412 /* ('+' / '-' MultiplicativeExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002413 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH)
2414 && ((exp->expr[exp->tok_pos[*tok_idx]] == '+') || (exp->expr[exp->tok_pos[*tok_idx]] == '-'))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002415 exp_repeat_push(exp, prev_add_exp, LYXP_EXPR_ADDITIVE);
Michal Vasko004d3152020-06-11 19:59:22 +02002416 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002417
2418reparse_multiplicative_expr:
2419 /* UnaryExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002420 prev_mul_exp = *tok_idx;
2421 rc = reparse_unary_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002422 LY_CHECK_RET(rc);
2423
2424 /* ('*' / 'div' / 'mod' UnaryExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002425 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH)
2426 && ((exp->expr[exp->tok_pos[*tok_idx]] == '*') || (exp->tok_len[*tok_idx] == 3))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002427 exp_repeat_push(exp, prev_mul_exp, LYXP_EXPR_MULTIPLICATIVE);
Michal Vasko004d3152020-06-11 19:59:22 +02002428 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002429
Michal Vasko004d3152020-06-11 19:59:22 +02002430 rc = reparse_unary_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002431 LY_CHECK_RET(rc);
2432 }
2433 }
2434
2435 return LY_SUCCESS;
2436}
2437
2438/**
2439 * @brief Reparse EqualityExpr. Logs directly on error.
2440 *
2441 * [13] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
2442 * | EqualityExpr '!=' RelationalExpr
2443 * [14] RelationalExpr ::= AdditiveExpr
2444 * | RelationalExpr '<' AdditiveExpr
2445 * | RelationalExpr '>' AdditiveExpr
2446 * | RelationalExpr '<=' AdditiveExpr
2447 * | RelationalExpr '>=' AdditiveExpr
2448 *
2449 * @param[in] ctx Context for logging.
2450 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002451 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002452 * @return LY_ERR
2453 */
2454static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002455reparse_equality_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002456{
2457 uint16_t prev_eq_exp, prev_rel_exp;
2458 LY_ERR rc;
2459
Michal Vasko004d3152020-06-11 19:59:22 +02002460 prev_eq_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002461 goto reparse_additive_expr;
2462
2463 /* ('=' / '!=' RelationalExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002464 while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_EQUAL, LYXP_TOKEN_OPER_NEQUAL)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002465 exp_repeat_push(exp, prev_eq_exp, LYXP_EXPR_EQUALITY);
Michal Vasko004d3152020-06-11 19:59:22 +02002466 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002467
2468reparse_additive_expr:
2469 /* AdditiveExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002470 prev_rel_exp = *tok_idx;
2471 rc = reparse_additive_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002472 LY_CHECK_RET(rc);
2473
2474 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002475 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_COMP)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002476 exp_repeat_push(exp, prev_rel_exp, LYXP_EXPR_RELATIONAL);
Michal Vasko004d3152020-06-11 19:59:22 +02002477 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002478
Michal Vasko004d3152020-06-11 19:59:22 +02002479 rc = reparse_additive_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002480 LY_CHECK_RET(rc);
2481 }
2482 }
2483
2484 return LY_SUCCESS;
2485}
2486
2487/**
2488 * @brief Reparse OrExpr. Logs directly on error.
2489 *
2490 * [11] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
2491 * [12] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
2492 *
2493 * @param[in] ctx Context for logging.
2494 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002495 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002496 * @return LY_ERR
2497 */
2498static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002499reparse_or_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002500{
2501 uint16_t prev_or_exp, prev_and_exp;
2502 LY_ERR rc;
2503
Michal Vasko004d3152020-06-11 19:59:22 +02002504 prev_or_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002505 goto reparse_equality_expr;
2506
2507 /* ('or' AndExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002508 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 +02002509 exp_repeat_push(exp, prev_or_exp, LYXP_EXPR_OR);
Michal Vasko004d3152020-06-11 19:59:22 +02002510 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002511
2512reparse_equality_expr:
2513 /* EqualityExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002514 prev_and_exp = *tok_idx;
2515 rc = reparse_equality_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002516 LY_CHECK_RET(rc);
2517
2518 /* ('and' EqualityExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002519 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 +02002520 exp_repeat_push(exp, prev_and_exp, LYXP_EXPR_AND);
Michal Vasko004d3152020-06-11 19:59:22 +02002521 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002522
Michal Vasko004d3152020-06-11 19:59:22 +02002523 rc = reparse_equality_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002524 LY_CHECK_RET(rc);
2525 }
2526 }
2527
2528 return LY_SUCCESS;
2529}
Radek Krejcib1646a92018-11-02 16:08:26 +01002530
2531/**
2532 * @brief Parse NCName.
2533 *
2534 * @param[in] ncname Name to parse.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002535 * @return Length of @p ncname valid bytes.
Radek Krejcib1646a92018-11-02 16:08:26 +01002536 */
Radek Krejcid4270262019-01-07 15:07:25 +01002537static long int
Radek Krejcib1646a92018-11-02 16:08:26 +01002538parse_ncname(const char *ncname)
2539{
2540 unsigned int uc;
Radek Krejcid4270262019-01-07 15:07:25 +01002541 size_t size;
2542 long int len = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002543
2544 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), 0);
2545 if (!is_xmlqnamestartchar(uc) || (uc == ':')) {
2546 return len;
2547 }
2548
2549 do {
2550 len += size;
Radek Krejci9a564c92019-01-07 14:53:57 +01002551 if (!*ncname) {
2552 break;
2553 }
Radek Krejcid4270262019-01-07 15:07:25 +01002554 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), -len);
Radek Krejcib1646a92018-11-02 16:08:26 +01002555 } while (is_xmlqnamechar(uc) && (uc != ':'));
2556
2557 return len;
2558}
2559
2560/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02002561 * @brief Add @p token into the expression @p exp.
Radek Krejcib1646a92018-11-02 16:08:26 +01002562 *
Michal Vasko03ff5a72019-09-11 13:49:33 +02002563 * @param[in] ctx Context for logging.
Radek Krejcib1646a92018-11-02 16:08:26 +01002564 * @param[in] exp Expression to use.
2565 * @param[in] token Token to add.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002566 * @param[in] tok_pos Token position in the XPath expression.
Radek Krejcib1646a92018-11-02 16:08:26 +01002567 * @param[in] tok_len Token length in the XPath expression.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002568 * @return LY_ERR
Radek Krejcib1646a92018-11-02 16:08:26 +01002569 */
2570static LY_ERR
Michal Vasko14676352020-05-29 11:35:55 +02002571exp_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 +01002572{
2573 uint32_t prev;
2574
2575 if (exp->used == exp->size) {
2576 prev = exp->size;
2577 exp->size += LYXP_EXPR_SIZE_STEP;
2578 if (prev > exp->size) {
2579 LOGINT(ctx);
2580 return LY_EINT;
2581 }
2582
2583 exp->tokens = ly_realloc(exp->tokens, exp->size * sizeof *exp->tokens);
2584 LY_CHECK_ERR_RET(!exp->tokens, LOGMEM(ctx), LY_EMEM);
2585 exp->tok_pos = ly_realloc(exp->tok_pos, exp->size * sizeof *exp->tok_pos);
2586 LY_CHECK_ERR_RET(!exp->tok_pos, LOGMEM(ctx), LY_EMEM);
2587 exp->tok_len = ly_realloc(exp->tok_len, exp->size * sizeof *exp->tok_len);
2588 LY_CHECK_ERR_RET(!exp->tok_len, LOGMEM(ctx), LY_EMEM);
2589 }
2590
2591 exp->tokens[exp->used] = token;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002592 exp->tok_pos[exp->used] = tok_pos;
Radek Krejcib1646a92018-11-02 16:08:26 +01002593 exp->tok_len[exp->used] = tok_len;
2594 ++exp->used;
2595 return LY_SUCCESS;
2596}
2597
2598void
Michal Vasko14676352020-05-29 11:35:55 +02002599lyxp_expr_free(const struct ly_ctx *ctx, struct lyxp_expr *expr)
Radek Krejcib1646a92018-11-02 16:08:26 +01002600{
2601 uint16_t i;
2602
2603 if (!expr) {
2604 return;
2605 }
2606
2607 lydict_remove(ctx, expr->expr);
2608 free(expr->tokens);
2609 free(expr->tok_pos);
2610 free(expr->tok_len);
2611 if (expr->repeat) {
2612 for (i = 0; i < expr->used; ++i) {
2613 free(expr->repeat[i]);
2614 }
2615 }
2616 free(expr->repeat);
2617 free(expr);
2618}
2619
2620struct lyxp_expr *
Michal Vasko004d3152020-06-11 19:59:22 +02002621lyxp_expr_parse(const struct ly_ctx *ctx, const char *expr, size_t expr_len, int reparse)
Radek Krejcib1646a92018-11-02 16:08:26 +01002622{
2623 struct lyxp_expr *ret;
Radek Krejcid4270262019-01-07 15:07:25 +01002624 size_t parsed = 0, tok_len;
2625 long int ncname_len;
Radek Krejcib1646a92018-11-02 16:08:26 +01002626 enum lyxp_token tok_type;
2627 int prev_function_check = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002628 uint16_t tok_idx = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002629
Michal Vasko004d3152020-06-11 19:59:22 +02002630 if (!expr[0]) {
2631 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOF);
2632 return NULL;
2633 }
2634
2635 if (!expr_len) {
2636 expr_len = strlen(expr);
2637 }
2638 if (expr_len > UINT16_MAX) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002639 LOGERR(ctx, LY_EINVAL, "XPath expression cannot be longer than %ud characters.", UINT16_MAX);
2640 return NULL;
2641 }
2642
2643 /* init lyxp_expr structure */
2644 ret = calloc(1, sizeof *ret);
2645 LY_CHECK_ERR_GOTO(!ret, LOGMEM(ctx), error);
Michal Vasko004d3152020-06-11 19:59:22 +02002646 ret->expr = lydict_insert(ctx, expr, expr_len);
Radek Krejcib1646a92018-11-02 16:08:26 +01002647 LY_CHECK_ERR_GOTO(!ret->expr, LOGMEM(ctx), error);
2648 ret->used = 0;
2649 ret->size = LYXP_EXPR_SIZE_START;
2650 ret->tokens = malloc(ret->size * sizeof *ret->tokens);
2651 LY_CHECK_ERR_GOTO(!ret->tokens, LOGMEM(ctx), error);
2652
2653 ret->tok_pos = malloc(ret->size * sizeof *ret->tok_pos);
2654 LY_CHECK_ERR_GOTO(!ret->tok_pos, LOGMEM(ctx), error);
2655
2656 ret->tok_len = malloc(ret->size * sizeof *ret->tok_len);
2657 LY_CHECK_ERR_GOTO(!ret->tok_len, LOGMEM(ctx), error);
2658
Michal Vasko004d3152020-06-11 19:59:22 +02002659 /* make expr 0-terminated */
2660 expr = ret->expr;
2661
Radek Krejcib1646a92018-11-02 16:08:26 +01002662 while (is_xmlws(expr[parsed])) {
2663 ++parsed;
2664 }
2665
2666 do {
2667 if (expr[parsed] == '(') {
2668
2669 /* '(' */
2670 tok_len = 1;
2671 tok_type = LYXP_TOKEN_PAR1;
2672
2673 if (prev_function_check && ret->used && (ret->tokens[ret->used - 1] == LYXP_TOKEN_NAMETEST)) {
2674 /* it is a NodeType/FunctionName after all */
2675 if (((ret->tok_len[ret->used - 1] == 4)
2676 && (!strncmp(&expr[ret->tok_pos[ret->used - 1]], "node", 4)
2677 || !strncmp(&expr[ret->tok_pos[ret->used - 1]], "text", 4))) ||
2678 ((ret->tok_len[ret->used - 1] == 7)
2679 && !strncmp(&expr[ret->tok_pos[ret->used - 1]], "comment", 7))) {
2680 ret->tokens[ret->used - 1] = LYXP_TOKEN_NODETYPE;
2681 } else {
2682 ret->tokens[ret->used - 1] = LYXP_TOKEN_FUNCNAME;
2683 }
2684 prev_function_check = 0;
2685 }
2686
2687 } else if (expr[parsed] == ')') {
2688
2689 /* ')' */
2690 tok_len = 1;
2691 tok_type = LYXP_TOKEN_PAR2;
2692
2693 } else if (expr[parsed] == '[') {
2694
2695 /* '[' */
2696 tok_len = 1;
2697 tok_type = LYXP_TOKEN_BRACK1;
2698
2699 } else if (expr[parsed] == ']') {
2700
2701 /* ']' */
2702 tok_len = 1;
2703 tok_type = LYXP_TOKEN_BRACK2;
2704
2705 } else if (!strncmp(&expr[parsed], "..", 2)) {
2706
2707 /* '..' */
2708 tok_len = 2;
2709 tok_type = LYXP_TOKEN_DDOT;
2710
2711 } else if ((expr[parsed] == '.') && (!isdigit(expr[parsed + 1]))) {
2712
2713 /* '.' */
2714 tok_len = 1;
2715 tok_type = LYXP_TOKEN_DOT;
2716
2717 } else if (expr[parsed] == '@') {
2718
2719 /* '@' */
2720 tok_len = 1;
2721 tok_type = LYXP_TOKEN_AT;
2722
2723 } else if (expr[parsed] == ',') {
2724
2725 /* ',' */
2726 tok_len = 1;
2727 tok_type = LYXP_TOKEN_COMMA;
2728
2729 } else if (expr[parsed] == '\'') {
2730
2731 /* Literal with ' */
2732 for (tok_len = 1; (expr[parsed + tok_len] != '\0') && (expr[parsed + tok_len] != '\''); ++tok_len);
2733 LY_CHECK_ERR_GOTO(expr[parsed + tok_len] == '\0',
2734 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOE, expr[parsed], &expr[parsed]), error);
2735 ++tok_len;
2736 tok_type = LYXP_TOKEN_LITERAL;
2737
2738 } else if (expr[parsed] == '\"') {
2739
2740 /* Literal with " */
2741 for (tok_len = 1; (expr[parsed + tok_len] != '\0') && (expr[parsed + tok_len] != '\"'); ++tok_len);
2742 LY_CHECK_ERR_GOTO(expr[parsed + tok_len] == '\0',
2743 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOE, expr[parsed], &expr[parsed]), error);
2744 ++tok_len;
2745 tok_type = LYXP_TOKEN_LITERAL;
2746
2747 } else if ((expr[parsed] == '.') || (isdigit(expr[parsed]))) {
2748
2749 /* Number */
2750 for (tok_len = 0; isdigit(expr[parsed + tok_len]); ++tok_len);
2751 if (expr[parsed + tok_len] == '.') {
2752 ++tok_len;
2753 for (; isdigit(expr[parsed + tok_len]); ++tok_len);
2754 }
2755 tok_type = LYXP_TOKEN_NUMBER;
2756
2757 } else if (expr[parsed] == '/') {
2758
2759 /* Operator '/', '//' */
2760 if (!strncmp(&expr[parsed], "//", 2)) {
2761 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002762 tok_type = LYXP_TOKEN_OPER_RPATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002763 } else {
2764 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002765 tok_type = LYXP_TOKEN_OPER_PATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002766 }
Radek Krejcib1646a92018-11-02 16:08:26 +01002767
Michal Vasko3e48bf32020-06-01 08:39:07 +02002768 } else if (!strncmp(&expr[parsed], "!=", 2)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002769
Michal Vasko3e48bf32020-06-01 08:39:07 +02002770 /* Operator '!=' */
Radek Krejcib1646a92018-11-02 16:08:26 +01002771 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002772 tok_type = LYXP_TOKEN_OPER_NEQUAL;
2773
2774 } else if (!strncmp(&expr[parsed], "<=", 2) || !strncmp(&expr[parsed], ">=", 2)) {
2775
2776 /* Operator '<=', '>=' */
2777 tok_len = 2;
2778 tok_type = LYXP_TOKEN_OPER_COMP;
Radek Krejcib1646a92018-11-02 16:08:26 +01002779
2780 } else if (expr[parsed] == '|') {
2781
2782 /* Operator '|' */
2783 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002784 tok_type = LYXP_TOKEN_OPER_UNI;
Radek Krejcib1646a92018-11-02 16:08:26 +01002785
2786 } else if ((expr[parsed] == '+') || (expr[parsed] == '-')) {
2787
2788 /* Operator '+', '-' */
2789 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002790 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002791
Michal Vasko3e48bf32020-06-01 08:39:07 +02002792 } else if (expr[parsed] == '=') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002793
Michal Vasko3e48bf32020-06-01 08:39:07 +02002794 /* Operator '=' */
Radek Krejcib1646a92018-11-02 16:08:26 +01002795 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002796 tok_type = LYXP_TOKEN_OPER_EQUAL;
2797
2798 } else if ((expr[parsed] == '<') || (expr[parsed] == '>')) {
2799
2800 /* Operator '<', '>' */
2801 tok_len = 1;
2802 tok_type = LYXP_TOKEN_OPER_COMP;
Radek Krejcib1646a92018-11-02 16:08:26 +01002803
2804 } else if (ret->used && (ret->tokens[ret->used - 1] != LYXP_TOKEN_AT)
2805 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_PAR1)
2806 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_BRACK1)
2807 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_COMMA)
Michal Vasko3e48bf32020-06-01 08:39:07 +02002808 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_LOG)
2809 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_EQUAL)
2810 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_NEQUAL)
2811 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_COMP)
2812 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_MATH)
2813 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_UNI)
2814 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_PATH)
2815 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_RPATH)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002816
2817 /* Operator '*', 'or', 'and', 'mod', or 'div' */
2818 if (expr[parsed] == '*') {
2819 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002820 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002821
2822 } else if (!strncmp(&expr[parsed], "or", 2)) {
2823 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002824 tok_type = LYXP_TOKEN_OPER_LOG;
Radek Krejcib1646a92018-11-02 16:08:26 +01002825
2826 } else if (!strncmp(&expr[parsed], "and", 3)) {
2827 tok_len = 3;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002828 tok_type = LYXP_TOKEN_OPER_LOG;
Radek Krejcib1646a92018-11-02 16:08:26 +01002829
2830 } else if (!strncmp(&expr[parsed], "mod", 3) || !strncmp(&expr[parsed], "div", 3)) {
2831 tok_len = 3;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002832 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002833
2834 } else if (prev_function_check) {
Michal Vasko53078572019-05-24 08:50:15 +02002835 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "Invalid character 0x%x ('%c'), perhaps \"%.*s\" is supposed to be a function call.",
2836 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 +01002837 goto error;
2838 } else {
Radek Krejcid4270262019-01-07 15:07:25 +01002839 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INEXPR, parsed + 1, expr);
Radek Krejcib1646a92018-11-02 16:08:26 +01002840 goto error;
2841 }
2842 } else if (expr[parsed] == '*') {
2843
2844 /* NameTest '*' */
2845 tok_len = 1;
2846 tok_type = LYXP_TOKEN_NAMETEST;
2847
2848 } else {
2849
2850 /* NameTest (NCName ':' '*' | QName) or NodeType/FunctionName */
2851 ncname_len = parse_ncname(&expr[parsed]);
Radek Krejcid4270262019-01-07 15:07:25 +01002852 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 +01002853 tok_len = ncname_len;
2854
2855 if (expr[parsed + tok_len] == ':') {
2856 ++tok_len;
2857 if (expr[parsed + tok_len] == '*') {
2858 ++tok_len;
2859 } else {
2860 ncname_len = parse_ncname(&expr[parsed + tok_len]);
Radek Krejcid4270262019-01-07 15:07:25 +01002861 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 +01002862 tok_len += ncname_len;
2863 }
2864 /* remove old flag to prevent ambiguities */
2865 prev_function_check = 0;
2866 tok_type = LYXP_TOKEN_NAMETEST;
2867 } else {
2868 /* there is no prefix so it can still be NodeType/FunctionName, we can't finally decide now */
2869 prev_function_check = 1;
2870 tok_type = LYXP_TOKEN_NAMETEST;
2871 }
2872 }
2873
2874 /* store the token, move on to the next one */
2875 LY_CHECK_GOTO(exp_add_token(ctx, ret, tok_type, parsed, tok_len), error);
2876 parsed += tok_len;
2877 while (is_xmlws(expr[parsed])) {
2878 ++parsed;
2879 }
2880
2881 } while (expr[parsed]);
2882
Michal Vasko004d3152020-06-11 19:59:22 +02002883 if (reparse) {
2884 /* prealloc repeat */
2885 ret->repeat = calloc(ret->size, sizeof *ret->repeat);
2886 LY_CHECK_ERR_GOTO(!ret->repeat, LOGMEM(ctx), error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002887
Michal Vasko004d3152020-06-11 19:59:22 +02002888 /* fill repeat */
2889 LY_CHECK_GOTO(reparse_or_expr(ctx, ret, &tok_idx), error);
2890 if (ret->used > tok_idx) {
2891 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "Unparsed characters \"%s\" left at the end of an XPath expression.",
2892 &ret->expr[ret->tok_pos[tok_idx]]);
2893 goto error;
2894 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02002895 }
2896
2897 print_expr_struct_debug(ret);
2898
Radek Krejcib1646a92018-11-02 16:08:26 +01002899 return ret;
2900
2901error:
2902 lyxp_expr_free(ctx, ret);
2903 return NULL;
2904}
2905
Michal Vasko004d3152020-06-11 19:59:22 +02002906struct lyxp_expr *
2907lyxp_expr_dup(const struct ly_ctx *ctx, const struct lyxp_expr *exp)
2908{
2909 struct lyxp_expr *dup;
2910 uint32_t i, j;
2911
2912 dup = calloc(1, sizeof *dup);
2913 LY_CHECK_ERR_GOTO(!dup, LOGMEM(ctx), error);
2914
2915 dup->tokens = malloc(exp->used * sizeof *dup->tokens);
2916 LY_CHECK_ERR_GOTO(!dup->tokens, LOGMEM(ctx), error);
2917 memcpy(dup->tokens, exp->tokens, exp->used * sizeof *dup->tokens);
2918
2919 dup->tok_pos = malloc(exp->used * sizeof *dup->tok_pos);
2920 LY_CHECK_ERR_GOTO(!dup->tok_pos, LOGMEM(ctx), error);
2921 memcpy(dup->tok_pos, exp->tok_pos, exp->used * sizeof *dup->tok_pos);
2922
2923 dup->tok_len = malloc(exp->used * sizeof *dup->tok_len);
2924 LY_CHECK_ERR_GOTO(!dup->tok_len, LOGMEM(ctx), error);
2925 memcpy(dup->tok_len, exp->tok_len, exp->used * sizeof *dup->tok_len);
2926
2927 dup->repeat = malloc(exp->used * sizeof *dup->repeat);
2928 LY_CHECK_ERR_GOTO(!dup->repeat, LOGMEM(ctx), error);
2929 for (i = 0; i < exp->used; ++i) {
2930 if (!exp->repeat[i]) {
2931 dup->repeat[i] = NULL;
2932 } else {
2933 for (j = 0; exp->repeat[i][j]; ++j);
2934 /* the ending 0 as well */
2935 ++j;
2936
2937 dup->repeat[i] = malloc(j * sizeof *dup->repeat);
2938 LY_CHECK_ERR_GOTO(!dup->repeat[i], LOGMEM(ctx), error);
2939 memcpy(dup->repeat[i], exp->repeat[i], j * sizeof **dup->repeat);
2940 dup->repeat[i][j - 1] = 0;
2941 }
2942 }
2943
2944 dup->used = exp->used;
2945 dup->size = exp->used;
2946 dup->expr = lydict_insert(ctx, exp->expr, 0);
2947
2948 return dup;
2949
2950error:
2951 lyxp_expr_free(ctx, dup);
2952 return NULL;
2953}
2954
Michal Vasko03ff5a72019-09-11 13:49:33 +02002955/*
2956 * warn functions
2957 *
2958 * Warn functions check specific reasonable conditions for schema XPath
2959 * and print a warning if they are not satisfied.
2960 */
2961
2962/**
2963 * @brief Get the last-added schema node that is currently in the context.
2964 *
2965 * @param[in] set Set to search in.
2966 * @return Last-added schema context node, NULL if no node is in context.
2967 */
2968static struct lysc_node *
2969warn_get_scnode_in_ctx(struct lyxp_set *set)
2970{
2971 uint32_t i;
2972
2973 if (!set || (set->type != LYXP_SET_SCNODE_SET)) {
2974 return NULL;
2975 }
2976
2977 i = set->used;
2978 do {
2979 --i;
2980 if (set->val.scnodes[i].in_ctx == 1) {
2981 /* if there are more, simply return the first found (last added) */
2982 return set->val.scnodes[i].scnode;
2983 }
2984 } while (i);
2985
2986 return NULL;
2987}
2988
2989/**
2990 * @brief Test whether a type is numeric - integer type or decimal64.
2991 *
2992 * @param[in] type Type to test.
2993 * @return 1 if numeric, 0 otherwise.
2994 */
2995static int
2996warn_is_numeric_type(struct lysc_type *type)
2997{
2998 struct lysc_type_union *uni;
2999 int ret;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003000 LY_ARRAY_SIZE_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003001
3002 switch (type->basetype) {
3003 case LY_TYPE_DEC64:
3004 case LY_TYPE_INT8:
3005 case LY_TYPE_UINT8:
3006 case LY_TYPE_INT16:
3007 case LY_TYPE_UINT16:
3008 case LY_TYPE_INT32:
3009 case LY_TYPE_UINT32:
3010 case LY_TYPE_INT64:
3011 case LY_TYPE_UINT64:
3012 return 1;
3013 case LY_TYPE_UNION:
3014 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003015 LY_ARRAY_FOR(uni->types, u) {
3016 ret = warn_is_numeric_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003017 if (ret) {
3018 /* found a suitable type */
3019 return 1;
3020 }
3021 }
3022 /* did not find any suitable type */
3023 return 0;
3024 case LY_TYPE_LEAFREF:
3025 return warn_is_numeric_type(((struct lysc_type_leafref *)type)->realtype);
3026 default:
3027 return 0;
3028 }
3029}
3030
3031/**
3032 * @brief Test whether a type is string-like - no integers, decimal64 or binary.
3033 *
3034 * @param[in] type Type to test.
3035 * @return 1 if string, 0 otherwise.
3036 */
3037static int
3038warn_is_string_type(struct lysc_type *type)
3039{
3040 struct lysc_type_union *uni;
3041 int ret;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003042 LY_ARRAY_SIZE_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003043
3044 switch (type->basetype) {
3045 case LY_TYPE_BITS:
3046 case LY_TYPE_ENUM:
3047 case LY_TYPE_IDENT:
3048 case LY_TYPE_INST:
3049 case LY_TYPE_STRING:
3050 return 1;
3051 case LY_TYPE_UNION:
3052 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003053 LY_ARRAY_FOR(uni->types, u) {
3054 ret = warn_is_string_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003055 if (ret) {
3056 /* found a suitable type */
3057 return 1;
3058 }
3059 }
3060 /* did not find any suitable type */
3061 return 0;
3062 case LY_TYPE_LEAFREF:
3063 return warn_is_string_type(((struct lysc_type_leafref *)type)->realtype);
3064 default:
3065 return 0;
3066 }
3067}
3068
3069/**
3070 * @brief Test whether a type is one specific type.
3071 *
3072 * @param[in] type Type to test.
3073 * @param[in] base Expected type.
3074 * @return 1 if it is, 0 otherwise.
3075 */
3076static int
3077warn_is_specific_type(struct lysc_type *type, LY_DATA_TYPE base)
3078{
3079 struct lysc_type_union *uni;
3080 int ret;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003081 LY_ARRAY_SIZE_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003082
3083 if (type->basetype == base) {
3084 return 1;
3085 } else if (type->basetype == LY_TYPE_UNION) {
3086 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003087 LY_ARRAY_FOR(uni->types, u) {
3088 ret = warn_is_specific_type(uni->types[u], base);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003089 if (ret) {
3090 /* found a suitable type */
3091 return 1;
3092 }
3093 }
3094 /* did not find any suitable type */
3095 return 0;
3096 } else if (type->basetype == LY_TYPE_LEAFREF) {
3097 return warn_is_specific_type(((struct lysc_type_leafref *)type)->realtype, base);
3098 }
3099
3100 return 0;
3101}
3102
3103/**
3104 * @brief Get next type of a (union) type.
3105 *
3106 * @param[in] type Base type.
3107 * @param[in] prev_type Previously returned type.
3108 * @return Next type or NULL.
3109 */
3110static struct lysc_type *
3111warn_is_equal_type_next_type(struct lysc_type *type, struct lysc_type *prev_type)
3112{
3113 struct lysc_type_union *uni;
3114 int found = 0;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003115 LY_ARRAY_SIZE_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003116
3117 switch (type->basetype) {
3118 case LY_TYPE_UNION:
3119 uni = (struct lysc_type_union *)type;
3120 if (!prev_type) {
3121 return uni->types[0];
3122 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003123 LY_ARRAY_FOR(uni->types, u) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003124 if (found) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003125 return uni->types[u];
Michal Vasko03ff5a72019-09-11 13:49:33 +02003126 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003127 if (prev_type == uni->types[u]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003128 found = 1;
3129 }
3130 }
3131 return NULL;
3132 default:
3133 if (prev_type) {
3134 assert(type == prev_type);
3135 return NULL;
3136 } else {
3137 return type;
3138 }
3139 }
3140}
3141
3142/**
3143 * @brief Test whether 2 types have a common type.
3144 *
3145 * @param[in] type1 First type.
3146 * @param[in] type2 Second type.
3147 * @return 1 if they do, 0 otherwise.
3148 */
3149static int
3150warn_is_equal_type(struct lysc_type *type1, struct lysc_type *type2)
3151{
3152 struct lysc_type *t1, *rt1, *t2, *rt2;
3153
3154 t1 = NULL;
3155 while ((t1 = warn_is_equal_type_next_type(type1, t1))) {
3156 if (t1->basetype == LY_TYPE_LEAFREF) {
3157 rt1 = ((struct lysc_type_leafref *)t1)->realtype;
3158 } else {
3159 rt1 = t1;
3160 }
3161
3162 t2 = NULL;
3163 while ((t2 = warn_is_equal_type_next_type(type2, t2))) {
3164 if (t2->basetype == LY_TYPE_LEAFREF) {
3165 rt2 = ((struct lysc_type_leafref *)t2)->realtype;
3166 } else {
3167 rt2 = t2;
3168 }
3169
3170 if (rt2->basetype == rt1->basetype) {
3171 /* match found */
3172 return 1;
3173 }
3174 }
3175 }
3176
3177 return 0;
3178}
3179
3180/**
3181 * @brief Check both operands of comparison operators.
3182 *
3183 * @param[in] ctx Context for errors.
3184 * @param[in] set1 First operand set.
3185 * @param[in] set2 Second operand set.
3186 * @param[in] numbers_only Whether accept only numbers or other types are fine too (for '=' and '!=').
3187 * @param[in] expr Start of the expression to print with the warning.
3188 * @param[in] tok_pos Token position.
3189 */
3190static void
3191warn_operands(struct ly_ctx *ctx, struct lyxp_set *set1, struct lyxp_set *set2, int numbers_only, const char *expr, uint16_t tok_pos)
3192{
3193 struct lysc_node_leaf *node1, *node2;
3194 int leaves = 1, warning = 0;
3195
3196 node1 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set1);
3197 node2 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set2);
3198
3199 if (!node1 && !node2) {
3200 /* no node-sets involved, nothing to do */
3201 return;
3202 }
3203
3204 if (node1) {
3205 if (!(node1->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3206 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node1->nodetype), node1->name);
3207 warning = 1;
3208 leaves = 0;
3209 } else if (numbers_only && !warn_is_numeric_type(node1->type)) {
3210 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node1->name);
3211 warning = 1;
3212 }
3213 }
3214
3215 if (node2) {
3216 if (!(node2->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3217 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node2->nodetype), node2->name);
3218 warning = 1;
3219 leaves = 0;
3220 } else if (numbers_only && !warn_is_numeric_type(node2->type)) {
3221 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node2->name);
3222 warning = 1;
3223 }
3224 }
3225
3226 if (node1 && node2 && leaves && !numbers_only) {
3227 if ((warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type))
3228 || (!warn_is_numeric_type(node1->type) && warn_is_numeric_type(node2->type))
3229 || (!warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type)
3230 && !warn_is_equal_type(node1->type, node2->type))) {
3231 LOGWRN(ctx, "Incompatible types of operands \"%s\" and \"%s\" for comparison.", node1->name, node2->name);
3232 warning = 1;
3233 }
3234 }
3235
3236 if (warning) {
3237 LOGWRN(ctx, "Previous warning generated by XPath subexpression[%u] \"%.20s\".", tok_pos, expr + tok_pos);
3238 }
3239}
3240
3241/**
3242 * @brief Check that a value is valid for a leaf. If not applicable, does nothing.
3243 *
3244 * @param[in] exp Parsed XPath expression.
3245 * @param[in] set Set with the leaf/leaf-list.
3246 * @param[in] val_exp Index of the value (literal/number) in @p exp.
3247 * @param[in] equal_exp Index of the start of the equality expression in @p exp.
3248 * @param[in] last_equal_exp Index of the end of the equality expression in @p exp.
3249 */
3250static void
3251warn_equality_value(struct lyxp_expr *exp, struct lyxp_set *set, uint16_t val_exp, uint16_t equal_exp, uint16_t last_equal_exp)
3252{
3253 struct lysc_node *scnode;
3254 struct lysc_type *type;
3255 char *value;
3256 LY_ERR rc;
3257 struct ly_err_item *err = NULL;
3258
3259 if ((scnode = warn_get_scnode_in_ctx(set)) && (scnode->nodetype & (LYS_LEAF | LYS_LEAFLIST))
3260 && ((exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) || (exp->tokens[val_exp] == LYXP_TOKEN_NUMBER))) {
3261 /* check that the node can have the specified value */
3262 if (exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) {
3263 value = strndup(exp->expr + exp->tok_pos[val_exp] + 1, exp->tok_len[val_exp] - 2);
3264 } else {
3265 value = strndup(exp->expr + exp->tok_pos[val_exp], exp->tok_len[val_exp]);
3266 }
3267 if (!value) {
3268 LOGMEM(set->ctx);
3269 return;
3270 }
3271
3272 if ((((struct lysc_node_leaf *)scnode)->type->basetype == LY_TYPE_IDENT) && !strchr(value, ':')) {
3273 LOGWRN(set->ctx, "Identityref \"%s\" comparison with identity \"%s\" without prefix, consider adding"
3274 " a prefix or best using \"derived-from(-or-self)()\" functions.", scnode->name, value);
3275 LOGWRN(set->ctx, "Previous warning generated by XPath subexpression[%u] \"%.*s\".", exp->tok_pos[equal_exp],
3276 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
3277 exp->expr + exp->tok_pos[equal_exp]);
3278 }
3279
3280 type = ((struct lysc_node_leaf *)scnode)->type;
3281 if (type->basetype != LY_TYPE_IDENT) {
3282 rc = type->plugin->store(set->ctx, type, value, strlen(value), LY_TYPE_OPTS_SCHEMA,
3283 lys_resolve_prefix, (void *)type->dflt_mod, LYD_XML, NULL, NULL, NULL, NULL, &err);
3284
3285 if (err) {
3286 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type (%s).", value, err->msg);
3287 ly_err_free(err);
3288 } else if (rc != LY_SUCCESS) {
3289 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type.", value);
3290 }
3291 if (rc != LY_SUCCESS) {
3292 LOGWRN(set->ctx, "Previous warning generated by XPath subexpression[%u] \"%.*s\".", exp->tok_pos[equal_exp],
3293 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
3294 exp->expr + exp->tok_pos[equal_exp]);
3295 }
3296 }
3297 free(value);
3298 }
3299}
3300
3301/*
3302 * XPath functions
3303 */
3304
3305/**
3306 * @brief Execute the YANG 1.1 bit-is-set(node-set, string) function. Returns LYXP_SET_BOOLEAN
3307 * depending on whether the first node bit value from the second argument is set.
3308 *
3309 * @param[in] args Array of arguments.
3310 * @param[in] arg_count Count of elements in @p args.
3311 * @param[in,out] set Context and result set at the same time.
3312 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003313 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003314 */
3315static LY_ERR
3316xpath_bit_is_set(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3317{
3318 struct lyd_node_term *leaf;
3319 struct lysc_node_leaf *sleaf;
3320 struct lysc_type_bits *bits;
3321 LY_ERR rc = LY_SUCCESS;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003322 LY_ARRAY_SIZE_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003323
3324 if (options & LYXP_SCNODE_ALL) {
3325 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3326 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003327 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3328 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 +02003329 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_BITS)) {
3330 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"bits\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003331 }
3332
3333 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3334 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3335 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 +02003336 } else if (!warn_is_string_type(sleaf->type)) {
3337 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003338 }
3339 }
3340 set_scnode_clear_ctx(set);
3341 return rc;
3342 }
3343
Michal Vaskod3678892020-05-21 10:06:58 +02003344 if (args[0]->type != LYXP_SET_NODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003345 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)");
3346 return LY_EVALID;
3347 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003348 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003349 LY_CHECK_RET(rc);
3350
3351 set_fill_boolean(set, 0);
Michal Vaskod3678892020-05-21 10:06:58 +02003352 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003353 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
3354 if ((leaf->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST))
3355 && (((struct lysc_node_leaf *)leaf->schema)->type->basetype == LY_TYPE_BITS)) {
3356 bits = (struct lysc_type_bits *)((struct lysc_node_leaf *)leaf->schema)->type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003357 LY_ARRAY_FOR(bits->bits, u) {
3358 if (!strcmp(bits->bits[u].name, args[1]->val.str)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003359 set_fill_boolean(set, 1);
3360 break;
3361 }
3362 }
3363 }
3364 }
3365
3366 return LY_SUCCESS;
3367}
3368
3369/**
3370 * @brief Execute the XPath boolean(object) function. Returns LYXP_SET_BOOLEAN
3371 * with the argument converted to boolean.
3372 *
3373 * @param[in] args Array of arguments.
3374 * @param[in] arg_count Count of elements in @p args.
3375 * @param[in,out] set Context and result set at the same time.
3376 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003377 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003378 */
3379static LY_ERR
3380xpath_boolean(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3381{
3382 LY_ERR rc;
3383
3384 if (options & LYXP_SCNODE_ALL) {
3385 set_scnode_clear_ctx(set);
3386 return LY_SUCCESS;
3387 }
3388
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003389 rc = lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003390 LY_CHECK_RET(rc);
3391 set_fill_set(set, args[0]);
3392
3393 return LY_SUCCESS;
3394}
3395
3396/**
3397 * @brief Execute the XPath ceiling(number) function. Returns LYXP_SET_NUMBER
3398 * with the first argument rounded up to the nearest integer.
3399 *
3400 * @param[in] args Array of arguments.
3401 * @param[in] arg_count Count of elements in @p args.
3402 * @param[in,out] set Context and result set at the same time.
3403 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003404 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003405 */
3406static LY_ERR
3407xpath_ceiling(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3408{
3409 struct lysc_node_leaf *sleaf;
3410 LY_ERR rc = LY_SUCCESS;
3411
3412 if (options & LYXP_SCNODE_ALL) {
3413 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3414 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003415 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3416 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 +02003417 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
3418 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003419 }
3420 set_scnode_clear_ctx(set);
3421 return rc;
3422 }
3423
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003424 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003425 LY_CHECK_RET(rc);
3426 if ((long long)args[0]->val.num != args[0]->val.num) {
3427 set_fill_number(set, ((long long)args[0]->val.num) + 1);
3428 } else {
3429 set_fill_number(set, args[0]->val.num);
3430 }
3431
3432 return LY_SUCCESS;
3433}
3434
3435/**
3436 * @brief Execute the XPath concat(string, string, string*) function.
3437 * Returns LYXP_SET_STRING with the concatenation of all the arguments.
3438 *
3439 * @param[in] args Array of arguments.
3440 * @param[in] arg_count Count of elements in @p args.
3441 * @param[in,out] set Context and result set at the same time.
3442 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003443 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003444 */
3445static LY_ERR
3446xpath_concat(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
3447{
3448 uint16_t i;
3449 char *str = NULL;
3450 size_t used = 1;
3451 LY_ERR rc = LY_SUCCESS;
3452 struct lysc_node_leaf *sleaf;
3453
3454 if (options & LYXP_SCNODE_ALL) {
3455 for (i = 0; i < arg_count; ++i) {
3456 if ((args[i]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[i]))) {
3457 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3458 LOGWRN(set->ctx, "Argument #%u of %s is a %s node \"%s\".",
3459 i + 1, __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003460 } else if (!warn_is_string_type(sleaf->type)) {
3461 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 +02003462 }
3463 }
3464 }
3465 set_scnode_clear_ctx(set);
3466 return rc;
3467 }
3468
3469 for (i = 0; i < arg_count; ++i) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003470 rc = lyxp_set_cast(args[i], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003471 if (rc != LY_SUCCESS) {
3472 free(str);
3473 return rc;
3474 }
3475
3476 str = ly_realloc(str, (used + strlen(args[i]->val.str)) * sizeof(char));
3477 LY_CHECK_ERR_RET(!str, LOGMEM(set->ctx), LY_EMEM);
3478 strcpy(str + used - 1, args[i]->val.str);
3479 used += strlen(args[i]->val.str);
3480 }
3481
3482 /* free, kind of */
Michal Vaskod3678892020-05-21 10:06:58 +02003483 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003484 set->type = LYXP_SET_STRING;
3485 set->val.str = str;
3486
3487 return LY_SUCCESS;
3488}
3489
3490/**
3491 * @brief Execute the XPath contains(string, string) function.
3492 * Returns LYXP_SET_BOOLEAN whether the second argument can
3493 * be found in the first or not.
3494 *
3495 * @param[in] args Array of arguments.
3496 * @param[in] arg_count Count of elements in @p args.
3497 * @param[in,out] set Context and result set at the same time.
3498 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003499 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003500 */
3501static LY_ERR
3502xpath_contains(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3503{
3504 struct lysc_node_leaf *sleaf;
3505 LY_ERR rc = LY_SUCCESS;
3506
3507 if (options & LYXP_SCNODE_ALL) {
3508 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3509 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3510 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 +02003511 } else if (!warn_is_string_type(sleaf->type)) {
3512 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003513 }
3514 }
3515
3516 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3517 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3518 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 +02003519 } else if (!warn_is_string_type(sleaf->type)) {
3520 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003521 }
3522 }
3523 set_scnode_clear_ctx(set);
3524 return rc;
3525 }
3526
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003527 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003528 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003529 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003530 LY_CHECK_RET(rc);
3531
3532 if (strstr(args[0]->val.str, args[1]->val.str)) {
3533 set_fill_boolean(set, 1);
3534 } else {
3535 set_fill_boolean(set, 0);
3536 }
3537
3538 return LY_SUCCESS;
3539}
3540
3541/**
3542 * @brief Execute the XPath count(node-set) function. Returns LYXP_SET_NUMBER
3543 * with the size of the node-set from the argument.
3544 *
3545 * @param[in] args Array of arguments.
3546 * @param[in] arg_count Count of elements in @p args.
3547 * @param[in,out] set Context and result set at the same time.
3548 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003549 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003550 */
3551static LY_ERR
3552xpath_count(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3553{
3554 struct lysc_node *scnode = NULL;
3555 LY_ERR rc = LY_SUCCESS;
3556
3557 if (options & LYXP_SCNODE_ALL) {
3558 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(scnode = warn_get_scnode_in_ctx(args[0]))) {
3559 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003560 }
3561 set_scnode_clear_ctx(set);
3562 return rc;
3563 }
3564
Michal Vasko03ff5a72019-09-11 13:49:33 +02003565 if (args[0]->type != LYXP_SET_NODE_SET) {
3566 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "count(node-set)");
3567 return LY_EVALID;
3568 }
3569
3570 set_fill_number(set, args[0]->used);
3571 return LY_SUCCESS;
3572}
3573
3574/**
3575 * @brief Execute the XPath current() function. Returns LYXP_SET_NODE_SET
3576 * with the context with the intial node.
3577 *
3578 * @param[in] args Array of arguments.
3579 * @param[in] arg_count Count of elements in @p args.
3580 * @param[in,out] set Context and result set at the same time.
3581 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003582 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003583 */
3584static LY_ERR
3585xpath_current(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
3586{
3587 if (arg_count || args) {
3588 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGCOUNT, arg_count, "current()");
3589 return LY_EVALID;
3590 }
3591
3592 if (options & LYXP_SCNODE_ALL) {
3593 set_scnode_clear_ctx(set);
3594
Michal Vaskoecd62de2019-11-13 12:35:11 +01003595 lyxp_set_scnode_insert_node(set, set->ctx_scnode, LYXP_NODE_ELEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003596 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02003597 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003598
3599 /* position is filled later */
3600 set_insert_node(set, set->ctx_node, 0, LYXP_NODE_ELEM, 0);
3601 }
3602
3603 return LY_SUCCESS;
3604}
3605
3606/**
3607 * @brief Execute the YANG 1.1 deref(node-set) function. Returns LYXP_SET_NODE_SET with either
3608 * leafref or instance-identifier target node(s).
3609 *
3610 * @param[in] args Array of arguments.
3611 * @param[in] arg_count Count of elements in @p args.
3612 * @param[in,out] set Context and result set at the same time.
3613 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003614 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003615 */
3616static LY_ERR
3617xpath_deref(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3618{
3619 struct lyd_node_term *leaf;
Michal Vasko42e497c2020-01-06 08:38:25 +01003620 struct lysc_node_leaf *sleaf = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02003621 struct lysc_type_leafref *lref;
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003622 const struct lysc_node *target;
Michal Vasko004d3152020-06-11 19:59:22 +02003623 struct ly_path *p;
3624 struct lyd_node *node;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003625 char *errmsg = NULL;
3626 const char *val;
3627 int dynamic;
Michal Vasko00cbf532020-06-15 13:58:47 +02003628 uint8_t oper;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003629 LY_ERR rc = LY_SUCCESS;
3630
3631 if (options & LYXP_SCNODE_ALL) {
3632 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3633 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003634 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3635 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 +02003636 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_LEAFREF) && !warn_is_specific_type(sleaf->type, LY_TYPE_INST)) {
3637 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"leafref\" nor \"instance-identifier\".",
3638 __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003639 }
3640 set_scnode_clear_ctx(set);
Michal Vasko42e497c2020-01-06 08:38:25 +01003641 if (sleaf && (sleaf->type->basetype == LY_TYPE_LEAFREF)) {
Michal Vasko004d3152020-06-11 19:59:22 +02003642 lref = (struct lysc_type_leafref *)sleaf->type;
Michal Vasko00cbf532020-06-15 13:58:47 +02003643 oper = lysc_is_output((struct lysc_node *)sleaf) ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT;
Michal Vasko004d3152020-06-11 19:59:22 +02003644
3645 /* it was already evaluated on schema, it must succeed */
3646 if (set->format == LYD_JSON) {
Michal Vasko00cbf532020-06-15 13:58:47 +02003647 rc = ly_path_compile(set->ctx, sleaf->module, (struct lysc_node *)sleaf, lref->path, LY_PATH_LREF_TRUE,
3648 oper, LY_PATH_TARGET_MANY, lydjson_resolve_prefix, NULL, LYD_JSON, &p);
Michal Vasko004d3152020-06-11 19:59:22 +02003649 } else {
3650 assert(set->format == LYD_SCHEMA);
Michal Vasko00cbf532020-06-15 13:58:47 +02003651 rc = ly_path_compile(set->ctx, sleaf->module, (struct lysc_node *)sleaf, lref->path, LY_PATH_LREF_TRUE,
3652 oper, LY_PATH_TARGET_MANY, lys_resolve_prefix, lref->path_context, LYD_SCHEMA, &p);
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003653 }
Michal Vasko004d3152020-06-11 19:59:22 +02003654 assert(!rc);
3655
3656 /* get the target node */
3657 target = p[LY_ARRAY_SIZE(p) - 1].node;
3658 ly_path_free(set->ctx, p);
3659
3660 lyxp_set_scnode_insert_node(set, target, LYXP_NODE_ELEM);
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003661 }
3662
Michal Vasko03ff5a72019-09-11 13:49:33 +02003663 return rc;
3664 }
3665
Michal Vaskod3678892020-05-21 10:06:58 +02003666 if (args[0]->type != LYXP_SET_NODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003667 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "deref(node-set)");
3668 return LY_EVALID;
3669 }
3670
Michal Vaskod3678892020-05-21 10:06:58 +02003671 lyxp_set_free_content(set);
3672 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003673 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
3674 sleaf = (struct lysc_node_leaf *)leaf->schema;
3675 if (sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
3676 if (sleaf->type->basetype == LY_TYPE_LEAFREF) {
3677 /* find leafref target */
Michal Vasko004d3152020-06-11 19:59:22 +02003678 if (ly_type_find_leafref((struct lysc_type_leafref *)sleaf->type, (struct lyd_node *)leaf,
3679 &leaf->value, set->tree, &node, &errmsg)) {
3680 LOGERR(set->ctx, LY_EVALID, errmsg);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003681 free(errmsg);
Michal Vasko004d3152020-06-11 19:59:22 +02003682 return LY_EVALID;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003683 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003684 } else {
3685 assert(sleaf->type->basetype == LY_TYPE_INST);
Michal Vasko004d3152020-06-11 19:59:22 +02003686 if (ly_path_eval(leaf->value.target, set->tree, &node)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003687 val = lyd_value2str(leaf, &dynamic);
3688 LOGERR(set->ctx, LY_EVALID, "Invalid instance-identifier \"%s\" value - required instance not found.", val);
3689 if (dynamic) {
3690 free((char *)val);
3691 }
3692 return LY_EVALID;
3693 }
3694 }
Michal Vasko004d3152020-06-11 19:59:22 +02003695
3696 /* insert it */
3697 set_insert_node(set, node, 0, LYXP_NODE_ELEM, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003698 }
3699 }
3700
3701 return LY_SUCCESS;
3702}
3703
3704/**
3705 * @brief Execute the YANG 1.1 derived-from(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
3706 * on whether the first argument nodes contain a node of an identity derived from the second
3707 * argument identity.
3708 *
3709 * @param[in] args Array of arguments.
3710 * @param[in] arg_count Count of elements in @p args.
3711 * @param[in,out] set Context and result set at the same time.
3712 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003713 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003714 */
3715static LY_ERR
3716xpath_derived_from(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3717{
3718 uint16_t i;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003719 LY_ARRAY_SIZE_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003720 struct lyd_node_term *leaf;
3721 struct lysc_node_leaf *sleaf;
3722 struct lyd_value data = {0};
3723 struct ly_err_item *err = NULL;
3724 LY_ERR rc = LY_SUCCESS;
3725 int found;
3726
3727 if (options & LYXP_SCNODE_ALL) {
3728 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3729 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003730 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3731 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 +02003732 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_IDENT)) {
3733 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"identityref\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003734 }
3735
3736 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3737 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3738 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 +02003739 } else if (!warn_is_string_type(sleaf->type)) {
3740 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003741 }
3742 }
3743 set_scnode_clear_ctx(set);
3744 return rc;
3745 }
3746
Michal Vaskod3678892020-05-21 10:06:58 +02003747 if (args[0]->type != LYXP_SET_NODE_SET) {
3748 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
3749 "derived-from(node-set, string)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003750 return LY_EVALID;
3751 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003752 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003753 LY_CHECK_RET(rc);
3754
3755 set_fill_boolean(set, 0);
Michal Vaskod3678892020-05-21 10:06:58 +02003756 found = 0;
3757 for (i = 0; i < args[0]->used; ++i) {
3758 leaf = (struct lyd_node_term *)args[0]->val.nodes[i].node;
3759 sleaf = (struct lysc_node_leaf *)leaf->schema;
3760 if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_IDENT)) {
3761 /* store args[1] into ident */
3762 rc = sleaf->type->plugin->store(set->ctx, sleaf->type, args[1]->val.str, strlen(args[1]->val.str),
3763 LY_TYPE_OPTS_STORE, lys_resolve_prefix, (void *)sleaf->dflt_mod, set->format,
3764 (struct lyd_node *)leaf, set->tree, &data, NULL, &err);
3765 if (err) {
3766 ly_err_print(err);
3767 ly_err_free(err);
3768 }
3769 LY_CHECK_RET(rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003770
Michal Vaskod3678892020-05-21 10:06:58 +02003771 LY_ARRAY_FOR(data.ident->derived, u) {
3772 if (data.ident->derived[u] == leaf->value.ident) {
3773 set_fill_boolean(set, 1);
3774 found = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003775 break;
3776 }
3777 }
Michal Vaskod3678892020-05-21 10:06:58 +02003778 if (found) {
3779 break;
3780 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003781 }
3782 }
3783
3784 return LY_SUCCESS;
3785}
3786
3787/**
3788 * @brief Execute the YANG 1.1 derived-from-or-self(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
3789 * on whether the first argument nodes contain a node of an identity that either is or is derived from
3790 * the second argument identity.
3791 *
3792 * @param[in] args Array of arguments.
3793 * @param[in] arg_count Count of elements in @p args.
3794 * @param[in,out] set Context and result set at the same time.
3795 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003796 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003797 */
3798static LY_ERR
3799xpath_derived_from_or_self(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3800{
3801 uint16_t i;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003802 LY_ARRAY_SIZE_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003803 struct lyd_node_term *leaf;
3804 struct lysc_node_leaf *sleaf;
3805 struct lyd_value data = {0};
3806 struct ly_err_item *err = NULL;
3807 LY_ERR rc = LY_SUCCESS;
3808 int found;
3809
3810 if (options & LYXP_SCNODE_ALL) {
3811 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3812 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003813 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3814 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 +02003815 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_IDENT)) {
3816 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"identityref\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003817 }
3818
3819 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3820 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3821 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 +02003822 } else if (!warn_is_string_type(sleaf->type)) {
3823 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003824 }
3825 }
3826 set_scnode_clear_ctx(set);
3827 return rc;
3828 }
3829
Michal Vaskod3678892020-05-21 10:06:58 +02003830 if (args[0]->type != LYXP_SET_NODE_SET) {
3831 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
3832 "derived-from-or-self(node-set, string)");
Michal Vasko03ff5a72019-09-11 13:49:33 +02003833 return LY_EVALID;
3834 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003835 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003836 LY_CHECK_RET(rc);
3837
3838 set_fill_boolean(set, 0);
Michal Vaskod3678892020-05-21 10:06:58 +02003839 found = 0;
3840 for (i = 0; i < args[0]->used; ++i) {
3841 leaf = (struct lyd_node_term *)args[0]->val.nodes[i].node;
3842 sleaf = (struct lysc_node_leaf *)leaf->schema;
3843 if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_IDENT)) {
3844 /* store args[1] into ident */
3845 rc = sleaf->type->plugin->store(set->ctx, sleaf->type, args[1]->val.str, strlen(args[1]->val.str),
3846 LY_TYPE_OPTS_STORE, lys_resolve_prefix, (void *)sleaf->dflt_mod, set->format,
3847 (struct lyd_node *)leaf, set->tree, &data, NULL, &err);
3848 if (err) {
3849 ly_err_print(err);
3850 ly_err_free(err);
3851 }
3852 LY_CHECK_RET(rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003853
Michal Vaskod3678892020-05-21 10:06:58 +02003854 if (data.ident == leaf->value.ident) {
3855 set_fill_boolean(set, 1);
3856 break;
3857 }
3858 LY_ARRAY_FOR(data.ident->derived, u) {
3859 if (data.ident->derived[u] == leaf->value.ident) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003860 set_fill_boolean(set, 1);
Michal Vaskod3678892020-05-21 10:06:58 +02003861 found = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003862 break;
3863 }
Michal Vaskod3678892020-05-21 10:06:58 +02003864 }
3865 if (found) {
3866 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003867 }
3868 }
3869 }
3870
3871 return LY_SUCCESS;
3872}
3873
3874/**
3875 * @brief Execute the YANG 1.1 enum-value(node-set) function. Returns LYXP_SET_NUMBER
3876 * with the integer value of the first node's enum value, otherwise NaN.
3877 *
3878 * @param[in] args Array of arguments.
3879 * @param[in] arg_count Count of elements in @p args.
3880 * @param[in,out] set Context and result set at the same time.
3881 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003882 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003883 */
3884static LY_ERR
3885xpath_enum_value(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3886{
3887 struct lyd_node_term *leaf;
3888 struct lysc_node_leaf *sleaf;
3889 LY_ERR rc = LY_SUCCESS;
3890
3891 if (options & LYXP_SCNODE_ALL) {
3892 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3893 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003894 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3895 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 +02003896 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_ENUM)) {
3897 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"enumeration\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003898 }
3899 set_scnode_clear_ctx(set);
3900 return rc;
3901 }
3902
Michal Vaskod3678892020-05-21 10:06:58 +02003903 if (args[0]->type != LYXP_SET_NODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003904 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "enum-value(node-set)");
3905 return LY_EVALID;
3906 }
3907
3908 set_fill_number(set, NAN);
Michal Vaskod3678892020-05-21 10:06:58 +02003909 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003910 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
3911 sleaf = (struct lysc_node_leaf *)leaf->schema;
3912 if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_ENUM)) {
3913 set_fill_number(set, leaf->value.enum_item->value);
3914 }
3915 }
3916
3917 return LY_SUCCESS;
3918}
3919
3920/**
3921 * @brief Execute the XPath false() function. Returns LYXP_SET_BOOLEAN
3922 * with false value.
3923 *
3924 * @param[in] args Array of arguments.
3925 * @param[in] arg_count Count of elements in @p args.
3926 * @param[in,out] set Context and result set at the same time.
3927 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003928 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003929 */
3930static LY_ERR
3931xpath_false(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3932{
3933 if (options & LYXP_SCNODE_ALL) {
3934 set_scnode_clear_ctx(set);
3935 return LY_SUCCESS;
3936 }
3937
3938 set_fill_boolean(set, 0);
3939 return LY_SUCCESS;
3940}
3941
3942/**
3943 * @brief Execute the XPath floor(number) function. Returns LYXP_SET_NUMBER
3944 * with the first argument floored (truncated).
3945 *
3946 * @param[in] args Array of arguments.
3947 * @param[in] arg_count Count of elements in @p args.
3948 * @param[in,out] set Context and result set at the same time.
3949 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003950 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003951 */
3952static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003953xpath_floor(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int UNUSED(options))
Michal Vasko03ff5a72019-09-11 13:49:33 +02003954{
3955 LY_ERR rc;
3956
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003957 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003958 LY_CHECK_RET(rc);
3959 if (isfinite(args[0]->val.num)) {
3960 set_fill_number(set, (long long)args[0]->val.num);
3961 }
3962
3963 return LY_SUCCESS;
3964}
3965
3966/**
3967 * @brief Execute the XPath lang(string) function. Returns LYXP_SET_BOOLEAN
3968 * whether the language of the text matches the one from the argument.
3969 *
3970 * @param[in] args Array of arguments.
3971 * @param[in] arg_count Count of elements in @p args.
3972 * @param[in,out] set Context and result set at the same time.
3973 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003974 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003975 */
3976static LY_ERR
3977xpath_lang(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
3978{
3979 const struct lyd_node *node;
3980 struct lysc_node_leaf *sleaf;
Michal Vasko9f96a052020-03-10 09:41:45 +01003981 struct lyd_meta *meta = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003982 const char *val;
3983 int i, dynamic;
3984 LY_ERR rc = LY_SUCCESS;
3985
3986 if (options & LYXP_SCNODE_ALL) {
3987 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3988 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3989 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 +02003990 } else if (!warn_is_string_type(sleaf->type)) {
3991 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003992 }
3993 }
3994 set_scnode_clear_ctx(set);
3995 return rc;
3996 }
3997
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003998 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003999 LY_CHECK_RET(rc);
4000
Michal Vasko03ff5a72019-09-11 13:49:33 +02004001 if (set->type != LYXP_SET_NODE_SET) {
4002 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "lang(string)");
4003 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004004 } else if (!set->used) {
4005 set_fill_boolean(set, 0);
4006 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004007 }
4008
4009 switch (set->val.nodes[0].type) {
4010 case LYXP_NODE_ELEM:
4011 case LYXP_NODE_TEXT:
4012 node = set->val.nodes[0].node;
4013 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004014 case LYXP_NODE_META:
4015 node = set->val.meta[0].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004016 break;
4017 default:
4018 /* nothing to do with roots */
4019 set_fill_boolean(set, 0);
4020 return LY_SUCCESS;
4021 }
4022
Michal Vasko9f96a052020-03-10 09:41:45 +01004023 /* find lang metadata */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004024 for (; node; node = (struct lyd_node *)node->parent) {
Michal Vasko9f96a052020-03-10 09:41:45 +01004025 for (meta = node->meta; meta; meta = meta->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004026 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004027 if (meta->name && !strcmp(meta->name, "lang") && !strcmp(meta->annotation->module->name, "xml")) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004028 break;
4029 }
4030 }
4031
Michal Vasko9f96a052020-03-10 09:41:45 +01004032 if (meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004033 break;
4034 }
4035 }
4036
4037 /* compare languages */
Michal Vasko9f96a052020-03-10 09:41:45 +01004038 if (!meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004039 set_fill_boolean(set, 0);
4040 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01004041 val = lyd_meta2str(meta, &dynamic);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004042 for (i = 0; args[0]->val.str[i]; ++i) {
4043 if (tolower(args[0]->val.str[i]) != tolower(val[i])) {
4044 set_fill_boolean(set, 0);
4045 break;
4046 }
4047 }
4048 if (!args[0]->val.str[i]) {
4049 if (!val[i] || (val[i] == '-')) {
4050 set_fill_boolean(set, 1);
4051 } else {
4052 set_fill_boolean(set, 0);
4053 }
4054 }
4055 if (dynamic) {
4056 free((char *)val);
4057 }
4058 }
4059
4060 return LY_SUCCESS;
4061}
4062
4063/**
4064 * @brief Execute the XPath last() function. Returns LYXP_SET_NUMBER
4065 * with the context size.
4066 *
4067 * @param[in] args Array of arguments.
4068 * @param[in] arg_count Count of elements in @p args.
4069 * @param[in,out] set Context and result set at the same time.
4070 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004071 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004072 */
4073static LY_ERR
4074xpath_last(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4075{
4076 if (options & LYXP_SCNODE_ALL) {
4077 set_scnode_clear_ctx(set);
4078 return LY_SUCCESS;
4079 }
4080
Michal Vasko03ff5a72019-09-11 13:49:33 +02004081 if (set->type != LYXP_SET_NODE_SET) {
4082 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "last()");
4083 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004084 } else if (!set->used) {
4085 set_fill_number(set, 0);
4086 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004087 }
4088
4089 set_fill_number(set, set->ctx_size);
4090 return LY_SUCCESS;
4091}
4092
4093/**
4094 * @brief Execute the XPath local-name(node-set?) function. Returns LYXP_SET_STRING
4095 * with the node name without namespace from the argument or the context.
4096 *
4097 * @param[in] args Array of arguments.
4098 * @param[in] arg_count Count of elements in @p args.
4099 * @param[in,out] set Context and result set at the same time.
4100 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004101 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004102 */
4103static LY_ERR
4104xpath_local_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
4105{
4106 struct lyxp_set_node *item;
4107 /* suppress unused variable warning */
4108 (void)options;
4109
4110 if (options & LYXP_SCNODE_ALL) {
4111 set_scnode_clear_ctx(set);
4112 return LY_SUCCESS;
4113 }
4114
4115 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004116 if (args[0]->type != LYXP_SET_NODE_SET) {
4117 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "local-name(node-set?)");
4118 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004119 } else if (!args[0]->used) {
4120 set_fill_string(set, "", 0);
4121 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004122 }
4123
4124 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004125 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004126
4127 item = &args[0]->val.nodes[0];
4128 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004129 if (set->type != LYXP_SET_NODE_SET) {
4130 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "local-name(node-set?)");
4131 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004132 } else if (!set->used) {
4133 set_fill_string(set, "", 0);
4134 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004135 }
4136
4137 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004138 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004139
4140 item = &set->val.nodes[0];
4141 }
4142
4143 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004144 case LYXP_NODE_NONE:
4145 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004146 case LYXP_NODE_ROOT:
4147 case LYXP_NODE_ROOT_CONFIG:
4148 case LYXP_NODE_TEXT:
4149 set_fill_string(set, "", 0);
4150 break;
4151 case LYXP_NODE_ELEM:
4152 set_fill_string(set, item->node->schema->name, strlen(item->node->schema->name));
4153 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004154 case LYXP_NODE_META:
4155 set_fill_string(set, ((struct lyd_meta *)item->node)->name, strlen(((struct lyd_meta *)item->node)->name));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004156 break;
4157 }
4158
4159 return LY_SUCCESS;
4160}
4161
4162/**
4163 * @brief Execute the XPath name(node-set?) function. Returns LYXP_SET_STRING
4164 * with the node name fully qualified (with namespace) from the argument or the context.
4165 *
4166 * @param[in] args Array of arguments.
4167 * @param[in] arg_count Count of elements in @p args.
4168 * @param[in,out] set Context and result set at the same time.
4169 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004170 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004171 */
4172static LY_ERR
4173xpath_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
4174{
4175 struct lyxp_set_node *item;
4176 struct lys_module *mod;
4177 char *str;
4178 const char *name;
4179 int rc;
4180
4181 if (options & LYXP_SCNODE_ALL) {
4182 set_scnode_clear_ctx(set);
4183 return LY_SUCCESS;
4184 }
4185
4186 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004187 if (args[0]->type != LYXP_SET_NODE_SET) {
4188 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "name(node-set?)");
4189 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004190 } else if (!args[0]->used) {
4191 set_fill_string(set, "", 0);
4192 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004193 }
4194
4195 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004196 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004197
4198 item = &args[0]->val.nodes[0];
4199 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004200 if (set->type != LYXP_SET_NODE_SET) {
4201 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "name(node-set?)");
4202 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004203 } else if (!set->used) {
4204 set_fill_string(set, "", 0);
4205 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004206 }
4207
4208 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004209 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004210
4211 item = &set->val.nodes[0];
4212 }
4213
4214 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004215 case LYXP_NODE_NONE:
4216 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004217 case LYXP_NODE_ROOT:
4218 case LYXP_NODE_ROOT_CONFIG:
4219 case LYXP_NODE_TEXT:
4220 mod = NULL;
4221 name = NULL;
4222 break;
4223 case LYXP_NODE_ELEM:
4224 mod = item->node->schema->module;
4225 name = item->node->schema->name;
4226 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004227 case LYXP_NODE_META:
4228 mod = ((struct lyd_meta *)item->node)->annotation->module;
4229 name = ((struct lyd_meta *)item->node)->name;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004230 break;
4231 }
4232
4233 if (mod && name) {
4234 switch (set->format) {
Michal Vasko52927e22020-03-16 17:26:14 +01004235 case LYD_SCHEMA:
Michal Vasko03ff5a72019-09-11 13:49:33 +02004236 rc = asprintf(&str, "%s:%s", lys_prefix_find_module(set->local_mod, mod), name);
4237 break;
4238 case LYD_JSON:
4239 rc = asprintf(&str, "%s:%s", mod->name, name);
4240 break;
Michal Vasko52927e22020-03-16 17:26:14 +01004241 case LYD_XML:
Michal Vasko60ea6352020-06-29 13:39:39 +02004242 case LYD_LYB:
Michal Vasko9409ef62019-09-12 11:47:17 +02004243 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004244 }
4245 LY_CHECK_ERR_RET(rc == -1, LOGMEM(set->ctx), LY_EMEM);
4246 set_fill_string(set, str, strlen(str));
4247 free(str);
4248 } else {
4249 set_fill_string(set, "", 0);
4250 }
4251
4252 return LY_SUCCESS;
4253}
4254
4255/**
4256 * @brief Execute the XPath namespace-uri(node-set?) function. Returns LYXP_SET_STRING
4257 * with the namespace of the node from the argument or the context.
4258 *
4259 * @param[in] args Array of arguments.
4260 * @param[in] arg_count Count of elements in @p args.
4261 * @param[in,out] set Context and result set at the same time.
4262 * @param[in] options XPath options.
4263 * @return LY_ERR (LY_EINVAL for wrong arguments on schema)
4264 */
4265static LY_ERR
4266xpath_namespace_uri(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
4267{
4268 struct lyxp_set_node *item;
4269 struct lys_module *mod;
4270 /* suppress unused variable warning */
4271 (void)options;
4272
4273 if (options & LYXP_SCNODE_ALL) {
4274 set_scnode_clear_ctx(set);
4275 return LY_SUCCESS;
4276 }
4277
4278 if (arg_count) {
Michal Vaskod3678892020-05-21 10:06:58 +02004279 if (args[0]->type != LYXP_SET_NODE_SET) {
4280 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
4281 "namespace-uri(node-set?)");
4282 return LY_EVALID;
4283 } else if (!args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004284 set_fill_string(set, "", 0);
4285 return LY_SUCCESS;
4286 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004287
4288 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004289 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004290
4291 item = &args[0]->val.nodes[0];
4292 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004293 if (set->type != LYXP_SET_NODE_SET) {
4294 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "namespace-uri(node-set?)");
4295 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004296 } else if (!set->used) {
4297 set_fill_string(set, "", 0);
4298 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004299 }
4300
4301 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004302 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004303
4304 item = &set->val.nodes[0];
4305 }
4306
4307 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004308 case LYXP_NODE_NONE:
4309 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004310 case LYXP_NODE_ROOT:
4311 case LYXP_NODE_ROOT_CONFIG:
4312 case LYXP_NODE_TEXT:
4313 set_fill_string(set, "", 0);
4314 break;
4315 case LYXP_NODE_ELEM:
Michal Vasko9f96a052020-03-10 09:41:45 +01004316 case LYXP_NODE_META:
Michal Vasko03ff5a72019-09-11 13:49:33 +02004317 if (item->type == LYXP_NODE_ELEM) {
4318 mod = item->node->schema->module;
Michal Vasko9f96a052020-03-10 09:41:45 +01004319 } else { /* LYXP_NODE_META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004320 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004321 mod = ((struct lyd_meta *)item->node)->annotation->module;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004322 }
4323
4324 set_fill_string(set, mod->ns, strlen(mod->ns));
4325 break;
4326 }
4327
4328 return LY_SUCCESS;
4329}
4330
4331/**
4332 * @brief Execute the XPath node() function (node type). Returns LYXP_SET_NODE_SET
4333 * with only nodes from the context. In practice it either leaves the context
4334 * as it is or returns an empty node set.
4335 *
4336 * @param[in] args Array of arguments.
4337 * @param[in] arg_count Count of elements in @p args.
4338 * @param[in,out] set Context and result set at the same time.
4339 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004340 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004341 */
4342static LY_ERR
4343xpath_node(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4344{
4345 if (options & LYXP_SCNODE_ALL) {
4346 set_scnode_clear_ctx(set);
4347 return LY_SUCCESS;
4348 }
4349
4350 if (set->type != LYXP_SET_NODE_SET) {
Michal Vaskod3678892020-05-21 10:06:58 +02004351 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004352 }
4353 return LY_SUCCESS;
4354}
4355
4356/**
4357 * @brief Execute the XPath normalize-space(string?) function. Returns LYXP_SET_STRING
4358 * with normalized value (no leading, trailing, double white spaces) of the node
4359 * from the argument or the context.
4360 *
4361 * @param[in] args Array of arguments.
4362 * @param[in] arg_count Count of elements in @p args.
4363 * @param[in,out] set Context and result set at the same time.
4364 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004365 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004366 */
4367static LY_ERR
4368xpath_normalize_space(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
4369{
4370 uint16_t i, new_used;
4371 char *new;
4372 int have_spaces = 0, space_before = 0;
4373 struct lysc_node_leaf *sleaf;
4374 LY_ERR rc = LY_SUCCESS;
4375
4376 if (options & LYXP_SCNODE_ALL) {
4377 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4378 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4379 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 +02004380 } else if (!warn_is_string_type(sleaf->type)) {
4381 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004382 }
4383 }
4384 set_scnode_clear_ctx(set);
4385 return rc;
4386 }
4387
4388 if (arg_count) {
4389 set_fill_set(set, args[0]);
4390 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004391 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004392 LY_CHECK_RET(rc);
4393
4394 /* is there any normalization necessary? */
4395 for (i = 0; set->val.str[i]; ++i) {
4396 if (is_xmlws(set->val.str[i])) {
4397 if ((i == 0) || space_before || (!set->val.str[i + 1])) {
4398 have_spaces = 1;
4399 break;
4400 }
4401 space_before = 1;
4402 } else {
4403 space_before = 0;
4404 }
4405 }
4406
4407 /* yep, there is */
4408 if (have_spaces) {
4409 /* it's enough, at least one character will go, makes space for ending '\0' */
4410 new = malloc(strlen(set->val.str) * sizeof(char));
4411 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4412 new_used = 0;
4413
4414 space_before = 0;
4415 for (i = 0; set->val.str[i]; ++i) {
4416 if (is_xmlws(set->val.str[i])) {
4417 if ((i == 0) || space_before) {
4418 space_before = 1;
4419 continue;
4420 } else {
4421 space_before = 1;
4422 }
4423 } else {
4424 space_before = 0;
4425 }
4426
4427 new[new_used] = (space_before ? ' ' : set->val.str[i]);
4428 ++new_used;
4429 }
4430
4431 /* at worst there is one trailing space now */
4432 if (new_used && is_xmlws(new[new_used - 1])) {
4433 --new_used;
4434 }
4435
4436 new = ly_realloc(new, (new_used + 1) * sizeof(char));
4437 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4438 new[new_used] = '\0';
4439
4440 free(set->val.str);
4441 set->val.str = new;
4442 }
4443
4444 return LY_SUCCESS;
4445}
4446
4447/**
4448 * @brief Execute the XPath not(boolean) function. Returns LYXP_SET_BOOLEAN
4449 * with the argument converted to boolean and logically inverted.
4450 *
4451 * @param[in] args Array of arguments.
4452 * @param[in] arg_count Count of elements in @p args.
4453 * @param[in,out] set Context and result set at the same time.
4454 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004455 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004456 */
4457static LY_ERR
4458xpath_not(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4459{
4460 if (options & LYXP_SCNODE_ALL) {
4461 set_scnode_clear_ctx(set);
4462 return LY_SUCCESS;
4463 }
4464
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004465 lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02004466 if (args[0]->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004467 set_fill_boolean(set, 0);
4468 } else {
4469 set_fill_boolean(set, 1);
4470 }
4471
4472 return LY_SUCCESS;
4473}
4474
4475/**
4476 * @brief Execute the XPath number(object?) function. Returns LYXP_SET_NUMBER
4477 * with the number representation of either the argument or the context.
4478 *
4479 * @param[in] args Array of arguments.
4480 * @param[in] arg_count Count of elements in @p args.
4481 * @param[in,out] set Context and result set at the same time.
4482 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004483 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004484 */
4485static LY_ERR
4486xpath_number(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
4487{
4488 LY_ERR rc;
4489
4490 if (options & LYXP_SCNODE_ALL) {
4491 set_scnode_clear_ctx(set);
4492 return LY_SUCCESS;
4493 }
4494
4495 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004496 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004497 LY_CHECK_RET(rc);
4498 set_fill_set(set, args[0]);
4499 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004500 rc = lyxp_set_cast(set, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004501 LY_CHECK_RET(rc);
4502 }
4503
4504 return LY_SUCCESS;
4505}
4506
4507/**
4508 * @brief Execute the XPath position() function. Returns LYXP_SET_NUMBER
4509 * with the context position.
4510 *
4511 * @param[in] args Array of arguments.
4512 * @param[in] arg_count Count of elements in @p args.
4513 * @param[in,out] set Context and result set at the same time.
4514 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004515 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004516 */
4517static LY_ERR
4518xpath_position(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4519{
4520 if (options & LYXP_SCNODE_ALL) {
4521 set_scnode_clear_ctx(set);
4522 return LY_SUCCESS;
4523 }
4524
Michal Vasko03ff5a72019-09-11 13:49:33 +02004525 if (set->type != LYXP_SET_NODE_SET) {
4526 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "position()");
4527 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004528 } else if (!set->used) {
4529 set_fill_number(set, 0);
4530 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004531 }
4532
4533 set_fill_number(set, set->ctx_pos);
4534
4535 /* UNUSED in 'Release' build type */
4536 (void)options;
4537 return LY_SUCCESS;
4538}
4539
4540/**
4541 * @brief Execute the YANG 1.1 re-match(string, string) function. Returns LYXP_SET_BOOLEAN
4542 * depending on whether the second argument regex matches the first argument string. For details refer to
4543 * YANG 1.1 RFC section 10.2.1.
4544 *
4545 * @param[in] args Array of arguments.
4546 * @param[in] arg_count Count of elements in @p args.
4547 * @param[in,out] set Context and result set at the same time.
4548 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004549 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004550 */
4551static LY_ERR
4552xpath_re_match(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4553{
4554 struct lysc_pattern **patterns = NULL, **pattern;
4555 struct lysc_node_leaf *sleaf;
4556 char *path;
4557 LY_ERR rc = LY_SUCCESS;
4558 struct ly_err_item *err;
4559
4560 if (options & LYXP_SCNODE_ALL) {
4561 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4562 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4563 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 +02004564 } else if (!warn_is_string_type(sleaf->type)) {
4565 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004566 }
4567 }
4568
4569 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4570 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4571 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 +02004572 } else if (!warn_is_string_type(sleaf->type)) {
4573 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004574 }
4575 }
4576 set_scnode_clear_ctx(set);
4577 return rc;
4578 }
4579
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004580 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004581 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004582 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004583 LY_CHECK_RET(rc);
4584
4585 LY_ARRAY_NEW_RET(set->ctx, patterns, pattern, LY_EMEM);
4586 *pattern = malloc(sizeof **pattern);
4587 path = lyd_path(set->ctx_node, LYD_PATH_LOG, NULL, 0);
4588 rc = lys_compile_type_pattern_check(set->ctx, path, args[1]->val.str, &(*pattern)->code);
4589 free(path);
4590 if (rc != LY_SUCCESS) {
4591 LY_ARRAY_FREE(patterns);
4592 return rc;
4593 }
4594
4595 rc = ly_type_validate_patterns(patterns, args[0]->val.str, strlen(args[0]->val.str), &err);
4596 pcre2_code_free((*pattern)->code);
4597 free(*pattern);
4598 LY_ARRAY_FREE(patterns);
4599 if (rc && (rc != LY_EVALID)) {
4600 ly_err_print(err);
4601 ly_err_free(err);
4602 return rc;
4603 }
4604
4605 if (rc == LY_EVALID) {
4606 ly_err_free(err);
4607 set_fill_boolean(set, 0);
4608 } else {
4609 set_fill_boolean(set, 1);
4610 }
4611
4612 return LY_SUCCESS;
4613}
4614
4615/**
4616 * @brief Execute the XPath round(number) function. Returns LYXP_SET_NUMBER
4617 * with the rounded first argument. For details refer to
4618 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-round.
4619 *
4620 * @param[in] args Array of arguments.
4621 * @param[in] arg_count Count of elements in @p args.
4622 * @param[in,out] set Context and result set at the same time.
4623 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004624 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004625 */
4626static LY_ERR
4627xpath_round(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4628{
4629 struct lysc_node_leaf *sleaf;
4630 LY_ERR rc = LY_SUCCESS;
4631
4632 if (options & LYXP_SCNODE_ALL) {
4633 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4634 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004635 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4636 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 +02004637 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
4638 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004639 }
4640 set_scnode_clear_ctx(set);
4641 return rc;
4642 }
4643
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004644 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004645 LY_CHECK_RET(rc);
4646
4647 /* cover only the cases where floor can't be used */
4648 if ((args[0]->val.num == -0.0f) || ((args[0]->val.num < 0) && (args[0]->val.num >= -0.5))) {
4649 set_fill_number(set, -0.0f);
4650 } else {
4651 args[0]->val.num += 0.5;
4652 rc = xpath_floor(args, 1, args[0], options);
4653 LY_CHECK_RET(rc);
4654 set_fill_number(set, args[0]->val.num);
4655 }
4656
4657 return LY_SUCCESS;
4658}
4659
4660/**
4661 * @brief Execute the XPath starts-with(string, string) function.
4662 * Returns LYXP_SET_BOOLEAN whether the second argument is
4663 * the prefix of the first or not.
4664 *
4665 * @param[in] args Array of arguments.
4666 * @param[in] arg_count Count of elements in @p args.
4667 * @param[in,out] set Context and result set at the same time.
4668 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004669 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004670 */
4671static LY_ERR
4672xpath_starts_with(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4673{
4674 struct lysc_node_leaf *sleaf;
4675 LY_ERR rc = LY_SUCCESS;
4676
4677 if (options & LYXP_SCNODE_ALL) {
4678 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4679 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4680 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 +02004681 } else if (!warn_is_string_type(sleaf->type)) {
4682 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004683 }
4684 }
4685
4686 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4687 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4688 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 +02004689 } else if (!warn_is_string_type(sleaf->type)) {
4690 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004691 }
4692 }
4693 set_scnode_clear_ctx(set);
4694 return rc;
4695 }
4696
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004697 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004698 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004699 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004700 LY_CHECK_RET(rc);
4701
4702 if (strncmp(args[0]->val.str, args[1]->val.str, strlen(args[1]->val.str))) {
4703 set_fill_boolean(set, 0);
4704 } else {
4705 set_fill_boolean(set, 1);
4706 }
4707
4708 return LY_SUCCESS;
4709}
4710
4711/**
4712 * @brief Execute the XPath string(object?) function. Returns LYXP_SET_STRING
4713 * with the string representation of either the argument or the context.
4714 *
4715 * @param[in] args Array of arguments.
4716 * @param[in] arg_count Count of elements in @p args.
4717 * @param[in,out] set Context and result set at the same time.
4718 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004719 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004720 */
4721static LY_ERR
4722xpath_string(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
4723{
4724 LY_ERR rc;
4725
4726 if (options & LYXP_SCNODE_ALL) {
4727 set_scnode_clear_ctx(set);
4728 return LY_SUCCESS;
4729 }
4730
4731 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004732 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004733 LY_CHECK_RET(rc);
4734 set_fill_set(set, args[0]);
4735 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004736 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004737 LY_CHECK_RET(rc);
4738 }
4739
4740 return LY_SUCCESS;
4741}
4742
4743/**
4744 * @brief Execute the XPath string-length(string?) function. Returns LYXP_SET_NUMBER
4745 * with the length of the string in either the argument or the context.
4746 *
4747 * @param[in] args Array of arguments.
4748 * @param[in] arg_count Count of elements in @p args.
4749 * @param[in,out] set Context and result set at the same time.
4750 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004751 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004752 */
4753static LY_ERR
4754xpath_string_length(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
4755{
4756 struct lysc_node_leaf *sleaf;
4757 LY_ERR rc = LY_SUCCESS;
4758
4759 if (options & LYXP_SCNODE_ALL) {
4760 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4761 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4762 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 +02004763 } else if (!warn_is_string_type(sleaf->type)) {
4764 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004765 }
4766 }
4767 if (!arg_count && (set->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set))) {
4768 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4769 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 +02004770 } else if (!warn_is_string_type(sleaf->type)) {
4771 LOGWRN(set->ctx, "Argument #0 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004772 }
4773 }
4774 set_scnode_clear_ctx(set);
4775 return rc;
4776 }
4777
4778 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004779 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004780 LY_CHECK_RET(rc);
4781 set_fill_number(set, strlen(args[0]->val.str));
4782 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004783 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004784 LY_CHECK_RET(rc);
4785 set_fill_number(set, strlen(set->val.str));
4786 }
4787
4788 return LY_SUCCESS;
4789}
4790
4791/**
4792 * @brief Execute the XPath substring(string, number, number?) function.
4793 * Returns LYXP_SET_STRING substring of the first argument starting
4794 * on the second argument index ending on the third argument index,
4795 * indexed from 1. For exact definition refer to
4796 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-substring.
4797 *
4798 * @param[in] args Array of arguments.
4799 * @param[in] arg_count Count of elements in @p args.
4800 * @param[in,out] set Context and result set at the same time.
4801 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004802 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004803 */
4804static LY_ERR
4805xpath_substring(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
4806{
4807 int start, len;
4808 uint16_t str_start, str_len, pos;
4809 struct lysc_node_leaf *sleaf;
4810 LY_ERR rc = LY_SUCCESS;
4811
4812 if (options & LYXP_SCNODE_ALL) {
4813 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4814 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4815 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 +02004816 } else if (!warn_is_string_type(sleaf->type)) {
4817 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004818 }
4819 }
4820
4821 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4822 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4823 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 +02004824 } else if (!warn_is_numeric_type(sleaf->type)) {
4825 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004826 }
4827 }
4828
4829 if ((arg_count == 3) && (args[2]->type == LYXP_SET_SCNODE_SET)
4830 && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
4831 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4832 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 +02004833 } else if (!warn_is_numeric_type(sleaf->type)) {
4834 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004835 }
4836 }
4837 set_scnode_clear_ctx(set);
4838 return rc;
4839 }
4840
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004841 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004842 LY_CHECK_RET(rc);
4843
4844 /* start */
4845 if (xpath_round(&args[1], 1, args[1], options)) {
4846 return -1;
4847 }
4848 if (isfinite(args[1]->val.num)) {
4849 start = args[1]->val.num - 1;
4850 } else if (isinf(args[1]->val.num) && signbit(args[1]->val.num)) {
4851 start = INT_MIN;
4852 } else {
4853 start = INT_MAX;
4854 }
4855
4856 /* len */
4857 if (arg_count == 3) {
4858 rc = xpath_round(&args[2], 1, args[2], options);
4859 LY_CHECK_RET(rc);
4860 if (isfinite(args[2]->val.num)) {
4861 len = args[2]->val.num;
4862 } else if (isnan(args[2]->val.num) || signbit(args[2]->val.num)) {
4863 len = 0;
4864 } else {
4865 len = INT_MAX;
4866 }
4867 } else {
4868 len = INT_MAX;
4869 }
4870
4871 /* find matching character positions */
4872 str_start = 0;
4873 str_len = 0;
4874 for (pos = 0; args[0]->val.str[pos]; ++pos) {
4875 if (pos < start) {
4876 ++str_start;
4877 } else if (pos < start + len) {
4878 ++str_len;
4879 } else {
4880 break;
4881 }
4882 }
4883
4884 set_fill_string(set, args[0]->val.str + str_start, str_len);
4885 return LY_SUCCESS;
4886}
4887
4888/**
4889 * @brief Execute the XPath substring-after(string, string) function.
4890 * Returns LYXP_SET_STRING with the string succeeding the occurance
4891 * of the second argument in the first or an empty string.
4892 *
4893 * @param[in] args Array of arguments.
4894 * @param[in] arg_count Count of elements in @p args.
4895 * @param[in,out] set Context and result set at the same time.
4896 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004897 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004898 */
4899static LY_ERR
4900xpath_substring_after(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4901{
4902 char *ptr;
4903 struct lysc_node_leaf *sleaf;
4904 LY_ERR rc = LY_SUCCESS;
4905
4906 if (options & LYXP_SCNODE_ALL) {
4907 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4908 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4909 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 +02004910 } else if (!warn_is_string_type(sleaf->type)) {
4911 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004912 }
4913 }
4914
4915 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4916 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4917 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 +02004918 } else if (!warn_is_string_type(sleaf->type)) {
4919 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004920 }
4921 }
4922 set_scnode_clear_ctx(set);
4923 return rc;
4924 }
4925
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004926 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004927 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004928 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004929 LY_CHECK_RET(rc);
4930
4931 ptr = strstr(args[0]->val.str, args[1]->val.str);
4932 if (ptr) {
4933 set_fill_string(set, ptr + strlen(args[1]->val.str), strlen(ptr + strlen(args[1]->val.str)));
4934 } else {
4935 set_fill_string(set, "", 0);
4936 }
4937
4938 return LY_SUCCESS;
4939}
4940
4941/**
4942 * @brief Execute the XPath substring-before(string, string) function.
4943 * Returns LYXP_SET_STRING with the string preceding the occurance
4944 * of the second argument in the first or an empty string.
4945 *
4946 * @param[in] args Array of arguments.
4947 * @param[in] arg_count Count of elements in @p args.
4948 * @param[in,out] set Context and result set at the same time.
4949 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004950 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004951 */
4952static LY_ERR
4953xpath_substring_before(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4954{
4955 char *ptr;
4956 struct lysc_node_leaf *sleaf;
4957 LY_ERR rc = LY_SUCCESS;
4958
4959 if (options & LYXP_SCNODE_ALL) {
4960 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4961 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4962 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 +02004963 } else if (!warn_is_string_type(sleaf->type)) {
4964 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004965 }
4966 }
4967
4968 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4969 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4970 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 +02004971 } else if (!warn_is_string_type(sleaf->type)) {
4972 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004973 }
4974 }
4975 set_scnode_clear_ctx(set);
4976 return rc;
4977 }
4978
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004979 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004980 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004981 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004982 LY_CHECK_RET(rc);
4983
4984 ptr = strstr(args[0]->val.str, args[1]->val.str);
4985 if (ptr) {
4986 set_fill_string(set, args[0]->val.str, ptr - args[0]->val.str);
4987 } else {
4988 set_fill_string(set, "", 0);
4989 }
4990
4991 return LY_SUCCESS;
4992}
4993
4994/**
4995 * @brief Execute the XPath sum(node-set) function. Returns LYXP_SET_NUMBER
4996 * with the sum of all the nodes in the context.
4997 *
4998 * @param[in] args Array of arguments.
4999 * @param[in] arg_count Count of elements in @p args.
5000 * @param[in,out] set Context and result set at the same time.
5001 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005002 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005003 */
5004static LY_ERR
5005xpath_sum(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
5006{
5007 long double num;
5008 char *str;
5009 uint16_t i;
5010 struct lyxp_set set_item;
5011 struct lysc_node_leaf *sleaf;
5012 LY_ERR rc = LY_SUCCESS;
5013
5014 if (options & LYXP_SCNODE_ALL) {
5015 if (args[0]->type == LYXP_SET_SCNODE_SET) {
5016 for (i = 0; i < args[0]->used; ++i) {
5017 if (args[0]->val.scnodes[i].in_ctx == 1) {
5018 sleaf = (struct lysc_node_leaf *)args[0]->val.scnodes[i].scnode;
5019 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5020 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__,
5021 lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005022 } else if (!warn_is_numeric_type(sleaf->type)) {
5023 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005024 }
5025 }
5026 }
5027 }
5028 set_scnode_clear_ctx(set);
5029 return rc;
5030 }
5031
5032 set_fill_number(set, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005033
5034 if (args[0]->type != LYXP_SET_NODE_SET) {
5035 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "sum(node-set)");
5036 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02005037 } else if (!args[0]->used) {
5038 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005039 }
5040
Michal Vasko5c4e5892019-11-14 12:31:38 +01005041 set_init(&set_item, set);
5042
Michal Vasko03ff5a72019-09-11 13:49:33 +02005043 set_item.type = LYXP_SET_NODE_SET;
5044 set_item.val.nodes = malloc(sizeof *set_item.val.nodes);
5045 LY_CHECK_ERR_RET(!set_item.val.nodes, LOGMEM(set->ctx), LY_EMEM);
5046
5047 set_item.used = 1;
5048 set_item.size = 1;
5049
5050 for (i = 0; i < args[0]->used; ++i) {
5051 set_item.val.nodes[0] = args[0]->val.nodes[i];
5052
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005053 rc = cast_node_set_to_string(&set_item, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005054 LY_CHECK_RET(rc);
5055 num = cast_string_to_number(str);
5056 free(str);
5057 set->val.num += num;
5058 }
5059
5060 free(set_item.val.nodes);
5061
5062 return LY_SUCCESS;
5063}
5064
5065/**
5066 * @brief Execute the XPath text() function (node type). Returns LYXP_SET_NODE_SET
5067 * with the text content of the nodes in the context.
5068 *
5069 * @param[in] args Array of arguments.
5070 * @param[in] arg_count Count of elements in @p args.
5071 * @param[in,out] set Context and result set at the same time.
5072 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005073 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005074 */
5075static LY_ERR
5076xpath_text(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
5077{
5078 uint32_t i;
5079
5080 if (options & LYXP_SCNODE_ALL) {
5081 set_scnode_clear_ctx(set);
5082 return LY_SUCCESS;
5083 }
5084
Michal Vasko03ff5a72019-09-11 13:49:33 +02005085 if (set->type != LYXP_SET_NODE_SET) {
5086 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "text()");
5087 return LY_EVALID;
5088 }
5089
5090 for (i = 0; i < set->used;) {
5091 switch (set->val.nodes[i].type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01005092 case LYXP_NODE_NONE:
5093 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005094 case LYXP_NODE_ELEM:
Michal Vasko03ff5a72019-09-11 13:49:33 +02005095 if (set->val.nodes[i].node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
5096 set->val.nodes[i].type = LYXP_NODE_TEXT;
5097 ++i;
5098 break;
5099 }
5100 /* fall through */
5101 case LYXP_NODE_ROOT:
5102 case LYXP_NODE_ROOT_CONFIG:
5103 case LYXP_NODE_TEXT:
Michal Vasko9f96a052020-03-10 09:41:45 +01005104 case LYXP_NODE_META:
Michal Vasko03ff5a72019-09-11 13:49:33 +02005105 set_remove_node(set, i);
5106 break;
5107 }
5108 }
5109
5110 return LY_SUCCESS;
5111}
5112
5113/**
5114 * @brief Execute the XPath translate(string, string, string) function.
5115 * Returns LYXP_SET_STRING with the first argument with the characters
5116 * from the second argument replaced by those on the corresponding
5117 * positions in the third argument.
5118 *
5119 * @param[in] args Array of arguments.
5120 * @param[in] arg_count Count of elements in @p args.
5121 * @param[in,out] set Context and result set at the same time.
5122 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005123 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005124 */
5125static LY_ERR
5126xpath_translate(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
5127{
5128 uint16_t i, j, new_used;
5129 char *new;
5130 int found, have_removed;
5131 struct lysc_node_leaf *sleaf;
5132 LY_ERR rc = LY_SUCCESS;
5133
5134 if (options & LYXP_SCNODE_ALL) {
5135 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5136 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5137 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 +02005138 } else if (!warn_is_string_type(sleaf->type)) {
5139 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005140 }
5141 }
5142
5143 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5144 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5145 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 +02005146 } else if (!warn_is_string_type(sleaf->type)) {
5147 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005148 }
5149 }
5150
5151 if ((args[2]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
5152 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5153 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 +02005154 } else if (!warn_is_string_type(sleaf->type)) {
5155 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005156 }
5157 }
5158 set_scnode_clear_ctx(set);
5159 return rc;
5160 }
5161
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005162 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005163 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005164 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005165 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005166 rc = lyxp_set_cast(args[2], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005167 LY_CHECK_RET(rc);
5168
5169 new = malloc((strlen(args[0]->val.str) + 1) * sizeof(char));
5170 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5171 new_used = 0;
5172
5173 have_removed = 0;
5174 for (i = 0; args[0]->val.str[i]; ++i) {
5175 found = 0;
5176
5177 for (j = 0; args[1]->val.str[j]; ++j) {
5178 if (args[0]->val.str[i] == args[1]->val.str[j]) {
5179 /* removing this char */
5180 if (j >= strlen(args[2]->val.str)) {
5181 have_removed = 1;
5182 found = 1;
5183 break;
5184 }
5185 /* replacing this char */
5186 new[new_used] = args[2]->val.str[j];
5187 ++new_used;
5188 found = 1;
5189 break;
5190 }
5191 }
5192
5193 /* copying this char */
5194 if (!found) {
5195 new[new_used] = args[0]->val.str[i];
5196 ++new_used;
5197 }
5198 }
5199
5200 if (have_removed) {
5201 new = ly_realloc(new, (new_used + 1) * sizeof(char));
5202 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5203 }
5204 new[new_used] = '\0';
5205
Michal Vaskod3678892020-05-21 10:06:58 +02005206 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005207 set->type = LYXP_SET_STRING;
5208 set->val.str = new;
5209
5210 return LY_SUCCESS;
5211}
5212
5213/**
5214 * @brief Execute the XPath true() function. Returns LYXP_SET_BOOLEAN
5215 * with true value.
5216 *
5217 * @param[in] args Array of arguments.
5218 * @param[in] arg_count Count of elements in @p args.
5219 * @param[in,out] set Context and result set at the same time.
5220 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005221 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005222 */
5223static LY_ERR
5224xpath_true(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
5225{
5226 if (options & LYXP_SCNODE_ALL) {
5227 set_scnode_clear_ctx(set);
5228 return LY_SUCCESS;
5229 }
5230
5231 set_fill_boolean(set, 1);
5232 return LY_SUCCESS;
5233}
5234
5235/*
5236 * moveto functions
5237 *
5238 * They and only they actually change the context (set).
5239 */
5240
5241/**
Michal Vasko6346ece2019-09-24 13:12:53 +02005242 * @brief Skip prefix and return corresponding model if there is a prefix. Logs directly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005243 *
Michal Vasko2104e9f2020-03-06 08:23:25 +01005244 * XPath @p set is expected to be a (sc)node set!
5245 *
Michal Vasko6346ece2019-09-24 13:12:53 +02005246 * @param[in,out] qname Qualified node name. If includes prefix, it is skipped.
5247 * @param[in,out] qname_len Length of @p qname, is updated accordingly.
5248 * @param[in] set Set with XPath context.
5249 * @param[out] moveto_mod Expected module of a matching node.
5250 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005251 */
Michal Vasko6346ece2019-09-24 13:12:53 +02005252static LY_ERR
5253moveto_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 +02005254{
Michal Vasko6346ece2019-09-24 13:12:53 +02005255 const struct lys_module *mod;
5256 const char *ptr;
5257 int pref_len;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005258 char *str;
5259
Michal Vasko2104e9f2020-03-06 08:23:25 +01005260 assert((set->type == LYXP_SET_NODE_SET) || (set->type == LYXP_SET_SCNODE_SET));
5261
Michal Vasko6346ece2019-09-24 13:12:53 +02005262 if ((ptr = ly_strnchr(*qname, ':', *qname_len))) {
5263 /* specific module */
5264 pref_len = ptr - *qname;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005265
Michal Vasko6346ece2019-09-24 13:12:53 +02005266 switch (set->format) {
Michal Vasko52927e22020-03-16 17:26:14 +01005267 case LYD_SCHEMA:
Michal Vasko6346ece2019-09-24 13:12:53 +02005268 /* schema, search all local module imports */
5269 mod = lys_module_find_prefix(set->local_mod, *qname, pref_len);
5270 break;
5271 case LYD_JSON:
5272 /* JSON data, search in context */
5273 str = strndup(*qname, pref_len);
Michal Vasko97e76fd2020-05-27 15:22:01 +02005274 mod = ly_ctx_get_module_implemented(set->ctx, str);
Michal Vasko6346ece2019-09-24 13:12:53 +02005275 free(str);
5276 break;
Michal Vasko52927e22020-03-16 17:26:14 +01005277 case LYD_XML:
Michal Vasko60ea6352020-06-29 13:39:39 +02005278 case LYD_LYB:
Michal Vasko6346ece2019-09-24 13:12:53 +02005279 LOGINT_RET(set->ctx);
5280 }
5281
Michal Vasko004d3152020-06-11 19:59:22 +02005282 /* check for errors and non-implemented modules, as they are not valid */
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005283 if (!mod || !mod->implemented) {
Michal Vasko2104e9f2020-03-06 08:23:25 +01005284 if (set->type == LYXP_SET_SCNODE_SET) {
5285 LOGVAL(set->ctx, LY_VLOG_LYSC, set->ctx_scnode, LY_VCODE_XP_INMOD, pref_len, *qname);
5286 } else {
5287 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INMOD, pref_len, *qname);
5288 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005289 return LY_EVALID;
5290 }
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005291
Michal Vasko6346ece2019-09-24 13:12:53 +02005292 *qname += pref_len + 1;
5293 *qname_len -= pref_len + 1;
5294 } else if (((*qname)[0] == '*') && (*qname_len == 1)) {
5295 /* all modules - special case */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005296 mod = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02005297 } else if (set->type == LYXP_SET_SCNODE_SET) {
5298 /* current node module */
5299 mod = set->ctx_scnode->module;
Michal Vasko6346ece2019-09-24 13:12:53 +02005300 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02005301 /* current node module */
5302 mod = set->ctx_node->schema->module;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005303 }
5304
Michal Vasko6346ece2019-09-24 13:12:53 +02005305 *moveto_mod = mod;
5306 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005307}
5308
5309/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02005310 * @brief Move context @p set to the root. Handles absolute path.
5311 * Result is LYXP_SET_NODE_SET.
5312 *
5313 * @param[in,out] set Set to use.
5314 * @param[in] options Xpath options.
5315 */
5316static void
5317moveto_root(struct lyxp_set *set, int options)
5318{
Michal Vasko03ff5a72019-09-11 13:49:33 +02005319 if (!set) {
5320 return;
5321 }
5322
5323 if (options & LYXP_SCNODE_ALL) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005324 set_scnode_clear_ctx(set);
Michal Vaskoecd62de2019-11-13 12:35:11 +01005325 lyxp_set_scnode_insert_node(set, NULL, set->root_type);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005326 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005327 set->type = LYXP_SET_NODE_SET;
5328 set->used = 0;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005329 set_insert_node(set, NULL, 0, set->root_type, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005330 }
5331}
5332
5333/**
Michal Vaskoa1424542019-11-14 16:08:52 +01005334 * @brief Check whether a node has some unresolved "when".
5335 *
5336 * @param[in] node Node to check.
5337 * @return LY_ERR value (LY_EINCOMPLETE if there are some unresolved "when")
5338 */
5339static LY_ERR
5340moveto_when_check(const struct lyd_node *node)
5341{
5342 const struct lysc_node *schema;
5343
5344 if (!node) {
5345 return LY_SUCCESS;
5346 }
5347
5348 schema = node->schema;
5349 do {
5350 if (schema->when && !(node->flags & LYD_WHEN_TRUE)) {
5351 return LY_EINCOMPLETE;
5352 }
5353 schema = schema->parent;
5354 } while (schema && (schema->nodetype & (LYS_CASE | LYS_CHOICE)));
5355
5356 return LY_SUCCESS;
5357}
5358
5359/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02005360 * @brief Check @p node as a part of NameTest processing.
5361 *
5362 * @param[in] node Node to check.
5363 * @param[in] root_type XPath root node type.
Michal Vaskod3678892020-05-21 10:06:58 +02005364 * @param[in] node_name Node name in the dictionary to move to, NULL for any node.
5365 * @param[in] moveto_mod Expected module of the node, NULL for any.
Michal Vasko6346ece2019-09-24 13:12:53 +02005366 * @return LY_ERR (LY_ENOT if node does not match, LY_EINCOMPLETE on unresolved when,
5367 * LY_EINVAL if netither node nor any children match)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005368 */
5369static LY_ERR
5370moveto_node_check(const struct lyd_node *node, enum lyxp_node_type root_type, const char *node_name,
5371 const struct lys_module *moveto_mod)
5372{
5373 /* module check */
5374 if (moveto_mod && (node->schema->module != moveto_mod)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005375 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005376 }
5377
Michal Vasko5c4e5892019-11-14 12:31:38 +01005378 /* context check */
5379 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005380 return LY_EINVAL;
5381 }
5382
5383 /* name check */
Michal Vasko61ac2f62020-05-25 12:39:51 +02005384 if (node_name && strcmp(node_name, "*") && (node->schema->name != node_name)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005385 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005386 }
5387
Michal Vaskoa1424542019-11-14 16:08:52 +01005388 /* when check */
5389 if (moveto_when_check(node)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005390 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01005391 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005392
5393 /* match */
5394 return LY_SUCCESS;
5395}
5396
5397/**
5398 * @brief Check @p node as a part of schema NameTest processing.
5399 *
5400 * @param[in] node Schema node to check.
5401 * @param[in] root_type XPath root node type.
Michal Vaskod3678892020-05-21 10:06:58 +02005402 * @param[in] node_name Node name in the dictionary to move to, NULL for any nodes.
5403 * @param[in] moveto_mod Expected module of the node, NULL for any.
Michal Vasko6346ece2019-09-24 13:12:53 +02005404 * @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 +02005405 */
5406static LY_ERR
5407moveto_scnode_check(const struct lysc_node *node, enum lyxp_node_type root_type, const char *node_name,
Michal Vaskocafad9d2019-11-07 15:20:03 +01005408 const struct lys_module *moveto_mod)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005409{
Michal Vasko03ff5a72019-09-11 13:49:33 +02005410 /* module check */
Michal Vaskod3678892020-05-21 10:06:58 +02005411 if (moveto_mod && (node->module != moveto_mod)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005412 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005413 }
5414
5415 /* context check */
5416 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (node->flags & LYS_CONFIG_R)) {
5417 return LY_EINVAL;
5418 }
5419
5420 /* name check */
Michal Vasko61ac2f62020-05-25 12:39:51 +02005421 if (node_name && strcmp(node_name, "*") && (node->name != node_name)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005422 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005423 }
5424
5425 /* match */
5426 return LY_SUCCESS;
5427}
5428
5429/**
Michal Vaskod3678892020-05-21 10:06:58 +02005430 * @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 +02005431 *
5432 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005433 * @param[in] mod Matching node module, NULL for any.
5434 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005435 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5436 */
5437static LY_ERR
Michal Vaskod3678892020-05-21 10:06:58 +02005438moveto_node(struct lyxp_set *set, const struct lys_module *mod, const char *ncname)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005439{
Michal Vaskof03ed032020-03-04 13:31:44 +01005440 uint32_t i;
Michal Vasko6346ece2019-09-24 13:12:53 +02005441 int replaced;
Michal Vaskod3678892020-05-21 10:06:58 +02005442 const struct lyd_node *siblings, *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005443 LY_ERR rc;
5444
Michal Vaskod3678892020-05-21 10:06:58 +02005445 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005446 return LY_SUCCESS;
5447 }
5448
5449 if (set->type != LYXP_SET_NODE_SET) {
5450 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
5451 return LY_EVALID;
5452 }
5453
Michal Vasko03ff5a72019-09-11 13:49:33 +02005454 for (i = 0; i < set->used; ) {
5455 replaced = 0;
5456
5457 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 +01005458 assert(!set->val.nodes[i].node);
Michal Vaskod3678892020-05-21 10:06:58 +02005459
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005460 /* search in all the trees */
Michal Vaskod3678892020-05-21 10:06:58 +02005461 siblings = set->tree;
Michal Vasko5bfd4be2020-06-23 13:26:19 +02005462 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005463 /* search in children */
Michal Vasko5bfd4be2020-06-23 13:26:19 +02005464 siblings = lyd_node_children(set->val.nodes[i].node, 0);
Michal Vaskod3678892020-05-21 10:06:58 +02005465 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005466
Michal Vaskod3678892020-05-21 10:06:58 +02005467 for (sub = siblings; sub; sub = sub->next) {
5468 rc = moveto_node_check(sub, set->root_type, ncname, mod);
5469 if (rc == LY_SUCCESS) {
5470 if (!replaced) {
5471 set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i);
5472 replaced = 1;
5473 } else {
5474 set_insert_node(set, sub, 0, LYXP_NODE_ELEM, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005475 }
Michal Vaskod3678892020-05-21 10:06:58 +02005476 ++i;
5477 } else if (rc == LY_EINCOMPLETE) {
5478 return rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005479 }
5480 }
5481
5482 if (!replaced) {
5483 /* no match */
5484 set_remove_node(set, i);
5485 }
5486 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005487
5488 return LY_SUCCESS;
5489}
5490
5491/**
Michal Vaskod3678892020-05-21 10:06:58 +02005492 * @brief Move context @p set to a node using hashes. Result is LYXP_SET_NODE_SET (or LYXP_SET_EMPTY).
5493 * Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005494 *
5495 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005496 * @param[in] scnode Matching node schema.
Michal Vasko004d3152020-06-11 19:59:22 +02005497 * @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 +02005498 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5499 */
5500static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02005501moveto_node_hash(struct lyxp_set *set, const struct lysc_node *scnode, const struct ly_path_predicate *predicates)
Michal Vaskod3678892020-05-21 10:06:58 +02005502{
Michal Vasko004d3152020-06-11 19:59:22 +02005503 LY_ERR ret = LY_SUCCESS;
Michal Vaskod3678892020-05-21 10:06:58 +02005504 uint32_t i;
5505 int replaced;
5506 const struct lyd_node *siblings;
Michal Vasko004d3152020-06-11 19:59:22 +02005507 struct lyd_node *sub, *inst = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02005508
Michal Vasko004d3152020-06-11 19:59:22 +02005509 assert(scnode && (!(scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)) || predicates));
Michal Vaskod3678892020-05-21 10:06:58 +02005510
5511 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02005512 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005513 }
5514
5515 if (set->type != LYXP_SET_NODE_SET) {
5516 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 +02005517 ret = LY_EVALID;
5518 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005519 }
5520
5521 /* context check for all the nodes since we have the schema node */
5522 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) {
5523 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02005524 goto cleanup;
5525 }
5526
5527 /* create specific data instance if needed */
5528 if (scnode->nodetype == LYS_LIST) {
5529 LY_CHECK_GOTO(ret = lyd_create_list(scnode, predicates, &inst), cleanup);
5530 } else if (scnode->nodetype == LYS_LEAFLIST) {
5531 LY_CHECK_GOTO(ret = lyd_create_term2(scnode, &predicates[0].value, &inst), cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02005532 }
5533
5534 for (i = 0; i < set->used; ) {
5535 replaced = 0;
5536 siblings = NULL;
5537
5538 if ((set->val.nodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.nodes[i].type == LYXP_NODE_ROOT)) {
5539 assert(!set->val.nodes[i].node);
5540
5541 /* search in all the trees */
5542 siblings = set->tree;
5543 } else if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
5544 /* search in children */
Michal Vasko5bfd4be2020-06-23 13:26:19 +02005545 siblings = lyd_node_children(set->val.nodes[i].node, 0);
Michal Vaskod3678892020-05-21 10:06:58 +02005546 }
5547
5548 /* find the node using hashes */
Michal Vasko004d3152020-06-11 19:59:22 +02005549 if (inst) {
5550 lyd_find_sibling_first(siblings, inst, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02005551 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02005552 lyd_find_sibling_val(siblings, scnode, NULL, 0, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02005553 }
5554
5555 /* when check */
5556 if (sub && moveto_when_check(sub)) {
Michal Vasko004d3152020-06-11 19:59:22 +02005557 ret = LY_EINCOMPLETE;
5558 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005559 }
5560
5561 if (sub) {
5562 /* pos filled later */
5563 if (!replaced) {
5564 set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i);
5565 replaced = 1;
5566 } else {
5567 set_insert_node(set, sub, 0, LYXP_NODE_ELEM, i);
5568 }
5569 ++i;
5570 }
5571
5572 if (!replaced) {
5573 /* no match */
5574 set_remove_node(set, i);
5575 }
5576 }
5577
Michal Vasko004d3152020-06-11 19:59:22 +02005578cleanup:
5579 lyd_free_tree(inst);
5580 return ret;
Michal Vaskod3678892020-05-21 10:06:58 +02005581}
5582
5583/**
5584 * @brief Move context @p set to a schema node. Result is LYXP_SET_SCNODE_SET (or LYXP_SET_EMPTY).
5585 *
5586 * @param[in,out] set Set to use.
5587 * @param[in] mod Matching node module, NULL for any.
5588 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005589 * @param[in] options XPath options.
5590 * @return LY_ERR
5591 */
5592static LY_ERR
Michal Vaskod3678892020-05-21 10:06:58 +02005593moveto_scnode(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, int options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005594{
Michal Vaskocafad9d2019-11-07 15:20:03 +01005595 int i, orig_used, idx, temp_ctx = 0, getnext_opts;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005596 uint32_t mod_idx;
Michal Vasko519fd602020-05-26 12:17:39 +02005597 const struct lysc_node *iter, *start_parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005598
Michal Vaskod3678892020-05-21 10:06:58 +02005599 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005600 return LY_SUCCESS;
5601 }
5602
5603 if (set->type != LYXP_SET_SCNODE_SET) {
Michal Vaskof6e51882019-12-16 09:59:45 +01005604 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 +02005605 return LY_EVALID;
5606 }
5607
Michal Vaskocafad9d2019-11-07 15:20:03 +01005608 /* getnext opts */
5609 getnext_opts = LYS_GETNEXT_NOSTATECHECK;
5610 if (options & LYXP_SCNODE_OUTPUT) {
5611 getnext_opts |= LYS_GETNEXT_OUTPUT;
5612 }
5613
Michal Vasko03ff5a72019-09-11 13:49:33 +02005614 orig_used = set->used;
5615 for (i = 0; i < orig_used; ++i) {
5616 if (set->val.scnodes[i].in_ctx != 1) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01005617 if (set->val.scnodes[i].in_ctx != -2) {
5618 continue;
5619 }
5620
5621 /* remember context node */
5622 set->val.scnodes[i].in_ctx = -1;
Michal Vaskoec4df482019-12-16 10:02:18 +01005623 } else {
5624 set->val.scnodes[i].in_ctx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005625 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005626
5627 start_parent = set->val.scnodes[i].scnode;
5628
5629 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 +02005630 /* 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 +02005631 * so use it directly (root node itself is useless in this case) */
5632 mod_idx = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02005633 while (mod || (mod = (struct lys_module *)ly_ctx_get_module_iter(set->ctx, &mod_idx))) {
Michal Vasko519fd602020-05-26 12:17:39 +02005634 iter = NULL;
Michal Vasko509de4d2019-12-10 14:51:30 +01005635 /* module may not be implemented */
Michal Vasko519fd602020-05-26 12:17:39 +02005636 while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) {
5637 if (!moveto_scnode_check(iter, set->root_type, ncname, mod)) {
5638 idx = lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005639 /* we need to prevent these nodes from being considered in this moveto */
5640 if ((idx < orig_used) && (idx > i)) {
5641 set->val.scnodes[idx].in_ctx = 2;
5642 temp_ctx = 1;
5643 }
5644 }
5645 }
5646
5647 if (!mod_idx) {
Michal Vaskod3678892020-05-21 10:06:58 +02005648 /* mod was specified, we are not going through the whole context */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005649 break;
5650 }
5651 /* next iteration */
Michal Vaskod3678892020-05-21 10:06:58 +02005652 mod = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005653 }
5654
Michal Vasko519fd602020-05-26 12:17:39 +02005655 } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
5656 iter = NULL;
5657 while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) {
5658 if (!moveto_scnode_check(iter, set->root_type, ncname, (mod ? mod : set->local_mod))) {
5659 idx = lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005660 if ((idx < orig_used) && (idx > i)) {
5661 set->val.scnodes[idx].in_ctx = 2;
5662 temp_ctx = 1;
5663 }
5664 }
5665 }
5666 }
5667 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005668
5669 /* correct temporary in_ctx values */
5670 if (temp_ctx) {
5671 for (i = 0; i < orig_used; ++i) {
5672 if (set->val.scnodes[i].in_ctx == 2) {
5673 set->val.scnodes[i].in_ctx = 1;
5674 }
5675 }
5676 }
5677
5678 return LY_SUCCESS;
5679}
5680
5681/**
Michal Vaskod3678892020-05-21 10:06:58 +02005682 * @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 +02005683 * Context position aware.
5684 *
5685 * @param[in] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005686 * @param[in] mod Matching node module, NULL for any.
5687 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005688 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5689 */
5690static LY_ERR
Michal Vaskod3678892020-05-21 10:06:58 +02005691moveto_node_alldesc(struct lyxp_set *set, const struct lys_module *mod, const char *ncname)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005692{
5693 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005694 const struct lyd_node *next, *elem, *start;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005695 struct lyxp_set ret_set;
5696 LY_ERR rc;
5697
Michal Vaskod3678892020-05-21 10:06:58 +02005698 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005699 return LY_SUCCESS;
5700 }
5701
5702 if (set->type != LYXP_SET_NODE_SET) {
5703 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
5704 return LY_EVALID;
5705 }
5706
Michal Vasko9f96a052020-03-10 09:41:45 +01005707 /* 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 +02005708 rc = moveto_node(set, NULL, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005709 LY_CHECK_RET(rc);
5710
Michal Vasko6346ece2019-09-24 13:12:53 +02005711 /* this loop traverses all the nodes in the set and adds/keeps only those that match qname */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005712 set_init(&ret_set, set);
5713 for (i = 0; i < set->used; ++i) {
5714
5715 /* TREE DFS */
5716 start = set->val.nodes[i].node;
5717 for (elem = next = start; elem; elem = next) {
Michal Vaskod3678892020-05-21 10:06:58 +02005718 rc = moveto_node_check(elem, set->root_type, ncname, mod);
Michal Vasko6346ece2019-09-24 13:12:53 +02005719 if (!rc) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005720 /* add matching node into result set */
5721 set_insert_node(&ret_set, elem, 0, LYXP_NODE_ELEM, ret_set.used);
5722 if (set_dup_node_check(set, elem, LYXP_NODE_ELEM, i)) {
5723 /* the node is a duplicate, we'll process it later in the set */
5724 goto skip_children;
5725 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005726 } else if (rc == LY_EINCOMPLETE) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005727 return rc;
5728 } else if (rc == LY_EINVAL) {
5729 goto skip_children;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005730 }
5731
5732 /* TREE DFS NEXT ELEM */
5733 /* select element for the next run - children first */
Michal Vasko5bfd4be2020-06-23 13:26:19 +02005734 next = lyd_node_children(elem, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005735 if (!next) {
5736skip_children:
5737 /* no children, so try siblings, but only if it's not the start,
5738 * that is considered to be the root and it's siblings are not traversed */
5739 if (elem != start) {
5740 next = elem->next;
5741 } else {
5742 break;
5743 }
5744 }
5745 while (!next) {
5746 /* no siblings, go back through the parents */
5747 if ((struct lyd_node *)elem->parent == start) {
5748 /* we are done, no next element to process */
5749 break;
5750 }
5751 /* parent is already processed, go to its sibling */
5752 elem = (struct lyd_node *)elem->parent;
5753 next = elem->next;
5754 }
5755 }
5756 }
5757
5758 /* make the temporary set the current one */
5759 ret_set.ctx_pos = set->ctx_pos;
5760 ret_set.ctx_size = set->ctx_size;
Michal Vaskod3678892020-05-21 10:06:58 +02005761 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005762 memcpy(set, &ret_set, sizeof *set);
5763
5764 return LY_SUCCESS;
5765}
5766
5767/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005768 * @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 +02005769 *
5770 * @param[in] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005771 * @param[in] mod Matching node module, NULL for any.
5772 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005773 * @param[in] options XPath options.
5774 * @return LY_ERR
5775 */
5776static LY_ERR
Michal Vaskod3678892020-05-21 10:06:58 +02005777moveto_scnode_alldesc(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, int options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005778{
Michal Vasko6346ece2019-09-24 13:12:53 +02005779 int i, orig_used, idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005780 const struct lysc_node *next, *elem, *start;
Michal Vasko6346ece2019-09-24 13:12:53 +02005781 LY_ERR rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005782
Michal Vaskod3678892020-05-21 10:06:58 +02005783 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005784 return LY_SUCCESS;
5785 }
5786
5787 if (set->type != LYXP_SET_SCNODE_SET) {
Michal Vaskof6e51882019-12-16 09:59:45 +01005788 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 +02005789 return LY_EVALID;
5790 }
5791
Michal Vasko03ff5a72019-09-11 13:49:33 +02005792 orig_used = set->used;
5793 for (i = 0; i < orig_used; ++i) {
5794 if (set->val.scnodes[i].in_ctx != 1) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01005795 if (set->val.scnodes[i].in_ctx != -2) {
5796 continue;
5797 }
5798
5799 /* remember context node */
5800 set->val.scnodes[i].in_ctx = -1;
Michal Vaskoec4df482019-12-16 10:02:18 +01005801 } else {
5802 set->val.scnodes[i].in_ctx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005803 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005804
5805 /* TREE DFS */
5806 start = set->val.scnodes[i].scnode;
5807 for (elem = next = start; elem; elem = next) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005808 if ((elem == start) || (elem->nodetype & (LYS_CHOICE | LYS_CASE))) {
5809 /* schema-only nodes, skip root */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005810 goto next_iter;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005811 }
5812
Michal Vaskod3678892020-05-21 10:06:58 +02005813 rc = moveto_scnode_check(elem, set->root_type, ncname, mod);
Michal Vasko6346ece2019-09-24 13:12:53 +02005814 if (!rc) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01005815 if ((idx = lyxp_set_scnode_dup_node_check(set, elem, LYXP_NODE_ELEM, i)) > -1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005816 set->val.scnodes[idx].in_ctx = 1;
5817 if (idx > i) {
5818 /* we will process it later in the set */
5819 goto skip_children;
5820 }
5821 } else {
Michal Vaskoecd62de2019-11-13 12:35:11 +01005822 lyxp_set_scnode_insert_node(set, elem, LYXP_NODE_ELEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005823 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005824 } else if (rc == LY_EINVAL) {
5825 goto skip_children;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005826 }
5827
5828next_iter:
5829 /* TREE DFS NEXT ELEM */
5830 /* select element for the next run - children first */
5831 next = lysc_node_children(elem, options & LYXP_SCNODE_OUTPUT ? LYS_CONFIG_R : LYS_CONFIG_W);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005832 if (!next) {
5833skip_children:
5834 /* no children, so try siblings, but only if it's not the start,
5835 * that is considered to be the root and it's siblings are not traversed */
5836 if (elem != start) {
5837 next = elem->next;
5838 } else {
5839 break;
5840 }
5841 }
5842 while (!next) {
5843 /* no siblings, go back through the parents */
5844 if (elem->parent == start) {
5845 /* we are done, no next element to process */
5846 break;
5847 }
5848 /* parent is already processed, go to its sibling */
5849 elem = elem->parent;
5850 next = elem->next;
5851 }
5852 }
5853 }
5854
5855 return LY_SUCCESS;
5856}
5857
5858/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005859 * @brief Move context @p set to an attribute. Result is LYXP_SET_NODE_SET.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005860 * Indirectly context position aware.
5861 *
5862 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005863 * @param[in] mod Matching metadata module, NULL for any.
5864 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005865 * @return LY_ERR
5866 */
5867static LY_ERR
Michal Vaskod3678892020-05-21 10:06:58 +02005868moveto_attr(struct lyxp_set *set, const struct lys_module *mod, const char *ncname)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005869{
5870 uint32_t i;
Michal Vaskod3678892020-05-21 10:06:58 +02005871 int replaced;
Michal Vasko9f96a052020-03-10 09:41:45 +01005872 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005873
Michal Vaskod3678892020-05-21 10:06:58 +02005874 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005875 return LY_SUCCESS;
5876 }
5877
5878 if (set->type != LYXP_SET_NODE_SET) {
5879 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
5880 return LY_EVALID;
5881 }
5882
Michal Vasko03ff5a72019-09-11 13:49:33 +02005883 for (i = 0; i < set->used; ) {
5884 replaced = 0;
5885
5886 /* only attributes of an elem (not dummy) can be in the result, skip all the rest;
5887 * our attributes are always qualified */
Michal Vasko5c4e5892019-11-14 12:31:38 +01005888 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01005889 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005890
5891 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02005892 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005893 continue;
5894 }
5895
Michal Vaskod3678892020-05-21 10:06:58 +02005896 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005897 /* match */
5898 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01005899 set->val.meta[i].meta = sub;
5900 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005901 /* pos does not change */
5902 replaced = 1;
5903 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01005904 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 +02005905 }
5906 ++i;
5907 }
5908 }
5909 }
5910
5911 if (!replaced) {
5912 /* no match */
5913 set_remove_node(set, i);
5914 }
5915 }
5916
5917 return LY_SUCCESS;
5918}
5919
5920/**
5921 * @brief Move context @p set1 to union with @p set2. @p set2 is emptied afterwards.
Michal Vasko61ac2f62020-05-25 12:39:51 +02005922 * Result is LYXP_SET_NODE_SET. Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005923 *
5924 * @param[in,out] set1 Set to use for the result.
5925 * @param[in] set2 Set that is copied to @p set1.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005926 * @return LY_ERR
5927 */
5928static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005929moveto_union(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005930{
5931 LY_ERR rc;
5932
Michal Vaskod3678892020-05-21 10:06:58 +02005933 if ((set1->type != LYXP_SET_NODE_SET) || (set2->type != LYXP_SET_NODE_SET)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005934 LOGVAL(set1->ctx, LY_VLOG_LYD, set1->ctx_node, LY_VCODE_XP_INOP_2, "union", print_set_type(set1), print_set_type(set2));
5935 return LY_EVALID;
5936 }
5937
5938 /* set2 is empty or both set1 and set2 */
Michal Vaskod3678892020-05-21 10:06:58 +02005939 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005940 return LY_SUCCESS;
5941 }
5942
Michal Vaskod3678892020-05-21 10:06:58 +02005943 if (!set1->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005944 memcpy(set1, set2, sizeof *set1);
5945 /* dynamic memory belongs to set1 now, do not free */
Michal Vaskod3678892020-05-21 10:06:58 +02005946 memset(set2, 0, sizeof *set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005947 return LY_SUCCESS;
5948 }
5949
5950 /* we assume sets are sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005951 assert(!set_sort(set1) && !set_sort(set2));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005952
5953 /* sort, remove duplicates */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005954 rc = set_sorted_merge(set1, set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005955 LY_CHECK_RET(rc);
5956
5957 /* final set must be sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005958 assert(!set_sort(set1));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005959
5960 return LY_SUCCESS;
5961}
5962
5963/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005964 * @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 +02005965 * Context position aware.
5966 *
5967 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005968 * @param[in] mod Matching metadata module, NULL for any.
5969 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005970 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5971 */
5972static int
Michal Vaskod3678892020-05-21 10:06:58 +02005973moveto_attr_alldesc(struct lyxp_set *set, const struct lys_module *mod, const char *ncname)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005974{
5975 uint32_t i;
Michal Vaskod3678892020-05-21 10:06:58 +02005976 int replaced;
Michal Vasko9f96a052020-03-10 09:41:45 +01005977 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005978 struct lyxp_set *set_all_desc = NULL;
5979 LY_ERR rc;
5980
Michal Vaskod3678892020-05-21 10:06:58 +02005981 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005982 return LY_SUCCESS;
5983 }
5984
5985 if (set->type != LYXP_SET_NODE_SET) {
5986 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
5987 return LY_EVALID;
5988 }
5989
Michal Vasko03ff5a72019-09-11 13:49:33 +02005990 /* can be optimized similarly to moveto_node_alldesc() and save considerable amount of memory,
5991 * but it likely won't be used much, so it's a waste of time */
5992 /* copy the context */
5993 set_all_desc = set_copy(set);
5994 /* get all descendant nodes (the original context nodes are removed) */
Michal Vaskod3678892020-05-21 10:06:58 +02005995 rc = moveto_node_alldesc(set_all_desc, NULL, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005996 if (rc != LY_SUCCESS) {
5997 lyxp_set_free(set_all_desc);
5998 return rc;
5999 }
6000 /* prepend the original context nodes */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006001 rc = moveto_union(set, set_all_desc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006002 if (rc != LY_SUCCESS) {
6003 lyxp_set_free(set_all_desc);
6004 return rc;
6005 }
6006 lyxp_set_free(set_all_desc);
6007
Michal Vasko03ff5a72019-09-11 13:49:33 +02006008 for (i = 0; i < set->used; ) {
6009 replaced = 0;
6010
6011 /* only attributes of an elem can be in the result, skip all the rest,
6012 * we have all attributes qualified in lyd tree */
6013 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01006014 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006015 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02006016 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006017 continue;
6018 }
6019
Michal Vaskod3678892020-05-21 10:06:58 +02006020 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006021 /* match */
6022 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01006023 set->val.meta[i].meta = sub;
6024 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006025 /* pos does not change */
6026 replaced = 1;
6027 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01006028 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 +02006029 }
6030 ++i;
6031 }
6032 }
6033 }
6034
6035 if (!replaced) {
6036 /* no match */
6037 set_remove_node(set, i);
6038 }
6039 }
6040
6041 return LY_SUCCESS;
6042}
6043
6044/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02006045 * @brief Move context @p set to self and al chilren, recursively. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET.
6046 * Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006047 *
6048 * @param[in] parent Current parent.
6049 * @param[in] parent_pos Position of @p parent.
6050 * @param[in] parent_type Node type of @p parent.
6051 * @param[in,out] to_set Set to use.
6052 * @param[in] dup_check_set Set for checking duplicities.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006053 * @param[in] options XPath options.
6054 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6055 */
6056static LY_ERR
6057moveto_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 +01006058 struct lyxp_set *to_set, const struct lyxp_set *dup_check_set, int options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006059{
Michal Vasko61ac2f62020-05-25 12:39:51 +02006060 const struct lyd_node *iter, *first;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006061 LY_ERR rc;
6062
6063 switch (parent_type) {
6064 case LYXP_NODE_ROOT:
6065 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko61ac2f62020-05-25 12:39:51 +02006066 assert(!parent);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006067
Michal Vasko61ac2f62020-05-25 12:39:51 +02006068 /* add all top-level nodes as elements */
6069 first = to_set->tree;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006070 break;
6071 case LYXP_NODE_ELEM:
Michal Vasko61ac2f62020-05-25 12:39:51 +02006072 /* add just the text node of this term element node */
6073 if (parent->schema->nodetype & (LYD_NODE_TERM | LYD_NODE_ANY)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006074 if (!set_dup_node_check(dup_check_set, parent, LYXP_NODE_TEXT, -1)) {
6075 set_insert_node(to_set, parent, parent_pos, LYXP_NODE_TEXT, to_set->used);
6076 }
Michal Vasko61ac2f62020-05-25 12:39:51 +02006077 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006078 }
Michal Vasko61ac2f62020-05-25 12:39:51 +02006079
6080 /* add all the children of this node */
Michal Vasko5bfd4be2020-06-23 13:26:19 +02006081 first = lyd_node_children(parent, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006082 break;
6083 default:
6084 LOGINT_RET(parent->schema->module->ctx);
6085 }
6086
Michal Vasko61ac2f62020-05-25 12:39:51 +02006087 /* add all top-level nodes as elements */
6088 LY_LIST_FOR(first, iter) {
6089 /* context check */
6090 if ((parent_type == LYXP_NODE_ROOT_CONFIG) && (iter->schema->flags & LYS_CONFIG_R)) {
6091 continue;
6092 }
6093
6094 /* when check */
6095 if (moveto_when_check(iter)) {
6096 return LY_EINCOMPLETE;
6097 }
6098
6099 if (!set_dup_node_check(dup_check_set, iter, LYXP_NODE_ELEM, -1)) {
6100 set_insert_node(to_set, iter, 0, LYXP_NODE_ELEM, to_set->used);
6101
6102 /* also add all the children of this node, recursively */
6103 rc = moveto_self_add_children_r(iter, 0, LYXP_NODE_ELEM, to_set, dup_check_set, options);
6104 LY_CHECK_RET(rc);
6105 }
6106 }
6107
Michal Vasko03ff5a72019-09-11 13:49:33 +02006108 return LY_SUCCESS;
6109}
6110
6111/**
6112 * @brief Move context @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET
6113 * (or LYXP_SET_EMPTY). Context position aware.
6114 *
6115 * @param[in,out] set Set to use.
6116 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6117 * @param[in] options XPath options.
6118 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6119 */
6120static LY_ERR
6121moveto_self(struct lyxp_set *set, int all_desc, int options)
6122{
6123 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006124 struct lyxp_set ret_set;
6125 LY_ERR rc;
6126
Michal Vaskod3678892020-05-21 10:06:58 +02006127 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006128 return LY_SUCCESS;
6129 }
6130
6131 if (set->type != LYXP_SET_NODE_SET) {
6132 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
6133 return LY_EVALID;
6134 }
6135
6136 /* nothing to do */
6137 if (!all_desc) {
6138 return LY_SUCCESS;
6139 }
6140
Michal Vasko03ff5a72019-09-11 13:49:33 +02006141 /* add all the children, they get added recursively */
6142 set_init(&ret_set, set);
6143 for (i = 0; i < set->used; ++i) {
6144 /* copy the current node to tmp */
6145 set_insert_node(&ret_set, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, ret_set.used);
6146
6147 /* do not touch attributes and text nodes */
Michal Vasko9f96a052020-03-10 09:41:45 +01006148 if ((set->val.nodes[i].type == LYXP_NODE_TEXT) || (set->val.nodes[i].type == LYXP_NODE_META)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006149 continue;
6150 }
6151
Michal Vasko03ff5a72019-09-11 13:49:33 +02006152 /* add all the children */
6153 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 +01006154 set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006155 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006156 lyxp_set_free_content(&ret_set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006157 return rc;
6158 }
6159 }
6160
6161 /* use the temporary set as the current one */
6162 ret_set.ctx_pos = set->ctx_pos;
6163 ret_set.ctx_size = set->ctx_size;
Michal Vaskod3678892020-05-21 10:06:58 +02006164 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006165 memcpy(set, &ret_set, sizeof *set);
6166
6167 return LY_SUCCESS;
6168}
6169
6170/**
6171 * @brief Move context schema @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_SCNODE_SET
6172 * (or LYXP_SET_EMPTY).
6173 *
6174 * @param[in,out] set Set to use.
6175 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6176 * @param[in] options XPath options.
6177 * @return LY_ERR
6178 */
6179static LY_ERR
6180moveto_scnode_self(struct lyxp_set *set, int all_desc, int options)
6181{
Michal Vasko519fd602020-05-26 12:17:39 +02006182 int getnext_opts;
6183 uint32_t i, mod_idx;
6184 const struct lysc_node *iter, *start_parent;
6185 const struct lys_module *mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006186
Michal Vaskod3678892020-05-21 10:06:58 +02006187 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006188 return LY_SUCCESS;
6189 }
6190
6191 if (set->type != LYXP_SET_SCNODE_SET) {
Michal Vaskof6e51882019-12-16 09:59:45 +01006192 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 +02006193 return LY_EVALID;
6194 }
6195
6196 /* nothing to do */
6197 if (!all_desc) {
6198 return LY_SUCCESS;
6199 }
6200
Michal Vasko519fd602020-05-26 12:17:39 +02006201 /* getnext opts */
6202 getnext_opts = LYS_GETNEXT_NOSTATECHECK;
6203 if (options & LYXP_SCNODE_OUTPUT) {
6204 getnext_opts |= LYS_GETNEXT_OUTPUT;
6205 }
6206
6207 /* add all the children, recursively as they are being added into the same set */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006208 for (i = 0; i < set->used; ++i) {
6209 if (set->val.scnodes[i].in_ctx != 1) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006210 if (set->val.scnodes[i].in_ctx != -2) {
6211 continue;
6212 }
6213
Michal Vasko519fd602020-05-26 12:17:39 +02006214 /* remember context node */
6215 set->val.scnodes[i].in_ctx = -1;
6216 } else {
6217 set->val.scnodes[i].in_ctx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006218 }
6219
Michal Vasko519fd602020-05-26 12:17:39 +02006220 start_parent = set->val.scnodes[i].scnode;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006221
Michal Vasko519fd602020-05-26 12:17:39 +02006222 if ((set->val.scnodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.scnodes[i].type == LYXP_NODE_ROOT)) {
6223 /* it can actually be in any module, it's all <running> */
6224 mod_idx = 0;
6225 while ((mod = (struct lys_module *)ly_ctx_get_module_iter(set->ctx, &mod_idx))) {
6226 iter = NULL;
6227 /* module may not be implemented */
6228 while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) {
6229 /* context check */
6230 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) {
6231 continue;
6232 }
6233
6234 lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM);
6235 /* throw away the insert index, we want to consider that node again, recursively */
6236 }
6237 }
6238
6239 } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
6240 iter = NULL;
6241 while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006242 /* context check */
Michal Vasko519fd602020-05-26 12:17:39 +02006243 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006244 continue;
6245 }
6246
Michal Vasko519fd602020-05-26 12:17:39 +02006247 lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006248 }
6249 }
6250 }
6251
6252 return LY_SUCCESS;
6253}
6254
6255/**
6256 * @brief Move context @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_NODE_SET
6257 * (or LYXP_SET_EMPTY). Context position aware.
6258 *
6259 * @param[in] set Set to use.
6260 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6261 * @param[in] options XPath options.
6262 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6263 */
6264static LY_ERR
6265moveto_parent(struct lyxp_set *set, int all_desc, int options)
6266{
6267 LY_ERR rc;
6268 uint32_t i;
6269 struct lyd_node *node, *new_node;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006270 enum lyxp_node_type new_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006271
Michal Vaskod3678892020-05-21 10:06:58 +02006272 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006273 return LY_SUCCESS;
6274 }
6275
6276 if (set->type != LYXP_SET_NODE_SET) {
6277 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
6278 return LY_EVALID;
6279 }
6280
6281 if (all_desc) {
6282 /* <path>//.. == <path>//./.. */
6283 rc = moveto_self(set, 1, options);
6284 LY_CHECK_RET(rc);
6285 }
6286
Michal Vasko57eab132019-09-24 11:46:26 +02006287 for (i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006288 node = set->val.nodes[i].node;
6289
6290 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
6291 new_node = (struct lyd_node *)node->parent;
6292 } else if (set->val.nodes[i].type == LYXP_NODE_TEXT) {
6293 new_node = node;
Michal Vasko9f96a052020-03-10 09:41:45 +01006294 } else if (set->val.nodes[i].type == LYXP_NODE_META) {
6295 new_node = set->val.meta[i].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006296 if (!new_node) {
6297 LOGINT_RET(set->ctx);
6298 }
6299 } else {
6300 /* root does not have a parent */
Michal Vasko2caefc12019-11-14 16:07:56 +01006301 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006302 continue;
6303 }
6304
Michal Vaskoa1424542019-11-14 16:08:52 +01006305 /* when check */
6306 if (moveto_when_check(new_node)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006307 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01006308 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006309
6310 /* node already there can also be the root */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006311 if (!new_node) {
6312 new_type = set->root_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006313
6314 /* node has a standard parent (it can equal the root, it's not the root yet since they are fake) */
6315 } else {
6316 new_type = LYXP_NODE_ELEM;
6317 }
6318
Michal Vasko03ff5a72019-09-11 13:49:33 +02006319 if (set_dup_node_check(set, new_node, new_type, -1)) {
Michal Vasko2caefc12019-11-14 16:07:56 +01006320 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006321 } else {
6322 set_replace_node(set, new_node, 0, new_type, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006323 }
6324 }
6325
Michal Vasko2caefc12019-11-14 16:07:56 +01006326 set_remove_nodes_none(set);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006327 assert(!set_sort(set) && !set_sorted_dup_node_clean(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006328
6329 return LY_SUCCESS;
6330}
6331
6332/**
6333 * @brief Move context schema @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_SCNODE_SET
6334 * (or LYXP_SET_EMPTY).
6335 *
6336 * @param[in] set Set to use.
6337 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6338 * @param[in] options XPath options.
6339 * @return LY_ERR
6340 */
6341static LY_ERR
6342moveto_scnode_parent(struct lyxp_set *set, int all_desc, int options)
6343{
6344 int idx, i, orig_used, temp_ctx = 0;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006345 const struct lysc_node *node, *new_node;
6346 enum lyxp_node_type new_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006347 LY_ERR rc;
6348
Michal Vaskod3678892020-05-21 10:06:58 +02006349 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006350 return LY_SUCCESS;
6351 }
6352
6353 if (set->type != LYXP_SET_SCNODE_SET) {
Michal Vaskof6e51882019-12-16 09:59:45 +01006354 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 +02006355 return LY_EVALID;
6356 }
6357
6358 if (all_desc) {
6359 /* <path>//.. == <path>//./.. */
6360 rc = moveto_scnode_self(set, 1, options);
6361 LY_CHECK_RET(rc);
6362 }
6363
Michal Vasko03ff5a72019-09-11 13:49:33 +02006364 orig_used = set->used;
6365 for (i = 0; i < orig_used; ++i) {
6366 if (set->val.scnodes[i].in_ctx != 1) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006367 if (set->val.scnodes[i].in_ctx != -2) {
6368 continue;
6369 }
6370
6371 /* remember context node */
6372 set->val.scnodes[i].in_ctx = -1;
Michal Vaskoec4df482019-12-16 10:02:18 +01006373 } else {
6374 set->val.scnodes[i].in_ctx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006375 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006376
6377 node = set->val.scnodes[i].scnode;
6378
6379 if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
Michal Vaskod3678892020-05-21 10:06:58 +02006380 new_node = lysc_data_parent(node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006381 } else {
6382 /* root does not have a parent */
6383 continue;
6384 }
6385
Michal Vasko03ff5a72019-09-11 13:49:33 +02006386 /* node has no parent */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006387 if (!new_node) {
6388 new_type = set->root_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006389
6390 /* node has a standard parent (it can equal the root, it's not the root yet since they are fake) */
6391 } else {
6392 new_type = LYXP_NODE_ELEM;
6393 }
6394
Michal Vaskoecd62de2019-11-13 12:35:11 +01006395 idx = lyxp_set_scnode_insert_node(set, new_node, new_type);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006396 if ((idx < orig_used) && (idx > i)) {
6397 set->val.scnodes[idx].in_ctx = 2;
6398 temp_ctx = 1;
6399 }
6400 }
6401
6402 if (temp_ctx) {
6403 for (i = 0; i < orig_used; ++i) {
6404 if (set->val.scnodes[i].in_ctx == 2) {
6405 set->val.scnodes[i].in_ctx = 1;
6406 }
6407 }
6408 }
6409
6410 return LY_SUCCESS;
6411}
6412
6413/**
6414 * @brief Move context @p set to the result of a comparison. Handles '=', '!=', '<=', '<', '>=', or '>'.
6415 * Result is LYXP_SET_BOOLEAN. Indirectly context position aware.
6416 *
6417 * @param[in,out] set1 Set to use for the result.
6418 * @param[in] set2 Set acting as the second operand for @p op.
6419 * @param[in] op Comparison operator to process.
6420 * @param[in] options XPath options.
6421 * @return LY_ERR
6422 */
6423static LY_ERR
6424moveto_op_comp(struct lyxp_set *set1, struct lyxp_set *set2, const char *op, int options)
6425{
6426 /*
6427 * NODE SET + NODE SET = NODE SET + STRING /(1 NODE SET) 2 STRING
6428 * NODE SET + STRING = STRING + STRING /1 STRING (2 STRING)
6429 * NODE SET + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6430 * NODE SET + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6431 * STRING + NODE SET = STRING + STRING /(1 STRING) 2 STRING
6432 * NUMBER + NODE SET = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6433 * BOOLEAN + NODE SET = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6434 *
6435 * '=' or '!='
6436 * BOOLEAN + BOOLEAN
6437 * BOOLEAN + STRING = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6438 * BOOLEAN + NUMBER = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6439 * STRING + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6440 * NUMBER + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6441 * NUMBER + NUMBER
6442 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6443 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6444 * STRING + STRING
6445 *
6446 * '<=', '<', '>=', '>'
6447 * NUMBER + NUMBER
6448 * BOOLEAN + BOOLEAN = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6449 * BOOLEAN + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6450 * BOOLEAN + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6451 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6452 * STRING + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6453 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6454 * NUMBER + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6455 * STRING + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6456 */
6457 struct lyxp_set iter1, iter2;
6458 int result;
6459 int64_t i;
6460 LY_ERR rc;
6461
Michal Vasko004d3152020-06-11 19:59:22 +02006462 memset(&iter1, 0, sizeof iter1);
6463 memset(&iter2, 0, sizeof iter2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006464
6465 /* iterative evaluation with node-sets */
6466 if ((set1->type == LYXP_SET_NODE_SET) || (set2->type == LYXP_SET_NODE_SET)) {
6467 if (set1->type == LYXP_SET_NODE_SET) {
6468 for (i = 0; i < set1->used; ++i) {
6469 switch (set2->type) {
6470 case LYXP_SET_NUMBER:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006471 rc = set_comp_cast(&iter1, set1, LYXP_SET_NUMBER, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006472 break;
6473 case LYXP_SET_BOOLEAN:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006474 rc = set_comp_cast(&iter1, set1, LYXP_SET_BOOLEAN, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006475 break;
6476 default:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006477 rc = set_comp_cast(&iter1, set1, LYXP_SET_STRING, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006478 break;
6479 }
6480 LY_CHECK_RET(rc);
6481
6482 rc = moveto_op_comp(&iter1, set2, op, options);
6483 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006484 lyxp_set_free_content(&iter1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006485 return rc;
6486 }
6487
6488 /* lazy evaluation until true */
Michal Vasko004d3152020-06-11 19:59:22 +02006489 if (iter1.val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006490 set_fill_boolean(set1, 1);
6491 return LY_SUCCESS;
6492 }
6493 }
6494 } else {
6495 for (i = 0; i < set2->used; ++i) {
6496 switch (set1->type) {
6497 case LYXP_SET_NUMBER:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006498 rc = set_comp_cast(&iter2, set2, LYXP_SET_NUMBER, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006499 break;
6500 case LYXP_SET_BOOLEAN:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006501 rc = set_comp_cast(&iter2, set2, LYXP_SET_BOOLEAN, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006502 break;
6503 default:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006504 rc = set_comp_cast(&iter2, set2, LYXP_SET_STRING, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006505 break;
6506 }
6507 LY_CHECK_RET(rc);
6508
6509 set_fill_set(&iter1, set1);
6510
6511 rc = moveto_op_comp(&iter1, &iter2, op, options);
6512 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006513 lyxp_set_free_content(&iter1);
6514 lyxp_set_free_content(&iter2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006515 return rc;
6516 }
Michal Vaskod3678892020-05-21 10:06:58 +02006517 lyxp_set_free_content(&iter2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006518
6519 /* lazy evaluation until true */
Michal Vasko004d3152020-06-11 19:59:22 +02006520 if (iter1.val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006521 set_fill_boolean(set1, 1);
6522 return LY_SUCCESS;
6523 }
6524 }
6525 }
6526
6527 /* false for all nodes */
6528 set_fill_boolean(set1, 0);
6529 return LY_SUCCESS;
6530 }
6531
6532 /* first convert properly */
6533 if ((op[0] == '=') || (op[0] == '!')) {
6534 if ((set1->type == LYXP_SET_BOOLEAN) || (set2->type == LYXP_SET_BOOLEAN)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006535 lyxp_set_cast(set1, LYXP_SET_BOOLEAN);
6536 lyxp_set_cast(set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006537 } else if ((set1->type == LYXP_SET_NUMBER) || (set2->type == LYXP_SET_NUMBER)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006538 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006539 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006540 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006541 LY_CHECK_RET(rc);
6542 } /* else we have 2 strings */
6543 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006544 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006545 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006546 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006547 LY_CHECK_RET(rc);
6548 }
6549
6550 assert(set1->type == set2->type);
6551
6552 /* compute result */
6553 if (op[0] == '=') {
6554 if (set1->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02006555 result = (set1->val.bln == set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006556 } else if (set1->type == LYXP_SET_NUMBER) {
6557 result = (set1->val.num == set2->val.num);
6558 } else {
6559 assert(set1->type == LYXP_SET_STRING);
Michal Vaskoac6c72f2019-11-14 16:09:34 +01006560 result = !strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006561 }
6562 } else if (op[0] == '!') {
6563 if (set1->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02006564 result = (set1->val.bln != set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006565 } else if (set1->type == LYXP_SET_NUMBER) {
6566 result = (set1->val.num != set2->val.num);
6567 } else {
6568 assert(set1->type == LYXP_SET_STRING);
Michal Vaskoac6c72f2019-11-14 16:09:34 +01006569 result = !strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006570 }
6571 } else {
6572 assert(set1->type == LYXP_SET_NUMBER);
6573 if (op[0] == '<') {
6574 if (op[1] == '=') {
6575 result = (set1->val.num <= set2->val.num);
6576 } else {
6577 result = (set1->val.num < set2->val.num);
6578 }
6579 } else {
6580 if (op[1] == '=') {
6581 result = (set1->val.num >= set2->val.num);
6582 } else {
6583 result = (set1->val.num > set2->val.num);
6584 }
6585 }
6586 }
6587
6588 /* assign result */
6589 if (result) {
6590 set_fill_boolean(set1, 1);
6591 } else {
6592 set_fill_boolean(set1, 0);
6593 }
6594
6595 return LY_SUCCESS;
6596}
6597
6598/**
6599 * @brief Move context @p set to the result of a basic operation. Handles '+', '-', unary '-', '*', 'div',
6600 * or 'mod'. Result is LYXP_SET_NUMBER. Indirectly context position aware.
6601 *
6602 * @param[in,out] set1 Set to use for the result.
6603 * @param[in] set2 Set acting as the second operand for @p op.
6604 * @param[in] op Operator to process.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006605 * @return LY_ERR
6606 */
6607static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006608moveto_op_math(struct lyxp_set *set1, struct lyxp_set *set2, const char *op)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006609{
6610 LY_ERR rc;
6611
6612 /* unary '-' */
6613 if (!set2 && (op[0] == '-')) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006614 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006615 LY_CHECK_RET(rc);
6616 set1->val.num *= -1;
6617 lyxp_set_free(set2);
6618 return LY_SUCCESS;
6619 }
6620
6621 assert(set1 && set2);
6622
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006623 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006624 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006625 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006626 LY_CHECK_RET(rc);
6627
6628 switch (op[0]) {
6629 /* '+' */
6630 case '+':
6631 set1->val.num += set2->val.num;
6632 break;
6633
6634 /* '-' */
6635 case '-':
6636 set1->val.num -= set2->val.num;
6637 break;
6638
6639 /* '*' */
6640 case '*':
6641 set1->val.num *= set2->val.num;
6642 break;
6643
6644 /* 'div' */
6645 case 'd':
6646 set1->val.num /= set2->val.num;
6647 break;
6648
6649 /* 'mod' */
6650 case 'm':
6651 set1->val.num = ((long long)set1->val.num) % ((long long)set2->val.num);
6652 break;
6653
6654 default:
6655 LOGINT_RET(set1->ctx);
6656 }
6657
6658 return LY_SUCCESS;
6659}
6660
6661/*
6662 * eval functions
6663 *
6664 * They execute a parsed XPath expression on some data subtree.
6665 */
6666
6667/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02006668 * @brief Evaluate Predicate. Logs directly on error.
6669 *
Michal Vaskod3678892020-05-21 10:06:58 +02006670 * [9] Predicate ::= '[' Expr ']'
Michal Vasko03ff5a72019-09-11 13:49:33 +02006671 *
6672 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02006673 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006674 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
6675 * @param[in] options XPath options.
6676 * @param[in] parent_pos_pred Whether parent predicate was a positional one.
6677 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6678 */
6679static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02006680eval_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 +02006681{
6682 LY_ERR rc;
Michal Vasko57eab132019-09-24 11:46:26 +02006683 uint16_t i, orig_exp;
Michal Vasko5c4e5892019-11-14 12:31:38 +01006684 uint32_t orig_pos, orig_size;
6685 int32_t pred_in_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006686 struct lyxp_set set2;
6687 struct lyd_node *orig_parent;
6688
6689 /* '[' */
6690 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02006691 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
6692 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006693
6694 if (!set) {
6695only_parse:
Michal Vasko004d3152020-06-11 19:59:22 +02006696 rc = eval_expr_select(exp, tok_idx, 0, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006697 LY_CHECK_RET(rc);
6698 } else if (set->type == LYXP_SET_NODE_SET) {
6699 /* 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 +01006700 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006701
6702 /* empty set, nothing to evaluate */
6703 if (!set->used) {
6704 goto only_parse;
6705 }
6706
Michal Vasko004d3152020-06-11 19:59:22 +02006707 orig_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006708 orig_pos = 0;
6709 orig_size = set->used;
6710 orig_parent = NULL;
Michal Vasko39dbf352020-05-21 10:08:59 +02006711 for (i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006712 set_init(&set2, set);
6713 set_insert_node(&set2, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, 0);
6714 /* remember the node context position for position() and context size for last(),
6715 * predicates should always be evaluated with respect to the child axis (since we do
6716 * not support explicit axes) so we assign positions based on their parents */
6717 if (parent_pos_pred && ((struct lyd_node *)set->val.nodes[i].node->parent != orig_parent)) {
6718 orig_parent = (struct lyd_node *)set->val.nodes[i].node->parent;
6719 orig_pos = 1;
6720 } else {
6721 ++orig_pos;
6722 }
6723
6724 set2.ctx_pos = orig_pos;
6725 set2.ctx_size = orig_size;
Michal Vasko004d3152020-06-11 19:59:22 +02006726 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006727
Michal Vasko004d3152020-06-11 19:59:22 +02006728 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006729 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006730 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006731 return rc;
6732 }
6733
6734 /* number is a position */
6735 if (set2.type == LYXP_SET_NUMBER) {
6736 if ((long long)set2.val.num == orig_pos) {
6737 set2.val.num = 1;
6738 } else {
6739 set2.val.num = 0;
6740 }
6741 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006742 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006743
6744 /* predicate satisfied or not? */
Michal Vasko004d3152020-06-11 19:59:22 +02006745 if (!set2.val.bln) {
Michal Vasko2caefc12019-11-14 16:07:56 +01006746 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006747 }
6748 }
Michal Vasko2caefc12019-11-14 16:07:56 +01006749 set_remove_nodes_none(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006750
6751 } else if (set->type == LYXP_SET_SCNODE_SET) {
6752 for (i = 0; i < set->used; ++i) {
6753 if (set->val.scnodes[i].in_ctx == 1) {
6754 /* there is a currently-valid node */
6755 break;
6756 }
6757 }
6758 /* empty set, nothing to evaluate */
6759 if (i == set->used) {
6760 goto only_parse;
6761 }
6762
Michal Vasko004d3152020-06-11 19:59:22 +02006763 orig_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006764
Michal Vasko03ff5a72019-09-11 13:49:33 +02006765 /* set special in_ctx to all the valid snodes */
6766 pred_in_ctx = set_scnode_new_in_ctx(set);
6767
6768 /* use the valid snodes one-by-one */
6769 for (i = 0; i < set->used; ++i) {
6770 if (set->val.scnodes[i].in_ctx != pred_in_ctx) {
6771 continue;
6772 }
6773 set->val.scnodes[i].in_ctx = 1;
6774
Michal Vasko004d3152020-06-11 19:59:22 +02006775 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006776
Michal Vasko004d3152020-06-11 19:59:22 +02006777 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006778 LY_CHECK_RET(rc);
6779
6780 set->val.scnodes[i].in_ctx = pred_in_ctx;
6781 }
6782
6783 /* restore the state as it was before the predicate */
6784 for (i = 0; i < set->used; ++i) {
6785 if (set->val.scnodes[i].in_ctx == 1) {
6786 set->val.scnodes[i].in_ctx = 0;
6787 } else if (set->val.scnodes[i].in_ctx == pred_in_ctx) {
6788 set->val.scnodes[i].in_ctx = 1;
6789 }
6790 }
6791
6792 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02006793 set2.type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006794 set_fill_set(&set2, set);
6795
Michal Vasko004d3152020-06-11 19:59:22 +02006796 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006797 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006798 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006799 return rc;
6800 }
6801
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006802 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02006803 if (!set2.val.bln) {
Michal Vaskod3678892020-05-21 10:06:58 +02006804 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006805 }
Michal Vaskod3678892020-05-21 10:06:58 +02006806 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006807 }
6808
6809 /* ']' */
Michal Vasko004d3152020-06-11 19:59:22 +02006810 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_BRACK2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006811 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02006812 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
6813 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006814
6815 return LY_SUCCESS;
6816}
6817
6818/**
Michal Vaskod3678892020-05-21 10:06:58 +02006819 * @brief Evaluate Literal. Logs directly on error.
6820 *
6821 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02006822 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02006823 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
6824 */
6825static void
Michal Vasko004d3152020-06-11 19:59:22 +02006826eval_literal(struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set)
Michal Vaskod3678892020-05-21 10:06:58 +02006827{
6828 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02006829 if (exp->tok_len[*tok_idx] == 2) {
Michal Vaskod3678892020-05-21 10:06:58 +02006830 set_fill_string(set, "", 0);
6831 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02006832 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 +02006833 }
6834 }
6835 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02006836 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
6837 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02006838}
6839
6840/**
Michal Vasko004d3152020-06-11 19:59:22 +02006841 * @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 +02006842 *
Michal Vasko004d3152020-06-11 19:59:22 +02006843 * @param[in] exp Full parsed XPath expression.
6844 * @param[in,out] tok_idx Index in @p exp at the beginning of the predicate, is updated on success.
6845 * @param[in] scnode Found schema node as context for the predicate.
6846 * @param[in] format Format of any prefixes in key names/values.
6847 * @param[out] predicates Parsed predicates.
6848 * @param[out] pred_type Type of @p predicates.
6849 * @return LY_SUCCESS on success,
6850 * @return LY_ERR on any error.
Michal Vaskod3678892020-05-21 10:06:58 +02006851 */
6852static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02006853eval_name_test_try_compile_predicates(struct lyxp_expr *exp, uint16_t *tok_idx, const struct lysc_node *scnode,
6854 LYD_FORMAT format, struct ly_path_predicate **predicates, enum ly_path_pred_type *pred_type)
Michal Vaskod3678892020-05-21 10:06:58 +02006855{
Michal Vasko004d3152020-06-11 19:59:22 +02006856 LY_ERR ret = LY_SUCCESS;
6857 uint16_t key_count, e_idx, pred_idx = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02006858 const struct lysc_node *key;
Michal Vasko004d3152020-06-11 19:59:22 +02006859 size_t pred_len;
6860 int prev_lo;
6861 struct lyxp_expr *exp2 = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02006862
Michal Vasko004d3152020-06-11 19:59:22 +02006863 assert(scnode->nodetype & (LYS_LIST | LYS_LEAFLIST));
Michal Vaskod3678892020-05-21 10:06:58 +02006864
Michal Vasko004d3152020-06-11 19:59:22 +02006865 if (scnode->nodetype == LYS_LIST) {
6866 /* get key count */
6867 if (scnode->flags & LYS_KEYLESS) {
6868 return LY_EINVAL;
6869 }
6870 for (key_count = 0, key = lysc_node_children(scnode, 0); key && (key->flags & LYS_KEY); key = key->next, ++key_count);
6871 assert(key_count);
Michal Vaskod3678892020-05-21 10:06:58 +02006872
Michal Vasko004d3152020-06-11 19:59:22 +02006873 /* learn where the predicates end */
6874 e_idx = *tok_idx;
6875 while (key_count) {
6876 /* '[' */
6877 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
6878 return LY_EINVAL;
6879 }
6880 ++e_idx;
6881
6882 /* ']' */
6883 while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
6884 ++e_idx;
6885 }
6886 ++e_idx;
6887
6888 /* another presumably key predicate parsed */
6889 --key_count;
6890 }
6891
6892 if (key_count) {
6893 /* some keys missing for sure */
6894 return LY_EINVAL;
6895 }
6896 } else {
6897 /* learn just where this single predicate ends */
6898 e_idx = *tok_idx;
6899
Michal Vaskod3678892020-05-21 10:06:58 +02006900 /* '[' */
Michal Vasko004d3152020-06-11 19:59:22 +02006901 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
6902 return LY_EINVAL;
6903 }
6904 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02006905
6906 /* ']' */
Michal Vasko004d3152020-06-11 19:59:22 +02006907 while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
6908 ++e_idx;
6909 }
6910 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02006911 }
6912
Michal Vasko004d3152020-06-11 19:59:22 +02006913 /* get the joined length of all the keys predicates/of the single leaf-list predicate */
6914 pred_len = (exp->tok_pos[e_idx - 1] + exp->tok_len[e_idx - 1]) - exp->tok_pos[*tok_idx];
6915
6916 /* turn logging off */
6917 prev_lo = ly_log_options(0);
6918
6919 /* parse the predicate(s) */
6920 LY_CHECK_GOTO(ret = ly_path_parse_predicate(scnode->module->ctx, exp->expr + exp->tok_pos[*tok_idx], pred_len,
6921 LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &exp2), cleanup);
6922
6923 /* compile */
6924 switch (format) {
6925 case LYD_SCHEMA:
Michal Vasko00cbf532020-06-15 13:58:47 +02006926 ret = ly_path_compile_predicate(scnode->module->ctx, scnode->module, scnode, exp2, &pred_idx, lys_resolve_prefix,
6927 scnode->module, LYD_SCHEMA, predicates, pred_type);
Michal Vasko004d3152020-06-11 19:59:22 +02006928 break;
6929 case LYD_JSON:
Michal Vasko00cbf532020-06-15 13:58:47 +02006930 ret = ly_path_compile_predicate(scnode->module->ctx, scnode->module, scnode, exp2, &pred_idx,
6931 lydjson_resolve_prefix, NULL, LYD_JSON, predicates, pred_type);
Michal Vasko004d3152020-06-11 19:59:22 +02006932 break;
6933 case LYD_XML:
Michal Vasko60ea6352020-06-29 13:39:39 +02006934 case LYD_LYB:
Michal Vasko004d3152020-06-11 19:59:22 +02006935 ret = LY_EINT;
6936 goto cleanup;
6937 }
6938 LY_CHECK_GOTO(ret, cleanup);
6939
6940 /* success, the predicate must include all the needed information for hash-based search */
6941 *tok_idx = e_idx;
6942
6943cleanup:
6944 ly_log_options(prev_lo);
6945 lyxp_expr_free(scnode->module->ctx, exp2);
6946 return ret;
Michal Vaskod3678892020-05-21 10:06:58 +02006947}
6948
6949/**
6950 * @brief Evaluate NameTest and any following Predicates. Logs directly on error.
6951 *
6952 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
6953 * [6] NodeTest ::= NameTest | NodeType '(' ')'
6954 * [7] NameTest ::= '*' | NCName ':' '*' | QName
6955 *
6956 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02006957 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02006958 * @param[in] attr_axis Whether to search attributes or standard nodes.
6959 * @param[in] all_desc Whether to search all the descendants or children only.
6960 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
6961 * @param[in] options XPath options.
6962 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6963 */
6964static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02006965eval_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 +02006966 int options)
6967{
6968 int i;
6969 char *path;
Michal Vasko004d3152020-06-11 19:59:22 +02006970 const char *ncname, *ncname_dict = NULL;
6971 uint16_t ncname_len;
Michal Vaskod3678892020-05-21 10:06:58 +02006972 const struct lys_module *moveto_mod;
6973 const struct lysc_node *scnode = NULL, *tmp;
Michal Vasko004d3152020-06-11 19:59:22 +02006974 struct ly_path_predicate *predicates = NULL;
6975 enum ly_path_pred_type pred_type = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02006976 LY_ERR rc = LY_SUCCESS;
6977
6978 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02006979 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
6980 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02006981
6982 if (!set) {
6983 goto moveto;
6984 }
6985
Michal Vasko004d3152020-06-11 19:59:22 +02006986 ncname = &exp->expr[exp->tok_pos[*tok_idx - 1]];
6987 ncname_len = exp->tok_len[*tok_idx - 1];
Michal Vaskod3678892020-05-21 10:06:58 +02006988
6989 /* parse (and skip) module name */
6990 rc = moveto_resolve_model(&ncname, &ncname_len, set, &moveto_mod);
6991 LY_CHECK_GOTO(rc, cleanup);
6992
6993 if (moveto_mod && !attr_axis && !all_desc && (set->type == LYXP_SET_NODE_SET)) {
6994 /* find the matching schema node in some parent in the context */
6995 for (i = 0; i < (signed)set->used; ++i) {
6996 if (set->val.nodes[i].type == set->root_type) {
6997 tmp = lys_find_child(NULL, moveto_mod, ncname, ncname_len, 0, 0);
6998 } else if ((set->val.nodes[i].type == LYXP_NODE_ELEM)
6999 && (!scnode || (lysc_data_parent(scnode) != set->val.nodes[i].node->schema))) {
7000 /* do not repeat the same search */
7001 tmp = lys_find_child(set->val.nodes[i].node->schema, moveto_mod, ncname, ncname_len, 0, 0);
7002 }
7003
7004 /* additional context check */
7005 if (tmp && (set->root_type == LYXP_NODE_ROOT_CONFIG) && (tmp->flags & LYS_CONFIG_R)) {
7006 tmp = NULL;
7007 }
7008
7009 if (tmp) {
7010 if (scnode) {
7011 /* we found a schema node with the same name but at different level, give up, too complicated */
7012 scnode = NULL;
7013 break;
7014 } else {
7015 /* remember the found schema node and continue to make sure it can be used */
7016 scnode = tmp;
7017 }
7018 tmp = NULL;
7019 }
7020 }
7021
Michal Vasko004d3152020-06-11 19:59:22 +02007022 if (scnode && (scnode->nodetype & (LYS_LIST | LYS_LEAFLIST))) {
7023 /* try to create the predicates */
7024 if (eval_name_test_try_compile_predicates(exp, tok_idx, scnode, set->format, &predicates, &pred_type)) {
7025 /* hashes cannot be used */
Michal Vaskod3678892020-05-21 10:06:58 +02007026 scnode = NULL;
7027 }
7028 }
7029 }
7030
Michal Vasko004d3152020-06-11 19:59:22 +02007031 if (!scnode && moveto_mod) {
7032 /* we are not able to match based on a schema node and not all the modules match,
7033 * use dictionary for efficient comparison */
7034 ncname_dict = lydict_insert(set->ctx, ncname, ncname_len);
Michal Vaskod3678892020-05-21 10:06:58 +02007035 }
7036
7037moveto:
7038 /* move to the attribute(s), data node(s), or schema node(s) */
7039 if (attr_axis) {
7040 if (set && (options & LYXP_SCNODE_ALL)) {
7041 set_scnode_clear_ctx(set);
7042 } else {
7043 if (all_desc) {
Michal Vasko004d3152020-06-11 19:59:22 +02007044 rc = moveto_attr_alldesc(set, moveto_mod, ncname_dict);
Michal Vaskod3678892020-05-21 10:06:58 +02007045 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007046 rc = moveto_attr(set, moveto_mod, ncname_dict);
Michal Vaskod3678892020-05-21 10:06:58 +02007047 }
7048 LY_CHECK_GOTO(rc, cleanup);
7049 }
7050 } else {
7051 if (set && (options & LYXP_SCNODE_ALL)) {
7052 if (all_desc) {
Michal Vasko004d3152020-06-11 19:59:22 +02007053 rc = moveto_scnode_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007054 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007055 rc = moveto_scnode(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007056 }
7057 LY_CHECK_GOTO(rc, cleanup);
7058
7059 for (i = set->used - 1; i > -1; --i) {
7060 if (set->val.scnodes[i].in_ctx > 0) {
7061 break;
7062 }
7063 }
7064 if (i == -1) {
7065 path = lysc_path(set->ctx_scnode, LYSC_PATH_LOG, NULL, 0);
7066 LOGWRN(set->ctx, "Schema node \"%.*s\" not found (%.*s) with context node \"%s\".",
Michal Vasko004d3152020-06-11 19:59:22 +02007067 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]],
7068 exp->tok_pos[*tok_idx] + exp->tok_len[*tok_idx], exp->expr, path);
Michal Vaskod3678892020-05-21 10:06:58 +02007069 free(path);
7070 }
7071 } else {
7072 if (all_desc) {
Michal Vasko004d3152020-06-11 19:59:22 +02007073 rc = moveto_node_alldesc(set, moveto_mod, ncname_dict);
Michal Vaskod3678892020-05-21 10:06:58 +02007074 } else {
7075 if (scnode) {
7076 /* we can find the nodes using hashes */
Michal Vasko004d3152020-06-11 19:59:22 +02007077 rc = moveto_node_hash(set, scnode, predicates);
Michal Vaskod3678892020-05-21 10:06:58 +02007078 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007079 rc = moveto_node(set, moveto_mod, ncname_dict);
Michal Vaskod3678892020-05-21 10:06:58 +02007080 }
7081 }
7082 LY_CHECK_GOTO(rc, cleanup);
7083 }
7084 }
7085
7086 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007087 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7088 rc = eval_predicate(exp, tok_idx, set, options, 1);
Michal Vaskod3678892020-05-21 10:06:58 +02007089 LY_CHECK_RET(rc);
7090 }
7091
7092cleanup:
Michal Vaskodb51a8d2020-05-27 15:22:29 +02007093 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007094 lydict_remove(set->ctx, ncname_dict);
7095 ly_path_predicates_free(set->ctx, pred_type, scnode, predicates);
Michal Vaskodb51a8d2020-05-27 15:22:29 +02007096 }
Michal Vaskod3678892020-05-21 10:06:58 +02007097 return rc;
7098}
7099
7100/**
7101 * @brief Evaluate NodeType and any following Predicates. Logs directly on error.
7102 *
7103 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
7104 * [6] NodeTest ::= NameTest | NodeType '(' ')'
7105 * [8] NodeType ::= 'text' | 'node'
7106 *
7107 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007108 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02007109 * @param[in] attr_axis Whether to search attributes or standard nodes.
7110 * @param[in] all_desc Whether to search all the descendants or children only.
7111 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7112 * @param[in] options XPath options.
7113 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7114 */
7115static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007116eval_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 +02007117 struct lyxp_set *set, int options)
7118{
7119 LY_ERR rc;
7120
7121 /* TODO */
7122 (void)attr_axis;
7123 (void)all_desc;
7124
7125 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007126 assert(exp->tok_len[*tok_idx] == 4);
Michal Vaskod3678892020-05-21 10:06:58 +02007127 if (set->type == LYXP_SET_SCNODE_SET) {
7128 set_scnode_clear_ctx(set);
7129 /* just for the debug message below */
7130 set = NULL;
7131 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007132 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "node", 4)) {
Michal Vaskod3678892020-05-21 10:06:58 +02007133 rc = xpath_node(NULL, 0, set, options);
7134 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007135 assert(!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "text", 4));
Michal Vaskod3678892020-05-21 10:06:58 +02007136 rc = xpath_text(NULL, 0, set, options);
7137 }
7138 LY_CHECK_RET(rc);
7139 }
7140 }
7141 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007142 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7143 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007144
7145 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02007146 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
Michal Vaskod3678892020-05-21 10:06:58 +02007147 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007148 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7149 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007150
7151 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007152 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
Michal Vaskod3678892020-05-21 10:06:58 +02007153 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007154 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7155 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007156
7157 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007158 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7159 rc = eval_predicate(exp, tok_idx, set, options, 1);
Michal Vaskod3678892020-05-21 10:06:58 +02007160 LY_CHECK_RET(rc);
7161 }
7162
7163 return LY_SUCCESS;
7164}
7165
7166/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02007167 * @brief Evaluate RelativeLocationPath. Logs directly on error.
7168 *
7169 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
7170 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
Michal Vaskod3678892020-05-21 10:06:58 +02007171 * [6] NodeTest ::= NameTest | NodeType '(' ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007172 *
7173 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007174 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007175 * @param[in] all_desc Whether to search all the descendants or children only.
7176 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7177 * @param[in] options XPath options.
7178 * @return LY_ERR (YL_EINCOMPLETE on unresolved when)
7179 */
7180static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007181eval_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 +02007182{
7183 int attr_axis;
7184 LY_ERR rc;
7185
7186 goto step;
7187 do {
7188 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02007189 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007190 all_desc = 0;
7191 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007192 assert(exp->tok_len[*tok_idx] == 2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007193 all_desc = 1;
7194 }
7195 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007196 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7197 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007198
7199step:
Michal Vaskod3678892020-05-21 10:06:58 +02007200 /* evaluate abbreviated axis '@'? if any */
Michal Vasko004d3152020-06-11 19:59:22 +02007201 if (exp->tokens[*tok_idx] == LYXP_TOKEN_AT) {
Michal Vaskod3678892020-05-21 10:06:58 +02007202 attr_axis = 1;
7203
7204 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007205 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7206 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007207 } else {
7208 attr_axis = 0;
7209 }
7210
Michal Vasko03ff5a72019-09-11 13:49:33 +02007211 /* Step */
Michal Vasko004d3152020-06-11 19:59:22 +02007212 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007213 case LYXP_TOKEN_DOT:
7214 /* evaluate '.' */
7215 if (set && (options & LYXP_SCNODE_ALL)) {
7216 rc = moveto_scnode_self(set, all_desc, options);
7217 } else {
7218 rc = moveto_self(set, all_desc, options);
7219 }
7220 LY_CHECK_RET(rc);
7221 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007222 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7223 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007224 break;
7225
7226 case LYXP_TOKEN_DDOT:
7227 /* evaluate '..' */
7228 if (set && (options & LYXP_SCNODE_ALL)) {
7229 rc = moveto_scnode_parent(set, all_desc, options);
7230 } else {
7231 rc = moveto_parent(set, all_desc, options);
7232 }
7233 LY_CHECK_RET(rc);
7234 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007235 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7236 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007237 break;
7238
Michal Vasko03ff5a72019-09-11 13:49:33 +02007239 case LYXP_TOKEN_NAMETEST:
Michal Vaskod3678892020-05-21 10:06:58 +02007240 /* evaluate NameTest Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007241 rc = eval_name_test_with_predicate(exp, tok_idx, attr_axis, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007242 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02007243 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007244
Michal Vaskod3678892020-05-21 10:06:58 +02007245 case LYXP_TOKEN_NODETYPE:
7246 /* evaluate NodeType Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007247 rc = eval_node_type_with_predicate(exp, tok_idx, attr_axis, all_desc, set, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007248 LY_CHECK_RET(rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007249 break;
7250
7251 default:
Michal Vasko02a77382019-09-12 11:47:35 +02007252 LOGINT_RET(set ? set->ctx : NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007253 }
Michal Vasko004d3152020-06-11 19:59:22 +02007254 } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007255
7256 return LY_SUCCESS;
7257}
7258
7259/**
7260 * @brief Evaluate AbsoluteLocationPath. Logs directly on error.
7261 *
7262 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
7263 *
7264 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007265 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007266 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7267 * @param[in] options XPath options.
7268 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7269 */
7270static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007271eval_absolute_location_path(struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set, int options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007272{
7273 int all_desc;
7274 LY_ERR rc;
7275
7276 if (set) {
7277 /* no matter what tokens follow, we need to be at the root */
7278 moveto_root(set, options);
7279 }
7280
7281 /* '/' RelativeLocationPath? */
Michal Vasko004d3152020-06-11 19:59:22 +02007282 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007283 /* evaluate '/' - deferred */
7284 all_desc = 0;
7285 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007286 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7287 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007288
Michal Vasko004d3152020-06-11 19:59:22 +02007289 if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007290 return LY_SUCCESS;
7291 }
Michal Vasko004d3152020-06-11 19:59:22 +02007292 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007293 case LYXP_TOKEN_DOT:
7294 case LYXP_TOKEN_DDOT:
7295 case LYXP_TOKEN_AT:
7296 case LYXP_TOKEN_NAMETEST:
7297 case LYXP_TOKEN_NODETYPE:
Michal Vasko004d3152020-06-11 19:59:22 +02007298 rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007299 LY_CHECK_RET(rc);
7300 break;
7301 default:
7302 break;
7303 }
7304
7305 /* '//' RelativeLocationPath */
7306 } else {
7307 /* evaluate '//' - deferred so as not to waste memory by remembering all the nodes */
7308 all_desc = 1;
7309 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007310 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7311 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007312
Michal Vasko004d3152020-06-11 19:59:22 +02007313 rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007314 LY_CHECK_RET(rc);
7315 }
7316
7317 return LY_SUCCESS;
7318}
7319
7320/**
7321 * @brief Evaluate FunctionCall. Logs directly on error.
7322 *
Michal Vaskod3678892020-05-21 10:06:58 +02007323 * [11] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007324 *
7325 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007326 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007327 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7328 * @param[in] options XPath options.
7329 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7330 */
7331static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007332eval_function_call(struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set, int options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007333{
7334 LY_ERR rc;
7335 LY_ERR (*xpath_func)(struct lyxp_set **, uint16_t, struct lyxp_set *, int) = NULL;
Michal Vasko0cbf54f2019-12-16 10:01:06 +01007336 uint16_t arg_count = 0, i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007337 struct lyxp_set **args = NULL, **args_aux;
7338
7339 if (set) {
7340 /* FunctionName */
Michal Vasko004d3152020-06-11 19:59:22 +02007341 switch (exp->tok_len[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007342 case 3:
Michal Vasko004d3152020-06-11 19:59:22 +02007343 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007344 xpath_func = &xpath_not;
Michal Vasko004d3152020-06-11 19:59:22 +02007345 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007346 xpath_func = &xpath_sum;
7347 }
7348 break;
7349 case 4:
Michal Vasko004d3152020-06-11 19:59:22 +02007350 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007351 xpath_func = &xpath_lang;
Michal Vasko004d3152020-06-11 19:59:22 +02007352 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007353 xpath_func = &xpath_last;
Michal Vasko004d3152020-06-11 19:59:22 +02007354 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007355 xpath_func = &xpath_name;
Michal Vasko004d3152020-06-11 19:59:22 +02007356 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007357 xpath_func = &xpath_true;
7358 }
7359 break;
7360 case 5:
Michal Vasko004d3152020-06-11 19:59:22 +02007361 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007362 xpath_func = &xpath_count;
Michal Vasko004d3152020-06-11 19:59:22 +02007363 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007364 xpath_func = &xpath_false;
Michal Vasko004d3152020-06-11 19:59:22 +02007365 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007366 xpath_func = &xpath_floor;
Michal Vasko004d3152020-06-11 19:59:22 +02007367 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007368 xpath_func = &xpath_round;
Michal Vasko004d3152020-06-11 19:59:22 +02007369 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007370 xpath_func = &xpath_deref;
7371 }
7372 break;
7373 case 6:
Michal Vasko004d3152020-06-11 19:59:22 +02007374 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007375 xpath_func = &xpath_concat;
Michal Vasko004d3152020-06-11 19:59:22 +02007376 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007377 xpath_func = &xpath_number;
Michal Vasko004d3152020-06-11 19:59:22 +02007378 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007379 xpath_func = &xpath_string;
7380 }
7381 break;
7382 case 7:
Michal Vasko004d3152020-06-11 19:59:22 +02007383 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007384 xpath_func = &xpath_boolean;
Michal Vasko004d3152020-06-11 19:59:22 +02007385 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007386 xpath_func = &xpath_ceiling;
Michal Vasko004d3152020-06-11 19:59:22 +02007387 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007388 xpath_func = &xpath_current;
7389 }
7390 break;
7391 case 8:
Michal Vasko004d3152020-06-11 19:59:22 +02007392 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007393 xpath_func = &xpath_contains;
Michal Vasko004d3152020-06-11 19:59:22 +02007394 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007395 xpath_func = &xpath_position;
Michal Vasko004d3152020-06-11 19:59:22 +02007396 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007397 xpath_func = &xpath_re_match;
7398 }
7399 break;
7400 case 9:
Michal Vasko004d3152020-06-11 19:59:22 +02007401 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007402 xpath_func = &xpath_substring;
Michal Vasko004d3152020-06-11 19:59:22 +02007403 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007404 xpath_func = &xpath_translate;
7405 }
7406 break;
7407 case 10:
Michal Vasko004d3152020-06-11 19:59:22 +02007408 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007409 xpath_func = &xpath_local_name;
Michal Vasko004d3152020-06-11 19:59:22 +02007410 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007411 xpath_func = &xpath_enum_value;
Michal Vasko004d3152020-06-11 19:59:22 +02007412 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007413 xpath_func = &xpath_bit_is_set;
7414 }
7415 break;
7416 case 11:
Michal Vasko004d3152020-06-11 19:59:22 +02007417 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007418 xpath_func = &xpath_starts_with;
7419 }
7420 break;
7421 case 12:
Michal Vasko004d3152020-06-11 19:59:22 +02007422 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007423 xpath_func = &xpath_derived_from;
7424 }
7425 break;
7426 case 13:
Michal Vasko004d3152020-06-11 19:59:22 +02007427 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007428 xpath_func = &xpath_namespace_uri;
Michal Vasko004d3152020-06-11 19:59:22 +02007429 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007430 xpath_func = &xpath_string_length;
7431 }
7432 break;
7433 case 15:
Michal Vasko004d3152020-06-11 19:59:22 +02007434 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007435 xpath_func = &xpath_normalize_space;
Michal Vasko004d3152020-06-11 19:59:22 +02007436 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007437 xpath_func = &xpath_substring_after;
7438 }
7439 break;
7440 case 16:
Michal Vasko004d3152020-06-11 19:59:22 +02007441 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007442 xpath_func = &xpath_substring_before;
7443 }
7444 break;
7445 case 20:
Michal Vasko004d3152020-06-11 19:59:22 +02007446 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007447 xpath_func = &xpath_derived_from_or_self;
7448 }
7449 break;
7450 }
7451
7452 if (!xpath_func) {
Michal Vasko004d3152020-06-11 19:59:22 +02007453 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 +02007454 return LY_EVALID;
7455 }
7456 }
7457
7458 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007459 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7460 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007461
7462 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02007463 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007464 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007465 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7466 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007467
7468 /* ( Expr ( ',' Expr )* )? */
Michal Vasko004d3152020-06-11 19:59:22 +02007469 if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007470 if (set) {
7471 args = malloc(sizeof *args);
7472 LY_CHECK_ERR_GOTO(!args, LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7473 arg_count = 1;
7474 args[0] = set_copy(set);
7475 if (!args[0]) {
7476 rc = LY_EMEM;
7477 goto cleanup;
7478 }
7479
Michal Vasko004d3152020-06-11 19:59:22 +02007480 rc = eval_expr_select(exp, tok_idx, 0, args[0], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007481 LY_CHECK_GOTO(rc, cleanup);
7482 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007483 rc = eval_expr_select(exp, tok_idx, 0, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007484 LY_CHECK_GOTO(rc, cleanup);
7485 }
7486 }
Michal Vasko004d3152020-06-11 19:59:22 +02007487 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007488 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007489 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7490 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007491
7492 if (set) {
7493 ++arg_count;
7494 args_aux = realloc(args, arg_count * sizeof *args);
7495 LY_CHECK_ERR_GOTO(!args_aux, arg_count--; LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7496 args = args_aux;
7497 args[arg_count - 1] = set_copy(set);
7498 if (!args[arg_count - 1]) {
7499 rc = LY_EMEM;
7500 goto cleanup;
7501 }
7502
Michal Vasko004d3152020-06-11 19:59:22 +02007503 rc = eval_expr_select(exp, tok_idx, 0, args[arg_count - 1], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007504 LY_CHECK_GOTO(rc, cleanup);
7505 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007506 rc = eval_expr_select(exp, tok_idx, 0, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007507 LY_CHECK_GOTO(rc, cleanup);
7508 }
7509 }
7510
7511 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007512 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007513 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007514 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7515 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007516
7517 if (set) {
7518 /* evaluate function */
7519 rc = xpath_func(args, arg_count, set, options);
7520
7521 if (options & LYXP_SCNODE_ALL) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007522 /* merge all nodes from arg evaluations */
7523 for (i = 0; i < arg_count; ++i) {
7524 set_scnode_clear_ctx(args[i]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007525 lyxp_set_scnode_merge(set, args[i]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007526 }
7527 }
7528 } else {
7529 rc = LY_SUCCESS;
7530 }
7531
7532cleanup:
7533 for (i = 0; i < arg_count; ++i) {
7534 lyxp_set_free(args[i]);
7535 }
7536 free(args);
7537
7538 return rc;
7539}
7540
7541/**
7542 * @brief Evaluate Number. Logs directly on error.
7543 *
7544 * @param[in] ctx Context for errors.
7545 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007546 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007547 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7548 * @return LY_ERR
7549 */
7550static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007551eval_number(struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007552{
7553 long double num;
7554 char *endptr;
7555
7556 if (set) {
7557 errno = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02007558 num = strtold(&exp->expr[exp->tok_pos[*tok_idx]], &endptr);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007559 if (errno) {
Michal Vasko004d3152020-06-11 19:59:22 +02007560 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 +02007561 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 +02007562 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]], strerror(errno));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007563 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02007564 } else if (endptr - &exp->expr[exp->tok_pos[*tok_idx]] != exp->tok_len[*tok_idx]) {
7565 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 +02007566 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 +02007567 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007568 return LY_EVALID;
7569 }
7570
7571 set_fill_number(set, num);
7572 }
7573
7574 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007575 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7576 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007577 return LY_SUCCESS;
7578}
7579
7580/**
7581 * @brief Evaluate PathExpr. Logs directly on error.
7582 *
Michal Vaskod3678892020-05-21 10:06:58 +02007583 * [12] PathExpr ::= LocationPath | PrimaryExpr Predicate*
Michal Vasko03ff5a72019-09-11 13:49:33 +02007584 * | PrimaryExpr Predicate* '/' RelativeLocationPath
7585 * | PrimaryExpr Predicate* '//' RelativeLocationPath
7586 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
Michal Vaskod3678892020-05-21 10:06:58 +02007587 * [10] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall
Michal Vasko03ff5a72019-09-11 13:49:33 +02007588 *
7589 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007590 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007591 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7592 * @param[in] options XPath options.
7593 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7594 */
7595static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007596eval_path_expr(struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set, int options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007597{
7598 int all_desc, parent_pos_pred;
7599 LY_ERR rc;
7600
Michal Vasko004d3152020-06-11 19:59:22 +02007601 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007602 case LYXP_TOKEN_PAR1:
7603 /* '(' Expr ')' */
7604
7605 /* '(' */
7606 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007607 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7608 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007609
7610 /* Expr */
Michal Vasko004d3152020-06-11 19:59:22 +02007611 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007612 LY_CHECK_RET(rc);
7613
7614 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007615 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007616 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007617 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7618 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007619
7620 parent_pos_pred = 0;
7621 goto predicate;
7622
7623 case LYXP_TOKEN_DOT:
7624 case LYXP_TOKEN_DDOT:
7625 case LYXP_TOKEN_AT:
7626 case LYXP_TOKEN_NAMETEST:
7627 case LYXP_TOKEN_NODETYPE:
7628 /* RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007629 rc = eval_relative_location_path(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007630 LY_CHECK_RET(rc);
7631 break;
7632
7633 case LYXP_TOKEN_FUNCNAME:
7634 /* FunctionCall */
7635 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007636 rc = eval_function_call(exp, tok_idx, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007637 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007638 rc = eval_function_call(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007639 }
7640 LY_CHECK_RET(rc);
7641
7642 parent_pos_pred = 1;
7643 goto predicate;
7644
Michal Vasko3e48bf32020-06-01 08:39:07 +02007645 case LYXP_TOKEN_OPER_PATH:
7646 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02007647 /* AbsoluteLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007648 rc = eval_absolute_location_path(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007649 LY_CHECK_RET(rc);
7650 break;
7651
7652 case LYXP_TOKEN_LITERAL:
7653 /* Literal */
7654 if (!set || (options & LYXP_SCNODE_ALL)) {
7655 if (set) {
7656 set_scnode_clear_ctx(set);
7657 }
Michal Vasko004d3152020-06-11 19:59:22 +02007658 eval_literal(exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007659 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007660 eval_literal(exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007661 }
7662
7663 parent_pos_pred = 1;
7664 goto predicate;
7665
7666 case LYXP_TOKEN_NUMBER:
7667 /* Number */
7668 if (!set || (options & LYXP_SCNODE_ALL)) {
7669 if (set) {
7670 set_scnode_clear_ctx(set);
7671 }
Michal Vasko004d3152020-06-11 19:59:22 +02007672 rc = eval_number(NULL, exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007673 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007674 rc = eval_number(set->ctx, exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007675 }
7676 LY_CHECK_RET(rc);
7677
7678 parent_pos_pred = 1;
7679 goto predicate;
7680
7681 default:
Michal Vasko004d3152020-06-11 19:59:22 +02007682 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INTOK, lyxp_print_token(exp->tokens[*tok_idx]),
7683 &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007684 return LY_EVALID;
7685 }
7686
7687 return LY_SUCCESS;
7688
7689predicate:
7690 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007691 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7692 rc = eval_predicate(exp, tok_idx, set, options, parent_pos_pred);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007693 LY_CHECK_RET(rc);
7694 }
7695
7696 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007697 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007698
7699 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02007700 if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007701 all_desc = 0;
7702 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007703 all_desc = 1;
7704 }
7705
7706 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007707 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7708 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007709
Michal Vasko004d3152020-06-11 19:59:22 +02007710 rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007711 LY_CHECK_RET(rc);
7712 }
7713
7714 return LY_SUCCESS;
7715}
7716
7717/**
7718 * @brief Evaluate UnionExpr. Logs directly on error.
7719 *
Michal Vaskod3678892020-05-21 10:06:58 +02007720 * [20] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007721 *
7722 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007723 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007724 * @param[in] repeat How many times this expression is repeated.
7725 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7726 * @param[in] options XPath options.
7727 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7728 */
7729static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007730eval_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 +02007731{
7732 LY_ERR rc = LY_SUCCESS;
7733 struct lyxp_set orig_set, set2;
7734 uint16_t i;
7735
7736 assert(repeat);
7737
7738 set_init(&orig_set, set);
7739 set_init(&set2, set);
7740
7741 set_fill_set(&orig_set, set);
7742
Michal Vasko004d3152020-06-11 19:59:22 +02007743 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007744 LY_CHECK_GOTO(rc, cleanup);
7745
7746 /* ('|' PathExpr)* */
7747 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007748 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_UNI);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007749 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007750 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7751 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007752
7753 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007754 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007755 LY_CHECK_GOTO(rc, cleanup);
7756 continue;
7757 }
7758
7759 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02007760 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007761 LY_CHECK_GOTO(rc, cleanup);
7762
7763 /* eval */
7764 if (options & LYXP_SCNODE_ALL) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01007765 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007766 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007767 rc = moveto_union(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007768 LY_CHECK_GOTO(rc, cleanup);
7769 }
7770 }
7771
7772cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007773 lyxp_set_free_content(&orig_set);
7774 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007775 return rc;
7776}
7777
7778/**
7779 * @brief Evaluate UnaryExpr. Logs directly on error.
7780 *
Michal Vaskod3678892020-05-21 10:06:58 +02007781 * [19] UnaryExpr ::= UnionExpr | '-' UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007782 *
7783 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007784 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007785 * @param[in] repeat How many times this expression is repeated.
7786 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7787 * @param[in] options XPath options.
7788 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7789 */
7790static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007791eval_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 +02007792{
7793 LY_ERR rc;
7794 uint16_t this_op, i;
7795
7796 assert(repeat);
7797
7798 /* ('-')+ */
Michal Vasko004d3152020-06-11 19:59:22 +02007799 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007800 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007801 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 +02007802
7803 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007804 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7805 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007806 }
7807
Michal Vasko004d3152020-06-11 19:59:22 +02007808 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNARY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007809 LY_CHECK_RET(rc);
7810
7811 if (set && (repeat % 2)) {
7812 if (options & LYXP_SCNODE_ALL) {
7813 warn_operands(set->ctx, set, NULL, 1, exp->expr, exp->tok_pos[this_op]);
7814 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007815 rc = moveto_op_math(set, NULL, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007816 LY_CHECK_RET(rc);
7817 }
7818 }
7819
7820 return LY_SUCCESS;
7821}
7822
7823/**
7824 * @brief Evaluate MultiplicativeExpr. Logs directly on error.
7825 *
Michal Vaskod3678892020-05-21 10:06:58 +02007826 * [18] MultiplicativeExpr ::= UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007827 * | MultiplicativeExpr '*' UnaryExpr
7828 * | MultiplicativeExpr 'div' UnaryExpr
7829 * | MultiplicativeExpr 'mod' UnaryExpr
7830 *
7831 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007832 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007833 * @param[in] repeat How many times this expression is repeated.
7834 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7835 * @param[in] options XPath options.
7836 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7837 */
7838static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007839eval_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 +02007840{
7841 LY_ERR rc;
7842 uint16_t this_op;
7843 struct lyxp_set orig_set, set2;
7844 uint16_t i;
7845
7846 assert(repeat);
7847
7848 set_init(&orig_set, set);
7849 set_init(&set2, set);
7850
7851 set_fill_set(&orig_set, set);
7852
Michal Vasko004d3152020-06-11 19:59:22 +02007853 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007854 LY_CHECK_GOTO(rc, cleanup);
7855
7856 /* ('*' / 'div' / 'mod' UnaryExpr)* */
7857 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007858 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007859
Michal Vasko004d3152020-06-11 19:59:22 +02007860 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007861 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007862 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7863 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007864
7865 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007866 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007867 LY_CHECK_GOTO(rc, cleanup);
7868 continue;
7869 }
7870
7871 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02007872 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007873 LY_CHECK_GOTO(rc, cleanup);
7874
7875 /* eval */
7876 if (options & LYXP_SCNODE_ALL) {
7877 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007878 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007879 set_scnode_clear_ctx(set);
7880 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007881 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007882 LY_CHECK_GOTO(rc, cleanup);
7883 }
7884 }
7885
7886cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007887 lyxp_set_free_content(&orig_set);
7888 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007889 return rc;
7890}
7891
7892/**
7893 * @brief Evaluate AdditiveExpr. Logs directly on error.
7894 *
Michal Vaskod3678892020-05-21 10:06:58 +02007895 * [17] AdditiveExpr ::= MultiplicativeExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007896 * | AdditiveExpr '+' MultiplicativeExpr
7897 * | AdditiveExpr '-' MultiplicativeExpr
7898 *
7899 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007900 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007901 * @param[in] repeat How many times this expression is repeated.
7902 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7903 * @param[in] options XPath options.
7904 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7905 */
7906static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007907eval_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 +02007908{
7909 LY_ERR rc;
7910 uint16_t this_op;
7911 struct lyxp_set orig_set, set2;
7912 uint16_t i;
7913
7914 assert(repeat);
7915
7916 set_init(&orig_set, set);
7917 set_init(&set2, set);
7918
7919 set_fill_set(&orig_set, set);
7920
Michal Vasko004d3152020-06-11 19:59:22 +02007921 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007922 LY_CHECK_GOTO(rc, cleanup);
7923
7924 /* ('+' / '-' MultiplicativeExpr)* */
7925 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007926 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007927
Michal Vasko004d3152020-06-11 19:59:22 +02007928 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007929 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007930 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7931 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007932
7933 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007934 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007935 LY_CHECK_GOTO(rc, cleanup);
7936 continue;
7937 }
7938
7939 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02007940 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007941 LY_CHECK_GOTO(rc, cleanup);
7942
7943 /* eval */
7944 if (options & LYXP_SCNODE_ALL) {
7945 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007946 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007947 set_scnode_clear_ctx(set);
7948 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007949 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007950 LY_CHECK_GOTO(rc, cleanup);
7951 }
7952 }
7953
7954cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007955 lyxp_set_free_content(&orig_set);
7956 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007957 return rc;
7958}
7959
7960/**
7961 * @brief Evaluate RelationalExpr. Logs directly on error.
7962 *
Michal Vaskod3678892020-05-21 10:06:58 +02007963 * [16] RelationalExpr ::= AdditiveExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007964 * | RelationalExpr '<' AdditiveExpr
7965 * | RelationalExpr '>' AdditiveExpr
7966 * | RelationalExpr '<=' AdditiveExpr
7967 * | RelationalExpr '>=' AdditiveExpr
7968 *
7969 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007970 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007971 * @param[in] repeat How many times this expression is repeated.
7972 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7973 * @param[in] options XPath options.
7974 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7975 */
7976static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007977eval_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 +02007978{
7979 LY_ERR rc;
7980 uint16_t this_op;
7981 struct lyxp_set orig_set, set2;
7982 uint16_t i;
7983
7984 assert(repeat);
7985
7986 set_init(&orig_set, set);
7987 set_init(&set2, set);
7988
7989 set_fill_set(&orig_set, set);
7990
Michal Vasko004d3152020-06-11 19:59:22 +02007991 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007992 LY_CHECK_GOTO(rc, cleanup);
7993
7994 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
7995 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007996 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007997
Michal Vasko004d3152020-06-11 19:59:22 +02007998 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_COMP);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007999 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02008000 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
8001 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008002
8003 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02008004 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008005 LY_CHECK_GOTO(rc, cleanup);
8006 continue;
8007 }
8008
8009 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008010 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008011 LY_CHECK_GOTO(rc, cleanup);
8012
8013 /* eval */
8014 if (options & LYXP_SCNODE_ALL) {
8015 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008016 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008017 set_scnode_clear_ctx(set);
8018 } else {
8019 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options);
8020 LY_CHECK_GOTO(rc, cleanup);
8021 }
8022 }
8023
8024cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008025 lyxp_set_free_content(&orig_set);
8026 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008027 return rc;
8028}
8029
8030/**
8031 * @brief Evaluate EqualityExpr. Logs directly on error.
8032 *
Michal Vaskod3678892020-05-21 10:06:58 +02008033 * [15] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008034 * | EqualityExpr '!=' RelationalExpr
8035 *
8036 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008037 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008038 * @param[in] repeat How many times this expression is repeated.
8039 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8040 * @param[in] options XPath options.
8041 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8042 */
8043static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02008044eval_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 +02008045{
8046 LY_ERR rc;
8047 uint16_t this_op;
8048 struct lyxp_set orig_set, set2;
8049 uint16_t i;
8050
8051 assert(repeat);
8052
8053 set_init(&orig_set, set);
8054 set_init(&set2, set);
8055
8056 set_fill_set(&orig_set, set);
8057
Michal Vasko004d3152020-06-11 19:59:22 +02008058 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008059 LY_CHECK_GOTO(rc, cleanup);
8060
8061 /* ('=' / '!=' RelationalExpr)* */
8062 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008063 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008064
Michal Vasko004d3152020-06-11 19:59:22 +02008065 assert((exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_EQUAL) || (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_NEQUAL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008066 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02008067 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
8068 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008069
8070 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02008071 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008072 LY_CHECK_GOTO(rc, cleanup);
8073 continue;
8074 }
8075
8076 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008077 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008078 LY_CHECK_GOTO(rc, cleanup);
8079
8080 /* eval */
8081 if (options & LYXP_SCNODE_ALL) {
8082 warn_operands(set->ctx, set, &set2, 0, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vasko004d3152020-06-11 19:59:22 +02008083 warn_equality_value(exp, set, *tok_idx - 1, this_op - 1, *tok_idx - 1);
8084 warn_equality_value(exp, &set2, this_op - 1, this_op - 1, *tok_idx - 1);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008085 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008086 set_scnode_clear_ctx(set);
8087 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008088 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options);
8089 LY_CHECK_GOTO(rc, cleanup);
8090 }
8091 }
8092
8093cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008094 lyxp_set_free_content(&orig_set);
8095 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008096 return rc;
8097}
8098
8099/**
8100 * @brief Evaluate AndExpr. Logs directly on error.
8101 *
Michal Vaskod3678892020-05-21 10:06:58 +02008102 * [14] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008103 *
8104 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008105 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008106 * @param[in] repeat How many times this expression is repeated.
8107 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8108 * @param[in] options XPath options.
8109 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8110 */
8111static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02008112eval_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 +02008113{
8114 LY_ERR rc;
8115 struct lyxp_set orig_set, set2;
8116 uint16_t i;
8117
8118 assert(repeat);
8119
8120 set_init(&orig_set, set);
8121 set_init(&set2, set);
8122
8123 set_fill_set(&orig_set, set);
8124
Michal Vasko004d3152020-06-11 19:59:22 +02008125 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008126 LY_CHECK_GOTO(rc, cleanup);
8127
8128 /* cast to boolean, we know that will be the final result */
8129 if (set && (options & LYXP_SCNODE_ALL)) {
8130 set_scnode_clear_ctx(set);
8131 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008132 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008133 }
8134
8135 /* ('and' EqualityExpr)* */
8136 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008137 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
8138 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (!set || !set->val.bln ? "skipped" : "parsed"),
8139 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
8140 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008141
8142 /* lazy evaluation */
Michal Vasko004d3152020-06-11 19:59:22 +02008143 if (!set || ((set->type == LYXP_SET_BOOLEAN) && !set->val.bln)) {
8144 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008145 LY_CHECK_GOTO(rc, cleanup);
8146 continue;
8147 }
8148
8149 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008150 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008151 LY_CHECK_GOTO(rc, cleanup);
8152
8153 /* eval - just get boolean value actually */
8154 if (set->type == LYXP_SET_SCNODE_SET) {
8155 set_scnode_clear_ctx(&set2);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008156 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008157 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008158 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008159 set_fill_set(set, &set2);
8160 }
8161 }
8162
8163cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008164 lyxp_set_free_content(&orig_set);
8165 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008166 return rc;
8167}
8168
8169/**
8170 * @brief Evaluate OrExpr. Logs directly on error.
8171 *
Michal Vaskod3678892020-05-21 10:06:58 +02008172 * [13] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008173 *
8174 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008175 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008176 * @param[in] repeat How many times this expression is repeated.
8177 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8178 * @param[in] options XPath options.
8179 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8180 */
8181static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02008182eval_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 +02008183{
8184 LY_ERR rc;
8185 struct lyxp_set orig_set, set2;
8186 uint16_t i;
8187
8188 assert(repeat);
8189
8190 set_init(&orig_set, set);
8191 set_init(&set2, set);
8192
8193 set_fill_set(&orig_set, set);
8194
Michal Vasko004d3152020-06-11 19:59:22 +02008195 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008196 LY_CHECK_GOTO(rc, cleanup);
8197
8198 /* cast to boolean, we know that will be the final result */
8199 if (set && (options & LYXP_SCNODE_ALL)) {
8200 set_scnode_clear_ctx(set);
8201 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008202 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008203 }
8204
8205 /* ('or' AndExpr)* */
8206 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008207 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
8208 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (!set || set->val.bln ? "skipped" : "parsed"),
8209 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
8210 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008211
8212 /* lazy evaluation */
Michal Vasko004d3152020-06-11 19:59:22 +02008213 if (!set || ((set->type == LYXP_SET_BOOLEAN) && set->val.bln)) {
8214 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008215 LY_CHECK_GOTO(rc, cleanup);
8216 continue;
8217 }
8218
8219 set_fill_set(&set2, &orig_set);
8220 /* expr_type cound have been LYXP_EXPR_NONE in all these later calls (except for the first one),
8221 * but it does not matter */
Michal Vasko004d3152020-06-11 19:59:22 +02008222 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008223 LY_CHECK_GOTO(rc, cleanup);
8224
8225 /* eval - just get boolean value actually */
8226 if (set->type == LYXP_SET_SCNODE_SET) {
8227 set_scnode_clear_ctx(&set2);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008228 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008229 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008230 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008231 set_fill_set(set, &set2);
8232 }
8233 }
8234
8235cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008236 lyxp_set_free_content(&orig_set);
8237 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008238 return rc;
8239}
8240
8241/**
Michal Vasko004d3152020-06-11 19:59:22 +02008242 * @brief Decide what expression is at the pointer @p tok_idx and evaluate it accordingly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008243 *
8244 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008245 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008246 * @param[in] etype Expression type to evaluate.
8247 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8248 * @param[in] options XPath options.
8249 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8250 */
8251static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02008252eval_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 +02008253{
8254 uint16_t i, count;
8255 enum lyxp_expr_type next_etype;
8256 LY_ERR rc;
8257
8258 /* process operator repeats */
Michal Vasko004d3152020-06-11 19:59:22 +02008259 if (!exp->repeat[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008260 next_etype = LYXP_EXPR_NONE;
8261 } else {
8262 /* find etype repeat */
Michal Vasko004d3152020-06-11 19:59:22 +02008263 for (i = 0; exp->repeat[*tok_idx][i] > etype; ++i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008264
8265 /* select one-priority lower because etype expression called us */
8266 if (i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008267 next_etype = exp->repeat[*tok_idx][i - 1];
Michal Vasko03ff5a72019-09-11 13:49:33 +02008268 /* count repeats for that expression */
Michal Vasko004d3152020-06-11 19:59:22 +02008269 for (count = 0; i && exp->repeat[*tok_idx][i - 1] == next_etype; ++count, --i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008270 } else {
8271 next_etype = LYXP_EXPR_NONE;
8272 }
8273 }
8274
8275 /* decide what expression are we parsing based on the repeat */
8276 switch (next_etype) {
8277 case LYXP_EXPR_OR:
Michal Vasko004d3152020-06-11 19:59:22 +02008278 rc = eval_or_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008279 break;
8280 case LYXP_EXPR_AND:
Michal Vasko004d3152020-06-11 19:59:22 +02008281 rc = eval_and_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008282 break;
8283 case LYXP_EXPR_EQUALITY:
Michal Vasko004d3152020-06-11 19:59:22 +02008284 rc = eval_equality_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008285 break;
8286 case LYXP_EXPR_RELATIONAL:
Michal Vasko004d3152020-06-11 19:59:22 +02008287 rc = eval_relational_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008288 break;
8289 case LYXP_EXPR_ADDITIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02008290 rc = eval_additive_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008291 break;
8292 case LYXP_EXPR_MULTIPLICATIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02008293 rc = eval_multiplicative_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008294 break;
8295 case LYXP_EXPR_UNARY:
Michal Vasko004d3152020-06-11 19:59:22 +02008296 rc = eval_unary_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008297 break;
8298 case LYXP_EXPR_UNION:
Michal Vasko004d3152020-06-11 19:59:22 +02008299 rc = eval_union_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008300 break;
8301 case LYXP_EXPR_NONE:
Michal Vasko004d3152020-06-11 19:59:22 +02008302 rc = eval_path_expr(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008303 break;
8304 default:
8305 LOGINT_RET(set->ctx);
8306 }
8307
8308 return rc;
8309}
8310
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008311/**
8312 * @brief Get root type.
8313 *
8314 * @param[in] ctx_node Context node.
8315 * @param[in] ctx_scnode Schema context node.
8316 * @param[in] options XPath options.
8317 * @return Root type.
8318 */
8319static enum lyxp_node_type
8320lyxp_get_root_type(const struct lyd_node *ctx_node, const struct lysc_node *ctx_scnode, int options)
8321{
8322 if (options & LYXP_SCNODE_ALL) {
8323 if (options & LYXP_SCNODE) {
8324 /* general root that can access everything */
8325 return LYXP_NODE_ROOT;
8326 } else if (!ctx_scnode || (ctx_scnode->flags & LYS_CONFIG_W)) {
8327 /* root context node can access only config data (because we said so, it is unspecified) */
8328 return LYXP_NODE_ROOT_CONFIG;
8329 } else {
8330 return LYXP_NODE_ROOT;
8331 }
8332 }
8333
8334 if (!ctx_node || (ctx_node->schema->flags & LYS_CONFIG_W)) {
8335 /* root context node can access only config data (because we said so, it is unspecified) */
8336 return LYXP_NODE_ROOT_CONFIG;
8337 }
8338
8339 return LYXP_NODE_ROOT;
8340}
8341
Michal Vasko03ff5a72019-09-11 13:49:33 +02008342LY_ERR
Michal Vaskoecd62de2019-11-13 12:35:11 +01008343lyxp_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 +01008344 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 +02008345{
Michal Vasko004d3152020-06-11 19:59:22 +02008346 uint16_t tok_idx = 0;
8347 const struct lyd_node *real_ctx_node;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008348 LY_ERR rc;
8349
Michal Vasko004d3152020-06-11 19:59:22 +02008350 LY_CHECK_ARG_RET(NULL, exp, local_mod, ctx_node, set, LY_EINVAL);
8351
8352 if ((ctx_node_type == LYXP_NODE_ROOT) || (ctx_node_type == LYXP_NODE_ROOT_CONFIG)) {
8353 /* we always need some context node because it is used for resolving unqualified names */
8354 real_ctx_node = NULL;
8355 } else {
8356 real_ctx_node = ctx_node;
8357 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008358
8359 /* prepare set for evaluation */
Michal Vasko004d3152020-06-11 19:59:22 +02008360 tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008361 memset(set, 0, sizeof *set);
Michal Vaskod3678892020-05-21 10:06:58 +02008362 set->type = LYXP_SET_NODE_SET;
Michal Vasko004d3152020-06-11 19:59:22 +02008363 set_insert_node(set, (struct lyd_node *)real_ctx_node, 0, ctx_node_type, 0);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008364 set->ctx = local_mod->ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008365 set->ctx_node = ctx_node;
Michal Vasko004d3152020-06-11 19:59:22 +02008366 set->root_type = lyxp_get_root_type(real_ctx_node, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008367 set->local_mod = local_mod;
Michal Vaskof03ed032020-03-04 13:31:44 +01008368 set->tree = tree;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008369 set->format = format;
8370
8371 /* evaluate */
Michal Vasko004d3152020-06-11 19:59:22 +02008372 rc = eval_expr_select(exp, &tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008373 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02008374 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008375 }
8376
Michal Vasko03ff5a72019-09-11 13:49:33 +02008377 return rc;
8378}
8379
8380#if 0
8381
8382/* full xml printing of set elements, not used currently */
8383
8384void
8385lyxp_set_print_xml(FILE *f, struct lyxp_set *set)
8386{
8387 uint32_t i;
8388 char *str_num;
8389 struct lyout out;
8390
8391 memset(&out, 0, sizeof out);
8392
8393 out.type = LYOUT_STREAM;
8394 out.method.f = f;
8395
8396 switch (set->type) {
8397 case LYXP_SET_EMPTY:
8398 ly_print(&out, "Empty XPath set\n\n");
8399 break;
8400 case LYXP_SET_BOOLEAN:
8401 ly_print(&out, "Boolean XPath set:\n");
8402 ly_print(&out, "%s\n\n", set->value.bool ? "true" : "false");
8403 break;
8404 case LYXP_SET_STRING:
8405 ly_print(&out, "String XPath set:\n");
8406 ly_print(&out, "\"%s\"\n\n", set->value.str);
8407 break;
8408 case LYXP_SET_NUMBER:
8409 ly_print(&out, "Number XPath set:\n");
8410
8411 if (isnan(set->value.num)) {
8412 str_num = strdup("NaN");
8413 } else if ((set->value.num == 0) || (set->value.num == -0.0f)) {
8414 str_num = strdup("0");
8415 } else if (isinf(set->value.num) && !signbit(set->value.num)) {
8416 str_num = strdup("Infinity");
8417 } else if (isinf(set->value.num) && signbit(set->value.num)) {
8418 str_num = strdup("-Infinity");
8419 } else if ((long long)set->value.num == set->value.num) {
8420 if (asprintf(&str_num, "%lld", (long long)set->value.num) == -1) {
8421 str_num = NULL;
8422 }
8423 } else {
8424 if (asprintf(&str_num, "%03.1Lf", set->value.num) == -1) {
8425 str_num = NULL;
8426 }
8427 }
8428 if (!str_num) {
8429 LOGMEM;
8430 return;
8431 }
8432 ly_print(&out, "%s\n\n", str_num);
8433 free(str_num);
8434 break;
8435 case LYXP_SET_NODE_SET:
8436 ly_print(&out, "Node XPath set:\n");
8437
8438 for (i = 0; i < set->used; ++i) {
8439 ly_print(&out, "%d. ", i + 1);
8440 switch (set->node_type[i]) {
8441 case LYXP_NODE_ROOT_ALL:
8442 ly_print(&out, "ROOT all\n\n");
8443 break;
8444 case LYXP_NODE_ROOT_CONFIG:
8445 ly_print(&out, "ROOT config\n\n");
8446 break;
8447 case LYXP_NODE_ROOT_STATE:
8448 ly_print(&out, "ROOT state\n\n");
8449 break;
8450 case LYXP_NODE_ROOT_NOTIF:
8451 ly_print(&out, "ROOT notification \"%s\"\n\n", set->value.nodes[i]->schema->name);
8452 break;
8453 case LYXP_NODE_ROOT_RPC:
8454 ly_print(&out, "ROOT rpc \"%s\"\n\n", set->value.nodes[i]->schema->name);
8455 break;
8456 case LYXP_NODE_ROOT_OUTPUT:
8457 ly_print(&out, "ROOT output \"%s\"\n\n", set->value.nodes[i]->schema->name);
8458 break;
8459 case LYXP_NODE_ELEM:
8460 ly_print(&out, "ELEM \"%s\"\n", set->value.nodes[i]->schema->name);
8461 xml_print_node(&out, 1, set->value.nodes[i], 1, LYP_FORMAT);
8462 ly_print(&out, "\n");
8463 break;
8464 case LYXP_NODE_TEXT:
8465 ly_print(&out, "TEXT \"%s\"\n\n", ((struct lyd_node_leaf_list *)set->value.nodes[i])->value_str);
8466 break;
8467 case LYXP_NODE_ATTR:
8468 ly_print(&out, "ATTR \"%s\" = \"%s\"\n\n", set->value.attrs[i]->name, set->value.attrs[i]->value);
8469 break;
8470 }
8471 }
8472 break;
8473 }
8474}
8475
8476#endif
8477
8478LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008479lyxp_set_cast(struct lyxp_set *set, enum lyxp_set_type target)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008480{
8481 long double num;
8482 char *str;
8483 LY_ERR rc;
8484
8485 if (!set || (set->type == target)) {
8486 return LY_SUCCESS;
8487 }
8488
8489 /* it's not possible to convert anything into a node set */
Michal Vaskod3678892020-05-21 10:06:58 +02008490 assert(target != LYXP_SET_NODE_SET);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008491
8492 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vaskod3678892020-05-21 10:06:58 +02008493 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008494 return LY_EINVAL;
8495 }
8496
8497 /* to STRING */
Michal Vaskod3678892020-05-21 10:06:58 +02008498 if ((target == LYXP_SET_STRING) || ((target == LYXP_SET_NUMBER) && (set->type == LYXP_SET_NODE_SET))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008499 switch (set->type) {
8500 case LYXP_SET_NUMBER:
8501 if (isnan(set->val.num)) {
8502 set->val.str = strdup("NaN");
8503 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8504 } else if ((set->val.num == 0) || (set->val.num == -0.0f)) {
8505 set->val.str = strdup("0");
8506 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8507 } else if (isinf(set->val.num) && !signbit(set->val.num)) {
8508 set->val.str = strdup("Infinity");
8509 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8510 } else if (isinf(set->val.num) && signbit(set->val.num)) {
8511 set->val.str = strdup("-Infinity");
8512 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8513 } else if ((long long)set->val.num == set->val.num) {
8514 if (asprintf(&str, "%lld", (long long)set->val.num) == -1) {
8515 LOGMEM_RET(set->ctx);
8516 }
8517 set->val.str = str;
8518 } else {
8519 if (asprintf(&str, "%03.1Lf", set->val.num) == -1) {
8520 LOGMEM_RET(set->ctx);
8521 }
8522 set->val.str = str;
8523 }
8524 break;
8525 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02008526 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008527 set->val.str = strdup("true");
8528 } else {
8529 set->val.str = strdup("false");
8530 }
8531 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM);
8532 break;
8533 case LYXP_SET_NODE_SET:
8534 assert(set->used);
8535
8536 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008537 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008538
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008539 rc = cast_node_set_to_string(set, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008540 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02008541 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008542 set->val.str = str;
8543 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008544 default:
8545 LOGINT_RET(set->ctx);
8546 }
8547 set->type = LYXP_SET_STRING;
8548 }
8549
8550 /* to NUMBER */
8551 if (target == LYXP_SET_NUMBER) {
8552 switch (set->type) {
8553 case LYXP_SET_STRING:
8554 num = cast_string_to_number(set->val.str);
Michal Vaskod3678892020-05-21 10:06:58 +02008555 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008556 set->val.num = num;
8557 break;
8558 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02008559 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008560 set->val.num = 1;
8561 } else {
8562 set->val.num = 0;
8563 }
8564 break;
8565 default:
8566 LOGINT_RET(set->ctx);
8567 }
8568 set->type = LYXP_SET_NUMBER;
8569 }
8570
8571 /* to BOOLEAN */
8572 if (target == LYXP_SET_BOOLEAN) {
8573 switch (set->type) {
8574 case LYXP_SET_NUMBER:
8575 if ((set->val.num == 0) || (set->val.num == -0.0f) || isnan(set->val.num)) {
Michal Vasko004d3152020-06-11 19:59:22 +02008576 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008577 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02008578 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008579 }
8580 break;
8581 case LYXP_SET_STRING:
8582 if (set->val.str[0]) {
Michal Vaskod3678892020-05-21 10:06:58 +02008583 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008584 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008585 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02008586 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008587 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008588 }
8589 break;
8590 case LYXP_SET_NODE_SET:
Michal Vaskod3678892020-05-21 10:06:58 +02008591 if (set->used) {
8592 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008593 set->val.bln = 1;
Michal Vaskod3678892020-05-21 10:06:58 +02008594 } else {
8595 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008596 set->val.bln = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02008597 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008598 break;
8599 default:
8600 LOGINT_RET(set->ctx);
8601 }
8602 set->type = LYXP_SET_BOOLEAN;
8603 }
8604
Michal Vasko03ff5a72019-09-11 13:49:33 +02008605 return LY_SUCCESS;
8606}
8607
8608LY_ERR
8609lyxp_atomize(struct lyxp_expr *exp, LYD_FORMAT format, const struct lys_module *local_mod, const struct lysc_node *ctx_scnode,
8610 enum lyxp_node_type ctx_scnode_type, struct lyxp_set *set, int options)
8611{
8612 struct ly_ctx *ctx;
Michal Vasko004d3152020-06-11 19:59:22 +02008613 const struct lysc_node *real_ctx_scnode;
8614 uint16_t tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008615
Michal Vasko004d3152020-06-11 19:59:22 +02008616 LY_CHECK_ARG_RET(NULL, exp, local_mod, ctx_scnode, set, LY_EINVAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008617
8618 ctx = local_mod->ctx;
Michal Vasko004d3152020-06-11 19:59:22 +02008619 if ((ctx_scnode_type == LYXP_NODE_ROOT) || (ctx_scnode_type == LYXP_NODE_ROOT_CONFIG)) {
8620 /* we always need some context node because it is used for resolving unqualified names */
8621 real_ctx_scnode = NULL;
8622 } else {
8623 real_ctx_scnode = ctx_scnode;
8624 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008625
8626 /* prepare set for evaluation */
Michal Vasko004d3152020-06-11 19:59:22 +02008627 tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008628 memset(set, 0, sizeof *set);
8629 set->type = LYXP_SET_SCNODE_SET;
Michal Vasko004d3152020-06-11 19:59:22 +02008630 lyxp_set_scnode_insert_node(set, real_ctx_scnode, ctx_scnode_type);
Michal Vasko5c4e5892019-11-14 12:31:38 +01008631 set->val.scnodes[0].in_ctx = -2;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008632 set->ctx = ctx;
8633 set->ctx_scnode = ctx_scnode;
Michal Vasko004d3152020-06-11 19:59:22 +02008634 set->root_type = lyxp_get_root_type(NULL, real_ctx_scnode, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008635 set->local_mod = local_mod;
8636 set->format = format;
8637
8638 /* evaluate */
Michal Vasko004d3152020-06-11 19:59:22 +02008639 return eval_expr_select(exp, &tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008640}