blob: 55a0f6b7be0c027cc7a39e6d6162ae273606eb8d [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{
Radek Krejci7f769d72020-07-11 23:13:56 +0200337 char *buf, *line, *ptr = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200338 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));
Michal Vasko3a41dff2020-07-15 14:30:28 +0200434 rc = lyd_print_all(out, any->value.tree, LYD_XML, 0);
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{
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02001018 uint16_t i, orig_used, end = 0;
Michal Vasko57eab132019-09-24 11:46:26 +02001019 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{
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02001303 const struct lyd_node *next, *elem = NULL, *top_sibling;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001304 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
Michal Vasko99c71642020-07-03 13:33:36 +02002937 dup->repeat[i] = malloc(j * sizeof **dup->repeat);
Michal Vasko004d3152020-06-11 19:59:22 +02002938 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;
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004176 struct lys_module *mod = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004177 char *str;
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004178 const char *name = NULL;
4179 int rc = -1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004180
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:
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004220 /* keep NULL */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004221 break;
4222 case LYXP_NODE_ELEM:
4223 mod = item->node->schema->module;
4224 name = item->node->schema->name;
4225 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004226 case LYXP_NODE_META:
4227 mod = ((struct lyd_meta *)item->node)->annotation->module;
4228 name = ((struct lyd_meta *)item->node)->name;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004229 break;
4230 }
4231
4232 if (mod && name) {
4233 switch (set->format) {
Michal Vasko52927e22020-03-16 17:26:14 +01004234 case LYD_SCHEMA:
Michal Vasko03ff5a72019-09-11 13:49:33 +02004235 rc = asprintf(&str, "%s:%s", lys_prefix_find_module(set->local_mod, mod), name);
4236 break;
4237 case LYD_JSON:
4238 rc = asprintf(&str, "%s:%s", mod->name, name);
4239 break;
Michal Vasko52927e22020-03-16 17:26:14 +01004240 case LYD_XML:
Michal Vasko60ea6352020-06-29 13:39:39 +02004241 case LYD_LYB:
Michal Vasko9409ef62019-09-12 11:47:17 +02004242 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004243 }
4244 LY_CHECK_ERR_RET(rc == -1, LOGMEM(set->ctx), LY_EMEM);
4245 set_fill_string(set, str, strlen(str));
4246 free(str);
4247 } else {
4248 set_fill_string(set, "", 0);
4249 }
4250
4251 return LY_SUCCESS;
4252}
4253
4254/**
4255 * @brief Execute the XPath namespace-uri(node-set?) function. Returns LYXP_SET_STRING
4256 * with the namespace of the node from the argument or the context.
4257 *
4258 * @param[in] args Array of arguments.
4259 * @param[in] arg_count Count of elements in @p args.
4260 * @param[in,out] set Context and result set at the same time.
4261 * @param[in] options XPath options.
4262 * @return LY_ERR (LY_EINVAL for wrong arguments on schema)
4263 */
4264static LY_ERR
4265xpath_namespace_uri(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
4266{
4267 struct lyxp_set_node *item;
4268 struct lys_module *mod;
4269 /* suppress unused variable warning */
4270 (void)options;
4271
4272 if (options & LYXP_SCNODE_ALL) {
4273 set_scnode_clear_ctx(set);
4274 return LY_SUCCESS;
4275 }
4276
4277 if (arg_count) {
Michal Vaskod3678892020-05-21 10:06:58 +02004278 if (args[0]->type != LYXP_SET_NODE_SET) {
4279 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
4280 "namespace-uri(node-set?)");
4281 return LY_EVALID;
4282 } else if (!args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004283 set_fill_string(set, "", 0);
4284 return LY_SUCCESS;
4285 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004286
4287 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004288 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004289
4290 item = &args[0]->val.nodes[0];
4291 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004292 if (set->type != LYXP_SET_NODE_SET) {
4293 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "namespace-uri(node-set?)");
4294 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004295 } else if (!set->used) {
4296 set_fill_string(set, "", 0);
4297 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004298 }
4299
4300 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004301 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004302
4303 item = &set->val.nodes[0];
4304 }
4305
4306 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004307 case LYXP_NODE_NONE:
4308 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004309 case LYXP_NODE_ROOT:
4310 case LYXP_NODE_ROOT_CONFIG:
4311 case LYXP_NODE_TEXT:
4312 set_fill_string(set, "", 0);
4313 break;
4314 case LYXP_NODE_ELEM:
Michal Vasko9f96a052020-03-10 09:41:45 +01004315 case LYXP_NODE_META:
Michal Vasko03ff5a72019-09-11 13:49:33 +02004316 if (item->type == LYXP_NODE_ELEM) {
4317 mod = item->node->schema->module;
Michal Vasko9f96a052020-03-10 09:41:45 +01004318 } else { /* LYXP_NODE_META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004319 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004320 mod = ((struct lyd_meta *)item->node)->annotation->module;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004321 }
4322
4323 set_fill_string(set, mod->ns, strlen(mod->ns));
4324 break;
4325 }
4326
4327 return LY_SUCCESS;
4328}
4329
4330/**
4331 * @brief Execute the XPath node() function (node type). Returns LYXP_SET_NODE_SET
4332 * with only nodes from the context. In practice it either leaves the context
4333 * as it is or returns an empty node set.
4334 *
4335 * @param[in] args Array of arguments.
4336 * @param[in] arg_count Count of elements in @p args.
4337 * @param[in,out] set Context and result set at the same time.
4338 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004339 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004340 */
4341static LY_ERR
4342xpath_node(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4343{
4344 if (options & LYXP_SCNODE_ALL) {
4345 set_scnode_clear_ctx(set);
4346 return LY_SUCCESS;
4347 }
4348
4349 if (set->type != LYXP_SET_NODE_SET) {
Michal Vaskod3678892020-05-21 10:06:58 +02004350 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004351 }
4352 return LY_SUCCESS;
4353}
4354
4355/**
4356 * @brief Execute the XPath normalize-space(string?) function. Returns LYXP_SET_STRING
4357 * with normalized value (no leading, trailing, double white spaces) of the node
4358 * from the argument or the context.
4359 *
4360 * @param[in] args Array of arguments.
4361 * @param[in] arg_count Count of elements in @p args.
4362 * @param[in,out] set Context and result set at the same time.
4363 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004364 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004365 */
4366static LY_ERR
4367xpath_normalize_space(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
4368{
4369 uint16_t i, new_used;
4370 char *new;
4371 int have_spaces = 0, space_before = 0;
4372 struct lysc_node_leaf *sleaf;
4373 LY_ERR rc = LY_SUCCESS;
4374
4375 if (options & LYXP_SCNODE_ALL) {
4376 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4377 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4378 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 +02004379 } else if (!warn_is_string_type(sleaf->type)) {
4380 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004381 }
4382 }
4383 set_scnode_clear_ctx(set);
4384 return rc;
4385 }
4386
4387 if (arg_count) {
4388 set_fill_set(set, args[0]);
4389 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004390 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004391 LY_CHECK_RET(rc);
4392
4393 /* is there any normalization necessary? */
4394 for (i = 0; set->val.str[i]; ++i) {
4395 if (is_xmlws(set->val.str[i])) {
4396 if ((i == 0) || space_before || (!set->val.str[i + 1])) {
4397 have_spaces = 1;
4398 break;
4399 }
4400 space_before = 1;
4401 } else {
4402 space_before = 0;
4403 }
4404 }
4405
4406 /* yep, there is */
4407 if (have_spaces) {
4408 /* it's enough, at least one character will go, makes space for ending '\0' */
4409 new = malloc(strlen(set->val.str) * sizeof(char));
4410 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4411 new_used = 0;
4412
4413 space_before = 0;
4414 for (i = 0; set->val.str[i]; ++i) {
4415 if (is_xmlws(set->val.str[i])) {
4416 if ((i == 0) || space_before) {
4417 space_before = 1;
4418 continue;
4419 } else {
4420 space_before = 1;
4421 }
4422 } else {
4423 space_before = 0;
4424 }
4425
4426 new[new_used] = (space_before ? ' ' : set->val.str[i]);
4427 ++new_used;
4428 }
4429
4430 /* at worst there is one trailing space now */
4431 if (new_used && is_xmlws(new[new_used - 1])) {
4432 --new_used;
4433 }
4434
4435 new = ly_realloc(new, (new_used + 1) * sizeof(char));
4436 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4437 new[new_used] = '\0';
4438
4439 free(set->val.str);
4440 set->val.str = new;
4441 }
4442
4443 return LY_SUCCESS;
4444}
4445
4446/**
4447 * @brief Execute the XPath not(boolean) function. Returns LYXP_SET_BOOLEAN
4448 * with the argument converted to boolean and logically inverted.
4449 *
4450 * @param[in] args Array of arguments.
4451 * @param[in] arg_count Count of elements in @p args.
4452 * @param[in,out] set Context and result set at the same time.
4453 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004454 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004455 */
4456static LY_ERR
4457xpath_not(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4458{
4459 if (options & LYXP_SCNODE_ALL) {
4460 set_scnode_clear_ctx(set);
4461 return LY_SUCCESS;
4462 }
4463
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004464 lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02004465 if (args[0]->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004466 set_fill_boolean(set, 0);
4467 } else {
4468 set_fill_boolean(set, 1);
4469 }
4470
4471 return LY_SUCCESS;
4472}
4473
4474/**
4475 * @brief Execute the XPath number(object?) function. Returns LYXP_SET_NUMBER
4476 * with the number representation of either the argument or the context.
4477 *
4478 * @param[in] args Array of arguments.
4479 * @param[in] arg_count Count of elements in @p args.
4480 * @param[in,out] set Context and result set at the same time.
4481 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004482 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004483 */
4484static LY_ERR
4485xpath_number(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
4486{
4487 LY_ERR rc;
4488
4489 if (options & LYXP_SCNODE_ALL) {
4490 set_scnode_clear_ctx(set);
4491 return LY_SUCCESS;
4492 }
4493
4494 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004495 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004496 LY_CHECK_RET(rc);
4497 set_fill_set(set, args[0]);
4498 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004499 rc = lyxp_set_cast(set, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004500 LY_CHECK_RET(rc);
4501 }
4502
4503 return LY_SUCCESS;
4504}
4505
4506/**
4507 * @brief Execute the XPath position() function. Returns LYXP_SET_NUMBER
4508 * with the context position.
4509 *
4510 * @param[in] args Array of arguments.
4511 * @param[in] arg_count Count of elements in @p args.
4512 * @param[in,out] set Context and result set at the same time.
4513 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004514 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004515 */
4516static LY_ERR
4517xpath_position(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4518{
4519 if (options & LYXP_SCNODE_ALL) {
4520 set_scnode_clear_ctx(set);
4521 return LY_SUCCESS;
4522 }
4523
Michal Vasko03ff5a72019-09-11 13:49:33 +02004524 if (set->type != LYXP_SET_NODE_SET) {
4525 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "position()");
4526 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004527 } else if (!set->used) {
4528 set_fill_number(set, 0);
4529 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004530 }
4531
4532 set_fill_number(set, set->ctx_pos);
4533
4534 /* UNUSED in 'Release' build type */
4535 (void)options;
4536 return LY_SUCCESS;
4537}
4538
4539/**
4540 * @brief Execute the YANG 1.1 re-match(string, string) function. Returns LYXP_SET_BOOLEAN
4541 * depending on whether the second argument regex matches the first argument string. For details refer to
4542 * YANG 1.1 RFC section 10.2.1.
4543 *
4544 * @param[in] args Array of arguments.
4545 * @param[in] arg_count Count of elements in @p args.
4546 * @param[in,out] set Context and result set at the same time.
4547 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004548 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004549 */
4550static LY_ERR
4551xpath_re_match(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4552{
4553 struct lysc_pattern **patterns = NULL, **pattern;
4554 struct lysc_node_leaf *sleaf;
4555 char *path;
4556 LY_ERR rc = LY_SUCCESS;
4557 struct ly_err_item *err;
4558
4559 if (options & LYXP_SCNODE_ALL) {
4560 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4561 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4562 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 +02004563 } else if (!warn_is_string_type(sleaf->type)) {
4564 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004565 }
4566 }
4567
4568 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4569 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4570 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 +02004571 } else if (!warn_is_string_type(sleaf->type)) {
4572 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004573 }
4574 }
4575 set_scnode_clear_ctx(set);
4576 return rc;
4577 }
4578
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004579 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004580 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004581 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004582 LY_CHECK_RET(rc);
4583
4584 LY_ARRAY_NEW_RET(set->ctx, patterns, pattern, LY_EMEM);
4585 *pattern = malloc(sizeof **pattern);
4586 path = lyd_path(set->ctx_node, LYD_PATH_LOG, NULL, 0);
4587 rc = lys_compile_type_pattern_check(set->ctx, path, args[1]->val.str, &(*pattern)->code);
4588 free(path);
4589 if (rc != LY_SUCCESS) {
4590 LY_ARRAY_FREE(patterns);
4591 return rc;
4592 }
4593
4594 rc = ly_type_validate_patterns(patterns, args[0]->val.str, strlen(args[0]->val.str), &err);
4595 pcre2_code_free((*pattern)->code);
4596 free(*pattern);
4597 LY_ARRAY_FREE(patterns);
4598 if (rc && (rc != LY_EVALID)) {
4599 ly_err_print(err);
4600 ly_err_free(err);
4601 return rc;
4602 }
4603
4604 if (rc == LY_EVALID) {
4605 ly_err_free(err);
4606 set_fill_boolean(set, 0);
4607 } else {
4608 set_fill_boolean(set, 1);
4609 }
4610
4611 return LY_SUCCESS;
4612}
4613
4614/**
4615 * @brief Execute the XPath round(number) function. Returns LYXP_SET_NUMBER
4616 * with the rounded first argument. For details refer to
4617 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-round.
4618 *
4619 * @param[in] args Array of arguments.
4620 * @param[in] arg_count Count of elements in @p args.
4621 * @param[in,out] set Context and result set at the same time.
4622 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004623 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004624 */
4625static LY_ERR
4626xpath_round(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4627{
4628 struct lysc_node_leaf *sleaf;
4629 LY_ERR rc = LY_SUCCESS;
4630
4631 if (options & LYXP_SCNODE_ALL) {
4632 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4633 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004634 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4635 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 +02004636 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
4637 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004638 }
4639 set_scnode_clear_ctx(set);
4640 return rc;
4641 }
4642
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004643 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004644 LY_CHECK_RET(rc);
4645
4646 /* cover only the cases where floor can't be used */
4647 if ((args[0]->val.num == -0.0f) || ((args[0]->val.num < 0) && (args[0]->val.num >= -0.5))) {
4648 set_fill_number(set, -0.0f);
4649 } else {
4650 args[0]->val.num += 0.5;
4651 rc = xpath_floor(args, 1, args[0], options);
4652 LY_CHECK_RET(rc);
4653 set_fill_number(set, args[0]->val.num);
4654 }
4655
4656 return LY_SUCCESS;
4657}
4658
4659/**
4660 * @brief Execute the XPath starts-with(string, string) function.
4661 * Returns LYXP_SET_BOOLEAN whether the second argument is
4662 * the prefix of the first or not.
4663 *
4664 * @param[in] args Array of arguments.
4665 * @param[in] arg_count Count of elements in @p args.
4666 * @param[in,out] set Context and result set at the same time.
4667 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004668 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004669 */
4670static LY_ERR
4671xpath_starts_with(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4672{
4673 struct lysc_node_leaf *sleaf;
4674 LY_ERR rc = LY_SUCCESS;
4675
4676 if (options & LYXP_SCNODE_ALL) {
4677 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4678 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4679 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 +02004680 } else if (!warn_is_string_type(sleaf->type)) {
4681 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004682 }
4683 }
4684
4685 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4686 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4687 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 +02004688 } else if (!warn_is_string_type(sleaf->type)) {
4689 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004690 }
4691 }
4692 set_scnode_clear_ctx(set);
4693 return rc;
4694 }
4695
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004696 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004697 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004698 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004699 LY_CHECK_RET(rc);
4700
4701 if (strncmp(args[0]->val.str, args[1]->val.str, strlen(args[1]->val.str))) {
4702 set_fill_boolean(set, 0);
4703 } else {
4704 set_fill_boolean(set, 1);
4705 }
4706
4707 return LY_SUCCESS;
4708}
4709
4710/**
4711 * @brief Execute the XPath string(object?) function. Returns LYXP_SET_STRING
4712 * with the string representation of either the argument or the context.
4713 *
4714 * @param[in] args Array of arguments.
4715 * @param[in] arg_count Count of elements in @p args.
4716 * @param[in,out] set Context and result set at the same time.
4717 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004718 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004719 */
4720static LY_ERR
4721xpath_string(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
4722{
4723 LY_ERR rc;
4724
4725 if (options & LYXP_SCNODE_ALL) {
4726 set_scnode_clear_ctx(set);
4727 return LY_SUCCESS;
4728 }
4729
4730 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004731 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004732 LY_CHECK_RET(rc);
4733 set_fill_set(set, args[0]);
4734 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004735 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004736 LY_CHECK_RET(rc);
4737 }
4738
4739 return LY_SUCCESS;
4740}
4741
4742/**
4743 * @brief Execute the XPath string-length(string?) function. Returns LYXP_SET_NUMBER
4744 * with the length of the string in either the argument or the context.
4745 *
4746 * @param[in] args Array of arguments.
4747 * @param[in] arg_count Count of elements in @p args.
4748 * @param[in,out] set Context and result set at the same time.
4749 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004750 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004751 */
4752static LY_ERR
4753xpath_string_length(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
4754{
4755 struct lysc_node_leaf *sleaf;
4756 LY_ERR rc = LY_SUCCESS;
4757
4758 if (options & LYXP_SCNODE_ALL) {
4759 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4760 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4761 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 +02004762 } else if (!warn_is_string_type(sleaf->type)) {
4763 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004764 }
4765 }
4766 if (!arg_count && (set->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set))) {
4767 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4768 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 +02004769 } else if (!warn_is_string_type(sleaf->type)) {
4770 LOGWRN(set->ctx, "Argument #0 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004771 }
4772 }
4773 set_scnode_clear_ctx(set);
4774 return rc;
4775 }
4776
4777 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004778 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004779 LY_CHECK_RET(rc);
4780 set_fill_number(set, strlen(args[0]->val.str));
4781 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004782 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004783 LY_CHECK_RET(rc);
4784 set_fill_number(set, strlen(set->val.str));
4785 }
4786
4787 return LY_SUCCESS;
4788}
4789
4790/**
4791 * @brief Execute the XPath substring(string, number, number?) function.
4792 * Returns LYXP_SET_STRING substring of the first argument starting
4793 * on the second argument index ending on the third argument index,
4794 * indexed from 1. For exact definition refer to
4795 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-substring.
4796 *
4797 * @param[in] args Array of arguments.
4798 * @param[in] arg_count Count of elements in @p args.
4799 * @param[in,out] set Context and result set at the same time.
4800 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004801 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004802 */
4803static LY_ERR
4804xpath_substring(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, int options)
4805{
4806 int start, len;
4807 uint16_t str_start, str_len, pos;
4808 struct lysc_node_leaf *sleaf;
4809 LY_ERR rc = LY_SUCCESS;
4810
4811 if (options & LYXP_SCNODE_ALL) {
4812 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4813 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4814 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 +02004815 } else if (!warn_is_string_type(sleaf->type)) {
4816 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004817 }
4818 }
4819
4820 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4821 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4822 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 +02004823 } else if (!warn_is_numeric_type(sleaf->type)) {
4824 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004825 }
4826 }
4827
4828 if ((arg_count == 3) && (args[2]->type == LYXP_SET_SCNODE_SET)
4829 && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
4830 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4831 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 +02004832 } else if (!warn_is_numeric_type(sleaf->type)) {
4833 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004834 }
4835 }
4836 set_scnode_clear_ctx(set);
4837 return rc;
4838 }
4839
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004840 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004841 LY_CHECK_RET(rc);
4842
4843 /* start */
4844 if (xpath_round(&args[1], 1, args[1], options)) {
4845 return -1;
4846 }
4847 if (isfinite(args[1]->val.num)) {
4848 start = args[1]->val.num - 1;
4849 } else if (isinf(args[1]->val.num) && signbit(args[1]->val.num)) {
4850 start = INT_MIN;
4851 } else {
4852 start = INT_MAX;
4853 }
4854
4855 /* len */
4856 if (arg_count == 3) {
4857 rc = xpath_round(&args[2], 1, args[2], options);
4858 LY_CHECK_RET(rc);
4859 if (isfinite(args[2]->val.num)) {
4860 len = args[2]->val.num;
4861 } else if (isnan(args[2]->val.num) || signbit(args[2]->val.num)) {
4862 len = 0;
4863 } else {
4864 len = INT_MAX;
4865 }
4866 } else {
4867 len = INT_MAX;
4868 }
4869
4870 /* find matching character positions */
4871 str_start = 0;
4872 str_len = 0;
4873 for (pos = 0; args[0]->val.str[pos]; ++pos) {
4874 if (pos < start) {
4875 ++str_start;
4876 } else if (pos < start + len) {
4877 ++str_len;
4878 } else {
4879 break;
4880 }
4881 }
4882
4883 set_fill_string(set, args[0]->val.str + str_start, str_len);
4884 return LY_SUCCESS;
4885}
4886
4887/**
4888 * @brief Execute the XPath substring-after(string, string) function.
4889 * Returns LYXP_SET_STRING with the string succeeding the occurance
4890 * of the second argument in the first or an empty string.
4891 *
4892 * @param[in] args Array of arguments.
4893 * @param[in] arg_count Count of elements in @p args.
4894 * @param[in,out] set Context and result set at the same time.
4895 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004896 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004897 */
4898static LY_ERR
4899xpath_substring_after(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4900{
4901 char *ptr;
4902 struct lysc_node_leaf *sleaf;
4903 LY_ERR rc = LY_SUCCESS;
4904
4905 if (options & LYXP_SCNODE_ALL) {
4906 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4907 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4908 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 +02004909 } else if (!warn_is_string_type(sleaf->type)) {
4910 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004911 }
4912 }
4913
4914 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4915 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4916 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 +02004917 } else if (!warn_is_string_type(sleaf->type)) {
4918 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004919 }
4920 }
4921 set_scnode_clear_ctx(set);
4922 return rc;
4923 }
4924
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004925 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004926 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004927 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004928 LY_CHECK_RET(rc);
4929
4930 ptr = strstr(args[0]->val.str, args[1]->val.str);
4931 if (ptr) {
4932 set_fill_string(set, ptr + strlen(args[1]->val.str), strlen(ptr + strlen(args[1]->val.str)));
4933 } else {
4934 set_fill_string(set, "", 0);
4935 }
4936
4937 return LY_SUCCESS;
4938}
4939
4940/**
4941 * @brief Execute the XPath substring-before(string, string) function.
4942 * Returns LYXP_SET_STRING with the string preceding the occurance
4943 * of the second argument in the first or an empty string.
4944 *
4945 * @param[in] args Array of arguments.
4946 * @param[in] arg_count Count of elements in @p args.
4947 * @param[in,out] set Context and result set at the same time.
4948 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004949 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004950 */
4951static LY_ERR
4952xpath_substring_before(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
4953{
4954 char *ptr;
4955 struct lysc_node_leaf *sleaf;
4956 LY_ERR rc = LY_SUCCESS;
4957
4958 if (options & LYXP_SCNODE_ALL) {
4959 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4960 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4961 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 +02004962 } else if (!warn_is_string_type(sleaf->type)) {
4963 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004964 }
4965 }
4966
4967 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4968 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4969 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 +02004970 } else if (!warn_is_string_type(sleaf->type)) {
4971 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004972 }
4973 }
4974 set_scnode_clear_ctx(set);
4975 return rc;
4976 }
4977
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004978 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004979 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004980 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004981 LY_CHECK_RET(rc);
4982
4983 ptr = strstr(args[0]->val.str, args[1]->val.str);
4984 if (ptr) {
4985 set_fill_string(set, args[0]->val.str, ptr - args[0]->val.str);
4986 } else {
4987 set_fill_string(set, "", 0);
4988 }
4989
4990 return LY_SUCCESS;
4991}
4992
4993/**
4994 * @brief Execute the XPath sum(node-set) function. Returns LYXP_SET_NUMBER
4995 * with the sum of all the nodes in the context.
4996 *
4997 * @param[in] args Array of arguments.
4998 * @param[in] arg_count Count of elements in @p args.
4999 * @param[in,out] set Context and result set at the same time.
5000 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005001 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005002 */
5003static LY_ERR
5004xpath_sum(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
5005{
5006 long double num;
5007 char *str;
5008 uint16_t i;
5009 struct lyxp_set set_item;
5010 struct lysc_node_leaf *sleaf;
5011 LY_ERR rc = LY_SUCCESS;
5012
5013 if (options & LYXP_SCNODE_ALL) {
5014 if (args[0]->type == LYXP_SET_SCNODE_SET) {
5015 for (i = 0; i < args[0]->used; ++i) {
5016 if (args[0]->val.scnodes[i].in_ctx == 1) {
5017 sleaf = (struct lysc_node_leaf *)args[0]->val.scnodes[i].scnode;
5018 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5019 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__,
5020 lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005021 } else if (!warn_is_numeric_type(sleaf->type)) {
5022 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005023 }
5024 }
5025 }
5026 }
5027 set_scnode_clear_ctx(set);
5028 return rc;
5029 }
5030
5031 set_fill_number(set, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005032
5033 if (args[0]->type != LYXP_SET_NODE_SET) {
5034 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "sum(node-set)");
5035 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02005036 } else if (!args[0]->used) {
5037 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005038 }
5039
Michal Vasko5c4e5892019-11-14 12:31:38 +01005040 set_init(&set_item, set);
5041
Michal Vasko03ff5a72019-09-11 13:49:33 +02005042 set_item.type = LYXP_SET_NODE_SET;
5043 set_item.val.nodes = malloc(sizeof *set_item.val.nodes);
5044 LY_CHECK_ERR_RET(!set_item.val.nodes, LOGMEM(set->ctx), LY_EMEM);
5045
5046 set_item.used = 1;
5047 set_item.size = 1;
5048
5049 for (i = 0; i < args[0]->used; ++i) {
5050 set_item.val.nodes[0] = args[0]->val.nodes[i];
5051
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005052 rc = cast_node_set_to_string(&set_item, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005053 LY_CHECK_RET(rc);
5054 num = cast_string_to_number(str);
5055 free(str);
5056 set->val.num += num;
5057 }
5058
5059 free(set_item.val.nodes);
5060
5061 return LY_SUCCESS;
5062}
5063
5064/**
5065 * @brief Execute the XPath text() function (node type). Returns LYXP_SET_NODE_SET
5066 * with the text content of the nodes in the context.
5067 *
5068 * @param[in] args Array of arguments.
5069 * @param[in] arg_count Count of elements in @p args.
5070 * @param[in,out] set Context and result set at the same time.
5071 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005072 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005073 */
5074static LY_ERR
5075xpath_text(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
5076{
5077 uint32_t i;
5078
5079 if (options & LYXP_SCNODE_ALL) {
5080 set_scnode_clear_ctx(set);
5081 return LY_SUCCESS;
5082 }
5083
Michal Vasko03ff5a72019-09-11 13:49:33 +02005084 if (set->type != LYXP_SET_NODE_SET) {
5085 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "text()");
5086 return LY_EVALID;
5087 }
5088
5089 for (i = 0; i < set->used;) {
5090 switch (set->val.nodes[i].type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01005091 case LYXP_NODE_NONE:
5092 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005093 case LYXP_NODE_ELEM:
Michal Vasko03ff5a72019-09-11 13:49:33 +02005094 if (set->val.nodes[i].node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
5095 set->val.nodes[i].type = LYXP_NODE_TEXT;
5096 ++i;
5097 break;
5098 }
5099 /* fall through */
5100 case LYXP_NODE_ROOT:
5101 case LYXP_NODE_ROOT_CONFIG:
5102 case LYXP_NODE_TEXT:
Michal Vasko9f96a052020-03-10 09:41:45 +01005103 case LYXP_NODE_META:
Michal Vasko03ff5a72019-09-11 13:49:33 +02005104 set_remove_node(set, i);
5105 break;
5106 }
5107 }
5108
5109 return LY_SUCCESS;
5110}
5111
5112/**
5113 * @brief Execute the XPath translate(string, string, string) function.
5114 * Returns LYXP_SET_STRING with the first argument with the characters
5115 * from the second argument replaced by those on the corresponding
5116 * positions in the third argument.
5117 *
5118 * @param[in] args Array of arguments.
5119 * @param[in] arg_count Count of elements in @p args.
5120 * @param[in,out] set Context and result set at the same time.
5121 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005122 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005123 */
5124static LY_ERR
5125xpath_translate(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
5126{
5127 uint16_t i, j, new_used;
5128 char *new;
5129 int found, have_removed;
5130 struct lysc_node_leaf *sleaf;
5131 LY_ERR rc = LY_SUCCESS;
5132
5133 if (options & LYXP_SCNODE_ALL) {
5134 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5135 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5136 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 +02005137 } else if (!warn_is_string_type(sleaf->type)) {
5138 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005139 }
5140 }
5141
5142 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5143 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5144 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 +02005145 } else if (!warn_is_string_type(sleaf->type)) {
5146 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005147 }
5148 }
5149
5150 if ((args[2]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
5151 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5152 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 +02005153 } else if (!warn_is_string_type(sleaf->type)) {
5154 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005155 }
5156 }
5157 set_scnode_clear_ctx(set);
5158 return rc;
5159 }
5160
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005161 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005162 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005163 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005164 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005165 rc = lyxp_set_cast(args[2], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005166 LY_CHECK_RET(rc);
5167
5168 new = malloc((strlen(args[0]->val.str) + 1) * sizeof(char));
5169 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5170 new_used = 0;
5171
5172 have_removed = 0;
5173 for (i = 0; args[0]->val.str[i]; ++i) {
5174 found = 0;
5175
5176 for (j = 0; args[1]->val.str[j]; ++j) {
5177 if (args[0]->val.str[i] == args[1]->val.str[j]) {
5178 /* removing this char */
5179 if (j >= strlen(args[2]->val.str)) {
5180 have_removed = 1;
5181 found = 1;
5182 break;
5183 }
5184 /* replacing this char */
5185 new[new_used] = args[2]->val.str[j];
5186 ++new_used;
5187 found = 1;
5188 break;
5189 }
5190 }
5191
5192 /* copying this char */
5193 if (!found) {
5194 new[new_used] = args[0]->val.str[i];
5195 ++new_used;
5196 }
5197 }
5198
5199 if (have_removed) {
5200 new = ly_realloc(new, (new_used + 1) * sizeof(char));
5201 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5202 }
5203 new[new_used] = '\0';
5204
Michal Vaskod3678892020-05-21 10:06:58 +02005205 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005206 set->type = LYXP_SET_STRING;
5207 set->val.str = new;
5208
5209 return LY_SUCCESS;
5210}
5211
5212/**
5213 * @brief Execute the XPath true() function. Returns LYXP_SET_BOOLEAN
5214 * with true value.
5215 *
5216 * @param[in] args Array of arguments.
5217 * @param[in] arg_count Count of elements in @p args.
5218 * @param[in,out] set Context and result set at the same time.
5219 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005220 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005221 */
5222static LY_ERR
5223xpath_true(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, int options)
5224{
5225 if (options & LYXP_SCNODE_ALL) {
5226 set_scnode_clear_ctx(set);
5227 return LY_SUCCESS;
5228 }
5229
5230 set_fill_boolean(set, 1);
5231 return LY_SUCCESS;
5232}
5233
5234/*
5235 * moveto functions
5236 *
5237 * They and only they actually change the context (set).
5238 */
5239
5240/**
Michal Vasko6346ece2019-09-24 13:12:53 +02005241 * @brief Skip prefix and return corresponding model if there is a prefix. Logs directly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005242 *
Michal Vasko2104e9f2020-03-06 08:23:25 +01005243 * XPath @p set is expected to be a (sc)node set!
5244 *
Michal Vasko6346ece2019-09-24 13:12:53 +02005245 * @param[in,out] qname Qualified node name. If includes prefix, it is skipped.
5246 * @param[in,out] qname_len Length of @p qname, is updated accordingly.
5247 * @param[in] set Set with XPath context.
5248 * @param[out] moveto_mod Expected module of a matching node.
5249 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005250 */
Michal Vasko6346ece2019-09-24 13:12:53 +02005251static LY_ERR
5252moveto_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 +02005253{
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02005254 const struct lys_module *mod = NULL;
Michal Vasko6346ece2019-09-24 13:12:53 +02005255 const char *ptr;
5256 int pref_len;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005257 char *str;
5258
Michal Vasko2104e9f2020-03-06 08:23:25 +01005259 assert((set->type == LYXP_SET_NODE_SET) || (set->type == LYXP_SET_SCNODE_SET));
5260
Michal Vasko6346ece2019-09-24 13:12:53 +02005261 if ((ptr = ly_strnchr(*qname, ':', *qname_len))) {
5262 /* specific module */
5263 pref_len = ptr - *qname;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005264
Michal Vasko6346ece2019-09-24 13:12:53 +02005265 switch (set->format) {
Michal Vasko52927e22020-03-16 17:26:14 +01005266 case LYD_SCHEMA:
Michal Vasko6346ece2019-09-24 13:12:53 +02005267 /* schema, search all local module imports */
5268 mod = lys_module_find_prefix(set->local_mod, *qname, pref_len);
5269 break;
5270 case LYD_JSON:
5271 /* JSON data, search in context */
5272 str = strndup(*qname, pref_len);
Michal Vasko97e76fd2020-05-27 15:22:01 +02005273 mod = ly_ctx_get_module_implemented(set->ctx, str);
Michal Vasko6346ece2019-09-24 13:12:53 +02005274 free(str);
5275 break;
Michal Vasko52927e22020-03-16 17:26:14 +01005276 case LYD_XML:
Michal Vasko60ea6352020-06-29 13:39:39 +02005277 case LYD_LYB:
Michal Vasko6346ece2019-09-24 13:12:53 +02005278 LOGINT_RET(set->ctx);
5279 }
5280
Michal Vasko004d3152020-06-11 19:59:22 +02005281 /* check for errors and non-implemented modules, as they are not valid */
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005282 if (!mod || !mod->implemented) {
Michal Vasko2104e9f2020-03-06 08:23:25 +01005283 if (set->type == LYXP_SET_SCNODE_SET) {
5284 LOGVAL(set->ctx, LY_VLOG_LYSC, set->ctx_scnode, LY_VCODE_XP_INMOD, pref_len, *qname);
5285 } else {
5286 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INMOD, pref_len, *qname);
5287 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005288 return LY_EVALID;
5289 }
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005290
Michal Vasko6346ece2019-09-24 13:12:53 +02005291 *qname += pref_len + 1;
5292 *qname_len -= pref_len + 1;
5293 } else if (((*qname)[0] == '*') && (*qname_len == 1)) {
5294 /* all modules - special case */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005295 mod = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02005296 } else if (set->type == LYXP_SET_SCNODE_SET) {
5297 /* current node module */
5298 mod = set->ctx_scnode->module;
Michal Vasko6346ece2019-09-24 13:12:53 +02005299 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02005300 /* current node module */
5301 mod = set->ctx_node->schema->module;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005302 }
5303
Michal Vasko6346ece2019-09-24 13:12:53 +02005304 *moveto_mod = mod;
5305 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005306}
5307
5308/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02005309 * @brief Move context @p set to the root. Handles absolute path.
5310 * Result is LYXP_SET_NODE_SET.
5311 *
5312 * @param[in,out] set Set to use.
5313 * @param[in] options Xpath options.
5314 */
5315static void
5316moveto_root(struct lyxp_set *set, int options)
5317{
Michal Vasko03ff5a72019-09-11 13:49:33 +02005318 if (!set) {
5319 return;
5320 }
5321
5322 if (options & LYXP_SCNODE_ALL) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005323 set_scnode_clear_ctx(set);
Michal Vaskoecd62de2019-11-13 12:35:11 +01005324 lyxp_set_scnode_insert_node(set, NULL, set->root_type);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005325 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005326 set->type = LYXP_SET_NODE_SET;
5327 set->used = 0;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005328 set_insert_node(set, NULL, 0, set->root_type, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005329 }
5330}
5331
5332/**
Michal Vaskoa1424542019-11-14 16:08:52 +01005333 * @brief Check whether a node has some unresolved "when".
5334 *
5335 * @param[in] node Node to check.
5336 * @return LY_ERR value (LY_EINCOMPLETE if there are some unresolved "when")
5337 */
5338static LY_ERR
5339moveto_when_check(const struct lyd_node *node)
5340{
5341 const struct lysc_node *schema;
5342
5343 if (!node) {
5344 return LY_SUCCESS;
5345 }
5346
5347 schema = node->schema;
5348 do {
5349 if (schema->when && !(node->flags & LYD_WHEN_TRUE)) {
5350 return LY_EINCOMPLETE;
5351 }
5352 schema = schema->parent;
5353 } while (schema && (schema->nodetype & (LYS_CASE | LYS_CHOICE)));
5354
5355 return LY_SUCCESS;
5356}
5357
5358/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02005359 * @brief Check @p node as a part of NameTest processing.
5360 *
5361 * @param[in] node Node to check.
5362 * @param[in] root_type XPath root node type.
Michal Vaskod3678892020-05-21 10:06:58 +02005363 * @param[in] node_name Node name in the dictionary to move to, NULL for any node.
5364 * @param[in] moveto_mod Expected module of the node, NULL for any.
Michal Vasko6346ece2019-09-24 13:12:53 +02005365 * @return LY_ERR (LY_ENOT if node does not match, LY_EINCOMPLETE on unresolved when,
5366 * LY_EINVAL if netither node nor any children match)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005367 */
5368static LY_ERR
5369moveto_node_check(const struct lyd_node *node, enum lyxp_node_type root_type, const char *node_name,
5370 const struct lys_module *moveto_mod)
5371{
5372 /* module check */
5373 if (moveto_mod && (node->schema->module != moveto_mod)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005374 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005375 }
5376
Michal Vasko5c4e5892019-11-14 12:31:38 +01005377 /* context check */
5378 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005379 return LY_EINVAL;
5380 }
5381
5382 /* name check */
Michal Vasko61ac2f62020-05-25 12:39:51 +02005383 if (node_name && strcmp(node_name, "*") && (node->schema->name != node_name)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005384 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005385 }
5386
Michal Vaskoa1424542019-11-14 16:08:52 +01005387 /* when check */
5388 if (moveto_when_check(node)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005389 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01005390 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005391
5392 /* match */
5393 return LY_SUCCESS;
5394}
5395
5396/**
5397 * @brief Check @p node as a part of schema NameTest processing.
5398 *
5399 * @param[in] node Schema node to check.
5400 * @param[in] root_type XPath root node type.
Michal Vaskod3678892020-05-21 10:06:58 +02005401 * @param[in] node_name Node name in the dictionary to move to, NULL for any nodes.
5402 * @param[in] moveto_mod Expected module of the node, NULL for any.
Michal Vasko6346ece2019-09-24 13:12:53 +02005403 * @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 +02005404 */
5405static LY_ERR
5406moveto_scnode_check(const struct lysc_node *node, enum lyxp_node_type root_type, const char *node_name,
Michal Vaskocafad9d2019-11-07 15:20:03 +01005407 const struct lys_module *moveto_mod)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005408{
Michal Vasko03ff5a72019-09-11 13:49:33 +02005409 /* module check */
Michal Vaskod3678892020-05-21 10:06:58 +02005410 if (moveto_mod && (node->module != moveto_mod)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005411 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005412 }
5413
5414 /* context check */
5415 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (node->flags & LYS_CONFIG_R)) {
5416 return LY_EINVAL;
5417 }
5418
5419 /* name check */
Michal Vasko61ac2f62020-05-25 12:39:51 +02005420 if (node_name && strcmp(node_name, "*") && (node->name != node_name)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005421 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005422 }
5423
5424 /* match */
5425 return LY_SUCCESS;
5426}
5427
5428/**
Michal Vaskod3678892020-05-21 10:06:58 +02005429 * @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 +02005430 *
5431 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005432 * @param[in] mod Matching node module, NULL for any.
5433 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005434 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5435 */
5436static LY_ERR
Michal Vaskod3678892020-05-21 10:06:58 +02005437moveto_node(struct lyxp_set *set, const struct lys_module *mod, const char *ncname)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005438{
Michal Vaskof03ed032020-03-04 13:31:44 +01005439 uint32_t i;
Michal Vasko6346ece2019-09-24 13:12:53 +02005440 int replaced;
Michal Vaskod3678892020-05-21 10:06:58 +02005441 const struct lyd_node *siblings, *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005442 LY_ERR rc;
5443
Michal Vaskod3678892020-05-21 10:06:58 +02005444 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005445 return LY_SUCCESS;
5446 }
5447
5448 if (set->type != LYXP_SET_NODE_SET) {
5449 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
5450 return LY_EVALID;
5451 }
5452
Michal Vasko03ff5a72019-09-11 13:49:33 +02005453 for (i = 0; i < set->used; ) {
5454 replaced = 0;
5455
5456 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 +01005457 assert(!set->val.nodes[i].node);
Michal Vaskod3678892020-05-21 10:06:58 +02005458
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005459 /* search in all the trees */
Michal Vaskod3678892020-05-21 10:06:58 +02005460 siblings = set->tree;
Michal Vasko5bfd4be2020-06-23 13:26:19 +02005461 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005462 /* search in children */
Michal Vasko5bfd4be2020-06-23 13:26:19 +02005463 siblings = lyd_node_children(set->val.nodes[i].node, 0);
Michal Vaskod3678892020-05-21 10:06:58 +02005464 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005465
Michal Vaskod3678892020-05-21 10:06:58 +02005466 for (sub = siblings; sub; sub = sub->next) {
5467 rc = moveto_node_check(sub, set->root_type, ncname, mod);
5468 if (rc == LY_SUCCESS) {
5469 if (!replaced) {
5470 set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i);
5471 replaced = 1;
5472 } else {
5473 set_insert_node(set, sub, 0, LYXP_NODE_ELEM, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005474 }
Michal Vaskod3678892020-05-21 10:06:58 +02005475 ++i;
5476 } else if (rc == LY_EINCOMPLETE) {
5477 return rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005478 }
5479 }
5480
5481 if (!replaced) {
5482 /* no match */
5483 set_remove_node(set, i);
5484 }
5485 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005486
5487 return LY_SUCCESS;
5488}
5489
5490/**
Michal Vaskod3678892020-05-21 10:06:58 +02005491 * @brief Move context @p set to a node using hashes. Result is LYXP_SET_NODE_SET (or LYXP_SET_EMPTY).
5492 * Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005493 *
5494 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005495 * @param[in] scnode Matching node schema.
Michal Vasko004d3152020-06-11 19:59:22 +02005496 * @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 +02005497 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5498 */
5499static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02005500moveto_node_hash(struct lyxp_set *set, const struct lysc_node *scnode, const struct ly_path_predicate *predicates)
Michal Vaskod3678892020-05-21 10:06:58 +02005501{
Michal Vasko004d3152020-06-11 19:59:22 +02005502 LY_ERR ret = LY_SUCCESS;
Michal Vaskod3678892020-05-21 10:06:58 +02005503 uint32_t i;
Michal Vaskod3678892020-05-21 10:06:58 +02005504 const struct lyd_node *siblings;
Michal Vasko004d3152020-06-11 19:59:22 +02005505 struct lyd_node *sub, *inst = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02005506
Michal Vasko004d3152020-06-11 19:59:22 +02005507 assert(scnode && (!(scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)) || predicates));
Michal Vaskod3678892020-05-21 10:06:58 +02005508
5509 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02005510 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005511 }
5512
5513 if (set->type != LYXP_SET_NODE_SET) {
5514 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 +02005515 ret = LY_EVALID;
5516 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005517 }
5518
5519 /* context check for all the nodes since we have the schema node */
5520 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) {
5521 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02005522 goto cleanup;
5523 }
5524
5525 /* create specific data instance if needed */
5526 if (scnode->nodetype == LYS_LIST) {
5527 LY_CHECK_GOTO(ret = lyd_create_list(scnode, predicates, &inst), cleanup);
5528 } else if (scnode->nodetype == LYS_LEAFLIST) {
5529 LY_CHECK_GOTO(ret = lyd_create_term2(scnode, &predicates[0].value, &inst), cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02005530 }
5531
5532 for (i = 0; i < set->used; ) {
Michal Vaskod3678892020-05-21 10:06:58 +02005533 siblings = NULL;
5534
5535 if ((set->val.nodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.nodes[i].type == LYXP_NODE_ROOT)) {
5536 assert(!set->val.nodes[i].node);
5537
5538 /* search in all the trees */
5539 siblings = set->tree;
5540 } else if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
5541 /* search in children */
Michal Vasko5bfd4be2020-06-23 13:26:19 +02005542 siblings = lyd_node_children(set->val.nodes[i].node, 0);
Michal Vaskod3678892020-05-21 10:06:58 +02005543 }
5544
5545 /* find the node using hashes */
Michal Vasko004d3152020-06-11 19:59:22 +02005546 if (inst) {
5547 lyd_find_sibling_first(siblings, inst, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02005548 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02005549 lyd_find_sibling_val(siblings, scnode, NULL, 0, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02005550 }
5551
5552 /* when check */
5553 if (sub && moveto_when_check(sub)) {
Michal Vasko004d3152020-06-11 19:59:22 +02005554 ret = LY_EINCOMPLETE;
5555 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005556 }
5557
5558 if (sub) {
5559 /* pos filled later */
Michal Vaskocb1b7c02020-07-03 13:38:12 +02005560 set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i);
Michal Vaskod3678892020-05-21 10:06:58 +02005561 ++i;
Michal Vaskocb1b7c02020-07-03 13:38:12 +02005562 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005563 /* no match */
5564 set_remove_node(set, i);
5565 }
5566 }
5567
Michal Vasko004d3152020-06-11 19:59:22 +02005568cleanup:
5569 lyd_free_tree(inst);
5570 return ret;
Michal Vaskod3678892020-05-21 10:06:58 +02005571}
5572
5573/**
5574 * @brief Move context @p set to a schema node. Result is LYXP_SET_SCNODE_SET (or LYXP_SET_EMPTY).
5575 *
5576 * @param[in,out] set Set to use.
5577 * @param[in] mod Matching node module, NULL for any.
5578 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005579 * @param[in] options XPath options.
5580 * @return LY_ERR
5581 */
5582static LY_ERR
Michal Vaskod3678892020-05-21 10:06:58 +02005583moveto_scnode(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, int options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005584{
Michal Vaskocafad9d2019-11-07 15:20:03 +01005585 int i, orig_used, idx, temp_ctx = 0, getnext_opts;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005586 uint32_t mod_idx;
Michal Vasko519fd602020-05-26 12:17:39 +02005587 const struct lysc_node *iter, *start_parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005588
Michal Vaskod3678892020-05-21 10:06:58 +02005589 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005590 return LY_SUCCESS;
5591 }
5592
5593 if (set->type != LYXP_SET_SCNODE_SET) {
Michal Vaskof6e51882019-12-16 09:59:45 +01005594 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 +02005595 return LY_EVALID;
5596 }
5597
Michal Vaskocafad9d2019-11-07 15:20:03 +01005598 /* getnext opts */
5599 getnext_opts = LYS_GETNEXT_NOSTATECHECK;
5600 if (options & LYXP_SCNODE_OUTPUT) {
5601 getnext_opts |= LYS_GETNEXT_OUTPUT;
5602 }
5603
Michal Vasko03ff5a72019-09-11 13:49:33 +02005604 orig_used = set->used;
5605 for (i = 0; i < orig_used; ++i) {
5606 if (set->val.scnodes[i].in_ctx != 1) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01005607 if (set->val.scnodes[i].in_ctx != -2) {
5608 continue;
5609 }
5610
5611 /* remember context node */
5612 set->val.scnodes[i].in_ctx = -1;
Michal Vaskoec4df482019-12-16 10:02:18 +01005613 } else {
5614 set->val.scnodes[i].in_ctx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005615 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005616
5617 start_parent = set->val.scnodes[i].scnode;
5618
5619 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 +02005620 /* 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 +02005621 * so use it directly (root node itself is useless in this case) */
5622 mod_idx = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02005623 while (mod || (mod = (struct lys_module *)ly_ctx_get_module_iter(set->ctx, &mod_idx))) {
Michal Vasko519fd602020-05-26 12:17:39 +02005624 iter = NULL;
Michal Vasko509de4d2019-12-10 14:51:30 +01005625 /* module may not be implemented */
Michal Vasko519fd602020-05-26 12:17:39 +02005626 while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) {
5627 if (!moveto_scnode_check(iter, set->root_type, ncname, mod)) {
5628 idx = lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005629 /* we need to prevent these nodes from being considered in this moveto */
5630 if ((idx < orig_used) && (idx > i)) {
5631 set->val.scnodes[idx].in_ctx = 2;
5632 temp_ctx = 1;
5633 }
5634 }
5635 }
5636
5637 if (!mod_idx) {
Michal Vaskod3678892020-05-21 10:06:58 +02005638 /* mod was specified, we are not going through the whole context */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005639 break;
5640 }
5641 /* next iteration */
Michal Vaskod3678892020-05-21 10:06:58 +02005642 mod = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005643 }
5644
Michal Vasko519fd602020-05-26 12:17:39 +02005645 } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
5646 iter = NULL;
5647 while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) {
5648 if (!moveto_scnode_check(iter, set->root_type, ncname, (mod ? mod : set->local_mod))) {
5649 idx = lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005650 if ((idx < orig_used) && (idx > i)) {
5651 set->val.scnodes[idx].in_ctx = 2;
5652 temp_ctx = 1;
5653 }
5654 }
5655 }
5656 }
5657 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005658
5659 /* correct temporary in_ctx values */
5660 if (temp_ctx) {
5661 for (i = 0; i < orig_used; ++i) {
5662 if (set->val.scnodes[i].in_ctx == 2) {
5663 set->val.scnodes[i].in_ctx = 1;
5664 }
5665 }
5666 }
5667
5668 return LY_SUCCESS;
5669}
5670
5671/**
Michal Vaskod3678892020-05-21 10:06:58 +02005672 * @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 +02005673 * Context position aware.
5674 *
5675 * @param[in] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005676 * @param[in] mod Matching node module, NULL for any.
5677 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005678 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5679 */
5680static LY_ERR
Michal Vaskod3678892020-05-21 10:06:58 +02005681moveto_node_alldesc(struct lyxp_set *set, const struct lys_module *mod, const char *ncname)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005682{
5683 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005684 const struct lyd_node *next, *elem, *start;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005685 struct lyxp_set ret_set;
5686 LY_ERR rc;
5687
Michal Vaskod3678892020-05-21 10:06:58 +02005688 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005689 return LY_SUCCESS;
5690 }
5691
5692 if (set->type != LYXP_SET_NODE_SET) {
5693 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
5694 return LY_EVALID;
5695 }
5696
Michal Vasko9f96a052020-03-10 09:41:45 +01005697 /* 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 +02005698 rc = moveto_node(set, NULL, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005699 LY_CHECK_RET(rc);
5700
Michal Vasko6346ece2019-09-24 13:12:53 +02005701 /* this loop traverses all the nodes in the set and adds/keeps only those that match qname */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005702 set_init(&ret_set, set);
5703 for (i = 0; i < set->used; ++i) {
5704
5705 /* TREE DFS */
5706 start = set->val.nodes[i].node;
5707 for (elem = next = start; elem; elem = next) {
Michal Vaskod3678892020-05-21 10:06:58 +02005708 rc = moveto_node_check(elem, set->root_type, ncname, mod);
Michal Vasko6346ece2019-09-24 13:12:53 +02005709 if (!rc) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005710 /* add matching node into result set */
5711 set_insert_node(&ret_set, elem, 0, LYXP_NODE_ELEM, ret_set.used);
5712 if (set_dup_node_check(set, elem, LYXP_NODE_ELEM, i)) {
5713 /* the node is a duplicate, we'll process it later in the set */
5714 goto skip_children;
5715 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005716 } else if (rc == LY_EINCOMPLETE) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005717 return rc;
5718 } else if (rc == LY_EINVAL) {
5719 goto skip_children;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005720 }
5721
5722 /* TREE DFS NEXT ELEM */
5723 /* select element for the next run - children first */
Michal Vasko5bfd4be2020-06-23 13:26:19 +02005724 next = lyd_node_children(elem, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005725 if (!next) {
5726skip_children:
5727 /* no children, so try siblings, but only if it's not the start,
5728 * that is considered to be the root and it's siblings are not traversed */
5729 if (elem != start) {
5730 next = elem->next;
5731 } else {
5732 break;
5733 }
5734 }
5735 while (!next) {
5736 /* no siblings, go back through the parents */
5737 if ((struct lyd_node *)elem->parent == start) {
5738 /* we are done, no next element to process */
5739 break;
5740 }
5741 /* parent is already processed, go to its sibling */
5742 elem = (struct lyd_node *)elem->parent;
5743 next = elem->next;
5744 }
5745 }
5746 }
5747
5748 /* make the temporary set the current one */
5749 ret_set.ctx_pos = set->ctx_pos;
5750 ret_set.ctx_size = set->ctx_size;
Michal Vaskod3678892020-05-21 10:06:58 +02005751 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005752 memcpy(set, &ret_set, sizeof *set);
5753
5754 return LY_SUCCESS;
5755}
5756
5757/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005758 * @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 +02005759 *
5760 * @param[in] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005761 * @param[in] mod Matching node module, NULL for any.
5762 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005763 * @param[in] options XPath options.
5764 * @return LY_ERR
5765 */
5766static LY_ERR
Michal Vaskod3678892020-05-21 10:06:58 +02005767moveto_scnode_alldesc(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, int options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005768{
Michal Vasko6346ece2019-09-24 13:12:53 +02005769 int i, orig_used, idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005770 const struct lysc_node *next, *elem, *start;
Michal Vasko6346ece2019-09-24 13:12:53 +02005771 LY_ERR rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005772
Michal Vaskod3678892020-05-21 10:06:58 +02005773 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005774 return LY_SUCCESS;
5775 }
5776
5777 if (set->type != LYXP_SET_SCNODE_SET) {
Michal Vaskof6e51882019-12-16 09:59:45 +01005778 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 +02005779 return LY_EVALID;
5780 }
5781
Michal Vasko03ff5a72019-09-11 13:49:33 +02005782 orig_used = set->used;
5783 for (i = 0; i < orig_used; ++i) {
5784 if (set->val.scnodes[i].in_ctx != 1) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01005785 if (set->val.scnodes[i].in_ctx != -2) {
5786 continue;
5787 }
5788
5789 /* remember context node */
5790 set->val.scnodes[i].in_ctx = -1;
Michal Vaskoec4df482019-12-16 10:02:18 +01005791 } else {
5792 set->val.scnodes[i].in_ctx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005793 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005794
5795 /* TREE DFS */
5796 start = set->val.scnodes[i].scnode;
5797 for (elem = next = start; elem; elem = next) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005798 if ((elem == start) || (elem->nodetype & (LYS_CHOICE | LYS_CASE))) {
5799 /* schema-only nodes, skip root */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005800 goto next_iter;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005801 }
5802
Michal Vaskod3678892020-05-21 10:06:58 +02005803 rc = moveto_scnode_check(elem, set->root_type, ncname, mod);
Michal Vasko6346ece2019-09-24 13:12:53 +02005804 if (!rc) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01005805 if ((idx = lyxp_set_scnode_dup_node_check(set, elem, LYXP_NODE_ELEM, i)) > -1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005806 set->val.scnodes[idx].in_ctx = 1;
5807 if (idx > i) {
5808 /* we will process it later in the set */
5809 goto skip_children;
5810 }
5811 } else {
Michal Vaskoecd62de2019-11-13 12:35:11 +01005812 lyxp_set_scnode_insert_node(set, elem, LYXP_NODE_ELEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005813 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005814 } else if (rc == LY_EINVAL) {
5815 goto skip_children;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005816 }
5817
5818next_iter:
5819 /* TREE DFS NEXT ELEM */
5820 /* select element for the next run - children first */
5821 next = lysc_node_children(elem, options & LYXP_SCNODE_OUTPUT ? LYS_CONFIG_R : LYS_CONFIG_W);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005822 if (!next) {
5823skip_children:
5824 /* no children, so try siblings, but only if it's not the start,
5825 * that is considered to be the root and it's siblings are not traversed */
5826 if (elem != start) {
5827 next = elem->next;
5828 } else {
5829 break;
5830 }
5831 }
5832 while (!next) {
5833 /* no siblings, go back through the parents */
5834 if (elem->parent == start) {
5835 /* we are done, no next element to process */
5836 break;
5837 }
5838 /* parent is already processed, go to its sibling */
5839 elem = elem->parent;
5840 next = elem->next;
5841 }
5842 }
5843 }
5844
5845 return LY_SUCCESS;
5846}
5847
5848/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005849 * @brief Move context @p set to an attribute. Result is LYXP_SET_NODE_SET.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005850 * Indirectly context position aware.
5851 *
5852 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005853 * @param[in] mod Matching metadata module, NULL for any.
5854 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005855 * @return LY_ERR
5856 */
5857static LY_ERR
Michal Vaskod3678892020-05-21 10:06:58 +02005858moveto_attr(struct lyxp_set *set, const struct lys_module *mod, const char *ncname)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005859{
5860 uint32_t i;
Michal Vaskod3678892020-05-21 10:06:58 +02005861 int replaced;
Michal Vasko9f96a052020-03-10 09:41:45 +01005862 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005863
Michal Vaskod3678892020-05-21 10:06:58 +02005864 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005865 return LY_SUCCESS;
5866 }
5867
5868 if (set->type != LYXP_SET_NODE_SET) {
5869 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
5870 return LY_EVALID;
5871 }
5872
Michal Vasko03ff5a72019-09-11 13:49:33 +02005873 for (i = 0; i < set->used; ) {
5874 replaced = 0;
5875
5876 /* only attributes of an elem (not dummy) can be in the result, skip all the rest;
5877 * our attributes are always qualified */
Michal Vasko5c4e5892019-11-14 12:31:38 +01005878 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01005879 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005880
5881 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02005882 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005883 continue;
5884 }
5885
Michal Vaskod3678892020-05-21 10:06:58 +02005886 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005887 /* match */
5888 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01005889 set->val.meta[i].meta = sub;
5890 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005891 /* pos does not change */
5892 replaced = 1;
5893 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01005894 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 +02005895 }
5896 ++i;
5897 }
5898 }
5899 }
5900
5901 if (!replaced) {
5902 /* no match */
5903 set_remove_node(set, i);
5904 }
5905 }
5906
5907 return LY_SUCCESS;
5908}
5909
5910/**
5911 * @brief Move context @p set1 to union with @p set2. @p set2 is emptied afterwards.
Michal Vasko61ac2f62020-05-25 12:39:51 +02005912 * Result is LYXP_SET_NODE_SET. Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005913 *
5914 * @param[in,out] set1 Set to use for the result.
5915 * @param[in] set2 Set that is copied to @p set1.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005916 * @return LY_ERR
5917 */
5918static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005919moveto_union(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005920{
5921 LY_ERR rc;
5922
Michal Vaskod3678892020-05-21 10:06:58 +02005923 if ((set1->type != LYXP_SET_NODE_SET) || (set2->type != LYXP_SET_NODE_SET)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005924 LOGVAL(set1->ctx, LY_VLOG_LYD, set1->ctx_node, LY_VCODE_XP_INOP_2, "union", print_set_type(set1), print_set_type(set2));
5925 return LY_EVALID;
5926 }
5927
5928 /* set2 is empty or both set1 and set2 */
Michal Vaskod3678892020-05-21 10:06:58 +02005929 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005930 return LY_SUCCESS;
5931 }
5932
Michal Vaskod3678892020-05-21 10:06:58 +02005933 if (!set1->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005934 memcpy(set1, set2, sizeof *set1);
5935 /* dynamic memory belongs to set1 now, do not free */
Michal Vaskod3678892020-05-21 10:06:58 +02005936 memset(set2, 0, sizeof *set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005937 return LY_SUCCESS;
5938 }
5939
5940 /* we assume sets are sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005941 assert(!set_sort(set1) && !set_sort(set2));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005942
5943 /* sort, remove duplicates */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005944 rc = set_sorted_merge(set1, set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005945 LY_CHECK_RET(rc);
5946
5947 /* final set must be sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005948 assert(!set_sort(set1));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005949
5950 return LY_SUCCESS;
5951}
5952
5953/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005954 * @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 +02005955 * Context position aware.
5956 *
5957 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005958 * @param[in] mod Matching metadata module, NULL for any.
5959 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005960 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5961 */
5962static int
Michal Vaskod3678892020-05-21 10:06:58 +02005963moveto_attr_alldesc(struct lyxp_set *set, const struct lys_module *mod, const char *ncname)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005964{
5965 uint32_t i;
Michal Vaskod3678892020-05-21 10:06:58 +02005966 int replaced;
Michal Vasko9f96a052020-03-10 09:41:45 +01005967 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005968 struct lyxp_set *set_all_desc = NULL;
5969 LY_ERR rc;
5970
Michal Vaskod3678892020-05-21 10:06:58 +02005971 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005972 return LY_SUCCESS;
5973 }
5974
5975 if (set->type != LYXP_SET_NODE_SET) {
5976 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
5977 return LY_EVALID;
5978 }
5979
Michal Vasko03ff5a72019-09-11 13:49:33 +02005980 /* can be optimized similarly to moveto_node_alldesc() and save considerable amount of memory,
5981 * but it likely won't be used much, so it's a waste of time */
5982 /* copy the context */
5983 set_all_desc = set_copy(set);
5984 /* get all descendant nodes (the original context nodes are removed) */
Michal Vaskod3678892020-05-21 10:06:58 +02005985 rc = moveto_node_alldesc(set_all_desc, NULL, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005986 if (rc != LY_SUCCESS) {
5987 lyxp_set_free(set_all_desc);
5988 return rc;
5989 }
5990 /* prepend the original context nodes */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005991 rc = moveto_union(set, set_all_desc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005992 if (rc != LY_SUCCESS) {
5993 lyxp_set_free(set_all_desc);
5994 return rc;
5995 }
5996 lyxp_set_free(set_all_desc);
5997
Michal Vasko03ff5a72019-09-11 13:49:33 +02005998 for (i = 0; i < set->used; ) {
5999 replaced = 0;
6000
6001 /* only attributes of an elem can be in the result, skip all the rest,
6002 * we have all attributes qualified in lyd tree */
6003 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01006004 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006005 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02006006 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006007 continue;
6008 }
6009
Michal Vaskod3678892020-05-21 10:06:58 +02006010 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006011 /* match */
6012 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01006013 set->val.meta[i].meta = sub;
6014 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006015 /* pos does not change */
6016 replaced = 1;
6017 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01006018 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 +02006019 }
6020 ++i;
6021 }
6022 }
6023 }
6024
6025 if (!replaced) {
6026 /* no match */
6027 set_remove_node(set, i);
6028 }
6029 }
6030
6031 return LY_SUCCESS;
6032}
6033
6034/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02006035 * @brief Move context @p set to self and al chilren, recursively. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET.
6036 * Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006037 *
6038 * @param[in] parent Current parent.
6039 * @param[in] parent_pos Position of @p parent.
6040 * @param[in] parent_type Node type of @p parent.
6041 * @param[in,out] to_set Set to use.
6042 * @param[in] dup_check_set Set for checking duplicities.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006043 * @param[in] options XPath options.
6044 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6045 */
6046static LY_ERR
6047moveto_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 +01006048 struct lyxp_set *to_set, const struct lyxp_set *dup_check_set, int options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006049{
Michal Vasko61ac2f62020-05-25 12:39:51 +02006050 const struct lyd_node *iter, *first;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006051 LY_ERR rc;
6052
6053 switch (parent_type) {
6054 case LYXP_NODE_ROOT:
6055 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko61ac2f62020-05-25 12:39:51 +02006056 assert(!parent);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006057
Michal Vasko61ac2f62020-05-25 12:39:51 +02006058 /* add all top-level nodes as elements */
6059 first = to_set->tree;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006060 break;
6061 case LYXP_NODE_ELEM:
Michal Vasko61ac2f62020-05-25 12:39:51 +02006062 /* add just the text node of this term element node */
6063 if (parent->schema->nodetype & (LYD_NODE_TERM | LYD_NODE_ANY)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006064 if (!set_dup_node_check(dup_check_set, parent, LYXP_NODE_TEXT, -1)) {
6065 set_insert_node(to_set, parent, parent_pos, LYXP_NODE_TEXT, to_set->used);
6066 }
Michal Vasko61ac2f62020-05-25 12:39:51 +02006067 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006068 }
Michal Vasko61ac2f62020-05-25 12:39:51 +02006069
6070 /* add all the children of this node */
Michal Vasko5bfd4be2020-06-23 13:26:19 +02006071 first = lyd_node_children(parent, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006072 break;
6073 default:
6074 LOGINT_RET(parent->schema->module->ctx);
6075 }
6076
Michal Vasko61ac2f62020-05-25 12:39:51 +02006077 /* add all top-level nodes as elements */
6078 LY_LIST_FOR(first, iter) {
6079 /* context check */
6080 if ((parent_type == LYXP_NODE_ROOT_CONFIG) && (iter->schema->flags & LYS_CONFIG_R)) {
6081 continue;
6082 }
6083
6084 /* when check */
6085 if (moveto_when_check(iter)) {
6086 return LY_EINCOMPLETE;
6087 }
6088
6089 if (!set_dup_node_check(dup_check_set, iter, LYXP_NODE_ELEM, -1)) {
6090 set_insert_node(to_set, iter, 0, LYXP_NODE_ELEM, to_set->used);
6091
6092 /* also add all the children of this node, recursively */
6093 rc = moveto_self_add_children_r(iter, 0, LYXP_NODE_ELEM, to_set, dup_check_set, options);
6094 LY_CHECK_RET(rc);
6095 }
6096 }
6097
Michal Vasko03ff5a72019-09-11 13:49:33 +02006098 return LY_SUCCESS;
6099}
6100
6101/**
6102 * @brief Move context @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET
6103 * (or LYXP_SET_EMPTY). Context position aware.
6104 *
6105 * @param[in,out] set Set to use.
6106 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6107 * @param[in] options XPath options.
6108 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6109 */
6110static LY_ERR
6111moveto_self(struct lyxp_set *set, int all_desc, int options)
6112{
6113 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006114 struct lyxp_set ret_set;
6115 LY_ERR rc;
6116
Michal Vaskod3678892020-05-21 10:06:58 +02006117 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006118 return LY_SUCCESS;
6119 }
6120
6121 if (set->type != LYXP_SET_NODE_SET) {
6122 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
6123 return LY_EVALID;
6124 }
6125
6126 /* nothing to do */
6127 if (!all_desc) {
6128 return LY_SUCCESS;
6129 }
6130
Michal Vasko03ff5a72019-09-11 13:49:33 +02006131 /* add all the children, they get added recursively */
6132 set_init(&ret_set, set);
6133 for (i = 0; i < set->used; ++i) {
6134 /* copy the current node to tmp */
6135 set_insert_node(&ret_set, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, ret_set.used);
6136
6137 /* do not touch attributes and text nodes */
Michal Vasko9f96a052020-03-10 09:41:45 +01006138 if ((set->val.nodes[i].type == LYXP_NODE_TEXT) || (set->val.nodes[i].type == LYXP_NODE_META)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006139 continue;
6140 }
6141
Michal Vasko03ff5a72019-09-11 13:49:33 +02006142 /* add all the children */
6143 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 +01006144 set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006145 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006146 lyxp_set_free_content(&ret_set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006147 return rc;
6148 }
6149 }
6150
6151 /* use the temporary set as the current one */
6152 ret_set.ctx_pos = set->ctx_pos;
6153 ret_set.ctx_size = set->ctx_size;
Michal Vaskod3678892020-05-21 10:06:58 +02006154 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006155 memcpy(set, &ret_set, sizeof *set);
6156
6157 return LY_SUCCESS;
6158}
6159
6160/**
6161 * @brief Move context schema @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_SCNODE_SET
6162 * (or LYXP_SET_EMPTY).
6163 *
6164 * @param[in,out] set Set to use.
6165 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6166 * @param[in] options XPath options.
6167 * @return LY_ERR
6168 */
6169static LY_ERR
6170moveto_scnode_self(struct lyxp_set *set, int all_desc, int options)
6171{
Michal Vasko519fd602020-05-26 12:17:39 +02006172 int getnext_opts;
6173 uint32_t i, mod_idx;
6174 const struct lysc_node *iter, *start_parent;
6175 const struct lys_module *mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006176
Michal Vaskod3678892020-05-21 10:06:58 +02006177 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006178 return LY_SUCCESS;
6179 }
6180
6181 if (set->type != LYXP_SET_SCNODE_SET) {
Michal Vaskof6e51882019-12-16 09:59:45 +01006182 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 +02006183 return LY_EVALID;
6184 }
6185
6186 /* nothing to do */
6187 if (!all_desc) {
6188 return LY_SUCCESS;
6189 }
6190
Michal Vasko519fd602020-05-26 12:17:39 +02006191 /* getnext opts */
6192 getnext_opts = LYS_GETNEXT_NOSTATECHECK;
6193 if (options & LYXP_SCNODE_OUTPUT) {
6194 getnext_opts |= LYS_GETNEXT_OUTPUT;
6195 }
6196
6197 /* add all the children, recursively as they are being added into the same set */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006198 for (i = 0; i < set->used; ++i) {
6199 if (set->val.scnodes[i].in_ctx != 1) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006200 if (set->val.scnodes[i].in_ctx != -2) {
6201 continue;
6202 }
6203
Michal Vasko519fd602020-05-26 12:17:39 +02006204 /* remember context node */
6205 set->val.scnodes[i].in_ctx = -1;
6206 } else {
6207 set->val.scnodes[i].in_ctx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006208 }
6209
Michal Vasko519fd602020-05-26 12:17:39 +02006210 start_parent = set->val.scnodes[i].scnode;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006211
Michal Vasko519fd602020-05-26 12:17:39 +02006212 if ((set->val.scnodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.scnodes[i].type == LYXP_NODE_ROOT)) {
6213 /* it can actually be in any module, it's all <running> */
6214 mod_idx = 0;
6215 while ((mod = (struct lys_module *)ly_ctx_get_module_iter(set->ctx, &mod_idx))) {
6216 iter = NULL;
6217 /* module may not be implemented */
6218 while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) {
6219 /* context check */
6220 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) {
6221 continue;
6222 }
6223
6224 lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM);
6225 /* throw away the insert index, we want to consider that node again, recursively */
6226 }
6227 }
6228
6229 } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
6230 iter = NULL;
6231 while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006232 /* context check */
Michal Vasko519fd602020-05-26 12:17:39 +02006233 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006234 continue;
6235 }
6236
Michal Vasko519fd602020-05-26 12:17:39 +02006237 lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006238 }
6239 }
6240 }
6241
6242 return LY_SUCCESS;
6243}
6244
6245/**
6246 * @brief Move context @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_NODE_SET
6247 * (or LYXP_SET_EMPTY). Context position aware.
6248 *
6249 * @param[in] set Set to use.
6250 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6251 * @param[in] options XPath options.
6252 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6253 */
6254static LY_ERR
6255moveto_parent(struct lyxp_set *set, int all_desc, int options)
6256{
6257 LY_ERR rc;
6258 uint32_t i;
6259 struct lyd_node *node, *new_node;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006260 enum lyxp_node_type new_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006261
Michal Vaskod3678892020-05-21 10:06:58 +02006262 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006263 return LY_SUCCESS;
6264 }
6265
6266 if (set->type != LYXP_SET_NODE_SET) {
6267 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
6268 return LY_EVALID;
6269 }
6270
6271 if (all_desc) {
6272 /* <path>//.. == <path>//./.. */
6273 rc = moveto_self(set, 1, options);
6274 LY_CHECK_RET(rc);
6275 }
6276
Michal Vasko57eab132019-09-24 11:46:26 +02006277 for (i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006278 node = set->val.nodes[i].node;
6279
6280 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
6281 new_node = (struct lyd_node *)node->parent;
6282 } else if (set->val.nodes[i].type == LYXP_NODE_TEXT) {
6283 new_node = node;
Michal Vasko9f96a052020-03-10 09:41:45 +01006284 } else if (set->val.nodes[i].type == LYXP_NODE_META) {
6285 new_node = set->val.meta[i].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006286 if (!new_node) {
6287 LOGINT_RET(set->ctx);
6288 }
6289 } else {
6290 /* root does not have a parent */
Michal Vasko2caefc12019-11-14 16:07:56 +01006291 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006292 continue;
6293 }
6294
Michal Vaskoa1424542019-11-14 16:08:52 +01006295 /* when check */
6296 if (moveto_when_check(new_node)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006297 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01006298 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006299
6300 /* node already there can also be the root */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006301 if (!new_node) {
6302 new_type = set->root_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006303
6304 /* node has a standard parent (it can equal the root, it's not the root yet since they are fake) */
6305 } else {
6306 new_type = LYXP_NODE_ELEM;
6307 }
6308
Michal Vasko03ff5a72019-09-11 13:49:33 +02006309 if (set_dup_node_check(set, new_node, new_type, -1)) {
Michal Vasko2caefc12019-11-14 16:07:56 +01006310 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006311 } else {
6312 set_replace_node(set, new_node, 0, new_type, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006313 }
6314 }
6315
Michal Vasko2caefc12019-11-14 16:07:56 +01006316 set_remove_nodes_none(set);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006317 assert(!set_sort(set) && !set_sorted_dup_node_clean(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006318
6319 return LY_SUCCESS;
6320}
6321
6322/**
6323 * @brief Move context schema @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_SCNODE_SET
6324 * (or LYXP_SET_EMPTY).
6325 *
6326 * @param[in] set Set to use.
6327 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6328 * @param[in] options XPath options.
6329 * @return LY_ERR
6330 */
6331static LY_ERR
6332moveto_scnode_parent(struct lyxp_set *set, int all_desc, int options)
6333{
6334 int idx, i, orig_used, temp_ctx = 0;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006335 const struct lysc_node *node, *new_node;
6336 enum lyxp_node_type new_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006337 LY_ERR rc;
6338
Michal Vaskod3678892020-05-21 10:06:58 +02006339 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006340 return LY_SUCCESS;
6341 }
6342
6343 if (set->type != LYXP_SET_SCNODE_SET) {
Michal Vaskof6e51882019-12-16 09:59:45 +01006344 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 +02006345 return LY_EVALID;
6346 }
6347
6348 if (all_desc) {
6349 /* <path>//.. == <path>//./.. */
6350 rc = moveto_scnode_self(set, 1, options);
6351 LY_CHECK_RET(rc);
6352 }
6353
Michal Vasko03ff5a72019-09-11 13:49:33 +02006354 orig_used = set->used;
6355 for (i = 0; i < orig_used; ++i) {
6356 if (set->val.scnodes[i].in_ctx != 1) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006357 if (set->val.scnodes[i].in_ctx != -2) {
6358 continue;
6359 }
6360
6361 /* remember context node */
6362 set->val.scnodes[i].in_ctx = -1;
Michal Vaskoec4df482019-12-16 10:02:18 +01006363 } else {
6364 set->val.scnodes[i].in_ctx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006365 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006366
6367 node = set->val.scnodes[i].scnode;
6368
6369 if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
Michal Vaskod3678892020-05-21 10:06:58 +02006370 new_node = lysc_data_parent(node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006371 } else {
6372 /* root does not have a parent */
6373 continue;
6374 }
6375
Michal Vasko03ff5a72019-09-11 13:49:33 +02006376 /* node has no parent */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006377 if (!new_node) {
6378 new_type = set->root_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006379
6380 /* node has a standard parent (it can equal the root, it's not the root yet since they are fake) */
6381 } else {
6382 new_type = LYXP_NODE_ELEM;
6383 }
6384
Michal Vaskoecd62de2019-11-13 12:35:11 +01006385 idx = lyxp_set_scnode_insert_node(set, new_node, new_type);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006386 if ((idx < orig_used) && (idx > i)) {
6387 set->val.scnodes[idx].in_ctx = 2;
6388 temp_ctx = 1;
6389 }
6390 }
6391
6392 if (temp_ctx) {
6393 for (i = 0; i < orig_used; ++i) {
6394 if (set->val.scnodes[i].in_ctx == 2) {
6395 set->val.scnodes[i].in_ctx = 1;
6396 }
6397 }
6398 }
6399
6400 return LY_SUCCESS;
6401}
6402
6403/**
6404 * @brief Move context @p set to the result of a comparison. Handles '=', '!=', '<=', '<', '>=', or '>'.
6405 * Result is LYXP_SET_BOOLEAN. Indirectly context position aware.
6406 *
6407 * @param[in,out] set1 Set to use for the result.
6408 * @param[in] set2 Set acting as the second operand for @p op.
6409 * @param[in] op Comparison operator to process.
6410 * @param[in] options XPath options.
6411 * @return LY_ERR
6412 */
6413static LY_ERR
6414moveto_op_comp(struct lyxp_set *set1, struct lyxp_set *set2, const char *op, int options)
6415{
6416 /*
6417 * NODE SET + NODE SET = NODE SET + STRING /(1 NODE SET) 2 STRING
6418 * NODE SET + STRING = STRING + STRING /1 STRING (2 STRING)
6419 * NODE SET + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6420 * NODE SET + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6421 * STRING + NODE SET = STRING + STRING /(1 STRING) 2 STRING
6422 * NUMBER + NODE SET = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6423 * BOOLEAN + NODE SET = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6424 *
6425 * '=' or '!='
6426 * BOOLEAN + BOOLEAN
6427 * BOOLEAN + STRING = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6428 * BOOLEAN + NUMBER = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6429 * STRING + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6430 * NUMBER + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6431 * NUMBER + NUMBER
6432 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6433 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6434 * STRING + STRING
6435 *
6436 * '<=', '<', '>=', '>'
6437 * NUMBER + NUMBER
6438 * BOOLEAN + BOOLEAN = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6439 * BOOLEAN + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6440 * BOOLEAN + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6441 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6442 * STRING + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6443 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6444 * NUMBER + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6445 * STRING + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6446 */
6447 struct lyxp_set iter1, iter2;
6448 int result;
6449 int64_t i;
6450 LY_ERR rc;
6451
Michal Vasko004d3152020-06-11 19:59:22 +02006452 memset(&iter1, 0, sizeof iter1);
6453 memset(&iter2, 0, sizeof iter2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006454
6455 /* iterative evaluation with node-sets */
6456 if ((set1->type == LYXP_SET_NODE_SET) || (set2->type == LYXP_SET_NODE_SET)) {
6457 if (set1->type == LYXP_SET_NODE_SET) {
6458 for (i = 0; i < set1->used; ++i) {
6459 switch (set2->type) {
6460 case LYXP_SET_NUMBER:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006461 rc = set_comp_cast(&iter1, set1, LYXP_SET_NUMBER, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006462 break;
6463 case LYXP_SET_BOOLEAN:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006464 rc = set_comp_cast(&iter1, set1, LYXP_SET_BOOLEAN, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006465 break;
6466 default:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006467 rc = set_comp_cast(&iter1, set1, LYXP_SET_STRING, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006468 break;
6469 }
6470 LY_CHECK_RET(rc);
6471
6472 rc = moveto_op_comp(&iter1, set2, op, options);
6473 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006474 lyxp_set_free_content(&iter1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006475 return rc;
6476 }
6477
6478 /* lazy evaluation until true */
Michal Vasko004d3152020-06-11 19:59:22 +02006479 if (iter1.val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006480 set_fill_boolean(set1, 1);
6481 return LY_SUCCESS;
6482 }
6483 }
6484 } else {
6485 for (i = 0; i < set2->used; ++i) {
6486 switch (set1->type) {
6487 case LYXP_SET_NUMBER:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006488 rc = set_comp_cast(&iter2, set2, LYXP_SET_NUMBER, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006489 break;
6490 case LYXP_SET_BOOLEAN:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006491 rc = set_comp_cast(&iter2, set2, LYXP_SET_BOOLEAN, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006492 break;
6493 default:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006494 rc = set_comp_cast(&iter2, set2, LYXP_SET_STRING, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006495 break;
6496 }
6497 LY_CHECK_RET(rc);
6498
6499 set_fill_set(&iter1, set1);
6500
6501 rc = moveto_op_comp(&iter1, &iter2, op, options);
6502 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006503 lyxp_set_free_content(&iter1);
6504 lyxp_set_free_content(&iter2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006505 return rc;
6506 }
Michal Vaskod3678892020-05-21 10:06:58 +02006507 lyxp_set_free_content(&iter2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006508
6509 /* lazy evaluation until true */
Michal Vasko004d3152020-06-11 19:59:22 +02006510 if (iter1.val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006511 set_fill_boolean(set1, 1);
6512 return LY_SUCCESS;
6513 }
6514 }
6515 }
6516
6517 /* false for all nodes */
6518 set_fill_boolean(set1, 0);
6519 return LY_SUCCESS;
6520 }
6521
6522 /* first convert properly */
6523 if ((op[0] == '=') || (op[0] == '!')) {
6524 if ((set1->type == LYXP_SET_BOOLEAN) || (set2->type == LYXP_SET_BOOLEAN)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006525 lyxp_set_cast(set1, LYXP_SET_BOOLEAN);
6526 lyxp_set_cast(set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006527 } else if ((set1->type == LYXP_SET_NUMBER) || (set2->type == LYXP_SET_NUMBER)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006528 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006529 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006530 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006531 LY_CHECK_RET(rc);
6532 } /* else we have 2 strings */
6533 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006534 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006535 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006536 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006537 LY_CHECK_RET(rc);
6538 }
6539
6540 assert(set1->type == set2->type);
6541
6542 /* compute result */
6543 if (op[0] == '=') {
6544 if (set1->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02006545 result = (set1->val.bln == set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006546 } else if (set1->type == LYXP_SET_NUMBER) {
6547 result = (set1->val.num == set2->val.num);
6548 } else {
6549 assert(set1->type == LYXP_SET_STRING);
Michal Vaskoac6c72f2019-11-14 16:09:34 +01006550 result = !strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006551 }
6552 } else if (op[0] == '!') {
6553 if (set1->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02006554 result = (set1->val.bln != set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006555 } else if (set1->type == LYXP_SET_NUMBER) {
6556 result = (set1->val.num != set2->val.num);
6557 } else {
6558 assert(set1->type == LYXP_SET_STRING);
Michal Vaskoac6c72f2019-11-14 16:09:34 +01006559 result = !strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006560 }
6561 } else {
6562 assert(set1->type == LYXP_SET_NUMBER);
6563 if (op[0] == '<') {
6564 if (op[1] == '=') {
6565 result = (set1->val.num <= set2->val.num);
6566 } else {
6567 result = (set1->val.num < set2->val.num);
6568 }
6569 } else {
6570 if (op[1] == '=') {
6571 result = (set1->val.num >= set2->val.num);
6572 } else {
6573 result = (set1->val.num > set2->val.num);
6574 }
6575 }
6576 }
6577
6578 /* assign result */
6579 if (result) {
6580 set_fill_boolean(set1, 1);
6581 } else {
6582 set_fill_boolean(set1, 0);
6583 }
6584
6585 return LY_SUCCESS;
6586}
6587
6588/**
6589 * @brief Move context @p set to the result of a basic operation. Handles '+', '-', unary '-', '*', 'div',
6590 * or 'mod'. Result is LYXP_SET_NUMBER. Indirectly context position aware.
6591 *
6592 * @param[in,out] set1 Set to use for the result.
6593 * @param[in] set2 Set acting as the second operand for @p op.
6594 * @param[in] op Operator to process.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006595 * @return LY_ERR
6596 */
6597static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006598moveto_op_math(struct lyxp_set *set1, struct lyxp_set *set2, const char *op)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006599{
6600 LY_ERR rc;
6601
6602 /* unary '-' */
6603 if (!set2 && (op[0] == '-')) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006604 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006605 LY_CHECK_RET(rc);
6606 set1->val.num *= -1;
6607 lyxp_set_free(set2);
6608 return LY_SUCCESS;
6609 }
6610
6611 assert(set1 && set2);
6612
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006613 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006614 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006615 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006616 LY_CHECK_RET(rc);
6617
6618 switch (op[0]) {
6619 /* '+' */
6620 case '+':
6621 set1->val.num += set2->val.num;
6622 break;
6623
6624 /* '-' */
6625 case '-':
6626 set1->val.num -= set2->val.num;
6627 break;
6628
6629 /* '*' */
6630 case '*':
6631 set1->val.num *= set2->val.num;
6632 break;
6633
6634 /* 'div' */
6635 case 'd':
6636 set1->val.num /= set2->val.num;
6637 break;
6638
6639 /* 'mod' */
6640 case 'm':
6641 set1->val.num = ((long long)set1->val.num) % ((long long)set2->val.num);
6642 break;
6643
6644 default:
6645 LOGINT_RET(set1->ctx);
6646 }
6647
6648 return LY_SUCCESS;
6649}
6650
6651/*
6652 * eval functions
6653 *
6654 * They execute a parsed XPath expression on some data subtree.
6655 */
6656
6657/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02006658 * @brief Evaluate Predicate. Logs directly on error.
6659 *
Michal Vaskod3678892020-05-21 10:06:58 +02006660 * [9] Predicate ::= '[' Expr ']'
Michal Vasko03ff5a72019-09-11 13:49:33 +02006661 *
6662 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02006663 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006664 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
6665 * @param[in] options XPath options.
6666 * @param[in] parent_pos_pred Whether parent predicate was a positional one.
6667 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6668 */
6669static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02006670eval_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 +02006671{
6672 LY_ERR rc;
Michal Vasko57eab132019-09-24 11:46:26 +02006673 uint16_t i, orig_exp;
Michal Vasko5c4e5892019-11-14 12:31:38 +01006674 uint32_t orig_pos, orig_size;
6675 int32_t pred_in_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006676 struct lyxp_set set2;
6677 struct lyd_node *orig_parent;
6678
6679 /* '[' */
6680 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02006681 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
6682 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006683
6684 if (!set) {
6685only_parse:
Michal Vasko004d3152020-06-11 19:59:22 +02006686 rc = eval_expr_select(exp, tok_idx, 0, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006687 LY_CHECK_RET(rc);
6688 } else if (set->type == LYXP_SET_NODE_SET) {
6689 /* 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 +01006690 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006691
6692 /* empty set, nothing to evaluate */
6693 if (!set->used) {
6694 goto only_parse;
6695 }
6696
Michal Vasko004d3152020-06-11 19:59:22 +02006697 orig_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006698 orig_pos = 0;
6699 orig_size = set->used;
6700 orig_parent = NULL;
Michal Vasko39dbf352020-05-21 10:08:59 +02006701 for (i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006702 set_init(&set2, set);
6703 set_insert_node(&set2, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, 0);
6704 /* remember the node context position for position() and context size for last(),
6705 * predicates should always be evaluated with respect to the child axis (since we do
6706 * not support explicit axes) so we assign positions based on their parents */
6707 if (parent_pos_pred && ((struct lyd_node *)set->val.nodes[i].node->parent != orig_parent)) {
6708 orig_parent = (struct lyd_node *)set->val.nodes[i].node->parent;
6709 orig_pos = 1;
6710 } else {
6711 ++orig_pos;
6712 }
6713
6714 set2.ctx_pos = orig_pos;
6715 set2.ctx_size = orig_size;
Michal Vasko004d3152020-06-11 19:59:22 +02006716 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006717
Michal Vasko004d3152020-06-11 19:59:22 +02006718 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006719 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006720 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006721 return rc;
6722 }
6723
6724 /* number is a position */
6725 if (set2.type == LYXP_SET_NUMBER) {
6726 if ((long long)set2.val.num == orig_pos) {
6727 set2.val.num = 1;
6728 } else {
6729 set2.val.num = 0;
6730 }
6731 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006732 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006733
6734 /* predicate satisfied or not? */
Michal Vasko004d3152020-06-11 19:59:22 +02006735 if (!set2.val.bln) {
Michal Vasko2caefc12019-11-14 16:07:56 +01006736 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006737 }
6738 }
Michal Vasko2caefc12019-11-14 16:07:56 +01006739 set_remove_nodes_none(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006740
6741 } else if (set->type == LYXP_SET_SCNODE_SET) {
6742 for (i = 0; i < set->used; ++i) {
6743 if (set->val.scnodes[i].in_ctx == 1) {
6744 /* there is a currently-valid node */
6745 break;
6746 }
6747 }
6748 /* empty set, nothing to evaluate */
6749 if (i == set->used) {
6750 goto only_parse;
6751 }
6752
Michal Vasko004d3152020-06-11 19:59:22 +02006753 orig_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006754
Michal Vasko03ff5a72019-09-11 13:49:33 +02006755 /* set special in_ctx to all the valid snodes */
6756 pred_in_ctx = set_scnode_new_in_ctx(set);
6757
6758 /* use the valid snodes one-by-one */
6759 for (i = 0; i < set->used; ++i) {
6760 if (set->val.scnodes[i].in_ctx != pred_in_ctx) {
6761 continue;
6762 }
6763 set->val.scnodes[i].in_ctx = 1;
6764
Michal Vasko004d3152020-06-11 19:59:22 +02006765 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006766
Michal Vasko004d3152020-06-11 19:59:22 +02006767 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006768 LY_CHECK_RET(rc);
6769
6770 set->val.scnodes[i].in_ctx = pred_in_ctx;
6771 }
6772
6773 /* restore the state as it was before the predicate */
6774 for (i = 0; i < set->used; ++i) {
6775 if (set->val.scnodes[i].in_ctx == 1) {
6776 set->val.scnodes[i].in_ctx = 0;
6777 } else if (set->val.scnodes[i].in_ctx == pred_in_ctx) {
6778 set->val.scnodes[i].in_ctx = 1;
6779 }
6780 }
6781
6782 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02006783 set2.type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006784 set_fill_set(&set2, set);
6785
Michal Vasko004d3152020-06-11 19:59:22 +02006786 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006787 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006788 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006789 return rc;
6790 }
6791
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006792 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02006793 if (!set2.val.bln) {
Michal Vaskod3678892020-05-21 10:06:58 +02006794 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006795 }
Michal Vaskod3678892020-05-21 10:06:58 +02006796 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006797 }
6798
6799 /* ']' */
Michal Vasko004d3152020-06-11 19:59:22 +02006800 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_BRACK2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006801 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02006802 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
6803 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006804
6805 return LY_SUCCESS;
6806}
6807
6808/**
Michal Vaskod3678892020-05-21 10:06:58 +02006809 * @brief Evaluate Literal. Logs directly on error.
6810 *
6811 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02006812 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02006813 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
6814 */
6815static void
Michal Vasko004d3152020-06-11 19:59:22 +02006816eval_literal(struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set)
Michal Vaskod3678892020-05-21 10:06:58 +02006817{
6818 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02006819 if (exp->tok_len[*tok_idx] == 2) {
Michal Vaskod3678892020-05-21 10:06:58 +02006820 set_fill_string(set, "", 0);
6821 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02006822 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 +02006823 }
6824 }
6825 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02006826 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
6827 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02006828}
6829
6830/**
Michal Vasko004d3152020-06-11 19:59:22 +02006831 * @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 +02006832 *
Michal Vasko004d3152020-06-11 19:59:22 +02006833 * @param[in] exp Full parsed XPath expression.
6834 * @param[in,out] tok_idx Index in @p exp at the beginning of the predicate, is updated on success.
6835 * @param[in] scnode Found schema node as context for the predicate.
6836 * @param[in] format Format of any prefixes in key names/values.
6837 * @param[out] predicates Parsed predicates.
6838 * @param[out] pred_type Type of @p predicates.
6839 * @return LY_SUCCESS on success,
6840 * @return LY_ERR on any error.
Michal Vaskod3678892020-05-21 10:06:58 +02006841 */
6842static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02006843eval_name_test_try_compile_predicates(struct lyxp_expr *exp, uint16_t *tok_idx, const struct lysc_node *scnode,
6844 LYD_FORMAT format, struct ly_path_predicate **predicates, enum ly_path_pred_type *pred_type)
Michal Vaskod3678892020-05-21 10:06:58 +02006845{
Michal Vasko004d3152020-06-11 19:59:22 +02006846 LY_ERR ret = LY_SUCCESS;
6847 uint16_t key_count, e_idx, pred_idx = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02006848 const struct lysc_node *key;
Michal Vasko004d3152020-06-11 19:59:22 +02006849 size_t pred_len;
6850 int prev_lo;
6851 struct lyxp_expr *exp2 = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02006852
Michal Vasko004d3152020-06-11 19:59:22 +02006853 assert(scnode->nodetype & (LYS_LIST | LYS_LEAFLIST));
Michal Vaskod3678892020-05-21 10:06:58 +02006854
Michal Vasko004d3152020-06-11 19:59:22 +02006855 if (scnode->nodetype == LYS_LIST) {
6856 /* get key count */
6857 if (scnode->flags & LYS_KEYLESS) {
6858 return LY_EINVAL;
6859 }
6860 for (key_count = 0, key = lysc_node_children(scnode, 0); key && (key->flags & LYS_KEY); key = key->next, ++key_count);
6861 assert(key_count);
Michal Vaskod3678892020-05-21 10:06:58 +02006862
Michal Vasko004d3152020-06-11 19:59:22 +02006863 /* learn where the predicates end */
6864 e_idx = *tok_idx;
6865 while (key_count) {
6866 /* '[' */
6867 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
6868 return LY_EINVAL;
6869 }
6870 ++e_idx;
6871
6872 /* ']' */
6873 while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
6874 ++e_idx;
6875 }
6876 ++e_idx;
6877
6878 /* another presumably key predicate parsed */
6879 --key_count;
6880 }
Michal Vasko004d3152020-06-11 19:59:22 +02006881 } else {
6882 /* learn just where this single predicate ends */
6883 e_idx = *tok_idx;
6884
Michal Vaskod3678892020-05-21 10:06:58 +02006885 /* '[' */
Michal Vasko004d3152020-06-11 19:59:22 +02006886 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
6887 return LY_EINVAL;
6888 }
6889 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02006890
6891 /* ']' */
Michal Vasko004d3152020-06-11 19:59:22 +02006892 while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
6893 ++e_idx;
6894 }
6895 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02006896 }
6897
Michal Vasko004d3152020-06-11 19:59:22 +02006898 /* get the joined length of all the keys predicates/of the single leaf-list predicate */
6899 pred_len = (exp->tok_pos[e_idx - 1] + exp->tok_len[e_idx - 1]) - exp->tok_pos[*tok_idx];
6900
6901 /* turn logging off */
6902 prev_lo = ly_log_options(0);
6903
6904 /* parse the predicate(s) */
6905 LY_CHECK_GOTO(ret = ly_path_parse_predicate(scnode->module->ctx, exp->expr + exp->tok_pos[*tok_idx], pred_len,
6906 LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &exp2), cleanup);
6907
6908 /* compile */
6909 switch (format) {
6910 case LYD_SCHEMA:
Michal Vasko00cbf532020-06-15 13:58:47 +02006911 ret = ly_path_compile_predicate(scnode->module->ctx, scnode->module, scnode, exp2, &pred_idx, lys_resolve_prefix,
6912 scnode->module, LYD_SCHEMA, predicates, pred_type);
Michal Vasko004d3152020-06-11 19:59:22 +02006913 break;
6914 case LYD_JSON:
Michal Vasko00cbf532020-06-15 13:58:47 +02006915 ret = ly_path_compile_predicate(scnode->module->ctx, scnode->module, scnode, exp2, &pred_idx,
6916 lydjson_resolve_prefix, NULL, LYD_JSON, predicates, pred_type);
Michal Vasko004d3152020-06-11 19:59:22 +02006917 break;
6918 case LYD_XML:
Michal Vasko60ea6352020-06-29 13:39:39 +02006919 case LYD_LYB:
Michal Vasko004d3152020-06-11 19:59:22 +02006920 ret = LY_EINT;
6921 goto cleanup;
6922 }
6923 LY_CHECK_GOTO(ret, cleanup);
6924
6925 /* success, the predicate must include all the needed information for hash-based search */
6926 *tok_idx = e_idx;
6927
6928cleanup:
6929 ly_log_options(prev_lo);
6930 lyxp_expr_free(scnode->module->ctx, exp2);
6931 return ret;
Michal Vaskod3678892020-05-21 10:06:58 +02006932}
6933
6934/**
6935 * @brief Evaluate NameTest and any following Predicates. Logs directly on error.
6936 *
6937 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
6938 * [6] NodeTest ::= NameTest | NodeType '(' ')'
6939 * [7] NameTest ::= '*' | NCName ':' '*' | QName
6940 *
6941 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02006942 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02006943 * @param[in] attr_axis Whether to search attributes or standard nodes.
6944 * @param[in] all_desc Whether to search all the descendants or children only.
6945 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
6946 * @param[in] options XPath options.
6947 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6948 */
6949static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02006950eval_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 +02006951 int options)
6952{
6953 int i;
6954 char *path;
Michal Vasko004d3152020-06-11 19:59:22 +02006955 const char *ncname, *ncname_dict = NULL;
6956 uint16_t ncname_len;
Michal Vaskod3678892020-05-21 10:06:58 +02006957 const struct lys_module *moveto_mod;
6958 const struct lysc_node *scnode = NULL, *tmp;
Michal Vasko004d3152020-06-11 19:59:22 +02006959 struct ly_path_predicate *predicates = NULL;
6960 enum ly_path_pred_type pred_type = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02006961 LY_ERR rc = LY_SUCCESS;
6962
6963 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02006964 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
6965 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02006966
6967 if (!set) {
6968 goto moveto;
6969 }
6970
Michal Vasko004d3152020-06-11 19:59:22 +02006971 ncname = &exp->expr[exp->tok_pos[*tok_idx - 1]];
6972 ncname_len = exp->tok_len[*tok_idx - 1];
Michal Vaskod3678892020-05-21 10:06:58 +02006973
6974 /* parse (and skip) module name */
6975 rc = moveto_resolve_model(&ncname, &ncname_len, set, &moveto_mod);
6976 LY_CHECK_GOTO(rc, cleanup);
6977
6978 if (moveto_mod && !attr_axis && !all_desc && (set->type == LYXP_SET_NODE_SET)) {
6979 /* find the matching schema node in some parent in the context */
6980 for (i = 0; i < (signed)set->used; ++i) {
6981 if (set->val.nodes[i].type == set->root_type) {
6982 tmp = lys_find_child(NULL, moveto_mod, ncname, ncname_len, 0, 0);
6983 } else if ((set->val.nodes[i].type == LYXP_NODE_ELEM)
6984 && (!scnode || (lysc_data_parent(scnode) != set->val.nodes[i].node->schema))) {
6985 /* do not repeat the same search */
6986 tmp = lys_find_child(set->val.nodes[i].node->schema, moveto_mod, ncname, ncname_len, 0, 0);
Radek Krejcib1247842020-07-02 16:22:38 +02006987 } else {
6988 tmp = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02006989 }
6990
6991 /* additional context check */
6992 if (tmp && (set->root_type == LYXP_NODE_ROOT_CONFIG) && (tmp->flags & LYS_CONFIG_R)) {
6993 tmp = NULL;
6994 }
6995
6996 if (tmp) {
6997 if (scnode) {
6998 /* we found a schema node with the same name but at different level, give up, too complicated */
6999 scnode = NULL;
7000 break;
7001 } else {
7002 /* remember the found schema node and continue to make sure it can be used */
7003 scnode = tmp;
7004 }
Michal Vaskod3678892020-05-21 10:06:58 +02007005 }
7006 }
7007
Michal Vasko004d3152020-06-11 19:59:22 +02007008 if (scnode && (scnode->nodetype & (LYS_LIST | LYS_LEAFLIST))) {
7009 /* try to create the predicates */
7010 if (eval_name_test_try_compile_predicates(exp, tok_idx, scnode, set->format, &predicates, &pred_type)) {
7011 /* hashes cannot be used */
Michal Vaskod3678892020-05-21 10:06:58 +02007012 scnode = NULL;
7013 }
7014 }
7015 }
7016
Michal Vasko004d3152020-06-11 19:59:22 +02007017 if (!scnode && moveto_mod) {
7018 /* we are not able to match based on a schema node and not all the modules match,
7019 * use dictionary for efficient comparison */
7020 ncname_dict = lydict_insert(set->ctx, ncname, ncname_len);
Michal Vaskod3678892020-05-21 10:06:58 +02007021 }
7022
7023moveto:
7024 /* move to the attribute(s), data node(s), or schema node(s) */
7025 if (attr_axis) {
7026 if (set && (options & LYXP_SCNODE_ALL)) {
7027 set_scnode_clear_ctx(set);
7028 } else {
7029 if (all_desc) {
Michal Vasko004d3152020-06-11 19:59:22 +02007030 rc = moveto_attr_alldesc(set, moveto_mod, ncname_dict);
Michal Vaskod3678892020-05-21 10:06:58 +02007031 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007032 rc = moveto_attr(set, moveto_mod, ncname_dict);
Michal Vaskod3678892020-05-21 10:06:58 +02007033 }
7034 LY_CHECK_GOTO(rc, cleanup);
7035 }
7036 } else {
7037 if (set && (options & LYXP_SCNODE_ALL)) {
7038 if (all_desc) {
Michal Vasko004d3152020-06-11 19:59:22 +02007039 rc = moveto_scnode_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007040 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007041 rc = moveto_scnode(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007042 }
7043 LY_CHECK_GOTO(rc, cleanup);
7044
7045 for (i = set->used - 1; i > -1; --i) {
7046 if (set->val.scnodes[i].in_ctx > 0) {
7047 break;
7048 }
7049 }
7050 if (i == -1) {
7051 path = lysc_path(set->ctx_scnode, LYSC_PATH_LOG, NULL, 0);
7052 LOGWRN(set->ctx, "Schema node \"%.*s\" not found (%.*s) with context node \"%s\".",
Michal Vasko004d3152020-06-11 19:59:22 +02007053 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]],
7054 exp->tok_pos[*tok_idx] + exp->tok_len[*tok_idx], exp->expr, path);
Michal Vaskod3678892020-05-21 10:06:58 +02007055 free(path);
7056 }
7057 } else {
7058 if (all_desc) {
Michal Vasko004d3152020-06-11 19:59:22 +02007059 rc = moveto_node_alldesc(set, moveto_mod, ncname_dict);
Michal Vaskod3678892020-05-21 10:06:58 +02007060 } else {
7061 if (scnode) {
7062 /* we can find the nodes using hashes */
Michal Vasko004d3152020-06-11 19:59:22 +02007063 rc = moveto_node_hash(set, scnode, predicates);
Michal Vaskod3678892020-05-21 10:06:58 +02007064 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007065 rc = moveto_node(set, moveto_mod, ncname_dict);
Michal Vaskod3678892020-05-21 10:06:58 +02007066 }
7067 }
7068 LY_CHECK_GOTO(rc, cleanup);
7069 }
7070 }
7071
7072 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007073 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7074 rc = eval_predicate(exp, tok_idx, set, options, 1);
Michal Vaskod3678892020-05-21 10:06:58 +02007075 LY_CHECK_RET(rc);
7076 }
7077
7078cleanup:
Michal Vaskodb51a8d2020-05-27 15:22:29 +02007079 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007080 lydict_remove(set->ctx, ncname_dict);
7081 ly_path_predicates_free(set->ctx, pred_type, scnode, predicates);
Michal Vaskodb51a8d2020-05-27 15:22:29 +02007082 }
Michal Vaskod3678892020-05-21 10:06:58 +02007083 return rc;
7084}
7085
7086/**
7087 * @brief Evaluate NodeType and any following Predicates. Logs directly on error.
7088 *
7089 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
7090 * [6] NodeTest ::= NameTest | NodeType '(' ')'
7091 * [8] NodeType ::= 'text' | 'node'
7092 *
7093 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007094 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02007095 * @param[in] attr_axis Whether to search attributes or standard nodes.
7096 * @param[in] all_desc Whether to search all the descendants or children only.
7097 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7098 * @param[in] options XPath options.
7099 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7100 */
7101static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007102eval_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 +02007103 struct lyxp_set *set, int options)
7104{
7105 LY_ERR rc;
7106
7107 /* TODO */
7108 (void)attr_axis;
7109 (void)all_desc;
7110
7111 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007112 assert(exp->tok_len[*tok_idx] == 4);
Michal Vaskod3678892020-05-21 10:06:58 +02007113 if (set->type == LYXP_SET_SCNODE_SET) {
7114 set_scnode_clear_ctx(set);
7115 /* just for the debug message below */
7116 set = NULL;
7117 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007118 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "node", 4)) {
Michal Vaskod3678892020-05-21 10:06:58 +02007119 rc = xpath_node(NULL, 0, set, options);
7120 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007121 assert(!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "text", 4));
Michal Vaskod3678892020-05-21 10:06:58 +02007122 rc = xpath_text(NULL, 0, set, options);
7123 }
7124 LY_CHECK_RET(rc);
7125 }
7126 }
7127 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007128 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7129 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007130
7131 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02007132 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
Michal Vaskod3678892020-05-21 10:06:58 +02007133 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007134 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7135 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007136
7137 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007138 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
Michal Vaskod3678892020-05-21 10:06:58 +02007139 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007140 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7141 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007142
7143 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007144 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7145 rc = eval_predicate(exp, tok_idx, set, options, 1);
Michal Vaskod3678892020-05-21 10:06:58 +02007146 LY_CHECK_RET(rc);
7147 }
7148
7149 return LY_SUCCESS;
7150}
7151
7152/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02007153 * @brief Evaluate RelativeLocationPath. Logs directly on error.
7154 *
7155 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
7156 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
Michal Vaskod3678892020-05-21 10:06:58 +02007157 * [6] NodeTest ::= NameTest | NodeType '(' ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007158 *
7159 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007160 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007161 * @param[in] all_desc Whether to search all the descendants or children only.
7162 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7163 * @param[in] options XPath options.
7164 * @return LY_ERR (YL_EINCOMPLETE on unresolved when)
7165 */
7166static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007167eval_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 +02007168{
7169 int attr_axis;
7170 LY_ERR rc;
7171
7172 goto step;
7173 do {
7174 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02007175 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007176 all_desc = 0;
7177 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007178 assert(exp->tok_len[*tok_idx] == 2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007179 all_desc = 1;
7180 }
7181 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007182 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7183 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007184
7185step:
Michal Vaskod3678892020-05-21 10:06:58 +02007186 /* evaluate abbreviated axis '@'? if any */
Michal Vasko004d3152020-06-11 19:59:22 +02007187 if (exp->tokens[*tok_idx] == LYXP_TOKEN_AT) {
Michal Vaskod3678892020-05-21 10:06:58 +02007188 attr_axis = 1;
7189
7190 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007191 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7192 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007193 } else {
7194 attr_axis = 0;
7195 }
7196
Michal Vasko03ff5a72019-09-11 13:49:33 +02007197 /* Step */
Michal Vasko004d3152020-06-11 19:59:22 +02007198 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007199 case LYXP_TOKEN_DOT:
7200 /* evaluate '.' */
7201 if (set && (options & LYXP_SCNODE_ALL)) {
7202 rc = moveto_scnode_self(set, all_desc, options);
7203 } else {
7204 rc = moveto_self(set, all_desc, options);
7205 }
7206 LY_CHECK_RET(rc);
7207 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007208 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7209 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007210 break;
7211
7212 case LYXP_TOKEN_DDOT:
7213 /* evaluate '..' */
7214 if (set && (options & LYXP_SCNODE_ALL)) {
7215 rc = moveto_scnode_parent(set, all_desc, options);
7216 } else {
7217 rc = moveto_parent(set, all_desc, options);
7218 }
7219 LY_CHECK_RET(rc);
7220 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007221 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7222 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007223 break;
7224
Michal Vasko03ff5a72019-09-11 13:49:33 +02007225 case LYXP_TOKEN_NAMETEST:
Michal Vaskod3678892020-05-21 10:06:58 +02007226 /* evaluate NameTest Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007227 rc = eval_name_test_with_predicate(exp, tok_idx, attr_axis, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007228 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02007229 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007230
Michal Vaskod3678892020-05-21 10:06:58 +02007231 case LYXP_TOKEN_NODETYPE:
7232 /* evaluate NodeType Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007233 rc = eval_node_type_with_predicate(exp, tok_idx, attr_axis, all_desc, set, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007234 LY_CHECK_RET(rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007235 break;
7236
7237 default:
Michal Vasko02a77382019-09-12 11:47:35 +02007238 LOGINT_RET(set ? set->ctx : NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007239 }
Michal Vasko004d3152020-06-11 19:59:22 +02007240 } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007241
7242 return LY_SUCCESS;
7243}
7244
7245/**
7246 * @brief Evaluate AbsoluteLocationPath. Logs directly on error.
7247 *
7248 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
7249 *
7250 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007251 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007252 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7253 * @param[in] options XPath options.
7254 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7255 */
7256static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007257eval_absolute_location_path(struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set, int options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007258{
7259 int all_desc;
7260 LY_ERR rc;
7261
7262 if (set) {
7263 /* no matter what tokens follow, we need to be at the root */
7264 moveto_root(set, options);
7265 }
7266
7267 /* '/' RelativeLocationPath? */
Michal Vasko004d3152020-06-11 19:59:22 +02007268 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007269 /* evaluate '/' - deferred */
7270 all_desc = 0;
7271 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007272 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7273 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007274
Michal Vasko004d3152020-06-11 19:59:22 +02007275 if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007276 return LY_SUCCESS;
7277 }
Michal Vasko004d3152020-06-11 19:59:22 +02007278 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007279 case LYXP_TOKEN_DOT:
7280 case LYXP_TOKEN_DDOT:
7281 case LYXP_TOKEN_AT:
7282 case LYXP_TOKEN_NAMETEST:
7283 case LYXP_TOKEN_NODETYPE:
Michal Vasko004d3152020-06-11 19:59:22 +02007284 rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007285 LY_CHECK_RET(rc);
7286 break;
7287 default:
7288 break;
7289 }
7290
7291 /* '//' RelativeLocationPath */
7292 } else {
7293 /* evaluate '//' - deferred so as not to waste memory by remembering all the nodes */
7294 all_desc = 1;
7295 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007296 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7297 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007298
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 }
7302
7303 return LY_SUCCESS;
7304}
7305
7306/**
7307 * @brief Evaluate FunctionCall. Logs directly on error.
7308 *
Michal Vaskod3678892020-05-21 10:06:58 +02007309 * [11] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007310 *
7311 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007312 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007313 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7314 * @param[in] options XPath options.
7315 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7316 */
7317static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007318eval_function_call(struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set, int options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007319{
7320 LY_ERR rc;
7321 LY_ERR (*xpath_func)(struct lyxp_set **, uint16_t, struct lyxp_set *, int) = NULL;
Michal Vasko0cbf54f2019-12-16 10:01:06 +01007322 uint16_t arg_count = 0, i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007323 struct lyxp_set **args = NULL, **args_aux;
7324
7325 if (set) {
7326 /* FunctionName */
Michal Vasko004d3152020-06-11 19:59:22 +02007327 switch (exp->tok_len[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007328 case 3:
Michal Vasko004d3152020-06-11 19:59:22 +02007329 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007330 xpath_func = &xpath_not;
Michal Vasko004d3152020-06-11 19:59:22 +02007331 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007332 xpath_func = &xpath_sum;
7333 }
7334 break;
7335 case 4:
Michal Vasko004d3152020-06-11 19:59:22 +02007336 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007337 xpath_func = &xpath_lang;
Michal Vasko004d3152020-06-11 19:59:22 +02007338 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007339 xpath_func = &xpath_last;
Michal Vasko004d3152020-06-11 19:59:22 +02007340 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007341 xpath_func = &xpath_name;
Michal Vasko004d3152020-06-11 19:59:22 +02007342 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007343 xpath_func = &xpath_true;
7344 }
7345 break;
7346 case 5:
Michal Vasko004d3152020-06-11 19:59:22 +02007347 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007348 xpath_func = &xpath_count;
Michal Vasko004d3152020-06-11 19:59:22 +02007349 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007350 xpath_func = &xpath_false;
Michal Vasko004d3152020-06-11 19:59:22 +02007351 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007352 xpath_func = &xpath_floor;
Michal Vasko004d3152020-06-11 19:59:22 +02007353 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007354 xpath_func = &xpath_round;
Michal Vasko004d3152020-06-11 19:59:22 +02007355 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007356 xpath_func = &xpath_deref;
7357 }
7358 break;
7359 case 6:
Michal Vasko004d3152020-06-11 19:59:22 +02007360 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007361 xpath_func = &xpath_concat;
Michal Vasko004d3152020-06-11 19:59:22 +02007362 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007363 xpath_func = &xpath_number;
Michal Vasko004d3152020-06-11 19:59:22 +02007364 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007365 xpath_func = &xpath_string;
7366 }
7367 break;
7368 case 7:
Michal Vasko004d3152020-06-11 19:59:22 +02007369 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007370 xpath_func = &xpath_boolean;
Michal Vasko004d3152020-06-11 19:59:22 +02007371 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007372 xpath_func = &xpath_ceiling;
Michal Vasko004d3152020-06-11 19:59:22 +02007373 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007374 xpath_func = &xpath_current;
7375 }
7376 break;
7377 case 8:
Michal Vasko004d3152020-06-11 19:59:22 +02007378 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007379 xpath_func = &xpath_contains;
Michal Vasko004d3152020-06-11 19:59:22 +02007380 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007381 xpath_func = &xpath_position;
Michal Vasko004d3152020-06-11 19:59:22 +02007382 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007383 xpath_func = &xpath_re_match;
7384 }
7385 break;
7386 case 9:
Michal Vasko004d3152020-06-11 19:59:22 +02007387 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007388 xpath_func = &xpath_substring;
Michal Vasko004d3152020-06-11 19:59:22 +02007389 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007390 xpath_func = &xpath_translate;
7391 }
7392 break;
7393 case 10:
Michal Vasko004d3152020-06-11 19:59:22 +02007394 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007395 xpath_func = &xpath_local_name;
Michal Vasko004d3152020-06-11 19:59:22 +02007396 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007397 xpath_func = &xpath_enum_value;
Michal Vasko004d3152020-06-11 19:59:22 +02007398 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007399 xpath_func = &xpath_bit_is_set;
7400 }
7401 break;
7402 case 11:
Michal Vasko004d3152020-06-11 19:59:22 +02007403 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007404 xpath_func = &xpath_starts_with;
7405 }
7406 break;
7407 case 12:
Michal Vasko004d3152020-06-11 19:59:22 +02007408 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007409 xpath_func = &xpath_derived_from;
7410 }
7411 break;
7412 case 13:
Michal Vasko004d3152020-06-11 19:59:22 +02007413 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007414 xpath_func = &xpath_namespace_uri;
Michal Vasko004d3152020-06-11 19:59:22 +02007415 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007416 xpath_func = &xpath_string_length;
7417 }
7418 break;
7419 case 15:
Michal Vasko004d3152020-06-11 19:59:22 +02007420 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007421 xpath_func = &xpath_normalize_space;
Michal Vasko004d3152020-06-11 19:59:22 +02007422 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007423 xpath_func = &xpath_substring_after;
7424 }
7425 break;
7426 case 16:
Michal Vasko004d3152020-06-11 19:59:22 +02007427 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007428 xpath_func = &xpath_substring_before;
7429 }
7430 break;
7431 case 20:
Michal Vasko004d3152020-06-11 19:59:22 +02007432 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007433 xpath_func = &xpath_derived_from_or_self;
7434 }
7435 break;
7436 }
7437
7438 if (!xpath_func) {
Michal Vasko004d3152020-06-11 19:59:22 +02007439 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 +02007440 return LY_EVALID;
7441 }
7442 }
7443
7444 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007445 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7446 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007447
7448 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02007449 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007450 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007451 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7452 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007453
7454 /* ( Expr ( ',' Expr )* )? */
Michal Vasko004d3152020-06-11 19:59:22 +02007455 if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007456 if (set) {
7457 args = malloc(sizeof *args);
7458 LY_CHECK_ERR_GOTO(!args, LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7459 arg_count = 1;
7460 args[0] = set_copy(set);
7461 if (!args[0]) {
7462 rc = LY_EMEM;
7463 goto cleanup;
7464 }
7465
Michal Vasko004d3152020-06-11 19:59:22 +02007466 rc = eval_expr_select(exp, tok_idx, 0, args[0], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007467 LY_CHECK_GOTO(rc, cleanup);
7468 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007469 rc = eval_expr_select(exp, tok_idx, 0, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007470 LY_CHECK_GOTO(rc, cleanup);
7471 }
7472 }
Michal Vasko004d3152020-06-11 19:59:22 +02007473 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007474 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007475 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7476 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007477
7478 if (set) {
7479 ++arg_count;
7480 args_aux = realloc(args, arg_count * sizeof *args);
7481 LY_CHECK_ERR_GOTO(!args_aux, arg_count--; LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7482 args = args_aux;
7483 args[arg_count - 1] = set_copy(set);
7484 if (!args[arg_count - 1]) {
7485 rc = LY_EMEM;
7486 goto cleanup;
7487 }
7488
Michal Vasko004d3152020-06-11 19:59:22 +02007489 rc = eval_expr_select(exp, tok_idx, 0, args[arg_count - 1], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007490 LY_CHECK_GOTO(rc, cleanup);
7491 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007492 rc = eval_expr_select(exp, tok_idx, 0, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007493 LY_CHECK_GOTO(rc, cleanup);
7494 }
7495 }
7496
7497 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007498 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007499 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007500 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7501 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007502
7503 if (set) {
7504 /* evaluate function */
7505 rc = xpath_func(args, arg_count, set, options);
7506
7507 if (options & LYXP_SCNODE_ALL) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007508 /* merge all nodes from arg evaluations */
7509 for (i = 0; i < arg_count; ++i) {
7510 set_scnode_clear_ctx(args[i]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007511 lyxp_set_scnode_merge(set, args[i]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007512 }
7513 }
7514 } else {
7515 rc = LY_SUCCESS;
7516 }
7517
7518cleanup:
7519 for (i = 0; i < arg_count; ++i) {
7520 lyxp_set_free(args[i]);
7521 }
7522 free(args);
7523
7524 return rc;
7525}
7526
7527/**
7528 * @brief Evaluate Number. Logs directly on error.
7529 *
7530 * @param[in] ctx Context for errors.
7531 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007532 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007533 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7534 * @return LY_ERR
7535 */
7536static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007537eval_number(struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007538{
7539 long double num;
7540 char *endptr;
7541
7542 if (set) {
7543 errno = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02007544 num = strtold(&exp->expr[exp->tok_pos[*tok_idx]], &endptr);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007545 if (errno) {
Michal Vasko004d3152020-06-11 19:59:22 +02007546 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 +02007547 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 +02007548 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]], strerror(errno));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007549 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02007550 } else if (endptr - &exp->expr[exp->tok_pos[*tok_idx]] != exp->tok_len[*tok_idx]) {
7551 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 +02007552 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 +02007553 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007554 return LY_EVALID;
7555 }
7556
7557 set_fill_number(set, num);
7558 }
7559
7560 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007561 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7562 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007563 return LY_SUCCESS;
7564}
7565
7566/**
7567 * @brief Evaluate PathExpr. Logs directly on error.
7568 *
Michal Vaskod3678892020-05-21 10:06:58 +02007569 * [12] PathExpr ::= LocationPath | PrimaryExpr Predicate*
Michal Vasko03ff5a72019-09-11 13:49:33 +02007570 * | PrimaryExpr Predicate* '/' RelativeLocationPath
7571 * | PrimaryExpr Predicate* '//' RelativeLocationPath
7572 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
Michal Vaskod3678892020-05-21 10:06:58 +02007573 * [10] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall
Michal Vasko03ff5a72019-09-11 13:49:33 +02007574 *
7575 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007576 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007577 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7578 * @param[in] options XPath options.
7579 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7580 */
7581static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007582eval_path_expr(struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set, int options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007583{
7584 int all_desc, parent_pos_pred;
7585 LY_ERR rc;
7586
Michal Vasko004d3152020-06-11 19:59:22 +02007587 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007588 case LYXP_TOKEN_PAR1:
7589 /* '(' Expr ')' */
7590
7591 /* '(' */
7592 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007593 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7594 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007595
7596 /* Expr */
Michal Vasko004d3152020-06-11 19:59:22 +02007597 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007598 LY_CHECK_RET(rc);
7599
7600 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007601 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007602 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007603 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7604 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007605
7606 parent_pos_pred = 0;
7607 goto predicate;
7608
7609 case LYXP_TOKEN_DOT:
7610 case LYXP_TOKEN_DDOT:
7611 case LYXP_TOKEN_AT:
7612 case LYXP_TOKEN_NAMETEST:
7613 case LYXP_TOKEN_NODETYPE:
7614 /* RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007615 rc = eval_relative_location_path(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007616 LY_CHECK_RET(rc);
7617 break;
7618
7619 case LYXP_TOKEN_FUNCNAME:
7620 /* FunctionCall */
7621 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007622 rc = eval_function_call(exp, tok_idx, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007623 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007624 rc = eval_function_call(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007625 }
7626 LY_CHECK_RET(rc);
7627
7628 parent_pos_pred = 1;
7629 goto predicate;
7630
Michal Vasko3e48bf32020-06-01 08:39:07 +02007631 case LYXP_TOKEN_OPER_PATH:
7632 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02007633 /* AbsoluteLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007634 rc = eval_absolute_location_path(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007635 LY_CHECK_RET(rc);
7636 break;
7637
7638 case LYXP_TOKEN_LITERAL:
7639 /* Literal */
7640 if (!set || (options & LYXP_SCNODE_ALL)) {
7641 if (set) {
7642 set_scnode_clear_ctx(set);
7643 }
Michal Vasko004d3152020-06-11 19:59:22 +02007644 eval_literal(exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007645 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007646 eval_literal(exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007647 }
7648
7649 parent_pos_pred = 1;
7650 goto predicate;
7651
7652 case LYXP_TOKEN_NUMBER:
7653 /* Number */
7654 if (!set || (options & LYXP_SCNODE_ALL)) {
7655 if (set) {
7656 set_scnode_clear_ctx(set);
7657 }
Michal Vasko004d3152020-06-11 19:59:22 +02007658 rc = eval_number(NULL, exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007659 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007660 rc = eval_number(set->ctx, exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007661 }
7662 LY_CHECK_RET(rc);
7663
7664 parent_pos_pred = 1;
7665 goto predicate;
7666
7667 default:
Michal Vasko004d3152020-06-11 19:59:22 +02007668 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INTOK, lyxp_print_token(exp->tokens[*tok_idx]),
7669 &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007670 return LY_EVALID;
7671 }
7672
7673 return LY_SUCCESS;
7674
7675predicate:
7676 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007677 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7678 rc = eval_predicate(exp, tok_idx, set, options, parent_pos_pred);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007679 LY_CHECK_RET(rc);
7680 }
7681
7682 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007683 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007684
7685 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02007686 if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007687 all_desc = 0;
7688 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007689 all_desc = 1;
7690 }
7691
7692 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007693 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7694 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007695
Michal Vasko004d3152020-06-11 19:59:22 +02007696 rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007697 LY_CHECK_RET(rc);
7698 }
7699
7700 return LY_SUCCESS;
7701}
7702
7703/**
7704 * @brief Evaluate UnionExpr. Logs directly on error.
7705 *
Michal Vaskod3678892020-05-21 10:06:58 +02007706 * [20] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007707 *
7708 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007709 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007710 * @param[in] repeat How many times this expression is repeated.
7711 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7712 * @param[in] options XPath options.
7713 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7714 */
7715static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007716eval_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 +02007717{
7718 LY_ERR rc = LY_SUCCESS;
7719 struct lyxp_set orig_set, set2;
7720 uint16_t i;
7721
7722 assert(repeat);
7723
7724 set_init(&orig_set, set);
7725 set_init(&set2, set);
7726
7727 set_fill_set(&orig_set, set);
7728
Michal Vasko004d3152020-06-11 19:59:22 +02007729 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007730 LY_CHECK_GOTO(rc, cleanup);
7731
7732 /* ('|' PathExpr)* */
7733 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007734 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_UNI);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007735 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007736 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7737 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007738
7739 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007740 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007741 LY_CHECK_GOTO(rc, cleanup);
7742 continue;
7743 }
7744
7745 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02007746 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007747 LY_CHECK_GOTO(rc, cleanup);
7748
7749 /* eval */
7750 if (options & LYXP_SCNODE_ALL) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01007751 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007752 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007753 rc = moveto_union(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007754 LY_CHECK_GOTO(rc, cleanup);
7755 }
7756 }
7757
7758cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007759 lyxp_set_free_content(&orig_set);
7760 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007761 return rc;
7762}
7763
7764/**
7765 * @brief Evaluate UnaryExpr. Logs directly on error.
7766 *
Michal Vaskod3678892020-05-21 10:06:58 +02007767 * [19] UnaryExpr ::= UnionExpr | '-' UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007768 *
7769 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007770 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007771 * @param[in] repeat How many times this expression is repeated.
7772 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7773 * @param[in] options XPath options.
7774 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7775 */
7776static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007777eval_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 +02007778{
7779 LY_ERR rc;
7780 uint16_t this_op, i;
7781
7782 assert(repeat);
7783
7784 /* ('-')+ */
Michal Vasko004d3152020-06-11 19:59:22 +02007785 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007786 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007787 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 +02007788
7789 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007790 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7791 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007792 }
7793
Michal Vasko004d3152020-06-11 19:59:22 +02007794 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNARY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007795 LY_CHECK_RET(rc);
7796
7797 if (set && (repeat % 2)) {
7798 if (options & LYXP_SCNODE_ALL) {
7799 warn_operands(set->ctx, set, NULL, 1, exp->expr, exp->tok_pos[this_op]);
7800 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007801 rc = moveto_op_math(set, NULL, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007802 LY_CHECK_RET(rc);
7803 }
7804 }
7805
7806 return LY_SUCCESS;
7807}
7808
7809/**
7810 * @brief Evaluate MultiplicativeExpr. Logs directly on error.
7811 *
Michal Vaskod3678892020-05-21 10:06:58 +02007812 * [18] MultiplicativeExpr ::= UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007813 * | MultiplicativeExpr '*' UnaryExpr
7814 * | MultiplicativeExpr 'div' UnaryExpr
7815 * | MultiplicativeExpr 'mod' UnaryExpr
7816 *
7817 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007818 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007819 * @param[in] repeat How many times this expression is repeated.
7820 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7821 * @param[in] options XPath options.
7822 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7823 */
7824static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007825eval_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 +02007826{
7827 LY_ERR rc;
7828 uint16_t this_op;
7829 struct lyxp_set orig_set, set2;
7830 uint16_t i;
7831
7832 assert(repeat);
7833
7834 set_init(&orig_set, set);
7835 set_init(&set2, set);
7836
7837 set_fill_set(&orig_set, set);
7838
Michal Vasko004d3152020-06-11 19:59:22 +02007839 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007840 LY_CHECK_GOTO(rc, cleanup);
7841
7842 /* ('*' / 'div' / 'mod' UnaryExpr)* */
7843 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007844 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007845
Michal Vasko004d3152020-06-11 19:59:22 +02007846 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007847 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007848 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7849 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007850
7851 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007852 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007853 LY_CHECK_GOTO(rc, cleanup);
7854 continue;
7855 }
7856
7857 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02007858 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007859 LY_CHECK_GOTO(rc, cleanup);
7860
7861 /* eval */
7862 if (options & LYXP_SCNODE_ALL) {
7863 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007864 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007865 set_scnode_clear_ctx(set);
7866 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007867 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007868 LY_CHECK_GOTO(rc, cleanup);
7869 }
7870 }
7871
7872cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007873 lyxp_set_free_content(&orig_set);
7874 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007875 return rc;
7876}
7877
7878/**
7879 * @brief Evaluate AdditiveExpr. Logs directly on error.
7880 *
Michal Vaskod3678892020-05-21 10:06:58 +02007881 * [17] AdditiveExpr ::= MultiplicativeExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007882 * | AdditiveExpr '+' MultiplicativeExpr
7883 * | AdditiveExpr '-' MultiplicativeExpr
7884 *
7885 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007886 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007887 * @param[in] repeat How many times this expression is repeated.
7888 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7889 * @param[in] options XPath options.
7890 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7891 */
7892static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007893eval_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 +02007894{
7895 LY_ERR rc;
7896 uint16_t this_op;
7897 struct lyxp_set orig_set, set2;
7898 uint16_t i;
7899
7900 assert(repeat);
7901
7902 set_init(&orig_set, set);
7903 set_init(&set2, set);
7904
7905 set_fill_set(&orig_set, set);
7906
Michal Vasko004d3152020-06-11 19:59:22 +02007907 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007908 LY_CHECK_GOTO(rc, cleanup);
7909
7910 /* ('+' / '-' MultiplicativeExpr)* */
7911 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007912 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007913
Michal Vasko004d3152020-06-11 19:59:22 +02007914 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007915 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007916 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7917 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007918
7919 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007920 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007921 LY_CHECK_GOTO(rc, cleanup);
7922 continue;
7923 }
7924
7925 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02007926 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007927 LY_CHECK_GOTO(rc, cleanup);
7928
7929 /* eval */
7930 if (options & LYXP_SCNODE_ALL) {
7931 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007932 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007933 set_scnode_clear_ctx(set);
7934 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007935 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007936 LY_CHECK_GOTO(rc, cleanup);
7937 }
7938 }
7939
7940cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007941 lyxp_set_free_content(&orig_set);
7942 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007943 return rc;
7944}
7945
7946/**
7947 * @brief Evaluate RelationalExpr. Logs directly on error.
7948 *
Michal Vaskod3678892020-05-21 10:06:58 +02007949 * [16] RelationalExpr ::= AdditiveExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007950 * | RelationalExpr '<' AdditiveExpr
7951 * | RelationalExpr '>' AdditiveExpr
7952 * | RelationalExpr '<=' AdditiveExpr
7953 * | RelationalExpr '>=' AdditiveExpr
7954 *
7955 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007956 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007957 * @param[in] repeat How many times this expression is repeated.
7958 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7959 * @param[in] options XPath options.
7960 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7961 */
7962static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007963eval_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 +02007964{
7965 LY_ERR rc;
7966 uint16_t this_op;
7967 struct lyxp_set orig_set, set2;
7968 uint16_t i;
7969
7970 assert(repeat);
7971
7972 set_init(&orig_set, set);
7973 set_init(&set2, set);
7974
7975 set_fill_set(&orig_set, set);
7976
Michal Vasko004d3152020-06-11 19:59:22 +02007977 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007978 LY_CHECK_GOTO(rc, cleanup);
7979
7980 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
7981 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007982 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007983
Michal Vasko004d3152020-06-11 19:59:22 +02007984 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_COMP);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007985 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007986 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7987 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007988
7989 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007990 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007991 LY_CHECK_GOTO(rc, cleanup);
7992 continue;
7993 }
7994
7995 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02007996 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007997 LY_CHECK_GOTO(rc, cleanup);
7998
7999 /* eval */
8000 if (options & LYXP_SCNODE_ALL) {
8001 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008002 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008003 set_scnode_clear_ctx(set);
8004 } else {
8005 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options);
8006 LY_CHECK_GOTO(rc, cleanup);
8007 }
8008 }
8009
8010cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008011 lyxp_set_free_content(&orig_set);
8012 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008013 return rc;
8014}
8015
8016/**
8017 * @brief Evaluate EqualityExpr. Logs directly on error.
8018 *
Michal Vaskod3678892020-05-21 10:06:58 +02008019 * [15] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008020 * | EqualityExpr '!=' RelationalExpr
8021 *
8022 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008023 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008024 * @param[in] repeat How many times this expression is repeated.
8025 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8026 * @param[in] options XPath options.
8027 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8028 */
8029static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02008030eval_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 +02008031{
8032 LY_ERR rc;
8033 uint16_t this_op;
8034 struct lyxp_set orig_set, set2;
8035 uint16_t i;
8036
8037 assert(repeat);
8038
8039 set_init(&orig_set, set);
8040 set_init(&set2, set);
8041
8042 set_fill_set(&orig_set, set);
8043
Michal Vasko004d3152020-06-11 19:59:22 +02008044 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008045 LY_CHECK_GOTO(rc, cleanup);
8046
8047 /* ('=' / '!=' RelationalExpr)* */
8048 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008049 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008050
Michal Vasko004d3152020-06-11 19:59:22 +02008051 assert((exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_EQUAL) || (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_NEQUAL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008052 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02008053 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
8054 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008055
8056 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02008057 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008058 LY_CHECK_GOTO(rc, cleanup);
8059 continue;
8060 }
8061
8062 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008063 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008064 LY_CHECK_GOTO(rc, cleanup);
8065
8066 /* eval */
8067 if (options & LYXP_SCNODE_ALL) {
8068 warn_operands(set->ctx, set, &set2, 0, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vasko004d3152020-06-11 19:59:22 +02008069 warn_equality_value(exp, set, *tok_idx - 1, this_op - 1, *tok_idx - 1);
8070 warn_equality_value(exp, &set2, this_op - 1, this_op - 1, *tok_idx - 1);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008071 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008072 set_scnode_clear_ctx(set);
8073 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008074 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options);
8075 LY_CHECK_GOTO(rc, cleanup);
8076 }
8077 }
8078
8079cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008080 lyxp_set_free_content(&orig_set);
8081 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008082 return rc;
8083}
8084
8085/**
8086 * @brief Evaluate AndExpr. Logs directly on error.
8087 *
Michal Vaskod3678892020-05-21 10:06:58 +02008088 * [14] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008089 *
8090 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008091 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008092 * @param[in] repeat How many times this expression is repeated.
8093 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8094 * @param[in] options XPath options.
8095 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8096 */
8097static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02008098eval_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 +02008099{
8100 LY_ERR rc;
8101 struct lyxp_set orig_set, set2;
8102 uint16_t i;
8103
8104 assert(repeat);
8105
8106 set_init(&orig_set, set);
8107 set_init(&set2, set);
8108
8109 set_fill_set(&orig_set, set);
8110
Michal Vasko004d3152020-06-11 19:59:22 +02008111 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008112 LY_CHECK_GOTO(rc, cleanup);
8113
8114 /* cast to boolean, we know that will be the final result */
8115 if (set && (options & LYXP_SCNODE_ALL)) {
8116 set_scnode_clear_ctx(set);
8117 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008118 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008119 }
8120
8121 /* ('and' EqualityExpr)* */
8122 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008123 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
8124 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (!set || !set->val.bln ? "skipped" : "parsed"),
8125 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
8126 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008127
8128 /* lazy evaluation */
Michal Vasko004d3152020-06-11 19:59:22 +02008129 if (!set || ((set->type == LYXP_SET_BOOLEAN) && !set->val.bln)) {
8130 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008131 LY_CHECK_GOTO(rc, cleanup);
8132 continue;
8133 }
8134
8135 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008136 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008137 LY_CHECK_GOTO(rc, cleanup);
8138
8139 /* eval - just get boolean value actually */
8140 if (set->type == LYXP_SET_SCNODE_SET) {
8141 set_scnode_clear_ctx(&set2);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008142 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008143 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008144 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008145 set_fill_set(set, &set2);
8146 }
8147 }
8148
8149cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008150 lyxp_set_free_content(&orig_set);
8151 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008152 return rc;
8153}
8154
8155/**
8156 * @brief Evaluate OrExpr. Logs directly on error.
8157 *
Michal Vaskod3678892020-05-21 10:06:58 +02008158 * [13] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008159 *
8160 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008161 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008162 * @param[in] repeat How many times this expression is repeated.
8163 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8164 * @param[in] options XPath options.
8165 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8166 */
8167static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02008168eval_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 +02008169{
8170 LY_ERR rc;
8171 struct lyxp_set orig_set, set2;
8172 uint16_t i;
8173
8174 assert(repeat);
8175
8176 set_init(&orig_set, set);
8177 set_init(&set2, set);
8178
8179 set_fill_set(&orig_set, set);
8180
Michal Vasko004d3152020-06-11 19:59:22 +02008181 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008182 LY_CHECK_GOTO(rc, cleanup);
8183
8184 /* cast to boolean, we know that will be the final result */
8185 if (set && (options & LYXP_SCNODE_ALL)) {
8186 set_scnode_clear_ctx(set);
8187 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008188 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008189 }
8190
8191 /* ('or' AndExpr)* */
8192 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008193 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
8194 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (!set || set->val.bln ? "skipped" : "parsed"),
8195 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
8196 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008197
8198 /* lazy evaluation */
Michal Vasko004d3152020-06-11 19:59:22 +02008199 if (!set || ((set->type == LYXP_SET_BOOLEAN) && set->val.bln)) {
8200 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008201 LY_CHECK_GOTO(rc, cleanup);
8202 continue;
8203 }
8204
8205 set_fill_set(&set2, &orig_set);
8206 /* expr_type cound have been LYXP_EXPR_NONE in all these later calls (except for the first one),
8207 * but it does not matter */
Michal Vasko004d3152020-06-11 19:59:22 +02008208 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008209 LY_CHECK_GOTO(rc, cleanup);
8210
8211 /* eval - just get boolean value actually */
8212 if (set->type == LYXP_SET_SCNODE_SET) {
8213 set_scnode_clear_ctx(&set2);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008214 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008215 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008216 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008217 set_fill_set(set, &set2);
8218 }
8219 }
8220
8221cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008222 lyxp_set_free_content(&orig_set);
8223 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008224 return rc;
8225}
8226
8227/**
Michal Vasko004d3152020-06-11 19:59:22 +02008228 * @brief Decide what expression is at the pointer @p tok_idx and evaluate it accordingly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008229 *
8230 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008231 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008232 * @param[in] etype Expression type to evaluate.
8233 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8234 * @param[in] options XPath options.
8235 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8236 */
8237static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02008238eval_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 +02008239{
8240 uint16_t i, count;
8241 enum lyxp_expr_type next_etype;
8242 LY_ERR rc;
8243
8244 /* process operator repeats */
Michal Vasko004d3152020-06-11 19:59:22 +02008245 if (!exp->repeat[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008246 next_etype = LYXP_EXPR_NONE;
8247 } else {
8248 /* find etype repeat */
Michal Vasko004d3152020-06-11 19:59:22 +02008249 for (i = 0; exp->repeat[*tok_idx][i] > etype; ++i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008250
8251 /* select one-priority lower because etype expression called us */
8252 if (i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008253 next_etype = exp->repeat[*tok_idx][i - 1];
Michal Vasko03ff5a72019-09-11 13:49:33 +02008254 /* count repeats for that expression */
Michal Vasko004d3152020-06-11 19:59:22 +02008255 for (count = 0; i && exp->repeat[*tok_idx][i - 1] == next_etype; ++count, --i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008256 } else {
8257 next_etype = LYXP_EXPR_NONE;
8258 }
8259 }
8260
8261 /* decide what expression are we parsing based on the repeat */
8262 switch (next_etype) {
8263 case LYXP_EXPR_OR:
Michal Vasko004d3152020-06-11 19:59:22 +02008264 rc = eval_or_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008265 break;
8266 case LYXP_EXPR_AND:
Michal Vasko004d3152020-06-11 19:59:22 +02008267 rc = eval_and_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008268 break;
8269 case LYXP_EXPR_EQUALITY:
Michal Vasko004d3152020-06-11 19:59:22 +02008270 rc = eval_equality_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008271 break;
8272 case LYXP_EXPR_RELATIONAL:
Michal Vasko004d3152020-06-11 19:59:22 +02008273 rc = eval_relational_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008274 break;
8275 case LYXP_EXPR_ADDITIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02008276 rc = eval_additive_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008277 break;
8278 case LYXP_EXPR_MULTIPLICATIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02008279 rc = eval_multiplicative_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008280 break;
8281 case LYXP_EXPR_UNARY:
Michal Vasko004d3152020-06-11 19:59:22 +02008282 rc = eval_unary_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008283 break;
8284 case LYXP_EXPR_UNION:
Michal Vasko004d3152020-06-11 19:59:22 +02008285 rc = eval_union_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008286 break;
8287 case LYXP_EXPR_NONE:
Michal Vasko004d3152020-06-11 19:59:22 +02008288 rc = eval_path_expr(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008289 break;
8290 default:
8291 LOGINT_RET(set->ctx);
8292 }
8293
8294 return rc;
8295}
8296
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008297/**
8298 * @brief Get root type.
8299 *
8300 * @param[in] ctx_node Context node.
8301 * @param[in] ctx_scnode Schema context node.
8302 * @param[in] options XPath options.
8303 * @return Root type.
8304 */
8305static enum lyxp_node_type
8306lyxp_get_root_type(const struct lyd_node *ctx_node, const struct lysc_node *ctx_scnode, int options)
8307{
8308 if (options & LYXP_SCNODE_ALL) {
8309 if (options & LYXP_SCNODE) {
8310 /* general root that can access everything */
8311 return LYXP_NODE_ROOT;
8312 } else if (!ctx_scnode || (ctx_scnode->flags & LYS_CONFIG_W)) {
8313 /* root context node can access only config data (because we said so, it is unspecified) */
8314 return LYXP_NODE_ROOT_CONFIG;
8315 } else {
8316 return LYXP_NODE_ROOT;
8317 }
8318 }
8319
8320 if (!ctx_node || (ctx_node->schema->flags & LYS_CONFIG_W)) {
8321 /* root context node can access only config data (because we said so, it is unspecified) */
8322 return LYXP_NODE_ROOT_CONFIG;
8323 }
8324
8325 return LYXP_NODE_ROOT;
8326}
8327
Michal Vasko03ff5a72019-09-11 13:49:33 +02008328LY_ERR
Michal Vaskoecd62de2019-11-13 12:35:11 +01008329lyxp_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 +01008330 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 +02008331{
Michal Vasko004d3152020-06-11 19:59:22 +02008332 uint16_t tok_idx = 0;
8333 const struct lyd_node *real_ctx_node;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008334 LY_ERR rc;
8335
Michal Vasko004d3152020-06-11 19:59:22 +02008336 LY_CHECK_ARG_RET(NULL, exp, local_mod, ctx_node, set, LY_EINVAL);
8337
8338 if ((ctx_node_type == LYXP_NODE_ROOT) || (ctx_node_type == LYXP_NODE_ROOT_CONFIG)) {
8339 /* we always need some context node because it is used for resolving unqualified names */
8340 real_ctx_node = NULL;
8341 } else {
8342 real_ctx_node = ctx_node;
8343 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008344
8345 /* prepare set for evaluation */
Michal Vasko004d3152020-06-11 19:59:22 +02008346 tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008347 memset(set, 0, sizeof *set);
Michal Vaskod3678892020-05-21 10:06:58 +02008348 set->type = LYXP_SET_NODE_SET;
Michal Vasko004d3152020-06-11 19:59:22 +02008349 set_insert_node(set, (struct lyd_node *)real_ctx_node, 0, ctx_node_type, 0);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008350 set->ctx = local_mod->ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008351 set->ctx_node = ctx_node;
Michal Vasko004d3152020-06-11 19:59:22 +02008352 set->root_type = lyxp_get_root_type(real_ctx_node, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008353 set->local_mod = local_mod;
Michal Vaskof03ed032020-03-04 13:31:44 +01008354 set->tree = tree;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008355 set->format = format;
8356
8357 /* evaluate */
Michal Vasko004d3152020-06-11 19:59:22 +02008358 rc = eval_expr_select(exp, &tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008359 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02008360 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008361 }
8362
Michal Vasko03ff5a72019-09-11 13:49:33 +02008363 return rc;
8364}
8365
8366#if 0
8367
8368/* full xml printing of set elements, not used currently */
8369
8370void
8371lyxp_set_print_xml(FILE *f, struct lyxp_set *set)
8372{
8373 uint32_t i;
8374 char *str_num;
8375 struct lyout out;
8376
8377 memset(&out, 0, sizeof out);
8378
8379 out.type = LYOUT_STREAM;
8380 out.method.f = f;
8381
8382 switch (set->type) {
8383 case LYXP_SET_EMPTY:
8384 ly_print(&out, "Empty XPath set\n\n");
8385 break;
8386 case LYXP_SET_BOOLEAN:
8387 ly_print(&out, "Boolean XPath set:\n");
8388 ly_print(&out, "%s\n\n", set->value.bool ? "true" : "false");
8389 break;
8390 case LYXP_SET_STRING:
8391 ly_print(&out, "String XPath set:\n");
8392 ly_print(&out, "\"%s\"\n\n", set->value.str);
8393 break;
8394 case LYXP_SET_NUMBER:
8395 ly_print(&out, "Number XPath set:\n");
8396
8397 if (isnan(set->value.num)) {
8398 str_num = strdup("NaN");
8399 } else if ((set->value.num == 0) || (set->value.num == -0.0f)) {
8400 str_num = strdup("0");
8401 } else if (isinf(set->value.num) && !signbit(set->value.num)) {
8402 str_num = strdup("Infinity");
8403 } else if (isinf(set->value.num) && signbit(set->value.num)) {
8404 str_num = strdup("-Infinity");
8405 } else if ((long long)set->value.num == set->value.num) {
8406 if (asprintf(&str_num, "%lld", (long long)set->value.num) == -1) {
8407 str_num = NULL;
8408 }
8409 } else {
8410 if (asprintf(&str_num, "%03.1Lf", set->value.num) == -1) {
8411 str_num = NULL;
8412 }
8413 }
8414 if (!str_num) {
8415 LOGMEM;
8416 return;
8417 }
8418 ly_print(&out, "%s\n\n", str_num);
8419 free(str_num);
8420 break;
8421 case LYXP_SET_NODE_SET:
8422 ly_print(&out, "Node XPath set:\n");
8423
8424 for (i = 0; i < set->used; ++i) {
8425 ly_print(&out, "%d. ", i + 1);
8426 switch (set->node_type[i]) {
8427 case LYXP_NODE_ROOT_ALL:
8428 ly_print(&out, "ROOT all\n\n");
8429 break;
8430 case LYXP_NODE_ROOT_CONFIG:
8431 ly_print(&out, "ROOT config\n\n");
8432 break;
8433 case LYXP_NODE_ROOT_STATE:
8434 ly_print(&out, "ROOT state\n\n");
8435 break;
8436 case LYXP_NODE_ROOT_NOTIF:
8437 ly_print(&out, "ROOT notification \"%s\"\n\n", set->value.nodes[i]->schema->name);
8438 break;
8439 case LYXP_NODE_ROOT_RPC:
8440 ly_print(&out, "ROOT rpc \"%s\"\n\n", set->value.nodes[i]->schema->name);
8441 break;
8442 case LYXP_NODE_ROOT_OUTPUT:
8443 ly_print(&out, "ROOT output \"%s\"\n\n", set->value.nodes[i]->schema->name);
8444 break;
8445 case LYXP_NODE_ELEM:
8446 ly_print(&out, "ELEM \"%s\"\n", set->value.nodes[i]->schema->name);
8447 xml_print_node(&out, 1, set->value.nodes[i], 1, LYP_FORMAT);
8448 ly_print(&out, "\n");
8449 break;
8450 case LYXP_NODE_TEXT:
8451 ly_print(&out, "TEXT \"%s\"\n\n", ((struct lyd_node_leaf_list *)set->value.nodes[i])->value_str);
8452 break;
8453 case LYXP_NODE_ATTR:
8454 ly_print(&out, "ATTR \"%s\" = \"%s\"\n\n", set->value.attrs[i]->name, set->value.attrs[i]->value);
8455 break;
8456 }
8457 }
8458 break;
8459 }
8460}
8461
8462#endif
8463
8464LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008465lyxp_set_cast(struct lyxp_set *set, enum lyxp_set_type target)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008466{
8467 long double num;
8468 char *str;
8469 LY_ERR rc;
8470
8471 if (!set || (set->type == target)) {
8472 return LY_SUCCESS;
8473 }
8474
8475 /* it's not possible to convert anything into a node set */
Michal Vaskod3678892020-05-21 10:06:58 +02008476 assert(target != LYXP_SET_NODE_SET);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008477
8478 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vaskod3678892020-05-21 10:06:58 +02008479 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008480 return LY_EINVAL;
8481 }
8482
8483 /* to STRING */
Michal Vaskod3678892020-05-21 10:06:58 +02008484 if ((target == LYXP_SET_STRING) || ((target == LYXP_SET_NUMBER) && (set->type == LYXP_SET_NODE_SET))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008485 switch (set->type) {
8486 case LYXP_SET_NUMBER:
8487 if (isnan(set->val.num)) {
8488 set->val.str = strdup("NaN");
8489 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8490 } else if ((set->val.num == 0) || (set->val.num == -0.0f)) {
8491 set->val.str = strdup("0");
8492 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8493 } else if (isinf(set->val.num) && !signbit(set->val.num)) {
8494 set->val.str = strdup("Infinity");
8495 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8496 } else if (isinf(set->val.num) && signbit(set->val.num)) {
8497 set->val.str = strdup("-Infinity");
8498 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8499 } else if ((long long)set->val.num == set->val.num) {
8500 if (asprintf(&str, "%lld", (long long)set->val.num) == -1) {
8501 LOGMEM_RET(set->ctx);
8502 }
8503 set->val.str = str;
8504 } else {
8505 if (asprintf(&str, "%03.1Lf", set->val.num) == -1) {
8506 LOGMEM_RET(set->ctx);
8507 }
8508 set->val.str = str;
8509 }
8510 break;
8511 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02008512 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008513 set->val.str = strdup("true");
8514 } else {
8515 set->val.str = strdup("false");
8516 }
8517 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM);
8518 break;
8519 case LYXP_SET_NODE_SET:
8520 assert(set->used);
8521
8522 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008523 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008524
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008525 rc = cast_node_set_to_string(set, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008526 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02008527 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008528 set->val.str = str;
8529 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008530 default:
8531 LOGINT_RET(set->ctx);
8532 }
8533 set->type = LYXP_SET_STRING;
8534 }
8535
8536 /* to NUMBER */
8537 if (target == LYXP_SET_NUMBER) {
8538 switch (set->type) {
8539 case LYXP_SET_STRING:
8540 num = cast_string_to_number(set->val.str);
Michal Vaskod3678892020-05-21 10:06:58 +02008541 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008542 set->val.num = num;
8543 break;
8544 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02008545 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008546 set->val.num = 1;
8547 } else {
8548 set->val.num = 0;
8549 }
8550 break;
8551 default:
8552 LOGINT_RET(set->ctx);
8553 }
8554 set->type = LYXP_SET_NUMBER;
8555 }
8556
8557 /* to BOOLEAN */
8558 if (target == LYXP_SET_BOOLEAN) {
8559 switch (set->type) {
8560 case LYXP_SET_NUMBER:
8561 if ((set->val.num == 0) || (set->val.num == -0.0f) || isnan(set->val.num)) {
Michal Vasko004d3152020-06-11 19:59:22 +02008562 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008563 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02008564 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008565 }
8566 break;
8567 case LYXP_SET_STRING:
8568 if (set->val.str[0]) {
Michal Vaskod3678892020-05-21 10:06:58 +02008569 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008570 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008571 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02008572 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008573 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008574 }
8575 break;
8576 case LYXP_SET_NODE_SET:
Michal Vaskod3678892020-05-21 10:06:58 +02008577 if (set->used) {
8578 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008579 set->val.bln = 1;
Michal Vaskod3678892020-05-21 10:06:58 +02008580 } else {
8581 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008582 set->val.bln = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02008583 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008584 break;
8585 default:
8586 LOGINT_RET(set->ctx);
8587 }
8588 set->type = LYXP_SET_BOOLEAN;
8589 }
8590
Michal Vasko03ff5a72019-09-11 13:49:33 +02008591 return LY_SUCCESS;
8592}
8593
8594LY_ERR
8595lyxp_atomize(struct lyxp_expr *exp, LYD_FORMAT format, const struct lys_module *local_mod, const struct lysc_node *ctx_scnode,
8596 enum lyxp_node_type ctx_scnode_type, struct lyxp_set *set, int options)
8597{
8598 struct ly_ctx *ctx;
Michal Vasko004d3152020-06-11 19:59:22 +02008599 const struct lysc_node *real_ctx_scnode;
8600 uint16_t tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008601
Michal Vasko004d3152020-06-11 19:59:22 +02008602 LY_CHECK_ARG_RET(NULL, exp, local_mod, ctx_scnode, set, LY_EINVAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008603
8604 ctx = local_mod->ctx;
Michal Vasko004d3152020-06-11 19:59:22 +02008605 if ((ctx_scnode_type == LYXP_NODE_ROOT) || (ctx_scnode_type == LYXP_NODE_ROOT_CONFIG)) {
8606 /* we always need some context node because it is used for resolving unqualified names */
8607 real_ctx_scnode = NULL;
8608 } else {
8609 real_ctx_scnode = ctx_scnode;
8610 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008611
8612 /* prepare set for evaluation */
Michal Vasko004d3152020-06-11 19:59:22 +02008613 tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008614 memset(set, 0, sizeof *set);
8615 set->type = LYXP_SET_SCNODE_SET;
Michal Vasko004d3152020-06-11 19:59:22 +02008616 lyxp_set_scnode_insert_node(set, real_ctx_scnode, ctx_scnode_type);
Michal Vasko5c4e5892019-11-14 12:31:38 +01008617 set->val.scnodes[0].in_ctx = -2;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008618 set->ctx = ctx;
8619 set->ctx_scnode = ctx_scnode;
Michal Vasko004d3152020-06-11 19:59:22 +02008620 set->root_type = lyxp_get_root_type(NULL, real_ctx_scnode, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008621 set->local_mod = local_mod;
8622 set->format = format;
8623
8624 /* evaluate */
Michal Vasko004d3152020-06-11 19:59:22 +02008625 return eval_expr_select(exp, &tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008626}