blob: 9b9e835963f5189ccd0b341ff1e17e8aa66f25ff [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);
Radek Krejci1deb5be2020-08-26 16:43:36 +020047static LY_ERR eval_expr_select(struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_expr_type etype, struct lyxp_set *set, uint32_t 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;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200168 struct lyxp_set_node *item;
169 struct lyxp_set_scnode *sitem;
170
171 if (ly_log_level < LY_LLDBG) {
172 return;
173 }
174
175 switch (set->type) {
176 case LYXP_SET_NODE_SET:
177 LOGDBG(LY_LDGXPATH, "set NODE SET:");
178 for (i = 0; i < set->used; ++i) {
179 item = &set->val.nodes[i];
180
181 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +0100182 case LYXP_NODE_NONE:
183 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): NONE", i + 1, item->pos);
184 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200185 case LYXP_NODE_ROOT:
186 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT", i + 1, item->pos);
187 break;
188 case LYXP_NODE_ROOT_CONFIG:
189 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ROOT CONFIG", i + 1, item->pos);
190 break;
191 case LYXP_NODE_ELEM:
192 if ((item->node->schema->nodetype == LYS_LIST)
193 && (((struct lyd_node_inner *)item->node)->child->schema->nodetype == LYS_LEAF)) {
194 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (1st child val: %s)", i + 1, item->pos,
Michal Vaskob7be7a82020-08-20 09:09:04 +0200195 item->node->schema->name, LYD_CANON_VALUE(lyd_node_children(item->node, 0)));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200196 } else if (((struct lyd_node_inner *)item->node)->schema->nodetype == LYS_LEAFLIST) {
197 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s (val: %s)", i + 1, item->pos,
Michal Vaskob7be7a82020-08-20 09:09:04 +0200198 item->node->schema->name, LYD_CANON_VALUE(item->node));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200199 } else {
200 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): ELEM %s", i + 1, item->pos, item->node->schema->name);
201 }
202 break;
203 case LYXP_NODE_TEXT:
204 if (item->node->schema->nodetype & LYS_ANYDATA) {
205 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): TEXT <%s>", i + 1, item->pos,
206 item->node->schema->nodetype == LYS_ANYXML ? "anyxml" : "anydata");
207 } else {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200208 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): TEXT %s", i + 1, item->pos, LYD_CANON_VALUE(item->node));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200209 }
210 break;
Michal Vasko9f96a052020-03-10 09:41:45 +0100211 case LYXP_NODE_META:
212 LOGDBG(LY_LDGXPATH, "\t%d (pos %u): META %s = %s", i + 1, item->pos, set->val.meta[i].meta->name,
213 set->val.meta[i].meta->value);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200214 break;
215 }
216 }
217 break;
218
219 case LYXP_SET_SCNODE_SET:
220 LOGDBG(LY_LDGXPATH, "set SCNODE SET:");
221 for (i = 0; i < set->used; ++i) {
222 sitem = &set->val.scnodes[i];
223
224 switch (sitem->type) {
225 case LYXP_NODE_ROOT:
226 LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT", i + 1, sitem->in_ctx);
227 break;
228 case LYXP_NODE_ROOT_CONFIG:
229 LOGDBG(LY_LDGXPATH, "\t%d (%u): ROOT CONFIG", i + 1, sitem->in_ctx);
230 break;
231 case LYXP_NODE_ELEM:
232 LOGDBG(LY_LDGXPATH, "\t%d (%u): ELEM %s", i + 1, sitem->in_ctx, sitem->scnode->name);
233 break;
234 default:
235 LOGINT(NULL);
236 break;
237 }
238 }
239 break;
240
Michal Vasko03ff5a72019-09-11 13:49:33 +0200241 case LYXP_SET_BOOLEAN:
242 LOGDBG(LY_LDGXPATH, "set BOOLEAN");
Michal Vasko004d3152020-06-11 19:59:22 +0200243 LOGDBG(LY_LDGXPATH, "\t%s", (set->val.bln ? "true" : "false"));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200244 break;
245
246 case LYXP_SET_STRING:
247 LOGDBG(LY_LDGXPATH, "set STRING");
248 LOGDBG(LY_LDGXPATH, "\t%s", set->val.str);
249 break;
250
251 case LYXP_SET_NUMBER:
252 LOGDBG(LY_LDGXPATH, "set NUMBER");
253
254 if (isnan(set->val.num)) {
255 str = strdup("NaN");
256 } else if ((set->val.num == 0) || (set->val.num == -0.0f)) {
257 str = strdup("0");
258 } else if (isinf(set->val.num) && !signbit(set->val.num)) {
259 str = strdup("Infinity");
260 } else if (isinf(set->val.num) && signbit(set->val.num)) {
261 str = strdup("-Infinity");
262 } else if ((long long)set->val.num == set->val.num) {
263 if (asprintf(&str, "%lld", (long long)set->val.num) == -1) {
264 str = NULL;
265 }
266 } else {
267 if (asprintf(&str, "%03.1Lf", set->val.num) == -1) {
268 str = NULL;
269 }
270 }
271 LY_CHECK_ERR_RET(!str, LOGMEM(NULL), );
272
273 LOGDBG(LY_LDGXPATH, "\t%s", str);
274 free(str);
275 }
276}
277
278#endif
279
280/**
281 * @brief Realloc the string \p str.
282 *
283 * @param[in] ctx libyang context for logging.
284 * @param[in] needed How much free space is required.
285 * @param[in,out] str Pointer to the string to use.
286 * @param[in,out] used Used bytes in \p str.
287 * @param[in,out] size Allocated bytes in \p str.
288 * @return LY_ERR
289 */
290static LY_ERR
Michal Vasko52927e22020-03-16 17:26:14 +0100291cast_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 +0200292{
293 if (*size - *used < needed) {
294 do {
295 if ((UINT16_MAX - *size) < LYXP_STRING_CAST_SIZE_STEP) {
296 LOGERR(ctx, LY_EINVAL, "XPath string length limit (%u) reached.", UINT16_MAX);
297 return LY_EINVAL;
298 }
299 *size += LYXP_STRING_CAST_SIZE_STEP;
300 } while (*size - *used < needed);
301 *str = ly_realloc(*str, *size * sizeof(char));
302 LY_CHECK_ERR_RET(!(*str), LOGMEM(ctx), LY_EMEM);
303 }
304
305 return LY_SUCCESS;
306}
307
308/**
309 * @brief Cast nodes recursively to one string @p str.
310 *
311 * @param[in] node Node to cast.
312 * @param[in] fake_cont Whether to put the data into a "fake" container.
313 * @param[in] root_type Type of the XPath root.
314 * @param[in] indent Current indent.
315 * @param[in,out] str Resulting string.
316 * @param[in,out] used Used bytes in @p str.
317 * @param[in,out] size Allocated bytes in @p str.
318 * @return LY_ERR
319 */
320static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200321cast_string_recursive(const struct lyd_node *node, uint8_t fake_cont, enum lyxp_node_type root_type, uint16_t indent, char **str,
Radek Krejci0f969882020-08-21 16:56:47 +0200322 uint16_t *used, uint16_t *size)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200323{
Radek Krejci7f769d72020-07-11 23:13:56 +0200324 char *buf, *line, *ptr = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200325 const char *value_str;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200326 const struct lyd_node *child;
Michal Vasko60ea6352020-06-29 13:39:39 +0200327 struct lyd_node *tree;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200328 struct lyd_node_any *any;
329 LY_ERR rc;
330
331 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) {
332 return LY_SUCCESS;
333 }
334
335 if (fake_cont) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200336 rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200337 LY_CHECK_RET(rc);
338 strcpy(*str + (*used - 1), "\n");
339 ++(*used);
340
341 ++indent;
342 }
343
344 switch (node->schema->nodetype) {
345 case LYS_CONTAINER:
346 case LYS_LIST:
347 case LYS_RPC:
348 case LYS_NOTIF:
Michal Vaskob7be7a82020-08-20 09:09:04 +0200349 rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200350 LY_CHECK_RET(rc);
351 strcpy(*str + (*used - 1), "\n");
352 ++(*used);
353
Michal Vasko5bfd4be2020-06-23 13:26:19 +0200354 for (child = lyd_node_children(node, 0); child; child = child->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200355 rc = cast_string_recursive(child, 0, root_type, indent + 1, str, used, size);
356 LY_CHECK_RET(rc);
357 }
358
359 break;
360
361 case LYS_LEAF:
362 case LYS_LEAFLIST:
Michal Vaskob7be7a82020-08-20 09:09:04 +0200363 value_str = LYD_CANON_VALUE(node);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200364
365 /* print indent */
Michal Vaskob7be7a82020-08-20 09:09:04 +0200366 LY_CHECK_RET(cast_string_realloc(LYD_CTX(node), indent * 2 + strlen(value_str) + 1, str, used, size));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200367 memset(*str + (*used - 1), ' ', indent * 2);
368 *used += indent * 2;
369
370 /* print value */
371 if (*used == 1) {
372 sprintf(*str + (*used - 1), "%s", value_str);
373 *used += strlen(value_str);
374 } else {
375 sprintf(*str + (*used - 1), "%s\n", value_str);
376 *used += strlen(value_str) + 1;
377 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200378
379 break;
380
381 case LYS_ANYXML:
382 case LYS_ANYDATA:
383 any = (struct lyd_node_any *)node;
384 if (!(void *)any->value.tree) {
385 /* no content */
386 buf = strdup("");
Michal Vaskob7be7a82020-08-20 09:09:04 +0200387 LY_CHECK_ERR_RET(!buf, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200388 } else {
Radek Krejci241f6b52020-05-21 18:13:49 +0200389 struct ly_out *out;
Radek Krejcia5bba312020-01-09 15:41:20 +0100390
Michal Vasko60ea6352020-06-29 13:39:39 +0200391 if (any->value_type == LYD_ANYDATA_LYB) {
392 /* try to parse it into a data tree */
Michal Vaskob7be7a82020-08-20 09:09:04 +0200393 if (lyd_parse_data_mem((struct ly_ctx *)LYD_CTX(node), any->value.mem, LYD_LYB, LYD_PARSE_ONLY | LYD_PARSE_STRICT, 0, &tree) == LY_SUCCESS) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200394 /* successfully parsed */
395 free(any->value.mem);
396 any->value.tree = tree;
397 any->value_type = LYD_ANYDATA_DATATREE;
398 }
Radek Krejci7931b192020-06-25 17:05:03 +0200399 /* error is covered by the following switch where LYD_ANYDATA_LYB causes failure */
Michal Vasko60ea6352020-06-29 13:39:39 +0200400 }
401
Michal Vasko03ff5a72019-09-11 13:49:33 +0200402 switch (any->value_type) {
403 case LYD_ANYDATA_STRING:
404 case LYD_ANYDATA_XML:
405 case LYD_ANYDATA_JSON:
406 buf = strdup(any->value.json);
Michal Vaskob7be7a82020-08-20 09:09:04 +0200407 LY_CHECK_ERR_RET(!buf, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200408 break;
409 case LYD_ANYDATA_DATATREE:
Radek Krejci84ce7b12020-06-11 17:28:25 +0200410 LY_CHECK_RET(ly_out_new_memory(&buf, 0, &out));
Michal Vasko3a41dff2020-07-15 14:30:28 +0200411 rc = lyd_print_all(out, any->value.tree, LYD_XML, 0);
Radek Krejci241f6b52020-05-21 18:13:49 +0200412 ly_out_free(out, NULL, 0);
Radek Krejcia5bba312020-01-09 15:41:20 +0100413 LY_CHECK_RET(rc < 0, -rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200414 break;
Michal Vasko60ea6352020-06-29 13:39:39 +0200415 case LYD_ANYDATA_LYB:
Michal Vaskob7be7a82020-08-20 09:09:04 +0200416 LOGERR(LYD_CTX(node), LY_EINVAL, "Cannot convert LYB anydata into string.");
Michal Vasko60ea6352020-06-29 13:39:39 +0200417 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200418 }
419 }
420
421 line = strtok_r(buf, "\n", &ptr);
422 do {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200423 rc = cast_string_realloc(LYD_CTX(node), indent * 2 + strlen(line) + 1, str, used, size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200424 if (rc != LY_SUCCESS) {
425 free(buf);
426 return rc;
427 }
428 memset(*str + (*used - 1), ' ', indent * 2);
429 *used += indent * 2;
430
431 strcpy(*str + (*used - 1), line);
432 *used += strlen(line);
433
434 strcpy(*str + (*used - 1), "\n");
435 *used += 1;
436 } while ((line = strtok_r(NULL, "\n", &ptr)));
437
438 free(buf);
439 break;
440
441 default:
Michal Vaskob7be7a82020-08-20 09:09:04 +0200442 LOGINT_RET(LYD_CTX(node));
Michal Vasko03ff5a72019-09-11 13:49:33 +0200443 }
444
445 if (fake_cont) {
Michal Vaskob7be7a82020-08-20 09:09:04 +0200446 rc = cast_string_realloc(LYD_CTX(node), 1, str, used, size);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200447 LY_CHECK_RET(rc);
448 strcpy(*str + (*used - 1), "\n");
449 ++(*used);
450
451 --indent;
452 }
453
454 return LY_SUCCESS;
455}
456
457/**
458 * @brief Cast an element into a string.
459 *
460 * @param[in] node Node to cast.
461 * @param[in] fake_cont Whether to put the data into a "fake" container.
462 * @param[in] root_type Type of the XPath root.
463 * @param[out] str Element cast to dynamically-allocated string.
464 * @return LY_ERR
465 */
466static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +0200467cast_string_elem(struct lyd_node *node, uint8_t fake_cont, enum lyxp_node_type root_type, char **str)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200468{
469 uint16_t used, size;
470 LY_ERR rc;
471
472 *str = malloc(LYXP_STRING_CAST_SIZE_START * sizeof(char));
Michal Vaskob7be7a82020-08-20 09:09:04 +0200473 LY_CHECK_ERR_RET(!*str, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200474 (*str)[0] = '\0';
475 used = 1;
476 size = LYXP_STRING_CAST_SIZE_START;
477
478 rc = cast_string_recursive(node, fake_cont, root_type, 0, str, &used, &size);
479 if (rc != LY_SUCCESS) {
480 free(*str);
481 return rc;
482 }
483
484 if (size > used) {
485 *str = ly_realloc(*str, used * sizeof(char));
Michal Vaskob7be7a82020-08-20 09:09:04 +0200486 LY_CHECK_ERR_RET(!*str, LOGMEM(LYD_CTX(node)), LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200487 }
488 return LY_SUCCESS;
489}
490
491/**
492 * @brief Cast a LYXP_SET_NODE_SET set into a string.
493 * Context position aware.
494 *
495 * @param[in] set Set to cast.
Michal Vasko03ff5a72019-09-11 13:49:33 +0200496 * @param[out] str Cast dynamically-allocated string.
497 * @return LY_ERR
498 */
499static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100500cast_node_set_to_string(struct lyxp_set *set, char **str)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200501{
Michal Vasko03ff5a72019-09-11 13:49:33 +0200502 switch (set->val.nodes[0].type) {
Michal Vasko2caefc12019-11-14 16:07:56 +0100503 case LYXP_NODE_NONE:
504 /* invalid */
505 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200506 case LYXP_NODE_ROOT:
507 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100508 return cast_string_elem(set->val.nodes[0].node, 1, set->root_type, str);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200509 case LYXP_NODE_ELEM:
510 case LYXP_NODE_TEXT:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100511 return cast_string_elem(set->val.nodes[0].node, 0, set->root_type, str);
Michal Vasko9f96a052020-03-10 09:41:45 +0100512 case LYXP_NODE_META:
Michal Vaskoba99a3e2020-08-18 15:50:05 +0200513 *str = strdup(set->val.meta[0].meta->value.canonical);
514 if (!*str) {
515 LOGMEM_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200516 }
517 return LY_SUCCESS;
518 }
519
520 LOGINT_RET(set->ctx);
521}
522
523/**
524 * @brief Cast a string into an XPath number.
525 *
526 * @param[in] str String to use.
527 * @return Cast number.
528 */
529static long double
530cast_string_to_number(const char *str)
531{
532 long double num;
533 char *ptr;
534
535 errno = 0;
536 num = strtold(str, &ptr);
537 if (errno || *ptr) {
538 num = NAN;
539 }
540 return num;
541}
542
543/**
544 * @brief Callback for checking value equality.
545 *
546 * @param[in] val1_p First value.
547 * @param[in] val2_p Second value.
548 * @param[in] mod Whether hash table is being modified.
549 * @param[in] cb_data Callback data.
550 * @return 0 if not equal, non-zero if equal.
551 */
Radek Krejci1deb5be2020-08-26 16:43:36 +0200552static uint8_t
553set_values_equal_cb(void *val1_p, void *val2_p, uint8_t UNUSED(mod), void *UNUSED(cb_data))
Michal Vasko03ff5a72019-09-11 13:49:33 +0200554{
555 struct lyxp_set_hash_node *val1, *val2;
556
557 val1 = (struct lyxp_set_hash_node *)val1_p;
558 val2 = (struct lyxp_set_hash_node *)val2_p;
559
560 if ((val1->node == val2->node) && (val1->type == val2->type)) {
561 return 1;
562 }
563
564 return 0;
565}
566
567/**
568 * @brief Insert node and its hash into set.
569 *
570 * @param[in] set et to insert to.
571 * @param[in] node Node with hash.
572 * @param[in] type Node type.
573 */
574static void
575set_insert_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type)
576{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200577 LY_ERR r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200578 uint32_t i, hash;
579 struct lyxp_set_hash_node hnode;
580
581 if (!set->ht && (set->used >= LYD_HT_MIN_ITEMS)) {
582 /* create hash table and add all the nodes */
583 set->ht = lyht_new(1, sizeof(struct lyxp_set_hash_node), set_values_equal_cb, NULL, 1);
584 for (i = 0; i < set->used; ++i) {
585 hnode.node = set->val.nodes[i].node;
586 hnode.type = set->val.nodes[i].type;
587
588 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
589 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
590 hash = dict_hash_multi(hash, NULL, 0);
591
592 r = lyht_insert(set->ht, &hnode, hash, NULL);
593 assert(!r);
594 (void)r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200595
Michal Vasko9d6befd2019-12-11 14:56:56 +0100596 if (hnode.node == node) {
597 /* it was just added, do not add it twice */
598 node = NULL;
599 }
600 }
601 }
602
603 if (set->ht && node) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200604 /* add the new node into hash table */
605 hnode.node = node;
606 hnode.type = type;
607
608 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
609 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
610 hash = dict_hash_multi(hash, NULL, 0);
611
612 r = lyht_insert(set->ht, &hnode, hash, NULL);
613 assert(!r);
614 (void)r;
615 }
616}
617
618/**
619 * @brief Remove node and its hash from set.
620 *
621 * @param[in] set Set to remove from.
622 * @param[in] node Node to remove.
623 * @param[in] type Node type.
624 */
625static void
626set_remove_node_hash(struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type)
627{
Radek Krejci1deb5be2020-08-26 16:43:36 +0200628 LY_ERR r;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200629 struct lyxp_set_hash_node hnode;
630 uint32_t hash;
631
632 if (set->ht) {
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_remove(set->ht, &hnode, hash);
641 assert(!r);
642 (void)r;
643
644 if (!set->ht->used) {
645 lyht_free(set->ht);
646 set->ht = NULL;
647 }
648 }
649}
650
651/**
652 * @brief Check whether node is in set based on its hash.
653 *
654 * @param[in] set Set to search in.
655 * @param[in] node Node to search for.
656 * @param[in] type Node type.
657 * @param[in] skip_idx Index in @p set to skip.
658 * @return LY_ERR
659 */
660static LY_ERR
661set_dup_node_hash_check(const struct lyxp_set *set, struct lyd_node *node, enum lyxp_node_type type, int skip_idx)
662{
663 struct lyxp_set_hash_node hnode, *match_p;
664 uint32_t hash;
665
666 hnode.node = node;
667 hnode.type = type;
668
669 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
670 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
671 hash = dict_hash_multi(hash, NULL, 0);
672
673 if (!lyht_find(set->ht, &hnode, hash, (void **)&match_p)) {
674 if ((skip_idx > -1) && (set->val.nodes[skip_idx].node == match_p->node) && (set->val.nodes[skip_idx].type == match_p->type)) {
675 /* we found it on the index that should be skipped, find another */
676 hnode = *match_p;
677 if (lyht_find_next(set->ht, &hnode, hash, (void **)&match_p)) {
678 /* none other found */
679 return LY_SUCCESS;
680 }
681 }
682
683 return LY_EEXIST;
684 }
685
686 /* not found */
687 return LY_SUCCESS;
688}
689
Michal Vaskod3678892020-05-21 10:06:58 +0200690void
691lyxp_set_free_content(struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200692{
693 if (!set) {
694 return;
695 }
696
697 if (set->type == LYXP_SET_NODE_SET) {
698 free(set->val.nodes);
699 lyht_free(set->ht);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200700 } else if (set->type == LYXP_SET_SCNODE_SET) {
701 free(set->val.scnodes);
Michal Vaskod3678892020-05-21 10:06:58 +0200702 lyht_free(set->ht);
703 } else {
704 if (set->type == LYXP_SET_STRING) {
705 free(set->val.str);
706 }
707 set->type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200708 }
Michal Vaskod3678892020-05-21 10:06:58 +0200709
710 set->val.nodes = NULL;
711 set->used = 0;
712 set->size = 0;
713 set->ht = NULL;
714 set->ctx_pos = 0;
715 set->ctx_pos = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200716}
717
Michal Vasko5e0e6eb2019-11-06 15:47:50 +0100718/**
719 * @brief Free dynamically-allocated set.
720 *
721 * @param[in] set Set to free.
722 */
723static void
Michal Vasko03ff5a72019-09-11 13:49:33 +0200724lyxp_set_free(struct lyxp_set *set)
725{
726 if (!set) {
727 return;
728 }
729
Michal Vaskod3678892020-05-21 10:06:58 +0200730 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200731 free(set);
732}
733
734/**
735 * @brief Initialize set context.
736 *
737 * @param[in] new Set to initialize.
738 * @param[in] set Arbitrary initialized set.
739 */
740static void
Michal Vasko4c7763f2020-07-27 17:40:37 +0200741set_init(struct lyxp_set *new, const struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200742{
743 memset(new, 0, sizeof *new);
Michal Vasko02a77382019-09-12 11:47:35 +0200744 if (set) {
745 new->ctx = set->ctx;
746 new->ctx_node = set->ctx_node;
Michal Vasko588112f2019-12-10 14:51:53 +0100747 new->root_type = set->root_type;
Michal Vasko6b26e742020-07-17 15:02:10 +0200748 new->context_op = set->context_op;
Michal Vasko02a77382019-09-12 11:47:35 +0200749 new->local_mod = set->local_mod;
Michal Vaskof03ed032020-03-04 13:31:44 +0100750 new->tree = set->tree;
Michal Vasko02a77382019-09-12 11:47:35 +0200751 new->format = set->format;
752 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200753}
754
755/**
756 * @brief Create a deep copy of a set.
757 *
758 * @param[in] set Set to copy.
759 * @return Copy of @p set.
760 */
761static struct lyxp_set *
762set_copy(struct lyxp_set *set)
763{
764 struct lyxp_set *ret;
765 uint16_t i;
766
767 if (!set) {
768 return NULL;
769 }
770
771 ret = malloc(sizeof *ret);
772 LY_CHECK_ERR_RET(!ret, LOGMEM(set->ctx), NULL);
773 set_init(ret, set);
774
775 if (set->type == LYXP_SET_SCNODE_SET) {
776 ret->type = set->type;
777
778 for (i = 0; i < set->used; ++i) {
Michal Vaskoba716542019-12-16 10:01:58 +0100779 if ((set->val.scnodes[i].in_ctx == 1) || (set->val.scnodes[i].in_ctx == -2)) {
Radek Krejci1deb5be2020-08-26 16:43:36 +0200780 int idx = lyxp_set_scnode_insert_node(ret, set->val.scnodes[i].scnode, set->val.scnodes[i].type);
Michal Vasko3f27c522020-01-06 08:37:49 +0100781 /* coverity seems to think scnodes can be NULL */
782 if ((idx == -1) || !ret->val.scnodes) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200783 lyxp_set_free(ret);
784 return NULL;
785 }
Michal Vaskoba716542019-12-16 10:01:58 +0100786 ret->val.scnodes[idx].in_ctx = set->val.scnodes[i].in_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200787 }
788 }
789 } else if (set->type == LYXP_SET_NODE_SET) {
790 ret->type = set->type;
791 ret->val.nodes = malloc(set->used * sizeof *ret->val.nodes);
792 LY_CHECK_ERR_RET(!ret->val.nodes, LOGMEM(set->ctx); free(ret), NULL);
793 memcpy(ret->val.nodes, set->val.nodes, set->used * sizeof *ret->val.nodes);
794
795 ret->used = ret->size = set->used;
796 ret->ctx_pos = set->ctx_pos;
797 ret->ctx_size = set->ctx_size;
798 ret->ht = lyht_dup(set->ht);
799 } else {
Radek Krejci0f969882020-08-21 16:56:47 +0200800 memcpy(ret, set, sizeof *ret);
801 if (set->type == LYXP_SET_STRING) {
802 ret->val.str = strdup(set->val.str);
803 LY_CHECK_ERR_RET(!ret->val.str, LOGMEM(set->ctx); free(ret), NULL);
804 }
Michal Vasko03ff5a72019-09-11 13:49:33 +0200805 }
806
807 return ret;
808}
809
810/**
811 * @brief Fill XPath set with a string. Any current data are disposed of.
812 *
813 * @param[in] set Set to fill.
814 * @param[in] string String to fill into \p set.
815 * @param[in] str_len Length of \p string. 0 is a valid value!
816 */
817static void
818set_fill_string(struct lyxp_set *set, const char *string, uint16_t str_len)
819{
Michal Vaskod3678892020-05-21 10:06:58 +0200820 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200821
822 set->type = LYXP_SET_STRING;
823 if ((str_len == 0) && (string[0] != '\0')) {
824 string = "";
825 }
826 set->val.str = strndup(string, str_len);
827}
828
829/**
830 * @brief Fill XPath set with a number. Any current data are disposed of.
831 *
832 * @param[in] set Set to fill.
833 * @param[in] number Number to fill into \p set.
834 */
835static void
836set_fill_number(struct lyxp_set *set, long double number)
837{
Michal Vaskod3678892020-05-21 10:06:58 +0200838 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200839
840 set->type = LYXP_SET_NUMBER;
841 set->val.num = number;
842}
843
844/**
845 * @brief Fill XPath set with a boolean. Any current data are disposed of.
846 *
847 * @param[in] set Set to fill.
848 * @param[in] boolean Boolean to fill into \p set.
849 */
850static void
Radek Krejci1deb5be2020-08-26 16:43:36 +0200851set_fill_boolean(struct lyxp_set *set, uint8_t boolean)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200852{
Michal Vaskod3678892020-05-21 10:06:58 +0200853 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200854
855 set->type = LYXP_SET_BOOLEAN;
Michal Vasko004d3152020-06-11 19:59:22 +0200856 set->val.bln = boolean;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200857}
858
859/**
860 * @brief Fill XPath set with the value from another set (deep assign).
861 * Any current data are disposed of.
862 *
863 * @param[in] trg Set to fill.
864 * @param[in] src Source set to copy into \p trg.
865 */
866static void
Michal Vasko4c7763f2020-07-27 17:40:37 +0200867set_fill_set(struct lyxp_set *trg, const struct lyxp_set *src)
Michal Vasko03ff5a72019-09-11 13:49:33 +0200868{
869 if (!trg || !src) {
870 return;
871 }
872
873 if (trg->type == LYXP_SET_NODE_SET) {
874 free(trg->val.nodes);
875 } else if (trg->type == LYXP_SET_STRING) {
876 free(trg->val.str);
877 }
878 set_init(trg, src);
879
880 if (src->type == LYXP_SET_SCNODE_SET) {
881 trg->type = LYXP_SET_SCNODE_SET;
882 trg->used = src->used;
883 trg->size = src->used;
884
885 trg->val.scnodes = ly_realloc(trg->val.scnodes, trg->size * sizeof *trg->val.scnodes);
886 LY_CHECK_ERR_RET(!trg->val.scnodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), );
887 memcpy(trg->val.scnodes, src->val.scnodes, src->used * sizeof *src->val.scnodes);
888 } else if (src->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +0200889 set_fill_boolean(trg, src->val.bln);
Michal Vasko44f3d2c2020-08-24 09:49:38 +0200890 } else if (src->type == LYXP_SET_NUMBER) {
Michal Vasko03ff5a72019-09-11 13:49:33 +0200891 set_fill_number(trg, src->val.num);
892 } else if (src->type == LYXP_SET_STRING) {
893 set_fill_string(trg, src->val.str, strlen(src->val.str));
894 } else {
895 if (trg->type == LYXP_SET_NODE_SET) {
896 free(trg->val.nodes);
897 } else if (trg->type == LYXP_SET_STRING) {
898 free(trg->val.str);
899 }
900
Michal Vaskod3678892020-05-21 10:06:58 +0200901 assert(src->type == LYXP_SET_NODE_SET);
902
903 trg->type = LYXP_SET_NODE_SET;
904 trg->used = src->used;
905 trg->size = src->used;
906 trg->ctx_pos = src->ctx_pos;
907 trg->ctx_size = src->ctx_size;
908
909 trg->val.nodes = malloc(trg->used * sizeof *trg->val.nodes);
910 LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx); memset(trg, 0, sizeof *trg), );
911 memcpy(trg->val.nodes, src->val.nodes, src->used * sizeof *src->val.nodes);
912 if (src->ht) {
913 trg->ht = lyht_dup(src->ht);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200914 } else {
Michal Vaskod3678892020-05-21 10:06:58 +0200915 trg->ht = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200916 }
917 }
918}
919
920/**
921 * @brief Clear context of all schema nodes.
922 *
923 * @param[in] set Set to clear.
924 */
925static void
926set_scnode_clear_ctx(struct lyxp_set *set)
927{
928 uint32_t i;
929
930 for (i = 0; i < set->used; ++i) {
931 if (set->val.scnodes[i].in_ctx == 1) {
932 set->val.scnodes[i].in_ctx = 0;
Michal Vasko5c4e5892019-11-14 12:31:38 +0100933 } else if (set->val.scnodes[i].in_ctx == -2) {
934 set->val.scnodes[i].in_ctx = -1;
Michal Vasko03ff5a72019-09-11 13:49:33 +0200935 }
936 }
937}
938
939/**
940 * @brief Remove a node from a set. Removing last node changes
941 * set into LYXP_SET_EMPTY. Context position aware.
942 *
943 * @param[in] set Set to use.
944 * @param[in] idx Index from @p set of the node to be removed.
945 */
946static void
947set_remove_node(struct lyxp_set *set, uint32_t idx)
948{
949 assert(set && (set->type == LYXP_SET_NODE_SET));
950 assert(idx < set->used);
951
952 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
953
954 --set->used;
955 if (set->used) {
956 memmove(&set->val.nodes[idx], &set->val.nodes[idx + 1],
957 (set->used - idx) * sizeof *set->val.nodes);
958 } else {
Michal Vaskod3678892020-05-21 10:06:58 +0200959 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +0200960 }
961}
962
963/**
Michal Vasko2caefc12019-11-14 16:07:56 +0100964 * @brief Remove a node from a set by setting its type to LYXP_NODE_NONE.
Michal Vasko57eab132019-09-24 11:46:26 +0200965 *
966 * @param[in] set Set to use.
967 * @param[in] idx Index from @p set of the node to be removed.
968 */
969static void
Michal Vasko2caefc12019-11-14 16:07:56 +0100970set_remove_node_none(struct lyxp_set *set, uint32_t idx)
Michal Vasko57eab132019-09-24 11:46:26 +0200971{
972 assert(set && (set->type == LYXP_SET_NODE_SET));
973 assert(idx < set->used);
974
Michal Vasko2caefc12019-11-14 16:07:56 +0100975 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
976 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
977 }
978 set->val.nodes[idx].type = LYXP_NODE_NONE;
Michal Vasko57eab132019-09-24 11:46:26 +0200979}
980
981/**
Michal Vasko2caefc12019-11-14 16:07:56 +0100982 * @brief Remove all LYXP_NODE_NONE nodes from a set. Removing last node changes
Michal Vasko57eab132019-09-24 11:46:26 +0200983 * set into LYXP_SET_EMPTY. Context position aware.
984 *
985 * @param[in] set Set to consolidate.
986 */
987static void
Michal Vasko2caefc12019-11-14 16:07:56 +0100988set_remove_nodes_none(struct lyxp_set *set)
Michal Vasko57eab132019-09-24 11:46:26 +0200989{
Michal Vaskoed4fcfe2020-07-08 10:38:56 +0200990 uint16_t i, orig_used, end = 0;
Michal Vasko57eab132019-09-24 11:46:26 +0200991 int32_t start;
992
Michal Vaskod3678892020-05-21 10:06:58 +0200993 assert(set);
Michal Vasko57eab132019-09-24 11:46:26 +0200994
995 orig_used = set->used;
996 set->used = 0;
Michal Vaskod989ba02020-08-24 10:59:24 +0200997 for (i = 0; i < orig_used; ) {
Michal Vasko57eab132019-09-24 11:46:26 +0200998 start = -1;
999 do {
Michal Vasko2caefc12019-11-14 16:07:56 +01001000 if ((set->val.nodes[i].type != LYXP_NODE_NONE) && (start == -1)) {
Michal Vasko57eab132019-09-24 11:46:26 +02001001 start = i;
Michal Vasko2caefc12019-11-14 16:07:56 +01001002 } else if ((start > -1) && (set->val.nodes[i].type == LYXP_NODE_NONE)) {
Michal Vasko57eab132019-09-24 11:46:26 +02001003 end = i;
1004 ++i;
1005 break;
1006 }
1007
1008 ++i;
1009 if (i == orig_used) {
1010 end = i;
1011 }
1012 } while (i < orig_used);
1013
1014 if (start > -1) {
1015 /* move the whole chunk of valid nodes together */
1016 if (set->used != (unsigned)start) {
1017 memmove(&set->val.nodes[set->used], &set->val.nodes[start], (end - start) * sizeof *set->val.nodes);
1018 }
1019 set->used += end - start;
1020 }
1021 }
Michal Vasko57eab132019-09-24 11:46:26 +02001022}
1023
1024/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02001025 * @brief Check for duplicates in a node set.
1026 *
1027 * @param[in] set Set to check.
1028 * @param[in] node Node to look for in @p set.
1029 * @param[in] node_type Type of @p node.
1030 * @param[in] skip_idx Index from @p set to skip.
1031 * @return LY_ERR
1032 */
1033static LY_ERR
1034set_dup_node_check(const struct lyxp_set *set, const struct lyd_node *node, enum lyxp_node_type node_type, int skip_idx)
1035{
1036 uint32_t i;
1037
Michal Vasko2caefc12019-11-14 16:07:56 +01001038 if (set->ht && node) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001039 return set_dup_node_hash_check(set, (struct lyd_node *)node, node_type, skip_idx);
1040 }
1041
1042 for (i = 0; i < set->used; ++i) {
1043 if ((skip_idx > -1) && (i == (unsigned)skip_idx)) {
1044 continue;
1045 }
1046
1047 if ((set->val.nodes[i].node == node) && (set->val.nodes[i].type == node_type)) {
1048 return LY_EEXIST;
1049 }
1050 }
1051
1052 return LY_SUCCESS;
1053}
1054
Michal Vaskoecd62de2019-11-13 12:35:11 +01001055int
1056lyxp_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 +02001057{
1058 uint32_t i;
1059
1060 for (i = 0; i < set->used; ++i) {
1061 if ((skip_idx > -1) && (i == (unsigned)skip_idx)) {
1062 continue;
1063 }
1064
1065 if ((set->val.scnodes[i].scnode == node) && (set->val.scnodes[i].type == node_type)) {
1066 return i;
1067 }
1068 }
1069
1070 return -1;
1071}
1072
Michal Vaskoecd62de2019-11-13 12:35:11 +01001073void
1074lyxp_set_scnode_merge(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001075{
1076 uint32_t orig_used, i, j;
1077
Michal Vaskod3678892020-05-21 10:06:58 +02001078 assert((set1->type == LYXP_SET_SCNODE_SET) && (set2->type == LYXP_SET_SCNODE_SET));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001079
Michal Vaskod3678892020-05-21 10:06:58 +02001080 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001081 return;
1082 }
1083
Michal Vaskod3678892020-05-21 10:06:58 +02001084 if (!set1->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001085 memcpy(set1, set2, sizeof *set1);
1086 return;
1087 }
1088
1089 if (set1->used + set2->used > set1->size) {
1090 set1->size = set1->used + set2->used;
1091 set1->val.scnodes = ly_realloc(set1->val.scnodes, set1->size * sizeof *set1->val.scnodes);
1092 LY_CHECK_ERR_RET(!set1->val.scnodes, LOGMEM(set1->ctx), );
1093 }
1094
1095 orig_used = set1->used;
1096
1097 for (i = 0; i < set2->used; ++i) {
1098 for (j = 0; j < orig_used; ++j) {
1099 /* detect duplicities */
1100 if (set1->val.scnodes[j].scnode == set2->val.scnodes[i].scnode) {
1101 break;
1102 }
1103 }
1104
1105 if (j == orig_used) {
1106 memcpy(&set1->val.scnodes[set1->used], &set2->val.scnodes[i], sizeof *set2->val.scnodes);
1107 ++set1->used;
1108 }
1109 }
1110
Michal Vaskod3678892020-05-21 10:06:58 +02001111 lyxp_set_free_content(set2);
1112 set2->type = LYXP_SET_SCNODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001113}
1114
1115/**
1116 * @brief Insert a node into a set. Context position aware.
1117 *
1118 * @param[in] set Set to use.
1119 * @param[in] node Node to insert to @p set.
1120 * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting.
1121 * @param[in] node_type Node type of @p node.
1122 * @param[in] idx Index in @p set to insert into.
1123 */
1124static void
1125set_insert_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx)
1126{
Michal Vaskod3678892020-05-21 10:06:58 +02001127 assert(set && (set->type == LYXP_SET_NODE_SET));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001128
Michal Vaskod3678892020-05-21 10:06:58 +02001129 if (!set->size) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001130 /* first item */
1131 if (idx) {
1132 /* no real harm done, but it is a bug */
1133 LOGINT(set->ctx);
1134 idx = 0;
1135 }
1136 set->val.nodes = malloc(LYXP_SET_SIZE_START * sizeof *set->val.nodes);
1137 LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), );
1138 set->type = LYXP_SET_NODE_SET;
1139 set->used = 0;
1140 set->size = LYXP_SET_SIZE_START;
1141 set->ctx_pos = 1;
1142 set->ctx_size = 1;
1143 set->ht = NULL;
1144 } else {
1145 /* not an empty set */
1146 if (set->used == set->size) {
1147
1148 /* set is full */
1149 set->val.nodes = ly_realloc(set->val.nodes, (set->size + LYXP_SET_SIZE_STEP) * sizeof *set->val.nodes);
1150 LY_CHECK_ERR_RET(!set->val.nodes, LOGMEM(set->ctx), );
1151 set->size += LYXP_SET_SIZE_STEP;
1152 }
1153
1154 if (idx > set->used) {
1155 LOGINT(set->ctx);
1156 idx = set->used;
1157 }
1158
1159 /* make space for the new node */
1160 if (idx < set->used) {
1161 memmove(&set->val.nodes[idx + 1], &set->val.nodes[idx], (set->used - idx) * sizeof *set->val.nodes);
1162 }
1163 }
1164
1165 /* finally assign the value */
1166 set->val.nodes[idx].node = (struct lyd_node *)node;
1167 set->val.nodes[idx].type = node_type;
1168 set->val.nodes[idx].pos = pos;
1169 ++set->used;
1170
Michal Vasko2caefc12019-11-14 16:07:56 +01001171 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1172 set_insert_node_hash(set, (struct lyd_node *)node, node_type);
1173 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001174}
1175
Michal Vaskoecd62de2019-11-13 12:35:11 +01001176int
1177lyxp_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 +02001178{
1179 int ret;
1180
1181 assert(set->type == LYXP_SET_SCNODE_SET);
1182
Michal Vaskoecd62de2019-11-13 12:35:11 +01001183 ret = lyxp_set_scnode_dup_node_check(set, node, node_type, -1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001184 if (ret > -1) {
1185 set->val.scnodes[ret].in_ctx = 1;
1186 } else {
1187 if (set->used == set->size) {
1188 set->val.scnodes = ly_realloc(set->val.scnodes, (set->size + LYXP_SET_SIZE_STEP) * sizeof *set->val.scnodes);
1189 LY_CHECK_ERR_RET(!set->val.scnodes, LOGMEM(set->ctx), -1);
1190 set->size += LYXP_SET_SIZE_STEP;
1191 }
1192
1193 ret = set->used;
1194 set->val.scnodes[ret].scnode = (struct lysc_node *)node;
1195 set->val.scnodes[ret].type = node_type;
1196 set->val.scnodes[ret].in_ctx = 1;
1197 ++set->used;
1198 }
1199
1200 return ret;
1201}
1202
1203/**
1204 * @brief Replace a node in a set with another. Context position aware.
1205 *
1206 * @param[in] set Set to use.
1207 * @param[in] node Node to insert to @p set.
1208 * @param[in] pos Sort position of @p node. If left 0, it is filled just before sorting.
1209 * @param[in] node_type Node type of @p node.
1210 * @param[in] idx Index in @p set of the node to replace.
1211 */
1212static void
1213set_replace_node(struct lyxp_set *set, const struct lyd_node *node, uint32_t pos, enum lyxp_node_type node_type, uint32_t idx)
1214{
1215 assert(set && (idx < set->used));
1216
Michal Vasko2caefc12019-11-14 16:07:56 +01001217 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1218 set_remove_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1219 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001220 set->val.nodes[idx].node = (struct lyd_node *)node;
1221 set->val.nodes[idx].type = node_type;
1222 set->val.nodes[idx].pos = pos;
Michal Vasko2caefc12019-11-14 16:07:56 +01001223 if (set->val.nodes[idx].type == LYXP_NODE_ELEM) {
1224 set_insert_node_hash(set, set->val.nodes[idx].node, set->val.nodes[idx].type);
1225 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02001226}
1227
1228/**
1229 * @brief Set all nodes with ctx 1 to a new unique context value.
1230 *
1231 * @param[in] set Set to modify.
1232 * @return New context value.
1233 */
Michal Vasko5c4e5892019-11-14 12:31:38 +01001234static int32_t
Michal Vasko03ff5a72019-09-11 13:49:33 +02001235set_scnode_new_in_ctx(struct lyxp_set *set)
1236{
Michal Vasko5c4e5892019-11-14 12:31:38 +01001237 uint32_t i;
1238 int32_t ret_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001239
1240 assert(set->type == LYXP_SET_SCNODE_SET);
1241
1242 ret_ctx = 3;
1243retry:
1244 for (i = 0; i < set->used; ++i) {
1245 if (set->val.scnodes[i].in_ctx >= ret_ctx) {
1246 ret_ctx = set->val.scnodes[i].in_ctx + 1;
1247 goto retry;
1248 }
1249 }
1250 for (i = 0; i < set->used; ++i) {
1251 if (set->val.scnodes[i].in_ctx == 1) {
1252 set->val.scnodes[i].in_ctx = ret_ctx;
1253 }
1254 }
1255
1256 return ret_ctx;
1257}
1258
1259/**
1260 * @brief Get unique @p node position in the data.
1261 *
1262 * @param[in] node Node to find.
1263 * @param[in] node_type Node type of @p node.
1264 * @param[in] root Root node.
1265 * @param[in] root_type Type of the XPath @p root node.
1266 * @param[in] prev Node that we think is before @p node in DFS from @p root. Can optionally
1267 * be used to increase efficiency and start the DFS from this node.
1268 * @param[in] prev_pos Node @p prev position. Optional, but must be set if @p prev is set.
1269 * @return Node position.
1270 */
1271static uint32_t
1272get_node_pos(const struct lyd_node *node, enum lyxp_node_type node_type, const struct lyd_node *root,
Radek Krejci0f969882020-08-21 16:56:47 +02001273 enum lyxp_node_type root_type, const struct lyd_node **prev, uint32_t *prev_pos)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001274{
Michal Vasko56daf732020-08-10 10:57:18 +02001275 const struct lyd_node *elem = NULL, *top_sibling;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001276 uint32_t pos = 1;
1277
1278 assert(prev && prev_pos && !root->prev->next);
1279
1280 if ((node_type == LYXP_NODE_ROOT) || (node_type == LYXP_NODE_ROOT_CONFIG)) {
1281 return 0;
1282 }
1283
1284 if (*prev) {
1285 /* start from the previous element instead from the root */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001286 pos = *prev_pos;
Radek Krejci1e008d22020-08-17 11:37:37 +02001287 for (top_sibling = *prev; top_sibling->parent; top_sibling = (struct lyd_node *)top_sibling->parent) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001288 goto dfs_search;
1289 }
1290
1291 for (top_sibling = root; top_sibling; top_sibling = top_sibling->next) {
Michal Vasko56daf732020-08-10 10:57:18 +02001292 LYD_TREE_DFS_BEGIN(top_sibling, elem) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001293dfs_search:
Michal Vasko56daf732020-08-10 10:57:18 +02001294 if (*prev && !elem) {
1295 /* resume previous DFS */
1296 elem = LYD_TREE_DFS_next = (struct lyd_node *)*prev;
1297 LYD_TREE_DFS_continue = 0;
1298 }
1299
Michal Vasko03ff5a72019-09-11 13:49:33 +02001300 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (elem->schema->flags & LYS_CONFIG_R)) {
Michal Vasko56daf732020-08-10 10:57:18 +02001301 /* skip */
1302 LYD_TREE_DFS_continue = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001303 } else {
Michal Vasko56daf732020-08-10 10:57:18 +02001304 if (elem == node) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001305 break;
1306 }
Michal Vasko56daf732020-08-10 10:57:18 +02001307 ++pos;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001308 }
Michal Vasko56daf732020-08-10 10:57:18 +02001309
1310 LYD_TREE_DFS_END(top_sibling, elem);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001311 }
1312
1313 /* node found */
1314 if (elem) {
1315 break;
1316 }
1317 }
1318
1319 if (!elem) {
1320 if (!(*prev)) {
1321 /* we went from root and failed to find it, cannot be */
Michal Vaskob7be7a82020-08-20 09:09:04 +02001322 LOGINT(LYD_CTX(node));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001323 return 0;
1324 } else {
Michal Vasko56daf732020-08-10 10:57:18 +02001325 /* start the search again from the beginning */
1326 *prev = root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001327
Michal Vasko56daf732020-08-10 10:57:18 +02001328 top_sibling = root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001329 pos = 1;
1330 goto dfs_search;
1331 }
1332 }
1333
1334 /* remember the last found node for next time */
1335 *prev = node;
1336 *prev_pos = pos;
1337
1338 return pos;
1339}
1340
1341/**
1342 * @brief Assign (fill) missing node positions.
1343 *
1344 * @param[in] set Set to fill positions in.
1345 * @param[in] root Context root node.
1346 * @param[in] root_type Context root type.
1347 * @return LY_ERR
1348 */
1349static LY_ERR
1350set_assign_pos(struct lyxp_set *set, const struct lyd_node *root, enum lyxp_node_type root_type)
1351{
1352 const struct lyd_node *prev = NULL, *tmp_node;
1353 uint32_t i, tmp_pos = 0;
1354
1355 for (i = 0; i < set->used; ++i) {
1356 if (!set->val.nodes[i].pos) {
1357 tmp_node = NULL;
1358 switch (set->val.nodes[i].type) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001359 case LYXP_NODE_META:
1360 tmp_node = set->val.meta[i].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001361 if (!tmp_node) {
1362 LOGINT_RET(root->schema->module->ctx);
1363 }
Radek Krejci0f969882020-08-21 16:56:47 +02001364 /* fallthrough */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001365 case LYXP_NODE_ELEM:
1366 case LYXP_NODE_TEXT:
1367 if (!tmp_node) {
1368 tmp_node = set->val.nodes[i].node;
1369 }
1370 set->val.nodes[i].pos = get_node_pos(tmp_node, set->val.nodes[i].type, root, root_type, &prev, &tmp_pos);
1371 break;
1372 default:
1373 /* all roots have position 0 */
1374 break;
1375 }
1376 }
1377 }
1378
1379 return LY_SUCCESS;
1380}
1381
1382/**
Michal Vasko9f96a052020-03-10 09:41:45 +01001383 * @brief Get unique @p meta position in the parent metadata.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001384 *
Michal Vasko9f96a052020-03-10 09:41:45 +01001385 * @param[in] meta Metadata to use.
1386 * @return Metadata position.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001387 */
1388static uint16_t
Michal Vasko9f96a052020-03-10 09:41:45 +01001389get_meta_pos(struct lyd_meta *meta)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001390{
1391 uint16_t pos = 0;
Michal Vasko9f96a052020-03-10 09:41:45 +01001392 struct lyd_meta *meta2;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001393
Michal Vasko9f96a052020-03-10 09:41:45 +01001394 for (meta2 = meta->parent->meta; meta2 && (meta2 != meta); meta2 = meta2->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001395 ++pos;
1396 }
1397
Michal Vasko9f96a052020-03-10 09:41:45 +01001398 assert(meta2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001399 return pos;
1400}
1401
1402/**
1403 * @brief Compare 2 nodes in respect to XPath document order.
1404 *
1405 * @param[in] item1 1st node.
1406 * @param[in] item2 2nd node.
1407 * @return If 1st > 2nd returns 1, 1st == 2nd returns 0, and 1st < 2nd returns -1.
1408 */
1409static int
1410set_sort_compare(struct lyxp_set_node *item1, struct lyxp_set_node *item2)
1411{
Michal Vasko9f96a052020-03-10 09:41:45 +01001412 uint32_t meta_pos1 = 0, meta_pos2 = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001413
1414 if (item1->pos < item2->pos) {
1415 return -1;
1416 }
1417
1418 if (item1->pos > item2->pos) {
1419 return 1;
1420 }
1421
1422 /* node positions are equal, the fun case */
1423
1424 /* 1st ELEM - == - 2nd TEXT, 1st TEXT - == - 2nd ELEM */
1425 /* special case since text nodes are actually saved as their parents */
1426 if ((item1->node == item2->node) && (item1->type != item2->type)) {
1427 if (item1->type == LYXP_NODE_ELEM) {
1428 assert(item2->type == LYXP_NODE_TEXT);
1429 return -1;
1430 } else {
1431 assert((item1->type == LYXP_NODE_TEXT) && (item2->type == LYXP_NODE_ELEM));
1432 return 1;
1433 }
1434 }
1435
Michal Vasko9f96a052020-03-10 09:41:45 +01001436 /* we need meta positions now */
1437 if (item1->type == LYXP_NODE_META) {
1438 meta_pos1 = get_meta_pos((struct lyd_meta *)item1->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001439 }
Michal Vasko9f96a052020-03-10 09:41:45 +01001440 if (item2->type == LYXP_NODE_META) {
1441 meta_pos2 = get_meta_pos((struct lyd_meta *)item2->node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001442 }
1443
Michal Vasko9f96a052020-03-10 09:41:45 +01001444 /* 1st ROOT - 2nd ROOT, 1st ELEM - 2nd ELEM, 1st TEXT - 2nd TEXT, 1st META - =pos= - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001445 /* check for duplicates */
1446 if (item1->node == item2->node) {
Michal Vasko9f96a052020-03-10 09:41:45 +01001447 assert((item1->type == item2->type) && ((item1->type != LYXP_NODE_META) || (meta_pos1 == meta_pos2)));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001448 return 0;
1449 }
1450
Michal Vasko9f96a052020-03-10 09:41:45 +01001451 /* 1st ELEM - 2nd TEXT, 1st ELEM - any pos - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001452 /* elem is always first, 2nd node is after it */
1453 if (item1->type == LYXP_NODE_ELEM) {
1454 assert(item2->type != LYXP_NODE_ELEM);
1455 return -1;
1456 }
1457
Michal Vasko9f96a052020-03-10 09:41:45 +01001458 /* 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 +02001459 /* 2nd is before 1st */
1460 if (((item1->type == LYXP_NODE_TEXT)
Michal Vasko9f96a052020-03-10 09:41:45 +01001461 && ((item2->type == LYXP_NODE_ELEM) || (item2->type == LYXP_NODE_META)))
1462 || ((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_ELEM))
1463 || (((item1->type == LYXP_NODE_META) && (item2->type == LYXP_NODE_META))
1464 && (meta_pos1 > meta_pos2))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001465 return 1;
1466 }
1467
Michal Vasko9f96a052020-03-10 09:41:45 +01001468 /* 1st META - any pos - 2nd TEXT, 1st META <pos< - 2nd META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001469 /* 2nd is after 1st */
1470 return -1;
1471}
1472
1473/**
1474 * @brief Set cast for comparisons.
1475 *
1476 * @param[in] trg Target set to cast source into.
1477 * @param[in] src Source set.
1478 * @param[in] type Target set type.
1479 * @param[in] src_idx Source set node index.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001480 * @return LY_ERR
1481 */
1482static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001483set_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 +02001484{
1485 assert(src->type == LYXP_SET_NODE_SET);
1486
1487 set_init(trg, src);
1488
1489 /* insert node into target set */
1490 set_insert_node(trg, src->val.nodes[src_idx].node, src->val.nodes[src_idx].pos, src->val.nodes[src_idx].type, 0);
1491
1492 /* cast target set appropriately */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001493 return lyxp_set_cast(trg, type);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001494}
1495
Michal Vasko4c7763f2020-07-27 17:40:37 +02001496/**
1497 * @brief Set content canonization for comparisons.
1498 *
1499 * @param[in] trg Target set to put the canononized source into.
1500 * @param[in] src Source set.
1501 * @param[in] xp_node Source XPath node/meta to use for canonization.
1502 * @return LY_ERR
1503 */
1504static LY_ERR
1505set_comp_canonize(struct lyxp_set *trg, const struct lyxp_set *src, const struct lyxp_set_node *xp_node)
1506{
1507 struct lysc_type *type = NULL;
1508 struct lyd_value val;
1509 struct ly_err_item *err = NULL;
1510 char *str, *ptr;
Radek Krejci1deb5be2020-08-26 16:43:36 +02001511 uint8_t dynamic;
Michal Vasko4c7763f2020-07-27 17:40:37 +02001512 LY_ERR rc;
1513
1514 /* is there anything to canonize even? */
1515 if ((src->type == LYXP_SET_NUMBER) || (src->type == LYXP_SET_STRING)) {
1516 /* do we have a type to use for canonization? */
1517 if ((xp_node->type == LYXP_NODE_ELEM) && (xp_node->node->schema->nodetype & LYD_NODE_TERM)) {
1518 type = ((struct lyd_node_term *)xp_node->node)->value.realtype;
1519 } else if (xp_node->type == LYXP_NODE_META) {
1520 type = ((struct lyd_meta *)xp_node->node)->value.realtype;
1521 }
1522 }
1523 if (!type) {
1524 goto fill;
1525 }
1526
1527 if (src->type == LYXP_SET_NUMBER) {
1528 /* canonize number */
1529 if (asprintf(&str, "%Lf", src->val.num) == -1) {
1530 LOGMEM(src->ctx);
1531 return LY_EMEM;
1532 }
1533 } else {
1534 /* canonize string */
1535 str = strdup(src->val.str);
1536 }
1537
1538 /* ignore errors, the value may not satisfy schema constraints */
Michal Vaskoba99a3e2020-08-18 15:50:05 +02001539 rc = type->plugin->store(src->ctx, type, str, strlen(str), LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_DYNAMIC,
1540 LY_PREF_JSON, NULL, NULL, NULL, &val, &err);
Michal Vasko4c7763f2020-07-27 17:40:37 +02001541 ly_err_free(err);
1542 if (rc) {
1543 /* invalid value */
1544 free(str);
1545 goto fill;
1546 }
1547
1548 /* storing successful, now print the canonical value */
Michal Vaskoc8a230d2020-08-14 12:17:10 +02001549 str = (char *)type->plugin->print(&val, LY_PREF_JSON, NULL, &dynamic);
Michal Vasko4c7763f2020-07-27 17:40:37 +02001550
1551 /* use the canonized value */
1552 set_init(trg, src);
1553 trg->type = src->type;
1554 if (src->type == LYXP_SET_NUMBER) {
1555 trg->val.num = strtold(str, &ptr);
1556 if (dynamic) {
1557 free(str);
1558 }
1559 LY_CHECK_ERR_RET(ptr[0], LOGINT(src->ctx), LY_EINT);
1560 } else {
1561 trg->val.str = (dynamic ? str : strdup(str));
1562 }
1563 type->plugin->free(src->ctx, &val);
1564 return LY_SUCCESS;
1565
1566fill:
1567 /* no canonization needed/possible */
1568 set_fill_set(trg, src);
1569 return LY_SUCCESS;
1570}
1571
Michal Vasko03ff5a72019-09-11 13:49:33 +02001572#ifndef NDEBUG
1573
1574/**
1575 * @brief Bubble sort @p set into XPath document order.
1576 * Context position aware. Unused in the 'Release' build target.
1577 *
1578 * @param[in] set Set to sort.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001579 * @return How many times the whole set was traversed - 1 (if set was sorted, returns 0).
1580 */
1581static int
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001582set_sort(struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001583{
1584 uint32_t i, j;
Radek Krejci1deb5be2020-08-26 16:43:36 +02001585 int ret = 0, cmp;
1586 uint8_t inverted, change;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001587 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001588 struct lyxp_set_node item;
1589 struct lyxp_set_hash_node hnode;
1590 uint64_t hash;
1591
Michal Vasko3cf8fbf2020-05-27 15:21:21 +02001592 if ((set->type != LYXP_SET_NODE_SET) || (set->used < 2)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001593 return 0;
1594 }
1595
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001596 /* find first top-level node to be used as anchor for positions */
Radek Krejci1e008d22020-08-17 11:37:37 +02001597 for (root = set->ctx_node; root->parent; root = (const struct lyd_node *)root->parent) {}
Michal Vaskod989ba02020-08-24 10:59:24 +02001598 for ( ; root->prev->next; root = root->prev) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001599
1600 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001601 if (set_assign_pos(set, root, set->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001602 return -1;
1603 }
1604
1605 LOGDBG(LY_LDGXPATH, "SORT BEGIN");
1606 print_set_debug(set);
1607
1608 for (i = 0; i < set->used; ++i) {
1609 inverted = 0;
1610 change = 0;
1611
1612 for (j = 1; j < set->used - i; ++j) {
1613 /* compare node positions */
1614 if (inverted) {
1615 cmp = set_sort_compare(&set->val.nodes[j], &set->val.nodes[j - 1]);
1616 } else {
1617 cmp = set_sort_compare(&set->val.nodes[j - 1], &set->val.nodes[j]);
1618 }
1619
1620 /* swap if needed */
1621 if ((inverted && (cmp < 0)) || (!inverted && (cmp > 0))) {
1622 change = 1;
1623
1624 item = set->val.nodes[j - 1];
1625 set->val.nodes[j - 1] = set->val.nodes[j];
1626 set->val.nodes[j] = item;
1627 } else {
1628 /* whether node_pos1 should be smaller than node_pos2 or the other way around */
1629 inverted = !inverted;
1630 }
1631 }
1632
1633 ++ret;
1634
1635 if (!change) {
1636 break;
1637 }
1638 }
1639
1640 LOGDBG(LY_LDGXPATH, "SORT END %d", ret);
1641 print_set_debug(set);
1642
1643 /* check node hashes */
1644 if (set->used >= LYD_HT_MIN_ITEMS) {
1645 assert(set->ht);
1646 for (i = 0; i < set->used; ++i) {
1647 hnode.node = set->val.nodes[i].node;
1648 hnode.type = set->val.nodes[i].type;
1649
1650 hash = dict_hash_multi(0, (const char *)&hnode.node, sizeof hnode.node);
1651 hash = dict_hash_multi(hash, (const char *)&hnode.type, sizeof hnode.type);
1652 hash = dict_hash_multi(hash, NULL, 0);
1653
1654 assert(!lyht_find(set->ht, &hnode, hash, NULL));
1655 }
1656 }
1657
1658 return ret - 1;
1659}
1660
1661/**
1662 * @brief Remove duplicate entries in a sorted node set.
1663 *
1664 * @param[in] set Sorted set to check.
1665 * @return LY_ERR (LY_EEXIST if some duplicates are found)
1666 */
1667static LY_ERR
1668set_sorted_dup_node_clean(struct lyxp_set *set)
1669{
1670 uint32_t i = 0;
1671 LY_ERR ret = LY_SUCCESS;
1672
1673 if (set->used > 1) {
1674 while (i < set->used - 1) {
1675 if ((set->val.nodes[i].node == set->val.nodes[i + 1].node)
1676 && (set->val.nodes[i].type == set->val.nodes[i + 1].type)) {
Michal Vasko2caefc12019-11-14 16:07:56 +01001677 set_remove_node_none(set, i + 1);
Michal Vasko57eab132019-09-24 11:46:26 +02001678 ret = LY_EEXIST;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001679 }
Michal Vasko57eab132019-09-24 11:46:26 +02001680 ++i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001681 }
1682 }
1683
Michal Vasko2caefc12019-11-14 16:07:56 +01001684 set_remove_nodes_none(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001685 return ret;
1686}
1687
1688#endif
1689
1690/**
1691 * @brief Merge 2 sorted sets into one.
1692 *
1693 * @param[in,out] trg Set to merge into. Duplicates are removed.
1694 * @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 +02001695 * @return LY_ERR
1696 */
1697static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001698set_sorted_merge(struct lyxp_set *trg, struct lyxp_set *src)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001699{
1700 uint32_t i, j, k, count, dup_count;
1701 int cmp;
1702 const struct lyd_node *root;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001703
Michal Vaskod3678892020-05-21 10:06:58 +02001704 if ((trg->type != LYXP_SET_NODE_SET) || (src->type != LYXP_SET_NODE_SET)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001705 return LY_EINVAL;
1706 }
1707
Michal Vaskod3678892020-05-21 10:06:58 +02001708 if (!src->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001709 return LY_SUCCESS;
Michal Vaskod3678892020-05-21 10:06:58 +02001710 } else if (!trg->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001711 set_fill_set(trg, src);
Michal Vaskod3678892020-05-21 10:06:58 +02001712 lyxp_set_free_content(src);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001713 return LY_SUCCESS;
1714 }
1715
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001716 /* find first top-level node to be used as anchor for positions */
Radek Krejci1e008d22020-08-17 11:37:37 +02001717 for (root = trg->ctx_node; root->parent; root = (const struct lyd_node *)root->parent) {}
Michal Vaskod989ba02020-08-24 10:59:24 +02001718 for ( ; root->prev->next; root = root->prev) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02001719
1720 /* fill positions */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01001721 if (set_assign_pos(trg, root, trg->root_type) || set_assign_pos(src, root, src->root_type)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001722 return LY_EINT;
1723 }
1724
1725#ifndef NDEBUG
1726 LOGDBG(LY_LDGXPATH, "MERGE target");
1727 print_set_debug(trg);
1728 LOGDBG(LY_LDGXPATH, "MERGE source");
1729 print_set_debug(src);
1730#endif
1731
1732 /* make memory for the merge (duplicates are not detected yet, so space
1733 * will likely be wasted on them, too bad) */
1734 if (trg->size - trg->used < src->used) {
1735 trg->size = trg->used + src->used;
1736
1737 trg->val.nodes = ly_realloc(trg->val.nodes, trg->size * sizeof *trg->val.nodes);
1738 LY_CHECK_ERR_RET(!trg->val.nodes, LOGMEM(src->ctx), LY_EMEM);
1739 }
1740
1741 i = 0;
1742 j = 0;
1743 count = 0;
1744 dup_count = 0;
1745 do {
1746 cmp = set_sort_compare(&src->val.nodes[i], &trg->val.nodes[j]);
1747 if (!cmp) {
1748 if (!count) {
1749 /* duplicate, just skip it */
1750 ++i;
1751 ++j;
1752 } else {
1753 /* we are copying something already, so let's copy the duplicate too,
1754 * we are hoping that afterwards there are some more nodes to
1755 * copy and this way we can copy them all together */
1756 ++count;
1757 ++dup_count;
1758 ++i;
1759 ++j;
1760 }
1761 } else if (cmp < 0) {
1762 /* inserting src node into trg, just remember it for now */
1763 ++count;
1764 ++i;
1765
1766 /* insert the hash now */
1767 set_insert_node_hash(trg, src->val.nodes[i - 1].node, src->val.nodes[i - 1].type);
1768 } else if (count) {
1769copy_nodes:
1770 /* time to actually copy the nodes, we have found the largest block of nodes */
1771 memmove(&trg->val.nodes[j + (count - dup_count)],
1772 &trg->val.nodes[j],
1773 (trg->used - j) * sizeof *trg->val.nodes);
1774 memcpy(&trg->val.nodes[j - dup_count], &src->val.nodes[i - count], count * sizeof *src->val.nodes);
1775
1776 trg->used += count - dup_count;
1777 /* do not change i, except the copying above, we are basically doing exactly what is in the else branch below */
1778 j += count - dup_count;
1779
1780 count = 0;
1781 dup_count = 0;
1782 } else {
1783 ++j;
1784 }
1785 } while ((i < src->used) && (j < trg->used));
1786
1787 if ((i < src->used) || count) {
1788 /* insert all the hashes first */
1789 for (k = i; k < src->used; ++k) {
1790 set_insert_node_hash(trg, src->val.nodes[k].node, src->val.nodes[k].type);
1791 }
1792
1793 /* loop ended, but we need to copy something at trg end */
1794 count += src->used - i;
1795 i = src->used;
1796 goto copy_nodes;
1797 }
1798
1799 /* we are inserting hashes before the actual node insert, which causes
1800 * situations when there were initially not enough items for a hash table,
1801 * but even after some were inserted, hash table was not created (during
1802 * insertion the number of items is not updated yet) */
1803 if (!trg->ht && (trg->used >= LYD_HT_MIN_ITEMS)) {
1804 set_insert_node_hash(trg, NULL, 0);
1805 }
1806
1807#ifndef NDEBUG
1808 LOGDBG(LY_LDGXPATH, "MERGE result");
1809 print_set_debug(trg);
1810#endif
1811
Michal Vaskod3678892020-05-21 10:06:58 +02001812 lyxp_set_free_content(src);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001813 return LY_SUCCESS;
1814}
1815
1816/*
1817 * (re)parse functions
1818 *
1819 * Parse functions parse the expression into
1820 * tokens (syntactic analysis).
1821 *
1822 * Reparse functions perform semantic analysis
1823 * (do not save the result, just a check) of
1824 * the expression and fill repeat indices.
1825 */
1826
Michal Vasko14676352020-05-29 11:35:55 +02001827LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02001828lyxp_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 +02001829{
Michal Vasko004d3152020-06-11 19:59:22 +02001830 if (exp->used == tok_idx) {
Michal Vasko14676352020-05-29 11:35:55 +02001831 if (ctx) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001832 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOF);
1833 }
Michal Vasko14676352020-05-29 11:35:55 +02001834 return LY_EINCOMPLETE;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001835 }
1836
Michal Vasko004d3152020-06-11 19:59:22 +02001837 if (want_tok && (exp->tokens[tok_idx] != want_tok)) {
Michal Vasko14676352020-05-29 11:35:55 +02001838 if (ctx) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001839 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK,
Michal Vasko004d3152020-06-11 19:59:22 +02001840 lyxp_print_token(exp->tokens[tok_idx]), &exp->expr[exp->tok_pos[tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001841 }
Michal Vasko14676352020-05-29 11:35:55 +02001842 return LY_ENOT;
1843 }
1844
1845 return LY_SUCCESS;
1846}
1847
Michal Vasko004d3152020-06-11 19:59:22 +02001848LY_ERR
1849lyxp_next_token(const struct ly_ctx *ctx, const struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_token want_tok)
1850{
1851 LY_CHECK_RET(lyxp_check_token(ctx, exp, *tok_idx, want_tok));
1852
1853 /* skip the token */
1854 ++(*tok_idx);
1855
1856 return LY_SUCCESS;
1857}
1858
Michal Vasko14676352020-05-29 11:35:55 +02001859/* just like lyxp_check_token() but tests for 2 tokens */
1860static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02001861exp_check_token2(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t tok_idx, enum lyxp_token want_tok1,
Radek Krejci0f969882020-08-21 16:56:47 +02001862 enum lyxp_token want_tok2)
Michal Vasko14676352020-05-29 11:35:55 +02001863{
Michal Vasko004d3152020-06-11 19:59:22 +02001864 if (exp->used == tok_idx) {
Michal Vasko14676352020-05-29 11:35:55 +02001865 if (ctx) {
1866 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOF);
1867 }
1868 return LY_EINCOMPLETE;
1869 }
1870
Michal Vasko004d3152020-06-11 19:59:22 +02001871 if ((exp->tokens[tok_idx] != want_tok1) && (exp->tokens[tok_idx] != want_tok2)) {
Michal Vasko14676352020-05-29 11:35:55 +02001872 if (ctx) {
1873 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK,
Michal Vasko004d3152020-06-11 19:59:22 +02001874 lyxp_print_token(exp->tokens[tok_idx]), &exp->expr[exp->tok_pos[tok_idx]]);
Michal Vasko14676352020-05-29 11:35:55 +02001875 }
1876 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001877 }
1878
1879 return LY_SUCCESS;
1880}
1881
1882/**
1883 * @brief Stack operation push on the repeat array.
1884 *
1885 * @param[in] exp Expression to use.
Michal Vasko004d3152020-06-11 19:59:22 +02001886 * @param[in] tok_idx Position in the expresion \p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001887 * @param[in] repeat_op_idx Index from \p exp of the operator token. This value is pushed.
1888 */
1889static void
Michal Vasko004d3152020-06-11 19:59:22 +02001890exp_repeat_push(struct lyxp_expr *exp, uint16_t tok_idx, uint16_t repeat_op_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001891{
1892 uint16_t i;
1893
Michal Vasko004d3152020-06-11 19:59:22 +02001894 if (exp->repeat[tok_idx]) {
Radek Krejci1e008d22020-08-17 11:37:37 +02001895 for (i = 0; exp->repeat[tok_idx][i]; ++i) {}
Michal Vasko004d3152020-06-11 19:59:22 +02001896 exp->repeat[tok_idx] = realloc(exp->repeat[tok_idx], (i + 2) * sizeof *exp->repeat[tok_idx]);
1897 LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), );
1898 exp->repeat[tok_idx][i] = repeat_op_idx;
1899 exp->repeat[tok_idx][i + 1] = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001900 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02001901 exp->repeat[tok_idx] = calloc(2, sizeof *exp->repeat[tok_idx]);
1902 LY_CHECK_ERR_RET(!exp->repeat[tok_idx], LOGMEM(NULL), );
1903 exp->repeat[tok_idx][0] = repeat_op_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02001904 }
1905}
1906
1907/**
1908 * @brief Reparse Predicate. Logs directly on error.
1909 *
1910 * [7] Predicate ::= '[' Expr ']'
1911 *
1912 * @param[in] ctx Context for logging.
1913 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02001914 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001915 * @return LY_ERR
1916 */
1917static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02001918reparse_predicate(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001919{
1920 LY_ERR rc;
1921
Michal Vasko004d3152020-06-11 19:59:22 +02001922 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001923 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02001924 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001925
Michal Vasko004d3152020-06-11 19:59:22 +02001926 rc = reparse_or_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001927 LY_CHECK_RET(rc);
1928
Michal Vasko004d3152020-06-11 19:59:22 +02001929 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_BRACK2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001930 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02001931 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001932
1933 return LY_SUCCESS;
1934}
1935
1936/**
1937 * @brief Reparse RelativeLocationPath. Logs directly on error.
1938 *
1939 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
1940 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
1941 * [6] NodeTest ::= NameTest | NodeType '(' ')'
1942 *
1943 * @param[in] ctx Context for logging.
1944 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02001945 * @param[in] tok_idx Position in the expression \p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02001946 * @return LY_ERR (LY_EINCOMPLETE on forward reference)
1947 */
1948static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02001949reparse_relative_location_path(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02001950{
1951 LY_ERR rc;
1952
Michal Vasko004d3152020-06-11 19:59:22 +02001953 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001954 LY_CHECK_RET(rc);
1955
1956 goto step;
1957 do {
1958 /* '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02001959 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001960
Michal Vasko004d3152020-06-11 19:59:22 +02001961 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001962 LY_CHECK_RET(rc);
1963step:
1964 /* Step */
Michal Vasko004d3152020-06-11 19:59:22 +02001965 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001966 case LYXP_TOKEN_DOT:
Michal Vasko004d3152020-06-11 19:59:22 +02001967 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001968 break;
1969
1970 case LYXP_TOKEN_DDOT:
Michal Vasko004d3152020-06-11 19:59:22 +02001971 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001972 break;
1973
1974 case LYXP_TOKEN_AT:
Michal Vasko004d3152020-06-11 19:59:22 +02001975 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001976
Michal Vasko004d3152020-06-11 19:59:22 +02001977 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001978 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02001979 if ((exp->tokens[*tok_idx] != LYXP_TOKEN_NAMETEST) && (exp->tokens[*tok_idx] != LYXP_TOKEN_NODETYPE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001980 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK,
Michal Vasko004d3152020-06-11 19:59:22 +02001981 lyxp_print_token(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001982 return LY_EVALID;
1983 }
Radek Krejci0f969882020-08-21 16:56:47 +02001984 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001985 case LYXP_TOKEN_NAMETEST:
Michal Vasko004d3152020-06-11 19:59:22 +02001986 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001987 goto reparse_predicate;
1988 break;
1989
1990 case LYXP_TOKEN_NODETYPE:
Michal Vasko004d3152020-06-11 19:59:22 +02001991 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001992
1993 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02001994 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001995 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02001996 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001997
1998 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02001999 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002000 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002001 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002002
2003reparse_predicate:
2004 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002005 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
2006 rc = reparse_predicate(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002007 LY_CHECK_RET(rc);
2008 }
2009 break;
2010 default:
2011 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK,
Michal Vasko004d3152020-06-11 19:59:22 +02002012 lyxp_print_token(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002013 return LY_EVALID;
2014 }
Michal Vasko004d3152020-06-11 19:59:22 +02002015 } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02002016
2017 return LY_SUCCESS;
2018}
2019
2020/**
2021 * @brief Reparse AbsoluteLocationPath. Logs directly on error.
2022 *
2023 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
2024 *
2025 * @param[in] ctx Context for logging.
2026 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002027 * @param[in] tok_idx Position in the expression \p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002028 * @return LY_ERR
2029 */
2030static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002031reparse_absolute_location_path(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002032{
2033 LY_ERR rc;
2034
Michal Vasko004d3152020-06-11 19:59:22 +02002035 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 +02002036
2037 /* '/' RelativeLocationPath? */
Michal Vasko004d3152020-06-11 19:59:22 +02002038 if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002039 /* '/' */
Michal Vasko004d3152020-06-11 19:59:22 +02002040 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002041
Michal Vasko004d3152020-06-11 19:59:22 +02002042 if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002043 return LY_SUCCESS;
2044 }
Michal Vasko004d3152020-06-11 19:59:22 +02002045 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002046 case LYXP_TOKEN_DOT:
2047 case LYXP_TOKEN_DDOT:
2048 case LYXP_TOKEN_AT:
2049 case LYXP_TOKEN_NAMETEST:
2050 case LYXP_TOKEN_NODETYPE:
Michal Vasko004d3152020-06-11 19:59:22 +02002051 rc = reparse_relative_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002052 LY_CHECK_RET(rc);
Radek Krejci0f969882020-08-21 16:56:47 +02002053 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02002054 default:
2055 break;
2056 }
2057
Michal Vasko03ff5a72019-09-11 13:49:33 +02002058 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02002059 /* '//' RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002060 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002061
Michal Vasko004d3152020-06-11 19:59:22 +02002062 rc = reparse_relative_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002063 LY_CHECK_RET(rc);
2064 }
2065
2066 return LY_SUCCESS;
2067}
2068
2069/**
2070 * @brief Reparse FunctionCall. Logs directly on error.
2071 *
2072 * [9] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
2073 *
2074 * @param[in] ctx Context for logging.
2075 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002076 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002077 * @return LY_ERR
2078 */
2079static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002080reparse_function_call(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002081{
Radek Krejci1deb5be2020-08-26 16:43:36 +02002082 int8_t min_arg_count = -1;
2083 uint32_t arg_count, max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002084 uint16_t func_tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002085 LY_ERR rc;
2086
Michal Vasko004d3152020-06-11 19:59:22 +02002087 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_FUNCNAME);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002088 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002089 func_tok_idx = *tok_idx;
2090 switch (exp->tok_len[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002091 case 3:
Michal Vasko004d3152020-06-11 19:59:22 +02002092 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002093 min_arg_count = 1;
2094 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002095 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002096 min_arg_count = 1;
2097 max_arg_count = 1;
2098 }
2099 break;
2100 case 4:
Michal Vasko004d3152020-06-11 19:59:22 +02002101 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002102 min_arg_count = 1;
2103 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002104 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002105 min_arg_count = 0;
2106 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002107 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002108 min_arg_count = 0;
2109 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002110 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002111 min_arg_count = 0;
2112 max_arg_count = 0;
2113 }
2114 break;
2115 case 5:
Michal Vasko004d3152020-06-11 19:59:22 +02002116 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002117 min_arg_count = 1;
2118 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002119 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002120 min_arg_count = 0;
2121 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002122 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002123 min_arg_count = 1;
2124 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002125 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002126 min_arg_count = 1;
2127 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002128 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002129 min_arg_count = 1;
2130 max_arg_count = 1;
2131 }
2132 break;
2133 case 6:
Michal Vasko004d3152020-06-11 19:59:22 +02002134 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002135 min_arg_count = 2;
Radek Krejci1deb5be2020-08-26 16:43:36 +02002136 max_arg_count = UINT32_MAX;
Michal Vasko004d3152020-06-11 19:59:22 +02002137 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002138 min_arg_count = 0;
2139 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002140 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002141 min_arg_count = 0;
2142 max_arg_count = 1;
2143 }
2144 break;
2145 case 7:
Michal Vasko004d3152020-06-11 19:59:22 +02002146 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002147 min_arg_count = 1;
2148 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002149 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002150 min_arg_count = 1;
2151 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002152 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002153 min_arg_count = 0;
2154 max_arg_count = 0;
2155 }
2156 break;
2157 case 8:
Michal Vasko004d3152020-06-11 19:59:22 +02002158 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002159 min_arg_count = 2;
2160 max_arg_count = 2;
Michal Vasko004d3152020-06-11 19:59:22 +02002161 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002162 min_arg_count = 0;
2163 max_arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002164 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002165 min_arg_count = 2;
2166 max_arg_count = 2;
2167 }
2168 break;
2169 case 9:
Michal Vasko004d3152020-06-11 19:59:22 +02002170 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002171 min_arg_count = 2;
2172 max_arg_count = 3;
Michal Vasko004d3152020-06-11 19:59:22 +02002173 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002174 min_arg_count = 3;
2175 max_arg_count = 3;
2176 }
2177 break;
2178 case 10:
Michal Vasko004d3152020-06-11 19:59:22 +02002179 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002180 min_arg_count = 0;
2181 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002182 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002183 min_arg_count = 1;
2184 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002185 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002186 min_arg_count = 2;
2187 max_arg_count = 2;
2188 }
2189 break;
2190 case 11:
Michal Vasko004d3152020-06-11 19:59:22 +02002191 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002192 min_arg_count = 2;
2193 max_arg_count = 2;
2194 }
2195 break;
2196 case 12:
Michal Vasko004d3152020-06-11 19:59:22 +02002197 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002198 min_arg_count = 2;
2199 max_arg_count = 2;
2200 }
2201 break;
2202 case 13:
Michal Vasko004d3152020-06-11 19:59:22 +02002203 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002204 min_arg_count = 0;
2205 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002206 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002207 min_arg_count = 0;
2208 max_arg_count = 1;
2209 }
2210 break;
2211 case 15:
Michal Vasko004d3152020-06-11 19:59:22 +02002212 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002213 min_arg_count = 0;
2214 max_arg_count = 1;
Michal Vasko004d3152020-06-11 19:59:22 +02002215 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002216 min_arg_count = 2;
2217 max_arg_count = 2;
2218 }
2219 break;
2220 case 16:
Michal Vasko004d3152020-06-11 19:59:22 +02002221 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002222 min_arg_count = 2;
2223 max_arg_count = 2;
2224 }
2225 break;
2226 case 20:
Michal Vasko004d3152020-06-11 19:59:22 +02002227 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002228 min_arg_count = 2;
2229 max_arg_count = 2;
2230 }
2231 break;
2232 }
2233 if (min_arg_count == -1) {
Michal Vasko004d3152020-06-11 19:59:22 +02002234 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 +02002235 return LY_EINVAL;
2236 }
Michal Vasko004d3152020-06-11 19:59:22 +02002237 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002238
2239 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02002240 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002241 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002242 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002243
2244 /* ( Expr ( ',' Expr )* )? */
2245 arg_count = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002246 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002247 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002248 if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002249 ++arg_count;
Michal Vasko004d3152020-06-11 19:59:22 +02002250 rc = reparse_or_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002251 LY_CHECK_RET(rc);
2252 }
Michal Vasko004d3152020-06-11 19:59:22 +02002253 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) {
2254 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002255
2256 ++arg_count;
Michal Vasko004d3152020-06-11 19:59:22 +02002257 rc = reparse_or_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002258 LY_CHECK_RET(rc);
2259 }
2260
2261 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02002262 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002263 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002264 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002265
Radek Krejci1deb5be2020-08-26 16:43:36 +02002266 if ((arg_count < (uint8_t)min_arg_count) || (arg_count > max_arg_count)) {
Michal Vasko004d3152020-06-11 19:59:22 +02002267 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INARGCOUNT, arg_count, exp->tok_len[func_tok_idx],
2268 &exp->expr[exp->tok_pos[func_tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002269 return LY_EVALID;
2270 }
2271
2272 return LY_SUCCESS;
2273}
2274
2275/**
2276 * @brief Reparse PathExpr. Logs directly on error.
2277 *
2278 * [10] PathExpr ::= LocationPath | PrimaryExpr Predicate*
2279 * | PrimaryExpr Predicate* '/' RelativeLocationPath
2280 * | PrimaryExpr Predicate* '//' RelativeLocationPath
2281 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
2282 * [8] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall
2283 *
2284 * @param[in] ctx Context for logging.
2285 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002286 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002287 * @return LY_ERR
2288 */
2289static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002290reparse_path_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002291{
2292 LY_ERR rc;
2293
Michal Vasko004d3152020-06-11 19:59:22 +02002294 if (lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko14676352020-05-29 11:35:55 +02002295 return LY_EVALID;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002296 }
2297
Michal Vasko004d3152020-06-11 19:59:22 +02002298 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002299 case LYXP_TOKEN_PAR1:
2300 /* '(' Expr ')' Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002301 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002302
Michal Vasko004d3152020-06-11 19:59:22 +02002303 rc = reparse_or_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002304 LY_CHECK_RET(rc);
2305
Michal Vasko004d3152020-06-11 19:59:22 +02002306 rc = lyxp_check_token(ctx, exp, *tok_idx, LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002307 LY_CHECK_RET(rc);
Michal Vasko004d3152020-06-11 19:59:22 +02002308 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002309 goto predicate;
2310 break;
2311 case LYXP_TOKEN_DOT:
2312 case LYXP_TOKEN_DDOT:
2313 case LYXP_TOKEN_AT:
2314 case LYXP_TOKEN_NAMETEST:
2315 case LYXP_TOKEN_NODETYPE:
2316 /* RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002317 rc = reparse_relative_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002318 LY_CHECK_RET(rc);
2319 break;
2320 case LYXP_TOKEN_FUNCNAME:
2321 /* FunctionCall */
Michal Vasko004d3152020-06-11 19:59:22 +02002322 rc = reparse_function_call(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002323 LY_CHECK_RET(rc);
2324 goto predicate;
2325 break;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002326 case LYXP_TOKEN_OPER_PATH:
2327 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02002328 /* AbsoluteLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002329 rc = reparse_absolute_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002330 LY_CHECK_RET(rc);
2331 break;
2332 case LYXP_TOKEN_LITERAL:
2333 /* Literal */
Michal Vasko004d3152020-06-11 19:59:22 +02002334 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002335 goto predicate;
2336 break;
2337 case LYXP_TOKEN_NUMBER:
2338 /* Number */
Michal Vasko004d3152020-06-11 19:59:22 +02002339 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002340 goto predicate;
2341 break;
2342 default:
2343 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INTOK,
Michal Vasko004d3152020-06-11 19:59:22 +02002344 lyxp_print_token(exp->tokens[*tok_idx]), &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002345 return LY_EVALID;
2346 }
2347
2348 return LY_SUCCESS;
2349
2350predicate:
2351 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02002352 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
2353 rc = reparse_predicate(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002354 LY_CHECK_RET(rc);
2355 }
2356
2357 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02002358 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002359
2360 /* '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02002361 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002362
Michal Vasko004d3152020-06-11 19:59:22 +02002363 rc = reparse_relative_location_path(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002364 LY_CHECK_RET(rc);
2365 }
2366
2367 return LY_SUCCESS;
2368}
2369
2370/**
2371 * @brief Reparse UnaryExpr. Logs directly on error.
2372 *
2373 * [17] UnaryExpr ::= UnionExpr | '-' UnaryExpr
2374 * [18] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
2375 *
2376 * @param[in] ctx Context for logging.
2377 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002378 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002379 * @return LY_ERR
2380 */
2381static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002382reparse_unary_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002383{
2384 uint16_t prev_exp;
2385 LY_ERR rc;
2386
2387 /* ('-')* */
Michal Vasko004d3152020-06-11 19:59:22 +02002388 prev_exp = *tok_idx;
2389 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH)
2390 && (exp->expr[exp->tok_pos[*tok_idx]] == '-')) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002391 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNARY);
Michal Vasko004d3152020-06-11 19:59:22 +02002392 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002393 }
2394
2395 /* PathExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002396 prev_exp = *tok_idx;
2397 rc = reparse_path_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002398 LY_CHECK_RET(rc);
2399
2400 /* ('|' PathExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002401 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_UNI)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002402 exp_repeat_push(exp, prev_exp, LYXP_EXPR_UNION);
Michal Vasko004d3152020-06-11 19:59:22 +02002403 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002404
Michal Vasko004d3152020-06-11 19:59:22 +02002405 rc = reparse_path_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002406 LY_CHECK_RET(rc);
2407 }
2408
2409 return LY_SUCCESS;
2410}
2411
2412/**
2413 * @brief Reparse AdditiveExpr. Logs directly on error.
2414 *
2415 * [15] AdditiveExpr ::= MultiplicativeExpr
2416 * | AdditiveExpr '+' MultiplicativeExpr
2417 * | AdditiveExpr '-' MultiplicativeExpr
2418 * [16] MultiplicativeExpr ::= UnaryExpr
2419 * | MultiplicativeExpr '*' UnaryExpr
2420 * | MultiplicativeExpr 'div' UnaryExpr
2421 * | MultiplicativeExpr 'mod' UnaryExpr
2422 *
2423 * @param[in] ctx Context for logging.
2424 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002425 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002426 * @return LY_ERR
2427 */
2428static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002429reparse_additive_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002430{
2431 uint16_t prev_add_exp, prev_mul_exp;
2432 LY_ERR rc;
2433
Michal Vasko004d3152020-06-11 19:59:22 +02002434 prev_add_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002435 goto reparse_multiplicative_expr;
2436
2437 /* ('+' / '-' MultiplicativeExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002438 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH)
2439 && ((exp->expr[exp->tok_pos[*tok_idx]] == '+') || (exp->expr[exp->tok_pos[*tok_idx]] == '-'))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002440 exp_repeat_push(exp, prev_add_exp, LYXP_EXPR_ADDITIVE);
Michal Vasko004d3152020-06-11 19:59:22 +02002441 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002442
2443reparse_multiplicative_expr:
2444 /* UnaryExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002445 prev_mul_exp = *tok_idx;
2446 rc = reparse_unary_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002447 LY_CHECK_RET(rc);
2448
2449 /* ('*' / 'div' / 'mod' UnaryExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002450 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_MATH)
2451 && ((exp->expr[exp->tok_pos[*tok_idx]] == '*') || (exp->tok_len[*tok_idx] == 3))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002452 exp_repeat_push(exp, prev_mul_exp, LYXP_EXPR_MULTIPLICATIVE);
Michal Vasko004d3152020-06-11 19:59:22 +02002453 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002454
Michal Vasko004d3152020-06-11 19:59:22 +02002455 rc = reparse_unary_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002456 LY_CHECK_RET(rc);
2457 }
2458 }
2459
2460 return LY_SUCCESS;
2461}
2462
2463/**
2464 * @brief Reparse EqualityExpr. Logs directly on error.
2465 *
2466 * [13] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
2467 * | EqualityExpr '!=' RelationalExpr
2468 * [14] RelationalExpr ::= AdditiveExpr
2469 * | RelationalExpr '<' AdditiveExpr
2470 * | RelationalExpr '>' AdditiveExpr
2471 * | RelationalExpr '<=' AdditiveExpr
2472 * | RelationalExpr '>=' AdditiveExpr
2473 *
2474 * @param[in] ctx Context for logging.
2475 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002476 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002477 * @return LY_ERR
2478 */
2479static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002480reparse_equality_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002481{
2482 uint16_t prev_eq_exp, prev_rel_exp;
2483 LY_ERR rc;
2484
Michal Vasko004d3152020-06-11 19:59:22 +02002485 prev_eq_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002486 goto reparse_additive_expr;
2487
2488 /* ('=' / '!=' RelationalExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002489 while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_EQUAL, LYXP_TOKEN_OPER_NEQUAL)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002490 exp_repeat_push(exp, prev_eq_exp, LYXP_EXPR_EQUALITY);
Michal Vasko004d3152020-06-11 19:59:22 +02002491 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002492
2493reparse_additive_expr:
2494 /* AdditiveExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002495 prev_rel_exp = *tok_idx;
2496 rc = reparse_additive_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002497 LY_CHECK_RET(rc);
2498
2499 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002500 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_COMP)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002501 exp_repeat_push(exp, prev_rel_exp, LYXP_EXPR_RELATIONAL);
Michal Vasko004d3152020-06-11 19:59:22 +02002502 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002503
Michal Vasko004d3152020-06-11 19:59:22 +02002504 rc = reparse_additive_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002505 LY_CHECK_RET(rc);
2506 }
2507 }
2508
2509 return LY_SUCCESS;
2510}
2511
2512/**
2513 * @brief Reparse OrExpr. Logs directly on error.
2514 *
2515 * [11] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
2516 * [12] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
2517 *
2518 * @param[in] ctx Context for logging.
2519 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02002520 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002521 * @return LY_ERR
2522 */
2523static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02002524reparse_or_expr(const struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx)
Michal Vasko03ff5a72019-09-11 13:49:33 +02002525{
2526 uint16_t prev_or_exp, prev_and_exp;
2527 LY_ERR rc;
2528
Michal Vasko004d3152020-06-11 19:59:22 +02002529 prev_or_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002530 goto reparse_equality_expr;
2531
2532 /* ('or' AndExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002533 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 +02002534 exp_repeat_push(exp, prev_or_exp, LYXP_EXPR_OR);
Michal Vasko004d3152020-06-11 19:59:22 +02002535 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002536
2537reparse_equality_expr:
2538 /* EqualityExpr */
Michal Vasko004d3152020-06-11 19:59:22 +02002539 prev_and_exp = *tok_idx;
2540 rc = reparse_equality_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002541 LY_CHECK_RET(rc);
2542
2543 /* ('and' EqualityExpr)* */
Michal Vasko004d3152020-06-11 19:59:22 +02002544 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 +02002545 exp_repeat_push(exp, prev_and_exp, LYXP_EXPR_AND);
Michal Vasko004d3152020-06-11 19:59:22 +02002546 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002547
Michal Vasko004d3152020-06-11 19:59:22 +02002548 rc = reparse_equality_expr(ctx, exp, tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002549 LY_CHECK_RET(rc);
2550 }
2551 }
2552
2553 return LY_SUCCESS;
2554}
Radek Krejcib1646a92018-11-02 16:08:26 +01002555
2556/**
2557 * @brief Parse NCName.
2558 *
2559 * @param[in] ncname Name to parse.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002560 * @return Length of @p ncname valid bytes.
Radek Krejcib1646a92018-11-02 16:08:26 +01002561 */
Radek Krejcid4270262019-01-07 15:07:25 +01002562static long int
Radek Krejcib1646a92018-11-02 16:08:26 +01002563parse_ncname(const char *ncname)
2564{
Radek Krejci1deb5be2020-08-26 16:43:36 +02002565 uint32_t uc;
Radek Krejcid4270262019-01-07 15:07:25 +01002566 size_t size;
2567 long int len = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002568
2569 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), 0);
2570 if (!is_xmlqnamestartchar(uc) || (uc == ':')) {
2571 return len;
2572 }
2573
2574 do {
2575 len += size;
Radek Krejci9a564c92019-01-07 14:53:57 +01002576 if (!*ncname) {
2577 break;
2578 }
Radek Krejcid4270262019-01-07 15:07:25 +01002579 LY_CHECK_RET(ly_getutf8(&ncname, &uc, &size), -len);
Radek Krejcib1646a92018-11-02 16:08:26 +01002580 } while (is_xmlqnamechar(uc) && (uc != ':'));
2581
2582 return len;
2583}
2584
2585/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02002586 * @brief Add @p token into the expression @p exp.
Radek Krejcib1646a92018-11-02 16:08:26 +01002587 *
Michal Vasko03ff5a72019-09-11 13:49:33 +02002588 * @param[in] ctx Context for logging.
Radek Krejcib1646a92018-11-02 16:08:26 +01002589 * @param[in] exp Expression to use.
2590 * @param[in] token Token to add.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002591 * @param[in] tok_pos Token position in the XPath expression.
Radek Krejcib1646a92018-11-02 16:08:26 +01002592 * @param[in] tok_len Token length in the XPath expression.
Michal Vasko03ff5a72019-09-11 13:49:33 +02002593 * @return LY_ERR
Radek Krejcib1646a92018-11-02 16:08:26 +01002594 */
2595static LY_ERR
Michal Vasko14676352020-05-29 11:35:55 +02002596exp_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 +01002597{
2598 uint32_t prev;
2599
2600 if (exp->used == exp->size) {
2601 prev = exp->size;
2602 exp->size += LYXP_EXPR_SIZE_STEP;
2603 if (prev > exp->size) {
2604 LOGINT(ctx);
2605 return LY_EINT;
2606 }
2607
2608 exp->tokens = ly_realloc(exp->tokens, exp->size * sizeof *exp->tokens);
2609 LY_CHECK_ERR_RET(!exp->tokens, LOGMEM(ctx), LY_EMEM);
2610 exp->tok_pos = ly_realloc(exp->tok_pos, exp->size * sizeof *exp->tok_pos);
2611 LY_CHECK_ERR_RET(!exp->tok_pos, LOGMEM(ctx), LY_EMEM);
2612 exp->tok_len = ly_realloc(exp->tok_len, exp->size * sizeof *exp->tok_len);
2613 LY_CHECK_ERR_RET(!exp->tok_len, LOGMEM(ctx), LY_EMEM);
2614 }
2615
2616 exp->tokens[exp->used] = token;
Michal Vasko03ff5a72019-09-11 13:49:33 +02002617 exp->tok_pos[exp->used] = tok_pos;
Radek Krejcib1646a92018-11-02 16:08:26 +01002618 exp->tok_len[exp->used] = tok_len;
2619 ++exp->used;
2620 return LY_SUCCESS;
2621}
2622
2623void
Michal Vasko14676352020-05-29 11:35:55 +02002624lyxp_expr_free(const struct ly_ctx *ctx, struct lyxp_expr *expr)
Radek Krejcib1646a92018-11-02 16:08:26 +01002625{
2626 uint16_t i;
2627
2628 if (!expr) {
2629 return;
2630 }
2631
2632 lydict_remove(ctx, expr->expr);
2633 free(expr->tokens);
2634 free(expr->tok_pos);
2635 free(expr->tok_len);
2636 if (expr->repeat) {
2637 for (i = 0; i < expr->used; ++i) {
2638 free(expr->repeat[i]);
2639 }
2640 }
2641 free(expr->repeat);
2642 free(expr);
2643}
2644
2645struct lyxp_expr *
Radek Krejci1deb5be2020-08-26 16:43:36 +02002646lyxp_expr_parse(const struct ly_ctx *ctx, const char *expr, size_t expr_len, uint8_t reparse)
Radek Krejcib1646a92018-11-02 16:08:26 +01002647{
2648 struct lyxp_expr *ret;
Radek Krejcid4270262019-01-07 15:07:25 +01002649 size_t parsed = 0, tok_len;
Radek Krejcib1646a92018-11-02 16:08:26 +01002650 enum lyxp_token tok_type;
Radek Krejci1deb5be2020-08-26 16:43:36 +02002651 uint8_t prev_function_check = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02002652 uint16_t tok_idx = 0;
Radek Krejcib1646a92018-11-02 16:08:26 +01002653
Michal Vasko004d3152020-06-11 19:59:22 +02002654 if (!expr[0]) {
2655 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOF);
2656 return NULL;
2657 }
2658
2659 if (!expr_len) {
2660 expr_len = strlen(expr);
2661 }
2662 if (expr_len > UINT16_MAX) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002663 LOGERR(ctx, LY_EINVAL, "XPath expression cannot be longer than %ud characters.", UINT16_MAX);
2664 return NULL;
2665 }
2666
2667 /* init lyxp_expr structure */
2668 ret = calloc(1, sizeof *ret);
2669 LY_CHECK_ERR_GOTO(!ret, LOGMEM(ctx), error);
Michal Vasko004d3152020-06-11 19:59:22 +02002670 ret->expr = lydict_insert(ctx, expr, expr_len);
Radek Krejcib1646a92018-11-02 16:08:26 +01002671 LY_CHECK_ERR_GOTO(!ret->expr, LOGMEM(ctx), error);
2672 ret->used = 0;
2673 ret->size = LYXP_EXPR_SIZE_START;
2674 ret->tokens = malloc(ret->size * sizeof *ret->tokens);
2675 LY_CHECK_ERR_GOTO(!ret->tokens, LOGMEM(ctx), error);
2676
2677 ret->tok_pos = malloc(ret->size * sizeof *ret->tok_pos);
2678 LY_CHECK_ERR_GOTO(!ret->tok_pos, LOGMEM(ctx), error);
2679
2680 ret->tok_len = malloc(ret->size * sizeof *ret->tok_len);
2681 LY_CHECK_ERR_GOTO(!ret->tok_len, LOGMEM(ctx), error);
2682
Michal Vasko004d3152020-06-11 19:59:22 +02002683 /* make expr 0-terminated */
2684 expr = ret->expr;
2685
Radek Krejcib1646a92018-11-02 16:08:26 +01002686 while (is_xmlws(expr[parsed])) {
2687 ++parsed;
2688 }
2689
2690 do {
2691 if (expr[parsed] == '(') {
2692
2693 /* '(' */
2694 tok_len = 1;
2695 tok_type = LYXP_TOKEN_PAR1;
2696
2697 if (prev_function_check && ret->used && (ret->tokens[ret->used - 1] == LYXP_TOKEN_NAMETEST)) {
2698 /* it is a NodeType/FunctionName after all */
2699 if (((ret->tok_len[ret->used - 1] == 4)
2700 && (!strncmp(&expr[ret->tok_pos[ret->used - 1]], "node", 4)
2701 || !strncmp(&expr[ret->tok_pos[ret->used - 1]], "text", 4))) ||
2702 ((ret->tok_len[ret->used - 1] == 7)
2703 && !strncmp(&expr[ret->tok_pos[ret->used - 1]], "comment", 7))) {
2704 ret->tokens[ret->used - 1] = LYXP_TOKEN_NODETYPE;
2705 } else {
2706 ret->tokens[ret->used - 1] = LYXP_TOKEN_FUNCNAME;
2707 }
2708 prev_function_check = 0;
2709 }
2710
2711 } else if (expr[parsed] == ')') {
2712
2713 /* ')' */
2714 tok_len = 1;
2715 tok_type = LYXP_TOKEN_PAR2;
2716
2717 } else if (expr[parsed] == '[') {
2718
2719 /* '[' */
2720 tok_len = 1;
2721 tok_type = LYXP_TOKEN_BRACK1;
2722
2723 } else if (expr[parsed] == ']') {
2724
2725 /* ']' */
2726 tok_len = 1;
2727 tok_type = LYXP_TOKEN_BRACK2;
2728
2729 } else if (!strncmp(&expr[parsed], "..", 2)) {
2730
2731 /* '..' */
2732 tok_len = 2;
2733 tok_type = LYXP_TOKEN_DDOT;
2734
2735 } else if ((expr[parsed] == '.') && (!isdigit(expr[parsed + 1]))) {
2736
2737 /* '.' */
2738 tok_len = 1;
2739 tok_type = LYXP_TOKEN_DOT;
2740
2741 } else if (expr[parsed] == '@') {
2742
2743 /* '@' */
2744 tok_len = 1;
2745 tok_type = LYXP_TOKEN_AT;
2746
2747 } else if (expr[parsed] == ',') {
2748
2749 /* ',' */
2750 tok_len = 1;
2751 tok_type = LYXP_TOKEN_COMMA;
2752
2753 } else if (expr[parsed] == '\'') {
2754
2755 /* Literal with ' */
Radek Krejci1e008d22020-08-17 11:37:37 +02002756 for (tok_len = 1; (expr[parsed + tok_len] != '\0') && (expr[parsed + tok_len] != '\''); ++tok_len) {}
Radek Krejcib1646a92018-11-02 16:08:26 +01002757 LY_CHECK_ERR_GOTO(expr[parsed + tok_len] == '\0',
2758 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOE, expr[parsed], &expr[parsed]), error);
2759 ++tok_len;
2760 tok_type = LYXP_TOKEN_LITERAL;
2761
2762 } else if (expr[parsed] == '\"') {
2763
2764 /* Literal with " */
Radek Krejci1e008d22020-08-17 11:37:37 +02002765 for (tok_len = 1; (expr[parsed + tok_len] != '\0') && (expr[parsed + tok_len] != '\"'); ++tok_len) {}
Radek Krejcib1646a92018-11-02 16:08:26 +01002766 LY_CHECK_ERR_GOTO(expr[parsed + tok_len] == '\0',
2767 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_EOE, expr[parsed], &expr[parsed]), error);
2768 ++tok_len;
2769 tok_type = LYXP_TOKEN_LITERAL;
2770
2771 } else if ((expr[parsed] == '.') || (isdigit(expr[parsed]))) {
2772
2773 /* Number */
Radek Krejci1e008d22020-08-17 11:37:37 +02002774 for (tok_len = 0; isdigit(expr[parsed + tok_len]); ++tok_len) {}
Radek Krejcib1646a92018-11-02 16:08:26 +01002775 if (expr[parsed + tok_len] == '.') {
2776 ++tok_len;
Michal Vaskod989ba02020-08-24 10:59:24 +02002777 for ( ; isdigit(expr[parsed + tok_len]); ++tok_len) {}
Radek Krejcib1646a92018-11-02 16:08:26 +01002778 }
2779 tok_type = LYXP_TOKEN_NUMBER;
2780
2781 } else if (expr[parsed] == '/') {
2782
2783 /* Operator '/', '//' */
2784 if (!strncmp(&expr[parsed], "//", 2)) {
2785 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002786 tok_type = LYXP_TOKEN_OPER_RPATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002787 } else {
2788 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002789 tok_type = LYXP_TOKEN_OPER_PATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002790 }
Radek Krejcib1646a92018-11-02 16:08:26 +01002791
Michal Vaskod989ba02020-08-24 10:59:24 +02002792 } else if (!strncmp(&expr[parsed], "!=", 2)) {
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 = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002796 tok_type = LYXP_TOKEN_OPER_NEQUAL;
2797
2798 } else if (!strncmp(&expr[parsed], "<=", 2) || !strncmp(&expr[parsed], ">=", 2)) {
2799
2800 /* Operator '<=', '>=' */
2801 tok_len = 2;
2802 tok_type = LYXP_TOKEN_OPER_COMP;
Radek Krejcib1646a92018-11-02 16:08:26 +01002803
2804 } else if (expr[parsed] == '|') {
2805
2806 /* Operator '|' */
2807 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002808 tok_type = LYXP_TOKEN_OPER_UNI;
Radek Krejcib1646a92018-11-02 16:08:26 +01002809
2810 } else if ((expr[parsed] == '+') || (expr[parsed] == '-')) {
2811
2812 /* Operator '+', '-' */
2813 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002814 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002815
Michal Vasko3e48bf32020-06-01 08:39:07 +02002816 } else if (expr[parsed] == '=') {
Radek Krejcib1646a92018-11-02 16:08:26 +01002817
Michal Vasko3e48bf32020-06-01 08:39:07 +02002818 /* Operator '=' */
Radek Krejcib1646a92018-11-02 16:08:26 +01002819 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002820 tok_type = LYXP_TOKEN_OPER_EQUAL;
2821
2822 } else if ((expr[parsed] == '<') || (expr[parsed] == '>')) {
2823
2824 /* Operator '<', '>' */
2825 tok_len = 1;
2826 tok_type = LYXP_TOKEN_OPER_COMP;
Radek Krejcib1646a92018-11-02 16:08:26 +01002827
2828 } else if (ret->used && (ret->tokens[ret->used - 1] != LYXP_TOKEN_AT)
2829 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_PAR1)
2830 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_BRACK1)
2831 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_COMMA)
Michal Vasko3e48bf32020-06-01 08:39:07 +02002832 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_LOG)
2833 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_EQUAL)
2834 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_NEQUAL)
2835 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_COMP)
2836 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_MATH)
2837 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_UNI)
2838 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_PATH)
2839 && (ret->tokens[ret->used - 1] != LYXP_TOKEN_OPER_RPATH)) {
Radek Krejcib1646a92018-11-02 16:08:26 +01002840
2841 /* Operator '*', 'or', 'and', 'mod', or 'div' */
2842 if (expr[parsed] == '*') {
2843 tok_len = 1;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002844 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002845
2846 } else if (!strncmp(&expr[parsed], "or", 2)) {
2847 tok_len = 2;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002848 tok_type = LYXP_TOKEN_OPER_LOG;
Radek Krejcib1646a92018-11-02 16:08:26 +01002849
2850 } else if (!strncmp(&expr[parsed], "and", 3)) {
2851 tok_len = 3;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002852 tok_type = LYXP_TOKEN_OPER_LOG;
Radek Krejcib1646a92018-11-02 16:08:26 +01002853
2854 } else if (!strncmp(&expr[parsed], "mod", 3) || !strncmp(&expr[parsed], "div", 3)) {
2855 tok_len = 3;
Michal Vasko3e48bf32020-06-01 08:39:07 +02002856 tok_type = LYXP_TOKEN_OPER_MATH;
Radek Krejcib1646a92018-11-02 16:08:26 +01002857
2858 } else if (prev_function_check) {
Michal Vasko53078572019-05-24 08:50:15 +02002859 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "Invalid character 0x%x ('%c'), perhaps \"%.*s\" is supposed to be a function call.",
2860 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 +01002861 goto error;
2862 } else {
Radek Krejcid4270262019-01-07 15:07:25 +01002863 LOGVAL(ctx, LY_VLOG_NONE, NULL, LY_VCODE_XP_INEXPR, parsed + 1, expr);
Radek Krejcib1646a92018-11-02 16:08:26 +01002864 goto error;
2865 }
2866 } else if (expr[parsed] == '*') {
2867
2868 /* NameTest '*' */
2869 tok_len = 1;
2870 tok_type = LYXP_TOKEN_NAMETEST;
2871
2872 } else {
2873
2874 /* NameTest (NCName ':' '*' | QName) or NodeType/FunctionName */
Radek Krejci1deb5be2020-08-26 16:43:36 +02002875 long int ncname_len = parse_ncname(&expr[parsed]);
Radek Krejcid4270262019-01-07 15:07:25 +01002876 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 +01002877 tok_len = ncname_len;
2878
2879 if (expr[parsed + tok_len] == ':') {
2880 ++tok_len;
2881 if (expr[parsed + tok_len] == '*') {
2882 ++tok_len;
2883 } else {
2884 ncname_len = parse_ncname(&expr[parsed + tok_len]);
Radek Krejcid4270262019-01-07 15:07:25 +01002885 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 +01002886 tok_len += ncname_len;
2887 }
2888 /* remove old flag to prevent ambiguities */
2889 prev_function_check = 0;
2890 tok_type = LYXP_TOKEN_NAMETEST;
2891 } else {
2892 /* there is no prefix so it can still be NodeType/FunctionName, we can't finally decide now */
2893 prev_function_check = 1;
2894 tok_type = LYXP_TOKEN_NAMETEST;
2895 }
2896 }
2897
2898 /* store the token, move on to the next one */
2899 LY_CHECK_GOTO(exp_add_token(ctx, ret, tok_type, parsed, tok_len), error);
2900 parsed += tok_len;
2901 while (is_xmlws(expr[parsed])) {
2902 ++parsed;
2903 }
2904
2905 } while (expr[parsed]);
2906
Michal Vasko004d3152020-06-11 19:59:22 +02002907 if (reparse) {
2908 /* prealloc repeat */
2909 ret->repeat = calloc(ret->size, sizeof *ret->repeat);
2910 LY_CHECK_ERR_GOTO(!ret->repeat, LOGMEM(ctx), error);
Radek Krejcib1646a92018-11-02 16:08:26 +01002911
Michal Vasko004d3152020-06-11 19:59:22 +02002912 /* fill repeat */
2913 LY_CHECK_GOTO(reparse_or_expr(ctx, ret, &tok_idx), error);
2914 if (ret->used > tok_idx) {
2915 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_XPATH, "Unparsed characters \"%s\" left at the end of an XPath expression.",
2916 &ret->expr[ret->tok_pos[tok_idx]]);
2917 goto error;
2918 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02002919 }
2920
2921 print_expr_struct_debug(ret);
2922
Radek Krejcib1646a92018-11-02 16:08:26 +01002923 return ret;
2924
2925error:
2926 lyxp_expr_free(ctx, ret);
2927 return NULL;
2928}
2929
Michal Vasko004d3152020-06-11 19:59:22 +02002930struct lyxp_expr *
2931lyxp_expr_dup(const struct ly_ctx *ctx, const struct lyxp_expr *exp)
2932{
2933 struct lyxp_expr *dup;
2934 uint32_t i, j;
2935
2936 dup = calloc(1, sizeof *dup);
2937 LY_CHECK_ERR_GOTO(!dup, LOGMEM(ctx), error);
2938
2939 dup->tokens = malloc(exp->used * sizeof *dup->tokens);
2940 LY_CHECK_ERR_GOTO(!dup->tokens, LOGMEM(ctx), error);
2941 memcpy(dup->tokens, exp->tokens, exp->used * sizeof *dup->tokens);
2942
2943 dup->tok_pos = malloc(exp->used * sizeof *dup->tok_pos);
2944 LY_CHECK_ERR_GOTO(!dup->tok_pos, LOGMEM(ctx), error);
2945 memcpy(dup->tok_pos, exp->tok_pos, exp->used * sizeof *dup->tok_pos);
2946
2947 dup->tok_len = malloc(exp->used * sizeof *dup->tok_len);
2948 LY_CHECK_ERR_GOTO(!dup->tok_len, LOGMEM(ctx), error);
2949 memcpy(dup->tok_len, exp->tok_len, exp->used * sizeof *dup->tok_len);
2950
2951 dup->repeat = malloc(exp->used * sizeof *dup->repeat);
2952 LY_CHECK_ERR_GOTO(!dup->repeat, LOGMEM(ctx), error);
2953 for (i = 0; i < exp->used; ++i) {
2954 if (!exp->repeat[i]) {
2955 dup->repeat[i] = NULL;
2956 } else {
Radek Krejci1e008d22020-08-17 11:37:37 +02002957 for (j = 0; exp->repeat[i][j]; ++j) {}
Michal Vasko004d3152020-06-11 19:59:22 +02002958 /* the ending 0 as well */
2959 ++j;
2960
Michal Vasko99c71642020-07-03 13:33:36 +02002961 dup->repeat[i] = malloc(j * sizeof **dup->repeat);
Michal Vasko004d3152020-06-11 19:59:22 +02002962 LY_CHECK_ERR_GOTO(!dup->repeat[i], LOGMEM(ctx), error);
2963 memcpy(dup->repeat[i], exp->repeat[i], j * sizeof **dup->repeat);
2964 dup->repeat[i][j - 1] = 0;
2965 }
2966 }
2967
2968 dup->used = exp->used;
2969 dup->size = exp->used;
2970 dup->expr = lydict_insert(ctx, exp->expr, 0);
2971
2972 return dup;
2973
2974error:
2975 lyxp_expr_free(ctx, dup);
2976 return NULL;
2977}
2978
Michal Vasko03ff5a72019-09-11 13:49:33 +02002979/*
2980 * warn functions
2981 *
2982 * Warn functions check specific reasonable conditions for schema XPath
2983 * and print a warning if they are not satisfied.
2984 */
2985
2986/**
2987 * @brief Get the last-added schema node that is currently in the context.
2988 *
2989 * @param[in] set Set to search in.
2990 * @return Last-added schema context node, NULL if no node is in context.
2991 */
2992static struct lysc_node *
2993warn_get_scnode_in_ctx(struct lyxp_set *set)
2994{
2995 uint32_t i;
2996
2997 if (!set || (set->type != LYXP_SET_SCNODE_SET)) {
2998 return NULL;
2999 }
3000
3001 i = set->used;
3002 do {
3003 --i;
3004 if (set->val.scnodes[i].in_ctx == 1) {
3005 /* if there are more, simply return the first found (last added) */
3006 return set->val.scnodes[i].scnode;
3007 }
3008 } while (i);
3009
3010 return NULL;
3011}
3012
3013/**
3014 * @brief Test whether a type is numeric - integer type or decimal64.
3015 *
3016 * @param[in] type Type to test.
3017 * @return 1 if numeric, 0 otherwise.
3018 */
Radek Krejci1deb5be2020-08-26 16:43:36 +02003019static uint8_t
Michal Vasko03ff5a72019-09-11 13:49:33 +02003020warn_is_numeric_type(struct lysc_type *type)
3021{
3022 struct lysc_type_union *uni;
Radek Krejci1deb5be2020-08-26 16:43:36 +02003023 uint8_t ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003024 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003025
3026 switch (type->basetype) {
3027 case LY_TYPE_DEC64:
3028 case LY_TYPE_INT8:
3029 case LY_TYPE_UINT8:
3030 case LY_TYPE_INT16:
3031 case LY_TYPE_UINT16:
3032 case LY_TYPE_INT32:
3033 case LY_TYPE_UINT32:
3034 case LY_TYPE_INT64:
3035 case LY_TYPE_UINT64:
3036 return 1;
3037 case LY_TYPE_UNION:
3038 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003039 LY_ARRAY_FOR(uni->types, u) {
3040 ret = warn_is_numeric_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003041 if (ret) {
3042 /* found a suitable type */
3043 return 1;
3044 }
3045 }
3046 /* did not find any suitable type */
3047 return 0;
3048 case LY_TYPE_LEAFREF:
3049 return warn_is_numeric_type(((struct lysc_type_leafref *)type)->realtype);
3050 default:
3051 return 0;
3052 }
3053}
3054
3055/**
3056 * @brief Test whether a type is string-like - no integers, decimal64 or binary.
3057 *
3058 * @param[in] type Type to test.
3059 * @return 1 if string, 0 otherwise.
3060 */
Radek Krejci1deb5be2020-08-26 16:43:36 +02003061static uint8_t
Michal Vasko03ff5a72019-09-11 13:49:33 +02003062warn_is_string_type(struct lysc_type *type)
3063{
3064 struct lysc_type_union *uni;
Radek Krejci1deb5be2020-08-26 16:43:36 +02003065 uint8_t ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003066 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003067
3068 switch (type->basetype) {
3069 case LY_TYPE_BITS:
3070 case LY_TYPE_ENUM:
3071 case LY_TYPE_IDENT:
3072 case LY_TYPE_INST:
3073 case LY_TYPE_STRING:
3074 return 1;
3075 case LY_TYPE_UNION:
3076 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003077 LY_ARRAY_FOR(uni->types, u) {
3078 ret = warn_is_string_type(uni->types[u]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003079 if (ret) {
3080 /* found a suitable type */
3081 return 1;
3082 }
3083 }
3084 /* did not find any suitable type */
3085 return 0;
3086 case LY_TYPE_LEAFREF:
3087 return warn_is_string_type(((struct lysc_type_leafref *)type)->realtype);
3088 default:
3089 return 0;
3090 }
3091}
3092
3093/**
3094 * @brief Test whether a type is one specific type.
3095 *
3096 * @param[in] type Type to test.
3097 * @param[in] base Expected type.
3098 * @return 1 if it is, 0 otherwise.
3099 */
Radek Krejci1deb5be2020-08-26 16:43:36 +02003100static uint8_t
Michal Vasko03ff5a72019-09-11 13:49:33 +02003101warn_is_specific_type(struct lysc_type *type, LY_DATA_TYPE base)
3102{
3103 struct lysc_type_union *uni;
Radek Krejci1deb5be2020-08-26 16:43:36 +02003104 uint8_t ret;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003105 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003106
3107 if (type->basetype == base) {
3108 return 1;
3109 } else if (type->basetype == LY_TYPE_UNION) {
3110 uni = (struct lysc_type_union *)type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003111 LY_ARRAY_FOR(uni->types, u) {
3112 ret = warn_is_specific_type(uni->types[u], base);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003113 if (ret) {
3114 /* found a suitable type */
3115 return 1;
3116 }
3117 }
3118 /* did not find any suitable type */
3119 return 0;
3120 } else if (type->basetype == LY_TYPE_LEAFREF) {
3121 return warn_is_specific_type(((struct lysc_type_leafref *)type)->realtype, base);
3122 }
3123
3124 return 0;
3125}
3126
3127/**
3128 * @brief Get next type of a (union) type.
3129 *
3130 * @param[in] type Base type.
3131 * @param[in] prev_type Previously returned type.
3132 * @return Next type or NULL.
3133 */
3134static struct lysc_type *
3135warn_is_equal_type_next_type(struct lysc_type *type, struct lysc_type *prev_type)
3136{
3137 struct lysc_type_union *uni;
Radek Krejci1deb5be2020-08-26 16:43:36 +02003138 uint8_t found = 0;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003139 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003140
3141 switch (type->basetype) {
3142 case LY_TYPE_UNION:
3143 uni = (struct lysc_type_union *)type;
3144 if (!prev_type) {
3145 return uni->types[0];
3146 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003147 LY_ARRAY_FOR(uni->types, u) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003148 if (found) {
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003149 return uni->types[u];
Michal Vasko03ff5a72019-09-11 13:49:33 +02003150 }
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003151 if (prev_type == uni->types[u]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003152 found = 1;
3153 }
3154 }
3155 return NULL;
3156 default:
3157 if (prev_type) {
3158 assert(type == prev_type);
3159 return NULL;
3160 } else {
3161 return type;
3162 }
3163 }
3164}
3165
3166/**
3167 * @brief Test whether 2 types have a common type.
3168 *
3169 * @param[in] type1 First type.
3170 * @param[in] type2 Second type.
3171 * @return 1 if they do, 0 otherwise.
3172 */
3173static int
3174warn_is_equal_type(struct lysc_type *type1, struct lysc_type *type2)
3175{
3176 struct lysc_type *t1, *rt1, *t2, *rt2;
3177
3178 t1 = NULL;
3179 while ((t1 = warn_is_equal_type_next_type(type1, t1))) {
3180 if (t1->basetype == LY_TYPE_LEAFREF) {
3181 rt1 = ((struct lysc_type_leafref *)t1)->realtype;
3182 } else {
3183 rt1 = t1;
3184 }
3185
3186 t2 = NULL;
3187 while ((t2 = warn_is_equal_type_next_type(type2, t2))) {
3188 if (t2->basetype == LY_TYPE_LEAFREF) {
3189 rt2 = ((struct lysc_type_leafref *)t2)->realtype;
3190 } else {
3191 rt2 = t2;
3192 }
3193
3194 if (rt2->basetype == rt1->basetype) {
3195 /* match found */
3196 return 1;
3197 }
3198 }
3199 }
3200
3201 return 0;
3202}
3203
3204/**
3205 * @brief Check both operands of comparison operators.
3206 *
3207 * @param[in] ctx Context for errors.
3208 * @param[in] set1 First operand set.
3209 * @param[in] set2 Second operand set.
3210 * @param[in] numbers_only Whether accept only numbers or other types are fine too (for '=' and '!=').
3211 * @param[in] expr Start of the expression to print with the warning.
3212 * @param[in] tok_pos Token position.
3213 */
3214static void
Radek Krejci1deb5be2020-08-26 16:43:36 +02003215warn_operands(struct ly_ctx *ctx, struct lyxp_set *set1, struct lyxp_set *set2, uint8_t numbers_only, const char *expr, uint16_t tok_pos)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003216{
3217 struct lysc_node_leaf *node1, *node2;
Radek Krejci1deb5be2020-08-26 16:43:36 +02003218 uint8_t leaves = 1, warning = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003219
3220 node1 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set1);
3221 node2 = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set2);
3222
3223 if (!node1 && !node2) {
3224 /* no node-sets involved, nothing to do */
3225 return;
3226 }
3227
3228 if (node1) {
3229 if (!(node1->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3230 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node1->nodetype), node1->name);
3231 warning = 1;
3232 leaves = 0;
3233 } else if (numbers_only && !warn_is_numeric_type(node1->type)) {
3234 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node1->name);
3235 warning = 1;
3236 }
3237 }
3238
3239 if (node2) {
3240 if (!(node2->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3241 LOGWRN(ctx, "Node type %s \"%s\" used as operand.", lys_nodetype2str(node2->nodetype), node2->name);
3242 warning = 1;
3243 leaves = 0;
3244 } else if (numbers_only && !warn_is_numeric_type(node2->type)) {
3245 LOGWRN(ctx, "Node \"%s\" is not of a numeric type, but used where it was expected.", node2->name);
3246 warning = 1;
3247 }
3248 }
3249
3250 if (node1 && node2 && leaves && !numbers_only) {
3251 if ((warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type))
3252 || (!warn_is_numeric_type(node1->type) && warn_is_numeric_type(node2->type))
3253 || (!warn_is_numeric_type(node1->type) && !warn_is_numeric_type(node2->type)
3254 && !warn_is_equal_type(node1->type, node2->type))) {
3255 LOGWRN(ctx, "Incompatible types of operands \"%s\" and \"%s\" for comparison.", node1->name, node2->name);
3256 warning = 1;
3257 }
3258 }
3259
3260 if (warning) {
3261 LOGWRN(ctx, "Previous warning generated by XPath subexpression[%u] \"%.20s\".", tok_pos, expr + tok_pos);
3262 }
3263}
3264
3265/**
3266 * @brief Check that a value is valid for a leaf. If not applicable, does nothing.
3267 *
3268 * @param[in] exp Parsed XPath expression.
3269 * @param[in] set Set with the leaf/leaf-list.
3270 * @param[in] val_exp Index of the value (literal/number) in @p exp.
3271 * @param[in] equal_exp Index of the start of the equality expression in @p exp.
3272 * @param[in] last_equal_exp Index of the end of the equality expression in @p exp.
3273 */
3274static void
3275warn_equality_value(struct lyxp_expr *exp, struct lyxp_set *set, uint16_t val_exp, uint16_t equal_exp, uint16_t last_equal_exp)
3276{
3277 struct lysc_node *scnode;
3278 struct lysc_type *type;
3279 char *value;
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003280 struct lyd_value storage;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003281 LY_ERR rc;
3282 struct ly_err_item *err = NULL;
3283
3284 if ((scnode = warn_get_scnode_in_ctx(set)) && (scnode->nodetype & (LYS_LEAF | LYS_LEAFLIST))
3285 && ((exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) || (exp->tokens[val_exp] == LYXP_TOKEN_NUMBER))) {
3286 /* check that the node can have the specified value */
3287 if (exp->tokens[val_exp] == LYXP_TOKEN_LITERAL) {
3288 value = strndup(exp->expr + exp->tok_pos[val_exp] + 1, exp->tok_len[val_exp] - 2);
3289 } else {
3290 value = strndup(exp->expr + exp->tok_pos[val_exp], exp->tok_len[val_exp]);
3291 }
3292 if (!value) {
3293 LOGMEM(set->ctx);
3294 return;
3295 }
3296
3297 if ((((struct lysc_node_leaf *)scnode)->type->basetype == LY_TYPE_IDENT) && !strchr(value, ':')) {
3298 LOGWRN(set->ctx, "Identityref \"%s\" comparison with identity \"%s\" without prefix, consider adding"
3299 " a prefix or best using \"derived-from(-or-self)()\" functions.", scnode->name, value);
3300 LOGWRN(set->ctx, "Previous warning generated by XPath subexpression[%u] \"%.*s\".", exp->tok_pos[equal_exp],
Radek Krejci0f969882020-08-21 16:56:47 +02003301 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
Michal Vasko03ff5a72019-09-11 13:49:33 +02003302 exp->expr + exp->tok_pos[equal_exp]);
3303 }
3304
3305 type = ((struct lysc_node_leaf *)scnode)->type;
3306 if (type->basetype != LY_TYPE_IDENT) {
Michal Vaskoc8a230d2020-08-14 12:17:10 +02003307 rc = type->plugin->store(set->ctx, type, value, strlen(value), LY_TYPE_OPTS_SCHEMA, LY_PREF_SCHEMA,
Radek Krejci0f969882020-08-21 16:56:47 +02003308 (void *)set->local_mod, NULL, NULL, &storage, &err);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003309
3310 if (err) {
3311 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type (%s).", value, err->msg);
3312 ly_err_free(err);
3313 } else if (rc != LY_SUCCESS) {
3314 LOGWRN(set->ctx, "Invalid value \"%s\" which does not fit the type.", value);
3315 }
3316 if (rc != LY_SUCCESS) {
3317 LOGWRN(set->ctx, "Previous warning generated by XPath subexpression[%u] \"%.*s\".", exp->tok_pos[equal_exp],
Radek Krejci0f969882020-08-21 16:56:47 +02003318 (exp->tok_pos[last_equal_exp] - exp->tok_pos[equal_exp]) + exp->tok_len[last_equal_exp],
Michal Vasko03ff5a72019-09-11 13:49:33 +02003319 exp->expr + exp->tok_pos[equal_exp]);
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003320 } else {
3321 type->plugin->free(set->ctx, &storage);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003322 }
3323 }
3324 free(value);
3325 }
3326}
3327
3328/*
3329 * XPath functions
3330 */
3331
3332/**
3333 * @brief Execute the YANG 1.1 bit-is-set(node-set, string) function. Returns LYXP_SET_BOOLEAN
3334 * depending on whether the first node bit value from the second argument is set.
3335 *
3336 * @param[in] args Array of arguments.
3337 * @param[in] arg_count Count of elements in @p args.
3338 * @param[in,out] set Context and result set at the same time.
3339 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003340 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003341 */
3342static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003343xpath_bit_is_set(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003344{
3345 struct lyd_node_term *leaf;
3346 struct lysc_node_leaf *sleaf;
3347 struct lysc_type_bits *bits;
3348 LY_ERR rc = LY_SUCCESS;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003349 LY_ARRAY_COUNT_TYPE u;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003350
3351 if (options & LYXP_SCNODE_ALL) {
3352 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3353 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003354 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3355 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 +02003356 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_BITS)) {
3357 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"bits\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003358 }
3359
3360 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3361 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3362 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 +02003363 } else if (!warn_is_string_type(sleaf->type)) {
3364 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003365 }
3366 }
3367 set_scnode_clear_ctx(set);
3368 return rc;
3369 }
3370
Michal Vaskod3678892020-05-21 10:06:58 +02003371 if (args[0]->type != LYXP_SET_NODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003372 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)");
3373 return LY_EVALID;
3374 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003375 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003376 LY_CHECK_RET(rc);
3377
3378 set_fill_boolean(set, 0);
Michal Vaskod3678892020-05-21 10:06:58 +02003379 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003380 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
3381 if ((leaf->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST))
3382 && (((struct lysc_node_leaf *)leaf->schema)->type->basetype == LY_TYPE_BITS)) {
3383 bits = (struct lysc_type_bits *)((struct lysc_node_leaf *)leaf->schema)->type;
Radek Krejci7eb54ba2020-05-18 16:30:04 +02003384 LY_ARRAY_FOR(bits->bits, u) {
3385 if (!strcmp(bits->bits[u].name, args[1]->val.str)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003386 set_fill_boolean(set, 1);
3387 break;
3388 }
3389 }
3390 }
3391 }
3392
3393 return LY_SUCCESS;
3394}
3395
3396/**
3397 * @brief Execute the XPath boolean(object) function. Returns LYXP_SET_BOOLEAN
3398 * with the argument converted to boolean.
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
Radek Krejci1deb5be2020-08-26 16:43:36 +02003407xpath_boolean(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003408{
3409 LY_ERR rc;
3410
3411 if (options & LYXP_SCNODE_ALL) {
3412 set_scnode_clear_ctx(set);
3413 return LY_SUCCESS;
3414 }
3415
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003416 rc = lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003417 LY_CHECK_RET(rc);
3418 set_fill_set(set, args[0]);
3419
3420 return LY_SUCCESS;
3421}
3422
3423/**
3424 * @brief Execute the XPath ceiling(number) function. Returns LYXP_SET_NUMBER
3425 * with the first argument rounded up to the nearest integer.
3426 *
3427 * @param[in] args Array of arguments.
3428 * @param[in] arg_count Count of elements in @p args.
3429 * @param[in,out] set Context and result set at the same time.
3430 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003431 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003432 */
3433static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003434xpath_ceiling(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003435{
3436 struct lysc_node_leaf *sleaf;
3437 LY_ERR rc = LY_SUCCESS;
3438
3439 if (options & LYXP_SCNODE_ALL) {
3440 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3441 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003442 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3443 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 +02003444 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
3445 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003446 }
3447 set_scnode_clear_ctx(set);
3448 return rc;
3449 }
3450
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003451 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003452 LY_CHECK_RET(rc);
3453 if ((long long)args[0]->val.num != args[0]->val.num) {
3454 set_fill_number(set, ((long long)args[0]->val.num) + 1);
3455 } else {
3456 set_fill_number(set, args[0]->val.num);
3457 }
3458
3459 return LY_SUCCESS;
3460}
3461
3462/**
3463 * @brief Execute the XPath concat(string, string, string*) function.
3464 * Returns LYXP_SET_STRING with the concatenation of all the arguments.
3465 *
3466 * @param[in] args Array of arguments.
3467 * @param[in] arg_count Count of elements in @p args.
3468 * @param[in,out] set Context and result set at the same time.
3469 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003470 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003471 */
3472static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003473xpath_concat(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003474{
3475 uint16_t i;
3476 char *str = NULL;
3477 size_t used = 1;
3478 LY_ERR rc = LY_SUCCESS;
3479 struct lysc_node_leaf *sleaf;
3480
3481 if (options & LYXP_SCNODE_ALL) {
3482 for (i = 0; i < arg_count; ++i) {
3483 if ((args[i]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[i]))) {
3484 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3485 LOGWRN(set->ctx, "Argument #%u of %s is a %s node \"%s\".",
3486 i + 1, __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003487 } else if (!warn_is_string_type(sleaf->type)) {
Radek Krejci70124c82020-08-14 22:17:03 +02003488 LOGWRN(set->ctx, "Argument #%u of %s is node \"%s\", not of string-type.", i + 1, __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003489 }
3490 }
3491 }
3492 set_scnode_clear_ctx(set);
3493 return rc;
3494 }
3495
3496 for (i = 0; i < arg_count; ++i) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003497 rc = lyxp_set_cast(args[i], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003498 if (rc != LY_SUCCESS) {
3499 free(str);
3500 return rc;
3501 }
3502
3503 str = ly_realloc(str, (used + strlen(args[i]->val.str)) * sizeof(char));
3504 LY_CHECK_ERR_RET(!str, LOGMEM(set->ctx), LY_EMEM);
3505 strcpy(str + used - 1, args[i]->val.str);
3506 used += strlen(args[i]->val.str);
3507 }
3508
3509 /* free, kind of */
Michal Vaskod3678892020-05-21 10:06:58 +02003510 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003511 set->type = LYXP_SET_STRING;
3512 set->val.str = str;
3513
3514 return LY_SUCCESS;
3515}
3516
3517/**
3518 * @brief Execute the XPath contains(string, string) function.
3519 * Returns LYXP_SET_BOOLEAN whether the second argument can
3520 * be found in the first or not.
3521 *
3522 * @param[in] args Array of arguments.
3523 * @param[in] arg_count Count of elements in @p args.
3524 * @param[in,out] set Context and result set at the same time.
3525 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003526 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003527 */
3528static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003529xpath_contains(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003530{
3531 struct lysc_node_leaf *sleaf;
3532 LY_ERR rc = LY_SUCCESS;
3533
3534 if (options & LYXP_SCNODE_ALL) {
3535 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3536 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3537 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 +02003538 } else if (!warn_is_string_type(sleaf->type)) {
3539 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003540 }
3541 }
3542
3543 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3544 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3545 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 +02003546 } else if (!warn_is_string_type(sleaf->type)) {
3547 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003548 }
3549 }
3550 set_scnode_clear_ctx(set);
3551 return rc;
3552 }
3553
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003554 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003555 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003556 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003557 LY_CHECK_RET(rc);
3558
3559 if (strstr(args[0]->val.str, args[1]->val.str)) {
3560 set_fill_boolean(set, 1);
3561 } else {
3562 set_fill_boolean(set, 0);
3563 }
3564
3565 return LY_SUCCESS;
3566}
3567
3568/**
3569 * @brief Execute the XPath count(node-set) function. Returns LYXP_SET_NUMBER
3570 * with the size of the node-set from the argument.
3571 *
3572 * @param[in] args Array of arguments.
3573 * @param[in] arg_count Count of elements in @p args.
3574 * @param[in,out] set Context and result set at the same time.
3575 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003576 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003577 */
3578static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003579xpath_count(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003580{
3581 struct lysc_node *scnode = NULL;
3582 LY_ERR rc = LY_SUCCESS;
3583
3584 if (options & LYXP_SCNODE_ALL) {
3585 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(scnode = warn_get_scnode_in_ctx(args[0]))) {
3586 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003587 }
3588 set_scnode_clear_ctx(set);
3589 return rc;
3590 }
3591
Michal Vasko03ff5a72019-09-11 13:49:33 +02003592 if (args[0]->type != LYXP_SET_NODE_SET) {
3593 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "count(node-set)");
3594 return LY_EVALID;
3595 }
3596
3597 set_fill_number(set, args[0]->used);
3598 return LY_SUCCESS;
3599}
3600
3601/**
3602 * @brief Execute the XPath current() function. Returns LYXP_SET_NODE_SET
3603 * with the context with the intial node.
3604 *
3605 * @param[in] args Array of arguments.
3606 * @param[in] arg_count Count of elements in @p args.
3607 * @param[in,out] set Context and result set at the same time.
3608 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003609 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003610 */
3611static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003612xpath_current(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003613{
3614 if (arg_count || args) {
3615 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGCOUNT, arg_count, "current()");
3616 return LY_EVALID;
3617 }
3618
3619 if (options & LYXP_SCNODE_ALL) {
3620 set_scnode_clear_ctx(set);
3621
Michal Vaskoecd62de2019-11-13 12:35:11 +01003622 lyxp_set_scnode_insert_node(set, set->ctx_scnode, LYXP_NODE_ELEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003623 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02003624 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003625
3626 /* position is filled later */
3627 set_insert_node(set, set->ctx_node, 0, LYXP_NODE_ELEM, 0);
3628 }
3629
3630 return LY_SUCCESS;
3631}
3632
3633/**
3634 * @brief Execute the YANG 1.1 deref(node-set) function. Returns LYXP_SET_NODE_SET with either
3635 * leafref or instance-identifier target node(s).
3636 *
3637 * @param[in] args Array of arguments.
3638 * @param[in] arg_count Count of elements in @p args.
3639 * @param[in,out] set Context and result set at the same time.
3640 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003641 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003642 */
3643static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003644xpath_deref(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003645{
3646 struct lyd_node_term *leaf;
Michal Vasko42e497c2020-01-06 08:38:25 +01003647 struct lysc_node_leaf *sleaf = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02003648 struct lysc_type_leafref *lref;
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003649 const struct lysc_node *target;
Michal Vasko004d3152020-06-11 19:59:22 +02003650 struct ly_path *p;
3651 struct lyd_node *node;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003652 char *errmsg = NULL;
Michal Vasko00cbf532020-06-15 13:58:47 +02003653 uint8_t oper;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003654 LY_ERR rc = LY_SUCCESS;
3655
3656 if (options & LYXP_SCNODE_ALL) {
3657 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3658 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003659 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3660 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 +02003661 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_LEAFREF) && !warn_is_specific_type(sleaf->type, LY_TYPE_INST)) {
3662 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"leafref\" nor \"instance-identifier\".",
3663 __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003664 }
3665 set_scnode_clear_ctx(set);
Michal Vasko42e497c2020-01-06 08:38:25 +01003666 if (sleaf && (sleaf->type->basetype == LY_TYPE_LEAFREF)) {
Michal Vasko004d3152020-06-11 19:59:22 +02003667 lref = (struct lysc_type_leafref *)sleaf->type;
Michal Vasko00cbf532020-06-15 13:58:47 +02003668 oper = lysc_is_output((struct lysc_node *)sleaf) ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT;
Michal Vasko004d3152020-06-11 19:59:22 +02003669
3670 /* it was already evaluated on schema, it must succeed */
Michal Vaskoc8a230d2020-08-14 12:17:10 +02003671 rc = ly_path_compile(set->ctx, sleaf->module, (struct lysc_node *)sleaf, lref->path, LY_PATH_LREF_TRUE,
3672 oper, LY_PATH_TARGET_MANY, set->format, lref->path_context, &p);
Michal Vasko004d3152020-06-11 19:59:22 +02003673 assert(!rc);
3674
3675 /* get the target node */
Michal Vaskofd69e1d2020-07-03 11:57:17 +02003676 target = p[LY_ARRAY_COUNT(p) - 1].node;
Michal Vasko004d3152020-06-11 19:59:22 +02003677 ly_path_free(set->ctx, p);
3678
3679 lyxp_set_scnode_insert_node(set, target, LYXP_NODE_ELEM);
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02003680 }
3681
Michal Vasko03ff5a72019-09-11 13:49:33 +02003682 return rc;
3683 }
3684
Michal Vaskod3678892020-05-21 10:06:58 +02003685 if (args[0]->type != LYXP_SET_NODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003686 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "deref(node-set)");
3687 return LY_EVALID;
3688 }
3689
Michal Vaskod3678892020-05-21 10:06:58 +02003690 lyxp_set_free_content(set);
3691 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003692 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
3693 sleaf = (struct lysc_node_leaf *)leaf->schema;
3694 if (sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
3695 if (sleaf->type->basetype == LY_TYPE_LEAFREF) {
3696 /* find leafref target */
Michal Vasko004d3152020-06-11 19:59:22 +02003697 if (ly_type_find_leafref((struct lysc_type_leafref *)sleaf->type, (struct lyd_node *)leaf,
3698 &leaf->value, set->tree, &node, &errmsg)) {
3699 LOGERR(set->ctx, LY_EVALID, errmsg);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003700 free(errmsg);
Michal Vasko004d3152020-06-11 19:59:22 +02003701 return LY_EVALID;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003702 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02003703 } else {
3704 assert(sleaf->type->basetype == LY_TYPE_INST);
Michal Vasko004d3152020-06-11 19:59:22 +02003705 if (ly_path_eval(leaf->value.target, set->tree, &node)) {
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003706 LOGERR(set->ctx, LY_EVALID, "Invalid instance-identifier \"%s\" value - required instance not found.",
Michal Vaskob7be7a82020-08-20 09:09:04 +02003707 LYD_CANON_VALUE(leaf));
Michal Vasko03ff5a72019-09-11 13:49:33 +02003708 return LY_EVALID;
3709 }
3710 }
Michal Vasko004d3152020-06-11 19:59:22 +02003711
3712 /* insert it */
3713 set_insert_node(set, node, 0, LYXP_NODE_ELEM, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003714 }
3715 }
3716
3717 return LY_SUCCESS;
3718}
3719
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003720static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003721xpath_derived_(struct lyxp_set **args, struct lyxp_set *set, uint32_t options, uint8_t self_match, const char *func)
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003722{
3723 uint16_t i;
3724 LY_ARRAY_COUNT_TYPE u;
3725 struct lyd_node_term *leaf;
3726 struct lysc_node_leaf *sleaf;
3727 struct lyd_meta *meta;
3728 struct lyd_value data = {0}, *val;
3729 struct ly_err_item *err = NULL;
3730 LY_ERR rc = LY_SUCCESS;
Radek Krejci1deb5be2020-08-26 16:43:36 +02003731 uint8_t found;
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003732
3733 if (options & LYXP_SCNODE_ALL) {
3734 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3735 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", func);
3736 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3737 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype),
3738 sleaf->name);
3739 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_IDENT)) {
3740 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"identityref\".", func, sleaf->name);
3741 }
3742
3743 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
3744 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3745 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", func, lys_nodetype2str(sleaf->nodetype),
3746 sleaf->name);
3747 } else if (!warn_is_string_type(sleaf->type)) {
3748 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", func, sleaf->name);
3749 }
3750 }
3751 set_scnode_clear_ctx(set);
3752 return rc;
3753 }
3754
3755 if (args[0]->type != LYXP_SET_NODE_SET) {
3756 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
3757 "derived-from(-or-self)(node-set, string)");
3758 return LY_EVALID;
3759 }
3760 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
3761 LY_CHECK_RET(rc);
3762
3763 set_fill_boolean(set, 0);
3764 found = 0;
3765 for (i = 0; i < args[0]->used; ++i) {
3766 if ((args[0]->val.nodes[i].type != LYXP_NODE_ELEM) && (args[0]->val.nodes[i].type != LYXP_NODE_META)) {
3767 continue;
3768 }
3769
3770 if (args[0]->val.nodes[i].type == LYXP_NODE_ELEM) {
3771 leaf = (struct lyd_node_term *)args[0]->val.nodes[i].node;
3772 sleaf = (struct lysc_node_leaf *)leaf->schema;
3773 val = &leaf->value;
3774 if (!(sleaf->nodetype & LYD_NODE_TERM) || (leaf->value.realtype->basetype != LY_TYPE_IDENT)) {
3775 /* uninteresting */
3776 continue;
3777 }
3778
3779 /* store args[1] as ident */
3780 rc = val->realtype->plugin->store(set->ctx, val->realtype, args[1]->val.str, strlen(args[1]->val.str),
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003781 0, set->format, (void *)set->local_mod,
Radek Krejci0f969882020-08-21 16:56:47 +02003782 (struct lyd_node *)leaf, set->tree, &data, &err);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003783 } else {
3784 meta = args[0]->val.meta[i].meta;
3785 val = &meta->value;
3786 if (val->realtype->basetype != LY_TYPE_IDENT) {
3787 /* uninteresting */
3788 continue;
3789 }
3790
3791 /* store args[1] as ident */
3792 rc = val->realtype->plugin->store(set->ctx, val->realtype, args[1]->val.str, strlen(args[1]->val.str),
Michal Vaskoba99a3e2020-08-18 15:50:05 +02003793 0, set->format, (void *)meta->annotation->module,
3794 meta->parent, set->tree, &data, &err);
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003795 }
3796
3797 if (err) {
3798 ly_err_print(err);
3799 ly_err_free(err);
3800 }
3801 LY_CHECK_RET(rc);
3802
3803 /* finally check the identity itself */
3804 if (self_match && (data.ident == val->ident)) {
3805 set_fill_boolean(set, 1);
3806 found = 1;
3807 }
3808 if (!found) {
3809 LY_ARRAY_FOR(data.ident->derived, u) {
3810 if (data.ident->derived[u] == val->ident) {
3811 set_fill_boolean(set, 1);
3812 found = 1;
3813 break;
3814 }
3815 }
3816 }
3817
3818 /* free temporary value */
3819 val->realtype->plugin->free(set->ctx, &data);
3820 if (found) {
3821 break;
3822 }
3823 }
3824
3825 return LY_SUCCESS;
3826}
3827
Michal Vasko03ff5a72019-09-11 13:49:33 +02003828/**
3829 * @brief Execute the YANG 1.1 derived-from(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
3830 * on whether the first argument nodes contain a node of an identity derived from the second
3831 * argument identity.
3832 *
3833 * @param[in] args Array of arguments.
3834 * @param[in] arg_count Count of elements in @p args.
3835 * @param[in,out] set Context and result set at the same time.
3836 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003837 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003838 */
3839static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003840xpath_derived_from(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003841{
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003842 return xpath_derived_(args, set, options, 0, __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003843}
3844
3845/**
3846 * @brief Execute the YANG 1.1 derived-from-or-self(node-set, string) function. Returns LYXP_SET_BOOLEAN depending
3847 * on whether the first argument nodes contain a node of an identity that either is or is derived from
3848 * the second argument identity.
3849 *
3850 * @param[in] args Array of arguments.
3851 * @param[in] arg_count Count of elements in @p args.
3852 * @param[in,out] set Context and result set at the same time.
3853 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003854 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003855 */
3856static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003857xpath_derived_from_or_self(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003858{
Michal Vaskoe0dd59d2020-07-17 16:10:23 +02003859 return xpath_derived_(args, set, options, 1, __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003860}
3861
3862/**
3863 * @brief Execute the YANG 1.1 enum-value(node-set) function. Returns LYXP_SET_NUMBER
3864 * with the integer value of the first node's enum value, otherwise NaN.
3865 *
3866 * @param[in] args Array of arguments.
3867 * @param[in] arg_count Count of elements in @p args.
3868 * @param[in,out] set Context and result set at the same time.
3869 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003870 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003871 */
3872static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003873xpath_enum_value(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003874{
3875 struct lyd_node_term *leaf;
3876 struct lysc_node_leaf *sleaf;
3877 LY_ERR rc = LY_SUCCESS;
3878
3879 if (options & LYXP_SCNODE_ALL) {
3880 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3881 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003882 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3883 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 +02003884 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_ENUM)) {
3885 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"enumeration\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003886 }
3887 set_scnode_clear_ctx(set);
3888 return rc;
3889 }
3890
Michal Vaskod3678892020-05-21 10:06:58 +02003891 if (args[0]->type != LYXP_SET_NODE_SET) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003892 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "enum-value(node-set)");
3893 return LY_EVALID;
3894 }
3895
3896 set_fill_number(set, NAN);
Michal Vaskod3678892020-05-21 10:06:58 +02003897 if (args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02003898 leaf = (struct lyd_node_term *)args[0]->val.nodes[0].node;
3899 sleaf = (struct lysc_node_leaf *)leaf->schema;
3900 if ((sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST)) && (sleaf->type->basetype == LY_TYPE_ENUM)) {
3901 set_fill_number(set, leaf->value.enum_item->value);
3902 }
3903 }
3904
3905 return LY_SUCCESS;
3906}
3907
3908/**
3909 * @brief Execute the XPath false() function. Returns LYXP_SET_BOOLEAN
3910 * with false value.
3911 *
3912 * @param[in] args Array of arguments.
3913 * @param[in] arg_count Count of elements in @p args.
3914 * @param[in,out] set Context and result set at the same time.
3915 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003916 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003917 */
3918static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003919xpath_false(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003920{
3921 if (options & LYXP_SCNODE_ALL) {
3922 set_scnode_clear_ctx(set);
3923 return LY_SUCCESS;
3924 }
3925
3926 set_fill_boolean(set, 0);
3927 return LY_SUCCESS;
3928}
3929
3930/**
3931 * @brief Execute the XPath floor(number) function. Returns LYXP_SET_NUMBER
3932 * with the first argument floored (truncated).
3933 *
3934 * @param[in] args Array of arguments.
3935 * @param[in] arg_count Count of elements in @p args.
3936 * @param[in,out] set Context and result set at the same time.
3937 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003938 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003939 */
3940static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003941xpath_floor(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t UNUSED(options))
Michal Vasko03ff5a72019-09-11 13:49:33 +02003942{
3943 LY_ERR rc;
3944
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003945 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003946 LY_CHECK_RET(rc);
3947 if (isfinite(args[0]->val.num)) {
3948 set_fill_number(set, (long long)args[0]->val.num);
3949 }
3950
3951 return LY_SUCCESS;
3952}
3953
3954/**
3955 * @brief Execute the XPath lang(string) function. Returns LYXP_SET_BOOLEAN
3956 * whether the language of the text matches the one from the argument.
3957 *
3958 * @param[in] args Array of arguments.
3959 * @param[in] arg_count Count of elements in @p args.
3960 * @param[in,out] set Context and result set at the same time.
3961 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01003962 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02003963 */
3964static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02003965xpath_lang(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02003966{
3967 const struct lyd_node *node;
3968 struct lysc_node_leaf *sleaf;
Michal Vasko9f96a052020-03-10 09:41:45 +01003969 struct lyd_meta *meta = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003970 const char *val;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003971 LY_ERR rc = LY_SUCCESS;
3972
3973 if (options & LYXP_SCNODE_ALL) {
3974 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
3975 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
3976 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 +02003977 } else if (!warn_is_string_type(sleaf->type)) {
3978 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003979 }
3980 }
3981 set_scnode_clear_ctx(set);
3982 return rc;
3983 }
3984
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01003985 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02003986 LY_CHECK_RET(rc);
3987
Michal Vasko03ff5a72019-09-11 13:49:33 +02003988 if (set->type != LYXP_SET_NODE_SET) {
3989 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "lang(string)");
3990 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02003991 } else if (!set->used) {
3992 set_fill_boolean(set, 0);
3993 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02003994 }
3995
3996 switch (set->val.nodes[0].type) {
3997 case LYXP_NODE_ELEM:
3998 case LYXP_NODE_TEXT:
3999 node = set->val.nodes[0].node;
4000 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004001 case LYXP_NODE_META:
4002 node = set->val.meta[0].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004003 break;
4004 default:
4005 /* nothing to do with roots */
4006 set_fill_boolean(set, 0);
4007 return LY_SUCCESS;
4008 }
4009
Michal Vasko9f96a052020-03-10 09:41:45 +01004010 /* find lang metadata */
Michal Vaskod989ba02020-08-24 10:59:24 +02004011 for ( ; node; node = (struct lyd_node *)node->parent) {
Michal Vasko9f96a052020-03-10 09:41:45 +01004012 for (meta = node->meta; meta; meta = meta->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004013 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004014 if (meta->name && !strcmp(meta->name, "lang") && !strcmp(meta->annotation->module->name, "xml")) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004015 break;
4016 }
4017 }
4018
Michal Vasko9f96a052020-03-10 09:41:45 +01004019 if (meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004020 break;
4021 }
4022 }
4023
4024 /* compare languages */
Michal Vasko9f96a052020-03-10 09:41:45 +01004025 if (!meta) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004026 set_fill_boolean(set, 0);
4027 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004028 uint64_t i;
4029
Michal Vaskoba99a3e2020-08-18 15:50:05 +02004030 val = meta->value.canonical;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004031 for (i = 0; args[0]->val.str[i]; ++i) {
4032 if (tolower(args[0]->val.str[i]) != tolower(val[i])) {
4033 set_fill_boolean(set, 0);
4034 break;
4035 }
4036 }
4037 if (!args[0]->val.str[i]) {
4038 if (!val[i] || (val[i] == '-')) {
4039 set_fill_boolean(set, 1);
4040 } else {
4041 set_fill_boolean(set, 0);
4042 }
4043 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004044 }
4045
4046 return LY_SUCCESS;
4047}
4048
4049/**
4050 * @brief Execute the XPath last() function. Returns LYXP_SET_NUMBER
4051 * with the context size.
4052 *
4053 * @param[in] args Array of arguments.
4054 * @param[in] arg_count Count of elements in @p args.
4055 * @param[in,out] set Context and result set at the same time.
4056 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004057 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004058 */
4059static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004060xpath_last(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004061{
4062 if (options & LYXP_SCNODE_ALL) {
4063 set_scnode_clear_ctx(set);
4064 return LY_SUCCESS;
4065 }
4066
Michal Vasko03ff5a72019-09-11 13:49:33 +02004067 if (set->type != LYXP_SET_NODE_SET) {
4068 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "last()");
4069 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004070 } else if (!set->used) {
4071 set_fill_number(set, 0);
4072 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004073 }
4074
4075 set_fill_number(set, set->ctx_size);
4076 return LY_SUCCESS;
4077}
4078
4079/**
4080 * @brief Execute the XPath local-name(node-set?) function. Returns LYXP_SET_STRING
4081 * with the node name without namespace from the argument or the context.
4082 *
4083 * @param[in] args Array of arguments.
4084 * @param[in] arg_count Count of elements in @p args.
4085 * @param[in,out] set Context and result set at the same time.
4086 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004087 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004088 */
4089static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004090xpath_local_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004091{
4092 struct lyxp_set_node *item;
4093 /* suppress unused variable warning */
4094 (void)options;
4095
4096 if (options & LYXP_SCNODE_ALL) {
4097 set_scnode_clear_ctx(set);
4098 return LY_SUCCESS;
4099 }
4100
4101 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004102 if (args[0]->type != LYXP_SET_NODE_SET) {
4103 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "local-name(node-set?)");
4104 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004105 } else if (!args[0]->used) {
4106 set_fill_string(set, "", 0);
4107 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004108 }
4109
4110 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004111 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004112
4113 item = &args[0]->val.nodes[0];
4114 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004115 if (set->type != LYXP_SET_NODE_SET) {
4116 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "local-name(node-set?)");
4117 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004118 } else if (!set->used) {
4119 set_fill_string(set, "", 0);
4120 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004121 }
4122
4123 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004124 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004125
4126 item = &set->val.nodes[0];
4127 }
4128
4129 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004130 case LYXP_NODE_NONE:
4131 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004132 case LYXP_NODE_ROOT:
4133 case LYXP_NODE_ROOT_CONFIG:
4134 case LYXP_NODE_TEXT:
4135 set_fill_string(set, "", 0);
4136 break;
4137 case LYXP_NODE_ELEM:
4138 set_fill_string(set, item->node->schema->name, strlen(item->node->schema->name));
4139 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004140 case LYXP_NODE_META:
4141 set_fill_string(set, ((struct lyd_meta *)item->node)->name, strlen(((struct lyd_meta *)item->node)->name));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004142 break;
4143 }
4144
4145 return LY_SUCCESS;
4146}
4147
4148/**
4149 * @brief Execute the XPath name(node-set?) function. Returns LYXP_SET_STRING
4150 * with the node name fully qualified (with namespace) from the argument or the context.
4151 *
4152 * @param[in] args Array of arguments.
4153 * @param[in] arg_count Count of elements in @p args.
4154 * @param[in,out] set Context and result set at the same time.
4155 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004156 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004157 */
4158static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004159xpath_name(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004160{
4161 struct lyxp_set_node *item;
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004162 struct lys_module *mod = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004163 char *str;
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004164 const char *name = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004165
4166 if (options & LYXP_SCNODE_ALL) {
4167 set_scnode_clear_ctx(set);
4168 return LY_SUCCESS;
4169 }
4170
4171 if (arg_count) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004172 if (args[0]->type != LYXP_SET_NODE_SET) {
4173 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "name(node-set?)");
4174 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004175 } else if (!args[0]->used) {
4176 set_fill_string(set, "", 0);
4177 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004178 }
4179
4180 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004181 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004182
4183 item = &args[0]->val.nodes[0];
4184 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004185 if (set->type != LYXP_SET_NODE_SET) {
4186 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "name(node-set?)");
4187 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004188 } else if (!set->used) {
4189 set_fill_string(set, "", 0);
4190 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004191 }
4192
4193 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004194 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004195
4196 item = &set->val.nodes[0];
4197 }
4198
4199 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004200 case LYXP_NODE_NONE:
4201 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004202 case LYXP_NODE_ROOT:
4203 case LYXP_NODE_ROOT_CONFIG:
4204 case LYXP_NODE_TEXT:
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02004205 /* keep NULL */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004206 break;
4207 case LYXP_NODE_ELEM:
4208 mod = item->node->schema->module;
4209 name = item->node->schema->name;
4210 break;
Michal Vasko9f96a052020-03-10 09:41:45 +01004211 case LYXP_NODE_META:
4212 mod = ((struct lyd_meta *)item->node)->annotation->module;
4213 name = ((struct lyd_meta *)item->node)->name;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004214 break;
4215 }
4216
4217 if (mod && name) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004218 int rc = -1;
4219
Michal Vasko03ff5a72019-09-11 13:49:33 +02004220 switch (set->format) {
Michal Vaskoc8a230d2020-08-14 12:17:10 +02004221 case LY_PREF_SCHEMA:
Michal Vasko03ff5a72019-09-11 13:49:33 +02004222 rc = asprintf(&str, "%s:%s", lys_prefix_find_module(set->local_mod, mod), name);
4223 break;
Michal Vaskoc8a230d2020-08-14 12:17:10 +02004224 case LY_PREF_JSON:
Michal Vasko03ff5a72019-09-11 13:49:33 +02004225 rc = asprintf(&str, "%s:%s", mod->name, name);
4226 break;
Michal Vaskoc8a230d2020-08-14 12:17:10 +02004227 case LY_PREF_XML:
Michal Vasko9409ef62019-09-12 11:47:17 +02004228 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004229 }
4230 LY_CHECK_ERR_RET(rc == -1, LOGMEM(set->ctx), LY_EMEM);
4231 set_fill_string(set, str, strlen(str));
4232 free(str);
4233 } else {
4234 set_fill_string(set, "", 0);
4235 }
4236
4237 return LY_SUCCESS;
4238}
4239
4240/**
4241 * @brief Execute the XPath namespace-uri(node-set?) function. Returns LYXP_SET_STRING
4242 * with the namespace of the node from the argument or the context.
4243 *
4244 * @param[in] args Array of arguments.
4245 * @param[in] arg_count Count of elements in @p args.
4246 * @param[in,out] set Context and result set at the same time.
4247 * @param[in] options XPath options.
4248 * @return LY_ERR (LY_EINVAL for wrong arguments on schema)
4249 */
4250static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004251xpath_namespace_uri(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004252{
4253 struct lyxp_set_node *item;
4254 struct lys_module *mod;
4255 /* suppress unused variable warning */
4256 (void)options;
4257
4258 if (options & LYXP_SCNODE_ALL) {
4259 set_scnode_clear_ctx(set);
4260 return LY_SUCCESS;
4261 }
4262
4263 if (arg_count) {
Michal Vaskod3678892020-05-21 10:06:58 +02004264 if (args[0]->type != LYXP_SET_NODE_SET) {
4265 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]),
4266 "namespace-uri(node-set?)");
4267 return LY_EVALID;
4268 } else if (!args[0]->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004269 set_fill_string(set, "", 0);
4270 return LY_SUCCESS;
4271 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02004272
4273 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004274 assert(!set_sort(args[0]));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004275
4276 item = &args[0]->val.nodes[0];
4277 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004278 if (set->type != LYXP_SET_NODE_SET) {
4279 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "namespace-uri(node-set?)");
4280 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004281 } else if (!set->used) {
4282 set_fill_string(set, "", 0);
4283 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004284 }
4285
4286 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004287 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02004288
4289 item = &set->val.nodes[0];
4290 }
4291
4292 switch (item->type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01004293 case LYXP_NODE_NONE:
4294 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004295 case LYXP_NODE_ROOT:
4296 case LYXP_NODE_ROOT_CONFIG:
4297 case LYXP_NODE_TEXT:
4298 set_fill_string(set, "", 0);
4299 break;
4300 case LYXP_NODE_ELEM:
Michal Vasko9f96a052020-03-10 09:41:45 +01004301 case LYXP_NODE_META:
Michal Vasko03ff5a72019-09-11 13:49:33 +02004302 if (item->type == LYXP_NODE_ELEM) {
4303 mod = item->node->schema->module;
Michal Vasko9f96a052020-03-10 09:41:45 +01004304 } else { /* LYXP_NODE_META */
Michal Vasko03ff5a72019-09-11 13:49:33 +02004305 /* annotations */
Michal Vasko9f96a052020-03-10 09:41:45 +01004306 mod = ((struct lyd_meta *)item->node)->annotation->module;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004307 }
4308
4309 set_fill_string(set, mod->ns, strlen(mod->ns));
4310 break;
4311 }
4312
4313 return LY_SUCCESS;
4314}
4315
4316/**
4317 * @brief Execute the XPath node() function (node type). Returns LYXP_SET_NODE_SET
4318 * with only nodes from the context. In practice it either leaves the context
4319 * as it is or returns an empty node set.
4320 *
4321 * @param[in] args Array of arguments.
4322 * @param[in] arg_count Count of elements in @p args.
4323 * @param[in,out] set Context and result set at the same time.
4324 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004325 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004326 */
4327static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004328xpath_node(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004329{
4330 if (options & LYXP_SCNODE_ALL) {
4331 set_scnode_clear_ctx(set);
4332 return LY_SUCCESS;
4333 }
4334
4335 if (set->type != LYXP_SET_NODE_SET) {
Michal Vaskod3678892020-05-21 10:06:58 +02004336 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004337 }
4338 return LY_SUCCESS;
4339}
4340
4341/**
4342 * @brief Execute the XPath normalize-space(string?) function. Returns LYXP_SET_STRING
4343 * with normalized value (no leading, trailing, double white spaces) of the node
4344 * from the argument or the context.
4345 *
4346 * @param[in] args Array of arguments.
4347 * @param[in] arg_count Count of elements in @p args.
4348 * @param[in,out] set Context and result set at the same time.
4349 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004350 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004351 */
4352static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004353xpath_normalize_space(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004354{
4355 uint16_t i, new_used;
4356 char *new;
Radek Krejci1deb5be2020-08-26 16:43:36 +02004357 uint8_t have_spaces = 0, space_before = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004358 struct lysc_node_leaf *sleaf;
4359 LY_ERR rc = LY_SUCCESS;
4360
4361 if (options & LYXP_SCNODE_ALL) {
4362 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4363 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4364 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004365 } else if (!warn_is_string_type(sleaf->type)) {
4366 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004367 }
4368 }
4369 set_scnode_clear_ctx(set);
4370 return rc;
4371 }
4372
4373 if (arg_count) {
4374 set_fill_set(set, args[0]);
4375 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004376 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004377 LY_CHECK_RET(rc);
4378
4379 /* is there any normalization necessary? */
4380 for (i = 0; set->val.str[i]; ++i) {
4381 if (is_xmlws(set->val.str[i])) {
4382 if ((i == 0) || space_before || (!set->val.str[i + 1])) {
4383 have_spaces = 1;
4384 break;
4385 }
4386 space_before = 1;
4387 } else {
4388 space_before = 0;
4389 }
4390 }
4391
4392 /* yep, there is */
4393 if (have_spaces) {
4394 /* it's enough, at least one character will go, makes space for ending '\0' */
4395 new = malloc(strlen(set->val.str) * sizeof(char));
4396 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4397 new_used = 0;
4398
4399 space_before = 0;
4400 for (i = 0; set->val.str[i]; ++i) {
4401 if (is_xmlws(set->val.str[i])) {
4402 if ((i == 0) || space_before) {
4403 space_before = 1;
4404 continue;
4405 } else {
4406 space_before = 1;
4407 }
4408 } else {
4409 space_before = 0;
4410 }
4411
4412 new[new_used] = (space_before ? ' ' : set->val.str[i]);
4413 ++new_used;
4414 }
4415
4416 /* at worst there is one trailing space now */
4417 if (new_used && is_xmlws(new[new_used - 1])) {
4418 --new_used;
4419 }
4420
4421 new = ly_realloc(new, (new_used + 1) * sizeof(char));
4422 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
4423 new[new_used] = '\0';
4424
4425 free(set->val.str);
4426 set->val.str = new;
4427 }
4428
4429 return LY_SUCCESS;
4430}
4431
4432/**
4433 * @brief Execute the XPath not(boolean) function. Returns LYXP_SET_BOOLEAN
4434 * with the argument converted to boolean and logically inverted.
4435 *
4436 * @param[in] args Array of arguments.
4437 * @param[in] arg_count Count of elements in @p args.
4438 * @param[in,out] set Context and result set at the same time.
4439 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004440 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004441 */
4442static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004443xpath_not(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004444{
4445 if (options & LYXP_SCNODE_ALL) {
4446 set_scnode_clear_ctx(set);
4447 return LY_SUCCESS;
4448 }
4449
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004450 lyxp_set_cast(args[0], LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02004451 if (args[0]->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004452 set_fill_boolean(set, 0);
4453 } else {
4454 set_fill_boolean(set, 1);
4455 }
4456
4457 return LY_SUCCESS;
4458}
4459
4460/**
4461 * @brief Execute the XPath number(object?) function. Returns LYXP_SET_NUMBER
4462 * with the number representation of either the argument or the context.
4463 *
4464 * @param[in] args Array of arguments.
4465 * @param[in] arg_count Count of elements in @p args.
4466 * @param[in,out] set Context and result set at the same time.
4467 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004468 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004469 */
4470static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004471xpath_number(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004472{
4473 LY_ERR rc;
4474
4475 if (options & LYXP_SCNODE_ALL) {
4476 set_scnode_clear_ctx(set);
4477 return LY_SUCCESS;
4478 }
4479
4480 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004481 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004482 LY_CHECK_RET(rc);
4483 set_fill_set(set, args[0]);
4484 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004485 rc = lyxp_set_cast(set, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004486 LY_CHECK_RET(rc);
4487 }
4488
4489 return LY_SUCCESS;
4490}
4491
4492/**
4493 * @brief Execute the XPath position() function. Returns LYXP_SET_NUMBER
4494 * with the context position.
4495 *
4496 * @param[in] args Array of arguments.
4497 * @param[in] arg_count Count of elements in @p args.
4498 * @param[in,out] set Context and result set at the same time.
4499 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004500 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004501 */
4502static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004503xpath_position(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004504{
4505 if (options & LYXP_SCNODE_ALL) {
4506 set_scnode_clear_ctx(set);
4507 return LY_SUCCESS;
4508 }
4509
Michal Vasko03ff5a72019-09-11 13:49:33 +02004510 if (set->type != LYXP_SET_NODE_SET) {
4511 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "position()");
4512 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02004513 } else if (!set->used) {
4514 set_fill_number(set, 0);
4515 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004516 }
4517
4518 set_fill_number(set, set->ctx_pos);
4519
4520 /* UNUSED in 'Release' build type */
4521 (void)options;
4522 return LY_SUCCESS;
4523}
4524
4525/**
4526 * @brief Execute the YANG 1.1 re-match(string, string) function. Returns LYXP_SET_BOOLEAN
4527 * depending on whether the second argument regex matches the first argument string. For details refer to
4528 * YANG 1.1 RFC section 10.2.1.
4529 *
4530 * @param[in] args Array of arguments.
4531 * @param[in] arg_count Count of elements in @p args.
4532 * @param[in,out] set Context and result set at the same time.
4533 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004534 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004535 */
4536static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004537xpath_re_match(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004538{
4539 struct lysc_pattern **patterns = NULL, **pattern;
4540 struct lysc_node_leaf *sleaf;
4541 char *path;
4542 LY_ERR rc = LY_SUCCESS;
4543 struct ly_err_item *err;
4544
4545 if (options & LYXP_SCNODE_ALL) {
4546 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4547 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4548 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004549 } else if (!warn_is_string_type(sleaf->type)) {
4550 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004551 }
4552 }
4553
4554 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4555 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4556 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004557 } else if (!warn_is_string_type(sleaf->type)) {
4558 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004559 }
4560 }
4561 set_scnode_clear_ctx(set);
4562 return rc;
4563 }
4564
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004565 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004566 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004567 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004568 LY_CHECK_RET(rc);
4569
4570 LY_ARRAY_NEW_RET(set->ctx, patterns, pattern, LY_EMEM);
4571 *pattern = malloc(sizeof **pattern);
4572 path = lyd_path(set->ctx_node, LYD_PATH_LOG, NULL, 0);
4573 rc = lys_compile_type_pattern_check(set->ctx, path, args[1]->val.str, &(*pattern)->code);
4574 free(path);
4575 if (rc != LY_SUCCESS) {
4576 LY_ARRAY_FREE(patterns);
4577 return rc;
4578 }
4579
4580 rc = ly_type_validate_patterns(patterns, args[0]->val.str, strlen(args[0]->val.str), &err);
4581 pcre2_code_free((*pattern)->code);
4582 free(*pattern);
4583 LY_ARRAY_FREE(patterns);
4584 if (rc && (rc != LY_EVALID)) {
4585 ly_err_print(err);
4586 ly_err_free(err);
4587 return rc;
4588 }
4589
4590 if (rc == LY_EVALID) {
4591 ly_err_free(err);
4592 set_fill_boolean(set, 0);
4593 } else {
4594 set_fill_boolean(set, 1);
4595 }
4596
4597 return LY_SUCCESS;
4598}
4599
4600/**
4601 * @brief Execute the XPath round(number) function. Returns LYXP_SET_NUMBER
4602 * with the rounded first argument. For details refer to
4603 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-round.
4604 *
4605 * @param[in] args Array of arguments.
4606 * @param[in] arg_count Count of elements in @p args.
4607 * @param[in,out] set Context and result set at the same time.
4608 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004609 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004610 */
4611static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004612xpath_round(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004613{
4614 struct lysc_node_leaf *sleaf;
4615 LY_ERR rc = LY_SUCCESS;
4616
4617 if (options & LYXP_SCNODE_ALL) {
4618 if ((args[0]->type != LYXP_SET_SCNODE_SET) || !(sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4619 LOGWRN(set->ctx, "Argument #1 of %s not a node-set as expected.", __func__);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004620 } else if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4621 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004622 } else if (!warn_is_specific_type(sleaf->type, LY_TYPE_DEC64)) {
4623 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of type \"decimal64\".", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004624 }
4625 set_scnode_clear_ctx(set);
4626 return rc;
4627 }
4628
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004629 rc = lyxp_set_cast(args[0], LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004630 LY_CHECK_RET(rc);
4631
4632 /* cover only the cases where floor can't be used */
4633 if ((args[0]->val.num == -0.0f) || ((args[0]->val.num < 0) && (args[0]->val.num >= -0.5))) {
4634 set_fill_number(set, -0.0f);
4635 } else {
4636 args[0]->val.num += 0.5;
4637 rc = xpath_floor(args, 1, args[0], options);
4638 LY_CHECK_RET(rc);
4639 set_fill_number(set, args[0]->val.num);
4640 }
4641
4642 return LY_SUCCESS;
4643}
4644
4645/**
4646 * @brief Execute the XPath starts-with(string, string) function.
4647 * Returns LYXP_SET_BOOLEAN whether the second argument is
4648 * the prefix of the first or not.
4649 *
4650 * @param[in] args Array of arguments.
4651 * @param[in] arg_count Count of elements in @p args.
4652 * @param[in,out] set Context and result set at the same time.
4653 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004654 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004655 */
4656static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004657xpath_starts_with(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004658{
4659 struct lysc_node_leaf *sleaf;
4660 LY_ERR rc = LY_SUCCESS;
4661
4662 if (options & LYXP_SCNODE_ALL) {
4663 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4664 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4665 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004666 } else if (!warn_is_string_type(sleaf->type)) {
4667 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004668 }
4669 }
4670
4671 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4672 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4673 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004674 } else if (!warn_is_string_type(sleaf->type)) {
4675 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004676 }
4677 }
4678 set_scnode_clear_ctx(set);
4679 return rc;
4680 }
4681
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004682 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004683 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004684 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004685 LY_CHECK_RET(rc);
4686
4687 if (strncmp(args[0]->val.str, args[1]->val.str, strlen(args[1]->val.str))) {
4688 set_fill_boolean(set, 0);
4689 } else {
4690 set_fill_boolean(set, 1);
4691 }
4692
4693 return LY_SUCCESS;
4694}
4695
4696/**
4697 * @brief Execute the XPath string(object?) function. Returns LYXP_SET_STRING
4698 * with the string representation of either the argument or the context.
4699 *
4700 * @param[in] args Array of arguments.
4701 * @param[in] arg_count Count of elements in @p args.
4702 * @param[in,out] set Context and result set at the same time.
4703 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004704 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004705 */
4706static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004707xpath_string(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004708{
4709 LY_ERR rc;
4710
4711 if (options & LYXP_SCNODE_ALL) {
4712 set_scnode_clear_ctx(set);
4713 return LY_SUCCESS;
4714 }
4715
4716 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004717 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004718 LY_CHECK_RET(rc);
4719 set_fill_set(set, args[0]);
4720 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004721 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004722 LY_CHECK_RET(rc);
4723 }
4724
4725 return LY_SUCCESS;
4726}
4727
4728/**
4729 * @brief Execute the XPath string-length(string?) function. Returns LYXP_SET_NUMBER
4730 * with the length of the string in either the argument or the context.
4731 *
4732 * @param[in] args Array of arguments.
4733 * @param[in] arg_count Count of elements in @p args.
4734 * @param[in,out] set Context and result set at the same time.
4735 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004736 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004737 */
4738static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004739xpath_string_length(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004740{
4741 struct lysc_node_leaf *sleaf;
4742 LY_ERR rc = LY_SUCCESS;
4743
4744 if (options & LYXP_SCNODE_ALL) {
4745 if (arg_count && (args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4746 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4747 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004748 } else if (!warn_is_string_type(sleaf->type)) {
4749 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004750 }
4751 }
4752 if (!arg_count && (set->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(set))) {
4753 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4754 LOGWRN(set->ctx, "Argument #0 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004755 } else if (!warn_is_string_type(sleaf->type)) {
4756 LOGWRN(set->ctx, "Argument #0 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004757 }
4758 }
4759 set_scnode_clear_ctx(set);
4760 return rc;
4761 }
4762
4763 if (arg_count) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004764 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004765 LY_CHECK_RET(rc);
4766 set_fill_number(set, strlen(args[0]->val.str));
4767 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004768 rc = lyxp_set_cast(set, LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004769 LY_CHECK_RET(rc);
4770 set_fill_number(set, strlen(set->val.str));
4771 }
4772
4773 return LY_SUCCESS;
4774}
4775
4776/**
4777 * @brief Execute the XPath substring(string, number, number?) function.
4778 * Returns LYXP_SET_STRING substring of the first argument starting
4779 * on the second argument index ending on the third argument index,
4780 * indexed from 1. For exact definition refer to
4781 * http://www.w3.org/TR/1999/REC-xpath-19991116/#function-substring.
4782 *
4783 * @param[in] args Array of arguments.
4784 * @param[in] arg_count Count of elements in @p args.
4785 * @param[in,out] set Context and result set at the same time.
4786 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004787 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004788 */
4789static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004790xpath_substring(struct lyxp_set **args, uint16_t arg_count, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004791{
Radek Krejci1deb5be2020-08-26 16:43:36 +02004792 int32_t start, len;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004793 uint16_t str_start, str_len, pos;
4794 struct lysc_node_leaf *sleaf;
4795 LY_ERR rc = LY_SUCCESS;
4796
4797 if (options & LYXP_SCNODE_ALL) {
4798 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4799 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4800 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004801 } else if (!warn_is_string_type(sleaf->type)) {
4802 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004803 }
4804 }
4805
4806 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4807 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4808 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004809 } else if (!warn_is_numeric_type(sleaf->type)) {
4810 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004811 }
4812 }
4813
4814 if ((arg_count == 3) && (args[2]->type == LYXP_SET_SCNODE_SET)
Radek Krejci0f969882020-08-21 16:56:47 +02004815 && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004816 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4817 LOGWRN(set->ctx, "Argument #3 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004818 } else if (!warn_is_numeric_type(sleaf->type)) {
4819 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004820 }
4821 }
4822 set_scnode_clear_ctx(set);
4823 return rc;
4824 }
4825
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004826 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004827 LY_CHECK_RET(rc);
4828
4829 /* start */
4830 if (xpath_round(&args[1], 1, args[1], options)) {
4831 return -1;
4832 }
4833 if (isfinite(args[1]->val.num)) {
4834 start = args[1]->val.num - 1;
4835 } else if (isinf(args[1]->val.num) && signbit(args[1]->val.num)) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004836 start = INT32_MIN;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004837 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004838 start = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004839 }
4840
4841 /* len */
4842 if (arg_count == 3) {
4843 rc = xpath_round(&args[2], 1, args[2], options);
4844 LY_CHECK_RET(rc);
Radek Krejci1deb5be2020-08-26 16:43:36 +02004845 if (isnan(args[2]->val.num) || signbit(args[2]->val.num)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02004846 len = 0;
Radek Krejci1deb5be2020-08-26 16:43:36 +02004847 } else if (isfinite(args[2]->val.num)) {
4848 len = args[2]->val.num;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004849 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004850 len = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004851 }
4852 } else {
Radek Krejci1deb5be2020-08-26 16:43:36 +02004853 len = INT32_MAX;
Michal Vasko03ff5a72019-09-11 13:49:33 +02004854 }
4855
4856 /* find matching character positions */
4857 str_start = 0;
4858 str_len = 0;
4859 for (pos = 0; args[0]->val.str[pos]; ++pos) {
4860 if (pos < start) {
4861 ++str_start;
4862 } else if (pos < start + len) {
4863 ++str_len;
4864 } else {
4865 break;
4866 }
4867 }
4868
4869 set_fill_string(set, args[0]->val.str + str_start, str_len);
4870 return LY_SUCCESS;
4871}
4872
4873/**
4874 * @brief Execute the XPath substring-after(string, string) function.
4875 * Returns LYXP_SET_STRING with the string succeeding the occurance
4876 * of the second argument in the first or an empty string.
4877 *
4878 * @param[in] args Array of arguments.
4879 * @param[in] arg_count Count of elements in @p args.
4880 * @param[in,out] set Context and result set at the same time.
4881 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004882 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004883 */
4884static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004885xpath_substring_after(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004886{
4887 char *ptr;
4888 struct lysc_node_leaf *sleaf;
4889 LY_ERR rc = LY_SUCCESS;
4890
4891 if (options & LYXP_SCNODE_ALL) {
4892 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4893 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4894 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004895 } else if (!warn_is_string_type(sleaf->type)) {
4896 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004897 }
4898 }
4899
4900 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4901 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4902 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004903 } else if (!warn_is_string_type(sleaf->type)) {
4904 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004905 }
4906 }
4907 set_scnode_clear_ctx(set);
4908 return rc;
4909 }
4910
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004911 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004912 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004913 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004914 LY_CHECK_RET(rc);
4915
4916 ptr = strstr(args[0]->val.str, args[1]->val.str);
4917 if (ptr) {
4918 set_fill_string(set, ptr + strlen(args[1]->val.str), strlen(ptr + strlen(args[1]->val.str)));
4919 } else {
4920 set_fill_string(set, "", 0);
4921 }
4922
4923 return LY_SUCCESS;
4924}
4925
4926/**
4927 * @brief Execute the XPath substring-before(string, string) function.
4928 * Returns LYXP_SET_STRING with the string preceding the occurance
4929 * of the second argument in the first or an empty string.
4930 *
4931 * @param[in] args Array of arguments.
4932 * @param[in] arg_count Count of elements in @p args.
4933 * @param[in,out] set Context and result set at the same time.
4934 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004935 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004936 */
4937static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004938xpath_substring_before(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004939{
4940 char *ptr;
4941 struct lysc_node_leaf *sleaf;
4942 LY_ERR rc = LY_SUCCESS;
4943
4944 if (options & LYXP_SCNODE_ALL) {
4945 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
4946 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4947 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004948 } else if (!warn_is_string_type(sleaf->type)) {
4949 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004950 }
4951 }
4952
4953 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
4954 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
4955 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004956 } else if (!warn_is_string_type(sleaf->type)) {
4957 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004958 }
4959 }
4960 set_scnode_clear_ctx(set);
4961 return rc;
4962 }
4963
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004964 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004965 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01004966 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02004967 LY_CHECK_RET(rc);
4968
4969 ptr = strstr(args[0]->val.str, args[1]->val.str);
4970 if (ptr) {
4971 set_fill_string(set, args[0]->val.str, ptr - args[0]->val.str);
4972 } else {
4973 set_fill_string(set, "", 0);
4974 }
4975
4976 return LY_SUCCESS;
4977}
4978
4979/**
4980 * @brief Execute the XPath sum(node-set) function. Returns LYXP_SET_NUMBER
4981 * with the sum of all the nodes in the context.
4982 *
4983 * @param[in] args Array of arguments.
4984 * @param[in] arg_count Count of elements in @p args.
4985 * @param[in,out] set Context and result set at the same time.
4986 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01004987 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02004988 */
4989static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02004990xpath_sum(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02004991{
4992 long double num;
4993 char *str;
4994 uint16_t i;
4995 struct lyxp_set set_item;
4996 struct lysc_node_leaf *sleaf;
4997 LY_ERR rc = LY_SUCCESS;
4998
4999 if (options & LYXP_SCNODE_ALL) {
5000 if (args[0]->type == LYXP_SET_SCNODE_SET) {
5001 for (i = 0; i < args[0]->used; ++i) {
5002 if (args[0]->val.scnodes[i].in_ctx == 1) {
5003 sleaf = (struct lysc_node_leaf *)args[0]->val.scnodes[i].scnode;
5004 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5005 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__,
5006 lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005007 } else if (!warn_is_numeric_type(sleaf->type)) {
5008 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of numeric type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005009 }
5010 }
5011 }
5012 }
5013 set_scnode_clear_ctx(set);
5014 return rc;
5015 }
5016
5017 set_fill_number(set, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005018
5019 if (args[0]->type != LYXP_SET_NODE_SET) {
5020 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INARGTYPE, 1, print_set_type(args[0]), "sum(node-set)");
5021 return LY_EVALID;
Michal Vaskod3678892020-05-21 10:06:58 +02005022 } else if (!args[0]->used) {
5023 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005024 }
5025
Michal Vasko5c4e5892019-11-14 12:31:38 +01005026 set_init(&set_item, set);
5027
Michal Vasko03ff5a72019-09-11 13:49:33 +02005028 set_item.type = LYXP_SET_NODE_SET;
5029 set_item.val.nodes = malloc(sizeof *set_item.val.nodes);
5030 LY_CHECK_ERR_RET(!set_item.val.nodes, LOGMEM(set->ctx), LY_EMEM);
5031
5032 set_item.used = 1;
5033 set_item.size = 1;
5034
5035 for (i = 0; i < args[0]->used; ++i) {
5036 set_item.val.nodes[0] = args[0]->val.nodes[i];
5037
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005038 rc = cast_node_set_to_string(&set_item, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005039 LY_CHECK_RET(rc);
5040 num = cast_string_to_number(str);
5041 free(str);
5042 set->val.num += num;
5043 }
5044
5045 free(set_item.val.nodes);
5046
5047 return LY_SUCCESS;
5048}
5049
5050/**
5051 * @brief Execute the XPath text() function (node type). Returns LYXP_SET_NODE_SET
5052 * with the text content of the nodes in the context.
5053 *
5054 * @param[in] args Array of arguments.
5055 * @param[in] arg_count Count of elements in @p args.
5056 * @param[in,out] set Context and result set at the same time.
5057 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005058 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005059 */
5060static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005061xpath_text(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005062{
5063 uint32_t i;
5064
5065 if (options & LYXP_SCNODE_ALL) {
5066 set_scnode_clear_ctx(set);
5067 return LY_SUCCESS;
5068 }
5069
Michal Vasko03ff5a72019-09-11 13:49:33 +02005070 if (set->type != LYXP_SET_NODE_SET) {
5071 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INCTX, print_set_type(set), "text()");
5072 return LY_EVALID;
5073 }
5074
Michal Vaskod989ba02020-08-24 10:59:24 +02005075 for (i = 0; i < set->used; ) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005076 switch (set->val.nodes[i].type) {
Michal Vasko2caefc12019-11-14 16:07:56 +01005077 case LYXP_NODE_NONE:
5078 LOGINT_RET(set->ctx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005079 case LYXP_NODE_ELEM:
Michal Vasko03ff5a72019-09-11 13:49:33 +02005080 if (set->val.nodes[i].node->schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
5081 set->val.nodes[i].type = LYXP_NODE_TEXT;
5082 ++i;
5083 break;
5084 }
Radek Krejci0f969882020-08-21 16:56:47 +02005085 /* fall through */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005086 case LYXP_NODE_ROOT:
5087 case LYXP_NODE_ROOT_CONFIG:
5088 case LYXP_NODE_TEXT:
Michal Vasko9f96a052020-03-10 09:41:45 +01005089 case LYXP_NODE_META:
Michal Vasko03ff5a72019-09-11 13:49:33 +02005090 set_remove_node(set, i);
5091 break;
5092 }
5093 }
5094
5095 return LY_SUCCESS;
5096}
5097
5098/**
5099 * @brief Execute the XPath translate(string, string, string) function.
5100 * Returns LYXP_SET_STRING with the first argument with the characters
5101 * from the second argument replaced by those on the corresponding
5102 * positions in the third argument.
5103 *
5104 * @param[in] args Array of arguments.
5105 * @param[in] arg_count Count of elements in @p args.
5106 * @param[in,out] set Context and result set at the same time.
5107 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005108 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005109 */
5110static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005111xpath_translate(struct lyxp_set **args, uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005112{
5113 uint16_t i, j, new_used;
5114 char *new;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005115 uint8_t have_removed;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005116 struct lysc_node_leaf *sleaf;
5117 LY_ERR rc = LY_SUCCESS;
5118
5119 if (options & LYXP_SCNODE_ALL) {
5120 if ((args[0]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[0]))) {
5121 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5122 LOGWRN(set->ctx, "Argument #1 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005123 } else if (!warn_is_string_type(sleaf->type)) {
5124 LOGWRN(set->ctx, "Argument #1 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005125 }
5126 }
5127
5128 if ((args[1]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[1]))) {
5129 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5130 LOGWRN(set->ctx, "Argument #2 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005131 } else if (!warn_is_string_type(sleaf->type)) {
5132 LOGWRN(set->ctx, "Argument #2 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005133 }
5134 }
5135
5136 if ((args[2]->type == LYXP_SET_SCNODE_SET) && (sleaf = (struct lysc_node_leaf *)warn_get_scnode_in_ctx(args[2]))) {
5137 if (!(sleaf->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
5138 LOGWRN(set->ctx, "Argument #3 of %s is a %s node \"%s\".", __func__, lys_nodetype2str(sleaf->nodetype), sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005139 } else if (!warn_is_string_type(sleaf->type)) {
5140 LOGWRN(set->ctx, "Argument #3 of %s is node \"%s\", not of string-type.", __func__, sleaf->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005141 }
5142 }
5143 set_scnode_clear_ctx(set);
5144 return rc;
5145 }
5146
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005147 rc = lyxp_set_cast(args[0], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005148 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005149 rc = lyxp_set_cast(args[1], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005150 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005151 rc = lyxp_set_cast(args[2], LYXP_SET_STRING);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005152 LY_CHECK_RET(rc);
5153
5154 new = malloc((strlen(args[0]->val.str) + 1) * sizeof(char));
5155 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5156 new_used = 0;
5157
5158 have_removed = 0;
5159 for (i = 0; args[0]->val.str[i]; ++i) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02005160 uint8_t found = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005161
5162 for (j = 0; args[1]->val.str[j]; ++j) {
5163 if (args[0]->val.str[i] == args[1]->val.str[j]) {
5164 /* removing this char */
5165 if (j >= strlen(args[2]->val.str)) {
5166 have_removed = 1;
5167 found = 1;
5168 break;
5169 }
5170 /* replacing this char */
5171 new[new_used] = args[2]->val.str[j];
5172 ++new_used;
5173 found = 1;
5174 break;
5175 }
5176 }
5177
5178 /* copying this char */
5179 if (!found) {
5180 new[new_used] = args[0]->val.str[i];
5181 ++new_used;
5182 }
5183 }
5184
5185 if (have_removed) {
5186 new = ly_realloc(new, (new_used + 1) * sizeof(char));
5187 LY_CHECK_ERR_RET(!new, LOGMEM(set->ctx), LY_EMEM);
5188 }
5189 new[new_used] = '\0';
5190
Michal Vaskod3678892020-05-21 10:06:58 +02005191 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005192 set->type = LYXP_SET_STRING;
5193 set->val.str = new;
5194
5195 return LY_SUCCESS;
5196}
5197
5198/**
5199 * @brief Execute the XPath true() function. Returns LYXP_SET_BOOLEAN
5200 * with true value.
5201 *
5202 * @param[in] args Array of arguments.
5203 * @param[in] arg_count Count of elements in @p args.
5204 * @param[in,out] set Context and result set at the same time.
5205 * @param[in] options XPath options.
Michal Vasko0cbf54f2019-12-16 10:01:06 +01005206 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005207 */
5208static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005209xpath_true(struct lyxp_set **UNUSED(args), uint16_t UNUSED(arg_count), struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005210{
5211 if (options & LYXP_SCNODE_ALL) {
5212 set_scnode_clear_ctx(set);
5213 return LY_SUCCESS;
5214 }
5215
5216 set_fill_boolean(set, 1);
5217 return LY_SUCCESS;
5218}
5219
5220/*
5221 * moveto functions
5222 *
5223 * They and only they actually change the context (set).
5224 */
5225
5226/**
Michal Vasko6346ece2019-09-24 13:12:53 +02005227 * @brief Skip prefix and return corresponding model if there is a prefix. Logs directly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005228 *
Michal Vasko2104e9f2020-03-06 08:23:25 +01005229 * XPath @p set is expected to be a (sc)node set!
5230 *
Michal Vasko6346ece2019-09-24 13:12:53 +02005231 * @param[in,out] qname Qualified node name. If includes prefix, it is skipped.
5232 * @param[in,out] qname_len Length of @p qname, is updated accordingly.
5233 * @param[in] set Set with XPath context.
5234 * @param[out] moveto_mod Expected module of a matching node.
5235 * @return LY_ERR
Michal Vasko03ff5a72019-09-11 13:49:33 +02005236 */
Michal Vasko6346ece2019-09-24 13:12:53 +02005237static LY_ERR
5238moveto_resolve_model(const char **qname, uint16_t *qname_len, struct lyxp_set *set, const struct lys_module **moveto_mod)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005239{
Michal Vaskoed4fcfe2020-07-08 10:38:56 +02005240 const struct lys_module *mod = NULL;
Michal Vasko6346ece2019-09-24 13:12:53 +02005241 const char *ptr;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005242 size_t pref_len;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005243
Michal Vasko2104e9f2020-03-06 08:23:25 +01005244 assert((set->type == LYXP_SET_NODE_SET) || (set->type == LYXP_SET_SCNODE_SET));
5245
Michal Vasko6346ece2019-09-24 13:12:53 +02005246 if ((ptr = ly_strnchr(*qname, ':', *qname_len))) {
5247 /* specific module */
5248 pref_len = ptr - *qname;
Michal Vaskoc8a230d2020-08-14 12:17:10 +02005249 mod = ly_resolve_prefix(set->ctx, *qname, pref_len, set->format, (void *)set->local_mod);
Michal Vasko6346ece2019-09-24 13:12:53 +02005250
Michal Vasko004d3152020-06-11 19:59:22 +02005251 /* check for errors and non-implemented modules, as they are not valid */
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005252 if (!mod || !mod->implemented) {
Michal Vasko2104e9f2020-03-06 08:23:25 +01005253 if (set->type == LYXP_SET_SCNODE_SET) {
5254 LOGVAL(set->ctx, LY_VLOG_LYSC, set->ctx_scnode, LY_VCODE_XP_INMOD, pref_len, *qname);
5255 } else {
5256 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INMOD, pref_len, *qname);
5257 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005258 return LY_EVALID;
5259 }
Juraj Vijtiukd75faa62019-11-26 14:10:10 +01005260
Michal Vasko6346ece2019-09-24 13:12:53 +02005261 *qname += pref_len + 1;
5262 *qname_len -= pref_len + 1;
5263 } else if (((*qname)[0] == '*') && (*qname_len == 1)) {
5264 /* all modules - special case */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005265 mod = NULL;
Michal Vasko004d3152020-06-11 19:59:22 +02005266 } else if (set->type == LYXP_SET_SCNODE_SET) {
5267 /* current node module */
5268 mod = set->ctx_scnode->module;
Michal Vasko6346ece2019-09-24 13:12:53 +02005269 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02005270 /* current node module */
5271 mod = set->ctx_node->schema->module;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005272 }
5273
Michal Vasko6346ece2019-09-24 13:12:53 +02005274 *moveto_mod = mod;
5275 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005276}
5277
5278/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02005279 * @brief Move context @p set to the root. Handles absolute path.
5280 * Result is LYXP_SET_NODE_SET.
5281 *
5282 * @param[in,out] set Set to use.
5283 * @param[in] options Xpath options.
5284 */
5285static void
Radek Krejci1deb5be2020-08-26 16:43:36 +02005286moveto_root(struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005287{
Michal Vasko03ff5a72019-09-11 13:49:33 +02005288 if (!set) {
5289 return;
5290 }
5291
5292 if (options & LYXP_SCNODE_ALL) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005293 set_scnode_clear_ctx(set);
Michal Vaskoecd62de2019-11-13 12:35:11 +01005294 lyxp_set_scnode_insert_node(set, NULL, set->root_type);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005295 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005296 set->type = LYXP_SET_NODE_SET;
5297 set->used = 0;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005298 set_insert_node(set, NULL, 0, set->root_type, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005299 }
5300}
5301
5302/**
Michal Vaskoa1424542019-11-14 16:08:52 +01005303 * @brief Check whether a node has some unresolved "when".
5304 *
5305 * @param[in] node Node to check.
5306 * @return LY_ERR value (LY_EINCOMPLETE if there are some unresolved "when")
5307 */
5308static LY_ERR
5309moveto_when_check(const struct lyd_node *node)
5310{
5311 const struct lysc_node *schema;
5312
5313 if (!node) {
5314 return LY_SUCCESS;
5315 }
5316
5317 schema = node->schema;
5318 do {
5319 if (schema->when && !(node->flags & LYD_WHEN_TRUE)) {
5320 return LY_EINCOMPLETE;
5321 }
5322 schema = schema->parent;
5323 } while (schema && (schema->nodetype & (LYS_CASE | LYS_CHOICE)));
5324
5325 return LY_SUCCESS;
5326}
5327
5328/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02005329 * @brief Check @p node as a part of NameTest processing.
5330 *
5331 * @param[in] node Node to check.
5332 * @param[in] root_type XPath root node type.
Michal Vasko6b26e742020-07-17 15:02:10 +02005333 * @param[in] context_op XPath operation parent.
Michal Vaskod3678892020-05-21 10:06:58 +02005334 * @param[in] node_name Node name in the dictionary to move to, NULL for any node.
5335 * @param[in] moveto_mod Expected module of the node, NULL for any.
Michal Vasko6346ece2019-09-24 13:12:53 +02005336 * @return LY_ERR (LY_ENOT if node does not match, LY_EINCOMPLETE on unresolved when,
5337 * LY_EINVAL if netither node nor any children match)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005338 */
5339static LY_ERR
Michal Vasko6b26e742020-07-17 15:02:10 +02005340moveto_node_check(const struct lyd_node *node, enum lyxp_node_type root_type, const struct lysc_node *context_op,
Radek Krejci0f969882020-08-21 16:56:47 +02005341 const char *node_name, const struct lys_module *moveto_mod)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005342{
5343 /* module check */
5344 if (moveto_mod && (node->schema->module != moveto_mod)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005345 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005346 }
5347
Michal Vasko5c4e5892019-11-14 12:31:38 +01005348 /* context check */
5349 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (node->schema->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005350 return LY_EINVAL;
Michal Vasko6b26e742020-07-17 15:02:10 +02005351 } else if (context_op && (node->schema->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && (node->schema != context_op)) {
5352 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005353 }
5354
5355 /* name check */
Michal Vasko61ac2f62020-05-25 12:39:51 +02005356 if (node_name && strcmp(node_name, "*") && (node->schema->name != node_name)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005357 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005358 }
5359
Michal Vaskoa1424542019-11-14 16:08:52 +01005360 /* when check */
5361 if (moveto_when_check(node)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005362 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01005363 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005364
5365 /* match */
5366 return LY_SUCCESS;
5367}
5368
5369/**
5370 * @brief Check @p node as a part of schema NameTest processing.
5371 *
5372 * @param[in] node Schema node to check.
5373 * @param[in] root_type XPath root node type.
Michal Vasko6b26e742020-07-17 15:02:10 +02005374 * @param[in] context_op XPath operation parent.
Michal Vaskod3678892020-05-21 10:06:58 +02005375 * @param[in] node_name Node name in the dictionary to move to, NULL for any nodes.
5376 * @param[in] moveto_mod Expected module of the node, NULL for any.
Michal Vasko6346ece2019-09-24 13:12:53 +02005377 * @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 +02005378 */
5379static LY_ERR
Michal Vasko6b26e742020-07-17 15:02:10 +02005380moveto_scnode_check(const struct lysc_node *node, enum lyxp_node_type root_type, const struct lysc_node *context_op,
Radek Krejci0f969882020-08-21 16:56:47 +02005381 const char *node_name, const struct lys_module *moveto_mod)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005382{
Michal Vasko03ff5a72019-09-11 13:49:33 +02005383 /* module check */
Michal Vaskod3678892020-05-21 10:06:58 +02005384 if (moveto_mod && (node->module != moveto_mod)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005385 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005386 }
5387
5388 /* context check */
5389 if ((root_type == LYXP_NODE_ROOT_CONFIG) && (node->flags & LYS_CONFIG_R)) {
5390 return LY_EINVAL;
Michal Vasko6b26e742020-07-17 15:02:10 +02005391 } else if (context_op && (node->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) && (node != context_op)) {
5392 return LY_EINVAL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005393 }
5394
5395 /* name check */
Michal Vasko61ac2f62020-05-25 12:39:51 +02005396 if (node_name && strcmp(node_name, "*") && (node->name != node_name)) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005397 return LY_ENOT;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005398 }
5399
5400 /* match */
5401 return LY_SUCCESS;
5402}
5403
5404/**
Michal Vaskod3678892020-05-21 10:06:58 +02005405 * @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 +02005406 *
5407 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005408 * @param[in] mod Matching node module, NULL for any.
5409 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005410 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5411 */
5412static LY_ERR
Michal Vaskod3678892020-05-21 10:06:58 +02005413moveto_node(struct lyxp_set *set, const struct lys_module *mod, const char *ncname)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005414{
Michal Vaskof03ed032020-03-04 13:31:44 +01005415 uint32_t i;
Michal Vaskod3678892020-05-21 10:06:58 +02005416 const struct lyd_node *siblings, *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005417 LY_ERR rc;
5418
Michal Vaskod3678892020-05-21 10:06:58 +02005419 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005420 return LY_SUCCESS;
5421 }
5422
5423 if (set->type != LYXP_SET_NODE_SET) {
5424 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
5425 return LY_EVALID;
5426 }
5427
Michal Vasko03ff5a72019-09-11 13:49:33 +02005428 for (i = 0; i < set->used; ) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02005429 uint8_t replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005430
5431 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 +01005432 assert(!set->val.nodes[i].node);
Michal Vaskod3678892020-05-21 10:06:58 +02005433
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005434 /* search in all the trees */
Michal Vaskod3678892020-05-21 10:06:58 +02005435 siblings = set->tree;
Michal Vasko5bfd4be2020-06-23 13:26:19 +02005436 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005437 /* search in children */
Michal Vasko5bfd4be2020-06-23 13:26:19 +02005438 siblings = lyd_node_children(set->val.nodes[i].node, 0);
Michal Vaskod3678892020-05-21 10:06:58 +02005439 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005440
Michal Vaskod3678892020-05-21 10:06:58 +02005441 for (sub = siblings; sub; sub = sub->next) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005442 rc = moveto_node_check(sub, set->root_type, set->context_op, ncname, mod);
Michal Vaskod3678892020-05-21 10:06:58 +02005443 if (rc == LY_SUCCESS) {
5444 if (!replaced) {
5445 set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i);
5446 replaced = 1;
5447 } else {
5448 set_insert_node(set, sub, 0, LYXP_NODE_ELEM, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005449 }
Michal Vaskod3678892020-05-21 10:06:58 +02005450 ++i;
5451 } else if (rc == LY_EINCOMPLETE) {
5452 return rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005453 }
5454 }
5455
5456 if (!replaced) {
5457 /* no match */
5458 set_remove_node(set, i);
5459 }
5460 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005461
5462 return LY_SUCCESS;
5463}
5464
5465/**
Michal Vaskod3678892020-05-21 10:06:58 +02005466 * @brief Move context @p set to a node using hashes. Result is LYXP_SET_NODE_SET (or LYXP_SET_EMPTY).
5467 * Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005468 *
5469 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005470 * @param[in] scnode Matching node schema.
Michal Vasko004d3152020-06-11 19:59:22 +02005471 * @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 +02005472 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5473 */
5474static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02005475moveto_node_hash(struct lyxp_set *set, const struct lysc_node *scnode, const struct ly_path_predicate *predicates)
Michal Vaskod3678892020-05-21 10:06:58 +02005476{
Michal Vasko004d3152020-06-11 19:59:22 +02005477 LY_ERR ret = LY_SUCCESS;
Michal Vaskod3678892020-05-21 10:06:58 +02005478 uint32_t i;
Michal Vaskod3678892020-05-21 10:06:58 +02005479 const struct lyd_node *siblings;
Michal Vasko004d3152020-06-11 19:59:22 +02005480 struct lyd_node *sub, *inst = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02005481
Michal Vasko004d3152020-06-11 19:59:22 +02005482 assert(scnode && (!(scnode->nodetype & (LYS_LIST | LYS_LEAFLIST)) || predicates));
Michal Vaskod3678892020-05-21 10:06:58 +02005483
5484 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02005485 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005486 }
5487
5488 if (set->type != LYXP_SET_NODE_SET) {
5489 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 +02005490 ret = LY_EVALID;
5491 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005492 }
5493
5494 /* context check for all the nodes since we have the schema node */
5495 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (scnode->flags & LYS_CONFIG_R)) {
5496 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02005497 goto cleanup;
Radek Krejci0f969882020-08-21 16:56:47 +02005498 } else if (set->context_op && (scnode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF))
Michal Vasko6b26e742020-07-17 15:02:10 +02005499 && (scnode != set->context_op)) {
5500 lyxp_set_free_content(set);
5501 goto cleanup;
Michal Vasko004d3152020-06-11 19:59:22 +02005502 }
5503
5504 /* create specific data instance if needed */
5505 if (scnode->nodetype == LYS_LIST) {
5506 LY_CHECK_GOTO(ret = lyd_create_list(scnode, predicates, &inst), cleanup);
5507 } else if (scnode->nodetype == LYS_LEAFLIST) {
5508 LY_CHECK_GOTO(ret = lyd_create_term2(scnode, &predicates[0].value, &inst), cleanup);
Michal Vaskod3678892020-05-21 10:06:58 +02005509 }
5510
5511 for (i = 0; i < set->used; ) {
Michal Vaskod3678892020-05-21 10:06:58 +02005512 siblings = NULL;
5513
5514 if ((set->val.nodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.nodes[i].type == LYXP_NODE_ROOT)) {
5515 assert(!set->val.nodes[i].node);
5516
5517 /* search in all the trees */
5518 siblings = set->tree;
5519 } else if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
5520 /* search in children */
Michal Vasko5bfd4be2020-06-23 13:26:19 +02005521 siblings = lyd_node_children(set->val.nodes[i].node, 0);
Michal Vaskod3678892020-05-21 10:06:58 +02005522 }
5523
5524 /* find the node using hashes */
Michal Vasko004d3152020-06-11 19:59:22 +02005525 if (inst) {
5526 lyd_find_sibling_first(siblings, inst, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02005527 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02005528 lyd_find_sibling_val(siblings, scnode, NULL, 0, &sub);
Michal Vaskod3678892020-05-21 10:06:58 +02005529 }
5530
5531 /* when check */
5532 if (sub && moveto_when_check(sub)) {
Michal Vasko004d3152020-06-11 19:59:22 +02005533 ret = LY_EINCOMPLETE;
5534 goto cleanup;
Michal Vaskod3678892020-05-21 10:06:58 +02005535 }
5536
5537 if (sub) {
5538 /* pos filled later */
Michal Vaskocb1b7c02020-07-03 13:38:12 +02005539 set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i);
Michal Vaskod3678892020-05-21 10:06:58 +02005540 ++i;
Michal Vaskocb1b7c02020-07-03 13:38:12 +02005541 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02005542 /* no match */
5543 set_remove_node(set, i);
5544 }
5545 }
5546
Michal Vasko004d3152020-06-11 19:59:22 +02005547cleanup:
5548 lyd_free_tree(inst);
5549 return ret;
Michal Vaskod3678892020-05-21 10:06:58 +02005550}
5551
5552/**
5553 * @brief Move context @p set to a schema node. Result is LYXP_SET_SCNODE_SET (or LYXP_SET_EMPTY).
5554 *
5555 * @param[in,out] set Set to use.
5556 * @param[in] mod Matching node module, NULL for any.
5557 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005558 * @param[in] options XPath options.
5559 * @return LY_ERR
5560 */
5561static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005562moveto_scnode(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005563{
Radek Krejci1deb5be2020-08-26 16:43:36 +02005564 int idx;
5565 uint8_t temp_ctx = 0;
5566 uint32_t getnext_opts;
5567 uint32_t orig_used, i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005568 uint32_t mod_idx;
Michal Vasko519fd602020-05-26 12:17:39 +02005569 const struct lysc_node *iter, *start_parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005570
Michal Vaskod3678892020-05-21 10:06:58 +02005571 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005572 return LY_SUCCESS;
5573 }
5574
5575 if (set->type != LYXP_SET_SCNODE_SET) {
Michal Vaskof6e51882019-12-16 09:59:45 +01005576 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 +02005577 return LY_EVALID;
5578 }
5579
Michal Vaskocafad9d2019-11-07 15:20:03 +01005580 /* getnext opts */
5581 getnext_opts = LYS_GETNEXT_NOSTATECHECK;
5582 if (options & LYXP_SCNODE_OUTPUT) {
5583 getnext_opts |= LYS_GETNEXT_OUTPUT;
5584 }
5585
Michal Vasko03ff5a72019-09-11 13:49:33 +02005586 orig_used = set->used;
5587 for (i = 0; i < orig_used; ++i) {
5588 if (set->val.scnodes[i].in_ctx != 1) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01005589 if (set->val.scnodes[i].in_ctx != -2) {
5590 continue;
5591 }
5592
5593 /* remember context node */
5594 set->val.scnodes[i].in_ctx = -1;
Michal Vaskoec4df482019-12-16 10:02:18 +01005595 } else {
5596 set->val.scnodes[i].in_ctx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005597 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005598
5599 start_parent = set->val.scnodes[i].scnode;
5600
5601 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 +02005602 /* 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 +02005603 * so use it directly (root node itself is useless in this case) */
5604 mod_idx = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02005605 while (mod || (mod = (struct lys_module *)ly_ctx_get_module_iter(set->ctx, &mod_idx))) {
Michal Vasko519fd602020-05-26 12:17:39 +02005606 iter = NULL;
Michal Vasko509de4d2019-12-10 14:51:30 +01005607 /* module may not be implemented */
Michal Vasko519fd602020-05-26 12:17:39 +02005608 while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005609 if (!moveto_scnode_check(iter, set->root_type, set->context_op, ncname, mod)) {
Michal Vasko519fd602020-05-26 12:17:39 +02005610 idx = lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM);
Radek Krejci1deb5be2020-08-26 16:43:36 +02005611 LY_CHECK_RET(idx < 0, LY_EMEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005612 /* we need to prevent these nodes from being considered in this moveto */
Radek Krejci1deb5be2020-08-26 16:43:36 +02005613 if (((uint32_t)idx < orig_used) && ((uint32_t)idx > i)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005614 set->val.scnodes[idx].in_ctx = 2;
5615 temp_ctx = 1;
5616 }
5617 }
5618 }
5619
5620 if (!mod_idx) {
Michal Vaskod3678892020-05-21 10:06:58 +02005621 /* mod was specified, we are not going through the whole context */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005622 break;
5623 }
5624 /* next iteration */
Michal Vaskod3678892020-05-21 10:06:58 +02005625 mod = NULL;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005626 }
5627
Michal Vasko519fd602020-05-26 12:17:39 +02005628 } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
5629 iter = NULL;
5630 while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005631 if (!moveto_scnode_check(iter, set->root_type, set->context_op, ncname, (mod ? mod : set->local_mod))) {
Michal Vasko519fd602020-05-26 12:17:39 +02005632 idx = lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM);
Radek Krejci1deb5be2020-08-26 16:43:36 +02005633 LY_CHECK_RET(idx < 0, LY_EMEM);
5634 if (((uint32_t)idx < orig_used) && ((uint32_t)idx > i)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005635 set->val.scnodes[idx].in_ctx = 2;
5636 temp_ctx = 1;
5637 }
5638 }
5639 }
5640 }
5641 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005642
5643 /* correct temporary in_ctx values */
5644 if (temp_ctx) {
5645 for (i = 0; i < orig_used; ++i) {
5646 if (set->val.scnodes[i].in_ctx == 2) {
5647 set->val.scnodes[i].in_ctx = 1;
5648 }
5649 }
5650 }
5651
5652 return LY_SUCCESS;
5653}
5654
5655/**
Michal Vaskod3678892020-05-21 10:06:58 +02005656 * @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 +02005657 * Context position aware.
5658 *
5659 * @param[in] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005660 * @param[in] mod Matching node module, NULL for any.
5661 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005662 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5663 */
5664static LY_ERR
Michal Vaskod3678892020-05-21 10:06:58 +02005665moveto_node_alldesc(struct lyxp_set *set, const struct lys_module *mod, const char *ncname)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005666{
5667 uint32_t i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005668 const struct lyd_node *next, *elem, *start;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005669 struct lyxp_set ret_set;
5670 LY_ERR rc;
5671
Michal Vaskod3678892020-05-21 10:06:58 +02005672 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005673 return LY_SUCCESS;
5674 }
5675
5676 if (set->type != LYXP_SET_NODE_SET) {
5677 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
5678 return LY_EVALID;
5679 }
5680
Michal Vasko9f96a052020-03-10 09:41:45 +01005681 /* 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 +02005682 rc = moveto_node(set, NULL, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005683 LY_CHECK_RET(rc);
5684
Michal Vasko6346ece2019-09-24 13:12:53 +02005685 /* this loop traverses all the nodes in the set and adds/keeps only those that match qname */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005686 set_init(&ret_set, set);
5687 for (i = 0; i < set->used; ++i) {
5688
5689 /* TREE DFS */
5690 start = set->val.nodes[i].node;
5691 for (elem = next = start; elem; elem = next) {
Michal Vasko6b26e742020-07-17 15:02:10 +02005692 rc = moveto_node_check(elem, set->root_type, set->context_op, ncname, mod);
Michal Vasko6346ece2019-09-24 13:12:53 +02005693 if (!rc) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005694 /* add matching node into result set */
5695 set_insert_node(&ret_set, elem, 0, LYXP_NODE_ELEM, ret_set.used);
5696 if (set_dup_node_check(set, elem, LYXP_NODE_ELEM, i)) {
5697 /* the node is a duplicate, we'll process it later in the set */
5698 goto skip_children;
5699 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005700 } else if (rc == LY_EINCOMPLETE) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005701 return rc;
5702 } else if (rc == LY_EINVAL) {
5703 goto skip_children;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005704 }
5705
5706 /* TREE DFS NEXT ELEM */
5707 /* select element for the next run - children first */
Michal Vasko5bfd4be2020-06-23 13:26:19 +02005708 next = lyd_node_children(elem, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005709 if (!next) {
5710skip_children:
5711 /* no children, so try siblings, but only if it's not the start,
5712 * that is considered to be the root and it's siblings are not traversed */
5713 if (elem != start) {
5714 next = elem->next;
5715 } else {
5716 break;
5717 }
5718 }
5719 while (!next) {
5720 /* no siblings, go back through the parents */
5721 if ((struct lyd_node *)elem->parent == start) {
5722 /* we are done, no next element to process */
5723 break;
5724 }
5725 /* parent is already processed, go to its sibling */
5726 elem = (struct lyd_node *)elem->parent;
5727 next = elem->next;
5728 }
5729 }
5730 }
5731
5732 /* make the temporary set the current one */
5733 ret_set.ctx_pos = set->ctx_pos;
5734 ret_set.ctx_size = set->ctx_size;
Michal Vaskod3678892020-05-21 10:06:58 +02005735 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005736 memcpy(set, &ret_set, sizeof *set);
5737
5738 return LY_SUCCESS;
5739}
5740
5741/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005742 * @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 +02005743 *
5744 * @param[in] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005745 * @param[in] mod Matching node module, NULL for any.
5746 * @param[in] ncname Matching node name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005747 * @param[in] options XPath options.
5748 * @return LY_ERR
5749 */
5750static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02005751moveto_scnode_alldesc(struct lyxp_set *set, const struct lys_module *mod, const char *ncname, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005752{
Radek Krejci1deb5be2020-08-26 16:43:36 +02005753 uint32_t i, orig_used;
5754 int idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005755 const struct lysc_node *next, *elem, *start;
Michal Vasko6346ece2019-09-24 13:12:53 +02005756 LY_ERR rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005757
Michal Vaskod3678892020-05-21 10:06:58 +02005758 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005759 return LY_SUCCESS;
5760 }
5761
5762 if (set->type != LYXP_SET_SCNODE_SET) {
Michal Vaskof6e51882019-12-16 09:59:45 +01005763 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 +02005764 return LY_EVALID;
5765 }
5766
Michal Vasko03ff5a72019-09-11 13:49:33 +02005767 orig_used = set->used;
5768 for (i = 0; i < orig_used; ++i) {
5769 if (set->val.scnodes[i].in_ctx != 1) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01005770 if (set->val.scnodes[i].in_ctx != -2) {
5771 continue;
5772 }
5773
5774 /* remember context node */
5775 set->val.scnodes[i].in_ctx = -1;
Michal Vaskoec4df482019-12-16 10:02:18 +01005776 } else {
5777 set->val.scnodes[i].in_ctx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005778 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02005779
5780 /* TREE DFS */
5781 start = set->val.scnodes[i].scnode;
5782 for (elem = next = start; elem; elem = next) {
Michal Vasko6346ece2019-09-24 13:12:53 +02005783 if ((elem == start) || (elem->nodetype & (LYS_CHOICE | LYS_CASE))) {
5784 /* schema-only nodes, skip root */
Michal Vasko03ff5a72019-09-11 13:49:33 +02005785 goto next_iter;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005786 }
5787
Michal Vasko6b26e742020-07-17 15:02:10 +02005788 rc = moveto_scnode_check(elem, set->root_type, set->context_op, ncname, mod);
Michal Vasko6346ece2019-09-24 13:12:53 +02005789 if (!rc) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01005790 if ((idx = lyxp_set_scnode_dup_node_check(set, elem, LYXP_NODE_ELEM, i)) > -1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005791 set->val.scnodes[idx].in_ctx = 1;
Radek Krejci1deb5be2020-08-26 16:43:36 +02005792 if ((uint32_t)idx > i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005793 /* we will process it later in the set */
5794 goto skip_children;
5795 }
5796 } else {
Michal Vaskoecd62de2019-11-13 12:35:11 +01005797 lyxp_set_scnode_insert_node(set, elem, LYXP_NODE_ELEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005798 }
Michal Vasko6346ece2019-09-24 13:12:53 +02005799 } else if (rc == LY_EINVAL) {
5800 goto skip_children;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005801 }
5802
5803next_iter:
5804 /* TREE DFS NEXT ELEM */
5805 /* select element for the next run - children first */
5806 next = lysc_node_children(elem, options & LYXP_SCNODE_OUTPUT ? LYS_CONFIG_R : LYS_CONFIG_W);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005807 if (!next) {
5808skip_children:
5809 /* no children, so try siblings, but only if it's not the start,
5810 * that is considered to be the root and it's siblings are not traversed */
5811 if (elem != start) {
5812 next = elem->next;
5813 } else {
5814 break;
5815 }
5816 }
5817 while (!next) {
5818 /* no siblings, go back through the parents */
5819 if (elem->parent == start) {
5820 /* we are done, no next element to process */
5821 break;
5822 }
5823 /* parent is already processed, go to its sibling */
5824 elem = elem->parent;
5825 next = elem->next;
5826 }
5827 }
5828 }
5829
5830 return LY_SUCCESS;
5831}
5832
5833/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005834 * @brief Move context @p set to an attribute. Result is LYXP_SET_NODE_SET.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005835 * Indirectly context position aware.
5836 *
5837 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005838 * @param[in] mod Matching metadata module, NULL for any.
5839 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005840 * @return LY_ERR
5841 */
5842static LY_ERR
Michal Vaskod3678892020-05-21 10:06:58 +02005843moveto_attr(struct lyxp_set *set, const struct lys_module *mod, const char *ncname)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005844{
Michal Vasko9f96a052020-03-10 09:41:45 +01005845 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005846
Michal Vaskod3678892020-05-21 10:06:58 +02005847 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005848 return LY_SUCCESS;
5849 }
5850
5851 if (set->type != LYXP_SET_NODE_SET) {
5852 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
5853 return LY_EVALID;
5854 }
5855
Radek Krejci1deb5be2020-08-26 16:43:36 +02005856 for (uint32_t i = 0; i < set->used; ) {
5857 uint8_t replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005858
5859 /* only attributes of an elem (not dummy) can be in the result, skip all the rest;
5860 * our attributes are always qualified */
Michal Vasko5c4e5892019-11-14 12:31:38 +01005861 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01005862 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005863
5864 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02005865 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005866 continue;
5867 }
5868
Michal Vaskod3678892020-05-21 10:06:58 +02005869 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005870 /* match */
5871 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01005872 set->val.meta[i].meta = sub;
5873 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005874 /* pos does not change */
5875 replaced = 1;
5876 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01005877 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 +02005878 }
5879 ++i;
5880 }
5881 }
5882 }
5883
5884 if (!replaced) {
5885 /* no match */
5886 set_remove_node(set, i);
5887 }
5888 }
5889
5890 return LY_SUCCESS;
5891}
5892
5893/**
5894 * @brief Move context @p set1 to union with @p set2. @p set2 is emptied afterwards.
Michal Vasko61ac2f62020-05-25 12:39:51 +02005895 * Result is LYXP_SET_NODE_SET. Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005896 *
5897 * @param[in,out] set1 Set to use for the result.
5898 * @param[in] set2 Set that is copied to @p set1.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005899 * @return LY_ERR
5900 */
5901static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005902moveto_union(struct lyxp_set *set1, struct lyxp_set *set2)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005903{
5904 LY_ERR rc;
5905
Michal Vaskod3678892020-05-21 10:06:58 +02005906 if ((set1->type != LYXP_SET_NODE_SET) || (set2->type != LYXP_SET_NODE_SET)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005907 LOGVAL(set1->ctx, LY_VLOG_LYD, set1->ctx_node, LY_VCODE_XP_INOP_2, "union", print_set_type(set1), print_set_type(set2));
5908 return LY_EVALID;
5909 }
5910
5911 /* set2 is empty or both set1 and set2 */
Michal Vaskod3678892020-05-21 10:06:58 +02005912 if (!set2->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005913 return LY_SUCCESS;
5914 }
5915
Michal Vaskod3678892020-05-21 10:06:58 +02005916 if (!set1->used) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005917 memcpy(set1, set2, sizeof *set1);
5918 /* dynamic memory belongs to set1 now, do not free */
Michal Vaskod3678892020-05-21 10:06:58 +02005919 memset(set2, 0, sizeof *set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005920 return LY_SUCCESS;
5921 }
5922
5923 /* we assume sets are sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005924 assert(!set_sort(set1) && !set_sort(set2));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005925
5926 /* sort, remove duplicates */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005927 rc = set_sorted_merge(set1, set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005928 LY_CHECK_RET(rc);
5929
5930 /* final set must be sorted */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005931 assert(!set_sort(set1));
Michal Vasko03ff5a72019-09-11 13:49:33 +02005932
5933 return LY_SUCCESS;
5934}
5935
5936/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02005937 * @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 +02005938 * Context position aware.
5939 *
5940 * @param[in,out] set Set to use.
Michal Vaskod3678892020-05-21 10:06:58 +02005941 * @param[in] mod Matching metadata module, NULL for any.
5942 * @param[in] ncname Matching metadata name in the dictionary, NULL for any.
Michal Vasko03ff5a72019-09-11 13:49:33 +02005943 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
5944 */
5945static int
Michal Vaskod3678892020-05-21 10:06:58 +02005946moveto_attr_alldesc(struct lyxp_set *set, const struct lys_module *mod, const char *ncname)
Michal Vasko03ff5a72019-09-11 13:49:33 +02005947{
Michal Vasko9f96a052020-03-10 09:41:45 +01005948 struct lyd_meta *sub;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005949 struct lyxp_set *set_all_desc = NULL;
5950 LY_ERR rc;
5951
Michal Vaskod3678892020-05-21 10:06:58 +02005952 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005953 return LY_SUCCESS;
5954 }
5955
5956 if (set->type != LYXP_SET_NODE_SET) {
5957 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
5958 return LY_EVALID;
5959 }
5960
Michal Vasko03ff5a72019-09-11 13:49:33 +02005961 /* can be optimized similarly to moveto_node_alldesc() and save considerable amount of memory,
5962 * but it likely won't be used much, so it's a waste of time */
5963 /* copy the context */
5964 set_all_desc = set_copy(set);
5965 /* get all descendant nodes (the original context nodes are removed) */
Michal Vaskod3678892020-05-21 10:06:58 +02005966 rc = moveto_node_alldesc(set_all_desc, NULL, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005967 if (rc != LY_SUCCESS) {
5968 lyxp_set_free(set_all_desc);
5969 return rc;
5970 }
5971 /* prepend the original context nodes */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01005972 rc = moveto_union(set, set_all_desc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02005973 if (rc != LY_SUCCESS) {
5974 lyxp_set_free(set_all_desc);
5975 return rc;
5976 }
5977 lyxp_set_free(set_all_desc);
5978
Radek Krejci1deb5be2020-08-26 16:43:36 +02005979 for (uint32_t i = 0; i < set->used; ) {
5980 uint8_t replaced = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005981
5982 /* only attributes of an elem can be in the result, skip all the rest,
5983 * we have all attributes qualified in lyd tree */
5984 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
Michal Vasko9f96a052020-03-10 09:41:45 +01005985 for (sub = set->val.nodes[i].node->meta; sub; sub = sub->next) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005986 /* check "namespace" */
Michal Vaskod3678892020-05-21 10:06:58 +02005987 if (mod && (sub->annotation->module != mod)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005988 continue;
5989 }
5990
Michal Vaskod3678892020-05-21 10:06:58 +02005991 if (!ncname || (sub->name == ncname)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02005992 /* match */
5993 if (!replaced) {
Michal Vasko9f96a052020-03-10 09:41:45 +01005994 set->val.meta[i].meta = sub;
5995 set->val.meta[i].type = LYXP_NODE_META;
Michal Vasko03ff5a72019-09-11 13:49:33 +02005996 /* pos does not change */
5997 replaced = 1;
5998 } else {
Michal Vasko9f96a052020-03-10 09:41:45 +01005999 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 +02006000 }
6001 ++i;
6002 }
6003 }
6004 }
6005
6006 if (!replaced) {
6007 /* no match */
6008 set_remove_node(set, i);
6009 }
6010 }
6011
6012 return LY_SUCCESS;
6013}
6014
6015/**
Michal Vasko61ac2f62020-05-25 12:39:51 +02006016 * @brief Move context @p set to self and al chilren, recursively. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET.
6017 * Context position aware.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006018 *
6019 * @param[in] parent Current parent.
6020 * @param[in] parent_pos Position of @p parent.
6021 * @param[in] parent_type Node type of @p parent.
6022 * @param[in,out] to_set Set to use.
6023 * @param[in] dup_check_set Set for checking duplicities.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006024 * @param[in] options XPath options.
6025 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6026 */
6027static LY_ERR
6028moveto_self_add_children_r(const struct lyd_node *parent, uint32_t parent_pos, enum lyxp_node_type parent_type,
Radek Krejci1deb5be2020-08-26 16:43:36 +02006029 struct lyxp_set *to_set, const struct lyxp_set *dup_check_set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006030{
Michal Vasko61ac2f62020-05-25 12:39:51 +02006031 const struct lyd_node *iter, *first;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006032 LY_ERR rc;
6033
6034 switch (parent_type) {
6035 case LYXP_NODE_ROOT:
6036 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko61ac2f62020-05-25 12:39:51 +02006037 assert(!parent);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006038
Michal Vasko61ac2f62020-05-25 12:39:51 +02006039 /* add all top-level nodes as elements */
6040 first = to_set->tree;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006041 break;
6042 case LYXP_NODE_ELEM:
Michal Vasko61ac2f62020-05-25 12:39:51 +02006043 /* add just the text node of this term element node */
6044 if (parent->schema->nodetype & (LYD_NODE_TERM | LYD_NODE_ANY)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006045 if (!set_dup_node_check(dup_check_set, parent, LYXP_NODE_TEXT, -1)) {
6046 set_insert_node(to_set, parent, parent_pos, LYXP_NODE_TEXT, to_set->used);
6047 }
Michal Vasko61ac2f62020-05-25 12:39:51 +02006048 return LY_SUCCESS;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006049 }
Michal Vasko61ac2f62020-05-25 12:39:51 +02006050
6051 /* add all the children of this node */
Michal Vasko5bfd4be2020-06-23 13:26:19 +02006052 first = lyd_node_children(parent, 0);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006053 break;
6054 default:
6055 LOGINT_RET(parent->schema->module->ctx);
6056 }
6057
Michal Vasko61ac2f62020-05-25 12:39:51 +02006058 /* add all top-level nodes as elements */
6059 LY_LIST_FOR(first, iter) {
6060 /* context check */
6061 if ((parent_type == LYXP_NODE_ROOT_CONFIG) && (iter->schema->flags & LYS_CONFIG_R)) {
6062 continue;
6063 }
6064
6065 /* when check */
6066 if (moveto_when_check(iter)) {
6067 return LY_EINCOMPLETE;
6068 }
6069
6070 if (!set_dup_node_check(dup_check_set, iter, LYXP_NODE_ELEM, -1)) {
6071 set_insert_node(to_set, iter, 0, LYXP_NODE_ELEM, to_set->used);
6072
6073 /* also add all the children of this node, recursively */
6074 rc = moveto_self_add_children_r(iter, 0, LYXP_NODE_ELEM, to_set, dup_check_set, options);
6075 LY_CHECK_RET(rc);
6076 }
6077 }
6078
Michal Vasko03ff5a72019-09-11 13:49:33 +02006079 return LY_SUCCESS;
6080}
6081
6082/**
6083 * @brief Move context @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_NODE_SET
6084 * (or LYXP_SET_EMPTY). Context position aware.
6085 *
6086 * @param[in,out] set Set to use.
6087 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6088 * @param[in] options XPath options.
6089 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6090 */
6091static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02006092moveto_self(struct lyxp_set *set, uint8_t all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006093{
Michal Vasko03ff5a72019-09-11 13:49:33 +02006094 struct lyxp_set ret_set;
6095 LY_ERR rc;
6096
Michal Vaskod3678892020-05-21 10:06:58 +02006097 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006098 return LY_SUCCESS;
6099 }
6100
6101 if (set->type != LYXP_SET_NODE_SET) {
6102 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
6103 return LY_EVALID;
6104 }
6105
6106 /* nothing to do */
6107 if (!all_desc) {
6108 return LY_SUCCESS;
6109 }
6110
Michal Vasko03ff5a72019-09-11 13:49:33 +02006111 /* add all the children, they get added recursively */
6112 set_init(&ret_set, set);
Radek Krejci1deb5be2020-08-26 16:43:36 +02006113 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006114 /* copy the current node to tmp */
6115 set_insert_node(&ret_set, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, ret_set.used);
6116
6117 /* do not touch attributes and text nodes */
Michal Vasko9f96a052020-03-10 09:41:45 +01006118 if ((set->val.nodes[i].type == LYXP_NODE_TEXT) || (set->val.nodes[i].type == LYXP_NODE_META)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006119 continue;
6120 }
6121
Michal Vasko03ff5a72019-09-11 13:49:33 +02006122 /* add all the children */
6123 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 +01006124 set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006125 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006126 lyxp_set_free_content(&ret_set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006127 return rc;
6128 }
6129 }
6130
6131 /* use the temporary set as the current one */
6132 ret_set.ctx_pos = set->ctx_pos;
6133 ret_set.ctx_size = set->ctx_size;
Michal Vaskod3678892020-05-21 10:06:58 +02006134 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006135 memcpy(set, &ret_set, sizeof *set);
6136
6137 return LY_SUCCESS;
6138}
6139
6140/**
6141 * @brief Move context schema @p set to self. Handles '/' or '//' and '.'. Result is LYXP_SET_SCNODE_SET
6142 * (or LYXP_SET_EMPTY).
6143 *
6144 * @param[in,out] set Set to use.
6145 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6146 * @param[in] options XPath options.
6147 * @return LY_ERR
6148 */
6149static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02006150moveto_scnode_self(struct lyxp_set *set, uint8_t all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006151{
Radek Krejci1deb5be2020-08-26 16:43:36 +02006152 uint32_t getnext_opts;
6153 uint32_t mod_idx;
Michal Vasko519fd602020-05-26 12:17:39 +02006154 const struct lysc_node *iter, *start_parent;
6155 const struct lys_module *mod;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006156
Michal Vaskod3678892020-05-21 10:06:58 +02006157 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006158 return LY_SUCCESS;
6159 }
6160
6161 if (set->type != LYXP_SET_SCNODE_SET) {
Michal Vaskof6e51882019-12-16 09:59:45 +01006162 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 +02006163 return LY_EVALID;
6164 }
6165
6166 /* nothing to do */
6167 if (!all_desc) {
6168 return LY_SUCCESS;
6169 }
6170
Michal Vasko519fd602020-05-26 12:17:39 +02006171 /* getnext opts */
6172 getnext_opts = LYS_GETNEXT_NOSTATECHECK;
6173 if (options & LYXP_SCNODE_OUTPUT) {
6174 getnext_opts |= LYS_GETNEXT_OUTPUT;
6175 }
6176
6177 /* add all the children, recursively as they are being added into the same set */
Radek Krejci1deb5be2020-08-26 16:43:36 +02006178 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006179 if (set->val.scnodes[i].in_ctx != 1) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006180 if (set->val.scnodes[i].in_ctx != -2) {
6181 continue;
6182 }
6183
Michal Vasko519fd602020-05-26 12:17:39 +02006184 /* remember context node */
6185 set->val.scnodes[i].in_ctx = -1;
6186 } else {
6187 set->val.scnodes[i].in_ctx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006188 }
6189
Michal Vasko519fd602020-05-26 12:17:39 +02006190 start_parent = set->val.scnodes[i].scnode;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006191
Michal Vasko519fd602020-05-26 12:17:39 +02006192 if ((set->val.scnodes[i].type == LYXP_NODE_ROOT_CONFIG) || (set->val.scnodes[i].type == LYXP_NODE_ROOT)) {
6193 /* it can actually be in any module, it's all <running> */
6194 mod_idx = 0;
6195 while ((mod = (struct lys_module *)ly_ctx_get_module_iter(set->ctx, &mod_idx))) {
6196 iter = NULL;
6197 /* module may not be implemented */
6198 while (mod->implemented && (iter = lys_getnext(iter, NULL, mod->compiled, getnext_opts))) {
6199 /* context check */
6200 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) {
6201 continue;
6202 }
6203
6204 lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM);
6205 /* throw away the insert index, we want to consider that node again, recursively */
6206 }
6207 }
6208
6209 } else if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
6210 iter = NULL;
6211 while ((iter = lys_getnext(iter, start_parent, NULL, getnext_opts))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006212 /* context check */
Michal Vasko519fd602020-05-26 12:17:39 +02006213 if ((set->root_type == LYXP_NODE_ROOT_CONFIG) && (iter->flags & LYS_CONFIG_R)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006214 continue;
6215 }
6216
Michal Vasko519fd602020-05-26 12:17:39 +02006217 lyxp_set_scnode_insert_node(set, iter, LYXP_NODE_ELEM);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006218 }
6219 }
6220 }
6221
6222 return LY_SUCCESS;
6223}
6224
6225/**
6226 * @brief Move context @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_NODE_SET
6227 * (or LYXP_SET_EMPTY). Context position aware.
6228 *
6229 * @param[in] set Set to use.
6230 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6231 * @param[in] options XPath options.
6232 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6233 */
6234static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02006235moveto_parent(struct lyxp_set *set, uint8_t all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006236{
6237 LY_ERR rc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006238 struct lyd_node *node, *new_node;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006239 enum lyxp_node_type new_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006240
Michal Vaskod3678892020-05-21 10:06:58 +02006241 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006242 return LY_SUCCESS;
6243 }
6244
6245 if (set->type != LYXP_SET_NODE_SET) {
6246 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INOP_1, "path operator", print_set_type(set));
6247 return LY_EVALID;
6248 }
6249
6250 if (all_desc) {
6251 /* <path>//.. == <path>//./.. */
6252 rc = moveto_self(set, 1, options);
6253 LY_CHECK_RET(rc);
6254 }
6255
Radek Krejci1deb5be2020-08-26 16:43:36 +02006256 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006257 node = set->val.nodes[i].node;
6258
6259 if (set->val.nodes[i].type == LYXP_NODE_ELEM) {
6260 new_node = (struct lyd_node *)node->parent;
6261 } else if (set->val.nodes[i].type == LYXP_NODE_TEXT) {
6262 new_node = node;
Michal Vasko9f96a052020-03-10 09:41:45 +01006263 } else if (set->val.nodes[i].type == LYXP_NODE_META) {
6264 new_node = set->val.meta[i].meta->parent;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006265 if (!new_node) {
6266 LOGINT_RET(set->ctx);
6267 }
6268 } else {
6269 /* root does not have a parent */
Michal Vasko2caefc12019-11-14 16:07:56 +01006270 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006271 continue;
6272 }
6273
Michal Vaskoa1424542019-11-14 16:08:52 +01006274 /* when check */
6275 if (moveto_when_check(new_node)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006276 return LY_EINCOMPLETE;
Michal Vaskoa1424542019-11-14 16:08:52 +01006277 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006278
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006279 if (!new_node) {
Radek Krejcif6a11002020-08-21 13:29:07 +02006280 /* node already there can also be the root */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006281 new_type = set->root_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006282
Michal Vasko03ff5a72019-09-11 13:49:33 +02006283 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02006284 /* node has a standard parent (it can equal the root, it's not the root yet since they are fake) */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006285 new_type = LYXP_NODE_ELEM;
6286 }
6287
Michal Vasko03ff5a72019-09-11 13:49:33 +02006288 if (set_dup_node_check(set, new_node, new_type, -1)) {
Michal Vasko2caefc12019-11-14 16:07:56 +01006289 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006290 } else {
6291 set_replace_node(set, new_node, 0, new_type, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006292 }
6293 }
6294
Michal Vasko2caefc12019-11-14 16:07:56 +01006295 set_remove_nodes_none(set);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006296 assert(!set_sort(set) && !set_sorted_dup_node_clean(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006297
6298 return LY_SUCCESS;
6299}
6300
6301/**
6302 * @brief Move context schema @p set to parent. Handles '/' or '//' and '..'. Result is LYXP_SET_SCNODE_SET
6303 * (or LYXP_SET_EMPTY).
6304 *
6305 * @param[in] set Set to use.
6306 * @param[in] all_desc Whether to go to all descendants ('//') or not ('/').
6307 * @param[in] options XPath options.
6308 * @return LY_ERR
6309 */
6310static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02006311moveto_scnode_parent(struct lyxp_set *set, uint8_t all_desc, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006312{
Radek Krejci1deb5be2020-08-26 16:43:36 +02006313 int idx;
6314 uint32_t i, orig_used;
6315 uint8_t temp_ctx = 0;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006316 const struct lysc_node *node, *new_node;
6317 enum lyxp_node_type new_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006318
Michal Vaskod3678892020-05-21 10:06:58 +02006319 if (!set) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006320 return LY_SUCCESS;
6321 }
6322
6323 if (set->type != LYXP_SET_SCNODE_SET) {
Michal Vaskof6e51882019-12-16 09:59:45 +01006324 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 +02006325 return LY_EVALID;
6326 }
6327
6328 if (all_desc) {
6329 /* <path>//.. == <path>//./.. */
Radek Krejci1deb5be2020-08-26 16:43:36 +02006330 LY_CHECK_RET(moveto_scnode_self(set, 1, options));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006331 }
6332
Michal Vasko03ff5a72019-09-11 13:49:33 +02006333 orig_used = set->used;
6334 for (i = 0; i < orig_used; ++i) {
6335 if (set->val.scnodes[i].in_ctx != 1) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01006336 if (set->val.scnodes[i].in_ctx != -2) {
6337 continue;
6338 }
6339
6340 /* remember context node */
6341 set->val.scnodes[i].in_ctx = -1;
Michal Vaskoec4df482019-12-16 10:02:18 +01006342 } else {
6343 set->val.scnodes[i].in_ctx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006344 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02006345
6346 node = set->val.scnodes[i].scnode;
6347
6348 if (set->val.scnodes[i].type == LYXP_NODE_ELEM) {
Michal Vaskod3678892020-05-21 10:06:58 +02006349 new_node = lysc_data_parent(node);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006350 } else {
6351 /* root does not have a parent */
6352 continue;
6353 }
6354
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006355 if (!new_node) {
Radek Krejcif6a11002020-08-21 13:29:07 +02006356 /* node has no parent */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006357 new_type = set->root_type;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006358
Michal Vasko03ff5a72019-09-11 13:49:33 +02006359 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02006360 /* node has a standard parent (it can equal the root, it's not the root yet since they are fake) */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006361 new_type = LYXP_NODE_ELEM;
6362 }
6363
Michal Vaskoecd62de2019-11-13 12:35:11 +01006364 idx = lyxp_set_scnode_insert_node(set, new_node, new_type);
Radek Krejci1deb5be2020-08-26 16:43:36 +02006365 LY_CHECK_RET(idx < 0, LY_EMEM);
6366 if (((uint32_t)idx < orig_used) && ((uint32_t)idx > i)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006367 set->val.scnodes[idx].in_ctx = 2;
6368 temp_ctx = 1;
6369 }
6370 }
6371
6372 if (temp_ctx) {
6373 for (i = 0; i < orig_used; ++i) {
6374 if (set->val.scnodes[i].in_ctx == 2) {
6375 set->val.scnodes[i].in_ctx = 1;
6376 }
6377 }
6378 }
6379
6380 return LY_SUCCESS;
6381}
6382
6383/**
6384 * @brief Move context @p set to the result of a comparison. Handles '=', '!=', '<=', '<', '>=', or '>'.
6385 * Result is LYXP_SET_BOOLEAN. Indirectly context position aware.
6386 *
6387 * @param[in,out] set1 Set to use for the result.
6388 * @param[in] set2 Set acting as the second operand for @p op.
6389 * @param[in] op Comparison operator to process.
6390 * @param[in] options XPath options.
6391 * @return LY_ERR
6392 */
6393static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02006394moveto_op_comp(struct lyxp_set *set1, struct lyxp_set *set2, const char *op, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006395{
6396 /*
6397 * NODE SET + NODE SET = NODE SET + STRING /(1 NODE SET) 2 STRING
6398 * NODE SET + STRING = STRING + STRING /1 STRING (2 STRING)
6399 * NODE SET + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6400 * NODE SET + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6401 * STRING + NODE SET = STRING + STRING /(1 STRING) 2 STRING
6402 * NUMBER + NODE SET = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6403 * BOOLEAN + NODE SET = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6404 *
6405 * '=' or '!='
6406 * BOOLEAN + BOOLEAN
6407 * BOOLEAN + STRING = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6408 * BOOLEAN + NUMBER = BOOLEAN + BOOLEAN /(1 BOOLEAN) 2 BOOLEAN
6409 * STRING + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6410 * NUMBER + BOOLEAN = BOOLEAN + BOOLEAN /1 BOOLEAN (2 BOOLEAN)
6411 * NUMBER + NUMBER
6412 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6413 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6414 * STRING + STRING
6415 *
6416 * '<=', '<', '>=', '>'
6417 * NUMBER + NUMBER
6418 * BOOLEAN + BOOLEAN = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6419 * BOOLEAN + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6420 * BOOLEAN + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6421 * NUMBER + STRING = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6422 * STRING + STRING = NUMBER + NUMBER /1 NUMBER, 2 NUMBER
6423 * STRING + NUMBER = NUMBER + NUMBER /1 NUMBER (2 NUMBER)
6424 * NUMBER + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6425 * STRING + BOOLEAN = NUMBER + NUMBER /(1 NUMBER) 2 NUMBER
6426 */
6427 struct lyxp_set iter1, iter2;
6428 int result;
6429 int64_t i;
6430 LY_ERR rc;
6431
Michal Vasko004d3152020-06-11 19:59:22 +02006432 memset(&iter1, 0, sizeof iter1);
6433 memset(&iter2, 0, sizeof iter2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006434
6435 /* iterative evaluation with node-sets */
6436 if ((set1->type == LYXP_SET_NODE_SET) || (set2->type == LYXP_SET_NODE_SET)) {
6437 if (set1->type == LYXP_SET_NODE_SET) {
6438 for (i = 0; i < set1->used; ++i) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006439 /* cast set1 */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006440 switch (set2->type) {
6441 case LYXP_SET_NUMBER:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006442 rc = set_comp_cast(&iter1, set1, LYXP_SET_NUMBER, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006443 break;
6444 case LYXP_SET_BOOLEAN:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006445 rc = set_comp_cast(&iter1, set1, LYXP_SET_BOOLEAN, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006446 break;
6447 default:
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006448 rc = set_comp_cast(&iter1, set1, LYXP_SET_STRING, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006449 break;
6450 }
6451 LY_CHECK_RET(rc);
6452
Michal Vasko4c7763f2020-07-27 17:40:37 +02006453 /* canonize set2 */
6454 LY_CHECK_ERR_RET(rc = set_comp_canonize(&iter2, set2, &set1->val.nodes[i]), lyxp_set_free_content(&iter1), rc);
6455
6456 /* compare recursively */
6457 rc = moveto_op_comp(&iter1, &iter2, op, options);
6458 lyxp_set_free_content(&iter2);
6459 LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006460
6461 /* lazy evaluation until true */
Michal Vasko004d3152020-06-11 19:59:22 +02006462 if (iter1.val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006463 set_fill_boolean(set1, 1);
6464 return LY_SUCCESS;
6465 }
6466 }
6467 } else {
6468 for (i = 0; i < set2->used; ++i) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006469 /* set set2 */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006470 switch (set1->type) {
Michal Vasko4c7763f2020-07-27 17:40:37 +02006471 case LYXP_SET_NUMBER:
6472 rc = set_comp_cast(&iter2, set2, LYXP_SET_NUMBER, i);
6473 break;
6474 case LYXP_SET_BOOLEAN:
6475 rc = set_comp_cast(&iter2, set2, LYXP_SET_BOOLEAN, i);
6476 break;
6477 default:
6478 rc = set_comp_cast(&iter2, set2, LYXP_SET_STRING, i);
6479 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006480 }
6481 LY_CHECK_RET(rc);
6482
Michal Vasko4c7763f2020-07-27 17:40:37 +02006483 /* canonize set1 */
6484 LY_CHECK_ERR_RET(rc = set_comp_canonize(&iter1, set1, &set2->val.nodes[i]), lyxp_set_free_content(&iter2), rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006485
Michal Vasko4c7763f2020-07-27 17:40:37 +02006486 /* compare recursively */
Michal Vasko03ff5a72019-09-11 13:49:33 +02006487 rc = moveto_op_comp(&iter1, &iter2, op, options);
Michal Vaskod3678892020-05-21 10:06:58 +02006488 lyxp_set_free_content(&iter2);
Michal Vasko4c7763f2020-07-27 17:40:37 +02006489 LY_CHECK_ERR_RET(rc, lyxp_set_free_content(&iter1), rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006490
6491 /* lazy evaluation until true */
Michal Vasko004d3152020-06-11 19:59:22 +02006492 if (iter1.val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006493 set_fill_boolean(set1, 1);
6494 return LY_SUCCESS;
6495 }
6496 }
6497 }
6498
6499 /* false for all nodes */
6500 set_fill_boolean(set1, 0);
6501 return LY_SUCCESS;
6502 }
6503
6504 /* first convert properly */
6505 if ((op[0] == '=') || (op[0] == '!')) {
6506 if ((set1->type == LYXP_SET_BOOLEAN) || (set2->type == LYXP_SET_BOOLEAN)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006507 lyxp_set_cast(set1, LYXP_SET_BOOLEAN);
6508 lyxp_set_cast(set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006509 } else if ((set1->type == LYXP_SET_NUMBER) || (set2->type == LYXP_SET_NUMBER)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006510 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006511 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006512 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006513 LY_CHECK_RET(rc);
6514 } /* else we have 2 strings */
6515 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006516 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006517 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006518 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006519 LY_CHECK_RET(rc);
6520 }
6521
6522 assert(set1->type == set2->type);
6523
6524 /* compute result */
6525 if (op[0] == '=') {
6526 if (set1->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02006527 result = (set1->val.bln == set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006528 } else if (set1->type == LYXP_SET_NUMBER) {
6529 result = (set1->val.num == set2->val.num);
6530 } else {
6531 assert(set1->type == LYXP_SET_STRING);
Michal Vaskoac6c72f2019-11-14 16:09:34 +01006532 result = !strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006533 }
6534 } else if (op[0] == '!') {
6535 if (set1->type == LYXP_SET_BOOLEAN) {
Michal Vasko004d3152020-06-11 19:59:22 +02006536 result = (set1->val.bln != set2->val.bln);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006537 } else if (set1->type == LYXP_SET_NUMBER) {
6538 result = (set1->val.num != set2->val.num);
6539 } else {
6540 assert(set1->type == LYXP_SET_STRING);
Michal Vaskoac6c72f2019-11-14 16:09:34 +01006541 result = !strcmp(set1->val.str, set2->val.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006542 }
6543 } else {
6544 assert(set1->type == LYXP_SET_NUMBER);
6545 if (op[0] == '<') {
6546 if (op[1] == '=') {
6547 result = (set1->val.num <= set2->val.num);
6548 } else {
6549 result = (set1->val.num < set2->val.num);
6550 }
6551 } else {
6552 if (op[1] == '=') {
6553 result = (set1->val.num >= set2->val.num);
6554 } else {
6555 result = (set1->val.num > set2->val.num);
6556 }
6557 }
6558 }
6559
6560 /* assign result */
6561 if (result) {
6562 set_fill_boolean(set1, 1);
6563 } else {
6564 set_fill_boolean(set1, 0);
6565 }
6566
6567 return LY_SUCCESS;
6568}
6569
6570/**
6571 * @brief Move context @p set to the result of a basic operation. Handles '+', '-', unary '-', '*', 'div',
6572 * or 'mod'. Result is LYXP_SET_NUMBER. Indirectly context position aware.
6573 *
6574 * @param[in,out] set1 Set to use for the result.
6575 * @param[in] set2 Set acting as the second operand for @p op.
6576 * @param[in] op Operator to process.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006577 * @return LY_ERR
6578 */
6579static LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006580moveto_op_math(struct lyxp_set *set1, struct lyxp_set *set2, const char *op)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006581{
6582 LY_ERR rc;
6583
6584 /* unary '-' */
6585 if (!set2 && (op[0] == '-')) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006586 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006587 LY_CHECK_RET(rc);
6588 set1->val.num *= -1;
6589 lyxp_set_free(set2);
6590 return LY_SUCCESS;
6591 }
6592
6593 assert(set1 && set2);
6594
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006595 rc = lyxp_set_cast(set1, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006596 LY_CHECK_RET(rc);
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006597 rc = lyxp_set_cast(set2, LYXP_SET_NUMBER);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006598 LY_CHECK_RET(rc);
6599
6600 switch (op[0]) {
6601 /* '+' */
6602 case '+':
6603 set1->val.num += set2->val.num;
6604 break;
6605
6606 /* '-' */
6607 case '-':
6608 set1->val.num -= set2->val.num;
6609 break;
6610
6611 /* '*' */
6612 case '*':
6613 set1->val.num *= set2->val.num;
6614 break;
6615
6616 /* 'div' */
6617 case 'd':
6618 set1->val.num /= set2->val.num;
6619 break;
6620
6621 /* 'mod' */
6622 case 'm':
6623 set1->val.num = ((long long)set1->val.num) % ((long long)set2->val.num);
6624 break;
6625
6626 default:
6627 LOGINT_RET(set1->ctx);
6628 }
6629
6630 return LY_SUCCESS;
6631}
6632
6633/*
6634 * eval functions
6635 *
6636 * They execute a parsed XPath expression on some data subtree.
6637 */
6638
6639/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02006640 * @brief Evaluate Predicate. Logs directly on error.
6641 *
Michal Vaskod3678892020-05-21 10:06:58 +02006642 * [9] Predicate ::= '[' Expr ']'
Michal Vasko03ff5a72019-09-11 13:49:33 +02006643 *
6644 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02006645 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02006646 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
6647 * @param[in] options XPath options.
6648 * @param[in] parent_pos_pred Whether parent predicate was a positional one.
6649 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6650 */
6651static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02006652eval_predicate(struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set, uint32_t options, uint8_t parent_pos_pred)
Michal Vasko03ff5a72019-09-11 13:49:33 +02006653{
6654 LY_ERR rc;
Michal Vasko57eab132019-09-24 11:46:26 +02006655 uint16_t i, orig_exp;
Michal Vasko5c4e5892019-11-14 12:31:38 +01006656 uint32_t orig_pos, orig_size;
6657 int32_t pred_in_ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006658 struct lyxp_set set2;
6659 struct lyd_node *orig_parent;
6660
6661 /* '[' */
6662 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02006663 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
6664 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006665
6666 if (!set) {
6667only_parse:
Michal Vasko004d3152020-06-11 19:59:22 +02006668 rc = eval_expr_select(exp, tok_idx, 0, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006669 LY_CHECK_RET(rc);
6670 } else if (set->type == LYXP_SET_NODE_SET) {
6671 /* 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 +01006672 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02006673
6674 /* empty set, nothing to evaluate */
6675 if (!set->used) {
6676 goto only_parse;
6677 }
6678
Michal Vasko004d3152020-06-11 19:59:22 +02006679 orig_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006680 orig_pos = 0;
6681 orig_size = set->used;
6682 orig_parent = NULL;
Michal Vasko39dbf352020-05-21 10:08:59 +02006683 for (i = 0; i < set->used; ++i) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02006684 set_init(&set2, set);
6685 set_insert_node(&set2, set->val.nodes[i].node, set->val.nodes[i].pos, set->val.nodes[i].type, 0);
6686 /* remember the node context position for position() and context size for last(),
6687 * predicates should always be evaluated with respect to the child axis (since we do
6688 * not support explicit axes) so we assign positions based on their parents */
6689 if (parent_pos_pred && ((struct lyd_node *)set->val.nodes[i].node->parent != orig_parent)) {
6690 orig_parent = (struct lyd_node *)set->val.nodes[i].node->parent;
6691 orig_pos = 1;
6692 } else {
6693 ++orig_pos;
6694 }
6695
6696 set2.ctx_pos = orig_pos;
6697 set2.ctx_size = orig_size;
Michal Vasko004d3152020-06-11 19:59:22 +02006698 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006699
Michal Vasko004d3152020-06-11 19:59:22 +02006700 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006701 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006702 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006703 return rc;
6704 }
6705
6706 /* number is a position */
6707 if (set2.type == LYXP_SET_NUMBER) {
6708 if ((long long)set2.val.num == orig_pos) {
6709 set2.val.num = 1;
6710 } else {
6711 set2.val.num = 0;
6712 }
6713 }
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006714 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006715
6716 /* predicate satisfied or not? */
Michal Vasko004d3152020-06-11 19:59:22 +02006717 if (!set2.val.bln) {
Michal Vasko2caefc12019-11-14 16:07:56 +01006718 set_remove_node_none(set, i);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006719 }
6720 }
Michal Vasko2caefc12019-11-14 16:07:56 +01006721 set_remove_nodes_none(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006722
6723 } else if (set->type == LYXP_SET_SCNODE_SET) {
6724 for (i = 0; i < set->used; ++i) {
6725 if (set->val.scnodes[i].in_ctx == 1) {
6726 /* there is a currently-valid node */
6727 break;
6728 }
6729 }
6730 /* empty set, nothing to evaluate */
6731 if (i == set->used) {
6732 goto only_parse;
6733 }
6734
Michal Vasko004d3152020-06-11 19:59:22 +02006735 orig_exp = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006736
Michal Vasko03ff5a72019-09-11 13:49:33 +02006737 /* set special in_ctx to all the valid snodes */
6738 pred_in_ctx = set_scnode_new_in_ctx(set);
6739
6740 /* use the valid snodes one-by-one */
6741 for (i = 0; i < set->used; ++i) {
6742 if (set->val.scnodes[i].in_ctx != pred_in_ctx) {
6743 continue;
6744 }
6745 set->val.scnodes[i].in_ctx = 1;
6746
Michal Vasko004d3152020-06-11 19:59:22 +02006747 *tok_idx = orig_exp;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006748
Michal Vasko004d3152020-06-11 19:59:22 +02006749 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006750 LY_CHECK_RET(rc);
6751
6752 set->val.scnodes[i].in_ctx = pred_in_ctx;
6753 }
6754
6755 /* restore the state as it was before the predicate */
6756 for (i = 0; i < set->used; ++i) {
6757 if (set->val.scnodes[i].in_ctx == 1) {
6758 set->val.scnodes[i].in_ctx = 0;
6759 } else if (set->val.scnodes[i].in_ctx == pred_in_ctx) {
6760 set->val.scnodes[i].in_ctx = 1;
6761 }
6762 }
6763
6764 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02006765 set2.type = LYXP_SET_NODE_SET;
Michal Vasko03ff5a72019-09-11 13:49:33 +02006766 set_fill_set(&set2, set);
6767
Michal Vasko004d3152020-06-11 19:59:22 +02006768 rc = eval_expr_select(exp, tok_idx, 0, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006769 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02006770 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006771 return rc;
6772 }
6773
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01006774 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko004d3152020-06-11 19:59:22 +02006775 if (!set2.val.bln) {
Michal Vaskod3678892020-05-21 10:06:58 +02006776 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006777 }
Michal Vaskod3678892020-05-21 10:06:58 +02006778 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006779 }
6780
6781 /* ']' */
Michal Vasko004d3152020-06-11 19:59:22 +02006782 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_BRACK2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006783 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02006784 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
6785 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02006786
6787 return LY_SUCCESS;
6788}
6789
6790/**
Michal Vaskod3678892020-05-21 10:06:58 +02006791 * @brief Evaluate Literal. Logs directly on error.
6792 *
6793 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02006794 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02006795 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
6796 */
6797static void
Michal Vasko004d3152020-06-11 19:59:22 +02006798eval_literal(struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set)
Michal Vaskod3678892020-05-21 10:06:58 +02006799{
6800 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02006801 if (exp->tok_len[*tok_idx] == 2) {
Michal Vaskod3678892020-05-21 10:06:58 +02006802 set_fill_string(set, "", 0);
6803 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02006804 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 +02006805 }
6806 }
6807 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02006808 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
6809 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02006810}
6811
6812/**
Michal Vasko004d3152020-06-11 19:59:22 +02006813 * @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 +02006814 *
Michal Vasko004d3152020-06-11 19:59:22 +02006815 * @param[in] exp Full parsed XPath expression.
6816 * @param[in,out] tok_idx Index in @p exp at the beginning of the predicate, is updated on success.
6817 * @param[in] scnode Found schema node as context for the predicate.
6818 * @param[in] format Format of any prefixes in key names/values.
6819 * @param[out] predicates Parsed predicates.
6820 * @param[out] pred_type Type of @p predicates.
6821 * @return LY_SUCCESS on success,
6822 * @return LY_ERR on any error.
Michal Vaskod3678892020-05-21 10:06:58 +02006823 */
6824static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02006825eval_name_test_try_compile_predicates(struct lyxp_expr *exp, uint16_t *tok_idx, const struct lysc_node *scnode,
Radek Krejci0f969882020-08-21 16:56:47 +02006826 LY_PREFIX_FORMAT format, struct ly_path_predicate **predicates,
6827 enum ly_path_pred_type *pred_type)
Michal Vaskod3678892020-05-21 10:06:58 +02006828{
Michal Vasko004d3152020-06-11 19:59:22 +02006829 LY_ERR ret = LY_SUCCESS;
6830 uint16_t key_count, e_idx, pred_idx = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02006831 const struct lysc_node *key;
Michal Vasko004d3152020-06-11 19:59:22 +02006832 size_t pred_len;
Radek Krejci1deb5be2020-08-26 16:43:36 +02006833 uint32_t prev_lo;
Michal Vasko004d3152020-06-11 19:59:22 +02006834 struct lyxp_expr *exp2 = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02006835
Michal Vasko004d3152020-06-11 19:59:22 +02006836 assert(scnode->nodetype & (LYS_LIST | LYS_LEAFLIST));
Michal Vaskod3678892020-05-21 10:06:58 +02006837
Michal Vasko004d3152020-06-11 19:59:22 +02006838 if (scnode->nodetype == LYS_LIST) {
6839 /* get key count */
6840 if (scnode->flags & LYS_KEYLESS) {
6841 return LY_EINVAL;
6842 }
Radek Krejci1e008d22020-08-17 11:37:37 +02006843 for (key_count = 0, key = lysc_node_children(scnode, 0); key && (key->flags & LYS_KEY); key = key->next, ++key_count) {}
Michal Vasko004d3152020-06-11 19:59:22 +02006844 assert(key_count);
Michal Vaskod3678892020-05-21 10:06:58 +02006845
Michal Vasko004d3152020-06-11 19:59:22 +02006846 /* learn where the predicates end */
6847 e_idx = *tok_idx;
6848 while (key_count) {
6849 /* '[' */
6850 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
6851 return LY_EINVAL;
6852 }
6853 ++e_idx;
6854
6855 /* ']' */
6856 while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
6857 ++e_idx;
6858 }
6859 ++e_idx;
6860
6861 /* another presumably key predicate parsed */
6862 --key_count;
6863 }
Michal Vasko004d3152020-06-11 19:59:22 +02006864 } else {
6865 /* learn just where this single predicate ends */
6866 e_idx = *tok_idx;
6867
Michal Vaskod3678892020-05-21 10:06:58 +02006868 /* '[' */
Michal Vasko004d3152020-06-11 19:59:22 +02006869 if (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK1)) {
6870 return LY_EINVAL;
6871 }
6872 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02006873
6874 /* ']' */
Michal Vasko004d3152020-06-11 19:59:22 +02006875 while (lyxp_check_token(NULL, exp, e_idx, LYXP_TOKEN_BRACK2)) {
6876 ++e_idx;
6877 }
6878 ++e_idx;
Michal Vaskod3678892020-05-21 10:06:58 +02006879 }
6880
Michal Vasko004d3152020-06-11 19:59:22 +02006881 /* get the joined length of all the keys predicates/of the single leaf-list predicate */
6882 pred_len = (exp->tok_pos[e_idx - 1] + exp->tok_len[e_idx - 1]) - exp->tok_pos[*tok_idx];
6883
6884 /* turn logging off */
6885 prev_lo = ly_log_options(0);
6886
6887 /* parse the predicate(s) */
Michal Vasko6b26e742020-07-17 15:02:10 +02006888 LY_CHECK_GOTO(ret = ly_path_parse_predicate(scnode->module->ctx, scnode, exp->expr + exp->tok_pos[*tok_idx], pred_len,
Michal Vasko004d3152020-06-11 19:59:22 +02006889 LY_PATH_PREFIX_OPTIONAL, LY_PATH_PRED_SIMPLE, &exp2), cleanup);
6890
6891 /* compile */
Michal Vaskoc8a230d2020-08-14 12:17:10 +02006892 ret = ly_path_compile_predicate(scnode->module->ctx, scnode, scnode->module, scnode, exp2, &pred_idx,
6893 format, scnode->module, predicates, pred_type);
Michal Vasko004d3152020-06-11 19:59:22 +02006894 LY_CHECK_GOTO(ret, cleanup);
6895
6896 /* success, the predicate must include all the needed information for hash-based search */
6897 *tok_idx = e_idx;
6898
6899cleanup:
6900 ly_log_options(prev_lo);
6901 lyxp_expr_free(scnode->module->ctx, exp2);
6902 return ret;
Michal Vaskod3678892020-05-21 10:06:58 +02006903}
6904
6905/**
6906 * @brief Evaluate NameTest and any following Predicates. Logs directly on error.
6907 *
6908 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
6909 * [6] NodeTest ::= NameTest | NodeType '(' ')'
6910 * [7] NameTest ::= '*' | NCName ':' '*' | QName
6911 *
6912 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02006913 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02006914 * @param[in] attr_axis Whether to search attributes or standard nodes.
6915 * @param[in] all_desc Whether to search all the descendants or children only.
6916 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
6917 * @param[in] options XPath options.
6918 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
6919 */
6920static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02006921eval_name_test_with_predicate(struct lyxp_expr *exp, uint16_t *tok_idx, uint8_t attr_axis, uint8_t all_desc, struct lyxp_set *set,
6922 uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02006923{
Michal Vaskod3678892020-05-21 10:06:58 +02006924 char *path;
Michal Vasko004d3152020-06-11 19:59:22 +02006925 const char *ncname, *ncname_dict = NULL;
6926 uint16_t ncname_len;
Michal Vaskod3678892020-05-21 10:06:58 +02006927 const struct lys_module *moveto_mod;
6928 const struct lysc_node *scnode = NULL, *tmp;
Michal Vasko004d3152020-06-11 19:59:22 +02006929 struct ly_path_predicate *predicates = NULL;
6930 enum ly_path_pred_type pred_type = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02006931 LY_ERR rc = LY_SUCCESS;
6932
6933 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02006934 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
6935 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02006936
6937 if (!set) {
6938 goto moveto;
6939 }
6940
Michal Vasko004d3152020-06-11 19:59:22 +02006941 ncname = &exp->expr[exp->tok_pos[*tok_idx - 1]];
6942 ncname_len = exp->tok_len[*tok_idx - 1];
Michal Vaskod3678892020-05-21 10:06:58 +02006943
6944 /* parse (and skip) module name */
6945 rc = moveto_resolve_model(&ncname, &ncname_len, set, &moveto_mod);
6946 LY_CHECK_GOTO(rc, cleanup);
6947
6948 if (moveto_mod && !attr_axis && !all_desc && (set->type == LYXP_SET_NODE_SET)) {
6949 /* find the matching schema node in some parent in the context */
Radek Krejci1deb5be2020-08-26 16:43:36 +02006950 for (uint32_t i = 0; i < set->used; ++i) {
Michal Vaskod3678892020-05-21 10:06:58 +02006951 if (set->val.nodes[i].type == set->root_type) {
6952 tmp = lys_find_child(NULL, moveto_mod, ncname, ncname_len, 0, 0);
6953 } else if ((set->val.nodes[i].type == LYXP_NODE_ELEM)
6954 && (!scnode || (lysc_data_parent(scnode) != set->val.nodes[i].node->schema))) {
6955 /* do not repeat the same search */
6956 tmp = lys_find_child(set->val.nodes[i].node->schema, moveto_mod, ncname, ncname_len, 0, 0);
Radek Krejcib1247842020-07-02 16:22:38 +02006957 } else {
6958 tmp = NULL;
Michal Vaskod3678892020-05-21 10:06:58 +02006959 }
6960
6961 /* additional context check */
6962 if (tmp && (set->root_type == LYXP_NODE_ROOT_CONFIG) && (tmp->flags & LYS_CONFIG_R)) {
6963 tmp = NULL;
6964 }
6965
6966 if (tmp) {
6967 if (scnode) {
6968 /* we found a schema node with the same name but at different level, give up, too complicated */
6969 scnode = NULL;
6970 break;
6971 } else {
6972 /* remember the found schema node and continue to make sure it can be used */
6973 scnode = tmp;
6974 }
Michal Vaskod3678892020-05-21 10:06:58 +02006975 }
6976 }
6977
Michal Vasko004d3152020-06-11 19:59:22 +02006978 if (scnode && (scnode->nodetype & (LYS_LIST | LYS_LEAFLIST))) {
6979 /* try to create the predicates */
6980 if (eval_name_test_try_compile_predicates(exp, tok_idx, scnode, set->format, &predicates, &pred_type)) {
6981 /* hashes cannot be used */
Michal Vaskod3678892020-05-21 10:06:58 +02006982 scnode = NULL;
6983 }
6984 }
6985 }
6986
Michal Vasko004d3152020-06-11 19:59:22 +02006987 if (!scnode && moveto_mod) {
6988 /* we are not able to match based on a schema node and not all the modules match,
6989 * use dictionary for efficient comparison */
6990 ncname_dict = lydict_insert(set->ctx, ncname, ncname_len);
Michal Vaskod3678892020-05-21 10:06:58 +02006991 }
6992
6993moveto:
6994 /* move to the attribute(s), data node(s), or schema node(s) */
6995 if (attr_axis) {
6996 if (set && (options & LYXP_SCNODE_ALL)) {
6997 set_scnode_clear_ctx(set);
6998 } else {
6999 if (all_desc) {
Michal Vasko004d3152020-06-11 19:59:22 +02007000 rc = moveto_attr_alldesc(set, moveto_mod, ncname_dict);
Michal Vaskod3678892020-05-21 10:06:58 +02007001 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007002 rc = moveto_attr(set, moveto_mod, ncname_dict);
Michal Vaskod3678892020-05-21 10:06:58 +02007003 }
7004 LY_CHECK_GOTO(rc, cleanup);
7005 }
7006 } else {
7007 if (set && (options & LYXP_SCNODE_ALL)) {
Radek Krejci1deb5be2020-08-26 16:43:36 +02007008 int64_t i;
7009
Michal Vaskod3678892020-05-21 10:06:58 +02007010 if (all_desc) {
Michal Vasko004d3152020-06-11 19:59:22 +02007011 rc = moveto_scnode_alldesc(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007012 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007013 rc = moveto_scnode(set, moveto_mod, ncname_dict, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007014 }
7015 LY_CHECK_GOTO(rc, cleanup);
7016
7017 for (i = set->used - 1; i > -1; --i) {
7018 if (set->val.scnodes[i].in_ctx > 0) {
7019 break;
7020 }
7021 }
7022 if (i == -1) {
7023 path = lysc_path(set->ctx_scnode, LYSC_PATH_LOG, NULL, 0);
7024 LOGWRN(set->ctx, "Schema node \"%.*s\" not found (%.*s) with context node \"%s\".",
Michal Vasko6b26e742020-07-17 15:02:10 +02007025 ncname_len, ncname, ncname - exp->expr, exp->expr, path);
Michal Vaskod3678892020-05-21 10:06:58 +02007026 free(path);
7027 }
7028 } else {
7029 if (all_desc) {
Michal Vasko004d3152020-06-11 19:59:22 +02007030 rc = moveto_node_alldesc(set, moveto_mod, ncname_dict);
Michal Vaskod3678892020-05-21 10:06:58 +02007031 } else {
7032 if (scnode) {
7033 /* we can find the nodes using hashes */
Michal Vasko004d3152020-06-11 19:59:22 +02007034 rc = moveto_node_hash(set, scnode, predicates);
Michal Vaskod3678892020-05-21 10:06:58 +02007035 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007036 rc = moveto_node(set, moveto_mod, ncname_dict);
Michal Vaskod3678892020-05-21 10:06:58 +02007037 }
7038 }
7039 LY_CHECK_GOTO(rc, cleanup);
7040 }
7041 }
7042
7043 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007044 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7045 rc = eval_predicate(exp, tok_idx, set, options, 1);
Michal Vaskod3678892020-05-21 10:06:58 +02007046 LY_CHECK_RET(rc);
7047 }
7048
7049cleanup:
Michal Vaskodb51a8d2020-05-27 15:22:29 +02007050 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007051 lydict_remove(set->ctx, ncname_dict);
7052 ly_path_predicates_free(set->ctx, pred_type, scnode, predicates);
Michal Vaskodb51a8d2020-05-27 15:22:29 +02007053 }
Michal Vaskod3678892020-05-21 10:06:58 +02007054 return rc;
7055}
7056
7057/**
7058 * @brief Evaluate NodeType and any following Predicates. Logs directly on error.
7059 *
7060 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
7061 * [6] NodeTest ::= NameTest | NodeType '(' ')'
7062 * [8] NodeType ::= 'text' | 'node'
7063 *
7064 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007065 * @param[in] tok_idx Position in the expression @p exp.
Michal Vaskod3678892020-05-21 10:06:58 +02007066 * @param[in] attr_axis Whether to search attributes or standard nodes.
7067 * @param[in] all_desc Whether to search all the descendants or children only.
7068 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7069 * @param[in] options XPath options.
7070 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7071 */
7072static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02007073eval_node_type_with_predicate(struct lyxp_expr *exp, uint16_t *tok_idx, uint8_t attr_axis, uint8_t all_desc,
7074 struct lyxp_set *set, uint32_t options)
Michal Vaskod3678892020-05-21 10:06:58 +02007075{
7076 LY_ERR rc;
7077
7078 /* TODO */
7079 (void)attr_axis;
7080 (void)all_desc;
7081
7082 if (set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007083 assert(exp->tok_len[*tok_idx] == 4);
Michal Vaskod3678892020-05-21 10:06:58 +02007084 if (set->type == LYXP_SET_SCNODE_SET) {
7085 set_scnode_clear_ctx(set);
7086 /* just for the debug message below */
7087 set = NULL;
7088 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007089 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "node", 4)) {
Michal Vaskod3678892020-05-21 10:06:58 +02007090 rc = xpath_node(NULL, 0, set, options);
7091 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007092 assert(!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "text", 4));
Michal Vaskod3678892020-05-21 10:06:58 +02007093 rc = xpath_text(NULL, 0, set, options);
7094 }
7095 LY_CHECK_RET(rc);
7096 }
7097 }
7098 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007099 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7100 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007101
7102 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02007103 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
Michal Vaskod3678892020-05-21 10:06:58 +02007104 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007105 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7106 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007107
7108 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007109 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
Michal Vaskod3678892020-05-21 10:06:58 +02007110 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007111 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7112 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007113
7114 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007115 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7116 rc = eval_predicate(exp, tok_idx, set, options, 1);
Michal Vaskod3678892020-05-21 10:06:58 +02007117 LY_CHECK_RET(rc);
7118 }
7119
7120 return LY_SUCCESS;
7121}
7122
7123/**
Michal Vasko03ff5a72019-09-11 13:49:33 +02007124 * @brief Evaluate RelativeLocationPath. Logs directly on error.
7125 *
7126 * [4] RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step
7127 * [5] Step ::= '@'? NodeTest Predicate* | '.' | '..'
Michal Vaskod3678892020-05-21 10:06:58 +02007128 * [6] NodeTest ::= NameTest | NodeType '(' ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007129 *
7130 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007131 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007132 * @param[in] all_desc Whether to search all the descendants or children only.
7133 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7134 * @param[in] options XPath options.
7135 * @return LY_ERR (YL_EINCOMPLETE on unresolved when)
7136 */
7137static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02007138eval_relative_location_path(struct lyxp_expr *exp, uint16_t *tok_idx, uint8_t all_desc, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007139{
Radek Krejci1deb5be2020-08-26 16:43:36 +02007140 uint8_t attr_axis;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007141 LY_ERR rc;
7142
7143 goto step;
7144 do {
7145 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02007146 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007147 all_desc = 0;
7148 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007149 assert(exp->tok_len[*tok_idx] == 2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007150 all_desc = 1;
7151 }
7152 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007153 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7154 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007155
7156step:
Michal Vaskod3678892020-05-21 10:06:58 +02007157 /* evaluate abbreviated axis '@'? if any */
Michal Vasko004d3152020-06-11 19:59:22 +02007158 if (exp->tokens[*tok_idx] == LYXP_TOKEN_AT) {
Michal Vaskod3678892020-05-21 10:06:58 +02007159 attr_axis = 1;
7160
7161 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007162 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7163 ++(*tok_idx);
Michal Vaskod3678892020-05-21 10:06:58 +02007164 } else {
7165 attr_axis = 0;
7166 }
7167
Michal Vasko03ff5a72019-09-11 13:49:33 +02007168 /* Step */
Michal Vasko004d3152020-06-11 19:59:22 +02007169 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007170 case LYXP_TOKEN_DOT:
7171 /* evaluate '.' */
7172 if (set && (options & LYXP_SCNODE_ALL)) {
7173 rc = moveto_scnode_self(set, all_desc, options);
7174 } else {
7175 rc = moveto_self(set, all_desc, options);
7176 }
7177 LY_CHECK_RET(rc);
7178 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007179 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7180 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007181 break;
7182
7183 case LYXP_TOKEN_DDOT:
7184 /* evaluate '..' */
7185 if (set && (options & LYXP_SCNODE_ALL)) {
7186 rc = moveto_scnode_parent(set, all_desc, options);
7187 } else {
7188 rc = moveto_parent(set, all_desc, options);
7189 }
7190 LY_CHECK_RET(rc);
7191 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007192 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7193 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007194 break;
7195
Michal Vasko03ff5a72019-09-11 13:49:33 +02007196 case LYXP_TOKEN_NAMETEST:
Michal Vaskod3678892020-05-21 10:06:58 +02007197 /* evaluate NameTest Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007198 rc = eval_name_test_with_predicate(exp, tok_idx, attr_axis, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007199 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02007200 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007201
Michal Vaskod3678892020-05-21 10:06:58 +02007202 case LYXP_TOKEN_NODETYPE:
7203 /* evaluate NodeType Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007204 rc = eval_node_type_with_predicate(exp, tok_idx, attr_axis, all_desc, set, options);
Michal Vaskod3678892020-05-21 10:06:58 +02007205 LY_CHECK_RET(rc);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007206 break;
7207
7208 default:
Michal Vasko02a77382019-09-12 11:47:35 +02007209 LOGINT_RET(set ? set->ctx : NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007210 }
Michal Vasko004d3152020-06-11 19:59:22 +02007211 } while (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007212
7213 return LY_SUCCESS;
7214}
7215
7216/**
7217 * @brief Evaluate AbsoluteLocationPath. Logs directly on error.
7218 *
7219 * [3] AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath
7220 *
7221 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007222 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007223 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7224 * @param[in] options XPath options.
7225 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7226 */
7227static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02007228eval_absolute_location_path(struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007229{
Radek Krejci1deb5be2020-08-26 16:43:36 +02007230 uint8_t all_desc;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007231 LY_ERR rc;
7232
7233 if (set) {
7234 /* no matter what tokens follow, we need to be at the root */
7235 moveto_root(set, options);
7236 }
7237
7238 /* '/' RelativeLocationPath? */
Michal Vasko004d3152020-06-11 19:59:22 +02007239 if (exp->tok_len[*tok_idx] == 1) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007240 /* evaluate '/' - deferred */
7241 all_desc = 0;
7242 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007243 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7244 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007245
Michal Vasko004d3152020-06-11 19:59:22 +02007246 if (lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_NONE)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007247 return LY_SUCCESS;
7248 }
Michal Vasko004d3152020-06-11 19:59:22 +02007249 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007250 case LYXP_TOKEN_DOT:
7251 case LYXP_TOKEN_DDOT:
7252 case LYXP_TOKEN_AT:
7253 case LYXP_TOKEN_NAMETEST:
7254 case LYXP_TOKEN_NODETYPE:
Michal Vasko004d3152020-06-11 19:59:22 +02007255 rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007256 LY_CHECK_RET(rc);
7257 break;
7258 default:
7259 break;
7260 }
7261
Michal Vasko03ff5a72019-09-11 13:49:33 +02007262 } else {
Radek Krejcif6a11002020-08-21 13:29:07 +02007263 /* '//' RelativeLocationPath */
Michal Vasko03ff5a72019-09-11 13:49:33 +02007264 /* evaluate '//' - deferred so as not to waste memory by remembering all the nodes */
7265 all_desc = 1;
7266 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007267 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7268 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007269
Michal Vasko004d3152020-06-11 19:59:22 +02007270 rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007271 LY_CHECK_RET(rc);
7272 }
7273
7274 return LY_SUCCESS;
7275}
7276
7277/**
7278 * @brief Evaluate FunctionCall. Logs directly on error.
7279 *
Michal Vaskod3678892020-05-21 10:06:58 +02007280 * [11] FunctionCall ::= FunctionName '(' ( Expr ( ',' Expr )* )? ')'
Michal Vasko03ff5a72019-09-11 13:49:33 +02007281 *
7282 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007283 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007284 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7285 * @param[in] options XPath options.
7286 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7287 */
7288static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02007289eval_function_call(struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007290{
7291 LY_ERR rc;
Radek Krejci1deb5be2020-08-26 16:43:36 +02007292 LY_ERR (*xpath_func)(struct lyxp_set **, uint16_t, struct lyxp_set *, uint32_t) = NULL;
Michal Vasko0cbf54f2019-12-16 10:01:06 +01007293 uint16_t arg_count = 0, i;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007294 struct lyxp_set **args = NULL, **args_aux;
7295
7296 if (set) {
7297 /* FunctionName */
Michal Vasko004d3152020-06-11 19:59:22 +02007298 switch (exp->tok_len[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007299 case 3:
Michal Vasko004d3152020-06-11 19:59:22 +02007300 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "not", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007301 xpath_func = &xpath_not;
Michal Vasko004d3152020-06-11 19:59:22 +02007302 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "sum", 3)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007303 xpath_func = &xpath_sum;
7304 }
7305 break;
7306 case 4:
Michal Vasko004d3152020-06-11 19:59:22 +02007307 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "lang", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007308 xpath_func = &xpath_lang;
Michal Vasko004d3152020-06-11 19:59:22 +02007309 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "last", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007310 xpath_func = &xpath_last;
Michal Vasko004d3152020-06-11 19:59:22 +02007311 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "name", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007312 xpath_func = &xpath_name;
Michal Vasko004d3152020-06-11 19:59:22 +02007313 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "true", 4)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007314 xpath_func = &xpath_true;
7315 }
7316 break;
7317 case 5:
Michal Vasko004d3152020-06-11 19:59:22 +02007318 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "count", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007319 xpath_func = &xpath_count;
Michal Vasko004d3152020-06-11 19:59:22 +02007320 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "false", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007321 xpath_func = &xpath_false;
Michal Vasko004d3152020-06-11 19:59:22 +02007322 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "floor", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007323 xpath_func = &xpath_floor;
Michal Vasko004d3152020-06-11 19:59:22 +02007324 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "round", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007325 xpath_func = &xpath_round;
Michal Vasko004d3152020-06-11 19:59:22 +02007326 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "deref", 5)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007327 xpath_func = &xpath_deref;
7328 }
7329 break;
7330 case 6:
Michal Vasko004d3152020-06-11 19:59:22 +02007331 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "concat", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007332 xpath_func = &xpath_concat;
Michal Vasko004d3152020-06-11 19:59:22 +02007333 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "number", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007334 xpath_func = &xpath_number;
Michal Vasko004d3152020-06-11 19:59:22 +02007335 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string", 6)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007336 xpath_func = &xpath_string;
7337 }
7338 break;
7339 case 7:
Michal Vasko004d3152020-06-11 19:59:22 +02007340 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "boolean", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007341 xpath_func = &xpath_boolean;
Michal Vasko004d3152020-06-11 19:59:22 +02007342 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "ceiling", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007343 xpath_func = &xpath_ceiling;
Michal Vasko004d3152020-06-11 19:59:22 +02007344 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "current", 7)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007345 xpath_func = &xpath_current;
7346 }
7347 break;
7348 case 8:
Michal Vasko004d3152020-06-11 19:59:22 +02007349 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "contains", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007350 xpath_func = &xpath_contains;
Michal Vasko004d3152020-06-11 19:59:22 +02007351 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "position", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007352 xpath_func = &xpath_position;
Michal Vasko004d3152020-06-11 19:59:22 +02007353 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "re-match", 8)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007354 xpath_func = &xpath_re_match;
7355 }
7356 break;
7357 case 9:
Michal Vasko004d3152020-06-11 19:59:22 +02007358 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007359 xpath_func = &xpath_substring;
Michal Vasko004d3152020-06-11 19:59:22 +02007360 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "translate", 9)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007361 xpath_func = &xpath_translate;
7362 }
7363 break;
7364 case 10:
Michal Vasko004d3152020-06-11 19:59:22 +02007365 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "local-name", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007366 xpath_func = &xpath_local_name;
Michal Vasko004d3152020-06-11 19:59:22 +02007367 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "enum-value", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007368 xpath_func = &xpath_enum_value;
Michal Vasko004d3152020-06-11 19:59:22 +02007369 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "bit-is-set", 10)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007370 xpath_func = &xpath_bit_is_set;
7371 }
7372 break;
7373 case 11:
Michal Vasko004d3152020-06-11 19:59:22 +02007374 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "starts-with", 11)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007375 xpath_func = &xpath_starts_with;
7376 }
7377 break;
7378 case 12:
Michal Vasko004d3152020-06-11 19:59:22 +02007379 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from", 12)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007380 xpath_func = &xpath_derived_from;
7381 }
7382 break;
7383 case 13:
Michal Vasko004d3152020-06-11 19:59:22 +02007384 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "namespace-uri", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007385 xpath_func = &xpath_namespace_uri;
Michal Vasko004d3152020-06-11 19:59:22 +02007386 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "string-length", 13)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007387 xpath_func = &xpath_string_length;
7388 }
7389 break;
7390 case 15:
Michal Vasko004d3152020-06-11 19:59:22 +02007391 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "normalize-space", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007392 xpath_func = &xpath_normalize_space;
Michal Vasko004d3152020-06-11 19:59:22 +02007393 } else if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-after", 15)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007394 xpath_func = &xpath_substring_after;
7395 }
7396 break;
7397 case 16:
Michal Vasko004d3152020-06-11 19:59:22 +02007398 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "substring-before", 16)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007399 xpath_func = &xpath_substring_before;
7400 }
7401 break;
7402 case 20:
Michal Vasko004d3152020-06-11 19:59:22 +02007403 if (!strncmp(&exp->expr[exp->tok_pos[*tok_idx]], "derived-from-or-self", 20)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007404 xpath_func = &xpath_derived_from_or_self;
7405 }
7406 break;
7407 }
7408
7409 if (!xpath_func) {
Michal Vasko004d3152020-06-11 19:59:22 +02007410 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 +02007411 return LY_EVALID;
7412 }
7413 }
7414
7415 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007416 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7417 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007418
7419 /* '(' */
Michal Vasko004d3152020-06-11 19:59:22 +02007420 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007421 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007422 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7423 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007424
7425 /* ( Expr ( ',' Expr )* )? */
Michal Vasko004d3152020-06-11 19:59:22 +02007426 if (exp->tokens[*tok_idx] != LYXP_TOKEN_PAR2) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007427 if (set) {
7428 args = malloc(sizeof *args);
7429 LY_CHECK_ERR_GOTO(!args, LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7430 arg_count = 1;
7431 args[0] = set_copy(set);
7432 if (!args[0]) {
7433 rc = LY_EMEM;
7434 goto cleanup;
7435 }
7436
Michal Vasko004d3152020-06-11 19:59:22 +02007437 rc = eval_expr_select(exp, tok_idx, 0, args[0], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007438 LY_CHECK_GOTO(rc, cleanup);
7439 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007440 rc = eval_expr_select(exp, tok_idx, 0, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007441 LY_CHECK_GOTO(rc, cleanup);
7442 }
7443 }
Michal Vasko004d3152020-06-11 19:59:22 +02007444 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_COMMA)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007445 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007446 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7447 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007448
7449 if (set) {
7450 ++arg_count;
7451 args_aux = realloc(args, arg_count * sizeof *args);
7452 LY_CHECK_ERR_GOTO(!args_aux, arg_count--; LOGMEM(set->ctx); rc = LY_EMEM, cleanup);
7453 args = args_aux;
7454 args[arg_count - 1] = set_copy(set);
7455 if (!args[arg_count - 1]) {
7456 rc = LY_EMEM;
7457 goto cleanup;
7458 }
7459
Michal Vasko004d3152020-06-11 19:59:22 +02007460 rc = eval_expr_select(exp, tok_idx, 0, args[arg_count - 1], options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007461 LY_CHECK_GOTO(rc, cleanup);
7462 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007463 rc = eval_expr_select(exp, tok_idx, 0, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007464 LY_CHECK_GOTO(rc, cleanup);
7465 }
7466 }
7467
7468 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007469 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007470 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007471 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7472 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007473
7474 if (set) {
7475 /* evaluate function */
7476 rc = xpath_func(args, arg_count, set, options);
7477
7478 if (options & LYXP_SCNODE_ALL) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007479 /* merge all nodes from arg evaluations */
7480 for (i = 0; i < arg_count; ++i) {
7481 set_scnode_clear_ctx(args[i]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007482 lyxp_set_scnode_merge(set, args[i]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007483 }
7484 }
7485 } else {
7486 rc = LY_SUCCESS;
7487 }
7488
7489cleanup:
7490 for (i = 0; i < arg_count; ++i) {
7491 lyxp_set_free(args[i]);
7492 }
7493 free(args);
7494
7495 return rc;
7496}
7497
7498/**
7499 * @brief Evaluate Number. Logs directly on error.
7500 *
7501 * @param[in] ctx Context for errors.
7502 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007503 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007504 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7505 * @return LY_ERR
7506 */
7507static LY_ERR
Michal Vasko004d3152020-06-11 19:59:22 +02007508eval_number(struct ly_ctx *ctx, struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007509{
7510 long double num;
7511 char *endptr;
7512
7513 if (set) {
7514 errno = 0;
Michal Vasko004d3152020-06-11 19:59:22 +02007515 num = strtold(&exp->expr[exp->tok_pos[*tok_idx]], &endptr);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007516 if (errno) {
Michal Vasko004d3152020-06-11 19:59:22 +02007517 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 +02007518 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 +02007519 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]], strerror(errno));
Michal Vasko03ff5a72019-09-11 13:49:33 +02007520 return LY_EVALID;
Michal Vasko004d3152020-06-11 19:59:22 +02007521 } else if (endptr - &exp->expr[exp->tok_pos[*tok_idx]] != exp->tok_len[*tok_idx]) {
7522 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 +02007523 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 +02007524 exp->tok_len[*tok_idx], &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007525 return LY_EVALID;
7526 }
7527
7528 set_fill_number(set, num);
7529 }
7530
7531 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007532 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7533 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007534 return LY_SUCCESS;
7535}
7536
7537/**
7538 * @brief Evaluate PathExpr. Logs directly on error.
7539 *
Michal Vaskod3678892020-05-21 10:06:58 +02007540 * [12] PathExpr ::= LocationPath | PrimaryExpr Predicate*
Michal Vasko03ff5a72019-09-11 13:49:33 +02007541 * | PrimaryExpr Predicate* '/' RelativeLocationPath
7542 * | PrimaryExpr Predicate* '//' RelativeLocationPath
7543 * [2] LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
Michal Vaskod3678892020-05-21 10:06:58 +02007544 * [10] PrimaryExpr ::= '(' Expr ')' | Literal | Number | FunctionCall
Michal Vasko03ff5a72019-09-11 13:49:33 +02007545 *
7546 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007547 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007548 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7549 * @param[in] options XPath options.
7550 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7551 */
7552static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02007553eval_path_expr(struct lyxp_expr *exp, uint16_t *tok_idx, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007554{
Radek Krejci1deb5be2020-08-26 16:43:36 +02007555 uint8_t all_desc, parent_pos_pred;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007556 LY_ERR rc;
7557
Michal Vasko004d3152020-06-11 19:59:22 +02007558 switch (exp->tokens[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007559 case LYXP_TOKEN_PAR1:
7560 /* '(' Expr ')' */
7561
7562 /* '(' */
7563 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007564 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7565 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007566
7567 /* Expr */
Michal Vasko004d3152020-06-11 19:59:22 +02007568 rc = eval_expr_select(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007569 LY_CHECK_RET(rc);
7570
7571 /* ')' */
Michal Vasko004d3152020-06-11 19:59:22 +02007572 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_PAR2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007573 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007574 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7575 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007576
7577 parent_pos_pred = 0;
7578 goto predicate;
7579
7580 case LYXP_TOKEN_DOT:
7581 case LYXP_TOKEN_DDOT:
7582 case LYXP_TOKEN_AT:
7583 case LYXP_TOKEN_NAMETEST:
7584 case LYXP_TOKEN_NODETYPE:
7585 /* RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007586 rc = eval_relative_location_path(exp, tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007587 LY_CHECK_RET(rc);
7588 break;
7589
7590 case LYXP_TOKEN_FUNCNAME:
7591 /* FunctionCall */
7592 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007593 rc = eval_function_call(exp, tok_idx, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007594 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007595 rc = eval_function_call(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007596 }
7597 LY_CHECK_RET(rc);
7598
7599 parent_pos_pred = 1;
7600 goto predicate;
7601
Michal Vasko3e48bf32020-06-01 08:39:07 +02007602 case LYXP_TOKEN_OPER_PATH:
7603 case LYXP_TOKEN_OPER_RPATH:
Michal Vasko03ff5a72019-09-11 13:49:33 +02007604 /* AbsoluteLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007605 rc = eval_absolute_location_path(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007606 LY_CHECK_RET(rc);
7607 break;
7608
7609 case LYXP_TOKEN_LITERAL:
7610 /* Literal */
7611 if (!set || (options & LYXP_SCNODE_ALL)) {
7612 if (set) {
7613 set_scnode_clear_ctx(set);
7614 }
Michal Vasko004d3152020-06-11 19:59:22 +02007615 eval_literal(exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007616 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007617 eval_literal(exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007618 }
7619
7620 parent_pos_pred = 1;
7621 goto predicate;
7622
7623 case LYXP_TOKEN_NUMBER:
7624 /* Number */
7625 if (!set || (options & LYXP_SCNODE_ALL)) {
7626 if (set) {
7627 set_scnode_clear_ctx(set);
7628 }
Michal Vasko004d3152020-06-11 19:59:22 +02007629 rc = eval_number(NULL, exp, tok_idx, NULL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007630 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02007631 rc = eval_number(set->ctx, exp, tok_idx, set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007632 }
7633 LY_CHECK_RET(rc);
7634
7635 parent_pos_pred = 1;
7636 goto predicate;
7637
7638 default:
Michal Vasko004d3152020-06-11 19:59:22 +02007639 LOGVAL(set->ctx, LY_VLOG_LYD, set->ctx_node, LY_VCODE_XP_INTOK, lyxp_print_token(exp->tokens[*tok_idx]),
7640 &exp->expr[exp->tok_pos[*tok_idx]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007641 return LY_EVALID;
7642 }
7643
7644 return LY_SUCCESS;
7645
7646predicate:
7647 /* Predicate* */
Michal Vasko004d3152020-06-11 19:59:22 +02007648 while (!lyxp_check_token(NULL, exp, *tok_idx, LYXP_TOKEN_BRACK1)) {
7649 rc = eval_predicate(exp, tok_idx, set, options, parent_pos_pred);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007650 LY_CHECK_RET(rc);
7651 }
7652
7653 /* ('/' or '//') RelativeLocationPath */
Michal Vasko004d3152020-06-11 19:59:22 +02007654 if (!exp_check_token2(NULL, exp, *tok_idx, LYXP_TOKEN_OPER_PATH, LYXP_TOKEN_OPER_RPATH)) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007655
7656 /* evaluate '/' or '//' */
Michal Vasko004d3152020-06-11 19:59:22 +02007657 if (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_PATH) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007658 all_desc = 0;
7659 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02007660 all_desc = 1;
7661 }
7662
7663 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007664 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7665 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007666
Michal Vasko004d3152020-06-11 19:59:22 +02007667 rc = eval_relative_location_path(exp, tok_idx, all_desc, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007668 LY_CHECK_RET(rc);
7669 }
7670
7671 return LY_SUCCESS;
7672}
7673
7674/**
7675 * @brief Evaluate UnionExpr. Logs directly on error.
7676 *
Michal Vaskod3678892020-05-21 10:06:58 +02007677 * [20] UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007678 *
7679 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007680 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007681 * @param[in] repeat How many times this expression is repeated.
7682 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7683 * @param[in] options XPath options.
7684 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7685 */
7686static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02007687eval_union_expr(struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007688{
7689 LY_ERR rc = LY_SUCCESS;
7690 struct lyxp_set orig_set, set2;
7691 uint16_t i;
7692
7693 assert(repeat);
7694
7695 set_init(&orig_set, set);
7696 set_init(&set2, set);
7697
7698 set_fill_set(&orig_set, set);
7699
Michal Vasko004d3152020-06-11 19:59:22 +02007700 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007701 LY_CHECK_GOTO(rc, cleanup);
7702
7703 /* ('|' PathExpr)* */
7704 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007705 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_UNI);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007706 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007707 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7708 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007709
7710 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007711 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007712 LY_CHECK_GOTO(rc, cleanup);
7713 continue;
7714 }
7715
7716 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02007717 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNION, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007718 LY_CHECK_GOTO(rc, cleanup);
7719
7720 /* eval */
7721 if (options & LYXP_SCNODE_ALL) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01007722 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007723 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007724 rc = moveto_union(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007725 LY_CHECK_GOTO(rc, cleanup);
7726 }
7727 }
7728
7729cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007730 lyxp_set_free_content(&orig_set);
7731 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007732 return rc;
7733}
7734
7735/**
7736 * @brief Evaluate UnaryExpr. Logs directly on error.
7737 *
Michal Vaskod3678892020-05-21 10:06:58 +02007738 * [19] UnaryExpr ::= UnionExpr | '-' UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007739 *
7740 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007741 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007742 * @param[in] repeat How many times this expression is repeated.
7743 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7744 * @param[in] options XPath options.
7745 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7746 */
7747static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02007748eval_unary_expr(struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007749{
7750 LY_ERR rc;
7751 uint16_t this_op, i;
7752
7753 assert(repeat);
7754
7755 /* ('-')+ */
Michal Vasko004d3152020-06-11 19:59:22 +02007756 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007757 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007758 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 +02007759
7760 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007761 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7762 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007763 }
7764
Michal Vasko004d3152020-06-11 19:59:22 +02007765 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_UNARY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007766 LY_CHECK_RET(rc);
7767
7768 if (set && (repeat % 2)) {
7769 if (options & LYXP_SCNODE_ALL) {
7770 warn_operands(set->ctx, set, NULL, 1, exp->expr, exp->tok_pos[this_op]);
7771 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007772 rc = moveto_op_math(set, NULL, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007773 LY_CHECK_RET(rc);
7774 }
7775 }
7776
7777 return LY_SUCCESS;
7778}
7779
7780/**
7781 * @brief Evaluate MultiplicativeExpr. Logs directly on error.
7782 *
Michal Vaskod3678892020-05-21 10:06:58 +02007783 * [18] MultiplicativeExpr ::= UnaryExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007784 * | MultiplicativeExpr '*' UnaryExpr
7785 * | MultiplicativeExpr 'div' UnaryExpr
7786 * | MultiplicativeExpr 'mod' UnaryExpr
7787 *
7788 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007789 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007790 * @param[in] repeat How many times this expression is repeated.
7791 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7792 * @param[in] options XPath options.
7793 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7794 */
7795static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02007796eval_multiplicative_expr(struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007797{
7798 LY_ERR rc;
7799 uint16_t this_op;
7800 struct lyxp_set orig_set, set2;
7801 uint16_t i;
7802
7803 assert(repeat);
7804
7805 set_init(&orig_set, set);
7806 set_init(&set2, set);
7807
7808 set_fill_set(&orig_set, set);
7809
Michal Vasko004d3152020-06-11 19:59:22 +02007810 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007811 LY_CHECK_GOTO(rc, cleanup);
7812
7813 /* ('*' / 'div' / 'mod' UnaryExpr)* */
7814 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007815 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007816
Michal Vasko004d3152020-06-11 19:59:22 +02007817 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007818 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007819 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7820 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007821
7822 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007823 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007824 LY_CHECK_GOTO(rc, cleanup);
7825 continue;
7826 }
7827
7828 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02007829 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_MULTIPLICATIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007830 LY_CHECK_GOTO(rc, cleanup);
7831
7832 /* eval */
7833 if (options & LYXP_SCNODE_ALL) {
7834 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007835 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007836 set_scnode_clear_ctx(set);
7837 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007838 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007839 LY_CHECK_GOTO(rc, cleanup);
7840 }
7841 }
7842
7843cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007844 lyxp_set_free_content(&orig_set);
7845 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007846 return rc;
7847}
7848
7849/**
7850 * @brief Evaluate AdditiveExpr. Logs directly on error.
7851 *
Michal Vaskod3678892020-05-21 10:06:58 +02007852 * [17] AdditiveExpr ::= MultiplicativeExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007853 * | AdditiveExpr '+' MultiplicativeExpr
7854 * | AdditiveExpr '-' MultiplicativeExpr
7855 *
7856 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007857 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007858 * @param[in] repeat How many times this expression is repeated.
7859 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7860 * @param[in] options XPath options.
7861 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7862 */
7863static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02007864eval_additive_expr(struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007865{
7866 LY_ERR rc;
7867 uint16_t this_op;
7868 struct lyxp_set orig_set, set2;
7869 uint16_t i;
7870
7871 assert(repeat);
7872
7873 set_init(&orig_set, set);
7874 set_init(&set2, set);
7875
7876 set_fill_set(&orig_set, set);
7877
Michal Vasko004d3152020-06-11 19:59:22 +02007878 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007879 LY_CHECK_GOTO(rc, cleanup);
7880
7881 /* ('+' / '-' MultiplicativeExpr)* */
7882 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007883 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007884
Michal Vasko004d3152020-06-11 19:59:22 +02007885 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_MATH);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007886 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007887 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7888 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007889
7890 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007891 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007892 LY_CHECK_GOTO(rc, cleanup);
7893 continue;
7894 }
7895
7896 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02007897 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_ADDITIVE, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007898 LY_CHECK_GOTO(rc, cleanup);
7899
7900 /* eval */
7901 if (options & LYXP_SCNODE_ALL) {
7902 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007903 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007904 set_scnode_clear_ctx(set);
7905 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01007906 rc = moveto_op_math(set, &set2, &exp->expr[exp->tok_pos[this_op]]);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007907 LY_CHECK_GOTO(rc, cleanup);
7908 }
7909 }
7910
7911cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007912 lyxp_set_free_content(&orig_set);
7913 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007914 return rc;
7915}
7916
7917/**
7918 * @brief Evaluate RelationalExpr. Logs directly on error.
7919 *
Michal Vaskod3678892020-05-21 10:06:58 +02007920 * [16] RelationalExpr ::= AdditiveExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007921 * | RelationalExpr '<' AdditiveExpr
7922 * | RelationalExpr '>' AdditiveExpr
7923 * | RelationalExpr '<=' AdditiveExpr
7924 * | RelationalExpr '>=' AdditiveExpr
7925 *
7926 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007927 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007928 * @param[in] repeat How many times this expression is repeated.
7929 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7930 * @param[in] options XPath options.
7931 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7932 */
7933static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02007934eval_relational_expr(struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02007935{
7936 LY_ERR rc;
7937 uint16_t this_op;
7938 struct lyxp_set orig_set, set2;
7939 uint16_t i;
7940
7941 assert(repeat);
7942
7943 set_init(&orig_set, set);
7944 set_init(&set2, set);
7945
7946 set_fill_set(&orig_set, set);
7947
Michal Vasko004d3152020-06-11 19:59:22 +02007948 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007949 LY_CHECK_GOTO(rc, cleanup);
7950
7951 /* ('<' / '>' / '<=' / '>=' AdditiveExpr)* */
7952 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02007953 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02007954
Michal Vasko004d3152020-06-11 19:59:22 +02007955 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_COMP);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007956 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02007957 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
7958 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007959
7960 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02007961 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007962 LY_CHECK_GOTO(rc, cleanup);
7963 continue;
7964 }
7965
7966 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02007967 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_RELATIONAL, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007968 LY_CHECK_GOTO(rc, cleanup);
7969
7970 /* eval */
7971 if (options & LYXP_SCNODE_ALL) {
7972 warn_operands(set->ctx, set, &set2, 1, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007973 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007974 set_scnode_clear_ctx(set);
7975 } else {
7976 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options);
7977 LY_CHECK_GOTO(rc, cleanup);
7978 }
7979 }
7980
7981cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02007982 lyxp_set_free_content(&orig_set);
7983 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02007984 return rc;
7985}
7986
7987/**
7988 * @brief Evaluate EqualityExpr. Logs directly on error.
7989 *
Michal Vaskod3678892020-05-21 10:06:58 +02007990 * [15] EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02007991 * | EqualityExpr '!=' RelationalExpr
7992 *
7993 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02007994 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02007995 * @param[in] repeat How many times this expression is repeated.
7996 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
7997 * @param[in] options XPath options.
7998 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
7999 */
8000static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02008001eval_equality_expr(struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008002{
8003 LY_ERR rc;
8004 uint16_t this_op;
8005 struct lyxp_set orig_set, set2;
8006 uint16_t i;
8007
8008 assert(repeat);
8009
8010 set_init(&orig_set, set);
8011 set_init(&set2, set);
8012
8013 set_fill_set(&orig_set, set);
8014
Michal Vasko004d3152020-06-11 19:59:22 +02008015 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008016 LY_CHECK_GOTO(rc, cleanup);
8017
8018 /* ('=' / '!=' RelationalExpr)* */
8019 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008020 this_op = *tok_idx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008021
Michal Vasko004d3152020-06-11 19:59:22 +02008022 assert((exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_EQUAL) || (exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_NEQUAL));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008023 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (set ? "parsed" : "skipped"),
Michal Vasko004d3152020-06-11 19:59:22 +02008024 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
8025 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008026
8027 if (!set) {
Michal Vasko004d3152020-06-11 19:59:22 +02008028 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008029 LY_CHECK_GOTO(rc, cleanup);
8030 continue;
8031 }
8032
8033 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008034 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_EQUALITY, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008035 LY_CHECK_GOTO(rc, cleanup);
8036
8037 /* eval */
8038 if (options & LYXP_SCNODE_ALL) {
8039 warn_operands(set->ctx, set, &set2, 0, exp->expr, exp->tok_pos[this_op - 1]);
Michal Vasko004d3152020-06-11 19:59:22 +02008040 warn_equality_value(exp, set, *tok_idx - 1, this_op - 1, *tok_idx - 1);
8041 warn_equality_value(exp, &set2, this_op - 1, this_op - 1, *tok_idx - 1);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008042 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008043 set_scnode_clear_ctx(set);
8044 } else {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008045 rc = moveto_op_comp(set, &set2, &exp->expr[exp->tok_pos[this_op]], options);
8046 LY_CHECK_GOTO(rc, cleanup);
8047 }
8048 }
8049
8050cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008051 lyxp_set_free_content(&orig_set);
8052 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008053 return rc;
8054}
8055
8056/**
8057 * @brief Evaluate AndExpr. Logs directly on error.
8058 *
Michal Vaskod3678892020-05-21 10:06:58 +02008059 * [14] AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008060 *
8061 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008062 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008063 * @param[in] repeat How many times this expression is repeated.
8064 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8065 * @param[in] options XPath options.
8066 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8067 */
8068static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02008069eval_and_expr(struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008070{
8071 LY_ERR rc;
8072 struct lyxp_set orig_set, set2;
8073 uint16_t i;
8074
8075 assert(repeat);
8076
8077 set_init(&orig_set, set);
8078 set_init(&set2, set);
8079
8080 set_fill_set(&orig_set, set);
8081
Michal Vasko004d3152020-06-11 19:59:22 +02008082 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008083 LY_CHECK_GOTO(rc, cleanup);
8084
8085 /* cast to boolean, we know that will be the final result */
8086 if (set && (options & LYXP_SCNODE_ALL)) {
8087 set_scnode_clear_ctx(set);
8088 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008089 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008090 }
8091
8092 /* ('and' EqualityExpr)* */
8093 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008094 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
8095 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (!set || !set->val.bln ? "skipped" : "parsed"),
8096 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
8097 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008098
8099 /* lazy evaluation */
Michal Vasko004d3152020-06-11 19:59:22 +02008100 if (!set || ((set->type == LYXP_SET_BOOLEAN) && !set->val.bln)) {
8101 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008102 LY_CHECK_GOTO(rc, cleanup);
8103 continue;
8104 }
8105
8106 set_fill_set(&set2, &orig_set);
Michal Vasko004d3152020-06-11 19:59:22 +02008107 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_AND, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008108 LY_CHECK_GOTO(rc, cleanup);
8109
8110 /* eval - just get boolean value actually */
8111 if (set->type == LYXP_SET_SCNODE_SET) {
8112 set_scnode_clear_ctx(&set2);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008113 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008114 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008115 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008116 set_fill_set(set, &set2);
8117 }
8118 }
8119
8120cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008121 lyxp_set_free_content(&orig_set);
8122 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008123 return rc;
8124}
8125
8126/**
8127 * @brief Evaluate OrExpr. Logs directly on error.
8128 *
Michal Vaskod3678892020-05-21 10:06:58 +02008129 * [13] OrExpr ::= AndExpr | OrExpr 'or' AndExpr
Michal Vasko03ff5a72019-09-11 13:49:33 +02008130 *
8131 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008132 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008133 * @param[in] repeat How many times this expression is repeated.
8134 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8135 * @param[in] options XPath options.
8136 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8137 */
8138static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02008139eval_or_expr(struct lyxp_expr *exp, uint16_t *tok_idx, uint16_t repeat, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008140{
8141 LY_ERR rc;
8142 struct lyxp_set orig_set, set2;
8143 uint16_t i;
8144
8145 assert(repeat);
8146
8147 set_init(&orig_set, set);
8148 set_init(&set2, set);
8149
8150 set_fill_set(&orig_set, set);
8151
Michal Vasko004d3152020-06-11 19:59:22 +02008152 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008153 LY_CHECK_GOTO(rc, cleanup);
8154
8155 /* cast to boolean, we know that will be the final result */
8156 if (set && (options & LYXP_SCNODE_ALL)) {
8157 set_scnode_clear_ctx(set);
8158 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008159 lyxp_set_cast(set, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008160 }
8161
8162 /* ('or' AndExpr)* */
8163 for (i = 0; i < repeat; ++i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008164 assert(exp->tokens[*tok_idx] == LYXP_TOKEN_OPER_LOG);
8165 LOGDBG(LY_LDGXPATH, "%-27s %s %s[%u]", __func__, (!set || set->val.bln ? "skipped" : "parsed"),
8166 lyxp_print_token(exp->tokens[*tok_idx]), exp->tok_pos[*tok_idx]);
8167 ++(*tok_idx);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008168
8169 /* lazy evaluation */
Michal Vasko004d3152020-06-11 19:59:22 +02008170 if (!set || ((set->type == LYXP_SET_BOOLEAN) && set->val.bln)) {
8171 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, NULL, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008172 LY_CHECK_GOTO(rc, cleanup);
8173 continue;
8174 }
8175
8176 set_fill_set(&set2, &orig_set);
8177 /* expr_type cound have been LYXP_EXPR_NONE in all these later calls (except for the first one),
8178 * but it does not matter */
Michal Vasko004d3152020-06-11 19:59:22 +02008179 rc = eval_expr_select(exp, tok_idx, LYXP_EXPR_OR, &set2, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008180 LY_CHECK_GOTO(rc, cleanup);
8181
8182 /* eval - just get boolean value actually */
8183 if (set->type == LYXP_SET_SCNODE_SET) {
8184 set_scnode_clear_ctx(&set2);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008185 lyxp_set_scnode_merge(set, &set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008186 } else {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008187 lyxp_set_cast(&set2, LYXP_SET_BOOLEAN);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008188 set_fill_set(set, &set2);
8189 }
8190 }
8191
8192cleanup:
Michal Vaskod3678892020-05-21 10:06:58 +02008193 lyxp_set_free_content(&orig_set);
8194 lyxp_set_free_content(&set2);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008195 return rc;
8196}
8197
8198/**
Michal Vasko004d3152020-06-11 19:59:22 +02008199 * @brief Decide what expression is at the pointer @p tok_idx and evaluate it accordingly.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008200 *
8201 * @param[in] exp Parsed XPath expression.
Michal Vasko004d3152020-06-11 19:59:22 +02008202 * @param[in] tok_idx Position in the expression @p exp.
Michal Vasko03ff5a72019-09-11 13:49:33 +02008203 * @param[in] etype Expression type to evaluate.
8204 * @param[in,out] set Context and result set. On NULL the rule is only parsed.
8205 * @param[in] options XPath options.
8206 * @return LY_ERR (LY_EINCOMPLETE on unresolved when)
8207 */
8208static LY_ERR
Radek Krejci1deb5be2020-08-26 16:43:36 +02008209eval_expr_select(struct lyxp_expr *exp, uint16_t *tok_idx, enum lyxp_expr_type etype, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008210{
8211 uint16_t i, count;
8212 enum lyxp_expr_type next_etype;
8213 LY_ERR rc;
8214
8215 /* process operator repeats */
Michal Vasko004d3152020-06-11 19:59:22 +02008216 if (!exp->repeat[*tok_idx]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008217 next_etype = LYXP_EXPR_NONE;
8218 } else {
8219 /* find etype repeat */
Radek Krejci1e008d22020-08-17 11:37:37 +02008220 for (i = 0; exp->repeat[*tok_idx][i] > etype; ++i) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02008221
8222 /* select one-priority lower because etype expression called us */
8223 if (i) {
Michal Vasko004d3152020-06-11 19:59:22 +02008224 next_etype = exp->repeat[*tok_idx][i - 1];
Michal Vasko03ff5a72019-09-11 13:49:33 +02008225 /* count repeats for that expression */
Radek Krejci1e008d22020-08-17 11:37:37 +02008226 for (count = 0; i && exp->repeat[*tok_idx][i - 1] == next_etype; ++count, --i) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02008227 } else {
8228 next_etype = LYXP_EXPR_NONE;
8229 }
8230 }
8231
8232 /* decide what expression are we parsing based on the repeat */
8233 switch (next_etype) {
8234 case LYXP_EXPR_OR:
Michal Vasko004d3152020-06-11 19:59:22 +02008235 rc = eval_or_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008236 break;
8237 case LYXP_EXPR_AND:
Michal Vasko004d3152020-06-11 19:59:22 +02008238 rc = eval_and_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008239 break;
8240 case LYXP_EXPR_EQUALITY:
Michal Vasko004d3152020-06-11 19:59:22 +02008241 rc = eval_equality_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008242 break;
8243 case LYXP_EXPR_RELATIONAL:
Michal Vasko004d3152020-06-11 19:59:22 +02008244 rc = eval_relational_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008245 break;
8246 case LYXP_EXPR_ADDITIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02008247 rc = eval_additive_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008248 break;
8249 case LYXP_EXPR_MULTIPLICATIVE:
Michal Vasko004d3152020-06-11 19:59:22 +02008250 rc = eval_multiplicative_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008251 break;
8252 case LYXP_EXPR_UNARY:
Michal Vasko004d3152020-06-11 19:59:22 +02008253 rc = eval_unary_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008254 break;
8255 case LYXP_EXPR_UNION:
Michal Vasko004d3152020-06-11 19:59:22 +02008256 rc = eval_union_expr(exp, tok_idx, count, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008257 break;
8258 case LYXP_EXPR_NONE:
Michal Vasko004d3152020-06-11 19:59:22 +02008259 rc = eval_path_expr(exp, tok_idx, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008260 break;
8261 default:
8262 LOGINT_RET(set->ctx);
8263 }
8264
8265 return rc;
8266}
8267
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008268/**
8269 * @brief Get root type.
8270 *
8271 * @param[in] ctx_node Context node.
8272 * @param[in] ctx_scnode Schema context node.
8273 * @param[in] options XPath options.
8274 * @return Root type.
8275 */
8276static enum lyxp_node_type
Radek Krejci1deb5be2020-08-26 16:43:36 +02008277lyxp_get_root_type(const struct lyd_node *ctx_node, const struct lysc_node *ctx_scnode, uint32_t options)
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008278{
Michal Vasko6b26e742020-07-17 15:02:10 +02008279 const struct lysc_node *op;
8280
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008281 if (options & LYXP_SCNODE_ALL) {
Radek Krejci1e008d22020-08-17 11:37:37 +02008282 for (op = ctx_scnode; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02008283
8284 if (op || (options & LYXP_SCNODE)) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008285 /* general root that can access everything */
8286 return LYXP_NODE_ROOT;
8287 } else if (!ctx_scnode || (ctx_scnode->flags & LYS_CONFIG_W)) {
8288 /* root context node can access only config data (because we said so, it is unspecified) */
8289 return LYXP_NODE_ROOT_CONFIG;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008290 }
Michal Vasko6b26e742020-07-17 15:02:10 +02008291 return LYXP_NODE_ROOT;
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008292 }
8293
Michal Vasko6b26e742020-07-17 15:02:10 +02008294 op = ctx_node ? ctx_node->schema : NULL;
Michal Vaskod989ba02020-08-24 10:59:24 +02008295 for ( ; op && !(op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)); op = op->parent) {}
Michal Vasko6b26e742020-07-17 15:02:10 +02008296
8297 if (!ctx_node || (!op && (ctx_node->schema->flags & LYS_CONFIG_W))) {
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008298 /* root context node can access only config data (because we said so, it is unspecified) */
8299 return LYXP_NODE_ROOT_CONFIG;
8300 }
8301
8302 return LYXP_NODE_ROOT;
8303}
8304
Michal Vasko03ff5a72019-09-11 13:49:33 +02008305LY_ERR
Michal Vaskoc8a230d2020-08-14 12:17:10 +02008306lyxp_eval(struct lyxp_expr *exp, LY_PREFIX_FORMAT format, const struct lys_module *local_mod, const struct lyd_node *ctx_node,
Radek Krejci1deb5be2020-08-26 16:43:36 +02008307 enum lyxp_node_type ctx_node_type, const struct lyd_node *tree, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008308{
Michal Vasko004d3152020-06-11 19:59:22 +02008309 uint16_t tok_idx = 0;
8310 const struct lyd_node *real_ctx_node;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008311 LY_ERR rc;
8312
Michal Vasko004d3152020-06-11 19:59:22 +02008313 LY_CHECK_ARG_RET(NULL, exp, local_mod, ctx_node, set, LY_EINVAL);
8314
8315 if ((ctx_node_type == LYXP_NODE_ROOT) || (ctx_node_type == LYXP_NODE_ROOT_CONFIG)) {
8316 /* we always need some context node because it is used for resolving unqualified names */
8317 real_ctx_node = NULL;
8318 } else {
8319 real_ctx_node = ctx_node;
8320 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008321
8322 /* prepare set for evaluation */
Michal Vasko004d3152020-06-11 19:59:22 +02008323 tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008324 memset(set, 0, sizeof *set);
Michal Vaskod3678892020-05-21 10:06:58 +02008325 set->type = LYXP_SET_NODE_SET;
Michal Vasko004d3152020-06-11 19:59:22 +02008326 set_insert_node(set, (struct lyd_node *)real_ctx_node, 0, ctx_node_type, 0);
Michal Vaskoecd62de2019-11-13 12:35:11 +01008327 set->ctx = local_mod->ctx;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008328 set->ctx_node = ctx_node;
Michal Vasko004d3152020-06-11 19:59:22 +02008329 set->root_type = lyxp_get_root_type(real_ctx_node, NULL, options);
Michal Vasko6b26e742020-07-17 15:02:10 +02008330 for (set->context_op = ctx_node->schema;
Radek Krejci0f969882020-08-21 16:56:47 +02008331 set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF));
8332 set->context_op = set->context_op->parent) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02008333 set->local_mod = local_mod;
Michal Vaskof03ed032020-03-04 13:31:44 +01008334 set->tree = tree;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008335 set->format = format;
8336
8337 /* evaluate */
Michal Vasko004d3152020-06-11 19:59:22 +02008338 rc = eval_expr_select(exp, &tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008339 if (rc != LY_SUCCESS) {
Michal Vaskod3678892020-05-21 10:06:58 +02008340 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008341 }
8342
Michal Vasko03ff5a72019-09-11 13:49:33 +02008343 return rc;
8344}
8345
8346#if 0
8347
8348/* full xml printing of set elements, not used currently */
8349
8350void
8351lyxp_set_print_xml(FILE *f, struct lyxp_set *set)
8352{
8353 uint32_t i;
8354 char *str_num;
8355 struct lyout out;
8356
8357 memset(&out, 0, sizeof out);
8358
8359 out.type = LYOUT_STREAM;
8360 out.method.f = f;
8361
8362 switch (set->type) {
8363 case LYXP_SET_EMPTY:
Michal Vasko5233e962020-08-14 14:26:20 +02008364 ly_print_(&out, "Empty XPath set\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008365 break;
8366 case LYXP_SET_BOOLEAN:
Michal Vasko5233e962020-08-14 14:26:20 +02008367 ly_print_(&out, "Boolean XPath set:\n");
8368 ly_print_(&out, "%s\n\n", set->value.bool ? "true" : "false");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008369 break;
8370 case LYXP_SET_STRING:
Michal Vasko5233e962020-08-14 14:26:20 +02008371 ly_print_(&out, "String XPath set:\n");
8372 ly_print_(&out, "\"%s\"\n\n", set->value.str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008373 break;
8374 case LYXP_SET_NUMBER:
Michal Vasko5233e962020-08-14 14:26:20 +02008375 ly_print_(&out, "Number XPath set:\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008376
8377 if (isnan(set->value.num)) {
8378 str_num = strdup("NaN");
8379 } else if ((set->value.num == 0) || (set->value.num == -0.0f)) {
8380 str_num = strdup("0");
8381 } else if (isinf(set->value.num) && !signbit(set->value.num)) {
8382 str_num = strdup("Infinity");
8383 } else if (isinf(set->value.num) && signbit(set->value.num)) {
8384 str_num = strdup("-Infinity");
8385 } else if ((long long)set->value.num == set->value.num) {
8386 if (asprintf(&str_num, "%lld", (long long)set->value.num) == -1) {
8387 str_num = NULL;
8388 }
8389 } else {
8390 if (asprintf(&str_num, "%03.1Lf", set->value.num) == -1) {
8391 str_num = NULL;
8392 }
8393 }
8394 if (!str_num) {
8395 LOGMEM;
8396 return;
8397 }
Michal Vasko5233e962020-08-14 14:26:20 +02008398 ly_print_(&out, "%s\n\n", str_num);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008399 free(str_num);
8400 break;
8401 case LYXP_SET_NODE_SET:
Michal Vasko5233e962020-08-14 14:26:20 +02008402 ly_print_(&out, "Node XPath set:\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008403
8404 for (i = 0; i < set->used; ++i) {
Michal Vasko5233e962020-08-14 14:26:20 +02008405 ly_print_(&out, "%d. ", i + 1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008406 switch (set->node_type[i]) {
8407 case LYXP_NODE_ROOT_ALL:
Michal Vasko5233e962020-08-14 14:26:20 +02008408 ly_print_(&out, "ROOT all\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008409 break;
8410 case LYXP_NODE_ROOT_CONFIG:
Michal Vasko5233e962020-08-14 14:26:20 +02008411 ly_print_(&out, "ROOT config\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008412 break;
8413 case LYXP_NODE_ROOT_STATE:
Michal Vasko5233e962020-08-14 14:26:20 +02008414 ly_print_(&out, "ROOT state\n\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008415 break;
8416 case LYXP_NODE_ROOT_NOTIF:
Michal Vasko5233e962020-08-14 14:26:20 +02008417 ly_print_(&out, "ROOT notification \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008418 break;
8419 case LYXP_NODE_ROOT_RPC:
Michal Vasko5233e962020-08-14 14:26:20 +02008420 ly_print_(&out, "ROOT rpc \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008421 break;
8422 case LYXP_NODE_ROOT_OUTPUT:
Michal Vasko5233e962020-08-14 14:26:20 +02008423 ly_print_(&out, "ROOT output \"%s\"\n\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008424 break;
8425 case LYXP_NODE_ELEM:
Michal Vasko5233e962020-08-14 14:26:20 +02008426 ly_print_(&out, "ELEM \"%s\"\n", set->value.nodes[i]->schema->name);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008427 xml_print_node(&out, 1, set->value.nodes[i], 1, LYP_FORMAT);
Michal Vasko5233e962020-08-14 14:26:20 +02008428 ly_print_(&out, "\n");
Michal Vasko03ff5a72019-09-11 13:49:33 +02008429 break;
8430 case LYXP_NODE_TEXT:
Michal Vasko5233e962020-08-14 14:26:20 +02008431 ly_print_(&out, "TEXT \"%s\"\n\n", ((struct lyd_node_leaf_list *)set->value.nodes[i])->value_str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008432 break;
8433 case LYXP_NODE_ATTR:
Michal Vasko5233e962020-08-14 14:26:20 +02008434 ly_print_(&out, "ATTR \"%s\" = \"%s\"\n\n", set->value.attrs[i]->name, set->value.attrs[i]->value);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008435 break;
8436 }
8437 }
8438 break;
8439 }
8440}
8441
8442#endif
8443
8444LY_ERR
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008445lyxp_set_cast(struct lyxp_set *set, enum lyxp_set_type target)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008446{
8447 long double num;
8448 char *str;
8449 LY_ERR rc;
8450
8451 if (!set || (set->type == target)) {
8452 return LY_SUCCESS;
8453 }
8454
8455 /* it's not possible to convert anything into a node set */
Michal Vaskod3678892020-05-21 10:06:58 +02008456 assert(target != LYXP_SET_NODE_SET);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008457
8458 if (set->type == LYXP_SET_SCNODE_SET) {
Michal Vaskod3678892020-05-21 10:06:58 +02008459 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008460 return LY_EINVAL;
8461 }
8462
8463 /* to STRING */
Michal Vaskod3678892020-05-21 10:06:58 +02008464 if ((target == LYXP_SET_STRING) || ((target == LYXP_SET_NUMBER) && (set->type == LYXP_SET_NODE_SET))) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008465 switch (set->type) {
8466 case LYXP_SET_NUMBER:
8467 if (isnan(set->val.num)) {
8468 set->val.str = strdup("NaN");
8469 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8470 } else if ((set->val.num == 0) || (set->val.num == -0.0f)) {
8471 set->val.str = strdup("0");
8472 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8473 } else if (isinf(set->val.num) && !signbit(set->val.num)) {
8474 set->val.str = strdup("Infinity");
8475 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8476 } else if (isinf(set->val.num) && signbit(set->val.num)) {
8477 set->val.str = strdup("-Infinity");
8478 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), -1);
8479 } else if ((long long)set->val.num == set->val.num) {
8480 if (asprintf(&str, "%lld", (long long)set->val.num) == -1) {
8481 LOGMEM_RET(set->ctx);
8482 }
8483 set->val.str = str;
8484 } else {
8485 if (asprintf(&str, "%03.1Lf", set->val.num) == -1) {
8486 LOGMEM_RET(set->ctx);
8487 }
8488 set->val.str = str;
8489 }
8490 break;
8491 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02008492 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008493 set->val.str = strdup("true");
8494 } else {
8495 set->val.str = strdup("false");
8496 }
8497 LY_CHECK_ERR_RET(!set->val.str, LOGMEM(set->ctx), LY_EMEM);
8498 break;
8499 case LYXP_SET_NODE_SET:
8500 assert(set->used);
8501
8502 /* we need the set sorted, it affects the result */
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008503 assert(!set_sort(set));
Michal Vasko03ff5a72019-09-11 13:49:33 +02008504
Michal Vasko5e0e6eb2019-11-06 15:47:50 +01008505 rc = cast_node_set_to_string(set, &str);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008506 LY_CHECK_RET(rc);
Michal Vaskod3678892020-05-21 10:06:58 +02008507 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008508 set->val.str = str;
8509 break;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008510 default:
8511 LOGINT_RET(set->ctx);
8512 }
8513 set->type = LYXP_SET_STRING;
8514 }
8515
8516 /* to NUMBER */
8517 if (target == LYXP_SET_NUMBER) {
8518 switch (set->type) {
8519 case LYXP_SET_STRING:
8520 num = cast_string_to_number(set->val.str);
Michal Vaskod3678892020-05-21 10:06:58 +02008521 lyxp_set_free_content(set);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008522 set->val.num = num;
8523 break;
8524 case LYXP_SET_BOOLEAN:
Michal Vasko004d3152020-06-11 19:59:22 +02008525 if (set->val.bln) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02008526 set->val.num = 1;
8527 } else {
8528 set->val.num = 0;
8529 }
8530 break;
8531 default:
8532 LOGINT_RET(set->ctx);
8533 }
8534 set->type = LYXP_SET_NUMBER;
8535 }
8536
8537 /* to BOOLEAN */
8538 if (target == LYXP_SET_BOOLEAN) {
8539 switch (set->type) {
8540 case LYXP_SET_NUMBER:
8541 if ((set->val.num == 0) || (set->val.num == -0.0f) || isnan(set->val.num)) {
Michal Vasko004d3152020-06-11 19:59:22 +02008542 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008543 } else {
Michal Vasko004d3152020-06-11 19:59:22 +02008544 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008545 }
8546 break;
8547 case LYXP_SET_STRING:
8548 if (set->val.str[0]) {
Michal Vaskod3678892020-05-21 10:06:58 +02008549 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008550 set->val.bln = 1;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008551 } else {
Michal Vaskod3678892020-05-21 10:06:58 +02008552 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008553 set->val.bln = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008554 }
8555 break;
8556 case LYXP_SET_NODE_SET:
Michal Vaskod3678892020-05-21 10:06:58 +02008557 if (set->used) {
8558 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008559 set->val.bln = 1;
Michal Vaskod3678892020-05-21 10:06:58 +02008560 } else {
8561 lyxp_set_free_content(set);
Michal Vasko004d3152020-06-11 19:59:22 +02008562 set->val.bln = 0;
Michal Vaskod3678892020-05-21 10:06:58 +02008563 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008564 break;
8565 default:
8566 LOGINT_RET(set->ctx);
8567 }
8568 set->type = LYXP_SET_BOOLEAN;
8569 }
8570
Michal Vasko03ff5a72019-09-11 13:49:33 +02008571 return LY_SUCCESS;
8572}
8573
8574LY_ERR
Michal Vaskoc8a230d2020-08-14 12:17:10 +02008575lyxp_atomize(struct lyxp_expr *exp, LY_PREFIX_FORMAT format, const struct lys_module *local_mod,
Radek Krejci1deb5be2020-08-26 16:43:36 +02008576 const struct lysc_node *ctx_scnode, enum lyxp_node_type ctx_scnode_type, struct lyxp_set *set, uint32_t options)
Michal Vasko03ff5a72019-09-11 13:49:33 +02008577{
8578 struct ly_ctx *ctx;
Michal Vasko004d3152020-06-11 19:59:22 +02008579 const struct lysc_node *real_ctx_scnode;
8580 uint16_t tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008581
Michal Vasko004d3152020-06-11 19:59:22 +02008582 LY_CHECK_ARG_RET(NULL, exp, local_mod, ctx_scnode, set, LY_EINVAL);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008583
8584 ctx = local_mod->ctx;
Michal Vasko004d3152020-06-11 19:59:22 +02008585 if ((ctx_scnode_type == LYXP_NODE_ROOT) || (ctx_scnode_type == LYXP_NODE_ROOT_CONFIG)) {
8586 /* we always need some context node because it is used for resolving unqualified names */
8587 real_ctx_scnode = NULL;
8588 } else {
8589 real_ctx_scnode = ctx_scnode;
8590 }
Michal Vasko03ff5a72019-09-11 13:49:33 +02008591
8592 /* prepare set for evaluation */
Michal Vasko004d3152020-06-11 19:59:22 +02008593 tok_idx = 0;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008594 memset(set, 0, sizeof *set);
8595 set->type = LYXP_SET_SCNODE_SET;
Michal Vasko004d3152020-06-11 19:59:22 +02008596 lyxp_set_scnode_insert_node(set, real_ctx_scnode, ctx_scnode_type);
Michal Vasko5c4e5892019-11-14 12:31:38 +01008597 set->val.scnodes[0].in_ctx = -2;
Michal Vasko03ff5a72019-09-11 13:49:33 +02008598 set->ctx = ctx;
8599 set->ctx_scnode = ctx_scnode;
Michal Vasko004d3152020-06-11 19:59:22 +02008600 set->root_type = lyxp_get_root_type(NULL, real_ctx_scnode, options);
Michal Vasko6b26e742020-07-17 15:02:10 +02008601 for (set->context_op = ctx_scnode;
Radek Krejci0f969882020-08-21 16:56:47 +02008602 set->context_op && !(set->context_op->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF));
8603 set->context_op = set->context_op->parent) {}
Michal Vasko03ff5a72019-09-11 13:49:33 +02008604 set->local_mod = local_mod;
8605 set->format = format;
8606
8607 /* evaluate */
Michal Vasko004d3152020-06-11 19:59:22 +02008608 return eval_expr_select(exp, &tok_idx, 0, set, options);
Michal Vasko03ff5a72019-09-11 13:49:33 +02008609}
Michal Vaskod43d71a2020-08-07 14:54:58 +02008610
8611API const char *
8612lyxp_get_expr(const struct lyxp_expr *path)
8613{
8614 if (!path) {
8615 return NULL;
8616 }
8617
8618 return path->expr;
8619}