blob: 954cd51244981043862246f81ba620127d1b8a8a [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"
Radek Krejci7931b192020-06-25 17:05:03 +020036#include "parser_data.h"
Michal Vasko004d3152020-06-11 19:59:22 +020037#include "path.h"
Michal Vasko03ff5a72019-09-11 13:49:33 +020038#include "plugins_types.h"
Radek Krejci535ea9f2020-05-29 16:01:05 +020039#include "printer.h"
40#include "printer_data.h"
41#include "tree.h"
42#include "tree_data_internal.h"
43#include "tree_schema_internal.h"
44#include "xml.h"
Michal Vasko03ff5a72019-09-11 13:49:33 +020045
Michal Vasko004d3152020-06-11 19:59:22 +020046static LY_ERR reparse_or_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx);
47static 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 +020048
49/**
50 * @brief Print the type of an XPath \p set.
51 *
52 * @param[in] set Set to use.
53 * @return Set type string.
54 */
55static const char *
56print_set_type(struct lyxp_set *set)
57{
58 switch (set->type) {
Michal Vasko03ff5a72019-09-11 13:49:33 +020059 case LYXP_SET_NODE_SET:
60 return "node set";
61 case LYXP_SET_SCNODE_SET:
62 return "schema node set";
63 case LYXP_SET_BOOLEAN:
64 return "boolean";
65 case LYXP_SET_NUMBER:
66 return "number";
67 case LYXP_SET_STRING:
68 return "string";
69 }
70
71 return NULL;
72}
73
Michal Vasko24cddf82020-06-01 08:17:01 +020074const char *
75lyxp_print_token(enum lyxp_token tok)
Michal Vasko03ff5a72019-09-11 13:49:33 +020076{
77 switch (tok) {
78 case LYXP_TOKEN_PAR1:
79 return "(";
80 case LYXP_TOKEN_PAR2:
81 return ")";
82 case LYXP_TOKEN_BRACK1:
83 return "[";
84 case LYXP_TOKEN_BRACK2:
85 return "]";
86 case LYXP_TOKEN_DOT:
87 return ".";
88 case LYXP_TOKEN_DDOT:
89 return "..";
90 case LYXP_TOKEN_AT:
91 return "@";
92 case LYXP_TOKEN_COMMA:
93 return ",";
94 case LYXP_TOKEN_NAMETEST:
95 return "NameTest";
96 case LYXP_TOKEN_NODETYPE:
97 return "NodeType";
98 case LYXP_TOKEN_FUNCNAME:
99 return "FunctionName";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200100 case LYXP_TOKEN_OPER_LOG:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200101 return "Operator(Logic)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200102 case LYXP_TOKEN_OPER_EQUAL:
103 return "Operator(Equal)";
104 case LYXP_TOKEN_OPER_NEQUAL:
105 return "Operator(Non-equal)";
106 case LYXP_TOKEN_OPER_COMP:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200107 return "Operator(Comparison)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200108 case LYXP_TOKEN_OPER_MATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200109 return "Operator(Math)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200110 case LYXP_TOKEN_OPER_UNI:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200111 return "Operator(Union)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200112 case LYXP_TOKEN_OPER_PATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +0200113 return "Operator(Path)";
Michal Vasko3e48bf32020-06-01 08:39:07 +0200114 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko14676352020-05-29 11:35:55 +0200115 return "Operator(Recursive Path)";
Michal Vasko03ff5a72019-09-11 13:49:33 +0200116 case LYXP_TOKEN_LITERAL:
117 return "Literal";
118 case LYXP_TOKEN_NUMBER:
119 return "Number";
120 default:
121 LOGINT(NULL);
122 return "";
123 }
124}
125
126/**
127 * @brief Print the whole expression \p exp to debug output.
128 *
129 * @param[in] exp Expression to use.
130 */
131static void
132print_expr_struct_debug(struct lyxp_expr *exp)
133{
134 uint16_t i, j;
135 char tmp[128];
136
137 if (!exp || (ly_log_level < LY_LLDBG)) {
138 return;
139 }
140
141 LOGDBG(LY_LDGXPATH, "expression \"%s\":", exp->expr);
142 for (i = 0; i < exp->used; ++i) {
Michal Vasko24cddf82020-06-01 08:17:01 +0200143 sprintf(tmp, "\ttoken %s, in expression \"%.*s\"", lyxp_print_token(exp->tokens[i]), exp->tok_len[i],
Michal Vasko03ff5a72019-09-11 13:49:33 +0200144 &exp->expr[exp->tok_pos[i]]);
145 if (exp->repeat[i]) {
146 sprintf(tmp + strlen(tmp), " (repeat %d", exp->repeat[i][0]);
147 for (j = 1; exp->repeat[i][j]; ++j) {
148 sprintf(tmp + strlen(tmp), ", %d", exp->repeat[i][j]);
149 }
150 strcat(tmp, ")");
151 }
152 LOGDBG(LY_LDGXPATH, tmp);
153 }
154}
155
156#ifndef NDEBUG
157
158/**
159 * @brief Print XPath set content to debug output.
160 *
161 * @param[in] set Set to print.
162 */
163static void
164print_set_debug(struct lyxp_set *set)
165{
166 uint32_t i;
167 char *str;
168 int dynamic;
169 struct lyxp_set_node *item;
170 struct lyxp_set_scnode *sitem;
171
172 if (ly_log_level < LY_LLDBG) {
173 return;
174 }
175
176 switch (set->type) {
177 case LYXP_SET_NODE_SET:
178 LOGDBG(LY_LDGXPATH, "set NODE SET:");
179 for (i = 0; i < set->used; ++i) {
180 item = &set->val.nodes[i];
181
182 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +0100183 case LYXP_NODE_NONE:
184 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): NONE", i + 1, item->pos);
185 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200186 case LYXP_NODE_ROOT:
187 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT", i + 1, item->pos);
188 break;
189 case LYXP_NODE_ROOT_CONFIG:
190 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT CONFIG", i + 1, item->pos);
191 break;
192 case LYXP_NODE_ELEM:
193 if ((item->node->schema->nodetype == LYS_LIST)
194 && (((struct lyd_node_inner *)item->node)->child->schema->nodetype == LYS_LEAF)) {
195 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (1st child val: %s)", i + 1, item->pos,
196 item->node->schema->name,
Michal Vasko5bfd4be2020-06-23 13:26:19 +0200197 (str = (char *)lyd_value2str((struct lyd_node_term *)lyd_node_children(item->node, 0), &dynamic)));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200198 if (dynamic) {
199 free(str);
200 }
201 } else if (((struct lyd_node_inner *)item->node)->schema->nodetype == LYS_LEAFLIST) {
202 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (val: %s)", i + 1, item->pos,
203 item->node->schema->name,
204 (str = (char *)lyd_value2str((struct lyd_node_term *)item->node, &dynamic)));
205 if (dynamic) {
206 free(str);
207 }
208 } else {
209 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s", i + 1, item->pos, item->node->schema->name);
210 }
211 break;
212 case LYXP_NODE_TEXT:
213 if (item->node->schema->nodetype & LYS_ANYDATA) {
214 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): TEXT <%s>", i + 1, item->pos,
215 item->node->schema->nodetype == LYS_ANYXML ? "anyxml" : "anydata");
216 } else {
217 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): TEXT %s", i + 1, item->pos,
218 (str = (char *)lyd_value2str((struct lyd_node_term *)item->node, &dynamic)));
219 if (dynamic) {
220 free(str);
221 }
222 }
223 break;
Michal Vasko9f96a052020-03-10 09:41:45 +0100224 case LYXP_NODE_META:
225 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): META %s = %s", i + 1, item->pos, set->val.meta[i].meta->name,
226 set->val.meta[i].meta->value);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200227 break;
228 }
229 }
230 break;
231
232 case LYXP_SET_SCNODE_SET:
233 LOGDBG(LY_LDGXPATH, "set SCNODE SET:");
234 for (i = 0; i < set->used; ++i) {
235 sitem = &set->val.scnodes[i];
236
237 switch (sitem->type) {
238 case LYXP_NODE_ROOT:
239 LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT", i + 1, sitem->in_ctx);
240 break;
241 case LYXP_NODE_ROOT_CONFIG:
242 LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT CONFIG", i + 1, sitem->in_ctx);
243 break;
244 case LYXP_NODE_ELEM:
245 LOGDBG(LY_LDGXPATH, "\t%d (%u): ELEM %s", i + 1, sitem->in_ctx, sitem->scnode->name);
246 break;
247 default:
248 LOGINT(NULL);
249 break;
250 }
251 }
252 break;
253
Michal Vasko03ff5a72019-09-11 13:49:33 +0200254 case LYXP_SET_BOOLEAN:
255 LOGDBG(LY_LDGXPATH, "set BOOLEAN");
Michal Vasko004d3152020-06-11 19:59:22 +0200256 LOGDBG(LY_LDGXPATH, "\t%s", (set->val.bln ? "true" : "false"));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200257 break;
258
259 case LYXP_SET_STRING:
260 LOGDBG(LY_LDGXPATH, "set STRING");
261 LOGDBG(LY_LDGXPATH, "\t%s", set->val.str);
262 break;
263
264 case LYXP_SET_NUMBER:
265 LOGDBG(LY_LDGXPATH, "set NUMBER");
266
267 if (isnan(set->val.num)) {
268 str = strdup("NaN");
269 } else if ((set->val.num == 0) || (set->val.num == -0.0f)) {
270 str = strdup("0");
271 } else if (isinf(set->val.num) && !signbit(set->val.num)) {
272 str = strdup("Infinity");
273 } else if (isinf(set->val.num) && signbit(set->val.num)) {
274 str = strdup("-Infinity");
275 } else if ((long long)set->val.num == set->val.num) {
276 if (asprintf(&str, "%lld", (long long)set->val.num) == -1) {
277 str = NULL;
278 }
279 } else {
280 if (asprintf(&str, "%03.1Lf", set->val.num) == -1) {
281 str = NULL;
282 }
283 }
284 LY_CHECK_ERR_RET(!str, LOGMEM(NULL), );
285
286 LOGDBG(LY_LDGXPATH, "\t%s", str);
287 free(str);
288 }
289}
290
291#endif
292
293/**
294 * @brief Realloc the string \p str.
295 *
296 * @param[in] ctx libyang context for logging.
297 * @param[in] needed How much free space is required.
298 * @param[in,out] str Pointer to the string to use.
299 * @param[in,out] used Used bytes in \p str.
300 * @param[in,out] size Allocated bytes in \p str.
301 * @return LY_ERR
302 */
303static LY_ERR
Michal Vasko52927e22020-03-16 17:26:14 +0100304cast_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 +0200305{
306 if (*size - *used < needed) {
307 do {
308 if ((UINT16_MAX - *size) < LYXP_STRING_CAST_SIZE_STEP) {
309 LOGERR(ctx, LY_EINVAL, "XPath string length limit (%u) reached.", UINT16_MAX);
310 return LY_EINVAL;
311 }
312 *size += LYXP_STRING_CAST_SIZE_STEP;
313 } while (*size - *used < needed);
314 *str = ly_realloc(*str, *size * sizeof(char));
315 LY_CHECK_ERR_RET(!(*str), LOGMEM(ctx), LY_EMEM);
316 }
317
318 return LY_SUCCESS;
319}
320
321/**
322 * @brief Cast nodes recursively to one string @p str.
323 *
324 * @param[in] node Node to cast.
325 * @param[in] fake_cont Whether to put the data into a "fake" container.
326 * @param[in] root_type Type of the XPath root.
327 * @param[in] indent Current indent.
328 * @param[in,out] str Resulting string.
329 * @param[in,out] used Used bytes in @p str.
330 * @param[in,out] size Allocated bytes in @p str.
331 * @return LY_ERR
332 */
333static LY_ERR
334cast_string_recursive(const struct lyd_node *node, int fake_cont, enum lyxp_node_type root_type, uint16_t indent, char **str,
335 uint16_t *used, uint16_t *size)
336{
337 char *buf, *line, *ptr;
338 const char *value_str;
339 int dynamic;
340 const struct lyd_node *child;
Michal Vasko60ea6352020-06-29 13:39:39 +0200341 struct lyd_node *tree;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200342 struct lyd_node_any *any;
343 LY_ERR rc;
344
345 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) {
346 return LY_SUCCESS;
347 }
348
349 if (fake_cont) {
350 rc = cast_string_realloc(LYD_NODE_CTX(node), 1, str, used, size);
351 LY_CHECK_RET(rc);
352 strcpy(*str + (*used - 1), "\n");
353 ++(*used);
354
355 ++indent;
356 }
357
358 switch (node->schema->nodetype) {
359 case LYS_CONTAINER:
360 case LYS_LIST:
361 case LYS_RPC:
362 case LYS_NOTIF:
363 rc = cast_string_realloc(LYD_NODE_CTX(node), 1, str, used, size);
364 LY_CHECK_RET(rc);
365 strcpy(*str + (*used - 1), "\n");
366 ++(*used);
367
Michal Vasko5bfd4be2020-06-23 13:26:19 +0200368 for (child = lyd_node_children(node, 0); child; child = child->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200369 rc = cast_string_recursive(child, 0, root_type, indent + 1, str, used, size);
370 LY_CHECK_RET(rc);
371 }
372
373 break;
374
375 case LYS_LEAF:
376 case LYS_LEAFLIST:
377 value_str = lyd_value2str(((struct lyd_node_term *)node), &dynamic);
378
379 /* print indent */
380 rc = cast_string_realloc(LYD_NODE_CTX(node), indent * 2 + strlen(value_str) + 1, str, used, size);
381 if (rc != LY_SUCCESS) {
382 if (dynamic) {
383 free((char *)value_str);
384 }
385 return rc;
386 }
387 memset(*str + (*used - 1), ' ', indent * 2);
388 *used += indent * 2;
389
390 /* print value */
391 if (*used == 1) {
392 sprintf(*str + (*used - 1), "%s", value_str);
393 *used += strlen(value_str);
394 } else {
395 sprintf(*str + (*used - 1), "%s\n", value_str);
396 *used += strlen(value_str) + 1;
397 }
398 if (dynamic) {
399 free((char *)value_str);
400 }
401
402 break;
403
404 case LYS_ANYXML:
405 case LYS_ANYDATA:
406 any = (struct lyd_node_any *)node;
407 if (!(void *)any->value.tree) {
408 /* no content */
409 buf = strdup("");
410 LY_CHECK_ERR_RET(!buf, LOGMEM(LYD_NODE_CTX(node)), LY_EMEM);
411 } else {
Radek Krejci241f6b52020-05-21 18:13:49 +0200412 struct ly_out *out;
Radek Krejcia5bba312020-01-09 15:41:20 +0100413
Michal Vasko60ea6352020-06-29 13:39:39 +0200414 if (any->value_type == LYD_ANYDATA_LYB) {
415 /* try to parse it into a data tree */
Radek Krejci7931b192020-06-25 17:05:03 +0200416 if (lyd_parse_data_mem((struct ly_ctx *)LYD_NODE_CTX(node), any->value.mem, LYD_LYB, LYD_PARSE_ONLY | LYD_PARSE_STRICT, 0, &tree) == LY_SUCCESS) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200417 /* successfully parsed */
418 free(any->value.mem);
419 any->value.tree = tree;
420 any->value_type = LYD_ANYDATA_DATATREE;
421 }
Radek Krejci7931b192020-06-25 17:05:03 +0200422 /* error is covered by the following switch where LYD_ANYDATA_LYB causes failure */
Michal Vasko60ea6352020-06-29 13:39:39 +0200423 }
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 Krejci7931b192020-06-25 17:05:03 +0200434 rc = lyd_print(out, any->value.tree, LYD_XML, LYD_PRINT_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;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003000 LY_ARRAY_COUNT_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;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003042 LY_ARRAY_COUNT_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;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003081 LY_ARRAY_COUNT_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;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003115 LY_ARRAY_COUNT_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;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003322 LY_ARRAY_COUNT_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 */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003657 target = p[LY_ARRAY_COUNT(p) - 1].node;
Michal Vasko004d3152020-06-11 19:59:22 +02003658 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;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003719 LY_ARRAY_COUNT_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;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003802 LY_ARRAY_COUNT_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);
Radek Krejcib1247842020-07-02 16:22:38 +02007002 } else {
7003 tmp = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02007004 }
7005
7006 /* additional context check */
7007 if (tmp && (set->root_type == LYXP_NODE_ROOT_CONFIG) && (tmp->flags & LYS_CONFIG_R)) {
7008 tmp = NULL;
7009 }
7010
7011 if (tmp) {
7012 if (scnode) {
7013 /* we found a schema node with the same name but at different level, give up, too complicated */
7014 scnode = NULL;
7015 break;
7016 } else {
7017 /* remember the found schema node and continue to make sure it can be used */
7018 scnode = tmp;
7019 }
Michal Vaskod3678892020-05-21 10:06:58 +02007020 }
7021 }
7022
Michal Vasko004d3152020-06-11 19:59:22 +02007023 if (scnode && (scnode->nodetype & (LYS_LIST | LYS_LEAFLIST))) {
7024 /* try to create the predicates */
7025 if (eval_name_test_try_compile_predicates(exp, tok_idx, scnode, set->format, &predicates, &pred_type)) {
7026 /* hashes cannot be used */
Michal Vaskod3678892020-05-21 10:06:58 +02007027 scnode = NULL;
7028 }
7029 }
7030 }
7031
Michal Vasko004d3152020-06-11 19:59:22 +02007032 if (!scnode && moveto_mod) {
7033 /* we are not able to match based on a schema node and not all the modules match,
7034 * use dictionary for efficient comparison */
7035 ncname_dict = lydict_insert(set->ctx, ncname, ncname_len);
Michal Vaskod3678892020-05-21 10:06:58 +02007036 }
7037
7038moveto:
7039 /* move to the attribute(s), data node(s), or schema node(s) */
7040 if (attr_axis) {
7041 if (set && (options & LYXP_SCNODE_ALL)) {
7042 set_scnode_clear_ctx(set);
7043 } else {
7044 if (all_desc) {
Michal Vasko004d3152020-06-11 19:59:22 +02007045 rc = moveto_attr_alldesc(set, moveto_mod, ncname_dict);
Michal Vaskod3678892020-05-21 10:06:58 +02007046 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007047 rc = moveto_attr(set, moveto_mod, ncname_dict);
Michal Vaskod3678892020-05-21 10:06:58 +02007048 }
7049 LY_CHECK_GOTO(rc, cleanup);
7050 }
7051 } else {
7052 if (set && (options & LYXP_SCNODE_ALL)) {
7053 if (all_desc) {
Michal Vasko004d3152020-06-11 19:59:22 +02007054 rc = moveto_scnode_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007055 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007056 rc = moveto_scnode(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007057 }
7058 LY_CHECK_GOTO(rc, cleanup);
7059
7060 for (i = set->used - 1; i > -1; --i) {
7061 if (set->val.scnodes[i].in_ctx > 0) {
7062 break;
7063 }
7064 }
7065 if (i == -1) {
7066 path = lysc_path(set->ctx_scnode, LYSC_PATH_LOG, NULL, 0);
7067 LOGWRN(set->ctx, "Schema node \"%.*s\" not found (%.*s) with context node \"%s\".",
Michal Vasko004d3152020-06-11 19:59:22 +02007068 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]],
7069 exp->tok_pos[*tok_idx] + exp->tok_len[*tok_idx], exp->expr, path);
Michal Vaskod3678892020-05-21 10:06:58 +02007070 free(path);
7071 }
7072 } else {
7073 if (all_desc) {
Michal Vasko004d3152020-06-11 19:59:22 +02007074 rc = moveto_node_alldesc(set, moveto_mod, ncname_dict);
Michal Vaskod3678892020-05-21 10:06:58 +02007075 } else {
7076 if (scnode) {
7077 /* we can find the nodes using hashes */
Michal Vasko004d3152020-06-11 19:59:22 +02007078 rc = moveto_node_hash(set, scnode, predicates);
Michal Vaskod3678892020-05-21 10:06:58 +02007079 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007080 rc = moveto_node(set, moveto_mod, ncname_dict);
Michal Vaskod3678892020-05-21 10:06:58 +02007081 }
7082 }
7083 LY_CHECK_GOTO(rc, cleanup);
7084 }
7085 }
7086
7087 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007088 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7089 rc = eval_predicate(exp, tok_idx, set, options, 1);
Michal Vaskod3678892020-05-21 10:06:58 +02007090 LY_CHECK_RET(rc);
7091 }
7092
7093cleanup:
Michal Vaskodb51a8d2020-05-27 15:22:29 +02007094 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007095 lydict_remove(set->ctx, ncname_dict);
7096 ly_path_predicates_free(set->ctx, pred_type, scnode, predicates);
Michal Vaskodb51a8d2020-05-27 15:22:29 +02007097 }
Michal Vaskod3678892020-05-21 10:06:58 +02007098 return rc;
7099}
7100
7101/**
7102 * @brief Evaluate NodeType and any following Predicates. Logs directly on error.
7103 *
7104 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
7105 * [6] NodeTest ::= NameTest | NodeType '(' ')'
7106 * [8] NodeType ::= 'text' | 'node'
7107 *
7108 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007109 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02007110 * @param[in] attr_axis Whether to search attributes or standard nodes.
7111 * @param[in] all_desc Whether to search all the descendants or children only.
7112 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7113 * @param[in] options XPath options.
7114 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7115 */
7116static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007117eval_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 +02007118 struct lyxp_set *set, int options)
7119{
7120 LY_ERR rc;
7121
7122 /* TODO */
7123 (void)attr_axis;
7124 (void)all_desc;
7125
7126 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007127 assert(exp->tok_len[*tok_idx] == 4);
Michal Vaskod3678892020-05-21 10:06:58 +02007128 if (set->type == LYXP_SET_SCNODE_SET) {
7129 set_scnode_clear_ctx(set);
7130 /* just for the debug message below */
7131 set = NULL;
7132 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007133 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "node", 4)) {
Michal Vaskod3678892020-05-21 10:06:58 +02007134 rc = xpath_node(NULL, 0, set, options);
7135 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007136 assert(!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "text", 4));
Michal Vaskod3678892020-05-21 10:06:58 +02007137 rc = xpath_text(NULL, 0, set, options);
7138 }
7139 LY_CHECK_RET(rc);
7140 }
7141 }
7142 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007143 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7144 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007145
7146 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02007147 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
Michal Vaskod3678892020-05-21 10:06:58 +02007148 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007149 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7150 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007151
7152 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007153 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
Michal Vaskod3678892020-05-21 10:06:58 +02007154 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007155 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7156 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007157
7158 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007159 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7160 rc = eval_predicate(exp, tok_idx, set, options, 1);
Michal Vaskod3678892020-05-21 10:06:58 +02007161 LY_CHECK_RET(rc);
7162 }
7163
7164 return LY_SUCCESS;
7165}
7166
7167/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02007168 * @brief Evaluate RelativeLocationPath. Logs directly on error.
7169 *
7170 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
7171 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
Michal Vaskod3678892020-05-21 10:06:58 +02007172 * [6] NodeTest ::= NameTest | NodeType '(' ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007173 *
7174 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007175 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007176 * @param[in] all_desc Whether to search all the descendants or children only.
7177 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7178 * @param[in] options XPath options.
7179 * @return LY_ERR (YL_EINCOMPLETE on unresolved when)
7180 */
7181static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007182eval_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 +02007183{
7184 int attr_axis;
7185 LY_ERR rc;
7186
7187 goto step;
7188 do {
7189 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02007190 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007191 all_desc = 0;
7192 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007193 assert(exp->tok_len[*tok_idx] == 2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007194 all_desc = 1;
7195 }
7196 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007197 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7198 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007199
7200step:
Michal Vaskod3678892020-05-21 10:06:58 +02007201 /* evaluate abbreviated axis '@'? if any */
Michal Vasko004d3152020-06-11 19:59:22 +02007202 if (exp->tokens[*tok_idx] == LYXP_TOKEN_AT) {
Michal Vaskod3678892020-05-21 10:06:58 +02007203 attr_axis = 1;
7204
7205 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007206 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7207 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007208 } else {
7209 attr_axis = 0;
7210 }
7211
Michal Vasko03ff5a72019-09-11 13:49:33 +02007212 /* Step */
Michal Vasko004d3152020-06-11 19:59:22 +02007213 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007214 case LYXP_TOKEN_DOT:
7215 /* evaluate '.' */
7216 if (set && (options & LYXP_SCNODE_ALL)) {
7217 rc = moveto_scnode_self(set, all_desc, options);
7218 } else {
7219 rc = moveto_self(set, all_desc, options);
7220 }
7221 LY_CHECK_RET(rc);
7222 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007223 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7224 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007225 break;
7226
7227 case LYXP_TOKEN_DDOT:
7228 /* evaluate '..' */
7229 if (set && (options & LYXP_SCNODE_ALL)) {
7230 rc = moveto_scnode_parent(set, all_desc, options);
7231 } else {
7232 rc = moveto_parent(set, all_desc, options);
7233 }
7234 LY_CHECK_RET(rc);
7235 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007236 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7237 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007238 break;
7239
Michal Vasko03ff5a72019-09-11 13:49:33 +02007240 case LYXP_TOKEN_NAMETEST:
Michal Vaskod3678892020-05-21 10:06:58 +02007241 /* evaluate NameTest Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007242 rc = eval_name_test_with_predicate(exp, tok_idx, attr_axis, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007243 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02007244 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007245
Michal Vaskod3678892020-05-21 10:06:58 +02007246 case LYXP_TOKEN_NODETYPE:
7247 /* evaluate NodeType Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007248 rc = eval_node_type_with_predicate(exp, tok_idx, attr_axis, all_desc, set, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007249 LY_CHECK_RET(rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007250 break;
7251
7252 default:
Michal Vasko02a77382019-09-12 11:47:35 +02007253 LOGINT_RET(set ? set->ctx : NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007254 }
Michal Vasko004d3152020-06-11 19:59:22 +02007255 } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007256
7257 return LY_SUCCESS;
7258}
7259
7260/**
7261 * @brief Evaluate AbsoluteLocationPath. Logs directly on error.
7262 *
7263 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
7264 *
7265 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007266 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007267 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7268 * @param[in] options XPath options.
7269 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7270 */
7271static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007272eval_absolute_location_path(struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set, int options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007273{
7274 int all_desc;
7275 LY_ERR rc;
7276
7277 if (set) {
7278 /* no matter what tokens follow, we need to be at the root */
7279 moveto_root(set, options);
7280 }
7281
7282 /* '/' RelativeLocationPath? */
Michal Vasko004d3152020-06-11 19:59:22 +02007283 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007284 /* evaluate '/' - deferred */
7285 all_desc = 0;
7286 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007287 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7288 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007289
Michal Vasko004d3152020-06-11 19:59:22 +02007290 if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007291 return LY_SUCCESS;
7292 }
Michal Vasko004d3152020-06-11 19:59:22 +02007293 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007294 case LYXP_TOKEN_DOT:
7295 case LYXP_TOKEN_DDOT:
7296 case LYXP_TOKEN_AT:
7297 case LYXP_TOKEN_NAMETEST:
7298 case LYXP_TOKEN_NODETYPE:
Michal Vasko004d3152020-06-11 19:59:22 +02007299 rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007300 LY_CHECK_RET(rc);
7301 break;
7302 default:
7303 break;
7304 }
7305
7306 /* '//' RelativeLocationPath */
7307 } else {
7308 /* evaluate '//' - deferred so as not to waste memory by remembering all the nodes */
7309 all_desc = 1;
7310 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007311 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7312 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007313
Michal Vasko004d3152020-06-11 19:59:22 +02007314 rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007315 LY_CHECK_RET(rc);
7316 }
7317
7318 return LY_SUCCESS;
7319}
7320
7321/**
7322 * @brief Evaluate FunctionCall. Logs directly on error.
7323 *
Michal Vaskod3678892020-05-21 10:06:58 +02007324 * [11] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007325 *
7326 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007327 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007328 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7329 * @param[in] options XPath options.
7330 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7331 */
7332static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007333eval_function_call(struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set, int options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007334{
7335 LY_ERR rc;
7336 LY_ERR (*xpath_func)(struct lyxp_set **, uint16_t, struct lyxp_set *, int) = NULL;
Michal Vasko0cbf54f2019-12-16 10:01:06 +01007337 uint16_t arg_count = 0, i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007338 struct lyxp_set **args = NULL, **args_aux;
7339
7340 if (set) {
7341 /* FunctionName */
Michal Vasko004d3152020-06-11 19:59:22 +02007342 switch (exp->tok_len[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007343 case 3:
Michal Vasko004d3152020-06-11 19:59:22 +02007344 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007345 xpath_func = &xpath_not;
Michal Vasko004d3152020-06-11 19:59:22 +02007346 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007347 xpath_func = &xpath_sum;
7348 }
7349 break;
7350 case 4:
Michal Vasko004d3152020-06-11 19:59:22 +02007351 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007352 xpath_func = &xpath_lang;
Michal Vasko004d3152020-06-11 19:59:22 +02007353 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007354 xpath_func = &xpath_last;
Michal Vasko004d3152020-06-11 19:59:22 +02007355 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007356 xpath_func = &xpath_name;
Michal Vasko004d3152020-06-11 19:59:22 +02007357 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007358 xpath_func = &xpath_true;
7359 }
7360 break;
7361 case 5:
Michal Vasko004d3152020-06-11 19:59:22 +02007362 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007363 xpath_func = &xpath_count;
Michal Vasko004d3152020-06-11 19:59:22 +02007364 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007365 xpath_func = &xpath_false;
Michal Vasko004d3152020-06-11 19:59:22 +02007366 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007367 xpath_func = &xpath_floor;
Michal Vasko004d3152020-06-11 19:59:22 +02007368 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007369 xpath_func = &xpath_round;
Michal Vasko004d3152020-06-11 19:59:22 +02007370 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007371 xpath_func = &xpath_deref;
7372 }
7373 break;
7374 case 6:
Michal Vasko004d3152020-06-11 19:59:22 +02007375 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007376 xpath_func = &xpath_concat;
Michal Vasko004d3152020-06-11 19:59:22 +02007377 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007378 xpath_func = &xpath_number;
Michal Vasko004d3152020-06-11 19:59:22 +02007379 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007380 xpath_func = &xpath_string;
7381 }
7382 break;
7383 case 7:
Michal Vasko004d3152020-06-11 19:59:22 +02007384 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007385 xpath_func = &xpath_boolean;
Michal Vasko004d3152020-06-11 19:59:22 +02007386 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007387 xpath_func = &xpath_ceiling;
Michal Vasko004d3152020-06-11 19:59:22 +02007388 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007389 xpath_func = &xpath_current;
7390 }
7391 break;
7392 case 8:
Michal Vasko004d3152020-06-11 19:59:22 +02007393 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007394 xpath_func = &xpath_contains;
Michal Vasko004d3152020-06-11 19:59:22 +02007395 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007396 xpath_func = &xpath_position;
Michal Vasko004d3152020-06-11 19:59:22 +02007397 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007398 xpath_func = &xpath_re_match;
7399 }
7400 break;
7401 case 9:
Michal Vasko004d3152020-06-11 19:59:22 +02007402 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007403 xpath_func = &xpath_substring;
Michal Vasko004d3152020-06-11 19:59:22 +02007404 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007405 xpath_func = &xpath_translate;
7406 }
7407 break;
7408 case 10:
Michal Vasko004d3152020-06-11 19:59:22 +02007409 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007410 xpath_func = &xpath_local_name;
Michal Vasko004d3152020-06-11 19:59:22 +02007411 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007412 xpath_func = &xpath_enum_value;
Michal Vasko004d3152020-06-11 19:59:22 +02007413 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007414 xpath_func = &xpath_bit_is_set;
7415 }
7416 break;
7417 case 11:
Michal Vasko004d3152020-06-11 19:59:22 +02007418 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007419 xpath_func = &xpath_starts_with;
7420 }
7421 break;
7422 case 12:
Michal Vasko004d3152020-06-11 19:59:22 +02007423 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007424 xpath_func = &xpath_derived_from;
7425 }
7426 break;
7427 case 13:
Michal Vasko004d3152020-06-11 19:59:22 +02007428 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007429 xpath_func = &xpath_namespace_uri;
Michal Vasko004d3152020-06-11 19:59:22 +02007430 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007431 xpath_func = &xpath_string_length;
7432 }
7433 break;
7434 case 15:
Michal Vasko004d3152020-06-11 19:59:22 +02007435 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007436 xpath_func = &xpath_normalize_space;
Michal Vasko004d3152020-06-11 19:59:22 +02007437 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007438 xpath_func = &xpath_substring_after;
7439 }
7440 break;
7441 case 16:
Michal Vasko004d3152020-06-11 19:59:22 +02007442 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007443 xpath_func = &xpath_substring_before;
7444 }
7445 break;
7446 case 20:
Michal Vasko004d3152020-06-11 19:59:22 +02007447 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007448 xpath_func = &xpath_derived_from_or_self;
7449 }
7450 break;
7451 }
7452
7453 if (!xpath_func) {
Michal Vasko004d3152020-06-11 19:59:22 +02007454 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 +02007455 return LY_EVALID;
7456 }
7457 }
7458
7459 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007460 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7461 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007462
7463 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02007464 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007465 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007466 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7467 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007468
7469 /* ( Expr ( ',' Expr )* )? */
Michal Vasko004d3152020-06-11 19:59:22 +02007470 if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007471 if (set) {
7472 args = malloc(sizeof *args);
7473 LY_CHECK_ERR_GOTO(!args, LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7474 arg_count = 1;
7475 args[0] = set_copy(set);
7476 if (!args[0]) {
7477 rc = LY_EMEM;
7478 goto cleanup;
7479 }
7480
Michal Vasko004d3152020-06-11 19:59:22 +02007481 rc = eval_expr_select(exp, tok_idx, 0, args[0], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007482 LY_CHECK_GOTO(rc, cleanup);
7483 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007484 rc = eval_expr_select(exp, tok_idx, 0, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007485 LY_CHECK_GOTO(rc, cleanup);
7486 }
7487 }
Michal Vasko004d3152020-06-11 19:59:22 +02007488 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007489 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007490 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7491 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007492
7493 if (set) {
7494 ++arg_count;
7495 args_aux = realloc(args, arg_count * sizeof *args);
7496 LY_CHECK_ERR_GOTO(!args_aux, arg_count--; LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7497 args = args_aux;
7498 args[arg_count - 1] = set_copy(set);
7499 if (!args[arg_count - 1]) {
7500 rc = LY_EMEM;
7501 goto cleanup;
7502 }
7503
Michal Vasko004d3152020-06-11 19:59:22 +02007504 rc = eval_expr_select(exp, tok_idx, 0, args[arg_count - 1], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007505 LY_CHECK_GOTO(rc, cleanup);
7506 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007507 rc = eval_expr_select(exp, tok_idx, 0, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007508 LY_CHECK_GOTO(rc, cleanup);
7509 }
7510 }
7511
7512 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007513 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007514 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007515 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7516 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007517
7518 if (set) {
7519 /* evaluate function */
7520 rc = xpath_func(args, arg_count, set, options);
7521
7522 if (options & LYXP_SCNODE_ALL) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007523 /* merge all nodes from arg evaluations */
7524 for (i = 0; i < arg_count; ++i) {
7525 set_scnode_clear_ctx(args[i]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007526 lyxp_set_scnode_merge(set, args[i]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007527 }
7528 }
7529 } else {
7530 rc = LY_SUCCESS;
7531 }
7532
7533cleanup:
7534 for (i = 0; i < arg_count; ++i) {
7535 lyxp_set_free(args[i]);
7536 }
7537 free(args);
7538
7539 return rc;
7540}
7541
7542/**
7543 * @brief Evaluate Number. Logs directly on error.
7544 *
7545 * @param[in] ctx Context for errors.
7546 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007547 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007548 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7549 * @return LY_ERR
7550 */
7551static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007552eval_number(struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007553{
7554 long double num;
7555 char *endptr;
7556
7557 if (set) {
7558 errno = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02007559 num = strtold(&exp->expr[exp->tok_pos[*tok_idx]], &endptr);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007560 if (errno) {
Michal Vasko004d3152020-06-11 19:59:22 +02007561 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 +02007562 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 +02007563 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]], strerror(errno));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007564 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02007565 } else if (endptr - &exp->expr[exp->tok_pos[*tok_idx]] != exp->tok_len[*tok_idx]) {
7566 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 +02007567 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 +02007568 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007569 return LY_EVALID;
7570 }
7571
7572 set_fill_number(set, num);
7573 }
7574
7575 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007576 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7577 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007578 return LY_SUCCESS;
7579}
7580
7581/**
7582 * @brief Evaluate PathExpr. Logs directly on error.
7583 *
Michal Vaskod3678892020-05-21 10:06:58 +02007584 * [12] PathExpr ::= LocationPath | PrimaryExpr Predicate*
Michal Vasko03ff5a72019-09-11 13:49:33 +02007585 * | PrimaryExpr Predicate* '/' RelativeLocationPath
7586 * | PrimaryExpr Predicate* '//' RelativeLocationPath
7587 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
Michal Vaskod3678892020-05-21 10:06:58 +02007588 * [10] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall
Michal Vasko03ff5a72019-09-11 13:49:33 +02007589 *
7590 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007591 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007592 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7593 * @param[in] options XPath options.
7594 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7595 */
7596static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007597eval_path_expr(struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set, int options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007598{
7599 int all_desc, parent_pos_pred;
7600 LY_ERR rc;
7601
Michal Vasko004d3152020-06-11 19:59:22 +02007602 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007603 case LYXP_TOKEN_PAR1:
7604 /* '(' Expr ')' */
7605
7606 /* '(' */
7607 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007608 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7609 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007610
7611 /* Expr */
Michal Vasko004d3152020-06-11 19:59:22 +02007612 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007613 LY_CHECK_RET(rc);
7614
7615 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007616 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007617 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007618 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7619 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007620
7621 parent_pos_pred = 0;
7622 goto predicate;
7623
7624 case LYXP_TOKEN_DOT:
7625 case LYXP_TOKEN_DDOT:
7626 case LYXP_TOKEN_AT:
7627 case LYXP_TOKEN_NAMETEST:
7628 case LYXP_TOKEN_NODETYPE:
7629 /* RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007630 rc = eval_relative_location_path(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007631 LY_CHECK_RET(rc);
7632 break;
7633
7634 case LYXP_TOKEN_FUNCNAME:
7635 /* FunctionCall */
7636 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007637 rc = eval_function_call(exp, tok_idx, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007638 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007639 rc = eval_function_call(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007640 }
7641 LY_CHECK_RET(rc);
7642
7643 parent_pos_pred = 1;
7644 goto predicate;
7645
Michal Vasko3e48bf32020-06-01 08:39:07 +02007646 case LYXP_TOKEN_OPER_PATH:
7647 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02007648 /* AbsoluteLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007649 rc = eval_absolute_location_path(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007650 LY_CHECK_RET(rc);
7651 break;
7652
7653 case LYXP_TOKEN_LITERAL:
7654 /* Literal */
7655 if (!set || (options & LYXP_SCNODE_ALL)) {
7656 if (set) {
7657 set_scnode_clear_ctx(set);
7658 }
Michal Vasko004d3152020-06-11 19:59:22 +02007659 eval_literal(exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007660 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007661 eval_literal(exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007662 }
7663
7664 parent_pos_pred = 1;
7665 goto predicate;
7666
7667 case LYXP_TOKEN_NUMBER:
7668 /* Number */
7669 if (!set || (options & LYXP_SCNODE_ALL)) {
7670 if (set) {
7671 set_scnode_clear_ctx(set);
7672 }
Michal Vasko004d3152020-06-11 19:59:22 +02007673 rc = eval_number(NULL, exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007674 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007675 rc = eval_number(set->ctx, exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007676 }
7677 LY_CHECK_RET(rc);
7678
7679 parent_pos_pred = 1;
7680 goto predicate;
7681
7682 default:
Michal Vasko004d3152020-06-11 19:59:22 +02007683 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INTOK, lyxp_print_token(exp->tokens[*tok_idx]),
7684 &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007685 return LY_EVALID;
7686 }
7687
7688 return LY_SUCCESS;
7689
7690predicate:
7691 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007692 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7693 rc = eval_predicate(exp, tok_idx, set, options, parent_pos_pred);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007694 LY_CHECK_RET(rc);
7695 }
7696
7697 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007698 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007699
7700 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02007701 if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007702 all_desc = 0;
7703 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007704 all_desc = 1;
7705 }
7706
7707 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007708 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7709 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007710
Michal Vasko004d3152020-06-11 19:59:22 +02007711 rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007712 LY_CHECK_RET(rc);
7713 }
7714
7715 return LY_SUCCESS;
7716}
7717
7718/**
7719 * @brief Evaluate UnionExpr. Logs directly on error.
7720 *
Michal Vaskod3678892020-05-21 10:06:58 +02007721 * [20] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007722 *
7723 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007724 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007725 * @param[in] repeat How many times this expression is repeated.
7726 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7727 * @param[in] options XPath options.
7728 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7729 */
7730static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007731eval_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 +02007732{
7733 LY_ERR rc = LY_SUCCESS;
7734 struct lyxp_set orig_set, set2;
7735 uint16_t i;
7736
7737 assert(repeat);
7738
7739 set_init(&orig_set, set);
7740 set_init(&set2, set);
7741
7742 set_fill_set(&orig_set, set);
7743
Michal Vasko004d3152020-06-11 19:59:22 +02007744 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007745 LY_CHECK_GOTO(rc, cleanup);
7746
7747 /* ('|' PathExpr)* */
7748 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007749 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_UNI);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007750 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007751 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7752 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007753
7754 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007755 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007756 LY_CHECK_GOTO(rc, cleanup);
7757 continue;
7758 }
7759
7760 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02007761 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007762 LY_CHECK_GOTO(rc, cleanup);
7763
7764 /* eval */
7765 if (options & LYXP_SCNODE_ALL) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01007766 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007767 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007768 rc = moveto_union(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007769 LY_CHECK_GOTO(rc, cleanup);
7770 }
7771 }
7772
7773cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007774 lyxp_set_free_content(&orig_set);
7775 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007776 return rc;
7777}
7778
7779/**
7780 * @brief Evaluate UnaryExpr. Logs directly on error.
7781 *
Michal Vaskod3678892020-05-21 10:06:58 +02007782 * [19] UnaryExpr ::= UnionExpr | '-' UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007783 *
7784 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007785 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007786 * @param[in] repeat How many times this expression is repeated.
7787 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7788 * @param[in] options XPath options.
7789 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7790 */
7791static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007792eval_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 +02007793{
7794 LY_ERR rc;
7795 uint16_t this_op, i;
7796
7797 assert(repeat);
7798
7799 /* ('-')+ */
Michal Vasko004d3152020-06-11 19:59:22 +02007800 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007801 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007802 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 +02007803
7804 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007805 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7806 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007807 }
7808
Michal Vasko004d3152020-06-11 19:59:22 +02007809 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNARY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007810 LY_CHECK_RET(rc);
7811
7812 if (set && (repeat % 2)) {
7813 if (options & LYXP_SCNODE_ALL) {
7814 warn_operands(set->ctx, set, NULL, 1, exp->expr, exp->tok_pos[this_op]);
7815 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007816 rc = moveto_op_math(set, NULL, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007817 LY_CHECK_RET(rc);
7818 }
7819 }
7820
7821 return LY_SUCCESS;
7822}
7823
7824/**
7825 * @brief Evaluate MultiplicativeExpr. Logs directly on error.
7826 *
Michal Vaskod3678892020-05-21 10:06:58 +02007827 * [18] MultiplicativeExpr ::= UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007828 * | MultiplicativeExpr '*' UnaryExpr
7829 * | MultiplicativeExpr 'div' UnaryExpr
7830 * | MultiplicativeExpr 'mod' UnaryExpr
7831 *
7832 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007833 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007834 * @param[in] repeat How many times this expression is repeated.
7835 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7836 * @param[in] options XPath options.
7837 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7838 */
7839static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007840eval_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 +02007841{
7842 LY_ERR rc;
7843 uint16_t this_op;
7844 struct lyxp_set orig_set, set2;
7845 uint16_t i;
7846
7847 assert(repeat);
7848
7849 set_init(&orig_set, set);
7850 set_init(&set2, set);
7851
7852 set_fill_set(&orig_set, set);
7853
Michal Vasko004d3152020-06-11 19:59:22 +02007854 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007855 LY_CHECK_GOTO(rc, cleanup);
7856
7857 /* ('*' / 'div' / 'mod' UnaryExpr)* */
7858 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007859 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007860
Michal Vasko004d3152020-06-11 19:59:22 +02007861 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007862 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007863 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7864 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007865
7866 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007867 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007868 LY_CHECK_GOTO(rc, cleanup);
7869 continue;
7870 }
7871
7872 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02007873 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007874 LY_CHECK_GOTO(rc, cleanup);
7875
7876 /* eval */
7877 if (options & LYXP_SCNODE_ALL) {
7878 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007879 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007880 set_scnode_clear_ctx(set);
7881 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007882 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007883 LY_CHECK_GOTO(rc, cleanup);
7884 }
7885 }
7886
7887cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007888 lyxp_set_free_content(&orig_set);
7889 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007890 return rc;
7891}
7892
7893/**
7894 * @brief Evaluate AdditiveExpr. Logs directly on error.
7895 *
Michal Vaskod3678892020-05-21 10:06:58 +02007896 * [17] AdditiveExpr ::= MultiplicativeExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007897 * | AdditiveExpr '+' MultiplicativeExpr
7898 * | AdditiveExpr '-' MultiplicativeExpr
7899 *
7900 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007901 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007902 * @param[in] repeat How many times this expression is repeated.
7903 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7904 * @param[in] options XPath options.
7905 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7906 */
7907static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007908eval_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 +02007909{
7910 LY_ERR rc;
7911 uint16_t this_op;
7912 struct lyxp_set orig_set, set2;
7913 uint16_t i;
7914
7915 assert(repeat);
7916
7917 set_init(&orig_set, set);
7918 set_init(&set2, set);
7919
7920 set_fill_set(&orig_set, set);
7921
Michal Vasko004d3152020-06-11 19:59:22 +02007922 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007923 LY_CHECK_GOTO(rc, cleanup);
7924
7925 /* ('+' / '-' MultiplicativeExpr)* */
7926 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007927 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007928
Michal Vasko004d3152020-06-11 19:59:22 +02007929 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007930 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007931 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7932 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007933
7934 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007935 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007936 LY_CHECK_GOTO(rc, cleanup);
7937 continue;
7938 }
7939
7940 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02007941 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007942 LY_CHECK_GOTO(rc, cleanup);
7943
7944 /* eval */
7945 if (options & LYXP_SCNODE_ALL) {
7946 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007947 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007948 set_scnode_clear_ctx(set);
7949 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007950 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007951 LY_CHECK_GOTO(rc, cleanup);
7952 }
7953 }
7954
7955cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007956 lyxp_set_free_content(&orig_set);
7957 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007958 return rc;
7959}
7960
7961/**
7962 * @brief Evaluate RelationalExpr. Logs directly on error.
7963 *
Michal Vaskod3678892020-05-21 10:06:58 +02007964 * [16] RelationalExpr ::= AdditiveExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007965 * | RelationalExpr '<' AdditiveExpr
7966 * | RelationalExpr '>' AdditiveExpr
7967 * | RelationalExpr '<=' AdditiveExpr
7968 * | RelationalExpr '>=' AdditiveExpr
7969 *
7970 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007971 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007972 * @param[in] repeat How many times this expression is repeated.
7973 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7974 * @param[in] options XPath options.
7975 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7976 */
7977static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007978eval_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 +02007979{
7980 LY_ERR rc;
7981 uint16_t this_op;
7982 struct lyxp_set orig_set, set2;
7983 uint16_t i;
7984
7985 assert(repeat);
7986
7987 set_init(&orig_set, set);
7988 set_init(&set2, set);
7989
7990 set_fill_set(&orig_set, set);
7991
Michal Vasko004d3152020-06-11 19:59:22 +02007992 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007993 LY_CHECK_GOTO(rc, cleanup);
7994
7995 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
7996 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007997 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007998
Michal Vasko004d3152020-06-11 19:59:22 +02007999 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_COMP);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008000 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02008001 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
8002 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008003
8004 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02008005 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008006 LY_CHECK_GOTO(rc, cleanup);
8007 continue;
8008 }
8009
8010 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008011 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008012 LY_CHECK_GOTO(rc, cleanup);
8013
8014 /* eval */
8015 if (options & LYXP_SCNODE_ALL) {
8016 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008017 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008018 set_scnode_clear_ctx(set);
8019 } else {
8020 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options);
8021 LY_CHECK_GOTO(rc, cleanup);
8022 }
8023 }
8024
8025cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008026 lyxp_set_free_content(&orig_set);
8027 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008028 return rc;
8029}
8030
8031/**
8032 * @brief Evaluate EqualityExpr. Logs directly on error.
8033 *
Michal Vaskod3678892020-05-21 10:06:58 +02008034 * [15] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008035 * | EqualityExpr '!=' RelationalExpr
8036 *
8037 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008038 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008039 * @param[in] repeat How many times this expression is repeated.
8040 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8041 * @param[in] options XPath options.
8042 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8043 */
8044static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02008045eval_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 +02008046{
8047 LY_ERR rc;
8048 uint16_t this_op;
8049 struct lyxp_set orig_set, set2;
8050 uint16_t i;
8051
8052 assert(repeat);
8053
8054 set_init(&orig_set, set);
8055 set_init(&set2, set);
8056
8057 set_fill_set(&orig_set, set);
8058
Michal Vasko004d3152020-06-11 19:59:22 +02008059 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008060 LY_CHECK_GOTO(rc, cleanup);
8061
8062 /* ('=' / '!=' RelationalExpr)* */
8063 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008064 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008065
Michal Vasko004d3152020-06-11 19:59:22 +02008066 assert((exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_EQUAL) || (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_NEQUAL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008067 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02008068 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
8069 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008070
8071 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02008072 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008073 LY_CHECK_GOTO(rc, cleanup);
8074 continue;
8075 }
8076
8077 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008078 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008079 LY_CHECK_GOTO(rc, cleanup);
8080
8081 /* eval */
8082 if (options & LYXP_SCNODE_ALL) {
8083 warn_operands(set->ctx, set, &set2, 0, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vasko004d3152020-06-11 19:59:22 +02008084 warn_equality_value(exp, set, *tok_idx - 1, this_op - 1, *tok_idx - 1);
8085 warn_equality_value(exp, &set2, this_op - 1, this_op - 1, *tok_idx - 1);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008086 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008087 set_scnode_clear_ctx(set);
8088 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008089 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options);
8090 LY_CHECK_GOTO(rc, cleanup);
8091 }
8092 }
8093
8094cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008095 lyxp_set_free_content(&orig_set);
8096 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008097 return rc;
8098}
8099
8100/**
8101 * @brief Evaluate AndExpr. Logs directly on error.
8102 *
Michal Vaskod3678892020-05-21 10:06:58 +02008103 * [14] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008104 *
8105 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008106 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008107 * @param[in] repeat How many times this expression is repeated.
8108 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8109 * @param[in] options XPath options.
8110 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8111 */
8112static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02008113eval_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 +02008114{
8115 LY_ERR rc;
8116 struct lyxp_set orig_set, set2;
8117 uint16_t i;
8118
8119 assert(repeat);
8120
8121 set_init(&orig_set, set);
8122 set_init(&set2, set);
8123
8124 set_fill_set(&orig_set, set);
8125
Michal Vasko004d3152020-06-11 19:59:22 +02008126 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008127 LY_CHECK_GOTO(rc, cleanup);
8128
8129 /* cast to boolean, we know that will be the final result */
8130 if (set && (options & LYXP_SCNODE_ALL)) {
8131 set_scnode_clear_ctx(set);
8132 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008133 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008134 }
8135
8136 /* ('and' EqualityExpr)* */
8137 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008138 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
8139 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (!set || !set->val.bln ? "skipped" : "parsed"),
8140 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
8141 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008142
8143 /* lazy evaluation */
Michal Vasko004d3152020-06-11 19:59:22 +02008144 if (!set || ((set->type == LYXP_SET_BOOLEAN) && !set->val.bln)) {
8145 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008146 LY_CHECK_GOTO(rc, cleanup);
8147 continue;
8148 }
8149
8150 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008151 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008152 LY_CHECK_GOTO(rc, cleanup);
8153
8154 /* eval - just get boolean value actually */
8155 if (set->type == LYXP_SET_SCNODE_SET) {
8156 set_scnode_clear_ctx(&set2);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008157 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008158 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008159 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008160 set_fill_set(set, &set2);
8161 }
8162 }
8163
8164cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008165 lyxp_set_free_content(&orig_set);
8166 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008167 return rc;
8168}
8169
8170/**
8171 * @brief Evaluate OrExpr. Logs directly on error.
8172 *
Michal Vaskod3678892020-05-21 10:06:58 +02008173 * [13] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008174 *
8175 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008176 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008177 * @param[in] repeat How many times this expression is repeated.
8178 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8179 * @param[in] options XPath options.
8180 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8181 */
8182static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02008183eval_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 +02008184{
8185 LY_ERR rc;
8186 struct lyxp_set orig_set, set2;
8187 uint16_t i;
8188
8189 assert(repeat);
8190
8191 set_init(&orig_set, set);
8192 set_init(&set2, set);
8193
8194 set_fill_set(&orig_set, set);
8195
Michal Vasko004d3152020-06-11 19:59:22 +02008196 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008197 LY_CHECK_GOTO(rc, cleanup);
8198
8199 /* cast to boolean, we know that will be the final result */
8200 if (set && (options & LYXP_SCNODE_ALL)) {
8201 set_scnode_clear_ctx(set);
8202 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008203 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008204 }
8205
8206 /* ('or' AndExpr)* */
8207 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008208 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
8209 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (!set || set->val.bln ? "skipped" : "parsed"),
8210 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
8211 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008212
8213 /* lazy evaluation */
Michal Vasko004d3152020-06-11 19:59:22 +02008214 if (!set || ((set->type == LYXP_SET_BOOLEAN) && set->val.bln)) {
8215 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008216 LY_CHECK_GOTO(rc, cleanup);
8217 continue;
8218 }
8219
8220 set_fill_set(&set2, &orig_set);
8221 /* expr_type cound have been LYXP_EXPR_NONE in all these later calls (except for the first one),
8222 * but it does not matter */
Michal Vasko004d3152020-06-11 19:59:22 +02008223 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008224 LY_CHECK_GOTO(rc, cleanup);
8225
8226 /* eval - just get boolean value actually */
8227 if (set->type == LYXP_SET_SCNODE_SET) {
8228 set_scnode_clear_ctx(&set2);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008229 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008230 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008231 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008232 set_fill_set(set, &set2);
8233 }
8234 }
8235
8236cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008237 lyxp_set_free_content(&orig_set);
8238 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008239 return rc;
8240}
8241
8242/**
Michal Vasko004d3152020-06-11 19:59:22 +02008243 * @brief Decide what expression is at the pointer @p tok_idx and evaluate it accordingly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008244 *
8245 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008246 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008247 * @param[in] etype Expression type to evaluate.
8248 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8249 * @param[in] options XPath options.
8250 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8251 */
8252static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02008253eval_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 +02008254{
8255 uint16_t i, count;
8256 enum lyxp_expr_type next_etype;
8257 LY_ERR rc;
8258
8259 /* process operator repeats */
Michal Vasko004d3152020-06-11 19:59:22 +02008260 if (!exp->repeat[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008261 next_etype = LYXP_EXPR_NONE;
8262 } else {
8263 /* find etype repeat */
Michal Vasko004d3152020-06-11 19:59:22 +02008264 for (i = 0; exp->repeat[*tok_idx][i] > etype; ++i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008265
8266 /* select one-priority lower because etype expression called us */
8267 if (i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008268 next_etype = exp->repeat[*tok_idx][i - 1];
Michal Vasko03ff5a72019-09-11 13:49:33 +02008269 /* count repeats for that expression */
Michal Vasko004d3152020-06-11 19:59:22 +02008270 for (count = 0; i && exp->repeat[*tok_idx][i - 1] == next_etype; ++count, --i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008271 } else {
8272 next_etype = LYXP_EXPR_NONE;
8273 }
8274 }
8275
8276 /* decide what expression are we parsing based on the repeat */
8277 switch (next_etype) {
8278 case LYXP_EXPR_OR:
Michal Vasko004d3152020-06-11 19:59:22 +02008279 rc = eval_or_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008280 break;
8281 case LYXP_EXPR_AND:
Michal Vasko004d3152020-06-11 19:59:22 +02008282 rc = eval_and_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008283 break;
8284 case LYXP_EXPR_EQUALITY:
Michal Vasko004d3152020-06-11 19:59:22 +02008285 rc = eval_equality_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008286 break;
8287 case LYXP_EXPR_RELATIONAL:
Michal Vasko004d3152020-06-11 19:59:22 +02008288 rc = eval_relational_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008289 break;
8290 case LYXP_EXPR_ADDITIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02008291 rc = eval_additive_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008292 break;
8293 case LYXP_EXPR_MULTIPLICATIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02008294 rc = eval_multiplicative_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008295 break;
8296 case LYXP_EXPR_UNARY:
Michal Vasko004d3152020-06-11 19:59:22 +02008297 rc = eval_unary_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008298 break;
8299 case LYXP_EXPR_UNION:
Michal Vasko004d3152020-06-11 19:59:22 +02008300 rc = eval_union_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008301 break;
8302 case LYXP_EXPR_NONE:
Michal Vasko004d3152020-06-11 19:59:22 +02008303 rc = eval_path_expr(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008304 break;
8305 default:
8306 LOGINT_RET(set->ctx);
8307 }
8308
8309 return rc;
8310}
8311
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008312/**
8313 * @brief Get root type.
8314 *
8315 * @param[in] ctx_node Context node.
8316 * @param[in] ctx_scnode Schema context node.
8317 * @param[in] options XPath options.
8318 * @return Root type.
8319 */
8320static enum lyxp_node_type
8321lyxp_get_root_type(const struct lyd_node *ctx_node, const struct lysc_node *ctx_scnode, int options)
8322{
8323 if (options & LYXP_SCNODE_ALL) {
8324 if (options & LYXP_SCNODE) {
8325 /* general root that can access everything */
8326 return LYXP_NODE_ROOT;
8327 } else if (!ctx_scnode || (ctx_scnode->flags & LYS_CONFIG_W)) {
8328 /* root context node can access only config data (because we said so, it is unspecified) */
8329 return LYXP_NODE_ROOT_CONFIG;
8330 } else {
8331 return LYXP_NODE_ROOT;
8332 }
8333 }
8334
8335 if (!ctx_node || (ctx_node->schema->flags & LYS_CONFIG_W)) {
8336 /* root context node can access only config data (because we said so, it is unspecified) */
8337 return LYXP_NODE_ROOT_CONFIG;
8338 }
8339
8340 return LYXP_NODE_ROOT;
8341}
8342
Michal Vasko03ff5a72019-09-11 13:49:33 +02008343LY_ERR
Michal Vaskoecd62de2019-11-13 12:35:11 +01008344lyxp_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 +01008345 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 +02008346{
Michal Vasko004d3152020-06-11 19:59:22 +02008347 uint16_t tok_idx = 0;
8348 const struct lyd_node *real_ctx_node;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008349 LY_ERR rc;
8350
Michal Vasko004d3152020-06-11 19:59:22 +02008351 LY_CHECK_ARG_RET(NULL, exp, local_mod, ctx_node, set, LY_EINVAL);
8352
8353 if ((ctx_node_type == LYXP_NODE_ROOT) || (ctx_node_type == LYXP_NODE_ROOT_CONFIG)) {
8354 /* we always need some context node because it is used for resolving unqualified names */
8355 real_ctx_node = NULL;
8356 } else {
8357 real_ctx_node = ctx_node;
8358 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008359
8360 /* prepare set for evaluation */
Michal Vasko004d3152020-06-11 19:59:22 +02008361 tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008362 memset(set, 0, sizeof *set);
Michal Vaskod3678892020-05-21 10:06:58 +02008363 set->type = LYXP_SET_NODE_SET;
Michal Vasko004d3152020-06-11 19:59:22 +02008364 set_insert_node(set, (struct lyd_node *)real_ctx_node, 0, ctx_node_type, 0);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008365 set->ctx = local_mod->ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008366 set->ctx_node = ctx_node;
Michal Vasko004d3152020-06-11 19:59:22 +02008367 set->root_type = lyxp_get_root_type(real_ctx_node, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008368 set->local_mod = local_mod;
Michal Vaskof03ed032020-03-04 13:31:44 +01008369 set->tree = tree;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008370 set->format = format;
8371
8372 /* evaluate */
Michal Vasko004d3152020-06-11 19:59:22 +02008373 rc = eval_expr_select(exp, &tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008374 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02008375 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008376 }
8377
Michal Vasko03ff5a72019-09-11 13:49:33 +02008378 return rc;
8379}
8380
8381#if 0
8382
8383/* full xml printing of set elements, not used currently */
8384
8385void
8386lyxp_set_print_xml(FILE *f, struct lyxp_set *set)
8387{
8388 uint32_t i;
8389 char *str_num;
8390 struct lyout out;
8391
8392 memset(&out, 0, sizeof out);
8393
8394 out.type = LYOUT_STREAM;
8395 out.method.f = f;
8396
8397 switch (set->type) {
8398 case LYXP_SET_EMPTY:
8399 ly_print(&out, "Empty XPath set\n\n");
8400 break;
8401 case LYXP_SET_BOOLEAN:
8402 ly_print(&out, "Boolean XPath set:\n");
8403 ly_print(&out, "%s\n\n", set->value.bool ? "true" : "false");
8404 break;
8405 case LYXP_SET_STRING:
8406 ly_print(&out, "String XPath set:\n");
8407 ly_print(&out, "\"%s\"\n\n", set->value.str);
8408 break;
8409 case LYXP_SET_NUMBER:
8410 ly_print(&out, "Number XPath set:\n");
8411
8412 if (isnan(set->value.num)) {
8413 str_num = strdup("NaN");
8414 } else if ((set->value.num == 0) || (set->value.num == -0.0f)) {
8415 str_num = strdup("0");
8416 } else if (isinf(set->value.num) && !signbit(set->value.num)) {
8417 str_num = strdup("Infinity");
8418 } else if (isinf(set->value.num) && signbit(set->value.num)) {
8419 str_num = strdup("-Infinity");
8420 } else if ((long long)set->value.num == set->value.num) {
8421 if (asprintf(&str_num, "%lld", (long long)set->value.num) == -1) {
8422 str_num = NULL;
8423 }
8424 } else {
8425 if (asprintf(&str_num, "%03.1Lf", set->value.num) == -1) {
8426 str_num = NULL;
8427 }
8428 }
8429 if (!str_num) {
8430 LOGMEM;
8431 return;
8432 }
8433 ly_print(&out, "%s\n\n", str_num);
8434 free(str_num);
8435 break;
8436 case LYXP_SET_NODE_SET:
8437 ly_print(&out, "Node XPath set:\n");
8438
8439 for (i = 0; i < set->used; ++i) {
8440 ly_print(&out, "%d. ", i + 1);
8441 switch (set->node_type[i]) {
8442 case LYXP_NODE_ROOT_ALL:
8443 ly_print(&out, "ROOT all\n\n");
8444 break;
8445 case LYXP_NODE_ROOT_CONFIG:
8446 ly_print(&out, "ROOT config\n\n");
8447 break;
8448 case LYXP_NODE_ROOT_STATE:
8449 ly_print(&out, "ROOT state\n\n");
8450 break;
8451 case LYXP_NODE_ROOT_NOTIF:
8452 ly_print(&out, "ROOT notification \"%s\"\n\n", set->value.nodes[i]->schema->name);
8453 break;
8454 case LYXP_NODE_ROOT_RPC:
8455 ly_print(&out, "ROOT rpc \"%s\"\n\n", set->value.nodes[i]->schema->name);
8456 break;
8457 case LYXP_NODE_ROOT_OUTPUT:
8458 ly_print(&out, "ROOT output \"%s\"\n\n", set->value.nodes[i]->schema->name);
8459 break;
8460 case LYXP_NODE_ELEM:
8461 ly_print(&out, "ELEM \"%s\"\n", set->value.nodes[i]->schema->name);
8462 xml_print_node(&out, 1, set->value.nodes[i], 1, LYP_FORMAT);
8463 ly_print(&out, "\n");
8464 break;
8465 case LYXP_NODE_TEXT:
8466 ly_print(&out, "TEXT \"%s\"\n\n", ((struct lyd_node_leaf_list *)set->value.nodes[i])->value_str);
8467 break;
8468 case LYXP_NODE_ATTR:
8469 ly_print(&out, "ATTR \"%s\" = \"%s\"\n\n", set->value.attrs[i]->name, set->value.attrs[i]->value);
8470 break;
8471 }
8472 }
8473 break;
8474 }
8475}
8476
8477#endif
8478
8479LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008480lyxp_set_cast(struct lyxp_set *set, enum lyxp_set_type target)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008481{
8482 long double num;
8483 char *str;
8484 LY_ERR rc;
8485
8486 if (!set || (set->type == target)) {
8487 return LY_SUCCESS;
8488 }
8489
8490 /* it's not possible to convert anything into a node set */
Michal Vaskod3678892020-05-21 10:06:58 +02008491 assert(target != LYXP_SET_NODE_SET);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008492
8493 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vaskod3678892020-05-21 10:06:58 +02008494 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008495 return LY_EINVAL;
8496 }
8497
8498 /* to STRING */
Michal Vaskod3678892020-05-21 10:06:58 +02008499 if ((target == LYXP_SET_STRING) || ((target == LYXP_SET_NUMBER) && (set->type == LYXP_SET_NODE_SET))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008500 switch (set->type) {
8501 case LYXP_SET_NUMBER:
8502 if (isnan(set->val.num)) {
8503 set->val.str = strdup("NaN");
8504 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8505 } else if ((set->val.num == 0) || (set->val.num == -0.0f)) {
8506 set->val.str = strdup("0");
8507 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8508 } else if (isinf(set->val.num) && !signbit(set->val.num)) {
8509 set->val.str = strdup("Infinity");
8510 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8511 } else if (isinf(set->val.num) && signbit(set->val.num)) {
8512 set->val.str = strdup("-Infinity");
8513 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8514 } else if ((long long)set->val.num == set->val.num) {
8515 if (asprintf(&str, "%lld", (long long)set->val.num) == -1) {
8516 LOGMEM_RET(set->ctx);
8517 }
8518 set->val.str = str;
8519 } else {
8520 if (asprintf(&str, "%03.1Lf", set->val.num) == -1) {
8521 LOGMEM_RET(set->ctx);
8522 }
8523 set->val.str = str;
8524 }
8525 break;
8526 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02008527 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008528 set->val.str = strdup("true");
8529 } else {
8530 set->val.str = strdup("false");
8531 }
8532 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM);
8533 break;
8534 case LYXP_SET_NODE_SET:
8535 assert(set->used);
8536
8537 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008538 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008539
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008540 rc = cast_node_set_to_string(set, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008541 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02008542 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008543 set->val.str = str;
8544 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008545 default:
8546 LOGINT_RET(set->ctx);
8547 }
8548 set->type = LYXP_SET_STRING;
8549 }
8550
8551 /* to NUMBER */
8552 if (target == LYXP_SET_NUMBER) {
8553 switch (set->type) {
8554 case LYXP_SET_STRING:
8555 num = cast_string_to_number(set->val.str);
Michal Vaskod3678892020-05-21 10:06:58 +02008556 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008557 set->val.num = num;
8558 break;
8559 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02008560 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008561 set->val.num = 1;
8562 } else {
8563 set->val.num = 0;
8564 }
8565 break;
8566 default:
8567 LOGINT_RET(set->ctx);
8568 }
8569 set->type = LYXP_SET_NUMBER;
8570 }
8571
8572 /* to BOOLEAN */
8573 if (target == LYXP_SET_BOOLEAN) {
8574 switch (set->type) {
8575 case LYXP_SET_NUMBER:
8576 if ((set->val.num == 0) || (set->val.num == -0.0f) || isnan(set->val.num)) {
Michal Vasko004d3152020-06-11 19:59:22 +02008577 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008578 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02008579 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008580 }
8581 break;
8582 case LYXP_SET_STRING:
8583 if (set->val.str[0]) {
Michal Vaskod3678892020-05-21 10:06:58 +02008584 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008585 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008586 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02008587 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008588 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008589 }
8590 break;
8591 case LYXP_SET_NODE_SET:
Michal Vaskod3678892020-05-21 10:06:58 +02008592 if (set->used) {
8593 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008594 set->val.bln = 1;
Michal Vaskod3678892020-05-21 10:06:58 +02008595 } else {
8596 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008597 set->val.bln = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02008598 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008599 break;
8600 default:
8601 LOGINT_RET(set->ctx);
8602 }
8603 set->type = LYXP_SET_BOOLEAN;
8604 }
8605
Michal Vasko03ff5a72019-09-11 13:49:33 +02008606 return LY_SUCCESS;
8607}
8608
8609LY_ERR
8610lyxp_atomize(struct lyxp_expr *exp, LYD_FORMAT format, const struct lys_module *local_mod, const struct lysc_node *ctx_scnode,
8611 enum lyxp_node_type ctx_scnode_type, struct lyxp_set *set, int options)
8612{
8613 struct ly_ctx *ctx;
Michal Vasko004d3152020-06-11 19:59:22 +02008614 const struct lysc_node *real_ctx_scnode;
8615 uint16_t tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008616
Michal Vasko004d3152020-06-11 19:59:22 +02008617 LY_CHECK_ARG_RET(NULL, exp, local_mod, ctx_scnode, set, LY_EINVAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008618
8619 ctx = local_mod->ctx;
Michal Vasko004d3152020-06-11 19:59:22 +02008620 if ((ctx_scnode_type == LYXP_NODE_ROOT) || (ctx_scnode_type == LYXP_NODE_ROOT_CONFIG)) {
8621 /* we always need some context node because it is used for resolving unqualified names */
8622 real_ctx_scnode = NULL;
8623 } else {
8624 real_ctx_scnode = ctx_scnode;
8625 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008626
8627 /* prepare set for evaluation */
Michal Vasko004d3152020-06-11 19:59:22 +02008628 tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008629 memset(set, 0, sizeof *set);
8630 set->type = LYXP_SET_SCNODE_SET;
Michal Vasko004d3152020-06-11 19:59:22 +02008631 lyxp_set_scnode_insert_node(set, real_ctx_scnode, ctx_scnode_type);
Michal Vasko5c4e5892019-11-14 12:31:38 +01008632 set->val.scnodes[0].in_ctx = -2;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008633 set->ctx = ctx;
8634 set->ctx_scnode = ctx_scnode;
Michal Vasko004d3152020-06-11 19:59:22 +02008635 set->root_type = lyxp_get_root_type(NULL, real_ctx_scnode, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008636 set->local_mod = local_mod;
8637 set->format = format;
8638
8639 /* evaluate */
Michal Vasko004d3152020-06-11 19:59:22 +02008640 return eval_expr_select(exp, &tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008641}